Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md.c |
| 3 | * |
| 4 | * \brief Generic message digest wrapper for PolarSSL |
| 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 | #include "polarssl/config.h" |
| 31 | |
| 32 | #if defined(POLARSSL_MD_C) |
| 33 | |
| 34 | #include "polarssl/md.h" |
| 35 | #include "polarssl/md_wrap.h" |
| 36 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 39 | #if defined _MSC_VER && !defined strcasecmp |
| 40 | #define strcasecmp _stricmp |
| 41 | #endif |
| 42 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 43 | static const int supported_digests[] = { |
| 44 | |
| 45 | #if defined(POLARSSL_MD2_C) |
| 46 | POLARSSL_MD_MD2, |
| 47 | #endif |
| 48 | |
| 49 | #if defined(POLARSSL_MD4_C) |
| 50 | POLARSSL_MD_MD4, |
| 51 | #endif |
| 52 | |
| 53 | #if defined(POLARSSL_MD5_C) |
| 54 | POLARSSL_MD_MD5, |
| 55 | #endif |
| 56 | |
| 57 | #if defined(POLARSSL_SHA1_C) |
| 58 | POLARSSL_MD_SHA1, |
| 59 | #endif |
| 60 | |
| 61 | #if defined(POLARSSL_SHA2_C) |
| 62 | POLARSSL_MD_SHA224, |
| 63 | POLARSSL_MD_SHA256, |
| 64 | #endif |
| 65 | |
| 66 | #if defined(POLARSSL_SHA4_C) |
| 67 | POLARSSL_MD_SHA384, |
| 68 | POLARSSL_MD_SHA512, |
| 69 | #endif |
| 70 | |
| 71 | 0 |
| 72 | }; |
| 73 | |
| 74 | const int *md_list( void ) |
| 75 | { |
| 76 | return supported_digests; |
| 77 | } |
| 78 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 79 | const md_info_t *md_info_from_string( const char *md_name ) |
| 80 | { |
| 81 | if( NULL == md_name ) |
| 82 | return NULL; |
| 83 | |
| 84 | /* Get the appropriate digest information */ |
| 85 | #if defined(POLARSSL_MD2_C) |
| 86 | if( !strcasecmp( "MD2", md_name ) ) |
| 87 | return md_info_from_type( POLARSSL_MD_MD2 ); |
| 88 | #endif |
| 89 | #if defined(POLARSSL_MD4_C) |
| 90 | if( !strcasecmp( "MD4", md_name ) ) |
| 91 | return md_info_from_type( POLARSSL_MD_MD4 ); |
| 92 | #endif |
| 93 | #if defined(POLARSSL_MD5_C) |
| 94 | if( !strcasecmp( "MD5", md_name ) ) |
| 95 | return md_info_from_type( POLARSSL_MD_MD5 ); |
| 96 | #endif |
| 97 | #if defined(POLARSSL_SHA1_C) |
| 98 | if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) ) |
| 99 | return md_info_from_type( POLARSSL_MD_SHA1 ); |
| 100 | #endif |
| 101 | #if defined(POLARSSL_SHA2_C) |
| 102 | if( !strcasecmp( "SHA224", md_name ) ) |
| 103 | return md_info_from_type( POLARSSL_MD_SHA224 ); |
| 104 | if( !strcasecmp( "SHA256", md_name ) ) |
| 105 | return md_info_from_type( POLARSSL_MD_SHA256 ); |
| 106 | #endif |
| 107 | #if defined(POLARSSL_SHA4_C) |
| 108 | if( !strcasecmp( "SHA384", md_name ) ) |
| 109 | return md_info_from_type( POLARSSL_MD_SHA384 ); |
| 110 | if( !strcasecmp( "SHA512", md_name ) ) |
| 111 | return md_info_from_type( POLARSSL_MD_SHA512 ); |
| 112 | #endif |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | const md_info_t *md_info_from_type( md_type_t md_type ) |
| 117 | { |
| 118 | switch( md_type ) |
| 119 | { |
| 120 | #if defined(POLARSSL_MD2_C) |
| 121 | case POLARSSL_MD_MD2: |
| 122 | return &md2_info; |
| 123 | #endif |
| 124 | #if defined(POLARSSL_MD4_C) |
| 125 | case POLARSSL_MD_MD4: |
| 126 | return &md4_info; |
| 127 | #endif |
| 128 | #if defined(POLARSSL_MD5_C) |
| 129 | case POLARSSL_MD_MD5: |
| 130 | return &md5_info; |
| 131 | #endif |
| 132 | #if defined(POLARSSL_SHA1_C) |
| 133 | case POLARSSL_MD_SHA1: |
| 134 | return &sha1_info; |
| 135 | #endif |
| 136 | #if defined(POLARSSL_SHA2_C) |
| 137 | case POLARSSL_MD_SHA224: |
| 138 | return &sha224_info; |
| 139 | case POLARSSL_MD_SHA256: |
| 140 | return &sha256_info; |
| 141 | #endif |
| 142 | #if defined(POLARSSL_SHA4_C) |
| 143 | case POLARSSL_MD_SHA384: |
| 144 | return &sha384_info; |
| 145 | case POLARSSL_MD_SHA512: |
| 146 | return &sha512_info; |
| 147 | #endif |
| 148 | default: |
| 149 | return NULL; |
| 150 | } |
| 151 | } |
| 152 | |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 153 | int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 154 | { |
| 155 | if( md_info == NULL ) |
| 156 | return 1; |
| 157 | |
| 158 | if( ctx == NULL || ctx->md_ctx != NULL ) |
| 159 | return 1; |
| 160 | |
| 161 | if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) |
| 162 | return 1; |
| 163 | |
| 164 | ctx->md_info = md_info; |
| 165 | |
| 166 | md_info->starts_func( ctx->md_ctx ); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 171 | int md_free_ctx( md_context_t *ctx ) |
| 172 | { |
| 173 | if( ctx == NULL || ctx->md_info == NULL ) |
| 174 | return 1; |
| 175 | |
| 176 | ctx->md_info->ctx_free_func( ctx->md_ctx ); |
| 177 | ctx->md_ctx = NULL; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | int md_starts( md_context_t *ctx ) |
| 183 | { |
| 184 | if( ctx == NULL || ctx->md_info == NULL ) |
| 185 | return 1; |
| 186 | |
| 187 | ctx->md_info->starts_func( ctx->md_ctx ); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 192 | 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] | 193 | { |
| 194 | if( ctx == NULL || ctx->md_info == NULL ) |
| 195 | return 1; |
| 196 | |
| 197 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | int md_finish( md_context_t *ctx, unsigned char *output ) |
| 203 | { |
| 204 | if( ctx == NULL || ctx->md_info == NULL ) |
| 205 | return 1; |
| 206 | |
| 207 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 212 | 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] | 213 | unsigned char *output ) |
| 214 | { |
| 215 | if ( md_info == NULL ) |
| 216 | return 1; |
| 217 | |
| 218 | md_info->digest_func( input, ilen, output ); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | int md_file( const md_info_t *md_info, const char *path, unsigned char *output ) |
| 224 | { |
| 225 | if( md_info == NULL ) |
| 226 | return 3; |
| 227 | |
| 228 | return md_info->file_func( path, output ); |
| 229 | } |
| 230 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 231 | 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] | 232 | { |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 233 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 234 | return 1; |
| 235 | |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 236 | ctx->md_info->hmac_starts_func( ctx->md_ctx, key, keylen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 241 | 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] | 242 | { |
| 243 | if( ctx == NULL || ctx->md_info == NULL ) |
| 244 | return 1; |
| 245 | |
| 246 | ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen ); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int md_hmac_finish( md_context_t *ctx, unsigned char *output) |
| 252 | { |
| 253 | if( ctx == NULL || ctx->md_info == NULL ) |
| 254 | return 1; |
| 255 | |
| 256 | ctx->md_info->hmac_finish_func( ctx->md_ctx, output); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int md_hmac_reset( md_context_t *ctx ) |
| 262 | { |
| 263 | if( ctx == NULL || ctx->md_info == NULL ) |
| 264 | return 1; |
| 265 | |
| 266 | ctx->md_info->hmac_reset_func( ctx->md_ctx); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 271 | int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 272 | 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 | if( md_info == NULL ) |
| 276 | return 1; |
| 277 | |
| 278 | md_info->hmac_func( key, keylen, input, ilen, output ); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | #endif |