OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [kern/] [md5c.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/kern/md5c.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*
23
 * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
24
 *
25
 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
26
 * rights reserved.
27
 *
28
 * License to copy and use this software is granted provided that it
29
 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
30
 * Algorithm" in all material mentioning or referencing this software
31
 * or this function.
32
 *
33
 * License is also granted to make and use derivative works provided
34
 * that such works are identified as "derived from the RSA Data
35
 * Security, Inc. MD5 Message-Digest Algorithm" in all material
36
 * mentioning or referencing the derived work.
37
 *
38
 * RSA Data Security, Inc. makes no representations concerning either
39
 * the merchantability of this software or the suitability of this
40
 * software for any particular purpose. It is provided "as is"
41
 * without express or implied warranty of any kind.
42
 *
43
 * These notices must be retained in any copies of any part of this
44
 * documentation and/or software.
45
 *
46
 * $FreeBSD: src/sys/kern/md5c.c,v 1.17 1999/12/29 04:54:39 peter Exp $
47
 *
48
 * This code is the same as the code published by RSA Inc.  It has been
49
 * edited for clarity and style only.
50
 */
51
 
52
#include <sys/param.h>
53
#include <sys/types.h>
54
 
55
#ifdef _KERNEL
56
#include <string.h>
57
#endif
58
 
59
#include <sys/md5.h>
60
 
61
 
62
#ifdef _KERNEL
63
#define memset(x,y,z)   bzero(x,z);
64
#define memcpy(x,y,z)   bcopy(y, x, z)
65
#endif
66
 
67
#if defined(__i386__) || defined(__alpha__)
68
#define Encode memcpy
69
#define Decode memcpy
70
#else /* __i386__ */
71
 
72
/*
73
 * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
74
 * a multiple of 4.
75
 */
76
 
77
/* XXX not prototyped, and not compatible with memcpy(). */
78
static void
79
Encode (unsigned char *output, u_int32_t *input, unsigned int len)
80
{
81
        unsigned int i, j;
82
 
83
        for (i = 0, j = 0; j < len; i++, j += 4) {
84
                output[j] = (unsigned char)(input[i] & 0xff);
85
                output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
86
                output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
87
                output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
88
        }
89
}
90
 
91
/*
92
 * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
93
 * a multiple of 4.
94
 */
95
 
96
static void
97
Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
98
{
99
        unsigned int i, j;
100
 
101
        for (i = 0, j = 0; j < len; i++, j += 4)
102
                output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) |
103
                    (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24);
104
}
105
#endif /* i386 */
106
 
107
static unsigned char PADDING[64] = {
108
  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
111
};
112
 
113
/* F, G, H and I are basic MD5 functions. */
114
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
115
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
116
#define H(x, y, z) ((x) ^ (y) ^ (z))
117
#define I(x, y, z) ((y) ^ ((x) | (~z)))
118
 
119
/* ROTATE_LEFT rotates x left n bits. */
120
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
121
 
122
/*
123
 * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
124
 * Rotation is separate from addition to prevent recomputation.
125
 */
126
#define FF(a, b, c, d, x, s, ac) { \
127
        (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
128
        (a) = ROTATE_LEFT ((a), (s)); \
129
        (a) += (b); \
130
        }
131
#define GG(a, b, c, d, x, s, ac) { \
132
        (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
133
        (a) = ROTATE_LEFT ((a), (s)); \
134
        (a) += (b); \
135
        }
136
#define HH(a, b, c, d, x, s, ac) { \
137
        (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
138
        (a) = ROTATE_LEFT ((a), (s)); \
139
        (a) += (b); \
140
        }
141
#define II(a, b, c, d, x, s, ac) { \
142
        (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
143
        (a) = ROTATE_LEFT ((a), (s)); \
144
        (a) += (b); \
145
        }
146
 
147
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
148
 
149
void
150
MD5Init (context)
151
        MD5_CTX *context;
152
{
153
 
154
        context->count[0] = context->count[1] = 0;
155
 
156
        /* Load magic initialization constants.  */
157
        context->state[0] = 0x67452301;
158
        context->state[1] = 0xefcdab89;
159
        context->state[2] = 0x98badcfe;
160
        context->state[3] = 0x10325476;
161
}
162
 
163
/*
164
 * MD5 block update operation. Continues an MD5 message-digest
165
 * operation, processing another message block, and updating the
166
 * context.
167
 */
168
 
169
void
170
MD5Update (context, input, inputLen)
171
        MD5_CTX *context;
