1 /* $NetBSD: md4c.c,v 1.3 2008/02/16 17:37:13 apb Exp $ */ 2 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 */ 8 9 /* 10 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. 11 * 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. 16 * 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. 21 * 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. 26 * 27 * These notices must be retained in any copies of any part of this 28 * documentation and/or software. 29 */ 30 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.3 2008/02/16 17:37:13 apb Exp $"); 35 #endif /* LIBC_SCCS and not lint */ 36 37 #include "namespace.h" 38 39 #include <sys/types.h> 40 41 #include <assert.h> 42 #include <md4.h> 43 #include <string.h> 44 45 #if HAVE_NBTOOL_CONFIG_H 46 #include "nbtool_config.h" 47 #endif 48 49 #else 50 51 #include <sys/param.h> 52 #include <sys/md4.h> 53 #include <lib/libkern/libkern.h> 54 55 #endif /* !_KERNEL && !_STANDALONE */ 56 57 #if !HAVE_MD4_H 58 59 typedef unsigned char *POINTER; 60 typedef uint16_t UINT2; 61 typedef uint32_t UINT4; 62 63 /* 64 * Constants for MD4Transform routine. 65 */ 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 78 79 static void MD4Transform __P((UINT4 [4], const unsigned char [64])); 80 81 static void Encode __P((unsigned char *, UINT4 *, unsigned int)); 82 static void Decode __P((UINT4 *, const unsigned char *, unsigned int)); 83 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 88 }; 89 90 /* 91 * F, G and H are basic MD4 functions. 92 */ 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)) 96 97 /* 98 * ROTATE_LEFT rotates x left n bits. 99 */ 100 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 101 102 /* 103 * FF, GG and HH are transformations for rounds 1, 2 and 3. 104 * Rotation is separate from addition to prevent recomputation. 105 */ 106 #define FF(a, b, c, d, x, s) { \ 107 (a) += F ((b), (c), (d)) + (x); \ 108 (a) = ROTATE_LEFT ((a), (s)); \ 109 } 110 111 #define GG(a, b, c, d, x, s) { \ 112 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \ 113 (a) = ROTATE_LEFT ((a), (s)); \ 114 } 115 116 #define HH(a, b, c, d, x, s) { \ 117 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \ 118 (a) = ROTATE_LEFT ((a), (s)); \ 119 } 120 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 127 128 /* 129 * MD4 initialization. Begins an MD4 operation, writing a new context. 130 */ 131 void 132 MD4Init(context) 133 MD4_CTX *context; /* context */ 134 { 135 136 _DIAGASSERT(context != 0); 137 138 context->count[0] = context->count[1] = 0; 139 140 /* Load magic initialization constants. */ 141 context->state[0] = 0x67452301; 142 context->state[1] = 0xefcdab89; 143 context->state[2] = 0x98badcfe; 144 context->state[3] = 0x10325476; 145 } 146 147 /* 148 * MD4 block update operation. Continues an MD4 message-digest 149 * operation, processing another message block, and updating the 150 * context. 151 */ 152 void 153 MD4Update (context, input, inputLen) 154 MD4_CTX *context; /* context */ 155 const unsigned char *input; /* input block */ 156 unsigned int inputLen; /* length of input block */ 157 { 158 unsigned int i, idx, partLen; 159 160 _DIAGASSERT(context != 0); 161 _DIAGASSERT(input != 0); 162 163 /* Compute number of bytes mod 64 */ 164 idx = (unsigned int)((context->count[0] >> 3) & 0x3F); 165 166 /* Update number of bits */ 167 if ((context->count[0] += ((UINT4)inputLen << 3)) 168 < ((UINT4)inputLen << 3)) 169 context->count[1]++; 170 context->count[1] += ((UINT4)inputLen >> 29); 171 172 partLen = 64 - idx; 173 174 /* Transform as many times as possible. */ 175 if (inputLen >= partLen) { 176 memcpy(&context->buffer[idx], input, partLen); 177 MD4Transform(context->state, context->buffer); 178 179 for (i = partLen; i + 63 < inputLen; i += 64) 180 MD4Transform(context->state, &input[i]); 181 182 idx = 0; 183 } else 184 i = 0; 185 186 /* Buffer remaining input */ 187 memcpy(&context->buffer[idx], &input[i], inputLen - i); 188 } 189 190 /* 191 * MD4 finalization. Ends an MD4 message-digest operation, writing the 192 * message digest and zeroing the context. 193 */ 194 void 195 MD4Final (digest, context) 196 unsigned char digest[16]; /* message digest */ 197 MD4_CTX *context; /* context */ 198 { 199 unsigned char bits[8]; 200 unsigned int idx, padLen; 201 202 _DIAGASSERT(digest != 0); 203 _DIAGASSERT(context != 0); 204 205 /* Save number of bits */ 206 Encode(bits, context->count, 8); 207 208 /* Pad out to 56 mod 64. */ 209 idx = (unsigned int)((context->count[0] >> 3) & 0x3f); 210 padLen = (idx < 56) ? (56 - idx) : (120 - idx); 211 MD4Update(context, PADDING, padLen); 212 213 /* Append length (before padding) */ 214 MD4Update(context, bits, 8); 215 216 /* Store state in digest */ 217 Encode(digest, context->state, 16); 218 219 /* Zeroize sensitive information. */ 220 memset(context, 0, sizeof(*context)); 221 } 222 223 /* 224 * MD4 basic transformation. Transforms state based on block. 225 */ 226 static void 227 MD4Transform (state, block) 228 UINT4 state[4]; 229 const unsigned char block[64]; 230 { 231 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 232 233 Decode(x, block, 64); 234 235 /* Round 1 */ 236 FF (a, b, c, d, x[ 0], S11); /* 1 */ 237 FF (d, a, b, c, x[ 1], S12); /* 2 */ 238 FF (c, d, a, b, x[ 2], S13); /* 3 */ 239 FF (b, c, d, a, x[ 3], S14); /* 4 */ 240 FF (a, b, c, d, x[ 4], S11); /* 5 */ 241 FF (d, a, b, c, x[ 5], S12); /* 6 */ 242 FF (c, d, a, b, x[ 6], S13); /* 7 */ 243 FF (b, c, d, a, x[ 7], S14); /* 8 */ 244 FF (a, b, c, d, x[ 8], S11); /* 9 */ 245 FF (d, a, b, c, x[ 9], S12); /* 10 */ 246 FF (c, d, a, b, x[10], S13); /* 11 */ 247 FF (b, c, d, a, x[11], S14); /* 12 */ 248 FF (a, b, c, d, x[12], S11); /* 13 */ 249 FF (d, a, b, c, x[13], S12); /* 14 */ 250 FF (c, d, a, b, x[14], S13); /* 15 */ 251 FF (b, c, d, a, x[15], S14); /* 16 */ 252 253 /* Round 2 */ 254 GG (a, b, c, d, x[ 0], S21); /* 17 */ 255 GG (d, a, b, c, x[ 4], S22); /* 18 */ 256 GG (c, d, a, b, x[ 8], S23); /* 19 */ 257 GG (b, c, d, a, x[12], S24); /* 20 */ 258 GG (a, b, c, d, x[ 1], S21); /* 21 */ 259 GG (d, a, b, c, x[ 5], S22); /* 22 */ 260 GG (c, d, a, b, x[ 9], S23); /* 23 */ 261 GG (b, c, d, a, x[13], S24); /* 24 */ 262 GG (a, b, c, d, x[ 2], S21); /* 25 */ 263 GG (d, a, b, c, x[ 6], S22); /* 26 */ 264 GG (c, d, a, b, x[10], S23); /* 27 */ 265 GG (b, c, d, a, x[14], S24); /* 28 */ 266 GG (a, b, c, d, x[ 3], S21); /* 29 */ 267 GG (d, a, b, c, x[ 7], S22); /* 30 */ 268 GG (c, d, a, b, x[11], S23); /* 31 */ 269 GG (b, c, d, a, x[15], S24); /* 32 */ 270 271 /* Round 3 */ 272 HH (a, b, c, d, x[ 0], S31); /* 33 */ 273 HH (d, a, b, c, x[ 8], S32); /* 34 */ 274 HH (c, d, a, b, x[ 4], S33); /* 35 */ 275 HH (b, c, d, a, x[12], S34); /* 36 */ 276 HH (a, b, c, d, x[ 2], S31); /* 37 */ 277 HH (d, a, b, c, x[10], S32); /* 38 */ 278 HH (c, d, a, b, x[ 6], S33); /* 39 */ 279 HH (b, c, d, a, x[14], S34); /* 40 */ 280 HH (a, b, c, d, x[ 1], S31); /* 41 */ 281 HH (d, a, b, c, x[ 9], S32); /* 42 */ 282 HH (c, d, a, b, x[ 5], S33); /* 43 */ 283 HH (b, c, d, a, x[13], S34); /* 44 */ 284 HH (a, b, c, d, x[ 3], S31); /* 45 */ 285 HH (d, a, b, c, x[11], S32); /* 46 */ 286 HH (c, d, a, b, x[ 7], S33); /* 47 */ 287 HH (b, c, d, a, x[15], S34); /* 48 */ 288 289 state[0] += a; 290 state[1] += b; 291 state[2] += c; 292 state[3] += d; 293 294 /* Zeroize sensitive information. */ 295 memset(x, 0, sizeof (x)); 296 } 297 298 /* 299 * Encodes input (UINT4) into output (unsigned char). Assumes len is 300 * a multiple of 4. 301 */ 302 static void 303 Encode(output, input, len) 304 unsigned char *output; 305 UINT4 *input; 306 unsigned int len; 307 { 308 unsigned int i, j; 309 310 for (i = 0, j = 0; j < len; i++, j += 4) { 311 output[j] = (unsigned char)(input[i] & 0xff); 312 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 313 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 314 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 315 } 316 } 317 318 /* 319 * Decodes input (unsigned char) into output (UINT4). Assumes len is 320 * a multiple of 4. 321 */ 322 static void 323 Decode(output, input, len) 324 UINT4 *output; 325 const unsigned char *input; 326 unsigned int len; 327 { 328 unsigned int i, j; 329 330 for (i = 0, j = 0; j < len; i++, j += 4) 331 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | 332 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); 333 } 334 335 #endif /* HAVE_MD4_H */ 336