mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * RFC 1321 compliant MD5 implementation
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * The MD5 algorithm was designed by Ron Rivest in 1991.
ansond 0:137634ff4186 24 *
ansond 0:137634ff4186 25 * http://www.ietf.org/rfc/rfc1321.txt
ansond 0:137634ff4186 26 */
ansond 0:137634ff4186 27
ansond 0:137634ff4186 28 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 29 #include "polarssl/config.h"
ansond 0:137634ff4186 30 #else
ansond 0:137634ff4186 31 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 32 #endif
ansond 0:137634ff4186 33
ansond 0:137634ff4186 34 #if defined(POLARSSL_MD5_C)
ansond 0:137634ff4186 35
ansond 0:137634ff4186 36 #include "polarssl/md5.h"
ansond 0:137634ff4186 37
ansond 0:137634ff4186 38 #include <string.h>
ansond 0:137634ff4186 39
ansond 0:137634ff4186 40 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 41 #include <stdio.h>
ansond 0:137634ff4186 42 #endif
ansond 0:137634ff4186 43
ansond 0:137634ff4186 44 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 45 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 46 #include "polarssl/platform.h"
ansond 0:137634ff4186 47 #else
ansond 0:137634ff4186 48 #include <stdio.h>
ansond 0:137634ff4186 49 #define polarssl_printf printf
ansond 0:137634ff4186 50 #endif /* POLARSSL_PLATFORM_C */
ansond 0:137634ff4186 51 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 54 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 56 }
ansond 0:137634ff4186 57
ansond 0:137634ff4186 58 #if !defined(POLARSSL_MD5_ALT)
ansond 0:137634ff4186 59
ansond 0:137634ff4186 60 /*
ansond 0:137634ff4186 61 * 32-bit integer manipulation macros (little endian)
ansond 0:137634ff4186 62 */
ansond 0:137634ff4186 63 #ifndef GET_UINT32_LE
ansond 0:137634ff4186 64 #define GET_UINT32_LE(n,b,i) \
ansond 0:137634ff4186 65 { \
ansond 0:137634ff4186 66 (n) = ( (uint32_t) (b)[(i) ] ) \
ansond 0:137634ff4186 67 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
ansond 0:137634ff4186 68 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
ansond 0:137634ff4186 69 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
ansond 0:137634ff4186 70 }
ansond 0:137634ff4186 71 #endif
ansond 0:137634ff4186 72
ansond 0:137634ff4186 73 #ifndef PUT_UINT32_LE
ansond 0:137634ff4186 74 #define PUT_UINT32_LE(n,b,i) \
ansond 0:137634ff4186 75 { \
ansond 0:137634ff4186 76 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
ansond 0:137634ff4186 77 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
ansond 0:137634ff4186 78 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
ansond 0:137634ff4186 79 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
ansond 0:137634ff4186 80 }
ansond 0:137634ff4186 81 #endif
ansond 0:137634ff4186 82
ansond 0:137634ff4186 83 void md5_init( md5_context *ctx )
ansond 0:137634ff4186 84 {
ansond 0:137634ff4186 85 memset( ctx, 0, sizeof( md5_context ) );
ansond 0:137634ff4186 86 }
ansond 0:137634ff4186 87
ansond 0:137634ff4186 88 void md5_free( md5_context *ctx )
ansond 0:137634ff4186 89 {
ansond 0:137634ff4186 90 if( ctx == NULL )
ansond 0:137634ff4186 91 return;
ansond 0:137634ff4186 92
ansond 0:137634ff4186 93 polarssl_zeroize( ctx, sizeof( md5_context ) );
ansond 0:137634ff4186 94 }
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 /*
ansond 0:137634ff4186 97 * MD5 context setup
ansond 0:137634ff4186 98 */
ansond 0:137634ff4186 99 void md5_starts( md5_context *ctx )
ansond 0:137634ff4186 100 {
ansond 0:137634ff4186 101 ctx->total[0] = 0;
ansond 0:137634ff4186 102 ctx->total[1] = 0;
ansond 0:137634ff4186 103
ansond 0:137634ff4186 104 ctx->state[0] = 0x67452301;
ansond 0:137634ff4186 105 ctx->state[1] = 0xEFCDAB89;
ansond 0:137634ff4186 106 ctx->state[2] = 0x98BADCFE;
ansond 0:137634ff4186 107 ctx->state[3] = 0x10325476;
ansond 0:137634ff4186 108 }
ansond 0:137634ff4186 109
ansond 0:137634ff4186 110 void md5_process( md5_context *ctx, const unsigned char data[64] )
ansond 0:137634ff4186 111 {
ansond 0:137634ff4186 112 uint32_t X[16], A, B, C, D;
ansond 0:137634ff4186 113
ansond 0:137634ff4186 114 GET_UINT32_LE( X[ 0], data, 0 );
ansond 0:137634ff4186 115 GET_UINT32_LE( X[ 1], data, 4 );
ansond 0:137634ff4186 116 GET_UINT32_LE( X[ 2], data, 8 );
ansond 0:137634ff4186 117 GET_UINT32_LE( X[ 3], data, 12 );
ansond 0:137634ff4186 118 GET_UINT32_LE( X[ 4], data, 16 );
ansond 0:137634ff4186 119 GET_UINT32_LE( X[ 5], data, 20 );
ansond 0:137634ff4186 120 GET_UINT32_LE( X[ 6], data, 24 );
ansond 0:137634ff4186 121 GET_UINT32_LE( X[ 7], data, 28 );
ansond 0:137634ff4186 122 GET_UINT32_LE( X[ 8], data, 32 );
ansond 0:137634ff4186 123 GET_UINT32_LE( X[ 9], data, 36 );
ansond 0:137634ff4186 124 GET_UINT32_LE( X[10], data, 40 );
ansond 0:137634ff4186 125 GET_UINT32_LE( X[11], data, 44 );
ansond 0:137634ff4186 126 GET_UINT32_LE( X[12], data, 48 );
ansond 0:137634ff4186 127 GET_UINT32_LE( X[13], data, 52 );
ansond 0:137634ff4186 128 GET_UINT32_LE( X[14], data, 56 );
ansond 0:137634ff4186 129 GET_UINT32_LE( X[15], data, 60 );
ansond 0:137634ff4186 130
ansond 0:137634ff4186 131 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
ansond 0:137634ff4186 132
ansond 0:137634ff4186 133 #define P(a,b,c,d,k,s,t) \
ansond 0:137634ff4186 134 { \
ansond 0:137634ff4186 135 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
ansond 0:137634ff4186 136 }
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 A = ctx->state[0];
ansond 0:137634ff4186 139 B = ctx->state[1];
ansond 0:137634ff4186 140 C = ctx->state[2];
ansond 0:137634ff4186 141 D = ctx->state[3];
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 #define F(x,y,z) (z ^ (x & (y ^ z)))
ansond 0:137634ff4186 144
ansond 0:137634ff4186 145 P( A, B, C, D, 0, 7, 0xD76AA478 );
ansond 0:137634ff4186 146 P( D, A, B, C, 1, 12, 0xE8C7B756 );
ansond 0:137634ff4186 147 P( C, D, A, B, 2, 17, 0x242070DB );
ansond 0:137634ff4186 148 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
ansond 0:137634ff4186 149 P( A, B, C, D, 4, 7, 0xF57C0FAF );
ansond 0:137634ff4186 150 P( D, A, B, C, 5, 12, 0x4787C62A );
ansond 0:137634ff4186 151 P( C, D, A, B, 6, 17, 0xA8304613 );
ansond 0:137634ff4186 152 P( B, C, D, A, 7, 22, 0xFD469501 );
ansond 0:137634ff4186 153 P( A, B, C, D, 8, 7, 0x698098D8 );
ansond 0:137634ff4186 154 P( D, A, B, C, 9, 12, 0x8B44F7AF );
ansond 0:137634ff4186 155 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
ansond 0:137634ff4186 156 P( B, C, D, A, 11, 22, 0x895CD7BE );
ansond 0:137634ff4186 157 P( A, B, C, D, 12, 7, 0x6B901122 );
ansond 0:137634ff4186 158 P( D, A, B, C, 13, 12, 0xFD987193 );
ansond 0:137634ff4186 159 P( C, D, A, B, 14, 17, 0xA679438E );
ansond 0:137634ff4186 160 P( B, C, D, A, 15, 22, 0x49B40821 );
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 #undef F
ansond 0:137634ff4186 163
ansond 0:137634ff4186 164 #define F(x,y,z) (y ^ (z & (x ^ y)))
ansond 0:137634ff4186 165
ansond 0:137634ff4186 166 P( A, B, C, D, 1, 5, 0xF61E2562 );
ansond 0:137634ff4186 167 P( D, A, B, C, 6, 9, 0xC040B340 );
ansond 0:137634ff4186 168 P( C, D, A, B, 11, 14, 0x265E5A51 );
ansond 0:137634ff4186 169 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
ansond 0:137634ff4186 170 P( A, B, C, D, 5, 5, 0xD62F105D );
ansond 0:137634ff4186 171 P( D, A, B, C, 10, 9, 0x02441453 );
ansond 0:137634ff4186 172 P( C, D, A, B, 15, 14, 0xD8A1E681 );
ansond 0:137634ff4186 173 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
ansond 0:137634ff4186 174 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
ansond 0:137634ff4186 175 P( D, A, B, C, 14, 9, 0xC33707D6 );
ansond 0:137634ff4186 176 P( C, D, A, B, 3, 14, 0xF4D50D87 );
ansond 0:137634ff4186 177 P( B, C, D, A, 8, 20, 0x455A14ED );
ansond 0:137634ff4186 178 P( A, B, C, D, 13, 5, 0xA9E3E905 );
ansond 0:137634ff4186 179 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
ansond 0:137634ff4186 180 P( C, D, A, B, 7, 14, 0x676F02D9 );
ansond 0:137634ff4186 181 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
ansond 0:137634ff4186 182
ansond 0:137634ff4186 183 #undef F
ansond 0:137634ff4186 184
ansond 0:137634ff4186 185 #define F(x,y,z) (x ^ y ^ z)
ansond 0:137634ff4186 186
ansond 0:137634ff4186 187 P( A, B, C, D, 5, 4, 0xFFFA3942 );
ansond 0:137634ff4186 188 P( D, A, B, C, 8, 11, 0x8771F681 );
ansond 0:137634ff4186 189 P( C, D, A, B, 11, 16, 0x6D9D6122 );
ansond 0:137634ff4186 190 P( B, C, D, A, 14, 23, 0xFDE5380C );
ansond 0:137634ff4186 191 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
ansond 0:137634ff4186 192 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
ansond 0:137634ff4186 193 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
ansond 0:137634ff4186 194 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
ansond 0:137634ff4186 195 P( A, B, C, D, 13, 4, 0x289B7EC6 );
ansond 0:137634ff4186 196 P( D, A, B, C, 0, 11, 0xEAA127FA );
ansond 0:137634ff4186 197 P( C, D, A, B, 3, 16, 0xD4EF3085 );
ansond 0:137634ff4186 198 P( B, C, D, A, 6, 23, 0x04881D05 );
ansond 0:137634ff4186 199 P( A, B, C, D, 9, 4, 0xD9D4D039 );
ansond 0:137634ff4186 200 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
ansond 0:137634ff4186 201 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
ansond 0:137634ff4186 202 P( B, C, D, A, 2, 23, 0xC4AC5665 );
ansond 0:137634ff4186 203
ansond 0:137634ff4186 204 #undef F
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 #define F(x,y,z) (y ^ (x | ~z))
ansond 0:137634ff4186 207
ansond 0:137634ff4186 208 P( A, B, C, D, 0, 6, 0xF4292244 );
ansond 0:137634ff4186 209 P( D, A, B, C, 7, 10, 0x432AFF97 );
ansond 0:137634ff4186 210 P( C, D, A, B, 14, 15, 0xAB9423A7 );
ansond 0:137634ff4186 211 P( B, C, D, A, 5, 21, 0xFC93A039 );
ansond 0:137634ff4186 212 P( A, B, C, D, 12, 6, 0x655B59C3 );
ansond 0:137634ff4186 213 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
ansond 0:137634ff4186 214 P( C, D, A, B, 10, 15, 0xFFEFF47D );
ansond 0:137634ff4186 215 P( B, C, D, A, 1, 21, 0x85845DD1 );
ansond 0:137634ff4186 216 P( A, B, C, D, 8, 6, 0x6FA87E4F );
ansond 0:137634ff4186 217 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
ansond 0:137634ff4186 218 P( C, D, A, B, 6, 15, 0xA3014314 );
ansond 0:137634ff4186 219 P( B, C, D, A, 13, 21, 0x4E0811A1 );
ansond 0:137634ff4186 220 P( A, B, C, D, 4, 6, 0xF7537E82 );
ansond 0:137634ff4186 221 P( D, A, B, C, 11, 10, 0xBD3AF235 );
ansond 0:137634ff4186 222 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
ansond 0:137634ff4186 223 P( B, C, D, A, 9, 21, 0xEB86D391 );
ansond 0:137634ff4186 224
ansond 0:137634ff4186 225 #undef F
ansond 0:137634ff4186 226
ansond 0:137634ff4186 227 ctx->state[0] += A;
ansond 0:137634ff4186 228 ctx->state[1] += B;
ansond 0:137634ff4186 229 ctx->state[2] += C;
ansond 0:137634ff4186 230 ctx->state[3] += D;
ansond 0:137634ff4186 231 }
ansond 0:137634ff4186 232
ansond 0:137634ff4186 233 /*
ansond 0:137634ff4186 234 * MD5 process buffer
ansond 0:137634ff4186 235 */
ansond 0:137634ff4186 236 void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen )
ansond 0:137634ff4186 237 {
ansond 0:137634ff4186 238 size_t fill;
ansond 0:137634ff4186 239 uint32_t left;
ansond 0:137634ff4186 240
ansond 0:137634ff4186 241 if( ilen == 0 )
ansond 0:137634ff4186 242 return;
ansond 0:137634ff4186 243
ansond 0:137634ff4186 244 left = ctx->total[0] & 0x3F;
ansond 0:137634ff4186 245 fill = 64 - left;
ansond 0:137634ff4186 246
ansond 0:137634ff4186 247 ctx->total[0] += (uint32_t) ilen;
ansond 0:137634ff4186 248 ctx->total[0] &= 0xFFFFFFFF;
ansond 0:137634ff4186 249
ansond 0:137634ff4186 250 if( ctx->total[0] < (uint32_t) ilen )
ansond 0:137634ff4186 251 ctx->total[1]++;
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 if( left && ilen >= fill )
ansond 0:137634ff4186 254 {
ansond 0:137634ff4186 255 memcpy( (void *) (ctx->buffer + left), input, fill );
ansond 0:137634ff4186 256 md5_process( ctx, ctx->buffer );
ansond 0:137634ff4186 257 input += fill;
ansond 0:137634ff4186 258 ilen -= fill;
ansond 0:137634ff4186 259 left = 0;
ansond 0:137634ff4186 260 }
ansond 0:137634ff4186 261
ansond 0:137634ff4186 262 while( ilen >= 64 )
ansond 0:137634ff4186 263 {
ansond 0:137634ff4186 264 md5_process( ctx, input );
ansond 0:137634ff4186 265 input += 64;
ansond 0:137634ff4186 266 ilen -= 64;
ansond 0:137634ff4186 267 }
ansond 0:137634ff4186 268
ansond 0:137634ff4186 269 if( ilen > 0 )
ansond 0:137634ff4186 270 {
ansond 0:137634ff4186 271 memcpy( (void *) (ctx->buffer + left), input, ilen );
ansond 0:137634ff4186 272 }
ansond 0:137634ff4186 273 }
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 static const unsigned char md5_padding[64] =
ansond 0:137634ff4186 276 {
ansond 0:137634ff4186 277 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
ansond 0:137634ff4186 281 };
ansond 0:137634ff4186 282
ansond 0:137634ff4186 283 /*
ansond 0:137634ff4186 284 * MD5 final digest
ansond 0:137634ff4186 285 */
ansond 0:137634ff4186 286 void md5_finish( md5_context *ctx, unsigned char output[16] )
ansond 0:137634ff4186 287 {
ansond 0:137634ff4186 288 uint32_t last, padn;
ansond 0:137634ff4186 289 uint32_t high, low;
ansond 0:137634ff4186 290 unsigned char msglen[8];
ansond 0:137634ff4186 291
ansond 0:137634ff4186 292 high = ( ctx->total[0] >> 29 )
ansond 0:137634ff4186 293 | ( ctx->total[1] << 3 );
ansond 0:137634ff4186 294 low = ( ctx->total[0] << 3 );
ansond 0:137634ff4186 295
ansond 0:137634ff4186 296 PUT_UINT32_LE( low, msglen, 0 );
ansond 0:137634ff4186 297 PUT_UINT32_LE( high, msglen, 4 );
ansond 0:137634ff4186 298
ansond 0:137634ff4186 299 last = ctx->total[0] & 0x3F;
ansond 0:137634ff4186 300 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
ansond 0:137634ff4186 301
ansond 0:137634ff4186 302 md5_update( ctx, md5_padding, padn );
ansond 0:137634ff4186 303 md5_update( ctx, msglen, 8 );
ansond 0:137634ff4186 304
ansond 0:137634ff4186 305 PUT_UINT32_LE( ctx->state[0], output, 0 );
ansond 0:137634ff4186 306 PUT_UINT32_LE( ctx->state[1], output, 4 );
ansond 0:137634ff4186 307 PUT_UINT32_LE( ctx->state[2], output, 8 );
ansond 0:137634ff4186 308 PUT_UINT32_LE( ctx->state[3], output, 12 );
ansond 0:137634ff4186 309 }
ansond 0:137634ff4186 310
ansond 0:137634ff4186 311 #endif /* !POLARSSL_MD5_ALT */
ansond 0:137634ff4186 312
ansond 0:137634ff4186 313 /*
ansond 0:137634ff4186 314 * output = MD5( input buffer )
ansond 0:137634ff4186 315 */
ansond 0:137634ff4186 316 void md5( const unsigned char *input, size_t ilen, unsigned char output[16] )
ansond 0:137634ff4186 317 {
ansond 0:137634ff4186 318 md5_context ctx;
ansond 0:137634ff4186 319
ansond 0:137634ff4186 320 md5_init( &ctx );
ansond 0:137634ff4186 321 md5_starts( &ctx );
ansond 0:137634ff4186 322 md5_update( &ctx, input, ilen );
ansond 0:137634ff4186 323 md5_finish( &ctx, output );
ansond 0:137634ff4186 324 md5_free( &ctx );
ansond 0:137634ff4186 325 }
ansond 0:137634ff4186 326
ansond 0:137634ff4186 327 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 328 /*
ansond 0:137634ff4186 329 * output = MD5( file contents )
ansond 0:137634ff4186 330 */
ansond 0:137634ff4186 331 int md5_file( const char *path, unsigned char output[16] )
ansond 0:137634ff4186 332 {
ansond 0:137634ff4186 333 FILE *f;
ansond 0:137634ff4186 334 size_t n;
ansond 0:137634ff4186 335 md5_context ctx;
ansond 0:137634ff4186 336 unsigned char buf[1024];
ansond 0:137634ff4186 337
ansond 0:137634ff4186 338 if( ( f = fopen( path, "rb" ) ) == NULL )
ansond 0:137634ff4186 339 return( POLARSSL_ERR_MD5_FILE_IO_ERROR );
ansond 0:137634ff4186 340
ansond 0:137634ff4186 341 md5_init( &ctx );
ansond 0:137634ff4186 342 md5_starts( &ctx );
ansond 0:137634ff4186 343
ansond 0:137634ff4186 344 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
ansond 0:137634ff4186 345 md5_update( &ctx, buf, n );
ansond 0:137634ff4186 346
ansond 0:137634ff4186 347 md5_finish( &ctx, output );
ansond 0:137634ff4186 348 md5_free( &ctx );
ansond 0:137634ff4186 349
ansond 0:137634ff4186 350 if( ferror( f ) != 0 )
ansond 0:137634ff4186 351 {
ansond 0:137634ff4186 352 fclose( f );
ansond 0:137634ff4186 353 return( POLARSSL_ERR_MD5_FILE_IO_ERROR );
ansond 0:137634ff4186 354 }
ansond 0:137634ff4186 355
ansond 0:137634ff4186 356 fclose( f );
ansond 0:137634ff4186 357 return( 0 );
ansond 0:137634ff4186 358 }
ansond 0:137634ff4186 359 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 360
ansond 0:137634ff4186 361 /*
ansond 0:137634ff4186 362 * MD5 HMAC context setup
ansond 0:137634ff4186 363 */
ansond 0:137634ff4186 364 void md5_hmac_starts( md5_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 365 size_t keylen )
ansond 0:137634ff4186 366 {
ansond 0:137634ff4186 367 size_t i;
ansond 0:137634ff4186 368 unsigned char sum[16];
ansond 0:137634ff4186 369
ansond 0:137634ff4186 370 if( keylen > 64 )
ansond 0:137634ff4186 371 {
ansond 0:137634ff4186 372 md5( key, keylen, sum );
ansond 0:137634ff4186 373 keylen = 16;
ansond 0:137634ff4186 374 key = sum;
ansond 0:137634ff4186 375 }
ansond 0:137634ff4186 376
ansond 0:137634ff4186 377 memset( ctx->ipad, 0x36, 64 );
ansond 0:137634ff4186 378 memset( ctx->opad, 0x5C, 64 );
ansond 0:137634ff4186 379
ansond 0:137634ff4186 380 for( i = 0; i < keylen; i++ )
ansond 0:137634ff4186 381 {
ansond 0:137634ff4186 382 ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
ansond 0:137634ff4186 383 ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
ansond 0:137634ff4186 384 }
ansond 0:137634ff4186 385
ansond 0:137634ff4186 386 md5_starts( ctx );
ansond 0:137634ff4186 387 md5_update( ctx, ctx->ipad, 64 );
ansond 0:137634ff4186 388
ansond 0:137634ff4186 389 polarssl_zeroize( sum, sizeof( sum ) );
ansond 0:137634ff4186 390 }
ansond 0:137634ff4186 391
ansond 0:137634ff4186 392 /*
ansond 0:137634ff4186 393 * MD5 HMAC process buffer
ansond 0:137634ff4186 394 */
ansond 0:137634ff4186 395 void md5_hmac_update( md5_context *ctx, const unsigned char *input,
ansond 0:137634ff4186 396 size_t ilen )
ansond 0:137634ff4186 397 {
ansond 0:137634ff4186 398 md5_update( ctx, input, ilen );
ansond 0:137634ff4186 399 }
ansond 0:137634ff4186 400
ansond 0:137634ff4186 401 /*
ansond 0:137634ff4186 402 * MD5 HMAC final digest
ansond 0:137634ff4186 403 */
ansond 0:137634ff4186 404 void md5_hmac_finish( md5_context *ctx, unsigned char output[16] )
ansond 0:137634ff4186 405 {
ansond 0:137634ff4186 406 unsigned char tmpbuf[16];
ansond 0:137634ff4186 407
ansond 0:137634ff4186 408 md5_finish( ctx, tmpbuf );
ansond 0:137634ff4186 409 md5_starts( ctx );
ansond 0:137634ff4186 410 md5_update( ctx, ctx->opad, 64 );
ansond 0:137634ff4186 411 md5_update( ctx, tmpbuf, 16 );
ansond 0:137634ff4186 412 md5_finish( ctx, output );
ansond 0:137634ff4186 413
ansond 0:137634ff4186 414 polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
ansond 0:137634ff4186 415 }
ansond 0:137634ff4186 416
ansond 0:137634ff4186 417 /*
ansond 0:137634ff4186 418 * MD5 HMAC context reset
ansond 0:137634ff4186 419 */
ansond 0:137634ff4186 420 void md5_hmac_reset( md5_context *ctx )
ansond 0:137634ff4186 421 {
ansond 0:137634ff4186 422 md5_starts( ctx );
ansond 0:137634ff4186 423 md5_update( ctx, ctx->ipad, 64 );
ansond 0:137634ff4186 424 }
ansond 0:137634ff4186 425
ansond 0:137634ff4186 426 /*
ansond 0:137634ff4186 427 * output = HMAC-MD5( hmac key, input buffer )
ansond 0:137634ff4186 428 */
ansond 0:137634ff4186 429 void md5_hmac( const unsigned char *key, size_t keylen,
ansond 0:137634ff4186 430 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 431 unsigned char output[16] )
ansond 0:137634ff4186 432 {
ansond 0:137634ff4186 433 md5_context ctx;
ansond 0:137634ff4186 434
ansond 0:137634ff4186 435 md5_init( &ctx );
ansond 0:137634ff4186 436 md5_hmac_starts( &ctx, key, keylen );
ansond 0:137634ff4186 437 md5_hmac_update( &ctx, input, ilen );
ansond 0:137634ff4186 438 md5_hmac_finish( &ctx, output );
ansond 0:137634ff4186 439 md5_free( &ctx );
ansond 0:137634ff4186 440 }
ansond 0:137634ff4186 441
ansond 0:137634ff4186 442 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 443 /*
ansond 0:137634ff4186 444 * RFC 1321 test vectors
ansond 0:137634ff4186 445 */
ansond 0:137634ff4186 446 static const unsigned char md5_test_buf[7][81] =
ansond 0:137634ff4186 447 {
ansond 0:137634ff4186 448 { "" },
ansond 0:137634ff4186 449 { "a" },
ansond 0:137634ff4186 450 { "abc" },
ansond 0:137634ff4186 451 { "message digest" },
ansond 0:137634ff4186 452 { "abcdefghijklmnopqrstuvwxyz" },
ansond 0:137634ff4186 453 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
ansond 0:137634ff4186 454 { "12345678901234567890123456789012345678901234567890123456789012" \
ansond 0:137634ff4186 455 "345678901234567890" }
ansond 0:137634ff4186 456 };
ansond 0:137634ff4186 457
ansond 0:137634ff4186 458 static const int md5_test_buflen[7] =
ansond 0:137634ff4186 459 {
ansond 0:137634ff4186 460 0, 1, 3, 14, 26, 62, 80
ansond 0:137634ff4186 461 };
ansond 0:137634ff4186 462
ansond 0:137634ff4186 463 static const unsigned char md5_test_sum[7][16] =
ansond 0:137634ff4186 464 {
ansond 0:137634ff4186 465 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
ansond 0:137634ff4186 466 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
ansond 0:137634ff4186 467 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
ansond 0:137634ff4186 468 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
ansond 0:137634ff4186 469 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
ansond 0:137634ff4186 470 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
ansond 0:137634ff4186 471 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
ansond 0:137634ff4186 472 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
ansond 0:137634ff4186 473 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
ansond 0:137634ff4186 474 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
ansond 0:137634ff4186 475 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
ansond 0:137634ff4186 476 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
ansond 0:137634ff4186 477 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
ansond 0:137634ff4186 478 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
ansond 0:137634ff4186 479 };
ansond 0:137634ff4186 480
ansond 0:137634ff4186 481 /*
ansond 0:137634ff4186 482 * RFC 2202 test vectors
ansond 0:137634ff4186 483 */
ansond 0:137634ff4186 484 static const unsigned char md5_hmac_test_key[7][26] =
ansond 0:137634ff4186 485 {
ansond 0:137634ff4186 486 { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" },
ansond 0:137634ff4186 487 { "Jefe" },
ansond 0:137634ff4186 488 { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" },
ansond 0:137634ff4186 489 { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
ansond 0:137634ff4186 490 "\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
ansond 0:137634ff4186 491 { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" },
ansond 0:137634ff4186 492 { "" }, /* 0xAA 80 times */
ansond 0:137634ff4186 493 { "" }
ansond 0:137634ff4186 494 };
ansond 0:137634ff4186 495
ansond 0:137634ff4186 496 static const int md5_hmac_test_keylen[7] =
ansond 0:137634ff4186 497 {
ansond 0:137634ff4186 498 16, 4, 16, 25, 16, 80, 80
ansond 0:137634ff4186 499 };
ansond 0:137634ff4186 500
ansond 0:137634ff4186 501 static const unsigned char md5_hmac_test_buf[7][74] =
ansond 0:137634ff4186 502 {
ansond 0:137634ff4186 503 { "Hi There" },
ansond 0:137634ff4186 504 { "what do ya want for nothing?" },
ansond 0:137634ff4186 505 { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
ansond 0:137634ff4186 506 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
ansond 0:137634ff4186 507 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
ansond 0:137634ff4186 508 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
ansond 0:137634ff4186 509 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
ansond 0:137634ff4186 510 { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
ansond 0:137634ff4186 511 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
ansond 0:137634ff4186 512 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
ansond 0:137634ff4186 513 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
ansond 0:137634ff4186 514 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
ansond 0:137634ff4186 515 { "Test With Truncation" },
ansond 0:137634ff4186 516 { "Test Using Larger Than Block-Size Key - Hash Key First" },
ansond 0:137634ff4186 517 { "Test Using Larger Than Block-Size Key and Larger"
ansond 0:137634ff4186 518 " Than One Block-Size Data" }
ansond 0:137634ff4186 519 };
ansond 0:137634ff4186 520
ansond 0:137634ff4186 521 static const int md5_hmac_test_buflen[7] =
ansond 0:137634ff4186 522 {
ansond 0:137634ff4186 523 8, 28, 50, 50, 20, 54, 73
ansond 0:137634ff4186 524 };
ansond 0:137634ff4186 525
ansond 0:137634ff4186 526 static const unsigned char md5_hmac_test_sum[7][16] =
ansond 0:137634ff4186 527 {
ansond 0:137634ff4186 528 { 0x92, 0x94, 0x72, 0x7A, 0x36, 0x38, 0xBB, 0x1C,
ansond 0:137634ff4186 529 0x13, 0xF4, 0x8E, 0xF8, 0x15, 0x8B, 0xFC, 0x9D },
ansond 0:137634ff4186 530 { 0x75, 0x0C, 0x78, 0x3E, 0x6A, 0xB0, 0xB5, 0x03,
ansond 0:137634ff4186 531 0xEA, 0xA8, 0x6E, 0x31, 0x0A, 0x5D, 0xB7, 0x38 },
ansond 0:137634ff4186 532 { 0x56, 0xBE, 0x34, 0x52, 0x1D, 0x14, 0x4C, 0x88,
ansond 0:137634ff4186 533 0xDB, 0xB8, 0xC7, 0x33, 0xF0, 0xE8, 0xB3, 0xF6 },
ansond 0:137634ff4186 534 { 0x69, 0x7E, 0xAF, 0x0A, 0xCA, 0x3A, 0x3A, 0xEA,
ansond 0:137634ff4186 535 0x3A, 0x75, 0x16, 0x47, 0x46, 0xFF, 0xAA, 0x79 },
ansond 0:137634ff4186 536 { 0x56, 0x46, 0x1E, 0xF2, 0x34, 0x2E, 0xDC, 0x00,
ansond 0:137634ff4186 537 0xF9, 0xBA, 0xB9, 0x95 },
ansond 0:137634ff4186 538 { 0x6B, 0x1A, 0xB7, 0xFE, 0x4B, 0xD7, 0xBF, 0x8F,
ansond 0:137634ff4186 539 0x0B, 0x62, 0xE6, 0xCE, 0x61, 0xB9, 0xD0, 0xCD },
ansond 0:137634ff4186 540 { 0x6F, 0x63, 0x0F, 0xAD, 0x67, 0xCD, 0xA0, 0xEE,
ansond 0:137634ff4186 541 0x1F, 0xB1, 0xF5, 0x62, 0xDB, 0x3A, 0xA5, 0x3E }
ansond 0:137634ff4186 542 };
ansond 0:137634ff4186 543
ansond 0:137634ff4186 544 /*
ansond 0:137634ff4186 545 * Checkup routine
ansond 0:137634ff4186 546 */
ansond 0:137634ff4186 547 int md5_self_test( int verbose )
ansond 0:137634ff4186 548 {
ansond 0:137634ff4186 549 int i, buflen;
ansond 0:137634ff4186 550 unsigned char buf[1024];
ansond 0:137634ff4186 551 unsigned char md5sum[16];
ansond 0:137634ff4186 552 md5_context ctx;
ansond 0:137634ff4186 553
ansond 0:137634ff4186 554 for( i = 0; i < 7; i++ )
ansond 0:137634ff4186 555 {
ansond 0:137634ff4186 556 if( verbose != 0 )
ansond 0:137634ff4186 557 polarssl_printf( " MD5 test #%d: ", i + 1 );
ansond 0:137634ff4186 558
ansond 0:137634ff4186 559 md5( md5_test_buf[i], md5_test_buflen[i], md5sum );
ansond 0:137634ff4186 560
ansond 0:137634ff4186 561 if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
ansond 0:137634ff4186 562 {
ansond 0:137634ff4186 563 if( verbose != 0 )
ansond 0:137634ff4186 564 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 565
ansond 0:137634ff4186 566 return( 1 );
ansond 0:137634ff4186 567 }
ansond 0:137634ff4186 568
ansond 0:137634ff4186 569 if( verbose != 0 )
ansond 0:137634ff4186 570 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 571 }
ansond 0:137634ff4186 572
ansond 0:137634ff4186 573 if( verbose != 0 )
ansond 0:137634ff4186 574 polarssl_printf( "\n" );
ansond 0:137634ff4186 575
ansond 0:137634ff4186 576 for( i = 0; i < 7; i++ )
ansond 0:137634ff4186 577 {
ansond 0:137634ff4186 578 if( verbose != 0 )
ansond 0:137634ff4186 579 polarssl_printf( " HMAC-MD5 test #%d: ", i + 1 );
ansond 0:137634ff4186 580
ansond 0:137634ff4186 581 if( i == 5 || i == 6 )
ansond 0:137634ff4186 582 {
ansond 0:137634ff4186 583 memset( buf, 0xAA, buflen = 80 );
ansond 0:137634ff4186 584 md5_hmac_starts( &ctx, buf, buflen );
ansond 0:137634ff4186 585 }
ansond 0:137634ff4186 586 else
ansond 0:137634ff4186 587 md5_hmac_starts( &ctx, md5_hmac_test_key[i],
ansond 0:137634ff4186 588 md5_hmac_test_keylen[i] );
ansond 0:137634ff4186 589
ansond 0:137634ff4186 590 md5_hmac_update( &ctx, md5_hmac_test_buf[i],
ansond 0:137634ff4186 591 md5_hmac_test_buflen[i] );
ansond 0:137634ff4186 592
ansond 0:137634ff4186 593 md5_hmac_finish( &ctx, md5sum );
ansond 0:137634ff4186 594
ansond 0:137634ff4186 595 buflen = ( i == 4 ) ? 12 : 16;
ansond 0:137634ff4186 596
ansond 0:137634ff4186 597 if( memcmp( md5sum, md5_hmac_test_sum[i], buflen ) != 0 )
ansond 0:137634ff4186 598 {
ansond 0:137634ff4186 599 if( verbose != 0 )
ansond 0:137634ff4186 600 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 601
ansond 0:137634ff4186 602 return( 1 );
ansond 0:137634ff4186 603 }
ansond 0:137634ff4186 604
ansond 0:137634ff4186 605 if( verbose != 0 )
ansond 0:137634ff4186 606 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 607 }
ansond 0:137634ff4186 608
ansond 0:137634ff4186 609 if( verbose != 0 )
ansond 0:137634ff4186 610 polarssl_printf( "\n" );
ansond 0:137634ff4186 611
ansond 0:137634ff4186 612 return( 0 );
ansond 0:137634ff4186 613 }
ansond 0:137634ff4186 614
ansond 0:137634ff4186 615 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 616
ansond 0:137634ff4186 617 #endif /* POLARSSL_MD5_C */
ansond 0:137634ff4186 618