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