Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md.c |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 4 | * \brief Generic message digest wrapper for mbed TLS |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 10 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 11 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | */ |
| 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(POLARSSL_MD_C) |
| 34 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/md.h" |
| 36 | #include "mbedtls/md_wrap.h" |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 38 | #if defined(POLARSSL_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 42 | #define polarssl_malloc malloc |
| 43 | #define polarssl_free free |
| 44 | #endif |
| 45 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 46 | #include <string.h> |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 47 | |
Paul Bakker | 6edcd41 | 2013-10-29 15:22:54 +0100 | [diff] [blame] | 48 | #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ |
| 49 | !defined(EFI32) |
| 50 | #define strcasecmp _stricmp |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 53 | /* Implementation that should never be optimized out by the compiler */ |
| 54 | static void polarssl_zeroize( void *v, size_t n ) { |
| 55 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 56 | } |
| 57 | |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 58 | static const int supported_digests[] = { |
| 59 | |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 60 | #if defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 61 | POLARSSL_MD_SHA512, |
Manuel Pégourié-Gonnard | 480905d | 2014-08-21 19:38:32 +0200 | [diff] [blame] | 62 | POLARSSL_MD_SHA384, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 65 | #if defined(POLARSSL_SHA256_C) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 66 | POLARSSL_MD_SHA256, |
Manuel Pégourié-Gonnard | 480905d | 2014-08-21 19:38:32 +0200 | [diff] [blame] | 67 | POLARSSL_MD_SHA224, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 70 | #if defined(POLARSSL_SHA1_C) |
| 71 | POLARSSL_MD_SHA1, |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Manuel Pégourié-Gonnard | bd77254 | 2014-07-07 14:02:33 +0200 | [diff] [blame] | 74 | #if defined(POLARSSL_RIPEMD160_C) |
| 75 | POLARSSL_MD_RIPEMD160, |
| 76 | #endif |
| 77 | |
| 78 | #if defined(POLARSSL_MD5_C) |
| 79 | POLARSSL_MD_MD5, |
| 80 | #endif |
| 81 | |
| 82 | #if defined(POLARSSL_MD4_C) |
| 83 | POLARSSL_MD_MD4, |
| 84 | #endif |
| 85 | |
| 86 | #if defined(POLARSSL_MD2_C) |
| 87 | POLARSSL_MD_MD2, |
| 88 | #endif |
| 89 | |
| 90 | POLARSSL_MD_NONE |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | const int *md_list( void ) |
| 94 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 95 | return( supported_digests ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 98 | const md_info_t *md_info_from_string( const char *md_name ) |
| 99 | { |
| 100 | if( NULL == md_name ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 101 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 102 | |
| 103 | /* Get the appropriate digest information */ |
| 104 | #if defined(POLARSSL_MD2_C) |
| 105 | if( !strcasecmp( "MD2", md_name ) ) |
| 106 | return md_info_from_type( POLARSSL_MD_MD2 ); |
| 107 | #endif |
| 108 | #if defined(POLARSSL_MD4_C) |
| 109 | if( !strcasecmp( "MD4", md_name ) ) |
| 110 | return md_info_from_type( POLARSSL_MD_MD4 ); |
| 111 | #endif |
| 112 | #if defined(POLARSSL_MD5_C) |
| 113 | if( !strcasecmp( "MD5", md_name ) ) |
| 114 | return md_info_from_type( POLARSSL_MD_MD5 ); |
| 115 | #endif |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 116 | #if defined(POLARSSL_RIPEMD160_C) |
| 117 | if( !strcasecmp( "RIPEMD160", md_name ) ) |
| 118 | return md_info_from_type( POLARSSL_MD_RIPEMD160 ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 119 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 120 | #if defined(POLARSSL_SHA1_C) |
| 121 | if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) ) |
| 122 | return md_info_from_type( POLARSSL_MD_SHA1 ); |
| 123 | #endif |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 124 | #if defined(POLARSSL_SHA256_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 125 | if( !strcasecmp( "SHA224", md_name ) ) |
| 126 | return md_info_from_type( POLARSSL_MD_SHA224 ); |
| 127 | if( !strcasecmp( "SHA256", md_name ) ) |
| 128 | return md_info_from_type( POLARSSL_MD_SHA256 ); |
| 129 | #endif |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 130 | #if defined(POLARSSL_SHA512_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 131 | if( !strcasecmp( "SHA384", md_name ) ) |
| 132 | return md_info_from_type( POLARSSL_MD_SHA384 ); |
| 133 | if( !strcasecmp( "SHA512", md_name ) ) |
| 134 | return md_info_from_type( POLARSSL_MD_SHA512 ); |
| 135 | #endif |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 136 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | const md_info_t *md_info_from_type( md_type_t md_type ) |
| 140 | { |
| 141 | switch( md_type ) |
| 142 | { |
| 143 | #if defined(POLARSSL_MD2_C) |
| 144 | case POLARSSL_MD_MD2: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 145 | return( &md2_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 146 | #endif |
| 147 | #if defined(POLARSSL_MD4_C) |
| 148 | case POLARSSL_MD_MD4: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 149 | return( &md4_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 150 | #endif |
| 151 | #if defined(POLARSSL_MD5_C) |
| 152 | case POLARSSL_MD_MD5: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 153 | return( &md5_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 154 | #endif |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 155 | #if defined(POLARSSL_RIPEMD160_C) |
| 156 | case POLARSSL_MD_RIPEMD160: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 157 | return( &ripemd160_info ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 158 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 159 | #if defined(POLARSSL_SHA1_C) |
| 160 | case POLARSSL_MD_SHA1: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 161 | return( &sha1_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 162 | #endif |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 163 | #if defined(POLARSSL_SHA256_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 164 | case POLARSSL_MD_SHA224: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 165 | return( &sha224_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 166 | case POLARSSL_MD_SHA256: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 167 | return( &sha256_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 168 | #endif |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 169 | #if defined(POLARSSL_SHA512_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 170 | case POLARSSL_MD_SHA384: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 171 | return( &sha384_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 172 | case POLARSSL_MD_SHA512: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 173 | return( &sha512_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 174 | #endif |
| 175 | default: |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 176 | return( NULL ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 180 | void md_init( md_context_t *ctx ) |
| 181 | { |
| 182 | memset( ctx, 0, sizeof( md_context_t ) ); |
| 183 | } |
| 184 | |
| 185 | void md_free( md_context_t *ctx ) |
| 186 | { |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 187 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 188 | return; |
| 189 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 190 | if( ctx->md_ctx != NULL ) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 191 | ctx->md_info->ctx_free_func( ctx->md_ctx ); |
| 192 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 193 | if( ctx->hmac_ctx != NULL ) |
| 194 | { |
| 195 | polarssl_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size ); |
| 196 | polarssl_free( ctx->hmac_ctx ); |
| 197 | } |
| 198 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 199 | polarssl_zeroize( ctx, sizeof( md_context_t ) ); |
| 200 | } |
| 201 | |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame^] | 202 | #if ! defined(POLARSSL_DEPRECATED_REMOVED) |
| 203 | int md_init_ctx( md_context_t *ctx, const md_info_t *md_info ) |
| 204 | { |
| 205 | return md_setup( ctx, md_info, 1 ); |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | int md_setup( md_context_t *ctx, const md_info_t *md_info, int hmac ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 210 | { |
Paul Bakker | 279432a | 2012-04-26 10:09:35 +0000 | [diff] [blame] | 211 | if( md_info == NULL || ctx == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 212 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 213 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 214 | if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 215 | return( POLARSSL_ERR_MD_ALLOC_FAILED ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | 4063ceb | 2015-03-25 16:08:53 +0100 | [diff] [blame] | 217 | if( hmac != 0 ) |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 218 | { |
Manuel Pégourié-Gonnard | 4063ceb | 2015-03-25 16:08:53 +0100 | [diff] [blame] | 219 | ctx->hmac_ctx = polarssl_malloc( 2 * md_info->block_size ); |
| 220 | if( ctx->hmac_ctx == NULL ) |
| 221 | { |
| 222 | md_info->ctx_free_func( ctx->md_ctx ); |
| 223 | return( POLARSSL_ERR_MD_ALLOC_FAILED ); |
| 224 | } |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 227 | ctx->md_info = md_info; |
| 228 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 229 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 232 | int md_starts( md_context_t *ctx ) |
| 233 | { |
| 234 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 235 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 236 | |
| 237 | ctx->md_info->starts_func( ctx->md_ctx ); |
| 238 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 239 | return( 0 ); |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 242 | 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] | 243 | { |
| 244 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 245 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 246 | |
| 247 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
| 248 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 249 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int md_finish( md_context_t *ctx, unsigned char *output ) |
| 253 | { |
| 254 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 255 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 256 | |
| 257 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
| 258 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 259 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 262 | 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] | 263 | unsigned char *output ) |
| 264 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 265 | if( md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 266 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 267 | |
| 268 | md_info->digest_func( input, ilen, output ); |
| 269 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 270 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int md_file( const md_info_t *md_info, const char *path, unsigned char *output ) |
| 274 | { |
Paul Bakker | 8913f82 | 2012-01-14 18:07:41 +0000 | [diff] [blame] | 275 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 276 | int ret; |
Paul Bakker | 8913f82 | 2012-01-14 18:07:41 +0000 | [diff] [blame] | 277 | #endif |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 278 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 279 | if( md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 280 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 281 | |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 282 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 283 | ret = md_info->file_func( path, output ); |
Paul Bakker | 8913f82 | 2012-01-14 18:07:41 +0000 | [diff] [blame] | 284 | if( ret != 0 ) |
| 285 | return( POLARSSL_ERR_MD_FILE_IO_ERROR + ret ); |
Paul Bakker | 9c021ad | 2011-06-09 15:55:11 +0000 | [diff] [blame] | 286 | |
Paul Bakker | 8913f82 | 2012-01-14 18:07:41 +0000 | [diff] [blame] | 287 | return( ret ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 288 | #else |
| 289 | ((void) path); |
| 290 | ((void) output); |
| 291 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 292 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 293 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 296 | 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] | 297 | { |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 298 | unsigned char sum[POLARSSL_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 299 | unsigned char *ipad, *opad; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 300 | size_t i; |
| 301 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 302 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 303 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 304 | |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 305 | if( keylen > (size_t) ctx->md_info->block_size ) |
| 306 | { |
| 307 | ctx->md_info->starts_func( ctx->md_ctx ); |
| 308 | ctx->md_info->update_func( ctx->md_ctx, key, keylen ); |
| 309 | ctx->md_info->finish_func( ctx->md_ctx, sum ); |
| 310 | |
| 311 | keylen = ctx->md_info->size; |
| 312 | key = sum; |
| 313 | } |
| 314 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 315 | ipad = (unsigned char *) ctx->hmac_ctx; |
| 316 | opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size; |
| 317 | |
| 318 | memset( ipad, 0x36, ctx->md_info->block_size ); |
| 319 | memset( opad, 0x5C, ctx->md_info->block_size ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 320 | |
| 321 | for( i = 0; i < keylen; i++ ) |
| 322 | { |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 323 | ipad[i] = (unsigned char)( ipad[i] ^ key[i] ); |
| 324 | opad[i] = (unsigned char)( opad[i] ^ key[i] ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | polarssl_zeroize( sum, sizeof( sum ) ); |
| 328 | |
| 329 | ctx->md_info->starts_func( ctx->md_ctx ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 330 | ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 331 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 332 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 335 | 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] | 336 | { |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 337 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 338 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 339 | |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 340 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 341 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 342 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 345 | int md_hmac_finish( md_context_t *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 346 | { |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 347 | unsigned char tmp[POLARSSL_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 348 | unsigned char *opad; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 349 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 350 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 351 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 352 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 353 | opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size; |
| 354 | |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 355 | ctx->md_info->finish_func( ctx->md_ctx, tmp ); |
| 356 | ctx->md_info->starts_func( ctx->md_ctx ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 357 | ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size ); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 358 | ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size ); |
| 359 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 360 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 361 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int md_hmac_reset( md_context_t *ctx ) |
| 365 | { |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 366 | unsigned char *ipad; |
| 367 | |
| 368 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 369 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 370 | |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 371 | ipad = (unsigned char *) ctx->hmac_ctx; |
| 372 | |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 373 | ctx->md_info->starts_func( ctx->md_ctx ); |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 374 | ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 375 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 376 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 379 | int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 380 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 381 | unsigned char *output ) |
| 382 | { |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 383 | md_context_t ctx; |
| 384 | int ret; |
| 385 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 386 | if( md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 387 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 388 | |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 389 | md_init( &ctx ); |
| 390 | |
Manuel Pégourié-Gonnard | abb6744 | 2015-03-25 16:29:51 +0100 | [diff] [blame] | 391 | if( ( ret = md_setup( &ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 392 | return( ret ); |
| 393 | |
| 394 | md_hmac_starts( &ctx, key, keylen ); |
| 395 | md_hmac_update( &ctx, input, ilen ); |
| 396 | md_hmac_finish( &ctx, output ); |
| 397 | |
| 398 | md_free( &ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 399 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 400 | return( 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 403 | int md_process( md_context_t *ctx, const unsigned char *data ) |
| 404 | { |
| 405 | if( ctx == NULL || ctx->md_info == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 406 | return( POLARSSL_ERR_MD_BAD_INPUT_DATA ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 407 | |
| 408 | ctx->md_info->process_func( ctx->md_ctx, data ); |
| 409 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 410 | return( 0 ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Manuel Pégourié-Gonnard | ca878db | 2015-03-24 12:13:30 +0100 | [diff] [blame] | 413 | unsigned char md_get_size( const md_info_t *md_info ) |
| 414 | { |
| 415 | if( md_info == NULL ) |
| 416 | return( 0 ); |
| 417 | |
| 418 | return md_info->size; |
| 419 | } |
| 420 | |
| 421 | md_type_t md_get_type( const md_info_t *md_info ) |
| 422 | { |
| 423 | if( md_info == NULL ) |
| 424 | return( POLARSSL_MD_NONE ); |
| 425 | |
| 426 | return md_info->type; |
| 427 | } |
| 428 | |
| 429 | const char *md_get_name( const md_info_t *md_info ) |
| 430 | { |
| 431 | if( md_info == NULL ) |
| 432 | return( NULL ); |
| 433 | |
| 434 | return md_info->name; |
| 435 | } |
| 436 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 437 | #endif /* POLARSSL_MD_C */ |