diff options
author | ben <ben@13f79535-47bb-0310-9956-ffa450edef68> | 2003-11-05 13:34:53 +0000 |
---|---|---|
committer | ben <ben@13f79535-47bb-0310-9956-ffa450edef68> | 2003-11-05 13:34:53 +0000 |
commit | d0d784a8f5bb0acda4837c09ed0e703591e24cc5 (patch) | |
tree | 88f9bafaa81def990de0edc3c607ec52872e2f9c /random | |
parent | ece1d38373a230b97637409a8255918bcfc2a451 (diff) | |
download | libapr-d0d784a8f5bb0acda4837c09ed0e703591e24cc5.tar.gz |
Endianness and APR types for random.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@64728 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'random')
-rw-r--r-- | random/unix/sha2.c | 109 | ||||
-rw-r--r-- | random/unix/sha2.h | 151 |
2 files changed, 52 insertions, 208 deletions
diff --git a/random/unix/sha2.c b/random/unix/sha2.c index e1e50d908..540c99b39 100644 --- a/random/unix/sha2.c +++ b/random/unix/sha2.c @@ -82,67 +82,10 @@ * */ - /*** SHA-256/384/512 Machine Architecture Definitions *****************/ -/* - * BYTE_ORDER NOTE: - * - * Please make sure that your system defines BYTE_ORDER. If your - * architecture is little-endian, make sure it also defines - * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are - * equivilent. - * - * If your system does not define the above, then you can do so by - * hand like this: - * - * #define LITTLE_ENDIAN 1234 - * #define BIG_ENDIAN 4321 - * - * And for little-endian machines, add: - * - * #define BYTE_ORDER LITTLE_ENDIAN - * - * Or for big-endian machines: - * - * #define BYTE_ORDER BIG_ENDIAN - * - * The FreeBSD machine this was written on defines BYTE_ORDER - * appropriately by including <sys/types.h> (which in turn includes - * <machine/endian.h> where the appropriate definitions are actually - * made). - */ -#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) -#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN -#endif - -/* - * Define the followingsha2_* types to types of the correct length on - * the native archtecture. Most BSD systems and Linux define u_intXX_t - * types. Machines with very recent ANSI C headers, can use the - * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H - * during compile or in the sha.h header file. - * - * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t - * will need to define these three typedefs below (and the appropriate - * ones in sha.h too) by hand according to their system architecture. - * - * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t - * types and pointing out recent ANSI C support for uintXX_t in inttypes.h. - */ -#ifdef SHA2_USE_INTTYPES_H - -typedef uint8_t sha2_byte; /* Exactly 1 byte */ -typedef uint32_t sha2_word32; /* Exactly 4 bytes */ -typedef uint64_t sha2_word64; /* Exactly 8 bytes */ - -#else /* SHA2_USE_INTTYPES_H */ - -typedef u_int8_t sha2_byte; /* Exactly 1 byte */ -typedef u_int32_t sha2_word32; /* Exactly 4 bytes */ -typedef u_int64_t sha2_word64; /* Exactly 8 bytes */ - -#endif /* SHA2_USE_INTTYPES_H */ - +typedef apr_byte_t sha2_byte; /* Exactly 1 byte */ +typedef apr_uint32_t sha2_word32; /* Exactly 4 bytes */ +typedef apr_uint64_t sha2_word64; /* Exactly 8 bytes */ /*** SHA-256/384/512 Various Length Definitions ***********************/ /* NOTE: Most of these are in sha2.h */ @@ -152,7 +95,7 @@ typedef u_int64_t sha2_word64; /* Exactly 8 bytes */ /*** ENDIAN REVERSAL MACROS *******************************************/ -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN #define REVERSE32(w,x) { \ sha2_word32 tmp = (w); \ tmp = (tmp >> 16) | (tmp << 16); \ @@ -166,7 +109,7 @@ typedef u_int64_t sha2_word64; /* Exactly 8 bytes */ (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ ((tmp & 0x0000ffff0000ffffULL) << 16); \ } -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#endif /* !APR_IS_BIGENDIAN */ /* * Macro for incrementally adding the unsigned 64-bit integer n to the @@ -372,7 +315,7 @@ void SHA256_Init(SHA256_CTX* context) { /* Unrolled SHA-256 round macros: */ -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ REVERSE32(*data++, W256[j]); \ @@ -383,7 +326,7 @@ void SHA256_Init(SHA256_CTX* context) { j++ -#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#else /* APR_IS_BIGENDIAN */ #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \ @@ -392,7 +335,7 @@ void SHA256_Init(SHA256_CTX* context) { (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \ j++ -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#endif /* APR_IS_BIGENDIAN */ #define ROUND256(a,b,c,d,e,f,g,h) \ s0 = W256[(j+1)&0x0f]; \ @@ -482,15 +425,15 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) { j = 0; do { -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN /* Copy data while converting to host byte order */ REVERSE32(*data++,W256[j]); /* Apply the SHA-256 compression function to update a..h */ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; -#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#else /* APR_IS_BIGENDIAN */ /* Apply the SHA-256 compression function to update a..h with copy */ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++); -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#endif /* APR_IS_BIGENDIAN */ T2 = Sigma0_256(a) + Maj(a, b, c); h = g; g = f; @@ -601,7 +544,7 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) { /* If no digest buffer is passed, we don't bother doing this: */ if (digest != (sha2_byte*)0) { usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN /* Convert FROM host byte order */ REVERSE64(context->bitcount,context->bitcount); #endif @@ -635,7 +578,7 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) { /* Final transform: */ SHA256_Transform(context, (sha2_word32*)context->buffer); -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN { /* Convert TO host byte order */ int j; @@ -699,7 +642,7 @@ void SHA512_Init(SHA512_CTX* context) { #ifdef SHA2_UNROLL_TRANSFORM /* Unrolled SHA-512 round macros: */ -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ REVERSE64(*data++, W512[j]); \ @@ -710,7 +653,7 @@ void SHA512_Init(SHA512_CTX* context) { j++ -#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#else /* APR_IS_BIGENDIAN */ #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \ @@ -719,7 +662,7 @@ void SHA512_Init(SHA512_CTX* context) { (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \ j++ -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#endif /* APR_IS_BIGENDIAN */ #define ROUND512(a,b,c,d,e,f,g,h) \ s0 = W512[(j+1)&0x0f]; \ @@ -804,15 +747,15 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) { j = 0; do { -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN /* Convert TO host byte order */ REVERSE64(*data++, W512[j]); /* Apply the SHA-512 compression function to update a..h */ T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j]; -#else /* BYTE_ORDER == LITTLE_ENDIAN */ +#else /* APR_IS_BIGENDIAN */ /* Apply the SHA-512 compression function to update a..h with copy */ T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++); -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ +#endif /* APR_IS_BIGENDIAN */ T2 = Sigma0_512(a) + Maj(a, b, c); h = g; g = f; @@ -917,7 +860,7 @@ void SHA512_Last(SHA512_CTX* context) { unsigned int usedspace; usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN /* Convert FROM host byte order */ REVERSE64(context->bitcount[0],context->bitcount[0]); REVERSE64(context->bitcount[1],context->bitcount[1]); @@ -965,7 +908,7 @@ void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) { SHA512_Last(context); /* Save the hash data for output: */ -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN { /* Convert TO host byte order */ int j; @@ -974,9 +917,9 @@ void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) { *d++ = context->state[j]; } } -#else +#else /* APR_IS_BIGENDIAN */ MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH); -#endif +#endif /* APR_IS_BIGENDIAN */ } /* Zero out state data */ @@ -1040,7 +983,7 @@ void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) { SHA512_Last((SHA512_CTX*)context); /* Save the hash data for output: */ -#if BYTE_ORDER == LITTLE_ENDIAN +#if !APR_IS_BIGENDIAN { /* Convert TO host byte order */ int j; @@ -1049,9 +992,9 @@ void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) { *d++ = context->state[j]; } } -#else +#else /* APR_IS_BIGENDIAN */ MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH); -#endif +#endif /* APR_IS_BIGENDIAN */ } /* Zero out state data */ diff --git a/random/unix/sha2.h b/random/unix/sha2.h index ba9ecb196..cba4f993d 100644 --- a/random/unix/sha2.h +++ b/random/unix/sha2.h @@ -65,20 +65,7 @@ extern "C" { #endif - -/* - * Import u_intXX_t size_t type definitions from system headers. You - * may need to change this, or define these things yourself in this - * file. - */ -#include <sys/types.h> - -#ifdef SHA2_USE_INTTYPES_H - -#include <inttypes.h> - -#endif /* SHA2_USE_INTTYPES_H */ - +#include "apr.h" /*** SHA-256/384/512 Various Length Definitions ***********************/ #define SHA256_BLOCK_LENGTH 64 @@ -93,127 +80,41 @@ extern "C" { /*** SHA-256/384/512 Context Structures *******************************/ -/* NOTE: If your architecture does not define either u_intXX_t types or - * uintXX_t (from inttypes.h), you may need to define things by hand - * for your system: - */ -#if 0 -typedef unsigned char u_int8_t; /* 1-byte (8-bits) */ -typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */ -typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */ -#endif -/* - * Most BSD systems already define u_intXX_t types, as does Linux. - * Some systems, however, like Compaq's Tru64 Unix instead can use - * uintXX_t types defined by very recent ANSI C standards and included - * in the file: - * - * #include <inttypes.h> - * - * If you choose to use <inttypes.h> then please define: - * - * #define SHA2_USE_INTTYPES_H - * - * Or on the command line during compile: - * - * cc -DSHA2_USE_INTTYPES_H ... - */ -#ifdef SHA2_USE_INTTYPES_H - -typedef struct _SHA256_CTX { - uint32_t state[8]; - uint64_t bitcount; - uint8_t buffer[SHA256_BLOCK_LENGTH]; -} SHA256_CTX; -typedef struct _SHA512_CTX { - uint64_t state[8]; - uint64_t bitcount[2]; - uint8_t buffer[SHA512_BLOCK_LENGTH]; -} SHA512_CTX; - -#else /* SHA2_USE_INTTYPES_H */ - typedef struct _SHA256_CTX { - u_int32_t state[8]; - u_int64_t bitcount; - u_int8_t buffer[SHA256_BLOCK_LENGTH]; + apr_uint32_t state[8]; + apr_uint64_t bitcount; + apr_byte_t buffer[SHA256_BLOCK_LENGTH]; } SHA256_CTX; typedef struct _SHA512_CTX { - u_int64_t state[8]; - u_int64_t bitcount[2]; - u_int8_t buffer[SHA512_BLOCK_LENGTH]; + apr_uint64_t state[8]; + apr_uint64_t bitcount[2]; + apr_byte_t buffer[SHA512_BLOCK_LENGTH]; } SHA512_CTX; -#endif /* SHA2_USE_INTTYPES_H */ - typedef SHA512_CTX SHA384_CTX; /*** SHA-256/384/512 Function Prototypes ******************************/ -#ifndef NOPROTO -#ifdef SHA2_USE_INTTYPES_H - void SHA256_Init(SHA256_CTX *); -void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); -void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); -char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); -char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); - -void SHA384_Init(SHA384_CTX*); -void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); -void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); -char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); -char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); - -void SHA512_Init(SHA512_CTX*); -void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); -void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); -char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); -char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); - -#else /* SHA2_USE_INTTYPES_H */ - -void SHA256_Init(SHA256_CTX *); -void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t); -void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); -char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); -char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); - -void SHA384_Init(SHA384_CTX*); -void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t); -void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); -char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); -char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); - -void SHA512_Init(SHA512_CTX*); -void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t); -void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); -char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); -char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); - -#endif /* SHA2_USE_INTTYPES_H */ - -#else /* NOPROTO */ - -void SHA256_Init(); -void SHA256_Update(); -void SHA256_Final(); -char* SHA256_End(); -char* SHA256_Data(); - -void SHA384_Init(); -void SHA384_Update(); -void SHA384_Final(); -char* SHA384_End(); -char* SHA384_Data(); - -void SHA512_Init(); -void SHA512_Update(); -void SHA512_Final(); -char* SHA512_End(); -char* SHA512_Data(); - -#endif /* NOPROTO */ +void SHA256_Update(SHA256_CTX *, const apr_byte_t *, size_t); +void SHA256_Final(apr_byte_t [SHA256_DIGEST_LENGTH], SHA256_CTX *); +char* SHA256_End(SHA256_CTX *, char [SHA256_DIGEST_STRING_LENGTH]); +char* SHA256_Data(const apr_byte_t *, size_t, + char [SHA256_DIGEST_STRING_LENGTH]); + +void SHA384_Init(SHA384_CTX *); +void SHA384_Update(SHA384_CTX *, const apr_byte_t *, size_t); +void SHA384_Final(apr_byte_t [SHA384_DIGEST_LENGTH], SHA384_CTX *); +char* SHA384_End(SHA384_CTX *, char [SHA384_DIGEST_STRING_LENGTH]); +char* SHA384_Data(const apr_byte_t *, size_t, + char [SHA384_DIGEST_STRING_LENGTH]); + +void SHA512_Init(SHA512_CTX *); +void SHA512_Update(SHA512_CTX *, const apr_byte_t *, size_t); +void SHA512_Final(apr_byte_t [SHA512_DIGEST_LENGTH], SHA512_CTX *); +char* SHA512_End(SHA512_CTX *, char [SHA512_DIGEST_STRING_LENGTH]); +char* SHA512_Data(const apr_byte_t *, size_t, + char [SHA512_DIGEST_STRING_LENGTH]); #ifdef __cplusplus } |