]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/md5.c
Add 'hf 15 csetuid' command to set UID on ISO15693 Magic tags (#842)
[proxmark3-svn] / common / mbedtls / md5.c
1 /*
2 * RFC 1321 compliant MD5 implementation
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23 /*
24 * The MD5 algorithm was designed by Ron Rivest in 1991.
25 *
26 * http://www.ietf.org/rfc/rfc1321.txt
27 */
28
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
31 #else
32 #include MBEDTLS_CONFIG_FILE
33 #endif
34
35 #if defined(MBEDTLS_MD5_C)
36
37 #include "mbedtls/md5.h"
38 #include "mbedtls/platform_util.h"
39
40 #include <string.h>
41
42 #if defined(MBEDTLS_SELF_TEST)
43 #if defined(MBEDTLS_PLATFORM_C)
44 #include "mbedtls/platform.h"
45 #else
46 #include <stdio.h>
47 #define mbedtls_printf printf
48 #endif /* MBEDTLS_PLATFORM_C */
49 #endif /* MBEDTLS_SELF_TEST */
50
51 #if !defined(MBEDTLS_MD5_ALT)
52
53 /*
54 * 32-bit integer manipulation macros (little endian)
55 */
56 #ifndef GET_UINT32_LE
57 #define GET_UINT32_LE(n,b,i) \
58 { \
59 (n) = ( (uint32_t) (b)[(i) ] ) \
60 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
61 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
62 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
63 }
64 #endif
65
66 #ifndef PUT_UINT32_LE
67 #define PUT_UINT32_LE(n,b,i) \
68 { \
69 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
70 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
71 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
72 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
73 }
74 #endif
75
76 void mbedtls_md5_init( mbedtls_md5_context *ctx )
77 {
78 memset( ctx, 0, sizeof( mbedtls_md5_context ) );
79 }
80
81 void mbedtls_md5_free( mbedtls_md5_context *ctx )
82 {
83 if( ctx == NULL )
84 return;
85
86 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md5_context ) );
87 }
88
89 void mbedtls_md5_clone( mbedtls_md5_context *dst,
90 const mbedtls_md5_context *src )
91 {
92 *dst = *src;
93 }
94
95 /*
96 * MD5 context setup
97 */
98 int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
99 {
100 ctx->total[0] = 0;
101 ctx->total[1] = 0;
102
103 ctx->state[0] = 0x67452301;
104 ctx->state[1] = 0xEFCDAB89;
105 ctx->state[2] = 0x98BADCFE;
106 ctx->state[3] = 0x10325476;
107
108 return( 0 );
109 }
110
111 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
112 void mbedtls_md5_starts( mbedtls_md5_context *ctx )
113 {
114 mbedtls_md5_starts_ret( ctx );
115 }
116 #endif
117
118 #if !defined(MBEDTLS_MD5_PROCESS_ALT)
119 int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
120 const unsigned char data[64] )
121 {
122 uint32_t X[16], A, B, C, D;
123
124 GET_UINT32_LE( X[ 0], data, 0 );
125 GET_UINT32_LE( X[ 1], data, 4 );
126 GET_UINT32_LE( X[ 2], data, 8 );
127 GET_UINT32_LE( X[ 3], data, 12 );
128 GET_UINT32_LE( X[ 4], data, 16 );
129 GET_UINT32_LE( X[ 5], data, 20 );
130 GET_UINT32_LE( X[ 6], data, 24 );
131 GET_UINT32_LE( X[ 7], data, 28 );
132 GET_UINT32_LE( X[ 8], data, 32 );
133 GET_UINT32_LE( X[ 9], data, 36 );
134 GET_UINT32_LE( X[10], data, 40 );
135 GET_UINT32_LE( X[11], data, 44 );
136 GET_UINT32_LE( X[12], data, 48 );
137 GET_UINT32_LE( X[13], data, 52 );
138 GET_UINT32_LE( X[14], data, 56 );
139 GET_UINT32_LE( X[15], data, 60 );
140
141 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
142
143 #define P(a,b,c,d,k,s,t) \
144 { \
145 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
146 }
147
148 A = ctx->state[0];
149 B = ctx->state[1];
150 C = ctx->state[2];
151 D = ctx->state[3];
152
153 #define F(x,y,z) (z ^ (x & (y ^ z)))
154
155 P( A, B, C, D, 0, 7, 0xD76AA478 );
156 P( D, A, B, C, 1, 12, 0xE8C7B756 );
157 P( C, D, A, B, 2, 17, 0x242070DB );
158 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
159 P( A, B, C, D, 4, 7, 0xF57C0FAF );
160 P( D, A, B, C, 5, 12, 0x4787C62A );
161 P( C, D, A, B, 6, 17, 0xA8304613 );
162 P( B, C, D, A, 7, 22, 0xFD469501 );
163 P( A, B, C, D, 8, 7, 0x698098D8 );
164 P( D, A, B, C, 9, 12, 0x8B44F7AF );
165 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
166 P( B, C, D, A, 11, 22, 0x895CD7BE );
167 P( A, B, C, D, 12, 7, 0x6B901122 );
168 P( D, A, B, C, 13, 12, 0xFD987193 );
169 P( C, D, A, B, 14, 17, 0xA679438E );
170 P( B, C, D, A, 15, 22, 0x49B40821 );
171
172 #undef F
173
174 #define F(x,y,z) (y ^ (z & (x ^ y)))
175
176 P( A, B, C, D, 1, 5, 0xF61E2562 );
177 P( D, A, B, C, 6, 9, 0xC040B340 );
178 P( C, D, A, B, 11, 14, 0x265E5A51 );
179 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
180 P( A, B, C, D, 5, 5, 0xD62F105D );
181 P( D, A, B, C, 10, 9, 0x02441453 );
182 P( C, D, A, B, 15, 14, 0xD8A1E681 );
183 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
184 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
185 P( D, A, B, C, 14, 9, 0xC33707D6 );
186 P( C, D, A, B, 3, 14, 0xF4D50D87 );
187 P( B, C, D, A, 8, 20, 0x455A14ED );
188 P( A, B, C, D, 13, 5, 0xA9E3E905 );
189 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
190 P( C, D, A, B, 7, 14, 0x676F02D9 );
191 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
192
193 #undef F
194
195 #define F(x,y,z) (x ^ y ^ z)
196
197 P( A, B, C, D, 5, 4, 0xFFFA3942 );
198 P( D, A, B, C, 8, 11, 0x8771F681 );
199 P( C, D, A, B, 11, 16, 0x6D9D6122 );
200 P( B, C, D, A, 14, 23, 0xFDE5380C );
201 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
202 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
203 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
204 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
205 P( A, B, C, D, 13, 4, 0x289B7EC6 );
206 P( D, A, B, C, 0, 11, 0xEAA127FA );
207 P( C, D, A, B, 3, 16, 0xD4EF3085 );
208 P( B, C, D, A, 6, 23, 0x04881D05 );
209 P( A, B, C, D, 9, 4, 0xD9D4D039 );
210 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
211 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
212 P( B, C, D, A, 2, 23, 0xC4AC5665 );
213
214 #undef F
215
216 #define F(x,y,z) (y ^ (x | ~z))
217
218 P( A, B, C, D, 0, 6, 0xF4292244 );
219 P( D, A, B, C, 7, 10, 0x432AFF97 );
220 P( C, D, A, B, 14, 15, 0xAB9423A7 );
221 P( B, C, D, A, 5, 21, 0xFC93A039 );
222 P( A, B, C, D, 12, 6, 0x655B59C3 );
223 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
224 P( C, D, A, B, 10, 15, 0xFFEFF47D );
225 P( B, C, D, A, 1, 21, 0x85845DD1 );
226 P( A, B, C, D, 8, 6, 0x6FA87E4F );
227 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
228 P( C, D, A, B, 6, 15, 0xA3014314 );
229 P( B, C, D, A, 13, 21, 0x4E0811A1 );
230 P( A, B, C, D, 4, 6, 0xF7537E82 );
231 P( D, A, B, C, 11, 10, 0xBD3AF235 );
232 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
233 P( B, C, D, A, 9, 21, 0xEB86D391 );
234
235 #undef F
236
237 ctx->state[0] += A;
238 ctx->state[1] += B;
239 ctx->state[2] += C;
240 ctx->state[3] += D;
241
242 return( 0 );
243 }
244
245 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
246 void mbedtls_md5_process( mbedtls_md5_context *ctx,
247 const unsigned char data[64] )
248 {
249 mbedtls_internal_md5_process( ctx, data );
250 }
251 #endif
252 #endif /* !MBEDTLS_MD5_PROCESS_ALT */
253
254 /*
255 * MD5 process buffer
256 */
257 int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
258 const unsigned char *input,
259 size_t ilen )
260 {
261 int ret;
262 size_t fill;
263 uint32_t left;
264
265 if( ilen == 0 )
266 return( 0 );
267
268 left = ctx->total[0] & 0x3F;
269 fill = 64 - left;
270
271 ctx->total[0] += (uint32_t) ilen;
272 ctx->total[0] &= 0xFFFFFFFF;
273
274 if( ctx->total[0] < (uint32_t) ilen )
275 ctx->total[1]++;
276
277 if( left && ilen >= fill )
278 {
279 memcpy( (void *) (ctx->buffer + left), input, fill );
280 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
281 return( ret );
282
283 input += fill;
284 ilen -= fill;
285 left = 0;
286 }
287
288 while( ilen >= 64 )
289 {
290 if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
291 return( ret );
292
293 input += 64;
294 ilen -= 64;
295 }
296
297 if( ilen > 0 )
298 {
299 memcpy( (void *) (ctx->buffer + left), input, ilen );
300 }
301
302 return( 0 );
303 }
304
305 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
306 void mbedtls_md5_update( mbedtls_md5_context *ctx,
307 const unsigned char *input,
308 size_t ilen )
309 {
310 mbedtls_md5_update_ret( ctx, input, ilen );
311 }
312 #endif
313
314 /*
315 * MD5 final digest
316 */
317 int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
318 unsigned char output[16] )
319 {
320 int ret;
321 uint32_t used;
322 uint32_t high, low;
323
324 /*
325 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
326 */
327 used = ctx->total[0] & 0x3F;
328
329 ctx->buffer[used++] = 0x80;
330
331 if( used <= 56 )
332 {
333 /* Enough room for padding + length in current block */
334 memset( ctx->buffer + used, 0, 56 - used );
335 }
336 else
337 {
338 /* We'll need an extra block */
339 memset( ctx->buffer + used, 0, 64 - used );
340
341 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
342 return( ret );
343
344 memset( ctx->buffer, 0, 56 );
345 }
346
347 /*
348 * Add message length
349 */
350 high = ( ctx->total[0] >> 29 )
351 | ( ctx->total[1] << 3 );
352 low = ( ctx->total[0] << 3 );
353
354 PUT_UINT32_LE( low, ctx->buffer, 56 );
355 PUT_UINT32_LE( high, ctx->buffer, 60 );
356
357 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
358 return( ret );
359
360 /*
361 * Output final state
362 */
363 PUT_UINT32_LE( ctx->state[0], output, 0 );
364 PUT_UINT32_LE( ctx->state[1], output, 4 );
365 PUT_UINT32_LE( ctx->state[2], output, 8 );
366 PUT_UINT32_LE( ctx->state[3], output, 12 );
367
368 return( 0 );
369 }
370
371 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
372 void mbedtls_md5_finish( mbedtls_md5_context *ctx,
373 unsigned char output[16] )
374 {
375 mbedtls_md5_finish_ret( ctx, output );
376 }
377 #endif
378
379 #endif /* !MBEDTLS_MD5_ALT */
380
381 /*
382 * output = MD5( input buffer )
383 */
384 int mbedtls_md5_ret( const unsigned char *input,
385 size_t ilen,
386 unsigned char output[16] )
387 {
388 int ret;
389 mbedtls_md5_context ctx;
390
391 mbedtls_md5_init( &ctx );
392
393 if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
394 goto exit;
395
396 if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
397 goto exit;
398
399 if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
400 goto exit;
401
402 exit:
403 mbedtls_md5_free( &ctx );
404
405 return( ret );
406 }
407
408 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
409 void mbedtls_md5( const unsigned char *input,
410 size_t ilen,
411 unsigned char output[16] )
412 {
413 mbedtls_md5_ret( input, ilen, output );
414 }
415 #endif
416
417 #if defined(MBEDTLS_SELF_TEST)
418 /*
419 * RFC 1321 test vectors
420 */
421 static const unsigned char md5_test_buf[7][81] =
422 {
423 { "" },
424 { "a" },
425 { "abc" },
426 { "message digest" },
427 { "abcdefghijklmnopqrstuvwxyz" },
428 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
429 { "12345678901234567890123456789012345678901234567890123456789012"
430 "345678901234567890" }
431 };
432
433 static const size_t md5_test_buflen[7] =
434 {
435 0, 1, 3, 14, 26, 62, 80
436 };
437
438 static const unsigned char md5_test_sum[7][16] =
439 {
440 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
441 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
442 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
443 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
444 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
445 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
446 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
447 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
448 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
449 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
450 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
451 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
452 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
453 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
454 };
455
456 /*
457 * Checkup routine
458 */
459 int mbedtls_md5_self_test( int verbose )
460 {
461 int i, ret = 0;
462 unsigned char md5sum[16];
463
464 for( i = 0; i < 7; i++ )
465 {
466 if( verbose != 0 )
467 mbedtls_printf( " MD5 test #%d: ", i + 1 );
468
469 ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
470 if( ret != 0 )
471 goto fail;
472
473 if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
474 {
475 ret = 1;
476 goto fail;
477 }
478
479 if( verbose != 0 )
480 mbedtls_printf( "passed\n" );
481 }
482
483 if( verbose != 0 )
484 mbedtls_printf( "\n" );
485
486 return( 0 );
487
488 fail:
489 if( verbose != 0 )
490 mbedtls_printf( "failed\n" );
491
492 return( ret );
493 }
494
495 #endif /* MBEDTLS_SELF_TEST */
496
497 #endif /* MBEDTLS_MD5_C */
Impressum, Datenschutz