Remove building with NOCRYPTO option
[minix.git] / common / lib / libc / md / md4c.c
blobd51d95fcb3b5109d31984227cb0003d73a92c884
1 /* $NetBSD: md4c.c,v 1.5 2012/03/20 16:21:41 matt Exp $ */
3 /*
4 * This file is derived from the RSA Data Security, Inc. MD4 Message-Digest
5 * Algorithm and has been modified by Jason R. Thorpe <thorpej@NetBSD.org>
6 * for portability and formatting.
7 */
9 /*
10 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
12 * License to copy and use this software is granted provided that it
13 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
14 * Algorithm" in all material mentioning or referencing this software
15 * or this function.
17 * License is also granted to make and use derivative works provided
18 * that such works are identified as "derived from the RSA Data
19 * Security, Inc. MD4 Message-Digest Algorithm" in all material
20 * mentioning or referencing the derived work.
22 * RSA Data Security, Inc. makes no representations concerning either
23 * the merchantability of this software or the suitability of this
24 * software for any particular purpose. It is provided "as is"
25 * without express or implied warranty of any kind.
27 * These notices must be retained in any copies of any part of this
28 * documentation and/or software.
31 #if !defined(_KERNEL) && !defined(_STANDALONE)
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: md4c.c,v 1.5 2012/03/20 16:21:41 matt Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
39 #include <sys/types.h>
41 #include <assert.h>
42 #include <md4.h>
43 #include <string.h>
45 #if HAVE_NBTOOL_CONFIG_H
46 #include "nbtool_config.h"
47 #endif
49 #else
51 #include <sys/param.h>
52 #include <sys/md4.h>
53 #include <lib/libkern/libkern.h>
55 #endif /* !_KERNEL && !_STANDALONE */
57 #if !HAVE_MD4_H
59 typedef unsigned char *POINTER;
60 typedef uint16_t UINT2;
61 typedef uint32_t UINT4;
64 * Constants for MD4Transform routine.
66 #define S11 3
67 #define S12 7
68 #define S13 11
69 #define S14 19
70 #define S21 3
71 #define S22 5
72 #define S23 9
73 #define S24 13
74 #define S31 3
75 #define S32 9
76 #define S33 11
77 #define S34 15
79 static void MD4Transform(UINT4 [4], const unsigned char [64]);
81 static void Encode(unsigned char *, UINT4 *, unsigned int);
82 static void Decode(UINT4 *, const unsigned char *, unsigned int);
84 static const unsigned char PADDING[64] = {
85 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
91 * F, G and H are basic MD4 functions.
93 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
94 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
95 #define H(x, y, z) ((x) ^ (y) ^ (z))
98 * ROTATE_LEFT rotates x left n bits.
100 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
103 * FF, GG and HH are transformations for rounds 1, 2 and 3.
104 * Rotation is separate from addition to prevent recomputation.
106 #define FF(a, b, c, d, x, s) { \
107 (a) += F ((b), (c), (d)) + (x); \
108 (a) = ROTATE_LEFT ((a), (s)); \
111 #define GG(a, b, c, d, x, s) { \
112 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
113 (a) = ROTATE_LEFT ((a), (s)); \
116 #define HH(a, b, c, d, x, s) { \
117 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
118 (a) = ROTATE_LEFT ((a), (s)); \
121 #if !defined(_KERNEL) && !defined(_STANDALONE) && defined(__weak_alias)
122 __weak_alias(MD4Init,_MD4Init)
123 __weak_alias(MD4Update,_MD4Update)
124 __weak_alias(MD4Final,_MD4Final)
125 __weak_alias(MD4Transform,_MD4Transform)
126 #endif
129 * MD4 initialization. Begins an MD4 operation, writing a new context.
131 void
132 MD4Init(MD4_CTX *context) /* context */
135 _DIAGASSERT(context != 0);
137 context->count[0] = context->count[1] = 0;
139 /* Load magic initialization constants. */
140 context->state[0] = 0x67452301;
141 context->state[1] = 0xefcdab89;
142 context->state[2] = 0x98badcfe;
143 context->state[3] = 0x10325476;
147 * MD4 block update operation. Continues an MD4 message-digest
148 * operation, processing another message block, and updating the
149 * context.
151 void
152 MD4Update (MD4_CTX *context, /* context */
153 const unsigned char *input, /* input block */
154 unsigned int inputLen) /* length of input block */
156 unsigned int i, idx, partLen;
158 _DIAGASSERT(context != 0);
159 _DIAGASSERT(input != 0);
161 /* Compute number of bytes mod 64 */
162 idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
164 /* Update number of bits */
165 if ((context->count[0] += ((UINT4)inputLen << 3))
166 < ((UINT4)inputLen << 3))
167 context->count[1]++;
168 context->count[1] += ((UINT4)inputLen >> 29);
170 partLen = 64 - idx;
172 /* Transform as many times as possible. */
173 if (inputLen >= partLen) {
174 memcpy(&context->buffer[idx], input, partLen);
175 MD4Transform(context->state, context->buffer);
177 for (i = partLen; i + 63 < inputLen; i += 64)
178 MD4Transform(context->state, &input[i]);
180 idx = 0;
181 } else
182 i = 0;
184 /* Buffer remaining input */
185 memcpy(&context->buffer[idx], &input[i], inputLen - i);
189 * MD4 finalization. Ends an MD4 message-digest operation, writing the
190 * message digest and zeroing the context.
192 void
193 MD4Final (unsigned char digest[16], /* message digest */
194 MD4_CTX *context) /* context */
196 unsigned char bits[8];
197 unsigned int idx, padLen;
199 _DIAGASSERT(digest != 0);
200 _DIAGASSERT(context != 0);
202 /* Save number of bits */
203 Encode(bits, context->count, 8);
205 /* Pad out to 56 mod 64. */
206 idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
207 padLen = (idx < 56) ? (56 - idx) : (120 - idx);
208 MD4Update(context, PADDING, padLen);
210 /* Append length (before padding) */
211 MD4Update(context, bits, 8);
213 /* Store state in digest */
214 Encode(digest, context->state, 16);
216 /* Zeroize sensitive information. */
217 memset(context, 0, sizeof(*context));
221 * MD4 basic transformation. Transforms state based on block.
223 static void
224 MD4Transform (UINT4 state[4], const unsigned char block[64])
226 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
228 Decode(x, block, 64);
230 /* Round 1 */
231 FF (a, b, c, d, x[ 0], S11); /* 1 */
232 FF (d, a, b, c, x[ 1], S12); /* 2 */
233 FF (c, d, a, b, x[ 2], S13); /* 3 */
234 FF (b, c, d, a, x[ 3], S14); /* 4 */
235 FF (a, b, c, d, x[ 4], S11); /* 5 */
236 FF (d, a, b, c, x[ 5], S12); /* 6 */
237 FF (c, d, a, b, x[ 6], S13); /* 7 */
238 FF (b, c, d, a, x[ 7], S14); /* 8 */
239 FF (a, b, c, d, x[ 8], S11); /* 9 */
240 FF (d, a, b, c, x[ 9], S12); /* 10 */
241 FF (c, d, a, b, x[10], S13); /* 11 */
242 FF (b, c, d, a, x[11], S14); /* 12 */
243 FF (a, b, c, d, x[12], S11); /* 13 */
244 FF (d, a, b, c, x[13], S12); /* 14 */
245 FF (c, d, a, b, x[14], S13); /* 15 */
246 FF (b, c, d, a, x[15], S14); /* 16 */
248 /* Round 2 */
249 GG (a, b, c, d, x[ 0], S21); /* 17 */
250 GG (d, a, b, c, x[ 4], S22); /* 18 */
251 GG (c, d, a, b, x[ 8], S23); /* 19 */
252 GG (b, c, d, a, x[12], S24); /* 20 */
253 GG (a, b, c, d, x[ 1], S21); /* 21 */
254 GG (d, a, b, c, x[ 5], S22); /* 22 */
255 GG (c, d, a, b, x[ 9], S23); /* 23 */
256 GG (b, c, d, a, x[13], S24); /* 24 */
257 GG (a, b, c, d, x[ 2], S21); /* 25 */
258 GG (d, a, b, c, x[ 6], S22); /* 26 */
259 GG (c, d, a, b, x[10], S23); /* 27 */
260 GG (b, c, d, a, x[14], S24); /* 28 */
261 GG (a, b, c, d, x[ 3], S21); /* 29 */
262 GG (d, a, b, c, x[ 7], S22); /* 30 */
263 GG (c, d, a, b, x[11], S23); /* 31 */
264 GG (b, c, d, a, x[15], S24); /* 32 */
266 /* Round 3 */
267 HH (a, b, c, d, x[ 0], S31); /* 33 */
268 HH (d, a, b, c, x[ 8], S32); /* 34 */
269 HH (c, d, a, b, x[ 4], S33); /* 35 */
270 HH (b, c, d, a, x[12], S34); /* 36 */
271 HH (a, b, c, d, x[ 2], S31); /* 37 */
272 HH (d, a, b, c, x[10], S32); /* 38 */
273 HH (c, d, a, b, x[ 6], S33); /* 39 */
274 HH (b, c, d, a, x[14], S34); /* 40 */
275 HH (a, b, c, d, x[ 1], S31); /* 41 */
276 HH (d, a, b, c, x[ 9], S32); /* 42 */
277 HH (c, d, a, b, x[ 5], S33); /* 43 */
278 HH (b, c, d, a, x[13], S34); /* 44 */
279 HH (a, b, c, d, x[ 3], S31); /* 45 */
280 HH (d, a, b, c, x[11], S32); /* 46 */
281 HH (c, d, a, b, x[ 7], S33); /* 47 */
282 HH (b, c, d, a, x[15], S34); /* 48 */
284 state[0] += a;
285 state[1] += b;
286 state[2] += c;
287 state[3] += d;
289 /* Zeroize sensitive information. */
290 memset(x, 0, sizeof (x));
294 * Encodes input (UINT4) into output (unsigned char). Assumes len is
295 * a multiple of 4.
297 static void
298 Encode(unsigned char *output, UINT4 *input, unsigned int len)
300 unsigned int i, j;
302 for (i = 0, j = 0; j < len; i++, j += 4) {
303 output[j] = (unsigned char)(input[i] & 0xff);
304 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
305 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
306 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
311 * Decodes input (unsigned char) into output (UINT4). Assumes len is
312 * a multiple of 4.
314 static void
315 Decode(UINT4 *output, const unsigned char *input, unsigned int len)
317 unsigned int i, j;
319 for (i = 0, j = 0; j < len; i++, j += 4)
320 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
321 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
324 #endif /* HAVE_MD4_H */