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