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 1186/1320 compliant MD4 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 MD4 algorithm was designed by Ron Rivest in 1990.
ansond 0:137634ff4186 24 *
ansond 0:137634ff4186 25 * http://www.ietf.org/rfc/rfc1186.txt
ansond 0:137634ff4186 26 * http://www.ietf.org/rfc/rfc1320.txt
ansond 0:137634ff4186 27 */
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 30 #include "polarssl/config.h"
ansond 0:137634ff4186 31 #else
ansond 0:137634ff4186 32 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 33 #endif
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(POLARSSL_MD4_C)
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #include "polarssl/md4.h"
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #include <string.h>
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 42 #include <stdio.h>
ansond 0:137634ff4186 43 #endif
ansond 0:137634ff4186 44
ansond 0:137634ff4186 45 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 46 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 47 #include "polarssl/platform.h"
ansond 0:137634ff4186 48 #else
ansond 0:137634ff4186 49 #include <stdio.h>
ansond 0:137634ff4186 50 #define polarssl_printf printf
ansond 0:137634ff4186 51 #endif /* POLARSSL_PLATFORM_C */
ansond 0:137634ff4186 52 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 53
ansond 0:137634ff4186 54 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 55 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 56 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 57 }
ansond 0:137634ff4186 58
ansond 0:137634ff4186 59 #if !defined(POLARSSL_MD4_ALT)
ansond 0:137634ff4186 60
ansond 0:137634ff4186 61 /*
ansond 0:137634ff4186 62 * 32-bit integer manipulation macros (little endian)
ansond 0:137634ff4186 63 */
ansond 0:137634ff4186 64 #ifndef GET_UINT32_LE
ansond 0:137634ff4186 65 #define GET_UINT32_LE(n,b,i) \
ansond 0:137634ff4186 66 { \
ansond 0:137634ff4186 67 (n) = ( (uint32_t) (b)[(i) ] ) \
ansond 0:137634ff4186 68 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
ansond 0:137634ff4186 69 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
ansond 0:137634ff4186 70 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
ansond 0:137634ff4186 71 }
ansond 0:137634ff4186 72 #endif
ansond 0:137634ff4186 73
ansond 0:137634ff4186 74 #ifndef PUT_UINT32_LE
ansond 0:137634ff4186 75 #define PUT_UINT32_LE(n,b,i) \
ansond 0:137634ff4186 76 { \
ansond 0:137634ff4186 77 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
ansond 0:137634ff4186 78 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
ansond 0:137634ff4186 79 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
ansond 0:137634ff4186 80 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
ansond 0:137634ff4186 81 }
ansond 0:137634ff4186 82 #endif
ansond 0:137634ff4186 83
ansond 0:137634ff4186 84 void md4_init( md4_context *ctx )
ansond 0:137634ff4186 85 {
ansond 0:137634ff4186 86 memset( ctx, 0, sizeof( md4_context ) );
ansond 0:137634ff4186 87 }
ansond 0:137634ff4186 88
ansond 0:137634ff4186 89 void md4_free( md4_context *ctx )
ansond 0:137634ff4186 90 {
ansond 0:137634ff4186 91 if( ctx == NULL )
ansond 0:137634ff4186 92 return;
ansond 0:137634ff4186 93
ansond 0:137634ff4186 94 polarssl_zeroize( ctx, sizeof( md4_context ) );
ansond 0:137634ff4186 95 }
ansond 0:137634ff4186 96
ansond 0:137634ff4186 97 /*
ansond 0:137634ff4186 98 * MD4 context setup
ansond 0:137634ff4186 99 */
ansond 0:137634ff4186 100 void md4_starts( md4_context *ctx )
ansond 0:137634ff4186 101 {
ansond 0:137634ff4186 102 ctx->total[0] = 0;
ansond 0:137634ff4186 103 ctx->total[1] = 0;
ansond 0:137634ff4186 104
ansond 0:137634ff4186 105 ctx->state[0] = 0x67452301;
ansond 0:137634ff4186 106 ctx->state[1] = 0xEFCDAB89;
ansond 0:137634ff4186 107 ctx->state[2] = 0x98BADCFE;
ansond 0:137634ff4186 108 ctx->state[3] = 0x10325476;
ansond 0:137634ff4186 109 }
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 void md4_process( md4_context *ctx, const unsigned char data[64] )
ansond 0:137634ff4186 112 {
ansond 0:137634ff4186 113 uint32_t X[16], A, B, C, D;
ansond 0:137634ff4186 114
ansond 0:137634ff4186 115 GET_UINT32_LE( X[ 0], data, 0 );
ansond 0:137634ff4186 116 GET_UINT32_LE( X[ 1], data, 4 );
ansond 0:137634ff4186 117 GET_UINT32_LE( X[ 2], data, 8 );
ansond 0:137634ff4186 118 GET_UINT32_LE( X[ 3], data, 12 );
ansond 0:137634ff4186 119 GET_UINT32_LE( X[ 4], data, 16 );
ansond 0:137634ff4186 120 GET_UINT32_LE( X[ 5], data, 20 );
ansond 0:137634ff4186 121 GET_UINT32_LE( X[ 6], data, 24 );
ansond 0:137634ff4186 122 GET_UINT32_LE( X[ 7], data, 28 );
ansond 0:137634ff4186 123 GET_UINT32_LE( X[ 8], data, 32 );
ansond 0:137634ff4186 124 GET_UINT32_LE( X[ 9], data, 36 );
ansond 0:137634ff4186 125 GET_UINT32_LE( X[10], data, 40 );
ansond 0:137634ff4186 126 GET_UINT32_LE( X[11], data, 44 );
ansond 0:137634ff4186 127 GET_UINT32_LE( X[12], data, 48 );
ansond 0:137634ff4186 128 GET_UINT32_LE( X[13], data, 52 );
ansond 0:137634ff4186 129 GET_UINT32_LE( X[14], data, 56 );
ansond 0:137634ff4186 130 GET_UINT32_LE( X[15], data, 60 );
ansond 0:137634ff4186 131
ansond 0:137634ff4186 132 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
ansond 0:137634ff4186 133
ansond 0:137634ff4186 134 A = ctx->state[0];
ansond 0:137634ff4186 135 B = ctx->state[1];
ansond 0:137634ff4186 136 C = ctx->state[2];
ansond 0:137634ff4186 137 D = ctx->state[3];
ansond 0:137634ff4186 138
ansond 0:137634ff4186 139 #define F(x, y, z) ((x & y) | ((~x) & z))
ansond 0:137634ff4186 140 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
ansond 0:137634ff4186 141
ansond 0:137634ff4186 142 P( A, B, C, D, X[ 0], 3 );
ansond 0:137634ff4186 143 P( D, A, B, C, X[ 1], 7 );
ansond 0:137634ff4186 144 P( C, D, A, B, X[ 2], 11 );
ansond 0:137634ff4186 145 P( B, C, D, A, X[ 3], 19 );
ansond 0:137634ff4186 146 P( A, B, C, D, X[ 4], 3 );
ansond 0:137634ff4186 147 P( D, A, B, C, X[ 5], 7 );
ansond 0:137634ff4186 148 P( C, D, A, B, X[ 6], 11 );
ansond 0:137634ff4186 149 P( B, C, D, A, X[ 7], 19 );
ansond 0:137634ff4186 150 P( A, B, C, D, X[ 8], 3 );
ansond 0:137634ff4186 151 P( D, A, B, C, X[ 9], 7 );
ansond 0:137634ff4186 152 P( C, D, A, B, X[10], 11 );
ansond 0:137634ff4186 153 P( B, C, D, A, X[11], 19 );
ansond 0:137634ff4186 154 P( A, B, C, D, X[12], 3 );
ansond 0:137634ff4186 155 P( D, A, B, C, X[13], 7 );
ansond 0:137634ff4186 156 P( C, D, A, B, X[14], 11 );
ansond 0:137634ff4186 157 P( B, C, D, A, X[15], 19 );
ansond 0:137634ff4186 158
ansond 0:137634ff4186 159 #undef P
ansond 0:137634ff4186 160 #undef F
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 #define F(x,y,z) ((x & y) | (x & z) | (y & z))
ansond 0:137634ff4186 163 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
ansond 0:137634ff4186 164
ansond 0:137634ff4186 165 P( A, B, C, D, X[ 0], 3 );
ansond 0:137634ff4186 166 P( D, A, B, C, X[ 4], 5 );
ansond 0:137634ff4186 167 P( C, D, A, B, X[ 8], 9 );
ansond 0:137634ff4186 168 P( B, C, D, A, X[12], 13 );
ansond 0:137634ff4186 169 P( A, B, C, D, X[ 1], 3 );
ansond 0:137634ff4186 170 P( D, A, B, C, X[ 5], 5 );
ansond 0:137634ff4186 171 P( C, D, A, B, X[ 9], 9 );
ansond 0:137634ff4186 172 P( B, C, D, A, X[13], 13 );
ansond 0:137634ff4186 173 P( A, B, C, D, X[ 2], 3 );
ansond 0:137634ff4186 174 P( D, A, B, C, X[ 6], 5 );
ansond 0:137634ff4186 175 P( C, D, A, B, X[10], 9 );
ansond 0:137634ff4186 176 P( B, C, D, A, X[14], 13 );
ansond 0:137634ff4186 177 P( A, B, C, D, X[ 3], 3 );
ansond 0:137634ff4186 178 P( D, A, B, C, X[ 7], 5 );
ansond 0:137634ff4186 179 P( C, D, A, B, X[11], 9 );
ansond 0:137634ff4186 180 P( B, C, D, A, X[15], 13 );
ansond 0:137634ff4186 181
ansond 0:137634ff4186 182 #undef P
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 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
ansond 0:137634ff4186 187
ansond 0:137634ff4186 188 P( A, B, C, D, X[ 0], 3 );
ansond 0:137634ff4186 189 P( D, A, B, C, X[ 8], 9 );
ansond 0:137634ff4186 190 P( C, D, A, B, X[ 4], 11 );
ansond 0:137634ff4186 191 P( B, C, D, A, X[12], 15 );
ansond 0:137634ff4186 192 P( A, B, C, D, X[ 2], 3 );
ansond 0:137634ff4186 193 P( D, A, B, C, X[10], 9 );
ansond 0:137634ff4186 194 P( C, D, A, B, X[ 6], 11 );
ansond 0:137634ff4186 195 P( B, C, D, A, X[14], 15 );
ansond 0:137634ff4186 196 P( A, B, C, D, X[ 1], 3 );
ansond 0:137634ff4186 197 P( D, A, B, C, X[ 9], 9 );
ansond 0:137634ff4186 198 P( C, D, A, B, X[ 5], 11 );
ansond 0:137634ff4186 199 P( B, C, D, A, X[13], 15 );
ansond 0:137634ff4186 200 P( A, B, C, D, X[ 3], 3 );
ansond 0:137634ff4186 201 P( D, A, B, C, X[11], 9 );
ansond 0:137634ff4186 202 P( C, D, A, B, X[ 7], 11 );
ansond 0:137634ff4186 203 P( B, C, D, A, X[15], 15 );
ansond 0:137634ff4186 204
ansond 0:137634ff4186 205 #undef F
ansond 0:137634ff4186 206 #undef P
ansond 0:137634ff4186 207
ansond 0:137634ff4186 208 ctx->state[0] += A;
ansond 0:137634ff4186 209 ctx->state[1] += B;
ansond 0:137634ff4186 210 ctx->state[2] += C;
ansond 0:137634ff4186 211 ctx->state[3] += D;
ansond 0:137634ff4186 212 }
ansond 0:137634ff4186 213
ansond 0:137634ff4186 214 /*
ansond 0:137634ff4186 215 * MD4 process buffer
ansond 0:137634ff4186 216 */
ansond 0:137634ff4186 217 void md4_update( md4_context *ctx, const unsigned char *input, size_t ilen )
ansond 0:137634ff4186 218 {
ansond 0:137634ff4186 219 size_t fill;
ansond 0:137634ff4186 220 uint32_t left;
ansond 0:137634ff4186 221
ansond 0:137634ff4186 222 if( ilen == 0 )
ansond 0:137634ff4186 223 return;
ansond 0:137634ff4186 224
ansond 0:137634ff4186 225 left = ctx->total[0] & 0x3F;
ansond 0:137634ff4186 226 fill = 64 - left;
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 ctx->total[0] += (uint32_t) ilen;
ansond 0:137634ff4186 229 ctx->total[0] &= 0xFFFFFFFF;
ansond 0:137634ff4186 230
ansond 0:137634ff4186 231 if( ctx->total[0] < (uint32_t) ilen )
ansond 0:137634ff4186 232 ctx->total[1]++;
ansond 0:137634ff4186 233
ansond 0:137634ff4186 234 if( left && ilen >= fill )
ansond 0:137634ff4186 235 {
ansond 0:137634ff4186 236 memcpy( (void *) (ctx->buffer + left),
ansond 0:137634ff4186 237 (void *) input, fill );
ansond 0:137634ff4186 238 md4_process( ctx, ctx->buffer );
ansond 0:137634ff4186 239 input += fill;
ansond 0:137634ff4186 240 ilen -= fill;
ansond 0:137634ff4186 241 left = 0;
ansond 0:137634ff4186 242 }
ansond 0:137634ff4186 243
ansond 0:137634ff4186 244 while( ilen >= 64 )
ansond 0:137634ff4186 245 {
ansond 0:137634ff4186 246 md4_process( ctx, input );
ansond 0:137634ff4186 247 input += 64;
ansond 0:137634ff4186 248 ilen -= 64;
ansond 0:137634ff4186 249 }
ansond 0:137634ff4186 250
ansond 0:137634ff4186 251 if( ilen > 0 )
ansond 0:137634ff4186 252 {
ansond 0:137634ff4186 253 memcpy( (void *) (ctx->buffer + left),
ansond 0:137634ff4186 254 (void *) input, ilen );
ansond 0:137634ff4186 255 }
ansond 0:137634ff4186 256 }
ansond 0:137634ff4186 257
ansond 0:137634ff4186 258 static const unsigned char md4_padding[64] =
ansond 0:137634ff4186 259 {
ansond 0:137634ff4186 260 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ansond 0:137634ff4186 263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
ansond 0:137634ff4186 264 };
ansond 0:137634ff4186 265
ansond 0:137634ff4186 266 /*
ansond 0:137634ff4186 267 * MD4 final digest
ansond 0:137634ff4186 268 */
ansond 0:137634ff4186 269 void md4_finish( md4_context *ctx, unsigned char output[16] )
ansond 0:137634ff4186 270 {
ansond 0:137634ff4186 271 uint32_t last, padn;
ansond 0:137634ff4186 272 uint32_t high, low;
ansond 0:137634ff4186 273 unsigned char msglen[8];
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 high = ( ctx->total[0] >> 29 )
ansond 0:137634ff4186 276 | ( ctx->total[1] << 3 );
ansond 0:137634ff4186 277 low = ( ctx->total[0] << 3 );
ansond 0:137634ff4186 278
ansond 0:137634ff4186 279 PUT_UINT32_LE( low, msglen, 0 );
ansond 0:137634ff4186 280 PUT_UINT32_LE( high, msglen, 4 );
ansond 0:137634ff4186 281
ansond 0:137634ff4186 282 last = ctx->total[0] & 0x3F;
ansond 0:137634ff4186 283 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
ansond 0:137634ff4186 284
ansond 0:137634ff4186 285 md4_update( ctx, (unsigned char *) md4_padding, padn );
ansond 0:137634ff4186 286 md4_update( ctx, msglen, 8 );
ansond 0:137634ff4186 287
ansond 0:137634ff4186 288 PUT_UINT32_LE( ctx->state[0], output, 0 );
ansond 0:137634ff4186 289 PUT_UINT32_LE( ctx->state[1], output, 4 );
ansond 0:137634ff4186 290 PUT_UINT32_LE( ctx->state[2], output, 8 );
ansond 0:137634ff4186 291 PUT_UINT32_LE( ctx->state[3], output, 12 );
ansond 0:137634ff4186 292 }
ansond 0:137634ff4186 293
ansond 0:137634ff4186 294 #endif /* !POLARSSL_MD4_ALT */
ansond 0:137634ff4186 295
ansond 0:137634ff4186 296 /*
ansond 0:137634ff4186 297 * output = MD4( input buffer )
ansond 0:137634ff4186 298 */
ansond 0:137634ff4186 299 void md4( const unsigned char *input, size_t ilen, unsigned char output[16] )
ansond 0:137634ff4186 300 {
ansond 0:137634ff4186 301 md4_context ctx;
ansond 0:137634ff4186 302
ansond 0:137634ff4186 303 md4_init( &ctx );
ansond 0:137634ff4186 304 md4_starts( &ctx );
ansond 0:137634ff4186 305 md4_update( &ctx, input, ilen );
ansond 0:137634ff4186 306 md4_finish( &ctx, output );
ansond 0:137634ff4186 307 md4_free( &ctx );
ansond 0:137634ff4186 308 }
ansond 0:137634ff4186 309
ansond 0:137634ff4186 310 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 311 /*
ansond 0:137634ff4186 312 * output = MD4( file contents )
ansond 0:137634ff4186 313 */
ansond 0:137634ff4186 314 int md4_file( const char *path, unsigned char output[16] )
ansond 0:137634ff4186 315 {
ansond 0:137634ff4186 316 FILE *f;
ansond 0:137634ff4186 317 size_t n;
ansond 0:137634ff4186 318 md4_context ctx;
ansond 0:137634ff4186 319 unsigned char buf[1024];
ansond 0:137634ff4186 320
ansond 0:137634ff4186 321 if( ( f = fopen( path, "rb" ) ) == NULL )
ansond 0:137634ff4186 322 return( POLARSSL_ERR_MD4_FILE_IO_ERROR );
ansond 0:137634ff4186 323
ansond 0:137634ff4186 324 md4_init( &ctx );
ansond 0:137634ff4186 325 md4_starts( &ctx );
ansond 0:137634ff4186 326
ansond 0:137634ff4186 327 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
ansond 0:137634ff4186 328 md4_update( &ctx, buf, n );
ansond 0:137634ff4186 329
ansond 0:137634ff4186 330 md4_finish( &ctx, output );
ansond 0:137634ff4186 331 md4_free( &ctx );
ansond 0:137634ff4186 332
ansond 0:137634ff4186 333 if( ferror( f ) != 0 )
ansond 0:137634ff4186 334 {
ansond 0:137634ff4186 335 fclose( f );
ansond 0:137634ff4186 336 return( POLARSSL_ERR_MD4_FILE_IO_ERROR );
ansond 0:137634ff4186 337 }
ansond 0:137634ff4186 338
ansond 0:137634ff4186 339 fclose( f );
ansond 0:137634ff4186 340 return( 0 );
ansond 0:137634ff4186 341 }
ansond 0:137634ff4186 342 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 343
ansond 0:137634ff4186 344 /*
ansond 0:137634ff4186 345 * MD4 HMAC context setup
ansond 0:137634ff4186 346 */
ansond 0:137634ff4186 347 void md4_hmac_starts( md4_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 348 size_t keylen )
ansond 0:137634ff4186 349 {
ansond 0:137634ff4186 350 size_t i;
ansond 0:137634ff4186 351 unsigned char sum[16];
ansond 0:137634ff4186 352
ansond 0:137634ff4186 353 if( keylen > 64 )
ansond 0:137634ff4186 354 {
ansond 0:137634ff4186 355 md4( key, keylen, sum );
ansond 0:137634ff4186 356 keylen = 16;
ansond 0:137634ff4186 357 key = sum;
ansond 0:137634ff4186 358 }
ansond 0:137634ff4186 359
ansond 0:137634ff4186 360 memset( ctx->ipad, 0x36, 64 );
ansond 0:137634ff4186 361 memset( ctx->opad, 0x5C, 64 );
ansond 0:137634ff4186 362
ansond 0:137634ff4186 363 for( i = 0; i < keylen; i++ )
ansond 0:137634ff4186 364 {
ansond 0:137634ff4186 365 ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
ansond 0:137634ff4186 366 ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
ansond 0:137634ff4186 367 }
ansond 0:137634ff4186 368
ansond 0:137634ff4186 369 md4_starts( ctx );
ansond 0:137634ff4186 370 md4_update( ctx, ctx->ipad, 64 );
ansond 0:137634ff4186 371
ansond 0:137634ff4186 372 polarssl_zeroize( sum, sizeof( sum ) );
ansond 0:137634ff4186 373 }
ansond 0:137634ff4186 374
ansond 0:137634ff4186 375 /*
ansond 0:137634ff4186 376 * MD4 HMAC process buffer
ansond 0:137634ff4186 377 */
ansond 0:137634ff4186 378 void md4_hmac_update( md4_context *ctx, const unsigned char *input,
ansond 0:137634ff4186 379 size_t ilen )
ansond 0:137634ff4186 380 {
ansond 0:137634ff4186 381 md4_update( ctx, input, ilen );
ansond 0:137634ff4186 382 }
ansond 0:137634ff4186 383
ansond 0:137634ff4186 384 /*
ansond 0:137634ff4186 385 * MD4 HMAC final digest
ansond 0:137634ff4186 386 */
ansond 0:137634ff4186 387 void md4_hmac_finish( md4_context *ctx, unsigned char output[16] )
ansond 0:137634ff4186 388 {
ansond 0:137634ff4186 389 unsigned char tmpbuf[16];
ansond 0:137634ff4186 390
ansond 0:137634ff4186 391 md4_finish( ctx, tmpbuf );
ansond 0:137634ff4186 392 md4_starts( ctx );
ansond 0:137634ff4186 393 md4_update( ctx, ctx->opad, 64 );
ansond 0:137634ff4186 394 md4_update( ctx, tmpbuf, 16 );
ansond 0:137634ff4186 395 md4_finish( ctx, output );
ansond 0:137634ff4186 396
ansond 0:137634ff4186 397 polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
ansond 0:137634ff4186 398 }
ansond 0:137634ff4186 399
ansond 0:137634ff4186 400 /*
ansond 0:137634ff4186 401 * MD4 HMAC context reset
ansond 0:137634ff4186 402 */
ansond 0:137634ff4186 403 void md4_hmac_reset( md4_context *ctx )
ansond 0:137634ff4186 404 {
ansond 0:137634ff4186 405 md4_starts( ctx );
ansond 0:137634ff4186 406 md4_update( ctx, ctx->ipad, 64 );
ansond 0:137634ff4186 407 }
ansond 0:137634ff4186 408
ansond 0:137634ff4186 409 /*
ansond 0:137634ff4186 410 * output = HMAC-MD4( hmac key, input buffer )
ansond 0:137634ff4186 411 */
ansond 0:137634ff4186 412 void md4_hmac( const unsigned char *key, size_t keylen,
ansond 0:137634ff4186 413 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 414 unsigned char output[16] )
ansond 0:137634ff4186 415 {
ansond 0:137634ff4186 416 md4_context ctx;
ansond 0:137634ff4186 417
ansond 0:137634ff4186 418 md4_init( &ctx );
ansond 0:137634ff4186 419 md4_hmac_starts( &ctx, key, keylen );
ansond 0:137634ff4186 420 md4_hmac_update( &ctx, input, ilen );
ansond 0:137634ff4186 421 md4_hmac_finish( &ctx, output );
ansond 0:137634ff4186 422 md4_free( &ctx );
ansond 0:137634ff4186 423 }
ansond 0:137634ff4186 424
ansond 0:137634ff4186 425 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 426
ansond 0:137634ff4186 427 /*
ansond 0:137634ff4186 428 * RFC 1320 test vectors
ansond 0:137634ff4186 429 */
ansond 0:137634ff4186 430 static const char md4_test_str[7][81] =
ansond 0:137634ff4186 431 {
ansond 0:137634ff4186 432 { "" },
ansond 0:137634ff4186 433 { "a" },
ansond 0:137634ff4186 434 { "abc" },
ansond 0:137634ff4186 435 { "message digest" },
ansond 0:137634ff4186 436 { "abcdefghijklmnopqrstuvwxyz" },
ansond 0:137634ff4186 437 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
ansond 0:137634ff4186 438 { "12345678901234567890123456789012345678901234567890123456789012" \
ansond 0:137634ff4186 439 "345678901234567890" }
ansond 0:137634ff4186 440 };
ansond 0:137634ff4186 441
ansond 0:137634ff4186 442 static const unsigned char md4_test_sum[7][16] =
ansond 0:137634ff4186 443 {
ansond 0:137634ff4186 444 { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
ansond 0:137634ff4186 445 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
ansond 0:137634ff4186 446 { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
ansond 0:137634ff4186 447 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
ansond 0:137634ff4186 448 { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
ansond 0:137634ff4186 449 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
ansond 0:137634ff4186 450 { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
ansond 0:137634ff4186 451 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
ansond 0:137634ff4186 452 { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
ansond 0:137634ff4186 453 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
ansond 0:137634ff4186 454 { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
ansond 0:137634ff4186 455 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
ansond 0:137634ff4186 456 { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
ansond 0:137634ff4186 457 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
ansond 0:137634ff4186 458 };
ansond 0:137634ff4186 459
ansond 0:137634ff4186 460 /*
ansond 0:137634ff4186 461 * Checkup routine
ansond 0:137634ff4186 462 */
ansond 0:137634ff4186 463 int md4_self_test( int verbose )
ansond 0:137634ff4186 464 {
ansond 0:137634ff4186 465 int i;
ansond 0:137634ff4186 466 unsigned char md4sum[16];
ansond 0:137634ff4186 467
ansond 0:137634ff4186 468 for( i = 0; i < 7; i++ )
ansond 0:137634ff4186 469 {
ansond 0:137634ff4186 470 if( verbose != 0 )
ansond 0:137634ff4186 471 polarssl_printf( " MD4 test #%d: ", i + 1 );
ansond 0:137634ff4186 472
ansond 0:137634ff4186 473 md4( (unsigned char *) md4_test_str[i],
ansond 0:137634ff4186 474 strlen( md4_test_str[i] ), md4sum );
ansond 0:137634ff4186 475
ansond 0:137634ff4186 476 if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
ansond 0:137634ff4186 477 {
ansond 0:137634ff4186 478 if( verbose != 0 )
ansond 0:137634ff4186 479 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 480
ansond 0:137634ff4186 481 return( 1 );
ansond 0:137634ff4186 482 }
ansond 0:137634ff4186 483
ansond 0:137634ff4186 484 if( verbose != 0 )
ansond 0:137634ff4186 485 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 486 }
ansond 0:137634ff4186 487
ansond 0:137634ff4186 488 if( verbose != 0 )
ansond 0:137634ff4186 489 polarssl_printf( "\n" );
ansond 0:137634ff4186 490
ansond 0:137634ff4186 491 return( 0 );
ansond 0:137634ff4186 492 }
ansond 0:137634ff4186 493
ansond 0:137634ff4186 494 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 495
ansond 0:137634ff4186 496 #endif /* POLARSSL_MD4_C */
ansond 0:137634ff4186 497