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