6 **********************************************************************
8 ** RSA Data Security, Inc. MD4 Message Digest Algorithm **
9 ** Created: 2/17/90 RLR **
10 ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
11 **********************************************************************
15 **********************************************************************
16 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
18 ** License to copy and use this software is granted provided that **
19 ** it is identified as the "RSA Data Security, Inc. MD4 Message **
20 ** Digest Algorithm" in all material mentioning or referencing this **
21 ** software or this function. **
23 ** License is also granted to make and use derivative works **
24 ** provided that such works are identified as "derived from the RSA **
25 ** Data Security, Inc. MD4 Message Digest Algorithm" in all **
26 ** material mentioning or referencing the derived work. **
28 ** RSA Data Security, Inc. makes no representations concerning **
29 ** either the merchantability of this software or the suitability **
30 ** of this software for any particular purpose. It is provided "as **
31 ** is" without express or implied warranty of any kind. **
33 ** These notices must be retained in any copies of any part of this **
34 ** documentation and/or software. **
35 **********************************************************************
44 #define UL(x) ((krb5_ui_4) x)
47 /* forward declaration */
48 #if defined(__STDC__) || defined(KRB5_PROVIDE_PROTOTYPES)
49 static void Transform (krb5_ui_4 FAR *, krb5_ui_4 FAR *);
51 static void Transform ();
54 static unsigned char PADDING[64] = {
55 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 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
65 /* F, G and H are basic MD4 functions: selection, majority, parity */
66 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
67 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
68 #define H(x, y, z) ((x) ^ (y) ^ (z))
70 /* ROTATE_LEFT rotates x left n bits */
71 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
73 /* FF, GG and HH are MD4 transformations for rounds 1, 2 and 3 */
74 /* Rotation is separate from addition to prevent recomputation */
75 #define FF(a, b, c, d, x, s) \
76 {(a) += F ((b), (c), (d)) + (x); \
77 (a) = ROTATE_LEFT ((a), (s));}
78 #define GG(a, b, c, d, x, s) \
79 {(a) += G ((b), (c), (d)) + (x) + UL(013240474631); \
80 (a) = ROTATE_LEFT ((a), (s));}
81 #define HH(a, b, c, d, x, s) \
82 {(a) += H ((b), (c), (d)) + (x) + UL(015666365641); \
83 (a) = ROTATE_LEFT ((a), (s));}
87 MD4_CTX FAR *mdContext;
89 mdContext->i[0] = mdContext->i[1] = (krb5_ui_4)0;
91 /* Load magic initialization constants.
93 mdContext->buf[0] = UL(0x67452301);
94 mdContext->buf[1] = UL(0xefcdab89);
95 mdContext->buf[2] = UL(0x98badcfe);
96 mdContext->buf[3] = UL(0x10325476);
100 MD4Update (mdContext, inBuf, inLen)
101 MD4_CTX FAR *mdContext;
102 unsigned char FAR *inBuf;
109 /* compute number of bytes mod 64 */
110 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
112 /* update number of bits */
113 if ((mdContext->i[0] + ((krb5_ui_4)inLen << 3)) < mdContext->i[0])
115 mdContext->i[0] += ((krb5_ui_4)inLen << 3);
116 mdContext->i[1] += ((krb5_ui_4)inLen >> 29);
119 /* add new character to buffer, increment mdi */
120 mdContext->in[mdi++] = *inBuf++;
122 /* transform if necessary */
124 for (i = 0, ii = 0; i < 16; i++, ii += 4)
125 in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
126 (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
127 (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
128 ((krb5_ui_4)mdContext->in[ii]);
129 Transform (mdContext->buf, in);
137 MD4_CTX FAR *mdContext;
144 /* save number of bits */
145 in[14] = mdContext->i[0];
146 in[15] = mdContext->i[1];
148 /* compute number of bytes mod 64 */
149 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
151 /* pad out to 56 mod 64 */
152 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
153 MD4Update (mdContext, PADDING, padLen);
155 /* append length in bits and transform */
156 for (i = 0, ii = 0; i < 14; i++, ii += 4)
157 in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) |
158 (((krb5_ui_4)mdContext->in[ii+2]) << 16) |
159 (((krb5_ui_4)mdContext->in[ii+1]) << 8) |
160 ((krb5_ui_4)mdContext->in[ii]);
161 Transform (mdContext->buf, in);
164 /* store buffer in digest */
165 for (i = 0, ii = 0; i < 4; i++, ii += 4) {
166 mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
167 mdContext->digest[ii+1] =
168 (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
169 mdContext->digest[ii+2] =
170 (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
171 mdContext->digest[ii+3] =
172 (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
176 /* Basic MD4 step. Transform buf based on in.
178 static void Transform (buf, in)
182 krb5_ui_4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
185 FF (a, b, c, d, in[ 0], 3);
186 FF (d, a, b, c, in[ 1], 7);
187 FF (c, d, a, b, in[ 2], 11);
188 FF (b, c, d, a, in[ 3], 19);
189 FF (a, b, c, d, in[ 4], 3);
190 FF (d, a, b, c, in[ 5], 7);
191 FF (c, d, a, b, in[ 6], 11);
192 FF (b, c, d, a, in[ 7], 19);
193 FF (a, b, c, d, in[ 8], 3);
194 FF (d, a, b, c, in[ 9], 7);
195 FF (c, d, a, b, in[10], 11);
196 FF (b, c, d, a, in[11], 19);
197 FF (a, b, c, d, in[12], 3);
198 FF (d, a, b, c, in[13], 7);
199 FF (c, d, a, b, in[14], 11);
200 FF (b, c, d, a, in[15], 19);
203 GG (a, b, c, d, in[ 0], 3);
204 GG (d, a, b, c, in[ 4], 5);
205 GG (c, d, a, b, in[ 8], 9);
206 GG (b, c, d, a, in[12], 13);
207 GG (a, b, c, d, in[ 1], 3);
208 GG (d, a, b, c, in[ 5], 5);
209 GG (c, d, a, b, in[ 9], 9);
210 GG (b, c, d, a, in[13], 13);
211 GG (a, b, c, d, in[ 2], 3);
212 GG (d, a, b, c, in[ 6], 5);
213 GG (c, d, a, b, in[10], 9);
214 GG (b, c, d, a, in[14], 13);
215 GG (a, b, c, d, in[ 3], 3);
216 GG (d, a, b, c, in[ 7], 5);
217 GG (c, d, a, b, in[11], 9);
218 GG (b, c, d, a, in[15], 13);
221 HH (a, b, c, d, in[ 0], 3);
222 HH (d, a, b, c, in[ 8], 9);
223 HH (c, d, a, b, in[ 4], 11);
224 HH (b, c, d, a, in[12], 15);
225 HH (a, b, c, d, in[ 2], 3);
226 HH (d, a, b, c, in[10], 9);
227 HH (c, d, a, b, in[ 6], 11);
228 HH (b, c, d, a, in[14], 15);
229 HH (a, b, c, d, in[ 1], 3);
230 HH (d, a, b, c, in[ 9], 9);
231 HH (c, d, a, b, in[ 5], 11);
232 HH (b, c, d, a, in[13], 15);
233 HH (a, b, c, d, in[ 3], 3);
234 HH (d, a, b, c, in[11], 9);
235 HH (c, d, a, b, in[ 7], 11);
236 HH (b, c, d, a, in[15], 15);
245 **********************************************************************
247 ******************************* (cut) ********************************