10Sstevel@tonic-gate /* crypto/md32_common.h */
20Sstevel@tonic-gate /* ====================================================================
30Sstevel@tonic-gate * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate * are met:
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
140Sstevel@tonic-gate * the documentation and/or other materials provided with the
150Sstevel@tonic-gate * distribution.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
180Sstevel@tonic-gate * software must display the following acknowledgment:
190Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
200Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
210Sstevel@tonic-gate *
220Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
230Sstevel@tonic-gate * endorse or promote products derived from this software without
240Sstevel@tonic-gate * prior written permission. For written permission, please contact
250Sstevel@tonic-gate * licensing@OpenSSL.org.
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
280Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
290Sstevel@tonic-gate * permission of the OpenSSL Project.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
320Sstevel@tonic-gate * acknowledgment:
330Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
340Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
370Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
380Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
390Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
400Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
410Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
420Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
430Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
440Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
450Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
460Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
470Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
480Sstevel@tonic-gate * ====================================================================
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
510Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
520Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
530Sstevel@tonic-gate *
540Sstevel@tonic-gate */
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * This is a generic 32 bit "collector" for message digest algorithms.
580Sstevel@tonic-gate * Whenever needed it collects input character stream into chunks of
590Sstevel@tonic-gate * 32 bit values and invokes a block function that performs actual hash
600Sstevel@tonic-gate * calculations.
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * Porting guide.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * Obligatory macros:
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
670Sstevel@tonic-gate * this macro defines byte order of input stream.
680Sstevel@tonic-gate * HASH_CBLOCK
690Sstevel@tonic-gate * size of a unit chunk HASH_BLOCK operates on.
700Sstevel@tonic-gate * HASH_LONG
710Sstevel@tonic-gate * has to be at lest 32 bit wide, if it's wider, then
720Sstevel@tonic-gate * HASH_LONG_LOG2 *has to* be defined along
730Sstevel@tonic-gate * HASH_CTX
740Sstevel@tonic-gate * context structure that at least contains following
750Sstevel@tonic-gate * members:
760Sstevel@tonic-gate * typedef struct {
770Sstevel@tonic-gate * ...
780Sstevel@tonic-gate * HASH_LONG Nl,Nh;
790Sstevel@tonic-gate * HASH_LONG data[HASH_LBLOCK];
80*2139Sjp161948 * unsigned int num;
810Sstevel@tonic-gate * ...
820Sstevel@tonic-gate * } HASH_CTX;
830Sstevel@tonic-gate * HASH_UPDATE
840Sstevel@tonic-gate * name of "Update" function, implemented here.
850Sstevel@tonic-gate * HASH_TRANSFORM
860Sstevel@tonic-gate * name of "Transform" function, implemented here.
870Sstevel@tonic-gate * HASH_FINAL
880Sstevel@tonic-gate * name of "Final" function, implemented here.
890Sstevel@tonic-gate * HASH_BLOCK_HOST_ORDER
900Sstevel@tonic-gate * name of "block" function treating *aligned* input message
910Sstevel@tonic-gate * in host byte order, implemented externally.
920Sstevel@tonic-gate * HASH_BLOCK_DATA_ORDER
930Sstevel@tonic-gate * name of "block" function treating *unaligned* input message
940Sstevel@tonic-gate * in original (data) byte order, implemented externally (it
950Sstevel@tonic-gate * actually is optional if data and host are of the same
960Sstevel@tonic-gate * "endianess").
970Sstevel@tonic-gate * HASH_MAKE_STRING
980Sstevel@tonic-gate * macro convering context variables to an ASCII hash string.
990Sstevel@tonic-gate *
1000Sstevel@tonic-gate * Optional macros:
1010Sstevel@tonic-gate *
1020Sstevel@tonic-gate * B_ENDIAN or L_ENDIAN
1030Sstevel@tonic-gate * defines host byte-order.
1040Sstevel@tonic-gate * HASH_LONG_LOG2
1050Sstevel@tonic-gate * defaults to 2 if not states otherwise.
1060Sstevel@tonic-gate * HASH_LBLOCK
1070Sstevel@tonic-gate * assumed to be HASH_CBLOCK/4 if not stated otherwise.
1080Sstevel@tonic-gate * HASH_BLOCK_DATA_ORDER_ALIGNED
1090Sstevel@tonic-gate * alternative "block" function capable of treating
1100Sstevel@tonic-gate * aligned input message in original (data) order,
1110Sstevel@tonic-gate * implemented externally.
1120Sstevel@tonic-gate *
1130Sstevel@tonic-gate * MD5 example:
1140Sstevel@tonic-gate *
1150Sstevel@tonic-gate * #define DATA_ORDER_IS_LITTLE_ENDIAN
1160Sstevel@tonic-gate *
1170Sstevel@tonic-gate * #define HASH_LONG MD5_LONG
1180Sstevel@tonic-gate * #define HASH_LONG_LOG2 MD5_LONG_LOG2
1190Sstevel@tonic-gate * #define HASH_CTX MD5_CTX
1200Sstevel@tonic-gate * #define HASH_CBLOCK MD5_CBLOCK
1210Sstevel@tonic-gate * #define HASH_LBLOCK MD5_LBLOCK
1220Sstevel@tonic-gate * #define HASH_UPDATE MD5_Update
1230Sstevel@tonic-gate * #define HASH_TRANSFORM MD5_Transform
1240Sstevel@tonic-gate * #define HASH_FINAL MD5_Final
1250Sstevel@tonic-gate * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
1260Sstevel@tonic-gate * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
1270Sstevel@tonic-gate *
1280Sstevel@tonic-gate * <appro@fy.chalmers.se>
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
1320Sstevel@tonic-gate #error "DATA_ORDER must be defined!"
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate #ifndef HASH_CBLOCK
1360Sstevel@tonic-gate #error "HASH_CBLOCK must be defined!"
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate #ifndef HASH_LONG
1390Sstevel@tonic-gate #error "HASH_LONG must be defined!"
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate #ifndef HASH_CTX
1420Sstevel@tonic-gate #error "HASH_CTX must be defined!"
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate #ifndef HASH_UPDATE
1460Sstevel@tonic-gate #error "HASH_UPDATE must be defined!"
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate #ifndef HASH_TRANSFORM
1490Sstevel@tonic-gate #error "HASH_TRANSFORM must be defined!"
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate #ifndef HASH_FINAL
1520Sstevel@tonic-gate #error "HASH_FINAL must be defined!"
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate #ifndef HASH_BLOCK_HOST_ORDER
1560Sstevel@tonic-gate #error "HASH_BLOCK_HOST_ORDER must be defined!"
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate #if 0
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
1620Sstevel@tonic-gate * isn't defined.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate #ifndef HASH_BLOCK_DATA_ORDER
1650Sstevel@tonic-gate #error "HASH_BLOCK_DATA_ORDER must be defined!"
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate #ifndef HASH_LBLOCK
1700Sstevel@tonic-gate #define HASH_LBLOCK (HASH_CBLOCK/4)
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate #ifndef HASH_LONG_LOG2
1740Sstevel@tonic-gate #define HASH_LONG_LOG2 2
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate * Engage compiler specific rotate intrinsic function if available.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate #undef ROTATE
1810Sstevel@tonic-gate #ifndef PEDANTIC
182*2139Sjp161948 # if defined(_MSC_VER) || defined(__ICC)
1830Sstevel@tonic-gate # define ROTATE(a,n) _lrotl(a,n)
1840Sstevel@tonic-gate # elif defined(__MWERKS__)
1850Sstevel@tonic-gate # if defined(__POWERPC__)
1860Sstevel@tonic-gate # define ROTATE(a,n) __rlwinm(a,n,0,31)
1870Sstevel@tonic-gate # elif defined(__MC68K__)
1880Sstevel@tonic-gate /* Motorola specific tweak. <appro@fy.chalmers.se> */
1890Sstevel@tonic-gate # define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
1900Sstevel@tonic-gate # else
1910Sstevel@tonic-gate # define ROTATE(a,n) __rol(a,n)
1920Sstevel@tonic-gate # endif
1930Sstevel@tonic-gate # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate * Some GNU C inline assembler templates. Note that these are
1960Sstevel@tonic-gate * rotates by *constant* number of bits! But that's exactly
1970Sstevel@tonic-gate * what we need here...
1980Sstevel@tonic-gate * <appro@fy.chalmers.se>
1990Sstevel@tonic-gate */
2000Sstevel@tonic-gate # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
2010Sstevel@tonic-gate # define ROTATE(a,n) ({ register unsigned int ret; \
202*2139Sjp161948 __asm__ ( \
2030Sstevel@tonic-gate "roll %1,%0" \
2040Sstevel@tonic-gate : "=r"(ret) \
2050Sstevel@tonic-gate : "I"(n), "0"(a) \
2060Sstevel@tonic-gate : "cc"); \
2070Sstevel@tonic-gate ret; \
2080Sstevel@tonic-gate })
209*2139Sjp161948 # elif defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
2100Sstevel@tonic-gate # define ROTATE(a,n) ({ register unsigned int ret; \
211*2139Sjp161948 __asm__ ( \
2120Sstevel@tonic-gate "rlwinm %0,%1,%2,0,31" \
2130Sstevel@tonic-gate : "=r"(ret) \
2140Sstevel@tonic-gate : "r"(a), "I"(n)); \
2150Sstevel@tonic-gate ret; \
2160Sstevel@tonic-gate })
2170Sstevel@tonic-gate # endif
2180Sstevel@tonic-gate # endif
2190Sstevel@tonic-gate #endif /* PEDANTIC */
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate #if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
2220Sstevel@tonic-gate /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
2230Sstevel@tonic-gate #ifdef ROTATE
2240Sstevel@tonic-gate /* 5 instructions with rotate instruction, else 9 */
2250Sstevel@tonic-gate #define REVERSE_FETCH32(a,l) ( \
2260Sstevel@tonic-gate l=*(const HASH_LONG *)(a), \
2270Sstevel@tonic-gate ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
2280Sstevel@tonic-gate )
2290Sstevel@tonic-gate #else
2300Sstevel@tonic-gate /* 6 instructions with rotate instruction, else 8 */
2310Sstevel@tonic-gate #define REVERSE_FETCH32(a,l) ( \
2320Sstevel@tonic-gate l=*(const HASH_LONG *)(a), \
2330Sstevel@tonic-gate l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
2340Sstevel@tonic-gate ROTATE(l,16) \
2350Sstevel@tonic-gate )
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
2380Sstevel@tonic-gate * It's rewritten as above for two reasons:
2390Sstevel@tonic-gate * - RISCs aren't good at long constants and have to explicitely
2400Sstevel@tonic-gate * compose 'em with several (well, usually 2) instructions in a
2410Sstevel@tonic-gate * register before performing the actual operation and (as you
2420Sstevel@tonic-gate * already realized:-) having same constant should inspire the
2430Sstevel@tonic-gate * compiler to permanently allocate the only register for it;
2440Sstevel@tonic-gate * - most modern CPUs have two ALUs, but usually only one has
2450Sstevel@tonic-gate * circuitry for shifts:-( this minor tweak inspires compiler
2460Sstevel@tonic-gate * to schedule shift instructions in a better way...
2470Sstevel@tonic-gate *
2480Sstevel@tonic-gate * <appro@fy.chalmers.se>
2490Sstevel@tonic-gate */
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate #endif
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate #ifndef ROTATE
2540Sstevel@tonic-gate #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
2550Sstevel@tonic-gate #endif
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
2590Sstevel@tonic-gate * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
2600Sstevel@tonic-gate * and host are of the same "endianess". It's possible to mask
2610Sstevel@tonic-gate * this with blank #define HASH_BLOCK_DATA_ORDER though...
2620Sstevel@tonic-gate *
2630Sstevel@tonic-gate * <appro@fy.chalmers.se>
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate #if defined(B_ENDIAN)
2660Sstevel@tonic-gate # if defined(DATA_ORDER_IS_BIG_ENDIAN)
2670Sstevel@tonic-gate # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
2680Sstevel@tonic-gate # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
2690Sstevel@tonic-gate # endif
2700Sstevel@tonic-gate # endif
2710Sstevel@tonic-gate #elif defined(L_ENDIAN)
2720Sstevel@tonic-gate # if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
2730Sstevel@tonic-gate # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
2740Sstevel@tonic-gate # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
2750Sstevel@tonic-gate # endif
2760Sstevel@tonic-gate # endif
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
2800Sstevel@tonic-gate #ifndef HASH_BLOCK_DATA_ORDER
2810Sstevel@tonic-gate #error "HASH_BLOCK_DATA_ORDER must be defined!"
2820Sstevel@tonic-gate #endif
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate #if defined(DATA_ORDER_IS_BIG_ENDIAN)
2860Sstevel@tonic-gate
287*2139Sjp161948 #ifndef PEDANTIC
288*2139Sjp161948 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
289*2139Sjp161948 # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
290*2139Sjp161948 (defined(__x86_64) || defined(__x86_64__))
291*2139Sjp161948 /*
292*2139Sjp161948 * This gives ~30-40% performance improvement in SHA-256 compiled
293*2139Sjp161948 * with gcc [on P4]. Well, first macro to be frank. We can pull
294*2139Sjp161948 * this trick on x86* platforms only, because these CPUs can fetch
295*2139Sjp161948 * unaligned data without raising an exception.
296*2139Sjp161948 */
297*2139Sjp161948 # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
298*2139Sjp161948 __asm__ ("bswapl %0":"=r"(r):"0"(r)); \
299*2139Sjp161948 (c)+=4; (l)=r; })
300*2139Sjp161948 # define HOST_l2c(l,c) ({ unsigned int r=(l); \
301*2139Sjp161948 __asm__ ("bswapl %0":"=r"(r):"0"(r)); \
302*2139Sjp161948 *((unsigned int *)(c))=r; (c)+=4; r; })
303*2139Sjp161948 # endif
304*2139Sjp161948 # endif
305*2139Sjp161948 #endif
306*2139Sjp161948
307*2139Sjp161948 #ifndef HOST_c2l
3080Sstevel@tonic-gate #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
3090Sstevel@tonic-gate l|=(((unsigned long)(*((c)++)))<<16), \
3100Sstevel@tonic-gate l|=(((unsigned long)(*((c)++)))<< 8), \
3110Sstevel@tonic-gate l|=(((unsigned long)(*((c)++))) ), \
3120Sstevel@tonic-gate l)
313*2139Sjp161948 #endif
3140Sstevel@tonic-gate #define HOST_p_c2l(c,l,n) { \
3150Sstevel@tonic-gate switch (n) { \
3160Sstevel@tonic-gate case 0: l =((unsigned long)(*((c)++)))<<24; \
3170Sstevel@tonic-gate case 1: l|=((unsigned long)(*((c)++)))<<16; \
3180Sstevel@tonic-gate case 2: l|=((unsigned long)(*((c)++)))<< 8; \
3190Sstevel@tonic-gate case 3: l|=((unsigned long)(*((c)++))); \
3200Sstevel@tonic-gate } }
3210Sstevel@tonic-gate #define HOST_p_c2l_p(c,l,sc,len) { \
3220Sstevel@tonic-gate switch (sc) { \
3230Sstevel@tonic-gate case 0: l =((unsigned long)(*((c)++)))<<24; \
3240Sstevel@tonic-gate if (--len == 0) break; \
3250Sstevel@tonic-gate case 1: l|=((unsigned long)(*((c)++)))<<16; \
3260Sstevel@tonic-gate if (--len == 0) break; \
3270Sstevel@tonic-gate case 2: l|=((unsigned long)(*((c)++)))<< 8; \
3280Sstevel@tonic-gate } }
3290Sstevel@tonic-gate /* NOTE the pointer is not incremented at the end of this */
3300Sstevel@tonic-gate #define HOST_c2l_p(c,l,n) { \
3310Sstevel@tonic-gate l=0; (c)+=n; \
3320Sstevel@tonic-gate switch (n) { \
3330Sstevel@tonic-gate case 3: l =((unsigned long)(*(--(c))))<< 8; \
3340Sstevel@tonic-gate case 2: l|=((unsigned long)(*(--(c))))<<16; \
3350Sstevel@tonic-gate case 1: l|=((unsigned long)(*(--(c))))<<24; \
3360Sstevel@tonic-gate } }
337*2139Sjp161948 #ifndef HOST_l2c
3380Sstevel@tonic-gate #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
3390Sstevel@tonic-gate *((c)++)=(unsigned char)(((l)>>16)&0xff), \
3400Sstevel@tonic-gate *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
3410Sstevel@tonic-gate *((c)++)=(unsigned char)(((l) )&0xff), \
3420Sstevel@tonic-gate l)
343*2139Sjp161948 #endif
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
3460Sstevel@tonic-gate
347*2139Sjp161948 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
348*2139Sjp161948 # ifndef B_ENDIAN
349*2139Sjp161948 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
350*2139Sjp161948 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
351*2139Sjp161948 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
352*2139Sjp161948 # endif
353*2139Sjp161948 #endif
354*2139Sjp161948
355*2139Sjp161948 #ifndef HOST_c2l
3560Sstevel@tonic-gate #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
3570Sstevel@tonic-gate l|=(((unsigned long)(*((c)++)))<< 8), \
3580Sstevel@tonic-gate l|=(((unsigned long)(*((c)++)))<<16), \
3590Sstevel@tonic-gate l|=(((unsigned long)(*((c)++)))<<24), \
3600Sstevel@tonic-gate l)
361*2139Sjp161948 #endif
3620Sstevel@tonic-gate #define HOST_p_c2l(c,l,n) { \
3630Sstevel@tonic-gate switch (n) { \
3640Sstevel@tonic-gate case 0: l =((unsigned long)(*((c)++))); \
3650Sstevel@tonic-gate case 1: l|=((unsigned long)(*((c)++)))<< 8; \
3660Sstevel@tonic-gate case 2: l|=((unsigned long)(*((c)++)))<<16; \
3670Sstevel@tonic-gate case 3: l|=((unsigned long)(*((c)++)))<<24; \
3680Sstevel@tonic-gate } }
3690Sstevel@tonic-gate #define HOST_p_c2l_p(c,l,sc,len) { \
3700Sstevel@tonic-gate switch (sc) { \
3710Sstevel@tonic-gate case 0: l =((unsigned long)(*((c)++))); \
3720Sstevel@tonic-gate if (--len == 0) break; \
3730Sstevel@tonic-gate case 1: l|=((unsigned long)(*((c)++)))<< 8; \
3740Sstevel@tonic-gate if (--len == 0) break; \
3750Sstevel@tonic-gate case 2: l|=((unsigned long)(*((c)++)))<<16; \
3760Sstevel@tonic-gate } }
3770Sstevel@tonic-gate /* NOTE the pointer is not incremented at the end of this */
3780Sstevel@tonic-gate #define HOST_c2l_p(c,l,n) { \
3790Sstevel@tonic-gate l=0; (c)+=n; \
3800Sstevel@tonic-gate switch (n) { \
3810Sstevel@tonic-gate case 3: l =((unsigned long)(*(--(c))))<<16; \
3820Sstevel@tonic-gate case 2: l|=((unsigned long)(*(--(c))))<< 8; \
3830Sstevel@tonic-gate case 1: l|=((unsigned long)(*(--(c)))); \
3840Sstevel@tonic-gate } }
385*2139Sjp161948 #ifndef HOST_l2c
3860Sstevel@tonic-gate #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
3870Sstevel@tonic-gate *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
3880Sstevel@tonic-gate *((c)++)=(unsigned char)(((l)>>16)&0xff), \
3890Sstevel@tonic-gate *((c)++)=(unsigned char)(((l)>>24)&0xff), \
3900Sstevel@tonic-gate l)
391*2139Sjp161948 #endif
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate #endif
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * Time for some action:-)
3970Sstevel@tonic-gate */
3980Sstevel@tonic-gate
HASH_UPDATE(HASH_CTX * c,const void * data_,size_t len)399*2139Sjp161948 int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate const unsigned char *data=data_;
4020Sstevel@tonic-gate register HASH_LONG * p;
403*2139Sjp161948 register HASH_LONG l;
404*2139Sjp161948 size_t sw,sc,ew,ec;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate if (len==0) return 1;
4070Sstevel@tonic-gate
408*2139Sjp161948 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
4090Sstevel@tonic-gate /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
4100Sstevel@tonic-gate * Wei Dai <weidai@eskimo.com> for pointing it out. */
4110Sstevel@tonic-gate if (l < c->Nl) /* overflow */
4120Sstevel@tonic-gate c->Nh++;
413*2139Sjp161948 c->Nh+=(len>>29); /* might cause compiler warning on 16-bit */
4140Sstevel@tonic-gate c->Nl=l;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate if (c->num != 0)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate p=c->data;
4190Sstevel@tonic-gate sw=c->num>>2;
4200Sstevel@tonic-gate sc=c->num&0x03;
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate if ((c->num+len) >= HASH_CBLOCK)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
4250Sstevel@tonic-gate for (; sw<HASH_LBLOCK; sw++)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate HOST_c2l(data,l); p[sw]=l;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate HASH_BLOCK_HOST_ORDER (c,p,1);
4300Sstevel@tonic-gate len-=(HASH_CBLOCK-c->num);
4310Sstevel@tonic-gate c->num=0;
4320Sstevel@tonic-gate /* drop through and do the rest */
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate else
4350Sstevel@tonic-gate {
436*2139Sjp161948 c->num+=(unsigned int)len;
4370Sstevel@tonic-gate if ((sc+len) < 4) /* ugly, add char's to a word */
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate else
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate ew=(c->num>>2);
4440Sstevel@tonic-gate ec=(c->num&0x03);
4450Sstevel@tonic-gate if (sc)
4460Sstevel@tonic-gate l=p[sw];
4470Sstevel@tonic-gate HOST_p_c2l(data,l,sc);
4480Sstevel@tonic-gate p[sw++]=l;
4490Sstevel@tonic-gate for (; sw < ew; sw++)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate HOST_c2l(data,l); p[sw]=l;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate if (ec)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate HOST_c2l_p(data,l,ec); p[sw]=l;
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate return 1;
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate sw=len/HASH_CBLOCK;
4630Sstevel@tonic-gate if (sw > 0)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
4680Sstevel@tonic-gate * only if sizeof(HASH_LONG)==4.
4690Sstevel@tonic-gate */
470*2139Sjp161948 if ((((size_t)data)%4) == 0)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate /* data is properly aligned so that we can cast it: */
473*2139Sjp161948 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,sw);
4740Sstevel@tonic-gate sw*=HASH_CBLOCK;
4750Sstevel@tonic-gate data+=sw;
4760Sstevel@tonic-gate len-=sw;
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate else
4790Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER)
4800Sstevel@tonic-gate while (sw--)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate memcpy (p=c->data,data,HASH_CBLOCK);
4830Sstevel@tonic-gate HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
4840Sstevel@tonic-gate data+=HASH_CBLOCK;
4850Sstevel@tonic-gate len-=HASH_CBLOCK;
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate #endif
4880Sstevel@tonic-gate #endif
4890Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate HASH_BLOCK_DATA_ORDER(c,data,sw);
4920Sstevel@tonic-gate sw*=HASH_CBLOCK;
4930Sstevel@tonic-gate data+=sw;
4940Sstevel@tonic-gate len-=sw;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate #endif
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate if (len!=0)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate p = c->data;
5020Sstevel@tonic-gate c->num = len;
5030Sstevel@tonic-gate ew=len>>2; /* words to copy */
5040Sstevel@tonic-gate ec=len&0x03;
5050Sstevel@tonic-gate for (; ew; ew--,p++)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate HOST_c2l(data,l); *p=l;
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate HOST_c2l_p(data,l,ec);
5100Sstevel@tonic-gate *p=l;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate return 1;
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate
HASH_TRANSFORM(HASH_CTX * c,const unsigned char * data)5160Sstevel@tonic-gate void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
519*2139Sjp161948 if ((((size_t)data)%4) == 0)
5200Sstevel@tonic-gate /* data is properly aligned so that we can cast it: */
521*2139Sjp161948 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,1);
5220Sstevel@tonic-gate else
5230Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate memcpy (c->data,data,HASH_CBLOCK);
5260Sstevel@tonic-gate HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate #endif
5290Sstevel@tonic-gate #endif
5300Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER)
5310Sstevel@tonic-gate HASH_BLOCK_DATA_ORDER (c,data,1);
5320Sstevel@tonic-gate #endif
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate
HASH_FINAL(unsigned char * md,HASH_CTX * c)5360Sstevel@tonic-gate int HASH_FINAL (unsigned char *md, HASH_CTX *c)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate register HASH_LONG *p;
5390Sstevel@tonic-gate register unsigned long l;
5400Sstevel@tonic-gate register int i,j;
5410Sstevel@tonic-gate static const unsigned char end[4]={0x80,0x00,0x00,0x00};
5420Sstevel@tonic-gate const unsigned char *cp=end;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate /* c->num should definitly have room for at least one more byte. */
5450Sstevel@tonic-gate p=c->data;
5460Sstevel@tonic-gate i=c->num>>2;
5470Sstevel@tonic-gate j=c->num&0x03;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate #if 0
5500Sstevel@tonic-gate /* purify often complains about the following line as an
5510Sstevel@tonic-gate * Uninitialized Memory Read. While this can be true, the
5520Sstevel@tonic-gate * following p_c2l macro will reset l when that case is true.
5530Sstevel@tonic-gate * This is because j&0x03 contains the number of 'valid' bytes
5540Sstevel@tonic-gate * already in p[i]. If and only if j&0x03 == 0, the UMR will
5550Sstevel@tonic-gate * occur but this is also the only time p_c2l will do
5560Sstevel@tonic-gate * l= *(cp++) instead of l|= *(cp++)
5570Sstevel@tonic-gate * Many thanks to Alex Tang <altitude@cic.net> for pickup this
5580Sstevel@tonic-gate * 'potential bug' */
5590Sstevel@tonic-gate #ifdef PURIFY
5600Sstevel@tonic-gate if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
5610Sstevel@tonic-gate #endif
5620Sstevel@tonic-gate l=p[i];
5630Sstevel@tonic-gate #else
5640Sstevel@tonic-gate l = (j==0) ? 0 : p[i];
5650Sstevel@tonic-gate #endif
5660Sstevel@tonic-gate HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate if (i<HASH_LBLOCK) p[i]=0;
5710Sstevel@tonic-gate HASH_BLOCK_HOST_ORDER (c,p,1);
5720Sstevel@tonic-gate i=0;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate for (; i<(HASH_LBLOCK-2); i++)
5750Sstevel@tonic-gate p[i]=0;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate #if defined(DATA_ORDER_IS_BIG_ENDIAN)
5780Sstevel@tonic-gate p[HASH_LBLOCK-2]=c->Nh;
5790Sstevel@tonic-gate p[HASH_LBLOCK-1]=c->Nl;
5800Sstevel@tonic-gate #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
5810Sstevel@tonic-gate p[HASH_LBLOCK-2]=c->Nl;
5820Sstevel@tonic-gate p[HASH_LBLOCK-1]=c->Nh;
5830Sstevel@tonic-gate #endif
5840Sstevel@tonic-gate HASH_BLOCK_HOST_ORDER (c,p,1);
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate #ifndef HASH_MAKE_STRING
5870Sstevel@tonic-gate #error "HASH_MAKE_STRING must be defined!"
5880Sstevel@tonic-gate #else
5890Sstevel@tonic-gate HASH_MAKE_STRING(c,md);
5900Sstevel@tonic-gate #endif
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate c->num=0;
5930Sstevel@tonic-gate /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
5940Sstevel@tonic-gate * but I'm not worried :-)
5950Sstevel@tonic-gate OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
5960Sstevel@tonic-gate */
5970Sstevel@tonic-gate return 1;
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate #ifndef MD32_REG_T
6010Sstevel@tonic-gate #define MD32_REG_T long
6020Sstevel@tonic-gate /*
6030Sstevel@tonic-gate * This comment was originaly written for MD5, which is why it
6040Sstevel@tonic-gate * discusses A-D. But it basically applies to all 32-bit digests,
6050Sstevel@tonic-gate * which is why it was moved to common header file.
6060Sstevel@tonic-gate *
6070Sstevel@tonic-gate * In case you wonder why A-D are declared as long and not
6080Sstevel@tonic-gate * as MD5_LONG. Doing so results in slight performance
6090Sstevel@tonic-gate * boost on LP64 architectures. The catch is we don't
6100Sstevel@tonic-gate * really care if 32 MSBs of a 64-bit register get polluted
6110Sstevel@tonic-gate * with eventual overflows as we *save* only 32 LSBs in
6120Sstevel@tonic-gate * *either* case. Now declaring 'em long excuses the compiler
6130Sstevel@tonic-gate * from keeping 32 MSBs zeroed resulting in 13% performance
6140Sstevel@tonic-gate * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
6150Sstevel@tonic-gate * Well, to be honest it should say that this *prevents*
6160Sstevel@tonic-gate * performance degradation.
6170Sstevel@tonic-gate * <appro@fy.chalmers.se>
6180Sstevel@tonic-gate * Apparently there're LP64 compilers that generate better
6190Sstevel@tonic-gate * code if A-D are declared int. Most notably GCC-x86_64
6200Sstevel@tonic-gate * generates better code.
6210Sstevel@tonic-gate * <appro@fy.chalmers.se>
6220Sstevel@tonic-gate */
6230Sstevel@tonic-gate #endif
624