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