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