Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md.h |
| 3 | * |
| 4 | * \brief Generic message digest wrapper |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
| 8 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 9 | * |
| 10 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 11 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 12 | * |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along |
| 26 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 28 | */ |
| 29 | |
| 30 | #ifndef POLARSSL_MD_H |
| 31 | #define POLARSSL_MD_H |
| 32 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Paul Bakker | 09b1ec6 | 2011-07-27 16:28:54 +0000 | [diff] [blame^] | 35 | #if defined(_MSC_VER) && !defined(inline) |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 36 | #define inline _inline |
Paul Bakker | 569df2c | 2011-06-21 07:48:07 +0000 | [diff] [blame] | 37 | #else |
Paul Bakker | 09b1ec6 | 2011-07-27 16:28:54 +0000 | [diff] [blame^] | 38 | #if defined(__ARMCC_VERSION) && !defined(inline) |
Paul Bakker | 569df2c | 2011-06-21 07:48:07 +0000 | [diff] [blame] | 39 | #define inline __inline |
Paul Bakker | 74fb74e | 2011-06-21 13:36:18 +0000 | [diff] [blame] | 40 | #endif /* __ARMCC_VERSION */ |
Paul Bakker | 569df2c | 2011-06-21 07:48:07 +0000 | [diff] [blame] | 41 | #endif /*_MSC_VER */ |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 43 | #define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 44 | #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ |
| 45 | #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ |
| 46 | #define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */ |
| 47 | #define POLARSSL_ERR_MD_FILE_READ_FAILED -0x5280 /**< Failure when reading from file. */ |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 48 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 49 | typedef enum { |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 50 | POLARSSL_MD_NONE=0, |
| 51 | POLARSSL_MD_MD2, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 52 | POLARSSL_MD_MD4, |
| 53 | POLARSSL_MD_MD5, |
| 54 | POLARSSL_MD_SHA1, |
| 55 | POLARSSL_MD_SHA224, |
| 56 | POLARSSL_MD_SHA256, |
| 57 | POLARSSL_MD_SHA384, |
| 58 | POLARSSL_MD_SHA512, |
| 59 | } md_type_t; |
| 60 | |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 61 | #define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
| 62 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Message digest information. Allows message digest functions to be called |
| 65 | * in a generic way. |
| 66 | */ |
| 67 | typedef struct { |
| 68 | /** Digest identifier */ |
| 69 | md_type_t type; |
| 70 | |
| 71 | /** Name of the message digest */ |
| 72 | const char * name; |
| 73 | |
| 74 | /** Output length of the digest function */ |
| 75 | int size; |
| 76 | |
| 77 | /** Digest initialisation function */ |
| 78 | void (*starts_func)( void *ctx ); |
| 79 | |
| 80 | /** Digest update function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 81 | void (*update_func)( void *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 82 | |
| 83 | /** Digest finalisation function */ |
| 84 | void (*finish_func)( void *ctx, unsigned char *output ); |
| 85 | |
| 86 | /** Generic digest function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 87 | void (*digest_func)( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 88 | unsigned char *output ); |
| 89 | |
| 90 | /** Generic file digest function */ |
| 91 | int (*file_func)( const char *path, unsigned char *output ); |
| 92 | |
| 93 | /** HMAC Initialisation function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 94 | void (*hmac_starts_func)( void *ctx, const unsigned char *key, size_t keylen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 95 | |
| 96 | /** HMAC update function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 97 | void (*hmac_update_func)( void *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 98 | |
| 99 | /** HMAC finalisation function */ |
| 100 | void (*hmac_finish_func)( void *ctx, unsigned char *output); |
| 101 | |
| 102 | /** HMAC context reset function */ |
| 103 | void (*hmac_reset_func)( void *ctx ); |
| 104 | |
| 105 | /** Generic HMAC function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 106 | void (*hmac_func)( const unsigned char *key, size_t keylen, |
| 107 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 108 | unsigned char *output ); |
| 109 | |
| 110 | /** Allocate a new context */ |
| 111 | void * (*ctx_alloc_func)( void ); |
| 112 | |
| 113 | /** Free the given context */ |
| 114 | void (*ctx_free_func)( void *ctx ); |
| 115 | |
| 116 | } md_info_t; |
| 117 | |
| 118 | /** |
| 119 | * Generic message digest context. |
| 120 | */ |
| 121 | typedef struct { |
| 122 | /** Information about the associated message digest */ |
| 123 | const md_info_t *md_info; |
| 124 | |
| 125 | /** Digest-specific context */ |
| 126 | void *md_ctx; |
| 127 | } md_context_t; |
| 128 | |
| 129 | #define MD_CONTEXT_T_INIT { \ |
| 130 | NULL, /* md_info */ \ |
| 131 | NULL, /* md_ctx */ \ |
| 132 | } |
| 133 | |
| 134 | #ifdef __cplusplus |
| 135 | extern "C" { |
| 136 | #endif |
| 137 | |
| 138 | /** |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 139 | * \brief Returns the list of digests supported by the generic digest module. |
| 140 | * |
| 141 | * \return a statically allocated array of digests, the last entry |
| 142 | * is 0. |
| 143 | */ |
| 144 | const int *md_list( void ); |
| 145 | |
| 146 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 147 | * \brief Returns the message digest information associated with the |
| 148 | * given digest name. |
| 149 | * |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 150 | * \param md_name Name of the digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 151 | * |
| 152 | * \return The message digest information associated with md_name or |
| 153 | * NULL if not found. |
| 154 | */ |
| 155 | const md_info_t *md_info_from_string( const char *md_name ); |
| 156 | |
| 157 | /** |
| 158 | * \brief Returns the message digest information associated with the |
| 159 | * given digest type. |
| 160 | * |
| 161 | * \param md_type type of digest to search for. |
| 162 | * |
| 163 | * \return The message digest information associated with md_type or |
| 164 | * NULL if not found. |
| 165 | */ |
| 166 | const md_info_t *md_info_from_type( md_type_t md_type ); |
| 167 | |
| 168 | /** |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 169 | * \brief Initialises and fills the message digest context structure with |
| 170 | * the appropriate values. |
| 171 | * |
| 172 | * \param ctx context to initialise. May not be NULL. The |
| 173 | * digest-specific context (ctx->md_ctx) must be NULL. It will |
| 174 | * be allocated, and must be freed using md_free_ctx() later. |
| 175 | * \param md_info message digest to use. |
| 176 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 177 | * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on |
| 178 | * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 179 | * allocation of the cipher-specific context failed. |
| 180 | */ |
| 181 | int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ); |
| 182 | |
| 183 | /** |
| 184 | * \brief Free the message-specific context of ctx. Freeing ctx itself |
| 185 | * remains the responsibility of the caller. |
| 186 | * |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 187 | * \param ctx Free the message-specific context |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 188 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 189 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 190 | * verification fails. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 191 | */ |
| 192 | int md_free_ctx( md_context_t *ctx ); |
| 193 | |
| 194 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 195 | * \brief Returns the size of the message digest output. |
| 196 | * |
| 197 | * \param md_info message digest info |
| 198 | * |
| 199 | * \return size of the message digest output. |
| 200 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 201 | static inline unsigned char md_get_size( const md_info_t *md_info ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 202 | { |
| 203 | return md_info->size; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * \brief Returns the type of the message digest output. |
| 208 | * |
| 209 | * \param md_info message digest info |
| 210 | * |
| 211 | * \return type of the message digest output. |
| 212 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 213 | static inline md_type_t md_get_type( const md_info_t *md_info ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 214 | { |
| 215 | return md_info->type; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * \brief Returns the name of the message digest output. |
| 220 | * |
| 221 | * \param md_info message digest info |
| 222 | * |
| 223 | * \return name of the message digest output. |
| 224 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 225 | static inline const char *md_get_name( const md_info_t *md_info ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 226 | { |
| 227 | return md_info->name; |
| 228 | } |
| 229 | |
| 230 | /** |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 231 | * \brief Set-up the given context for a new message digest |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 232 | * |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 233 | * \param ctx generic message digest context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 234 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 235 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 236 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 237 | */ |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 238 | int md_starts( md_context_t *ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * \brief Generic message digest process buffer |
| 242 | * |
| 243 | * \param ctx Generic message digest context |
| 244 | * \param input buffer holding the datal |
| 245 | * \param ilen length of the input data |
| 246 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 247 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 248 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 249 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 250 | int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * \brief Generic message digest final digest |
| 254 | * |
| 255 | * \param ctx Generic message digest context |
| 256 | * \param output Generic message digest checksum result |
| 257 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 258 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 259 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 260 | */ |
| 261 | int md_finish( md_context_t *ctx, unsigned char *output ); |
| 262 | |
| 263 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 264 | * \brief Output = message_digest( input buffer ) |
| 265 | * |
| 266 | * \param md_info message digest info |
| 267 | * \param input buffer holding the data |
| 268 | * \param ilen length of the input data |
| 269 | * \param output Generic message digest checksum result |
| 270 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 271 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 272 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 273 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 274 | int md( const md_info_t *md_info, const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 275 | unsigned char *output ); |
| 276 | |
| 277 | /** |
| 278 | * \brief Output = message_digest( file contents ) |
| 279 | * |
| 280 | * \param md_info message digest info |
| 281 | * \param path input file name |
| 282 | * \param output generic message digest checksum result |
| 283 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 284 | * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen |
| 285 | * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed, |
| 286 | * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 287 | */ |
| 288 | int md_file( const md_info_t *md_info, const char *path, unsigned char *output ); |
| 289 | |
| 290 | /** |
| 291 | * \brief Generic HMAC context setup |
| 292 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 293 | * \param ctx HMAC context to be initialized |
| 294 | * \param key HMAC secret key |
| 295 | * \param keylen length of the HMAC key |
| 296 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 297 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 298 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 299 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 300 | int md_hmac_starts( md_context_t *ctx, const unsigned char *key, size_t keylen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * \brief Generic HMAC process buffer |
| 304 | * |
| 305 | * \param ctx HMAC context |
| 306 | * \param input buffer holding the data |
| 307 | * \param ilen length of the input data |
| 308 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 309 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 310 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 311 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 312 | int md_hmac_update( md_context_t *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * \brief Generic HMAC final digest |
| 316 | * |
| 317 | * \param ctx HMAC context |
| 318 | * \param output Generic HMAC checksum result |
| 319 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 320 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 321 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 322 | */ |
| 323 | int md_hmac_finish( md_context_t *ctx, unsigned char *output); |
| 324 | |
| 325 | /** |
| 326 | * \brief Generic HMAC context reset |
| 327 | * |
| 328 | * \param ctx HMAC context to be reset |
| 329 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 330 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 331 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 332 | */ |
| 333 | int md_hmac_reset( md_context_t *ctx ); |
| 334 | |
| 335 | /** |
| 336 | * \brief Output = Generic_HMAC( hmac key, input buffer ) |
| 337 | * |
| 338 | * \param md_info message digest info |
| 339 | * \param key HMAC secret key |
| 340 | * \param keylen length of the HMAC key |
| 341 | * \param input buffer holding the data |
| 342 | * \param ilen length of the input data |
| 343 | * \param output Generic HMAC-result |
| 344 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 345 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 346 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 347 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 348 | int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 349 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 350 | unsigned char *output ); |
| 351 | |
| 352 | #ifdef __cplusplus |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | #endif /* POLARSSL_MD_H */ |