Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/common/crypto/md4/md4.c
+++ new/usr/src/common/crypto/md4/md4.c
1 1 /*
2 - * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
4 4 */
5 5
6 -#pragma ident "%Z%%M% %I% %E% SMI"
7 -
8 6 /*
9 7 * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
10 8 */
11 9
12 10 /*
13 11 * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
14 12 *
15 13 * License to copy and use this software is granted provided that it
16 14 * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
17 15 * Algorithm" in all material mentioning or referencing this software
18 16 * or this function.
19 17 *
20 18 * License is also granted to make and use derivative works provided
21 19 * that such works are identified as "derived from the RSA Data
22 20 * Security, Inc. MD4 Message-Digest Algorithm" in all material
23 21 * mentioning or referencing the derived work.
24 22 *
25 23 * RSA Data Security, Inc. makes no representations concerning either
26 24 * the merchantability of this software or the suitability of this
27 25 * software for any particular purpose. It is provided "as is"
28 26 * without express or implied warranty of any kind.
29 27 *
30 28 * These notices must be retained in any copies of any part of this
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
31 29 * documentation and/or software.
32 30 */
33 31
34 32 #include <sys/types.h>
35 33 #ifdef _KERNEL
36 34 #include <sys/sunddi.h>
37 35 #else
38 36 #include <strings.h>
39 37 #endif /* _KERNEL */
40 38
39 +#if defined(__i386) || defined(__amd64)
40 +#define UNALIGNED_POINTERS_PERMITTED
41 +#endif
42 +
41 43 #include <sys/md4.h>
42 44
43 45 /*
44 46 * Constants for MD4Transform routine.
45 47 */
46 48 #define S11 3
47 49 #define S12 7
48 50 #define S13 11
49 51 #define S14 19
50 52 #define S21 3
51 53 #define S22 5
52 54 #define S23 9
53 55 #define S24 13
54 56 #define S31 3
55 57 #define S32 9
56 58 #define S33 11
57 59 #define S34 15
58 60
59 61 static void MD4Transform(uint32_t [4], unsigned char [64]);
60 62 static void Encode(unsigned char *, uint32_t *, unsigned int);
61 63 static void Decode(uint32_t *, unsigned char *, unsigned int);
62 64
63 65 static unsigned char PADDING[64] = {
64 66 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
67 69 };
68 70
69 71 /*
70 72 * F, G and H are basic MD4 functions.
71 73 */
72 74 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
73 75 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
74 76 #define H(x, y, z) ((x) ^ (y) ^ (z))
75 77
76 78 /*
77 79 * ROTATE_LEFT rotates x left n bits.
78 80 */
79 81 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
80 82
81 83 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
82 84 /* Rotation is separate from addition to prevent recomputation */
83 85
84 86 #define FF(a, b, c, d, x, s) { \
85 87 (a) += F((b), (c), (d)) + (x); \
86 88 (a) = ROTATE_LEFT((a), (s)); \
87 89 }
88 90 #define GG(a, b, c, d, x, s) { \
89 91 (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5a827999; \
90 92 (a) = ROTATE_LEFT((a), (s)); \
91 93 }
92 94 #define HH(a, b, c, d, x, s) { \
93 95 (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ed9eba1; \
94 96 (a) = ROTATE_LEFT((a), (s)); \
95 97 }
96 98
97 99 /*
98 100 * MD4 initialization. Begins an MD4 operation, writing a new context.
99 101 */
100 102 void
101 103 MD4Init(MD4_CTX *context)
102 104 {
103 105 context->count[0] = context->count[1] = 0;
104 106
105 107 /*
106 108 * Load magic initialization constants.
107 109 */
108 110 context->state[0] = 0x67452301UL;
109 111 context->state[1] = 0xefcdab89UL;
110 112 context->state[2] = 0x98badcfeUL;
111 113 context->state[3] = 0x10325476UL;
112 114 }
113 115
114 116
115 117 /*
116 118 * MD4 block update operation. Continues an MD4 message-digest
117 119 * operation, processing another message block, and updating the
118 120 * context.
119 121 */
120 122 void
121 123 MD4Update(MD4_CTX *context, const void *_RESTRICT_KYWD inptr, size_t inputLen)
122 124 {
123 125 unsigned int i, index, partLen;
124 126 uchar_t *input = (uchar_t *)inptr;
125 127
126 128 /* Compute number of bytes mod 64 */
127 129 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
128 130 /* Update number of bits */
129 131 if ((context->count[0] += ((uint32_t)inputLen << 3))
130 132 < ((uint32_t)inputLen << 3))
131 133 context->count[1]++;
132 134 context->count[1] += ((uint32_t)inputLen >> 29);
133 135
134 136 partLen = 64 - index;
135 137
136 138 /*
137 139 * Transform as many times as possible.
138 140 */
139 141 if (inputLen >= partLen) {
140 142 bcopy(input, &context->buffer[index], partLen);
141 143 MD4Transform(context->state, (uchar_t *)context->buffer);
142 144
143 145 for (i = partLen; i + 63 < inputLen; i += 64) {
144 146 MD4Transform(context->state, (uchar_t *)&input[i]);
145 147 }
146 148
147 149 index = 0;
148 150 } else {
149 151 i = 0;
150 152 }
151 153
152 154 /* Buffer remaining input */
153 155 bcopy(&input[i], &context->buffer[index], inputLen - i);
154 156 }
155 157
156 158 /*
157 159 * MD4 finalization. Ends an MD4 message-digest operation, writing the
158 160 * the message digest and zeroizing the context.
159 161 */
160 162 void
161 163 MD4Final(void *digest, MD4_CTX *context)
162 164 {
163 165 unsigned char bits[8];
164 166 unsigned int index, padLen;
165 167
166 168 /* Save number of bits */
167 169 Encode(bits, context->count, 8);
168 170
169 171 /*
170 172 * Pad out to 56 mod 64.
171 173 */
172 174 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
173 175 padLen = (index < 56) ? (56 - index) : (120 - index);
174 176 MD4Update(context, PADDING, padLen);
175 177
176 178 /* Append length (before padding) */
177 179 MD4Update(context, bits, 8);
178 180 /* Store state in digest */
179 181 Encode(digest, context->state, 16);
180 182
181 183 /* zeroize sensitive information */
182 184 bzero(context, sizeof (*context));
183 185 }
184 186
185 187 /*
186 188 * MD4 basic transformation. Transforms state based on block.
187 189 */
188 190 static void
189 191 MD4Transform(uint32_t state[4], unsigned char block[64])
190 192 {
191 193 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
192 194
193 195
194 196 Decode(x, block, 64);
195 197
196 198 /* Round 1 */
197 199 FF(a, b, c, d, x[ 0], S11); /* 1 */
198 200 FF(d, a, b, c, x[ 1], S12); /* 2 */
199 201 FF(c, d, a, b, x[ 2], S13); /* 3 */
200 202 FF(b, c, d, a, x[ 3], S14); /* 4 */
201 203 FF(a, b, c, d, x[ 4], S11); /* 5 */
202 204 FF(d, a, b, c, x[ 5], S12); /* 6 */
203 205 FF(c, d, a, b, x[ 6], S13); /* 7 */
204 206 FF(b, c, d, a, x[ 7], S14); /* 8 */
205 207 FF(a, b, c, d, x[ 8], S11); /* 9 */
206 208 FF(d, a, b, c, x[ 9], S12); /* 10 */
207 209 FF(c, d, a, b, x[10], S13); /* 11 */
208 210 FF(b, c, d, a, x[11], S14); /* 12 */
209 211 FF(a, b, c, d, x[12], S11); /* 13 */
210 212 FF(d, a, b, c, x[13], S12); /* 14 */
211 213 FF(c, d, a, b, x[14], S13); /* 15 */
212 214 FF(b, c, d, a, x[15], S14); /* 16 */
213 215
214 216 /* Round 2 */
215 217 GG(a, b, c, d, x[ 0], S21); /* 17 */
216 218 GG(d, a, b, c, x[ 4], S22); /* 18 */
217 219 GG(c, d, a, b, x[ 8], S23); /* 19 */
218 220 GG(b, c, d, a, x[12], S24); /* 20 */
219 221 GG(a, b, c, d, x[ 1], S21); /* 21 */
220 222 GG(d, a, b, c, x[ 5], S22); /* 22 */
221 223 GG(c, d, a, b, x[ 9], S23); /* 23 */
222 224 GG(b, c, d, a, x[13], S24); /* 24 */
223 225 GG(a, b, c, d, x[ 2], S21); /* 25 */
224 226 GG(d, a, b, c, x[ 6], S22); /* 26 */
225 227 GG(c, d, a, b, x[10], S23); /* 27 */
226 228 GG(b, c, d, a, x[14], S24); /* 28 */
227 229 GG(a, b, c, d, x[ 3], S21); /* 29 */
228 230 GG(d, a, b, c, x[ 7], S22); /* 30 */
229 231 GG(c, d, a, b, x[11], S23); /* 31 */
230 232 GG(b, c, d, a, x[15], S24); /* 32 */
231 233
232 234
233 235 /* Round 3 */
234 236 HH(a, b, c, d, x[ 0], S31); /* 33 */
235 237 HH(d, a, b, c, x[ 8], S32); /* 34 */
236 238 HH(c, d, a, b, x[ 4], S33); /* 35 */
237 239 HH(b, c, d, a, x[12], S34); /* 36 */
238 240 HH(a, b, c, d, x[ 2], S31); /* 37 */
239 241 HH(d, a, b, c, x[10], S32); /* 38 */
240 242 HH(c, d, a, b, x[ 6], S33); /* 39 */
241 243 HH(b, c, d, a, x[14], S34); /* 40 */
242 244 HH(a, b, c, d, x[ 1], S31); /* 41 */
243 245 HH(d, a, b, c, x[ 9], S32); /* 42 */
244 246 HH(c, d, a, b, x[ 5], S33); /* 43 */
245 247 HH(b, c, d, a, x[13], S34); /* 44 */
246 248 HH(a, b, c, d, x[ 3], S31); /* 45 */
247 249 HH(d, a, b, c, x[11], S32); /* 46 */
248 250 HH(c, d, a, b, x[ 7], S33); /* 47 */
249 251 HH(b, c, d, a, x[15], S34); /* 48 */
250 252
251 253 state[0] += a;
252 254 state[1] += b;
253 255 state[2] += c;
254 256 state[3] += d;
↓ open down ↓ |
204 lines elided |
↑ open up ↑ |
255 257
256 258 /* zeroize sensitive information */
257 259 bzero(x, sizeof (*x));
258 260 }
259 261
260 262 /*
261 263 * Encodes input (uint32_t) into output (unsigned char). Assumes len is
262 264 * a multiple of 4.
263 265 */
264 266 static void
265 -Encode(output, input, len)
266 - unsigned char *output;
267 - uint32_t *input;
268 - unsigned int len;
267 +Encode(unsigned char *output, uint32_t *input, unsigned int len)
269 268 {
270 269 unsigned int i, j;
271 270
272 271 for (i = 0, j = 0; j < len; i++, j += 4) {
272 +#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
273 + *(uint32_t *)&output[j] = input[i];
274 +#else
275 + /* endian-independent code */
273 276 output[j] = (unsigned char)(input[i] & 0xff);
274 277 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
275 278 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
276 279 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
280 +#endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
277 281 }
278 282 }
279 283
280 284 /*
281 285 * Decodes input (unsigned char) into output (uint32_t). Assumes len is
282 286 * a multiple of 4.
283 287 */
284 288 static void
285 -Decode(output, input, len)
286 - uint32_t *output;
287 - unsigned char *input;
288 - unsigned int len;
289 +Decode(uint32_t *output, unsigned char *input, unsigned int len)
289 290 {
290 291 unsigned int i, j;
291 292
292 - for (i = 0, j = 0; j < len; i++, j += 4)
293 + for (i = 0, j = 0; j < len; i++, j += 4) {
294 +#if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
295 + output[i] = *(uint32_t *)&input[j];
296 +#else
297 + /* endian-independent code */
293 298 output[i] = ((uint32_t)input[j]) |
294 - (((uint32_t)input[j+1]) << 8) |
295 - (((uint32_t)input[j+2]) << 16) |
296 - (((uint32_t)input[j+3]) << 24);
299 + (((uint32_t)input[j+1]) << 8) |
300 + (((uint32_t)input[j+2]) << 16) |
301 + (((uint32_t)input[j+3]) << 24);
302 +#endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
303 + }
304 +
297 305 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX