Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8774 rgimad 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 
41
 
42
#if defined(MBEDTLS_SELF_TEST)
43
#if defined(MBEDTLS_PLATFORM_C)
44
#include "mbedtls/platform.h"
45
#else
46
#include 
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)                                                          \
142
    ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) )
143
 
144
#define P(a,b,c,d,k,s,t)                                        \
145
    do                                                          \
146
    {                                                           \
147
        (a) += F((b),(c),(d)) + X[(k)] + (t);                   \
148
        (a) = S((a),(s)) + (b);                                 \
149
    } while( 0 )
150
 
151
    A = ctx->state[0];
152
    B = ctx->state[1];
153
    C = ctx->state[2];
154
    D = ctx->state[3];
155
 
156
#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
157
 
158
    P( A, B, C, D,  0,  7, 0xD76AA478 );
159
    P( D, A, B, C,  1, 12, 0xE8C7B756 );
160
    P( C, D, A, B,  2, 17, 0x242070DB );
161
    P( B, C, D, A,  3, 22, 0xC1BDCEEE );
162
    P( A, B, C, D,  4,  7, 0xF57C0FAF );
163
    P( D, A, B, C,  5, 12, 0x4787C62A );
164
    P( C, D, A, B,  6, 17, 0xA8304613 );
165
    P( B, C, D, A,  7, 22, 0xFD469501 );
166
    P( A, B, C, D,  8,  7, 0x698098D8 );
167
    P( D, A, B, C,  9, 12, 0x8B44F7AF );
168
    P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
169
    P( B, C, D, A, 11, 22, 0x895CD7BE );
170
    P( A, B, C, D, 12,  7, 0x6B901122 );
171
    P( D, A, B, C, 13, 12, 0xFD987193 );
172
    P( C, D, A, B, 14, 17, 0xA679438E );
173
    P( B, C, D, A, 15, 22, 0x49B40821 );
174
 
175
#undef F
176
 
177
#define F(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
178
 
179
    P( A, B, C, D,  1,  5, 0xF61E2562 );
180
    P( D, A, B, C,  6,  9, 0xC040B340 );
181
    P( C, D, A, B, 11, 14, 0x265E5A51 );
182
    P( B, C, D, A,  0, 20, 0xE9B6C7AA );
183
    P( A, B, C, D,  5,  5, 0xD62F105D );
184
    P( D, A, B, C, 10,  9, 0x02441453 );
185
    P( C, D, A, B, 15, 14, 0xD8A1E681 );
186
    P( B, C, D, A,  4, 20, 0xE7D3FBC8 );
187
    P( A, B, C, D,  9,  5, 0x21E1CDE6 );
188
    P( D, A, B, C, 14,  9, 0xC33707D6 );
189
    P( C, D, A, B,  3, 14, 0xF4D50D87 );
190
    P( B, C, D, A,  8, 20, 0x455A14ED );
191
    P( A, B, C, D, 13,  5, 0xA9E3E905 );
192
    P( D, A, B, C,  2,  9, 0xFCEFA3F8 );
193
    P( C, D, A, B,  7, 14, 0x676F02D9 );
194
    P( B, C, D, A, 12, 20, 0x8D2A4C8A );
195
 
196
#undef F
197
 
198
#define F(x,y,z) ((x) ^ (y) ^ (z))
199
 
200
    P( A, B, C, D,  5,  4, 0xFFFA3942 );
201
    P( D, A, B, C,  8, 11, 0x8771F681 );
202
    P( C, D, A, B, 11, 16, 0x6D9D6122 );
203
    P( B, C, D, A, 14, 23, 0xFDE5380C );
204
    P( A, B, C, D,  1,  4, 0xA4BEEA44 );
205
    P( D, A, B, C,  4, 11, 0x4BDECFA9 );
206
    P( C, D, A, B,  7, 16, 0xF6BB4B60 );
207
    P( B, C, D, A, 10, 23, 0xBEBFBC70 );
208
    P( A, B, C, D, 13,  4, 0x289B7EC6 );
209
    P( D, A, B, C,  0, 11, 0xEAA127FA );
210
    P( C, D, A, B,  3, 16, 0xD4EF3085 );
211
    P( B, C, D, A,  6, 23, 0x04881D05 );
