]>
Commit | Line | Data |
---|---|---|
bd3576d2 | 1 | /* ==================================================================== |
20f7563f | 2 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. |
bd3576d2 UM |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 9 | * notice, this list of conditions and the following disclaimer. |
bd3576d2 UM |
10 | * |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * 3. All advertising materials mentioning features or use of this | |
17 | * software must display the following acknowledgment: | |
18 | * "This product includes software developed by the OpenSSL Project | |
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
20 | * | |
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
22 | * endorse or promote products derived from this software without | |
23 | * prior written permission. For written permission, please contact | |
24 | * licensing@OpenSSL.org. | |
25 | * | |
26 | * 5. Products derived from this software may not be called "OpenSSL" | |
27 | * nor may "OpenSSL" appear in their names without prior written | |
28 | * permission of the OpenSSL Project. | |
29 | * | |
30 | * 6. Redistributions of any form whatsoever must retain the following | |
31 | * acknowledgment: | |
32 | * "This product includes software developed by the OpenSSL Project | |
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
34 | * | |
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | * ==================================================================== | |
48 | * | |
bd3576d2 UM |
49 | */ |
50 | ||
1d97c843 | 51 | /*- |
bd3576d2 UM |
52 | * This is a generic 32 bit "collector" for message digest algorithms. |
53 | * Whenever needed it collects input character stream into chunks of | |
54 | * 32 bit values and invokes a block function that performs actual hash | |
55 | * calculations. | |
56 | * | |
57 | * Porting guide. | |
58 | * | |
59 | * Obligatory macros: | |
60 | * | |
61 | * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN | |
0f113f3e | 62 | * this macro defines byte order of input stream. |
bd3576d2 | 63 | * HASH_CBLOCK |
0f113f3e | 64 | * size of a unit chunk HASH_BLOCK operates on. |
bd3576d2 | 65 | * HASH_LONG |
04f8bcf1 | 66 | * has to be at lest 32 bit wide. |
bd3576d2 | 67 | * HASH_CTX |
0f113f3e MC |
68 | * context structure that at least contains following |
69 | * members: | |
70 | * typedef struct { | |
71 | * ... | |
72 | * HASH_LONG Nl,Nh; | |
73 | * either { | |
74 | * HASH_LONG data[HASH_LBLOCK]; | |
75 | * unsigned char data[HASH_CBLOCK]; | |
76 | * }; | |
77 | * unsigned int num; | |
78 | * ... | |
79 | * } HASH_CTX; | |
80 | * data[] vector is expected to be zeroed upon first call to | |
81 | * HASH_UPDATE. | |
bd3576d2 | 82 | * HASH_UPDATE |
0f113f3e | 83 | * name of "Update" function, implemented here. |
bd3576d2 | 84 | * HASH_TRANSFORM |
0f113f3e | 85 | * name of "Transform" function, implemented here. |
bd3576d2 | 86 | * HASH_FINAL |
0f113f3e | 87 | * name of "Final" function, implemented here. |
bd3576d2 | 88 | * HASH_BLOCK_DATA_ORDER |
0f113f3e MC |
89 | * name of "block" function capable of treating *unaligned* input |
90 | * message in original (data) byte order, implemented externally. | |
1cbde6e4 | 91 | * HASH_MAKE_STRING |
0f113f3e | 92 | * macro convering context variables to an ASCII hash string. |
bd3576d2 | 93 | * |
bd3576d2 UM |
94 | * MD5 example: |
95 | * | |
0f113f3e | 96 | * #define DATA_ORDER_IS_LITTLE_ENDIAN |
bd3576d2 | 97 | * |
0f113f3e | 98 | * #define HASH_LONG MD5_LONG |
0f113f3e MC |
99 | * #define HASH_CTX MD5_CTX |
100 | * #define HASH_CBLOCK MD5_CBLOCK | |
101 | * #define HASH_UPDATE MD5_Update | |
102 | * #define HASH_TRANSFORM MD5_Transform | |
103 | * #define HASH_FINAL MD5_Final | |
104 | * #define HASH_BLOCK_DATA_ORDER md5_block_data_order | |
bd3576d2 | 105 | * |
0f113f3e | 106 | * <appro@fy.chalmers.se> |
bd3576d2 UM |
107 | */ |
108 | ||
109 | #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) | |
0f113f3e | 110 | # error "DATA_ORDER must be defined!" |
bd3576d2 UM |
111 | #endif |
112 | ||
113 | #ifndef HASH_CBLOCK | |
0f113f3e | 114 | # error "HASH_CBLOCK must be defined!" |
bd3576d2 UM |
115 | #endif |
116 | #ifndef HASH_LONG | |
0f113f3e | 117 | # error "HASH_LONG must be defined!" |
bd3576d2 UM |
118 | #endif |
119 | #ifndef HASH_CTX | |
0f113f3e | 120 | # error "HASH_CTX must be defined!" |
bd3576d2 UM |
121 | #endif |
122 | ||
123 | #ifndef HASH_UPDATE | |
0f113f3e | 124 | # error "HASH_UPDATE must be defined!" |
bd3576d2 UM |
125 | #endif |
126 | #ifndef HASH_TRANSFORM | |
0f113f3e | 127 | # error "HASH_TRANSFORM must be defined!" |
bd3576d2 UM |
128 | #endif |
129 | #ifndef HASH_FINAL | |
0f113f3e | 130 | # error "HASH_FINAL must be defined!" |
bd3576d2 UM |
131 | #endif |
132 | ||
bd3576d2 | 133 | #ifndef HASH_BLOCK_DATA_ORDER |
0f113f3e | 134 | # error "HASH_BLOCK_DATA_ORDER must be defined!" |
bd3576d2 | 135 | #endif |
bd3576d2 UM |
136 | |
137 | /* | |
138 | * Engage compiler specific rotate intrinsic function if available. | |
139 | */ | |
140 | #undef ROTATE | |
141 | #ifndef PEDANTIC | |
cf5ecc3e | 142 | # if defined(_MSC_VER) |
0f113f3e | 143 | # define ROTATE(a,n) _lrotl(a,n) |
cf5ecc3e | 144 | # elif defined(__ICC) |
0f113f3e | 145 | # define ROTATE(a,n) _rotl(a,n) |
cf1b7d96 | 146 | # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) |
bd3576d2 UM |
147 | /* |
148 | * Some GNU C inline assembler templates. Note that these are | |
149 | * rotates by *constant* number of bits! But that's exactly | |
150 | * what we need here... | |
0f113f3e | 151 | * <appro@fy.chalmers.se> |
bd3576d2 | 152 | */ |
2f98abbc | 153 | # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) |
0f113f3e MC |
154 | # define ROTATE(a,n) ({ register unsigned int ret; \ |
155 | asm ( \ | |
156 | "roll %1,%0" \ | |
157 | : "=r"(ret) \ | |
158 | : "I"(n), "0"((unsigned int)(a)) \ | |
159 | : "cc"); \ | |
160 | ret; \ | |
161 | }) | |
a9c32ace | 162 | # elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \ |
0f113f3e MC |
163 | defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__) |
164 | # define ROTATE(a,n) ({ register unsigned int ret; \ | |
165 | asm ( \ | |
166 | "rlwinm %0,%1,%2,0,31" \ | |
167 | : "=r"(ret) \ | |
168 | : "r"(a), "I"(n)); \ | |
169 | ret; \ | |
170 | }) | |
70831126 | 171 | # elif defined(__s390x__) |
0f113f3e MC |
172 | # define ROTATE(a,n) ({ register unsigned int ret; \ |
173 | asm ("rll %0,%1,%2" \ | |
174 | : "=r"(ret) \ | |
175 | : "r"(a), "I"(n)); \ | |
176 | ret; \ | |
177 | }) | |
bd3576d2 UM |
178 | # endif |
179 | # endif | |
0f113f3e | 180 | #endif /* PEDANTIC */ |
bd3576d2 | 181 | |
bd3576d2 | 182 | #ifndef ROTATE |
0f113f3e | 183 | # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) |
bd3576d2 UM |
184 | #endif |
185 | ||
bd3576d2 UM |
186 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
187 | ||
0f113f3e MC |
188 | # ifndef PEDANTIC |
189 | # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) | |
190 | # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \ | |
af2c2823 | 191 | (defined(__x86_64) || defined(__x86_64__)) |
0f113f3e | 192 | # if !defined(B_ENDIAN) |
a2eb9688 AP |
193 | /* |
194 | * This gives ~30-40% performance improvement in SHA-256 compiled | |
195 | * with gcc [on P4]. Well, first macro to be frank. We can pull | |
196 | * this trick on x86* platforms only, because these CPUs can fetch | |
197 | * unaligned data without raising an exception. | |
198 | */ | |
0f113f3e MC |
199 | # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \ |
200 | asm ("bswapl %0":"=r"(r):"0"(r)); \ | |
201 | (c)+=4; (l)=r; }) | |
202 | # define HOST_l2c(l,c) ({ unsigned int r=(l); \ | |
203 | asm ("bswapl %0":"=r"(r):"0"(r)); \ | |
204 | *((unsigned int *)(c))=r; (c)+=4; r; }) | |
205 | # endif | |
206 | # elif defined(__aarch64__) | |
207 | # if defined(__BYTE_ORDER__) | |
208 | # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ | |
209 | # define HOST_c2l(c,l) ({ unsigned int r; \ | |
210 | asm ("rev %w0,%w1" \ | |
211 | :"=r"(r) \ | |
212 | :"r"(*((const unsigned int *)(c))));\ | |
213 | (c)+=4; (l)=r; }) | |
214 | # define HOST_l2c(l,c) ({ unsigned int r; \ | |
215 | asm ("rev %w0,%w1" \ | |
216 | :"=r"(r) \ | |
217 | :"r"((unsigned int)(l)));\ | |
218 | *((unsigned int *)(c))=r; (c)+=4; r; }) | |
219 | # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |
220 | # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l)) | |
221 | # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l)) | |
222 | # endif | |
039081b8 AP |
223 | # endif |
224 | # endif | |
a2eb9688 | 225 | # endif |
0f113f3e MC |
226 | # if defined(__s390__) || defined(__s390x__) |
227 | # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l)) | |
228 | # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l)) | |
229 | # endif | |
a2eb9688 | 230 | # endif |
a2eb9688 | 231 | |
0f113f3e MC |
232 | # ifndef HOST_c2l |
233 | # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ | |
234 | l|=(((unsigned long)(*((c)++)))<<16), \ | |
235 | l|=(((unsigned long)(*((c)++)))<< 8), \ | |
236 | l|=(((unsigned long)(*((c)++))) ) ) | |
237 | # endif | |
238 | # ifndef HOST_l2c | |
239 | # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ | |
240 | *((c)++)=(unsigned char)(((l)>>16)&0xff), \ | |
241 | *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ | |
242 | *((c)++)=(unsigned char)(((l) )&0xff), \ | |
243 | l) | |
244 | # endif | |
bd3576d2 UM |
245 | |
246 | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | |
247 | ||
0f113f3e MC |
248 | # ifndef PEDANTIC |
249 | # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) | |
250 | # if defined(__s390x__) | |
251 | # define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \ | |
252 | :"=d"(l) :"m"(*(const unsigned int *)(c)));\ | |
253 | (c)+=4; (l); }) | |
254 | # define HOST_l2c(l,c) ({ asm ("strv %1,%0" \ | |
255 | :"=m"(*(unsigned int *)(c)) :"d"(l));\ | |
256 | (c)+=4; (l); }) | |
257 | # endif | |
b38c0add | 258 | # endif |
0f113f3e MC |
259 | # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) |
260 | # ifndef B_ENDIAN | |
021e5043 | 261 | /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */ |
0f113f3e MC |
262 | # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l) |
263 | # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l) | |
264 | # endif | |
021e5043 | 265 | # endif |
1110cea0 | 266 | # endif |
a2eb9688 | 267 | |
0f113f3e MC |
268 | # ifndef HOST_c2l |
269 | # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ | |
270 | l|=(((unsigned long)(*((c)++)))<< 8), \ | |
271 | l|=(((unsigned long)(*((c)++)))<<16), \ | |
272 | l|=(((unsigned long)(*((c)++)))<<24) ) | |
273 | # endif | |
274 | # ifndef HOST_l2c | |
275 | # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ | |
276 | *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ | |
277 | *((c)++)=(unsigned char)(((l)>>16)&0xff), \ | |
278 | *((c)++)=(unsigned char)(((l)>>24)&0xff), \ | |
279 | l) | |
280 | # endif | |
bd3576d2 UM |
281 | |
282 | #endif | |
283 | ||
284 | /* | |
285 | * Time for some action:-) | |
286 | */ | |
287 | ||
0f113f3e MC |
288 | int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) |
289 | { | |
290 | const unsigned char *data = data_; | |
291 | unsigned char *p; | |
292 | HASH_LONG l; | |
293 | size_t n; | |
bd3576d2 | 294 | |
0f113f3e MC |
295 | if (len == 0) |
296 | return 1; | |
bd3576d2 | 297 | |
0f113f3e MC |
298 | l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL; |
299 | /* | |
300 | * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai | |
301 | * <weidai@eskimo.com> for pointing it out. | |
302 | */ | |
303 | if (l < c->Nl) /* overflow */ | |
304 | c->Nh++; | |
305 | c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on | |
306 | * 16-bit */ | |
307 | c->Nl = l; | |
308 | ||
309 | n = c->num; | |
310 | if (n != 0) { | |
311 | p = (unsigned char *)c->data; | |
312 | ||
313 | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | |
314 | memcpy(p + n, data, HASH_CBLOCK - n); | |
315 | HASH_BLOCK_DATA_ORDER(c, p, 1); | |
316 | n = HASH_CBLOCK - n; | |
317 | data += n; | |
318 | len -= n; | |
319 | c->num = 0; | |
320 | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | |
321 | } else { | |
322 | memcpy(p + n, data, len); | |
323 | c->num += (unsigned int)len; | |
324 | return 1; | |
325 | } | |
326 | } | |
327 | ||
328 | n = len / HASH_CBLOCK; | |
329 | if (n > 0) { | |
330 | HASH_BLOCK_DATA_ORDER(c, data, n); | |
331 | n *= HASH_CBLOCK; | |
332 | data += n; | |
333 | len -= n; | |
334 | } | |
335 | ||
336 | if (len != 0) { | |
337 | p = (unsigned char *)c->data; | |
338 | c->num = (unsigned int)len; | |
339 | memcpy(p, data, len); | |
340 | } | |
341 | return 1; | |
342 | } | |
343 | ||
344 | void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) | |
345 | { | |
346 | HASH_BLOCK_DATA_ORDER(c, data, 1); | |
347 | } | |
348 | ||
349 | int HASH_FINAL(unsigned char *md, HASH_CTX *c) | |
350 | { | |
351 | unsigned char *p = (unsigned char *)c->data; | |
352 | size_t n = c->num; | |
353 | ||
354 | p[n] = 0x80; /* there is always room for one */ | |
355 | n++; | |
356 | ||
357 | if (n > (HASH_CBLOCK - 8)) { | |
358 | memset(p + n, 0, HASH_CBLOCK - n); | |
359 | n = 0; | |
360 | HASH_BLOCK_DATA_ORDER(c, p, 1); | |
361 | } | |
362 | memset(p + n, 0, HASH_CBLOCK - 8 - n); | |
363 | ||
364 | p += HASH_CBLOCK - 8; | |
bd3576d2 | 365 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
0f113f3e MC |
366 | (void)HOST_l2c(c->Nh, p); |
367 | (void)HOST_l2c(c->Nl, p); | |
bd3576d2 | 368 | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
0f113f3e MC |
369 | (void)HOST_l2c(c->Nl, p); |
370 | (void)HOST_l2c(c->Nh, p); | |
bd3576d2 | 371 | #endif |
0f113f3e MC |
372 | p -= HASH_CBLOCK; |
373 | HASH_BLOCK_DATA_ORDER(c, p, 1); | |
374 | c->num = 0; | |
375 | memset(p, 0, HASH_CBLOCK); | |
bd3576d2 | 376 | |
1cbde6e4 | 377 | #ifndef HASH_MAKE_STRING |
0f113f3e | 378 | # error "HASH_MAKE_STRING must be defined!" |
1cbde6e4 | 379 | #else |
0f113f3e | 380 | HASH_MAKE_STRING(c, md); |
1cbde6e4 | 381 | #endif |
bd3576d2 | 382 | |
0f113f3e MC |
383 | return 1; |
384 | } | |
2f98abbc AP |
385 | |
386 | #ifndef MD32_REG_T | |
0f113f3e MC |
387 | # if defined(__alpha) || defined(__sparcv9) || defined(__mips) |
388 | # define MD32_REG_T long | |
2f98abbc AP |
389 | /* |
390 | * This comment was originaly written for MD5, which is why it | |
391 | * discusses A-D. But it basically applies to all 32-bit digests, | |
392 | * which is why it was moved to common header file. | |
393 | * | |
394 | * In case you wonder why A-D are declared as long and not | |
395 | * as MD5_LONG. Doing so results in slight performance | |
396 | * boost on LP64 architectures. The catch is we don't | |
397 | * really care if 32 MSBs of a 64-bit register get polluted | |
398 | * with eventual overflows as we *save* only 32 LSBs in | |
399 | * *either* case. Now declaring 'em long excuses the compiler | |
400 | * from keeping 32 MSBs zeroed resulting in 13% performance | |
401 | * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. | |
0f113f3e | 402 | * Well, to be honest it should say that this *prevents* |
2f98abbc | 403 | * performance degradation. |
0f113f3e | 404 | * <appro@fy.chalmers.se> |
30ab7af2 | 405 | */ |
0f113f3e | 406 | # else |
30ab7af2 AP |
407 | /* |
408 | * Above is not absolute and there are LP64 compilers that | |
409 | * generate better code if MD32_REG_T is defined int. The above | |
410 | * pre-processor condition reflects the circumstances under which | |
411 | * the conclusion was made and is subject to further extension. | |
0f113f3e | 412 | * <appro@fy.chalmers.se> |
2f98abbc | 413 | */ |
0f113f3e MC |
414 | # define MD32_REG_T int |
415 | # endif | |
2f98abbc | 416 | #endif |