1 /* @(#) $Id: ./src/os_crypto/sha1/md32_common.h, 2011/09/08 dcid Exp $
3 /* Included on ossec */
5 /* crypto/md32_common.h */
6 /* ====================================================================
7 * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 * This is a generic 32 bit "collector" for message digest algorithms.
62 * Whenever needed it collects input character stream into chunks of
63 * 32 bit values and invokes a block function that performs actual hash
70 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
71 * this macro defines byte order of input stream.
73 * size of a unit chunk HASH_BLOCK operates on.
75 * has to be at lest 32 bit wide, if it's wider, then
76 * HASH_LONG_LOG2 *has to* be defined along
78 * context structure that at least contains following
83 * HASH_LONG data[HASH_LBLOCK];
88 * name of "Update" function, implemented here.
90 * name of "Transform" function, implemented here.
92 * name of "Final" function, implemented here.
93 * HASH_BLOCK_HOST_ORDER
94 * name of "block" function treating *aligned* input message
95 * in host byte order, implemented externally.
96 * HASH_BLOCK_DATA_ORDER
97 * name of "block" function treating *unaligned* input message
98 * in original (data) byte order, implemented externally (it
99 * actually is optional if data and host are of the same
102 * macro convering context variables to an ASCII hash string.
106 * B_ENDIAN or L_ENDIAN
107 * defines host byte-order.
109 * defaults to 2 if not states otherwise.
111 * assumed to be HASH_CBLOCK/4 if not stated otherwise.
112 * HASH_BLOCK_DATA_ORDER_ALIGNED
113 * alternative "block" function capable of treating
114 * aligned input message in original (data) order,
115 * implemented externally.
119 * #define DATA_ORDER_IS_LITTLE_ENDIAN
121 * #define HASH_LONG MD5_LONG
122 * #define HASH_LONG_LOG2 MD5_LONG_LOG2
123 * #define HASH_CTX MD5_CTX
124 * #define HASH_CBLOCK MD5_CBLOCK
125 * #define HASH_LBLOCK MD5_LBLOCK
126 * #define HASH_UPDATE MD5_Update
127 * #define HASH_TRANSFORM MD5_Transform
128 * #define HASH_FINAL MD5_Final
129 * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
130 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
132 * <appro@fy.chalmers.se>
136 #ifndef _MD32_COMMON__H
137 #define _MD32_COMMON__H
140 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
141 #error "DATA_ORDER must be defined!"
145 #error "HASH_CBLOCK must be defined!"
148 #error "HASH_LONG must be defined!"
151 #error "HASH_CTX must be defined!"
155 #error "HASH_UPDATE must be defined!"
157 #ifndef HASH_TRANSFORM
158 #error "HASH_TRANSFORM must be defined!"
161 #error "HASH_FINAL must be defined!"
164 #ifndef HASH_BLOCK_HOST_ORDER
165 #error "HASH_BLOCK_HOST_ORDER must be defined!"
170 * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
173 #ifndef HASH_BLOCK_DATA_ORDER
174 #error "HASH_BLOCK_DATA_ORDER must be defined!"
179 #define HASH_LBLOCK (HASH_CBLOCK/4)
182 #ifndef HASH_LONG_LOG2
183 #define HASH_LONG_LOG2 2
187 * Engage compiler specific rotate intrinsic function if available.
191 # if defined(_MSC_VER) || defined(__ICC)
192 # define ROTATE(a,n) _lrotl(a,n)
193 # elif defined(__MWERKS__)
194 # if defined(__POWERPC__)
195 # define ROTATE(a,n) __rlwinm(a,n,0,31)
196 # elif defined(__MC68K__)
197 /* Motorola specific tweak. <appro@fy.chalmers.se> */
198 # define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
200 # define ROTATE(a,n) __rol(a,n)
202 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
204 * Some GNU C inline assembler templates. Note that these are
205 * rotates by *constant* number of bits! But that's exactly
206 * what we need here...
207 * <appro@fy.chalmers.se>
209 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
210 # define ROTATE(a,n) ({ register unsigned int ret; \
218 # elif defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
219 # define ROTATE(a,n) ({ register unsigned int ret; \
221 "rlwinm %0,%1,%2,0,31" \
228 #endif /* PEDANTIC */
230 #if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
231 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
233 /* 5 instructions with rotate instruction, else 9 */
234 #define REVERSE_FETCH32(a,l) ( \
235 l=*(const HASH_LONG *)(a), \
236 ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
239 /* 6 instructions with rotate instruction, else 8 */
240 #define REVERSE_FETCH32(a,l) ( \
241 l=*(const HASH_LONG *)(a), \
242 l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
246 * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
247 * It's rewritten as above for two reasons:
248 * - RISCs aren't good at long constants and have to explicitely
249 * compose 'em with several (well, usually 2) instructions in a
250 * register before performing the actual operation and (as you
251 * already realized:-) having same constant should inspire the
252 * compiler to permanently allocate the only register for it;
253 * - most modern CPUs have two ALUs, but usually only one has
254 * circuitry for shifts:-( this minor tweak inspires compiler
255 * to schedule shift instructions in a better way...
257 * <appro@fy.chalmers.se>
263 #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
267 * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
268 * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
269 * and host are of the same "endianess". It's possible to mask
270 * this with blank #define HASH_BLOCK_DATA_ORDER though...
272 * <appro@fy.chalmers.se>
274 #if defined(B_ENDIAN)
275 # if defined(DATA_ORDER_IS_BIG_ENDIAN)
276 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
277 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
280 #elif defined(L_ENDIAN)
281 # if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
282 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
283 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
288 #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
289 #ifndef HASH_BLOCK_DATA_ORDER
290 #error "HASH_BLOCK_DATA_ORDER must be defined!"
294 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
297 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
298 # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
299 (defined(__x86_64) || defined(__x86_64__))
301 * This gives ~30-40% performance improvement in SHA-256 compiled
302 * with gcc [on P4]. Well, first macro to be frank. We can pull
303 * this trick on x86* platforms only, because these CPUs can fetch
304 * unaligned data without raising an exception.
306 # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
307 asm ("bswapl %0":"=r"(r):"0"(r)); \
309 # define HOST_l2c(l,c) ({ unsigned int r=(l); \
310 asm ("bswapl %0":"=r"(r):"0"(r)); \
311 *((unsigned int *)(c))=r; (c)+=4; r; })
317 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
318 l|=(((unsigned long)(*((c)++)))<<16), \
319 l|=(((unsigned long)(*((c)++)))<< 8), \
320 l|=(((unsigned long)(*((c)++))) ), \
323 #define HOST_p_c2l(c,l,n) { \
325 case 0: l =((unsigned long)(*((c)++)))<<24; \
326 case 1: l|=((unsigned long)(*((c)++)))<<16; \
327 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
328 case 3: l|=((unsigned long)(*((c)++))); \
330 #define HOST_p_c2l_p(c,l,sc,len) { \
332 case 0: l =((unsigned long)(*((c)++)))<<24; \
333 if (--len == 0) break; \
334 case 1: l|=((unsigned long)(*((c)++)))<<16; \
335 if (--len == 0) break; \
336 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
338 /* NOTE the pointer is not incremented at the end of this */
339 #define HOST_c2l_p(c,l,n) { \
342 case 3: l =((unsigned long)(*(--(c))))<< 8; \
343 case 2: l|=((unsigned long)(*(--(c))))<<16; \
344 case 1: l|=((unsigned long)(*(--(c))))<<24; \
347 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
348 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
349 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
350 *((c)++)=(unsigned char)(((l) )&0xff), \
354 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
356 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
358 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
359 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
360 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
365 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
366 l|=(((unsigned long)(*((c)++)))<< 8), \
367 l|=(((unsigned long)(*((c)++)))<<16), \
368 l|=(((unsigned long)(*((c)++)))<<24), \
371 #define HOST_p_c2l(c,l,n) { \
373 case 0: l =((unsigned long)(*((c)++))); \
374 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
375 case 2: l|=((unsigned long)(*((c)++)))<<16; \
376 case 3: l|=((unsigned long)(*((c)++)))<<24; \
378 #define HOST_p_c2l_p(c,l,sc,len) { \
380 case 0: l =((unsigned long)(*((c)++))); \
381 if (--len == 0) break; \
382 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
383 if (--len == 0) break; \
384 case 2: l|=((unsigned long)(*((c)++)))<<16; \
386 /* NOTE the pointer is not incremented at the end of this */
387 #define HOST_c2l_p(c,l,n) { \
390 case 3: l =((unsigned long)(*(--(c))))<<16; \
391 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
392 case 1: l|=((unsigned long)(*(--(c)))); \
395 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
396 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
397 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
398 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
405 * Time for some action:-)
408 int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
410 const unsigned char *data=data_;
411 register HASH_LONG * p;
412 register HASH_LONG l;
415 if (len==0) return 1;
417 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
418 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
419 * Wei Dai <weidai@eskimo.com> for pointing it out. */
420 if (l < c->Nl) /* overflow */
422 c->Nh+=(len>>29); /* might cause compiler warning on 16-bit */
431 if ((c->num+len) >= HASH_CBLOCK)
433 l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
434 for (; sw<HASH_LBLOCK; sw++)
436 HOST_c2l(data,l); p[sw]=l;
438 HASH_BLOCK_HOST_ORDER (c,p,1);
439 len-=(HASH_CBLOCK-c->num);
441 /* drop through and do the rest */
445 c->num+=(unsigned int)len;
446 if ((sc+len) < 4) /* ugly, add char's to a word */
448 l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
456 HOST_p_c2l(data,l,sc);
458 for (; sw < ew; sw++)
460 HOST_c2l(data,l); p[sw]=l;
464 HOST_c2l_p(data,l,ec); p[sw]=l;
474 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
476 * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
477 * only if sizeof(HASH_LONG)==4.
479 if ((((size_t)data)%4) == 0)
481 /* data is properly aligned so that we can cast it: */
482 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,sw);
488 #if !defined(HASH_BLOCK_DATA_ORDER)
491 memcpy (p=c->data,data,HASH_CBLOCK);
492 HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
498 #if defined(HASH_BLOCK_DATA_ORDER)
500 HASH_BLOCK_DATA_ORDER(c,data,sw);
512 ew=len>>2; /* words to copy */
516 HOST_c2l(data,l); *p=l;
518 HOST_c2l_p(data,l,ec);
525 void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
527 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
528 if ((((size_t)data)%4) == 0)
529 /* data is properly aligned so that we can cast it: */
530 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,1);
532 #if !defined(HASH_BLOCK_DATA_ORDER)
534 memcpy (c->data,data,HASH_CBLOCK);
535 HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
539 #if defined(HASH_BLOCK_DATA_ORDER)
540 HASH_BLOCK_DATA_ORDER (c,data,1);
545 int HASH_FINAL (unsigned char *md, HASH_CTX *c)
547 register HASH_LONG *p;
548 register unsigned long l;
550 static const unsigned char end[4]={0x80,0x00,0x00,0x00};
551 const unsigned char *cp=end;
553 /* c->num should definitly have room for at least one more byte. */
559 /* purify often complains about the following line as an
560 * Uninitialized Memory Read. While this can be true, the
561 * following p_c2l macro will reset l when that case is true.
562 * This is because j&0x03 contains the number of 'valid' bytes
563 * already in p[i]. If and only if j&0x03 == 0, the UMR will
564 * occur but this is also the only time p_c2l will do
565 * l= *(cp++) instead of l|= *(cp++)
566 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
569 if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
573 l = (j==0) ? 0 : p[i];
575 HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
577 if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
579 if (i<HASH_LBLOCK) p[i]=0;
580 HASH_BLOCK_HOST_ORDER (c,p,1);
583 for (; i<(HASH_LBLOCK-2); i++)
586 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
587 p[HASH_LBLOCK-2]=c->Nh;
588 p[HASH_LBLOCK-1]=c->Nl;
589 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
590 p[HASH_LBLOCK-2]=c->Nl;
591 p[HASH_LBLOCK-1]=c->Nh;
593 HASH_BLOCK_HOST_ORDER (c,p,1);
595 #ifndef HASH_MAKE_STRING
596 #error "HASH_MAKE_STRING must be defined!"
598 HASH_MAKE_STRING(c,md);
602 /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
603 * but I'm not worried :-)
604 OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
610 #define MD32_REG_T int
612 * This comment was originaly written for MD5, which is why it
613 * discusses A-D. But it basically applies to all 32-bit digests,
614 * which is why it was moved to common header file.
616 * In case you wonder why A-D are declared as long and not
617 * as MD5_LONG. Doing so results in slight performance
618 * boost on LP64 architectures. The catch is we don't
619 * really care if 32 MSBs of a 64-bit register get polluted
620 * with eventual overflows as we *save* only 32 LSBs in
621 * *either* case. Now declaring 'em long excuses the compiler
622 * from keeping 32 MSBs zeroed resulting in 13% performance
623 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
624 * Well, to be honest it should say that this *prevents*
625 * performance degradation.
626 * <appro@fy.chalmers.se>
627 * Apparently there're LP64 compilers that generate better
628 * code if A-D are declared int. Most notably GCC-x86_64
629 * generates better code.
630 * <appro@fy.chalmers.se>
635 #endif /* _MD32_COMMON__H */