~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Externals/polarssl/library/md2.c

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  RFC 1115/1319 compliant MD2 implementation
 
3
 *
 
4
 *  Copyright (C) 2006-2013, Brainspark B.V.
 
5
 *
 
6
 *  This file is part of PolarSSL (http://www.polarssl.org)
 
7
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 
8
 *
 
9
 *  All rights reserved.
 
10
 *
 
11
 *  This program is free software; you can redistribute it and/or modify
 
12
 *  it under the terms of the GNU General Public License as published by
 
13
 *  the Free Software Foundation; either version 2 of the License, or
 
14
 *  (at your option) any later version.
 
15
 *
 
16
 *  This program is distributed in the hope that it will be useful,
 
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 *  GNU General Public License for more details.
 
20
 *
 
21
 *  You should have received a copy of the GNU General Public License along
 
22
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 
23
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
24
 */
 
25
/*
 
26
 *  The MD2 algorithm was designed by Ron Rivest in 1989.
 
27
 *
 
28
 *  http://www.ietf.org/rfc/rfc1115.txt
 
29
 *  http://www.ietf.org/rfc/rfc1319.txt
 
30
 */
 
31
 
 
32
#include "polarssl/config.h"
 
33
 
 
34
#if defined(POLARSSL_MD2_C)
 
35
 
 
36
#include "polarssl/md2.h"
 
37
 
 
38
#if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST)
 
39
#include <stdio.h>
 
40
#endif
 
41
 
 
42
#if !defined(POLARSSL_MD2_ALT)
 
43
 
 
44
static const unsigned char PI_SUBST[256] =
 
45
{
 
46
    0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
 
47
    0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
 
48
    0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
 
49
    0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
 
50
    0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
 
51
    0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
 
52
    0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
 
53
    0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
 
54
    0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
 
55
    0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
 
56
    0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
 
57
    0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
 
58
    0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
 
59
    0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
 
60
    0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
 
61
    0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
 
62
    0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
 
63
    0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
 
64
    0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
 
65
    0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
 
66
    0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
 
67
    0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
 
68
    0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
 
69
    0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
 
70
    0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
 
71
    0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
 
72
};
 
73
 
 
74
/*
 
75
 * MD2 context setup
 
76
 */
 
77
void md2_starts( md2_context *ctx )
 
78
{
 
79
    memset( ctx->cksum, 0, 16 );
 
80
    memset( ctx->state, 0, 46 );
 
81
    memset( ctx->buffer, 0, 16 );
 
82
    ctx->left = 0;
 
83
}
 
84
 
 
85
static void md2_process( md2_context *ctx )
 
86
{
 
87
    int i, j;
 
88
    unsigned char t = 0;
 
89
 
 
90
    for( i = 0; i < 16; i++ )
 
91
    {
 
92
        ctx->state[i + 16] = ctx->buffer[i];
 
93
        ctx->state[i + 32] =
 
94
            (unsigned char)( ctx->buffer[i] ^ ctx->state[i]);
 
95
    }
 
96
 
 
97
    for( i = 0; i < 18; i++ )
 
98
    {
 
99
        for( j = 0; j < 48; j++ )
 
100
        {
 
101
            ctx->state[j] = (unsigned char)
 
102
               ( ctx->state[j] ^ PI_SUBST[t] );
 
103
            t  = ctx->state[j];
 
104
        }
 
105
 
 
106
        t = (unsigned char)( t + i );
 
107
    }
 
108
 
 
109
    t = ctx->cksum[15];
 
110
 
 
111
    for( i = 0; i < 16; i++ )
 
112
    {
 
113
        ctx->cksum[i] = (unsigned char)
 
114
           ( ctx->cksum[i] ^ PI_SUBST[ctx->buffer[i] ^ t] );
 
115
        t  = ctx->cksum[i];
 
116
    }
 
117
}
 
118
 
 
119
/*
 
120
 * MD2 process buffer
 
121
 */
 
122
void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen )
 
