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 | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 35 | #ifdef _MSC_VER |
| 36 | #define inline _inline |
| 37 | #endif |
| 38 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 39 | #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^] | 40 | #define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ |
| 41 | #define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ |
| 42 | #define POLARSSL_ERR_MD_FILE_OPEN_FAILED -0x5200 /**< Opening of file failed. */ |
| 43 | #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] | 44 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 45 | typedef enum { |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 46 | POLARSSL_MD_NONE=0, |
| 47 | POLARSSL_MD_MD2, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 48 | POLARSSL_MD_MD4, |
| 49 | POLARSSL_MD_MD5, |
| 50 | POLARSSL_MD_SHA1, |
| 51 | POLARSSL_MD_SHA224, |
| 52 | POLARSSL_MD_SHA256, |
| 53 | POLARSSL_MD_SHA384, |
| 54 | POLARSSL_MD_SHA512, |
| 55 | } md_type_t; |
| 56 | |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 57 | #define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
| 58 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Message digest information. Allows message digest functions to be called |
| 61 | * in a generic way. |
| 62 | */ |
| 63 | typedef struct { |
| 64 | /** Digest identifier */ |
| 65 | md_type_t type; |
| 66 | |
| 67 | /** Name of the message digest */ |
| 68 | const char * name; |
| 69 | |
| 70 | /** Output length of the digest function */ |
| 71 | int size; |
| 72 | |
| 73 | /** Digest initialisation function */ |
| 74 | void (*starts_func)( void *ctx ); |
| 75 | |
| 76 | /** Digest update function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 77 | void (*update_func)( void *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 78 | |
| 79 | /** Digest finalisation function */ |
| 80 | void (*finish_func)( void *ctx, unsigned char *output ); |
| 81 | |
| 82 | /** Generic digest function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 83 | void (*digest_func)( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 84 | unsigned char *output ); |
| 85 | |
| 86 | /** Generic file digest function */ |
| 87 | int (*file_func)( const char *path, unsigned char *output ); |
| 88 | |
| 89 | /** HMAC Initialisation function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 90 | 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] | 91 | |
| 92 | /** HMAC update function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 93 | 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] | 94 | |
| 95 | /** HMAC finalisation function */ |
| 96 | void (*hmac_finish_func)( void *ctx, unsigned char *output); |
| 97 | |
| 98 | /** HMAC context reset function */ |
| 99 | void (*hmac_reset_func)( void *ctx ); |
| 100 | |
| 101 | /** Generic HMAC function */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 102 | void (*hmac_func)( const unsigned char *key, size_t keylen, |
| 103 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 104 | unsigned char *output ); |
| 105 | |
| 106 | /** Allocate a new context */ |
| 107 | void * (*ctx_alloc_func)( void ); |
| 108 | |
| 109 | /** Free the given context */ |
| 110 | void (*ctx_free_func)( void *ctx ); |
| 111 | |
| 112 | } md_info_t; |
| 113 | |
| 114 | /** |
| 115 | * Generic message digest context. |
| 116 | */ |
| 117 | typedef struct { |
| 118 | /** Information about the associated message digest */ |
| 119 | const md_info_t *md_info; |
| 120 | |
| 121 | /** Digest-specific context */ |
| 122 | void *md_ctx; |
| 123 | } md_context_t; |
| 124 | |
| 125 | #define MD_CONTEXT_T_INIT { \ |
| 126 | NULL, /* md_info */ \ |
| 127 | NULL, /* md_ctx */ \ |
| 128 | } |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | extern "C" { |
| 132 | #endif |
| 133 | |
| 134 | /** |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 135 | * \brief Returns the list of digests supported by the generic digest module. |
| 136 | * |
| 137 | * \return a statically allocated array of digests, the last entry |
| 138 | * is 0. |
| 139 | */ |
| 140 | const int *md_list( void ); |
| 141 | |
| 142 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 143 | * \brief Returns the message digest information associated with the |
| 144 | * given digest name. |
| 145 | * |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 146 | * \param md_name Name of the digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 147 | * |
| 148 | * \return The message digest information associated with md_name or |
| 149 | * NULL if not found. |
| 150 | */ |
| 151 | const md_info_t *md_info_from_string( const char *md_name ); |
| 152 | |
| 153 | /** |
| 154 | * \brief Returns the message digest information associated with the |
| 155 | * given digest type. |
| 156 | * |
| 157 | * \param md_type type of digest to search for. |
| 158 | * |
| 159 | * \return The message digest information associated with md_type or |
| 160 | * NULL if not found. |
| 161 | */ |
| 162 | const md_info_t *md_info_from_type( md_type_t md_type ); |
| 163 | |
| 164 | /** |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 165 | * \brief Initialises and fills the message digest context structure with |
| 166 | * the appropriate values. |
| 167 | * |
| 168 | * \param ctx context to initialise. May not be NULL. The |
| 169 | * digest-specific context (ctx->md_ctx) must be NULL. It will |
| 170 | * be allocated, and must be freed using md_free_ctx() later. |
| 171 | * \param md_info message digest to use. |
| 172 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 173 | * \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on |
| 174 | * parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 175 | * allocation of the cipher-specific context failed. |
| 176 | */ |
| 177 | int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ); |
| 178 | |
| 179 | /** |
| 180 | * \brief Free the message-specific context of ctx. Freeing ctx itself |
| 181 | * remains the responsibility of the caller. |
| 182 | * |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 183 | * \param ctx Free the message-specific context |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 184 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 185 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 186 | * verification fails. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 187 | */ |
| 188 | int md_free_ctx( md_context_t *ctx ); |
| 189 | |
| 190 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 191 | * \brief Returns the size of the message digest output. |
| 192 | * |
| 193 | * \param md_info message digest info |
| 194 | * |
| 195 | * \return size of the message digest output. |
| 196 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 197 | 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] | 198 | { |
| 199 | return md_info->size; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * \brief Returns the type of the message digest output. |
| 204 | * |
| 205 | * \param md_info message digest info |
| 206 | * |
| 207 | * \return type of the message digest output. |
| 208 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | return md_info->type; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * \brief Returns the name of the message digest output. |
| 216 | * |
| 217 | * \param md_info message digest info |
| 218 | * |
| 219 | * \return name of the message digest output. |
| 220 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 221 | 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] | 222 | { |
| 223 | return md_info->name; |
| 224 | } |
| 225 | |
| 226 | /** |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 227 | * \brief Set-up the given context for a new message digest |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 228 | * |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 229 | * \param ctx generic message digest context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 230 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 231 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 232 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 233 | */ |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 234 | int md_starts( md_context_t *ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * \brief Generic message digest process buffer |
| 238 | * |
| 239 | * \param ctx Generic message digest context |
| 240 | * \param input buffer holding the datal |
| 241 | * \param ilen length of the input data |
| 242 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 243 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 244 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 245 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 246 | 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] | 247 | |
| 248 | /** |
| 249 | * \brief Generic message digest final digest |
| 250 | * |
| 251 | * \param ctx Generic message digest context |
| 252 | * \param output Generic message digest checksum result |
| 253 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 254 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 255 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 256 | */ |
| 257 | int md_finish( md_context_t *ctx, unsigned char *output ); |
| 258 | |
| 259 | /** |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 260 | * \brief Output = message_digest( input buffer ) |
| 261 | * |
| 262 | * \param md_info message digest info |
| 263 | * \param input buffer holding the data |
| 264 | * \param ilen length of the input data |
| 265 | * \param output Generic message digest checksum result |
| 266 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 267 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 268 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 269 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 270 | 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] | 271 | unsigned char *output ); |
| 272 | |
| 273 | /** |
| 274 | * \brief Output = message_digest( file contents ) |
| 275 | * |
| 276 | * \param md_info message digest info |
| 277 | * \param path input file name |
| 278 | * \param output generic message digest checksum result |
| 279 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 280 | * \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen |
| 281 | * failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed, |
| 282 | * POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 283 | */ |
| 284 | int md_file( const md_info_t *md_info, const char *path, unsigned char *output ); |
| 285 | |
| 286 | /** |
| 287 | * \brief Generic HMAC context setup |
| 288 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 289 | * \param ctx HMAC context to be initialized |
| 290 | * \param key HMAC secret key |
| 291 | * \param keylen length of the HMAC key |
| 292 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 293 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 294 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 295 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 296 | 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] | 297 | |
| 298 | /** |
| 299 | * \brief Generic HMAC process buffer |
| 300 | * |
| 301 | * \param ctx HMAC context |
| 302 | * \param input buffer holding the data |
| 303 | * \param ilen length of the input data |
| 304 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 305 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 306 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 307 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 308 | 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] | 309 | |
| 310 | /** |
| 311 | * \brief Generic HMAC final digest |
| 312 | * |
| 313 | * \param ctx HMAC context |
| 314 | * \param output Generic HMAC checksum result |
| 315 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 316 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 317 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 318 | */ |
| 319 | int md_hmac_finish( md_context_t *ctx, unsigned char *output); |
| 320 | |
| 321 | /** |
| 322 | * \brief Generic HMAC context reset |
| 323 | * |
| 324 | * \param ctx HMAC context to be reset |
| 325 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 326 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 327 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 328 | */ |
| 329 | int md_hmac_reset( md_context_t *ctx ); |
| 330 | |
| 331 | /** |
| 332 | * \brief Output = Generic_HMAC( hmac key, input buffer ) |
| 333 | * |
| 334 | * \param md_info message digest info |
| 335 | * \param key HMAC secret key |
| 336 | * \param keylen length of the HMAC key |
| 337 | * \param input buffer holding the data |
| 338 | * \param ilen length of the input data |
| 339 | * \param output Generic HMAC-result |
| 340 | * |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame^] | 341 | * \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter |
| 342 | * verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 343 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 344 | int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 345 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 346 | unsigned char *output ); |
| 347 | |
| 348 | #ifdef __cplusplus |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | #endif /* POLARSSL_MD_H */ |