172
        const unsigned char *input;
173
        unsigned int inputLen;
174
{
175
        unsigned int i, index, partLen;
176
 
177
        /* Compute number of bytes mod 64 */
178
        index = (unsigned int)((context->count[0] >> 3) & 0x3F);
179
 
180
        /* Update number of bits */
181
        if ((context->count[0] += ((u_int32_t)inputLen << 3))
182
            < ((u_int32_t)inputLen << 3))
183
                context->count[1]++;
184
        context->count[1] += ((u_int32_t)inputLen >> 29);
185
 
186
        partLen = 64 - index;
187
 
188
        /* Transform as many times as possible. */
189
        if (inputLen >= partLen) {
190
                memcpy((void *)&context->buffer[index], (const void *)input,
191
                    partLen);
192
                MD5Transform (context->state, context->buffer);
193
 
194
                for (i = partLen; i + 63 < inputLen; i += 64)
195
                        MD5Transform (context->state, &input[i]);
196
 
197
                index = 0;
198
        }
199
        else
200
                i = 0;
201
 
202
        /* Buffer remaining input */
203
        memcpy ((void *)&context->buffer[index], (const void *)&input[i],
204
            inputLen-i);
205
}
206
 
207
/*
208
 * MD5 padding. Adds padding followed by original length.
209
 */
210
 
211
void
212
MD5Pad (context)
213
        MD5_CTX *context;
214
{
215
        unsigned char bits[8];
216
        unsigned int index, padLen;
217
 
218
        /* Save number of bits */
219
        Encode (bits, context->count, 8);
220
 
221
        /* Pad out to 56 mod 64. */
222
        index = (unsigned int)((context->count[0] >> 3) & 0x3f);
223
        padLen = (index < 56) ? (56 - index) : (120 - index);
224
        MD5Update (context, PADDING, padLen);
225
 
226
        /* Append length (before padding) */
227
        MD5Update (context, bits, 8);
228
}
229
 
230
/*
231
 * MD5 finalization. Ends an MD5 message-digest operation, writing the
232
 * the message digest and zeroizing the context.
233
 */
234
 
235
void
236
MD5Final (digest, context)
237
        unsigned char digest[16];
238
        MD5_CTX *context;
239
{
240
        /* Do padding. */
241
        MD5Pad (context);
242
 
243
        /* Store state in digest */
244
        Encode (digest, context->state, 16);
245
 
246
        /* Zeroize sensitive information. */
247
        memset ((void *)context, 0, sizeof (*context));
248
}
249
 
250
/* MD5 basic transformation. Transforms state based on block. */
251
 
252
void
253
MD5Transform (state, block)
254
        u_int32_t state[4];
255
        const unsigned char block[64];
256
{
257
        u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
258
 
259
        Decode (x, block, 64);
260
 
261
        /* Round 1 */
262
#define S11 7
263
#define S12 12
264
#define S13 17
265
#define S14 22
266
        FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
267
        FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
268
        FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
269
        FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
270
        FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
271
        FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
272
        FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
273
        FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
274
        FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
275
        FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
276
        FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
277
        FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
278
        FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
279
        FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
280
        FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
281
        FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
282
 
283
        /* Round 2 */
284
#define S21 5
285
#define S22 9
286
#define S23 14
287
#define S24 20
288
        GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
289
        GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
290
        GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
291
        GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
292
        GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
293
        GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
294
        GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
295
        GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
296
        GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
297
        GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
298
        GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
299
        GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
300
        GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
301
        GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
302
        GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
303
        GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
304
 
305
        /* Round 3 */
306
#define S31 4
307
#define S32 11
308
#define S33 16
309
#define S34 23
310
        HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
311
        HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
312
        HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
313
        HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
314
        HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
315
        HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
316
        HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
317
        HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
318
        HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
319
        HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
320
        HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
321
        HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
322
        HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
323
        HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
324
        HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
325
        HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
326
 
327
        /* Round 4 */
328
#define S41 6
329
#define S42 10
330
#define S43 15
331
#define S44 21
332
        II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
333
        II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
334
        II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
335
        II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
336
        II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
337
        II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
338
        II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
339
        II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
340
        II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
341
        II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
342
        II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
343
        II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
344
        II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
345
        II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
346
        II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
347
        II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
348
 
349
        state[0] += a;
350
        state[1] += b;
351
        state[2] += c;
352
        state[3] += d;
353
 
354
        /* Zeroize sensitive information. */
355
        memset ((void *)x, 0, sizeof (x));
356
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.