123
{
 
124
    size_t fill;
 
125
 
 
126
    while( ilen > 0 )
 
127
    {
 
128
        if( ctx->left + ilen > 16 )
 
129
            fill = 16 - ctx->left;
 
130
        else
 
131
            fill = ilen;
 
132
 
 
133
        memcpy( ctx->buffer + ctx->left, input, fill );
 
134
 
 
135
        ctx->left += fill;
 
136
        input += fill;
 
137
        ilen  -= fill;
 
138
 
 
139
        if( ctx->left == 16 )
 
140
        {
 
141
            ctx->left = 0;
 
142
            md2_process( ctx );
 
143
        }
 
144
    }
 
145
}
 
146
 
 
147
/*
 
148
 * MD2 final digest
 
149
 */
 
150
void md2_finish( md2_context *ctx, unsigned char output[16] )
 
151
{
 
152
    size_t i;
 
153
    unsigned char x;
 
154
 
 
155
    x = (unsigned char)( 16 - ctx->left );
 
156
 
 
157
    for( i = ctx->left; i < 16; i++ )
 
158
        ctx->buffer[i] = x;
 
159
 
 
160
    md2_process( ctx );
 
161
 
 
162
    memcpy( ctx->buffer, ctx->cksum, 16 );
 
163
    md2_process( ctx );
 
164
 
 
165
    memcpy( output, ctx->state, 16 );
 
166
}
 
167
 
 
168
#endif /* !POLARSSL_MD2_ALT */
 
169
 
 
170
/*
 
171
 * output = MD2( input buffer )
 
172
 */
 
173
void md2( const unsigned char *input, size_t ilen, unsigned char output[16] )
 
174
{
 
175
    md2_context ctx;
 
176
 
 
177
    md2_starts( &ctx );
 
178
    md2_update( &ctx, input, ilen );
 
179
    md2_finish( &ctx, output );
 
180
 
 
181
    memset( &ctx, 0, sizeof( md2_context ) );
 
182
}
 
183
 
 
184
#if defined(POLARSSL_FS_IO)
 
185
/*
 
186
 * output = MD2( file contents )
 
187
 */
 
188
int md2_file( const char *path, unsigned char output[16] )
 
189
{
 
190
    FILE *f;
 
191
    size_t n;
 
192
    md2_context ctx;
 
193
    unsigned char buf[1024];
 
194
 
 
195
    if( ( f = fopen( path, "rb" ) ) == NULL )
 
196
        return( POLARSSL_ERR_MD2_FILE_IO_ERROR );
 
197
 
 
198
    md2_starts( &ctx );
 
199
 
 
200
    while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
 
201
        md2_update( &ctx, buf, n );
 
202
 
 
203
    md2_finish( &ctx, output );
 
204
 
 
205
    memset( &ctx, 0, sizeof( md2_context ) );
 
206
 
 
207
    if( ferror( f ) != 0 )
 
208
    {
 
209
        fclose( f );
 
210
        return( POLARSSL_ERR_MD2_FILE_IO_ERROR );
 
211
    }
 
212
 
 
213
    fclose( f );
 
214
    return( 0 );
 
215
}
 
216
#endif /* POLARSSL_FS_IO */
 
217
 
 
218
/*
 
219
 * MD2 HMAC context setup
 
220
 */
 
221
void md2_hmac_starts( md2_context *ctx, const unsigned char *key, size_t keylen )
 
222
{
 
223
    size_t i;
 
224
    unsigned char sum[16];
 
225
 
 
226
    if( keylen > 16 )
 
227
    {
 
228
        md2( key, keylen, sum );
 
229
        keylen = 16;
 
230
        key = sum;
 
231
    }
 
232
 
 
233
    memset( ctx->ipad, 0x36, 16 );
 
234
    memset( ctx->opad, 0x5C, 16 );
 
235
 
 
236
    for( i = 0; i < keylen; i++ )
 
237
    {
 
238
        ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
 
239
        ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
 
240
    }
 
241
 
 
242
    md2_starts( ctx );
 
243
    md2_update( ctx, ctx->ipad, 16 );
 
244
 
 
245
    memset( sum, 0, sizeof( sum ) );
 
246
}
 
