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