212
    P( A, B, C, D,  9,  4, 0xD9D4D039 );
213
    P( D, A, B, C, 12, 11, 0xE6DB99E5 );
214
    P( C, D, A, B, 15, 16, 0x1FA27CF8 );
215
    P( B, C, D, A,  2, 23, 0xC4AC5665 );
216
 
217
#undef F
218
 
219
#define F(x,y,z) ((y) ^ ((x) | ~(z)))
220
 
221
    P( A, B, C, D,  0,  6, 0xF4292244 );
222
    P( D, A, B, C,  7, 10, 0x432AFF97 );
223
    P( C, D, A, B, 14, 15, 0xAB9423A7 );
224
    P( B, C, D, A,  5, 21, 0xFC93A039 );
225
    P( A, B, C, D, 12,  6, 0x655B59C3 );
226
    P( D, A, B, C,  3, 10, 0x8F0CCC92 );
227
    P( C, D, A, B, 10, 15, 0xFFEFF47D );
228
    P( B, C, D, A,  1, 21, 0x85845DD1 );
229
    P( A, B, C, D,  8,  6, 0x6FA87E4F );
230
    P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
231
    P( C, D, A, B,  6, 15, 0xA3014314 );
232
    P( B, C, D, A, 13, 21, 0x4E0811A1 );
233
    P( A, B, C, D,  4,  6, 0xF7537E82 );
234
    P( D, A, B, C, 11, 10, 0xBD3AF235 );
235
    P( C, D, A, B,  2, 15, 0x2AD7D2BB );
236
    P( B, C, D, A,  9, 21, 0xEB86D391 );
237
 
238
#undef F
239
 
240
    ctx->state[0] += A;
241
    ctx->state[1] += B;
242
    ctx->state[2] += C;
243
    ctx->state[3] += D;
244
 
245
    return( 0 );
246
}
247
 
248
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
249
void mbedtls_md5_process( mbedtls_md5_context *ctx,
250
                          const unsigned char data[64] )
251
{
252
    mbedtls_internal_md5_process( ctx, data );
253
}
254
#endif
255
#endif /* !MBEDTLS_MD5_PROCESS_ALT */
256
 
257
/*
258
 * MD5 process buffer
259
 */
260
int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
261
                            const unsigned char *input,
262
                            size_t ilen )
263
{
264
    int ret;
265
    size_t fill;
266
    uint32_t left;
267
 
268
    if( ilen == 0 )
269
        return( 0 );
270
 
271
    left = ctx->total[0] & 0x3F;
272
    fill = 64 - left;
273
 
274
    ctx->total[0] += (uint32_t) ilen;
275
    ctx->total[0] &= 0xFFFFFFFF;
276
 
277
    if( ctx->total[0] < (uint32_t) ilen )
278
        ctx->total[1]++;
279
 
280
    if( left && ilen >= fill )
281
    {
282
        memcpy( (void *) (ctx->buffer + left), input, fill );
283
        if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
284
            return( ret );
285
 
286
        input += fill;
287
        ilen  -= fill;
288
        left = 0;
289
    }
290
 
291
    while( ilen >= 64 )
292
    {
293
        if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
294
            return( ret );
295
 
296
        input += 64;
297
        ilen  -= 64;
298
    }
299
 
300
    if( ilen > 0 )
301
    {
302
        memcpy( (void *) (ctx->buffer + left), input, ilen );
303
    }
304
 
305
    return( 0 );
306
}
307
 
308
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
309
void mbedtls_md5_update( mbedtls_md5_context *ctx,
310
                         const unsigned char *input,
311
                         size_t ilen )
312
{
313
    mbedtls_md5_update_ret( ctx, input, ilen );
314
}
315
#endif
316
 
317
/*
318
 * MD5 final digest
319
 */
320
int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
321
                            unsigned char output[16] )