247
 
 
248
/*
 
249
 * MD2 HMAC process buffer
 
250
 */
 
251
void md2_hmac_update( md2_context *ctx, const unsigned char *input, size_t ilen )
 
252
{
 
253
    md2_update( ctx, input, ilen );
 
254
}
 
255
 
 
256
/*
 
257
 * MD2 HMAC final digest
 
258
 */
 
259
void md2_hmac_finish( md2_context *ctx, unsigned char output[16] )
 
260
{
 
261
    unsigned char tmpbuf[16];
 
262
 
 
263
    md2_finish( ctx, tmpbuf );
 
264
    md2_starts( ctx );
 
265
    md2_update( ctx, ctx->opad, 16 );
 
266
    md2_update( ctx, tmpbuf, 16 );
 
267
    md2_finish( ctx, output );
 
268
 
 
269
    memset( tmpbuf, 0, sizeof( tmpbuf ) );
 
270
}
 
271
 
 
272
/*
 
273
 * MD2 HMAC context reset
 
274
 */
 
275
void md2_hmac_reset( md2_context *ctx )
 
276
{
 
277
    md2_starts( ctx );
 
278
    md2_update( ctx, ctx->ipad, 16 );
 
279
}
 
280
 
 
281
/*
 
282
 * output = HMAC-MD2( hmac key, input buffer )
 
283
 */
 
284
void md2_hmac( const unsigned char *key, size_t keylen,
 
285
               const unsigned char *input, size_t ilen,
 
286
               unsigned char output[16] )
 
287
{
 
288
    md2_context ctx;
 
289
 
 
290
    md2_hmac_starts( &ctx, key, keylen );
 
291
    md2_hmac_update( &ctx, input, ilen );
 
292
    md2_hmac_finish( &ctx, output );
 
293
 
 
294
    memset( &ctx, 0, sizeof( md2_context ) );
 
295
}
 
296
 
 
297
#if defined(POLARSSL_SELF_TEST)
 
298
 
 
299
/*
 
300
 * RFC 1319 test vectors
 
301
 */
 
302
static const char md2_test_str[7][81] =
 
303
{
 
304
    { "" },
 
305
    { "a" },
 
306
    { "abc" },
 
307
    { "message digest" },
 
308
    { "abcdefghijklmnopqrstuvwxyz" },
 
309
    { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
 
310
    { "12345678901234567890123456789012345678901234567890123456789012" \
 
311
      "345678901234567890" }
 
312
};
 
313
 
 
314
static const unsigned char md2_test_sum[7][16] =
 
315
{
 
316
    { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D,
 
317
      0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 },
 
318
    { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72,
 
319
      0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 },
 
320
    { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B,
 
321
      0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB },
 
322
    { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B,
 
323
      0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 },
 
324
    { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB,
 
325
      0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B },
 
326
    { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39,
 
327
      0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD },
 
328
    { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D,
 
329
      0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 }
 
330
};
 
331
 
 
332
/*
 
333
 * Checkup routine
 
334
 */
 
335
int md2_self_test( int verbose )
 
336
{
 
337
    int i;
 
338
    unsigned char md2sum[16];
 
339
 
 
340
    for( i = 0; i < 7; i++ )
 
341
    {
 
342
        if( verbose != 0 )
 
343
            printf( "  MD2 test #%d: ", i + 1 );
 
344
 
 
345
        md2( (unsigned char *) md2_test_str[i],
 
346
             strlen( md2_test_str[i] ), md2sum );
 
347
 
 
348
        if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 )
 
349
        {
 
350
            if( verbose != 0 )
 
351
                printf( "failed\n" );
 
352
 
 
353
            return( 1 );
 
354
        }
 
355
 
 
356
        if( verbose != 0 )
 
357
            printf( "passed\n" );
 
358
    }
 
359
 
 
360
    if( verbose != 0 )
 
361
        printf( "\n" );
 
362
 
 
363
    return( 0 );
 
364
}
 
365
 
 
366
#endif
 
367
 
 
368
#endif