1 /* 2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm 10 */ 11 12 /* 13 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. 14 * 15 * License to copy and use this software is granted provided that it 16 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest 17 * Algorithm" in all material mentioning or referencing this software 18 * or this function. 19 * 20 * License is also granted to make and use derivative works provided 21 * that such works are identified as "derived from the RSA Data 22 * Security, Inc. MD4 Message-Digest Algorithm" in all material 23 * mentioning or referencing the derived work. 24 * 25 * RSA Data Security, Inc. makes no representations concerning either 26 * the merchantability of this software or the suitability of this 27 * software for any particular purpose. It is provided "as is" 28 * without express or implied warranty of any kind. 29 * 30 * These notices must be retained in any copies of any part of this 31 * documentation and/or software. 32 */ 33 34 #include <strings.h> 35 #include <sys/types.h> 36 37 #include "md4.h" 38 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(ulong_t [4], unsigned char [64]); 56 static void Encode(unsigned char *, ulong_t *, unsigned int); 57 static void Decode(ulong_t *, unsigned char *, unsigned int); 58 59 static unsigned char PADDING[64] = { 60 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 63 }; 64 65 /* 66 * F, G and H are basic MD4 functions. 67 */ 68 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 69 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) 70 #define H(x, y, z) ((x) ^ (y) ^ (z)) 71 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 80 #define FF(a, b, c, d, x, s) { \ 81 (a) += F((b), (c), (d)) + (x); \ 82 (a) = ROTATE_LEFT((a), (s)); \ 83 } 84 #define GG(a, b, c, d, x, s) { \ 85 (a) += G((b), (c), (d)) + (x) + (ulong_t)0x5a827999; \ 86 (a) = ROTATE_LEFT((a), (s)); \ 87 } 88 #define HH(a, b, c, d, x, s) { \ 89 (a) += H((b), (c), (d)) + (x) + (ulong_t)0x6ed9eba1; \ 90 (a) = ROTATE_LEFT((a), (s)); \ 91 } 92 93 /* 94 * MD4 initialization. Begins an MD4 operation, writing a new context. 95 */ 96 void 97 MD4Init(context) 98 MD4_CTX *context; /* context */ 99 { 100 context->count[0] = context->count[1] = 0; 101 102 /* 103 * Load magic initialization constants. 104 */ 105 context->state[0] = 0x67452301UL; 106 context->state[1] = 0xefcdab89UL; 107 context->state[2] = 0x98badcfeUL; 108 context->state[3] = 0x10325476UL; 109 } 110 111 112 /* 113 * MD4 block update operation. Continues an MD4 message-digest 114 * operation, processing another message block, and updating the 115 * context. 116 */ 117 void 118 MD4Update(MD4_CTX *context, const void *_RESTRICT_KYWD inptr, size_t inputLen) 119 { 120 unsigned int i, index, partLen; 121 uchar_t *input = (uchar_t *)inptr; 122 123 /* Compute number of bytes mod 64 */ 124 index = (unsigned int)((context->count[0] >> 3) & 0x3F); 125 /* Update number of bits */ 126 if ((context->count[0] += ((ulong_t)inputLen << 3)) 127 < ((ulong_t)inputLen << 3)) 128 context->count[1]++; 129 context->count[1] += ((ulong_t)inputLen >> 29); 130 131 partLen = 64 - index; 132 133 /* 134 * Transform as many times as possible. 135 */ 136 if (inputLen >= partLen) { 137 bcopy(input, &context->buffer[index], partLen); 138 MD4Transform(context->state, (uchar_t *)context->buffer); 139 140 for (i = partLen; i + 63 < inputLen; i += 64) { 141 MD4Transform(context->state, (uchar_t *)&input[i]); 142 } 143 144 index = 0; 145 } else { 146 i = 0; 147 } 148 149 /* Buffer remaining input */ 150 bcopy(&input[i], &context->buffer[index], inputLen - i); 151 } 152 153 /* 154 * MD4 finalization. Ends an MD4 message-digest operation, writing the 155 * the message digest and zeroizing the context. 156 */ 157 void 158 MD4Final(void *digest, MD4_CTX *context) 159 { 160 unsigned char bits[8]; 161 unsigned int index, padLen; 162 163 /* Save number of bits */ 164 Encode(bits, context->count, 8); 165 166 /* 167 * Pad out to 56 mod 64. 168 */ 169 index = (unsigned int)((context->count[0] >> 3) & 0x3f); 170 padLen = (index < 56) ? (56 - index) : (120 - index); 171 MD4Update(context, PADDING, padLen); 172 173 /* Append length (before padding) */ 174 MD4Update(context, bits, 8); 175 /* Store state in digest */ 176 Encode(digest, context->state, 16); 177 178 /* zeroize sensitive information */ 179 bzero(context, sizeof (*context)); 180 } 181 182 /* 183 * MD4 basic transformation. Transforms state based on block. 184 */ 185 static void 186 MD4Transform(ulong_t state[4], unsigned char block[64]) 187 { 188 ulong_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 189 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 230 /* Round 3 */ 231 HH(a, b, c, d, x[ 0], S31); /* 33 */ 232 HH(d, a, b, c, x[ 8], S32); /* 34 */ 233 HH(c, d, a, b, x[ 4], S33); /* 35 */ 234 HH(b, c, d, a, x[12], S34); /* 36 */ 235 HH(a, b, c, d, x[ 2], S31); /* 37 */ 236 HH(d, a, b, c, x[10], S32); /* 38 */ 237 HH(c, d, a, b, x[ 6], S33); /* 39 */ 238 HH(b, c, d, a, x[14], S34); /* 40 */ 239 HH(a, b, c, d, x[ 1], S31); /* 41 */ 240 HH(d, a, b, c, x[ 9], S32); /* 42 */ 241 HH(c, d, a, b, x[ 5], S33); /* 43 */ 242 HH(b, c, d, a, x[13], S34); /* 44 */ 243 HH(a, b, c, d, x[ 3], S31); /* 45 */ 244 HH(d, a, b, c, x[11], S32); /* 46 */ 245 HH(c, d, a, b, x[ 7], S33); /* 47 */ 246 HH(b, c, d, a, x[15], S34); /* 48 */ 247 248 state[0] += a; 249 state[1] += b; 250 state[2] += c; 251 state[3] += d; 252 253 /* zeroize sensitive information */ 254 bzero(x, sizeof (*x)); 255 } 256 257 /* 258 * Encodes input (ulong_t) into output (unsigned char). Assumes len is 259 * a multiple of 4. 260 */ 261 static void 262 Encode(output, input, len) 263 unsigned char *output; 264 ulong_t *input; 265 unsigned int len; 266 { 267 unsigned int i, j; 268 269 for (i = 0, j = 0; j < len; i++, j += 4) { 270 output[j] = (unsigned char)(input[i] & 0xff); 271 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 272 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 273 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 274 } 275 } 276 277 /* 278 * Decodes input (unsigned char) into output (ulong_t). Assumes len is 279 * a multiple of 4. 280 */ 281 static void 282 Decode(output, input, len) 283 ulong_t *output; 284 unsigned char *input; 285 unsigned int len; 286 { 287 unsigned int i, j; 288 289 for (i = 0, j = 0; j < len; i++, j += 4) 290 output[i] = ((ulong_t)input[j]) | 291 (((ulong_t)input[j+1]) << 8) | 292 (((ulong_t)input[j+2]) << 16) | 293 (((ulong_t)input[j+3]) << 24); 294 } 295