ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/md5.C
Revision: 1.1
Committed: Fri Oct 14 00:21:57 2005 UTC (21 years ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     **********************************************************************
3     ** md5.h -- Header file for implementation of MD5 **
4     ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
5     ** Created: 2/17/90 RLR **
6     ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version **
7     ** Revised (for MD5): RLR 4/27/91 **
8     ** -- G modified to have y&~z instead of y&z **
9     ** -- FF, GG, HH modified to add in last register done **
10     ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
11     ** -- distinct additive constant for each step **
12     ** -- round 4 added, working mod 7 **
13     **********************************************************************
14     */
15    
16     /*
17     **********************************************************************
18     ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
19     ** **
20     ** License to copy and use this software is granted provided that **
21     ** it is identified as the "RSA Data Security, Inc. MD5 Message **
22     ** Digest Algorithm" in all material mentioning or referencing this **
23     ** software or this function. **
24     ** **
25     ** License is also granted to make and use derivative works **
26     ** provided that such works are identified as "derived from the RSA **
27     ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
28     ** material mentioning or referencing the derived work. **
29     ** **
30     ** RSA Data Security, Inc. makes no representations concerning **
31     ** either the merchantability of this software or the suitability **
32     ** of this software for any particular purpose. It is provided "as **
33     ** is" without express or implied warranty of any kind. **
34     ** **
35     ** These notices must be retained in any copies of any part of this **
36     ** documentation and/or software. **
37     **********************************************************************
38     */
39    
40     #include "md5.h"
41    
42     /*
43     **********************************************************************
44     ** End of md5.h **
45     ******************************* (cut) ********************************
46     */
47    
48     /*
49     **********************************************************************
50     ** md5.c **
51     ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
52     ** Created: 2/17/90 RLR **
53     ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
54     **********************************************************************
55     */
56    
57     /*
58     **********************************************************************
59     ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
60     ** **
61     ** License to copy and use this software is granted provided that **
62     ** it is identified as the "RSA Data Security, Inc. MD5 Message **
63     ** Digest Algorithm" in all material mentioning or referencing this **
64     ** software or this function. **
65     ** **
66     ** License is also granted to make and use derivative works **
67     ** provided that such works are identified as "derived from the RSA **
68     ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
69     ** material mentioning or referencing the derived work. **
70     ** **
71     ** RSA Data Security, Inc. makes no representations concerning **
72     ** either the merchantability of this software or the suitability **
73     ** of this software for any particular purpose. It is provided "as **
74     ** is" without express or implied warranty of any kind. **
75     ** **
76     ** These notices must be retained in any copies of any part of this **
77     ** documentation and/or software. **
78     **********************************************************************
79     */
80    
81     /* -- include the following line if the md5.h header file is separate -- */
82     /* #include "md5.h" */
83    
84     /* forward declaration */
85     static void Transform (UINT4 *buf, UINT4 *in);
86    
87     static unsigned char PADDING[64] = {
88     0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
96     };
97    
98     /* F, G and H are basic MD5 functions: selection, majority, parity */
99     #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
100     #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
101     #define H(x, y, z) ((x) ^ (y) ^ (z))
102     #define I(x, y, z) ((y) ^ ((x) | (~z)))
103    
104     /* ROTATE_LEFT rotates x left n bits */
105     #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
106    
107     /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
108     /* Rotation is separate from addition to prevent recomputation */
109     #define FF(a, b, c, d, x, s, ac) \
110     {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
111     (a) = ROTATE_LEFT ((a), (s)); \
112     (a) += (b); \
113     }
114     #define GG(a, b, c, d, x, s, ac) \
115     {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
116     (a) = ROTATE_LEFT ((a), (s)); \
117     (a) += (b); \
118     }
119     #define HH(a, b, c, d, x, s, ac) \
120     {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
121     (a) = ROTATE_LEFT ((a), (s)); \
122     (a) += (b); \
123     }
124     #define II(a, b, c, d, x, s, ac) \
125     {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
126     (a) = ROTATE_LEFT ((a), (s)); \
127     (a) += (b); \
128     }
129    
130     void MD5Init (MD5_CTX *mdContext)
131     {
132     mdContext->i[0] = mdContext->i[1] = (UINT4)0;
133    
134     /* Load magic initialization constants.
135     */
136     mdContext->buf[0] = (UINT4)0x67452301;
137     mdContext->buf[1] = (UINT4)0xefcdab89;
138     mdContext->buf[2] = (UINT4)0x98badcfe;
139     mdContext->buf[3] = (UINT4)0x10325476;
140     }
141    
142     void MD5Update (MD5_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
143     {
144     UINT4 in[16];
145     int mdi;
146     unsigned int i, ii;
147    
148     /* compute number of bytes mod 64 */
149     mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
150    
151     /* update number of bits */
152     if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
153     mdContext->i[1]++;
154     mdContext->i[0] += ((UINT4)inLen << 3);
155     mdContext->i[1] += ((UINT4)inLen >> 29);
156    
157     while (inLen--) {
158     /* add new character to buffer, increment mdi */
159     mdContext->in[mdi++] = *inBuf++;
160    
161     /* transform if necessary */
162     if (mdi == 0x40) {
163     for (i = 0, ii = 0; i < 16; i++, ii += 4)
164     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
165     (((UINT4)mdContext->in[ii+2]) << 16) |
166     (((UINT4)mdContext->in[ii+1]) << 8) |
167     ((UINT4)mdContext->in[ii]);
168     Transform (mdContext->buf, in);
169     mdi = 0;
170     }
171     }
172     }
173    
174     void MD5Final (MD5_CTX *mdContext)
175     {
176     UINT4 in[16];
177     int mdi;
178     unsigned int i, ii;
179     unsigned int padLen;
180    
181     /* save number of bits */
182     in[14] = mdContext->i[0];
183     in[15] = mdContext->i[1];
184    
185     /* compute number of bytes mod 64 */
186     mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
187    
188     /* pad out to 56 mod 64 */
189     padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
190     MD5Update (mdContext, PADDING, padLen);
191    
192     /* append length in bits and transform */
193     for (i = 0, ii = 0; i < 14; i++, ii += 4)
194     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
195     (((UINT4)mdContext->in[ii+2]) << 16) |
196     (((UINT4)mdContext->in[ii+1]) << 8) |
197     ((UINT4)mdContext->in[ii]);
198     Transform (mdContext->buf, in);
199    
200     /* store buffer in digest */
201     for (i = 0, ii = 0; i < 4; i++, ii += 4) {
202     mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
203     mdContext->digest[ii+1] =
204     (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
205     mdContext->digest[ii+2] =
206     (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
207     mdContext->digest[ii+3] =
208     (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
209     }
210     }
211    
212     /* Basic MD5 step. Transform buf based on in.
213     */
214     static void Transform (UINT4 *buf, UINT4 *in)
215     {
216     UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
217    
218     /* Round 1 */
219     #define S11 7
220     #define S12 12
221     #define S13 17
222     #define S14 22
223     FF ( a, b, c, d, in[ 0], S11, 3614090360UL); /* 1 */
224     FF ( d, a, b, c, in[ 1], S12, 3905402710UL); /* 2 */
225     FF ( c, d, a, b, in[ 2], S13, 606105819UL); /* 3 */
226     FF ( b, c, d, a, in[ 3], S14, 3250441966UL); /* 4 */
227     FF ( a, b, c, d, in[ 4], S11, 4118548399UL); /* 5 */
228     FF ( d, a, b, c, in[ 5], S12, 1200080426UL); /* 6 */
229     FF ( c, d, a, b, in[ 6], S13, 2821735955UL); /* 7 */
230     FF ( b, c, d, a, in[ 7], S14, 4249261313UL); /* 8 */
231     FF ( a, b, c, d, in[ 8], S11, 1770035416UL); /* 9 */
232     FF ( d, a, b, c, in[ 9], S12, 2336552879UL); /* 10 */
233     FF ( c, d, a, b, in[10], S13, 4294925233UL); /* 11 */
234     FF ( b, c, d, a, in[11], S14, 2304563134UL); /* 12 */
235     FF ( a, b, c, d, in[12], S11, 1804603682UL); /* 13 */
236     FF ( d, a, b, c, in[13], S12, 4254626195UL); /* 14 */
237     FF ( c, d, a, b, in[14], S13, 2792965006UL); /* 15 */
238     FF ( b, c, d, a, in[15], S14, 1236535329UL); /* 16 */
239    
240     /* Round 2 */
241     #define S21 5
242     #define S22 9
243     #define S23 14
244     #define S24 20
245     GG ( a, b, c, d, in[ 1], S21, 4129170786UL); /* 17 */
246     GG ( d, a, b, c, in[ 6], S22, 3225465664UL); /* 18 */
247     GG ( c, d, a, b, in[11], S23, 643717713UL); /* 19 */
248     GG ( b, c, d, a, in[ 0], S24, 3921069994UL); /* 20 */
249     GG ( a, b, c, d, in[ 5], S21, 3593408605UL); /* 21 */
250     GG ( d, a, b, c, in[10], S22, 38016083UL); /* 22 */
251     GG ( c, d, a, b, in[15], S23, 3634488961UL); /* 23 */
252     GG ( b, c, d, a, in[ 4], S24, 3889429448UL); /* 24 */
253     GG ( a, b, c, d, in[ 9], S21, 568446438UL); /* 25 */
254     GG ( d, a, b, c, in[14], S22, 3275163606UL); /* 26 */
255     GG ( c, d, a, b, in[ 3], S23, 4107603335UL); /* 27 */
256     GG ( b, c, d, a, in[ 8], S24, 1163531501UL); /* 28 */
257     GG ( a, b, c, d, in[13], S21, 2850285829UL); /* 29 */
258     GG ( d, a, b, c, in[ 2], S22, 4243563512UL); /* 30 */
259     GG ( c, d, a, b, in[ 7], S23, 1735328473UL); /* 31 */
260     GG ( b, c, d, a, in[12], S24, 2368359562UL); /* 32 */
261    
262     /* Round 3 */
263     #define S31 4
264     #define S32 11
265     #define S33 16
266     #define S34 23
267     HH ( a, b, c, d, in[ 5], S31, 4294588738UL); /* 33 */
268     HH ( d, a, b, c, in[ 8], S32, 2272392833UL); /* 34 */
269     HH ( c, d, a, b, in[11], S33, 1839030562UL); /* 35 */
270     HH ( b, c, d, a, in[14], S34, 4259657740UL); /* 36 */
271     HH ( a, b, c, d, in[ 1], S31, 2763975236UL); /* 37 */
272     HH ( d, a, b, c, in[ 4], S32, 1272893353UL); /* 38 */
273     HH ( c, d, a, b, in[ 7], S33, 4139469664UL); /* 39 */
274     HH ( b, c, d, a, in[10], S34, 3200236656UL); /* 40 */
275     HH ( a, b, c, d, in[13], S31, 681279174UL); /* 41 */
276     HH ( d, a, b, c, in[ 0], S32, 3936430074UL); /* 42 */
277     HH ( c, d, a, b, in[ 3], S33, 3572445317UL); /* 43 */
278     HH ( b, c, d, a, in[ 6], S34, 76029189UL); /* 44 */
279     HH ( a, b, c, d, in[ 9], S31, 3654602809UL); /* 45 */
280     HH ( d, a, b, c, in[12], S32, 3873151461UL); /* 46 */
281     HH ( c, d, a, b, in[15], S33, 530742520UL); /* 47 */
282     HH ( b, c, d, a, in[ 2], S34, 3299628645UL); /* 48 */
283    
284     /* Round 4 */
285     #define S41 6
286     #define S42 10
287     #define S43 15
288     #define S44 21
289     II ( a, b, c, d, in[ 0], S41, 4096336452UL); /* 49 */
290     II ( d, a, b, c, in[ 7], S42, 1126891415UL); /* 50 */
291     II ( c, d, a, b, in[14], S43, 2878612391UL); /* 51 */
292     II ( b, c, d, a, in[ 5], S44, 4237533241UL); /* 52 */
293     II ( a, b, c, d, in[12], S41, 1700485571UL); /* 53 */
294     II ( d, a, b, c, in[ 3], S42, 2399980690UL); /* 54 */
295     II ( c, d, a, b, in[10], S43, 4293915773UL); /* 55 */
296     II ( b, c, d, a, in[ 1], S44, 2240044497UL); /* 56 */
297     II ( a, b, c, d, in[ 8], S41, 1873313359UL); /* 57 */
298     II ( d, a, b, c, in[15], S42, 4264355552UL); /* 58 */
299     II ( c, d, a, b, in[ 6], S43, 2734768916UL); /* 59 */
300     II ( b, c, d, a, in[13], S44, 1309151649UL); /* 60 */
301     II ( a, b, c, d, in[ 4], S41, 4149444226UL); /* 61 */
302     II ( d, a, b, c, in[11], S42, 3174756917UL); /* 62 */
303     II ( c, d, a, b, in[ 2], S43, 718787259UL); /* 63 */
304     II ( b, c, d, a, in[ 9], S44, 3951481745UL); /* 64 */
305    
306     buf[0] += a;
307     buf[1] += b;
308     buf[2] += c;
309     buf[3] += d;
310     }
311    
312     /*
313     **********************************************************************
314     ** End of md5.c **
315     ******************************* (cut) ********************************
316     */