16f6a2b4bedaae6bb5ff4b197c2cba5f1202186f
[krb5.git] / src / lib / crypto / md4 / md4.c
1 /*
2  *      lib/crypto/md4/md4.c
3  */
4
5 /*
6  **********************************************************************
7  ** md4.c                                                            **
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  **********************************************************************
12  */
13
14 /*
15  **********************************************************************
16  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
17  **                                                                  **
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.                                       **
22  **                                                                  **
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.             **
27  **                                                                  **
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.             **
32  **                                                                  **
33  ** These notices must be retained in any copies of any part of this **
34  ** documentation and/or software.                                   **
35  **********************************************************************
36  */
37
38 #include "k5-int.h"
39 #include "rsa-md4.h"
40
41 #ifdef __STDC__
42 #define UL(x) x##UL
43 #else
44 #define UL(x) ((krb5_ui_4) x)
45 #endif    
46
47 /* forward declaration */
48 #if defined(__STDC__) || defined(KRB5_PROVIDE_PROTOTYPES)
49 static void Transform (krb5_ui_4 FAR *, krb5_ui_4 FAR *);
50 #else
51 static void Transform ();
52 #endif
53
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
63 };
64
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))
69
70 /* ROTATE_LEFT rotates x left n bits */
71 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
72
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));}
84
85 void INTERFACE
86 MD4Init (mdContext)
87 MD4_CTX FAR *mdContext;
88 {
89   mdContext->i[0] = mdContext->i[1] = (krb5_ui_4)0;
90
91   /* Load magic initialization constants.
92    */
93   mdContext->buf[0] = UL(0x67452301);
94   mdContext->buf[1] = UL(0xefcdab89);
95   mdContext->buf[2] = UL(0x98badcfe);
96   mdContext->buf[3] = UL(0x10325476);
97 }
98
99 void INTERFACE
100 MD4Update (mdContext, inBuf, inLen)
101 MD4_CTX FAR *mdContext;
102 unsigned char FAR *inBuf;
103 unsigned int inLen;
104 {
105   krb5_ui_4 in[16];
106   int mdi;
107   unsigned int i, ii;
108
109   /* compute number of bytes mod 64 */
110   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
111
112   /* update number of bits */
113   if ((mdContext->i[0] + ((krb5_ui_4)inLen << 3)) < mdContext->i[0])
114     mdContext->i[1]++;
115   mdContext->i[0] += ((krb5_ui_4)inLen << 3);
116   mdContext->i[1] += ((krb5_ui_4)inLen >> 29);
117
118   while (inLen--) {
119     /* add new character to buffer, increment mdi */
120     mdContext->in[mdi++] = *inBuf++;
121
122     /* transform if necessary */
123     if (mdi == 0x40) {
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);
130       mdi = 0;
131     }
132   }
133 }
134
135 void INTERFACE
136 MD4Final (mdContext)
137 MD4_CTX FAR *mdContext;
138 {
139   krb5_ui_4 in[16];
140   int mdi;
141   unsigned int i, ii;
142   unsigned int padLen;
143
144   /* save number of bits */
145   in[14] = mdContext->i[0];
146   in[15] = mdContext->i[1];
147
148   /* compute number of bytes mod 64 */
149   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
150
151   /* pad out to 56 mod 64 */
152   padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
153   MD4Update (mdContext, PADDING, padLen);
154
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);
162
163
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);
173   }
174 }
175
176 /* Basic MD4 step. Transform buf based on in.
177  */
178 static void Transform (buf, in)
179 krb5_ui_4 FAR *buf;
180 krb5_ui_4 FAR *in;
181 {
182   krb5_ui_4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
183
184   /* Round 1 */
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);
201
202   /* Round 2 */
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);
219
220   /* Round 3 */
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);
237
238   buf[0] += a;
239   buf[1] += b;
240   buf[2] += c;
241   buf[3] += d;
242 }
243
244 /*
245  **********************************************************************
246  ** End of md4.c                                                     **
247  ******************************* (cut) ********************************
248  */