2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * This is a generic 32 bit "collector" for message digest algorithms.
12 * Whenever needed it collects input character stream into chunks of
13 * 32 bit values and invokes a block function that performs actual hash
20 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
21 * this macro defines byte order of input stream.
23 * size of a unit chunk HASH_BLOCK operates on.
25 * has to be at lest 32 bit wide.
27 * context structure that at least contains following
33 * HASH_LONG data[HASH_LBLOCK];
34 * unsigned char data[HASH_CBLOCK];
39 * data[] vector is expected to be zeroed upon first call to
42 * name of "Update" function, implemented here.
44 * name of "Transform" function, implemented here.
46 * name of "Final" function, implemented here.
47 * HASH_BLOCK_DATA_ORDER
48 * name of "block" function capable of treating *unaligned* input
49 * message in original (data) byte order, implemented externally.
51 * macro convering context variables to an ASCII hash string.
55 * #define DATA_ORDER_IS_LITTLE_ENDIAN
57 * #define HASH_LONG MD5_LONG
58 * #define HASH_CTX MD5_CTX
59 * #define HASH_CBLOCK MD5_CBLOCK
60 * #define HASH_UPDATE MD5_Update
61 * #define HASH_TRANSFORM MD5_Transform
62 * #define HASH_FINAL MD5_Final
63 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
65 * <appro@fy.chalmers.se>
68 #include <openssl/crypto.h>
70 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
71 # error "DATA_ORDER must be defined!"
75 # error "HASH_CBLOCK must be defined!"
78 # error "HASH_LONG must be defined!"
81 # error "HASH_CTX must be defined!"
85 # error "HASH_UPDATE must be defined!"
87 #ifndef HASH_TRANSFORM
88 # error "HASH_TRANSFORM must be defined!"
91 # error "HASH_FINAL must be defined!"
94 #ifndef HASH_BLOCK_DATA_ORDER
95 # error "HASH_BLOCK_DATA_ORDER must be defined!"
99 * Engage compiler specific rotate intrinsic function if available.
103 # if defined(_MSC_VER)
104 # define ROTATE(a,n) _lrotl(a,n)
105 # elif defined(__ICC)
106 # define ROTATE(a,n) _rotl(a,n)
107 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
109 * Some GNU C inline assembler templates. Note that these are
110 * rotates by *constant* number of bits! But that's exactly
111 * what we need here...
112 * <appro@fy.chalmers.se>
114 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
115 # define ROTATE(a,n) ({ register unsigned int ret; \
119 : "I"(n), "0"((unsigned int)(a)) \
123 # elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
124 defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
125 # define ROTATE(a,n) ({ register unsigned int ret; \
127 "rlwinm %0,%1,%2,0,31" \
132 # elif defined(__s390x__)
133 # define ROTATE(a,n) ({ register unsigned int ret; \
134 asm ("rll %0,%1,%2" \
141 #endif /* PEDANTIC */
144 # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
147 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
150 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
151 # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
152 (defined(__x86_64) || defined(__x86_64__))
153 # if !defined(B_ENDIAN)
155 * This gives ~30-40% performance improvement in SHA-256 compiled
156 * with gcc [on P4]. Well, first macro to be frank. We can pull
157 * this trick on x86* platforms only, because these CPUs can fetch
158 * unaligned data without raising an exception.
160 # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
161 asm ("bswapl %0":"=r"(r):"0"(r)); \
163 # define HOST_l2c(l,c) ({ unsigned int r=(l); \
164 asm ("bswapl %0":"=r"(r):"0"(r)); \
165 *((unsigned int *)(c))=r; (c)+=4; r; })
167 # elif defined(__aarch64__)
168 # if defined(__BYTE_ORDER__)
169 # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
170 # define HOST_c2l(c,l) ({ unsigned int r; \
173 :"r"(*((const unsigned int *)(c))));\
175 # define HOST_l2c(l,c) ({ unsigned int r; \
178 :"r"((unsigned int)(l)));\
179 *((unsigned int *)(c))=r; (c)+=4; r; })
180 # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
181 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
182 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
187 # if defined(__s390__) || defined(__s390x__)
188 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
189 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
194 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
195 l|=(((unsigned long)(*((c)++)))<<16), \
196 l|=(((unsigned long)(*((c)++)))<< 8), \
197 l|=(((unsigned long)(*((c)++))) ) )
200 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
201 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
202 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
203 *((c)++)=(unsigned char)(((l) )&0xff), \
207 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
210 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
211 # if defined(__s390x__)
212 # define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \
213 :"=d"(l) :"m"(*(const unsigned int *)(c)));\
215 # define HOST_l2c(l,c) ({ asm ("strv %1,%0" \
216 :"=m"(*(unsigned int *)(c)) :"d"(l));\
220 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
222 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
223 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
224 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
230 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
231 l|=(((unsigned long)(*((c)++)))<< 8), \
232 l|=(((unsigned long)(*((c)++)))<<16), \
233 l|=(((unsigned long)(*((c)++)))<<24) )
236 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
237 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
238 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
239 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
246 * Time for some action:-)
249 int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
251 const unsigned char *data = data_;
259 l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
261 * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai
262 * <weidai@eskimo.com> for pointing it out.
264 if (l < c->Nl) /* overflow */
266 c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
272 p = (unsigned char *)c->data;
274 if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
275 memcpy(p + n, data, HASH_CBLOCK - n);
276 HASH_BLOCK_DATA_ORDER(c, p, 1);
282 * We use memset rather than OPENSSL_cleanse() here deliberately.
283 * Using OPENSSL_cleanse() here could be a performance issue. It
284 * will get properly cleansed on finalisation so this isn't a
287 memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
289 memcpy(p + n, data, len);
290 c->num += (unsigned int)len;
295 n = len / HASH_CBLOCK;
297 HASH_BLOCK_DATA_ORDER(c, data, n);
304 p = (unsigned char *)c->data;
305 c->num = (unsigned int)len;
306 memcpy(p, data, len);
311 void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
313 HASH_BLOCK_DATA_ORDER(c, data, 1);
316 int HASH_FINAL(unsigned char *md, HASH_CTX *c)
318 unsigned char *p = (unsigned char *)c->data;
321 p[n] = 0x80; /* there is always room for one */
324 if (n > (HASH_CBLOCK - 8)) {
325 memset(p + n, 0, HASH_CBLOCK - n);
327 HASH_BLOCK_DATA_ORDER(c, p, 1);
329 memset(p + n, 0, HASH_CBLOCK - 8 - n);
331 p += HASH_CBLOCK - 8;
332 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
333 (void)HOST_l2c(c->Nh, p);
334 (void)HOST_l2c(c->Nl, p);
335 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
336 (void)HOST_l2c(c->Nl, p);
337 (void)HOST_l2c(c->Nh, p);
340 HASH_BLOCK_DATA_ORDER(c, p, 1);
342 OPENSSL_cleanse(p, HASH_CBLOCK);
344 #ifndef HASH_MAKE_STRING
345 # error "HASH_MAKE_STRING must be defined!"
347 HASH_MAKE_STRING(c, md);
354 # if defined(__alpha) || defined(__sparcv9) || defined(__mips)
355 # define MD32_REG_T long
357 * This comment was originally written for MD5, which is why it
358 * discusses A-D. But it basically applies to all 32-bit digests,
359 * which is why it was moved to common header file.
361 * In case you wonder why A-D are declared as long and not
362 * as MD5_LONG. Doing so results in slight performance
363 * boost on LP64 architectures. The catch is we don't
364 * really care if 32 MSBs of a 64-bit register get polluted
365 * with eventual overflows as we *save* only 32 LSBs in
366 * *either* case. Now declaring 'em long excuses the compiler
367 * from keeping 32 MSBs zeroed resulting in 13% performance
368 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
369 * Well, to be honest it should say that this *prevents*
370 * performance degradation.
371 * <appro@fy.chalmers.se>
375 * Above is not absolute and there are LP64 compilers that
376 * generate better code if MD32_REG_T is defined int. The above
377 * pre-processor condition reflects the circumstances under which
378 * the conclusion was made and is subject to further extension.
379 * <appro@fy.chalmers.se>
381 # define MD32_REG_T int