]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libmd/md4c.c
sqlite3: Vendor import of sqlite3 3.43.1
[FreeBSD/FreeBSD.git] / lib / libmd / md4c.c
1 /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
2  */
3
4 #include <sys/cdefs.h>
5 /*-
6    SPDX-License-Identifier: RSA-MD
7
8    Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
9
10    License to copy and use this software is granted provided that it
11    is identified as the "RSA Data Security, Inc. MD4 Message-Digest
12    Algorithm" in all material mentioning or referencing this software
13    or this function.
14
15    License is also granted to make and use derivative works provided
16    that such works are identified as "derived from the RSA Data
17    Security, Inc. MD4 Message-Digest Algorithm" in all material
18    mentioning or referencing the derived work.
19
20    RSA Data Security, Inc. makes no representations concerning either
21    the merchantability of this software or the suitability of this
22    software for any particular purpose. It is provided "as is"
23    without express or implied warranty of any kind.
24
25    These notices must be retained in any copies of any part of this
26    documentation and/or software.
27  */
28
29 #include <sys/types.h>
30 #include <string.h>
31 #include "md4.h"
32
33 typedef unsigned char *POINTER;
34 typedef const unsigned char *CONST_POINTER;
35 typedef u_int16_t UINT2;
36 typedef u_int32_t UINT4;
37
38 #define PROTO_LIST(list) list
39
40 /* Constants for MD4Transform routine.
41  */
42 #define S11 3
43 #define S12 7
44 #define S13 11
45 #define S14 19
46 #define S21 3
47 #define S22 5
48 #define S23 9
49 #define S24 13
50 #define S31 3
51 #define S32 9
52 #define S33 11
53 #define S34 15
54
55 static void MD4Transform PROTO_LIST ((UINT4 [4], const unsigned char [64]));
56 static void Encode PROTO_LIST
57   ((unsigned char *, UINT4 *, unsigned int));
58 static void Decode PROTO_LIST
59   ((UINT4 *, const unsigned char *, unsigned int));
60
61 static unsigned char PADDING[64] = {
62   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
65 };
66
67 /* F, G and H are basic MD4 functions.
68  */
69 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
70 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
71 #define H(x, y, z) ((x) ^ (y) ^ (z))
72
73 /* ROTATE_LEFT rotates x left n bits.
74  */
75 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
76
77 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
78 /* Rotation is separate from addition to prevent recomputation */
79 #define FF(a, b, c, d, x, s) { \
80     (a) += F ((b), (c), (d)) + (x); \
81     (a) = ROTATE_LEFT ((a), (s)); \
82   }
83 #define GG(a, b, c, d, x, s) { \
84     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
85     (a) = ROTATE_LEFT ((a), (s)); \
86   }
87 #define HH(a, b, c, d, x, s) { \
88     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
89     (a) = ROTATE_LEFT ((a), (s)); \
90   }
91
92 /* MD4 initialization. Begins an MD4 operation, writing a new context.
93  */
94 void
95 MD4Init(MD4_CTX *context)
96 {
97   context->count[0] = context->count[1] = 0;
98
99   /* Load magic initialization constants.
100    */
101   context->state[0] = 0x67452301;
102   context->state[1] = 0xefcdab89;
103   context->state[2] = 0x98badcfe;
104   context->state[3] = 0x10325476;
105 }
106
107 /* MD4 block update operation. Continues an MD4 message-digest
108      operation, processing another message block, and updating the
109      context.
110  */
111 void
112 MD4Update(MD4_CTX *context, const void *in, unsigned int inputLen)
113 {
114   unsigned int i, idx, partLen;
115   const unsigned char *input = in;
116
117   /* Compute number of bytes mod 64 */
118   idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
119   /* Update number of bits */
120   if ((context->count[0] += ((UINT4)inputLen << 3))
121       < ((UINT4)inputLen << 3))
122     context->count[1]++;
123   context->count[1] += ((UINT4)inputLen >> 29);
124
125   partLen = 64 - idx;
126   /* Transform as many times as possible.
127    */
128   if (inputLen >= partLen) {
129     memcpy
130       ((POINTER)&context->buffer[idx], (CONST_POINTER)input, partLen);
131     MD4Transform (context->state, context->buffer);
132
133     for (i = partLen; i + 63 < inputLen; i += 64)
134       MD4Transform (context->state, &input[i]);
135
136     idx = 0;
137   }
138   else
139     i = 0;
140
141   /* Buffer remaining input */
142   memcpy
143     ((POINTER)&context->buffer[idx], (CONST_POINTER)&input[i],
144      inputLen-i);
145 }
146
147 /* MD4 padding. */
148 void
149 MD4Pad(MD4_CTX *context)
150 {
151   unsigned char bits[8];
152   unsigned int idx, padLen;
153
154   /* Save number of bits */
155   Encode (bits, context->count, 8);
156
157   /* Pad out to 56 mod 64.
158    */
159   idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
160   padLen = (idx < 56) ? (56 - idx) : (120 - idx);
161   MD4Update (context, PADDING, padLen);
162
163   /* Append length (before padding) */
164   MD4Update (context, bits, 8);
165 }
166
167 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
168      the message digest and zeroizing the context.
169  */
170 void
171 MD4Final(unsigned char digest[16], MD4_CTX *context)
172 {
173   /* Do padding */
174   MD4Pad (context);
175
176   /* Store state in digest */
177   Encode (digest, context->state, 16);
178
179   /* Zeroize sensitive information.
180    */
181   explicit_bzero(context, sizeof(*context));
182 }
183
184 /* MD4 basic transformation. Transforms state based on block.
185  */
186 static void
187 MD4Transform(UINT4 state[4], const unsigned char block[64])
188 {
189   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
190
191   Decode (x, block, 64);
192
193   /* Round 1 */
194   FF (a, b, c, d, x[ 0], S11); /* 1 */
195   FF (d, a, b, c, x[ 1], S12); /* 2 */
196   FF (c, d, a, b, x[ 2], S13); /* 3 */
197   FF (b, c, d, a, x[ 3], S14); /* 4 */
198   FF (a, b, c, d, x[ 4], S11); /* 5 */
199   FF (d, a, b, c, x[ 5], S12); /* 6 */
200   FF (c, d, a, b, x[ 6], S13); /* 7 */
201   FF (b, c, d, a, x[ 7], S14); /* 8 */
202   FF (a, b, c, d, x[ 8], S11); /* 9 */
203   FF (d, a, b, c, x[ 9], S12); /* 10 */
204   FF (c, d, a, b, x[10], S13); /* 11 */
205   FF (b, c, d, a, x[11], S14); /* 12 */
206   FF (a, b, c, d, x[12], S11); /* 13 */
207   FF (d, a, b, c, x[13], S12); /* 14 */
208   FF (c, d, a, b, x[14], S13); /* 15 */
209   FF (b, c, d, a, x[15], S14); /* 16 */
210
211   /* Round 2 */
212   GG (a, b, c, d, x[ 0], S21); /* 17 */
213   GG (d, a, b, c, x[ 4], S22); /* 18 */
214   GG (c, d, a, b, x[ 8], S23); /* 19 */
215   GG (b, c, d, a, x[12], S24); /* 20 */
216   GG (a, b, c, d, x[ 1], S21); /* 21 */
217   GG (d, a, b, c, x[ 5], S22); /* 22 */
218   GG (c, d, a, b, x[ 9], S23); /* 23 */
219   GG (b, c, d, a, x[13], S24); /* 24 */
220   GG (a, b, c, d, x[ 2], S21); /* 25 */
221   GG (d, a, b, c, x[ 6], S22); /* 26 */
222   GG (c, d, a, b, x[10], S23); /* 27 */
223   GG (b, c, d, a, x[14], S24); /* 28 */
224   GG (a, b, c, d, x[ 3], S21); /* 29 */
225   GG (d, a, b, c, x[ 7], S22); /* 30 */
226   GG (c, d, a, b, x[11], S23); /* 31 */
227   GG (b, c, d, a, x[15], S24); /* 32 */
228
229   /* Round 3 */
230   HH (a, b, c, d, x[ 0], S31); /* 33 */
231   HH (d, a, b, c, x[ 8], S32); /* 34 */
232   HH (c, d, a, b, x[ 4], S33); /* 35 */
233   HH (b, c, d, a, x[12], S34); /* 36 */
234   HH (a, b, c, d, x[ 2], S31); /* 37 */
235   HH (d, a, b, c, x[10], S32); /* 38 */
236   HH (c, d, a, b, x[ 6], S33); /* 39 */
237   HH (b, c, d, a, x[14], S34); /* 40 */
238   HH (a, b, c, d, x[ 1], S31); /* 41 */
239   HH (d, a, b, c, x[ 9], S32); /* 42 */
240   HH (c, d, a, b, x[ 5], S33); /* 43 */
241   HH (b, c, d, a, x[13], S34); /* 44 */
242   HH (a, b, c, d, x[ 3], S31); /* 45 */
243   HH (d, a, b, c, x[11], S32); /* 46 */
244   HH (c, d, a, b, x[ 7], S33); /* 47 */
245   HH (b, c, d, a, x[15], S34); /* 48 */
246
247   state[0] += a;
248   state[1] += b;
249   state[2] += c;
250   state[3] += d;
251
252   /* Zeroize sensitive information.
253    */
254   memset ((POINTER)x, 0, sizeof (x));
255 }
256
257 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
258      a multiple of 4.
259  */
260 static void
261 Encode(unsigned char *output, UINT4 *input, unsigned int len)
262 {
263   unsigned int i, j;
264
265   for (i = 0, j = 0; j < len; i++, j += 4) {
266     output[j] = (unsigned char)(input[i] & 0xff);
267     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
268     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
269     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
270   }
271 }
272
273 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
274      a multiple of 4.
275  */
276 static void
277 Decode(UINT4 *output, const unsigned char *input, unsigned int len)
278 {
279   unsigned int i, j;
280
281   for (i = 0, j = 0; j < len; i++, j += 4)
282     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
283       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
284 }
285
286 #ifdef WEAK_REFS
287 /* When building libmd, provide weak references. Note: this is not
288    activated in the context of compiling these sources for internal
289    use in libcrypt.
290  */
291 #undef MD4Init
292 __weak_reference(_libmd_MD4Init, MD4Init);
293 #undef MD4Update
294 __weak_reference(_libmd_MD4Update, MD4Update);
295 #undef MD4Pad
296 __weak_reference(_libmd_MD4Pad, MD4Pad);
297 #undef MD4Final
298 __weak_reference(_libmd_MD4Final, MD4Final);
299 #endif