1 /* crypto/md32_common.h */
2 /* ====================================================================
3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
53 * This is a generic 32 bit "collector" for message digest algorithms.
54 * Whenever needed it collects input character stream into chunks of
55 * 32 bit values and invokes a block function that performs actual hash
62 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
63 * this macro defines byte order of input stream.
65 * size of a unit chunk HASH_BLOCK operates on.
67 * has to be at lest 32 bit wide, if it's wider, then
68 * HASH_LONG_LOG2 *has to* be defined along
70 * context structure that at least contains following
76 * HASH_LONG data[HASH_LBLOCK];
77 * unsigned char data[HASH_CBLOCK];
82 * data[] vector is expected to be zeroed upon first call to
85 * name of "Update" function, implemented here.
87 * name of "Transform" function, implemented here.
89 * name of "Final" function, implemented here.
90 * HASH_BLOCK_DATA_ORDER
91 * name of "block" function capable of treating *unaligned* input
92 * message in original (data) byte order, implemented externally.
94 * macro convering context variables to an ASCII hash string.
98 * #define DATA_ORDER_IS_LITTLE_ENDIAN
100 * #define HASH_LONG MD5_LONG
101 * #define HASH_LONG_LOG2 MD5_LONG_LOG2
102 * #define HASH_CTX MD5_CTX
103 * #define HASH_CBLOCK MD5_CBLOCK
104 * #define HASH_UPDATE MD5_Update
105 * #define HASH_TRANSFORM MD5_Transform
106 * #define HASH_FINAL MD5_Final
107 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
109 * <appro@fy.chalmers.se>
112 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
113 # error "DATA_ORDER must be defined!"
117 # error "HASH_CBLOCK must be defined!"
120 # error "HASH_LONG must be defined!"
123 # error "HASH_CTX must be defined!"
127 # error "HASH_UPDATE must be defined!"
129 #ifndef HASH_TRANSFORM
130 # error "HASH_TRANSFORM must be defined!"
133 # error "HASH_FINAL must be defined!"
136 #ifndef HASH_BLOCK_DATA_ORDER
137 # error "HASH_BLOCK_DATA_ORDER must be defined!"
141 * Engage compiler specific rotate intrinsic function if available.
145 # if defined(_MSC_VER)
146 # define ROTATE(a,n) _lrotl(a,n)
147 # elif defined(__ICC)
148 # define ROTATE(a,n) _rotl(a,n)
149 # elif defined(__MWERKS__)
150 # if defined(__POWERPC__)
151 # define ROTATE(a,n) __rlwinm(a,n,0,31)
152 # elif defined(__MC68K__)
153 /* Motorola specific tweak. <appro@fy.chalmers.se> */
154 # define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
156 # define ROTATE(a,n) __rol(a,n)
158 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
160 * Some GNU C inline assembler templates. Note that these are
161 * rotates by *constant* number of bits! But that's exactly
162 * what we need here...
163 * <appro@fy.chalmers.se>
165 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
166 # define ROTATE(a,n) ({ register unsigned int ret; \
170 : "I"(n), "0"((unsigned int)(a)) \
174 # elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
175 defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
176 # define ROTATE(a,n) ({ register unsigned int ret; \
178 "rlwinm %0,%1,%2,0,31" \
183 # elif defined(__s390x__)
184 # define ROTATE(a,n) ({ register unsigned int ret; \
185 asm ("rll %0,%1,%2" \
192 #endif /* PEDANTIC */
195 # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
198 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
201 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
202 # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
203 (defined(__x86_64) || defined(__x86_64__))
204 # if !defined(B_ENDIAN)
206 * This gives ~30-40% performance improvement in SHA-256 compiled
207 * with gcc [on P4]. Well, first macro to be frank. We can pull
208 * this trick on x86* platforms only, because these CPUs can fetch
209 * unaligned data without raising an exception.
211 # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
212 asm ("bswapl %0":"=r"(r):"0"(r)); \
214 # define HOST_l2c(l,c) ({ unsigned int r=(l); \
215 asm ("bswapl %0":"=r"(r):"0"(r)); \
216 *((unsigned int *)(c))=r; (c)+=4; r; })
218 # elif defined(__aarch64__)
219 # if defined(__BYTE_ORDER__)
220 # if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
221 # define HOST_c2l(c,l) ({ unsigned int r; \
224 :"r"(*((const unsigned int *)(c))));\
226 # define HOST_l2c(l,c) ({ unsigned int r; \
229 :"r"((unsigned int)(l)));\
230 *((unsigned int *)(c))=r; (c)+=4; r; })
231 # elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
232 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
233 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
238 # if defined(__s390__) || defined(__s390x__)
239 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
240 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
245 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
246 l|=(((unsigned long)(*((c)++)))<<16), \
247 l|=(((unsigned long)(*((c)++)))<< 8), \
248 l|=(((unsigned long)(*((c)++))) ) )
251 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
252 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
253 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
254 *((c)++)=(unsigned char)(((l) )&0xff), \
258 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
261 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
262 # if defined(__s390x__)
263 # define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \
264 :"=d"(l) :"m"(*(const unsigned int *)(c)));\
266 # define HOST_l2c(l,c) ({ asm ("strv %1,%0" \
267 :"=m"(*(unsigned int *)(c)) :"d"(l));\
271 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
273 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
274 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
275 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
281 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
282 l|=(((unsigned long)(*((c)++)))<< 8), \
283 l|=(((unsigned long)(*((c)++)))<<16), \
284 l|=(((unsigned long)(*((c)++)))<<24) )
287 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
288 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
289 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
290 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
297 * Time for some action:-)
300 int HASH_UPDATE(HASH_CTX
*c
, const void *data_
, size_t len
)
302 const unsigned char *data
= data_
;
310 l
= (c
->Nl
+ (((HASH_LONG
) len
) << 3)) & 0xffffffffUL
;
312 * 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei Dai
313 * <weidai@eskimo.com> for pointing it out.
315 if (l
< c
->Nl
) /* overflow */
317 c
->Nh
+= (HASH_LONG
) (len
>> 29); /* might cause compiler warning on
323 p
= (unsigned char *)c
->data
;
325 if (len
>= HASH_CBLOCK
|| len
+ n
>= HASH_CBLOCK
) {
326 memcpy(p
+ n
, data
, HASH_CBLOCK
- n
);
327 HASH_BLOCK_DATA_ORDER(c
, p
, 1);
332 memset(p
, 0, HASH_CBLOCK
); /* keep it zeroed */
334 memcpy(p
+ n
, data
, len
);
335 c
->num
+= (unsigned int)len
;
340 n
= len
/ HASH_CBLOCK
;
342 HASH_BLOCK_DATA_ORDER(c
, data
, n
);
349 p
= (unsigned char *)c
->data
;
350 c
->num
= (unsigned int)len
;
351 memcpy(p
, data
, len
);
356 void HASH_TRANSFORM(HASH_CTX
*c
, const unsigned char *data
)
358 HASH_BLOCK_DATA_ORDER(c
, data
, 1);
361 int HASH_FINAL(unsigned char *md
, HASH_CTX
*c
)
363 unsigned char *p
= (unsigned char *)c
->data
;
366 p
[n
] = 0x80; /* there is always room for one */
369 if (n
> (HASH_CBLOCK
- 8)) {
370 memset(p
+ n
, 0, HASH_CBLOCK
- n
);
372 HASH_BLOCK_DATA_ORDER(c
, p
, 1);
374 memset(p
+ n
, 0, HASH_CBLOCK
- 8 - n
);
376 p
+= HASH_CBLOCK
- 8;
377 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
378 (void)HOST_l2c(c
->Nh
, p
);
379 (void)HOST_l2c(c
->Nl
, p
);
380 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
381 (void)HOST_l2c(c
->Nl
, p
);
382 (void)HOST_l2c(c
->Nh
, p
);
385 HASH_BLOCK_DATA_ORDER(c
, p
, 1);
387 memset(p
, 0, HASH_CBLOCK
);
389 #ifndef HASH_MAKE_STRING
390 # error "HASH_MAKE_STRING must be defined!"
392 HASH_MAKE_STRING(c
, md
);
399 # if defined(__alpha) || defined(__sparcv9) || defined(__mips)
400 # define MD32_REG_T long
402 * This comment was originaly written for MD5, which is why it
403 * discusses A-D. But it basically applies to all 32-bit digests,
404 * which is why it was moved to common header file.
406 * In case you wonder why A-D are declared as long and not
407 * as MD5_LONG. Doing so results in slight performance
408 * boost on LP64 architectures. The catch is we don't
409 * really care if 32 MSBs of a 64-bit register get polluted
410 * with eventual overflows as we *save* only 32 LSBs in
411 * *either* case. Now declaring 'em long excuses the compiler
412 * from keeping 32 MSBs zeroed resulting in 13% performance
413 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
414 * Well, to be honest it should say that this *prevents*
415 * performance degradation.
416 * <appro@fy.chalmers.se>
420 * Above is not absolute and there are LP64 compilers that
421 * generate better code if MD32_REG_T is defined int. The above
422 * pre-processor condition reflects the circumstances under which
423 * the conclusion was made and is subject to further extension.
424 * <appro@fy.chalmers.se>
426 # define MD32_REG_T int