xref: /freebsd/crypto/krb5/src/lib/crypto/builtin/md5/md5.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.
4  *
5  * License to copy and use this software is granted provided that
6  * it is identified as the "RSA Data Security, Inc. MD5 Message-
7  * Digest Algorithm" in all material mentioning or referencing this
8  * software or this function.
9  *
10  * License is also granted to make and use derivative works
11  * provided that such works are identified as "derived from the RSA
12  * Data Security, Inc. MD5 Message-Digest Algorithm" in all
13  * material mentioning or referencing the derived work.
14  *
15  * RSA Data Security, Inc. makes no representations concerning
16  * either the merchantability of this software or the suitability
17  * of this software for any particular purpose.  It is provided "as
18  * is" without express or implied warranty of any kind.
19  *
20  * These notices must be retained in any copies of any part of this
21  * documentation and/or software.
22  */
23 
24 /*
25 ***********************************************************************
26 ** md5.c -- the source code for MD5 routines                         **
27 ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
28 ** Created: 2/17/90 RLR                                              **
29 ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
30 ***********************************************************************
31 */
32 
33 /*
34  * Modified by John Carr, MIT, to use Kerberos 5 typedefs.
35  */
36 
37 #include "crypto_int.h"
38 #include "rsa-md5.h"
39 
40 #ifdef K5_BUILTIN_MD5
41 
42 /*
43 ***********************************************************************
44 **  Message-digest routines:                                         **
45 **  To form the message digest for a message M                       **
46 **    (1) Initialize a context buffer mdContext using krb5int_MD5Init   **
47 **    (2) Call krb5int_MD5Update on mdContext and M                     **
48 **    (3) Call krb5int_MD5Final on mdContext                            **
49 **  The message digest is now in mdContext->digest[0...15]           **
50 ***********************************************************************
51 */
52 
53 /* forward declaration */
54 static void Transform (krb5_ui_4 *buf, krb5_ui_4 *in);
55 
56 static const unsigned char PADDING[64] = {
57     0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
65 };
66 
67 /* F, G, H and I are basic MD5 functions */
68 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
69 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
70 #define H(x, y, z) ((x) ^ (y) ^ (z))
71 #define I(x, y, z) ((y) ^ ((x) | (~z)))
72 
73 /* ROTATE_LEFT rotates x left n bits */
74 #define ROTATE_LEFT(x, n) ((((x) << (n)) & 0xffffffff) | ((x) >> (32-(n))))
75 
76 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
77 /* Rotation is separate from addition to prevent recomputation */
78 #define FF(a, b, c, d, x, s, ac)                        \
79     {(a) += F ((b), (c), (d)) + (x) + (krb5_ui_4)(ac);  \
80         (a) &= 0xffffffff;                              \
81         (a) = ROTATE_LEFT ((a), (s));                   \
82         (a) += (b);                                     \
83         (a) &= 0xffffffff;                              \
84     }
85 #define GG(a, b, c, d, x, s, ac)                        \
86     {(a) += G ((b), (c), (d)) + (x) + (krb5_ui_4)(ac);  \
87         (a) &= 0xffffffff;                              \
88         (a) = ROTATE_LEFT ((a), (s));                   \
89         (a) += (b);                                     \
90         (a) &= 0xffffffff;                              \
91     }
92 #define HH(a, b, c, d, x, s, ac)                        \
93     {(a) += H ((b), (c), (d)) + (x) + (krb5_ui_4)(ac);  \
94         (a) &= 0xffffffff;                              \
95         (a) = ROTATE_LEFT ((a), (s));                   \
96         (a) += (b);                                     \
97         (a) &= 0xffffffff;                              \
98     }
99 #define II(a, b, c, d, x, s, ac)                        \
100     {(a) += I ((b), (c), (d)) + (x) + (krb5_ui_4)(ac);  \
101         (a) &= 0xffffffff;                              \
102         (a) = ROTATE_LEFT ((a), (s));                   \
103         (a) += (b);                                     \
104         (a) &= 0xffffffff;                              \
105     }
106 
107 /* The routine krb5int_MD5Init initializes the message-digest context
108    mdContext. All fields are set to zero.
109 */
110 void
krb5int_MD5Init(krb5_MD5_CTX * mdContext)111 krb5int_MD5Init (krb5_MD5_CTX *mdContext)
112 {
113     mdContext->i[0] = mdContext->i[1] = (krb5_ui_4)0;
114 
115     /* Load magic initialization constants.
116      */
117     mdContext->buf[0] = 0x67452301UL;
118     mdContext->buf[1] = 0xefcdab89UL;
119     mdContext->buf[2] = 0x98badcfeUL;
120     mdContext->buf[3] = 0x10325476UL;
121 }
122 
123 /* The routine krb5int_MD5Update updates the message-digest context to
124    account for the presence of each of the characters inBuf[0..inLen-1]
125    in the message whose digest is being computed.
126 */
127 void
krb5int_MD5Update(krb5_MD5_CTX * mdContext,const unsigned char * inBuf,unsigned int inLen)128 krb5int_MD5Update (krb5_MD5_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
129 {
130     krb5_ui_4 in[16];
131     int mdi;
132     unsigned int i, ii;
133 
134     /* compute number of bytes mod 64 */
135     mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
136 
137     /* update number of bits */
138     if ((mdContext->i[0] + ((krb5_ui_4)inLen << 3)) < mdContext->i[0])
139         mdContext->i[1]++;
140     mdContext->i[0] += ((krb5_ui_4)inLen << 3);
141     mdContext->i[1] += ((krb5_ui_4)inLen >> 29);
142 
143     while (inLen--) {
144         /* add new character to buffer, increment mdi */
145         mdContext->in[mdi++] = *inBuf++;
146 
147         /* transform if necessary */
148         if (mdi == 0x40) {
149             for (i = 0, ii = 0; i < 16; i++, ii += 4)
150                 in[i] = load_32_le(mdContext->in+ii);
151             Transform (mdContext->buf, in);
152             mdi = 0;
153         }
154     }
155 }
156 
157 /* The routine krb5int_MD5Final terminates the message-digest computation and
158    ends with the desired message digest in mdContext->digest[0...15].
159 */
160 void
krb5int_MD5Final(krb5_MD5_CTX * mdContext)161 krb5int_MD5Final (krb5_MD5_CTX *mdContext)
162 {
163     krb5_ui_4 in[16];
164     int mdi;
165     unsigned int i, ii;
166     unsigned int padLen;
167 
168     /* save number of bits */
169     in[14] = mdContext->i[0];
170     in[15] = mdContext->i[1];
171 
172     /* compute number of bytes mod 64 */
173     mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
174 
175     /* pad out to 56 mod 64 */
176     padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
177     krb5int_MD5Update (mdContext, PADDING, padLen);
178 
179     /* append length in bits and transform */
180     for (i = 0, ii = 0; i < 14; i++, ii += 4)
181         in[i] = load_32_le(mdContext->in+ii);
182     Transform (mdContext->buf, in);
183 
184     /* store buffer in digest */
185     for (i = 0, ii = 0; i < 4; i++, ii += 4) {
186         store_32_le(mdContext->buf[i], mdContext->digest+ii);
187     }
188 }
189 
190 /* Basic MD5 step. Transforms buf based on in.
191  */
Transform(krb5_ui_4 * buf,krb5_ui_4 * in)192 static void Transform (krb5_ui_4 *buf, krb5_ui_4 *in)
193 {
194     krb5_ui_4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
195 
196 #if defined(CONFIG_SMALL) && !defined(CONFIG_SMALL_NO_CRYPTO)
197 
198     int i;
199 #define ROTATE { krb5_ui_4 temp; temp = d, d = c, c = b, b = a, a = temp; }
200     for (i = 0; i < 16; i++) {
201         const unsigned char round1s[] = { 7, 12, 17, 22 };
202         const krb5_ui_4 round1consts[] = {
203             3614090360UL,   3905402710UL,    606105819UL,   3250441966UL,
204             4118548399UL,   1200080426UL,   2821735955UL,   4249261313UL,
205             1770035416UL,   2336552879UL,   4294925233UL,   2304563134UL,
206             1804603682UL,   4254626195UL,   2792965006UL,   1236535329UL,
207         };
208         FF (a, b, c, d, in[i], round1s[i%4], round1consts[i]);
209         ROTATE;
210     }
211     for (i = 0; i < 16; i++) {
212         const unsigned char round2s[] = { 5, 9, 14, 20 };
213         const krb5_ui_4 round2consts[] = {
214             4129170786UL,   3225465664UL,    643717713UL,   3921069994UL,
215             3593408605UL,     38016083UL,   3634488961UL,   3889429448UL,
216             568446438UL,   3275163606UL,   4107603335UL,   1163531501UL,
217             2850285829UL,   4243563512UL,   1735328473UL,   2368359562UL,
218         };
219         int r2index = (1 + i * 5) % 16;
220         GG (a, b, c, d, in[r2index], round2s[i%4], round2consts[i]);
221         ROTATE;
222     }
223     for (i = 0; i < 16; i++) {
224         static const unsigned char round3s[] = { 4, 11, 16, 23 };
225         static const krb5_ui_4 round3consts[] = {
226             4294588738UL,   2272392833UL,   1839030562UL,   4259657740UL,
227             2763975236UL,   1272893353UL,   4139469664UL,   3200236656UL,
228             681279174UL,   3936430074UL,   3572445317UL,     76029189UL,
229             3654602809UL,   3873151461UL,    530742520UL,   3299628645UL,
230         };
231         int r3index = (5 + i * 3) % 16;
232         HH (a, b, c, d, in[r3index], round3s[i%4], round3consts[i]);
233         ROTATE;
234     }
235     for (i = 0; i < 16; i++) {
236         static const unsigned char round4s[] = { 6, 10, 15, 21 };
237         static const krb5_ui_4 round4consts[] = {
238             4096336452UL,   1126891415UL,   2878612391UL,   4237533241UL,
239             1700485571UL,   2399980690UL,   4293915773UL,   2240044497UL,
240             1873313359UL,   4264355552UL,   2734768916UL,   1309151649UL,
241             4149444226UL,   3174756917UL,    718787259UL,   3951481745UL,
242         };
243         int r4index = (7 * i) % 16;
244         II (a, b, c, d, in[r4index], round4s[i%4], round4consts[i]);
245         ROTATE;
246     }
247 
248 #else
249 
250     /* Round 1 */
251 #define S11 7
252 #define S12 12
253 #define S13 17
254 #define S14 22
255     FF ( a, b, c, d, in[ 0], S11, 3614090360UL); /* 1 */
256     FF ( d, a, b, c, in[ 1], S12, 3905402710UL); /* 2 */
257     FF ( c, d, a, b, in[ 2], S13,  606105819UL); /* 3 */
258     FF ( b, c, d, a, in[ 3], S14, 3250441966UL); /* 4 */
259     FF ( a, b, c, d, in[ 4], S11, 4118548399UL); /* 5 */
260     FF ( d, a, b, c, in[ 5], S12, 1200080426UL); /* 6 */
261     FF ( c, d, a, b, in[ 6], S13, 2821735955UL); /* 7 */
262     FF ( b, c, d, a, in[ 7], S14, 4249261313UL); /* 8 */
263     FF ( a, b, c, d, in[ 8], S11, 1770035416UL); /* 9 */
264     FF ( d, a, b, c, in[ 9], S12, 2336552879UL); /* 10 */
265     FF ( c, d, a, b, in[10], S13, 4294925233UL); /* 11 */
266     FF ( b, c, d, a, in[11], S14, 2304563134UL); /* 12 */
267     FF ( a, b, c, d, in[12], S11, 1804603682UL); /* 13 */
268     FF ( d, a, b, c, in[13], S12, 4254626195UL); /* 14 */
269     FF ( c, d, a, b, in[14], S13, 2792965006UL); /* 15 */
270     FF ( b, c, d, a, in[15], S14, 1236535329UL); /* 16 */
271 
272     /* Round 2 */
273 #define S21 5
274 #define S22 9
275 #define S23 14
276 #define S24 20
277     GG ( a, b, c, d, in[ 1], S21, 4129170786UL); /* 17 */
278     GG ( d, a, b, c, in[ 6], S22, 3225465664UL); /* 18 */
279     GG ( c, d, a, b, in[11], S23,  643717713UL); /* 19 */
280     GG ( b, c, d, a, in[ 0], S24, 3921069994UL); /* 20 */
281     GG ( a, b, c, d, in[ 5], S21, 3593408605UL); /* 21 */
282     GG ( d, a, b, c, in[10], S22,   38016083UL); /* 22 */
283     GG ( c, d, a, b, in[15], S23, 3634488961UL); /* 23 */
284     GG ( b, c, d, a, in[ 4], S24, 3889429448UL); /* 24 */
285     GG ( a, b, c, d, in[ 9], S21,  568446438UL); /* 25 */
286     GG ( d, a, b, c, in[14], S22, 3275163606UL); /* 26 */
287     GG ( c, d, a, b, in[ 3], S23, 4107603335UL); /* 27 */
288     GG ( b, c, d, a, in[ 8], S24, 1163531501UL); /* 28 */
289     GG ( a, b, c, d, in[13], S21, 2850285829UL); /* 29 */
290     GG ( d, a, b, c, in[ 2], S22, 4243563512UL); /* 30 */
291     GG ( c, d, a, b, in[ 7], S23, 1735328473UL); /* 31 */
292     GG ( b, c, d, a, in[12], S24, 2368359562UL); /* 32 */
293 
294     /* Round 3 */
295 #define S31 4
296 #define S32 11
297 #define S33 16
298 #define S34 23
299     HH ( a, b, c, d, in[ 5], S31, 4294588738UL); /* 33 */
300     HH ( d, a, b, c, in[ 8], S32, 2272392833UL); /* 34 */
301     HH ( c, d, a, b, in[11], S33, 1839030562UL); /* 35 */
302     HH ( b, c, d, a, in[14], S34, 4259657740UL); /* 36 */
303     HH ( a, b, c, d, in[ 1], S31, 2763975236UL); /* 37 */
304     HH ( d, a, b, c, in[ 4], S32, 1272893353UL); /* 38 */
305     HH ( c, d, a, b, in[ 7], S33, 4139469664UL); /* 39 */
306     HH ( b, c, d, a, in[10], S34, 3200236656UL); /* 40 */
307     HH ( a, b, c, d, in[13], S31,  681279174UL); /* 41 */
308     HH ( d, a, b, c, in[ 0], S32, 3936430074UL); /* 42 */
309     HH ( c, d, a, b, in[ 3], S33, 3572445317UL); /* 43 */
310     HH ( b, c, d, a, in[ 6], S34,   76029189UL); /* 44 */
311     HH ( a, b, c, d, in[ 9], S31, 3654602809UL); /* 45 */
312     HH ( d, a, b, c, in[12], S32, 3873151461UL); /* 46 */
313     HH ( c, d, a, b, in[15], S33,  530742520UL); /* 47 */
314     HH ( b, c, d, a, in[ 2], S34, 3299628645UL); /* 48 */
315 
316     /* Round 4 */
317 #define S41 6
318 #define S42 10
319 #define S43 15
320 #define S44 21
321     II ( a, b, c, d, in[ 0], S41, 4096336452UL); /* 49 */
322     II ( d, a, b, c, in[ 7], S42, 1126891415UL); /* 50 */
323     II ( c, d, a, b, in[14], S43, 2878612391UL); /* 51 */
324     II ( b, c, d, a, in[ 5], S44, 4237533241UL); /* 52 */
325     II ( a, b, c, d, in[12], S41, 1700485571UL); /* 53 */
326     II ( d, a, b, c, in[ 3], S42, 2399980690UL); /* 54 */
327     II ( c, d, a, b, in[10], S43, 4293915773UL); /* 55 */
328     II ( b, c, d, a, in[ 1], S44, 2240044497UL); /* 56 */
329     II ( a, b, c, d, in[ 8], S41, 1873313359UL); /* 57 */
330     II ( d, a, b, c, in[15], S42, 4264355552UL); /* 58 */
331     II ( c, d, a, b, in[ 6], S43, 2734768916UL); /* 59 */
332     II ( b, c, d, a, in[13], S44, 1309151649UL); /* 60 */
333     II ( a, b, c, d, in[ 4], S41, 4149444226UL); /* 61 */
334     II ( d, a, b, c, in[11], S42, 3174756917UL); /* 62 */
335     II ( c, d, a, b, in[ 2], S43,  718787259UL); /* 63 */
336     II ( b, c, d, a, in[ 9], S44, 3951481745UL); /* 64 */
337 
338 #endif /* small? */
339 
340     buf[0] += a;
341     buf[1] += b;
342     buf[2] += c;
343     buf[3] += d;
344 }
345 
346 #endif /* K5_BUILTIN_MD5 */
347