322
{
323
    int ret;
324
    uint32_t used;
325
    uint32_t high, low;
326
 
327
    /*
328
     * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
329
     */
330
    used = ctx->total[0] & 0x3F;
331
 
332
    ctx->buffer[used++] = 0x80;
333
 
334
    if( used <= 56 )
335
    {
336
        /* Enough room for padding + length in current block */
337
        memset( ctx->buffer + used, 0, 56 - used );
338
    }
339
    else
340
    {
341
        /* We'll need an extra block */
342
        memset( ctx->buffer + used, 0, 64 - used );
343
 
344
        if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
345
            return( ret );
346
 
347
        memset( ctx->buffer, 0, 56 );
348
    }
349
 
350
    /*
351
     * Add message length
352
     */
353
    high = ( ctx->total[0] >> 29 )
354
         | ( ctx->total[1] <<  3 );
355
    low  = ( ctx->total[0] <<  3 );
356
 
357
    PUT_UINT32_LE( low,  ctx->buffer, 56 );
358
    PUT_UINT32_LE( high, ctx->buffer, 60 );
359
 
360
    if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
361
        return( ret );
362
 
363
    /*
364
     * Output final state
365
     */
366
    PUT_UINT32_LE( ctx->state[0], output,  0 );
367
    PUT_UINT32_LE( ctx->state[1], output,  4 );
368
    PUT_UINT32_LE( ctx->state[2], output,  8 );
369
    PUT_UINT32_LE( ctx->state[3], output, 12 );
370
 
371
    return( 0 );
372
}
373
 
374
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
375
void mbedtls_md5_finish( mbedtls_md5_context *ctx,
376
                         unsigned char output[16] )
377
{
378
    mbedtls_md5_finish_ret( ctx, output );
379
}
380
#endif
381
 
382
#endif /* !MBEDTLS_MD5_ALT */
383
 
384
/*
385
 * output = MD5( input buffer )
386
 */
387
int mbedtls_md5_ret( const unsigned char *input,
388
                     size_t ilen,
389
                     unsigned char output[16] )
390
{
391
    int ret;
392
    mbedtls_md5_context ctx;
393
 
394
    mbedtls_md5_init( &ctx );
395
 
396
    if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
397
        goto exit;
398
 
399
    if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
400
        goto exit;
401
 
402
    if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
403
        goto exit;
404
 
405
exit:
406
    mbedtls_md5_free( &ctx );
407
 
408
    return( ret );
409
}
410
 
411
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
412
void mbedtls_md5( const unsigned char *input,
413
                  size_t ilen,
414
                  unsigned char output[16] )
415
{
416
    mbedtls_md5_ret( input, ilen, output );
417
}
418
#endif
419
 
420
#if defined(MBEDTLS_SELF_TEST)
421
/*
422
 * RFC 1321 test vectors
423
 */
424
static const unsigned char md5_test_buf[7][81] =
425
{
426
    { "" },
427
    { "a" },
428
    { "abc" },
429
    { "message digest" },
430
    { "abcdefghijklmnopqrstuvwxyz" },
431
    { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
432
    { "12345678901234567890123456789012345678901234567890123456789012"
433
      "345678901234567890" }
434
};
435
 
436
static const size_t md5_test_buflen[7] =
437
{
438
    0, 1, 3, 14, 26, 62, 80
439
};
440
 
441
static const unsigned char md5_test_sum[7][16] =
442
{
443
    { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
444
      0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
445
    { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
446
      0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
447
    { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
448
      0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
449
    { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
450
      0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
451
    { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
452
      0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
453
    { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
454
      0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
455
    { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
456
      0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
457
};
458
 
459
/*
460
 * Checkup routine
461
 */
462
int mbedtls_md5_self_test( int verbose )
463
{
464
    int i, ret = 0;
465
    unsigned char md5sum[16];
466
 
467
    for( i = 0; i < 7; i++ )
468
    {
469
        if( verbose != 0 )
470
            mbedtls_printf( "  MD5 test #%d: ", i + 1 );
471
 
472
        ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
473
        if( ret != 0 )
474
            goto fail;
475
 
476
        if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
477
        {
478
            ret = 1;
479
            goto fail;
480
        }
481
 
482
        if( verbose != 0 )
483
            mbedtls_printf( "passed\n" );
484
    }
485
 
486
    if( verbose != 0 )
487
        mbedtls_printf( "\n" );
488
 
489
    return( 0 );
490
 
491
fail:
492
    if( verbose != 0 )
493
        mbedtls_printf( "failed\n" );
494
 
495
    return( ret );
496
}
497
 
498
#endif /* MBEDTLS_SELF_TEST */
499
 
500
#endif /* MBEDTLS_MD5_C */