sha2: Use KWIML fixed-size integer types and endian-ness

These are more portable than those named in the original sha2 code.
This commit is contained in:
Brad King 2011-11-15 20:18:58 -05:00
parent fcc3ce5b0d
commit c1856a33d4
2 changed files with 42 additions and 184 deletions

View File

@ -87,37 +87,20 @@
* 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
/* CMake modification: use byte order from cmIML. */
# include "cmIML/ABI.h"
# undef BYTE_ORDER
# undef BIG_ENDIAN
# undef LITTLE_ENDIAN
# define BYTE_ORDER cmIML_ABI_ENDIAN_ID
# define BIG_ENDIAN cmIML_ABI_ENDIAN_ID_BIG
# define LITTLE_ENDIAN cmIML_ABI_ENDIAN_ID_LITTLE
#endif
/*
* Define the following sha_* 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 sha_byte; /* Exactly 1 byte */
typedef uint32_t sha_word32; /* Exactly 4 bytes */
typedef uint64_t sha_word64; /* Exactly 8 bytes */
#else /* SHA2_USE_INTTYPES_H */
typedef u_int8_t sha_byte; /* Exactly 1 byte */
typedef u_int32_t sha_word32; /* Exactly 4 bytes */
typedef u_int64_t sha_word64; /* Exactly 8 bytes */
#endif /* SHA2_USE_INTTYPES_H */
/* CMake modification: use types computed in header. */
typedef cm_sha2_uint8_t sha_byte; /* Exactly 1 byte */
typedef cm_sha2_uint32_t sha_word32; /* Exactly 4 bytes */
typedef cm_sha2_uint64_t sha_word64; /* Exactly 8 bytes */
/*** ENDIAN REVERSAL MACROS *******************************************/
#if BYTE_ORDER == LITTLE_ENDIAN

View File

@ -36,6 +36,12 @@
#ifndef __SHA2_H__
#define __SHA2_H__
/* CMake modification: use integer types from cmIML. */
#include "cmIML/INT.h"
typedef cmIML_INT_uint8_t cm_sha2_uint8_t;
typedef cmIML_INT_uint32_t cm_sha2_uint32_t;
typedef cmIML_INT_uint64_t cm_sha2_uint64_t;
#ifdef __cplusplus
extern "C" {
#endif
@ -48,13 +54,6 @@ extern "C" {
*/
#include <sys/types.h>
#ifdef SHA2_USE_INTTYPES_H
#include <inttypes.h>
#endif /* SHA2_USE_INTTYPES_H */
/*** SHA-224/256/384/512 Various Length Definitions *******************/
/* Digest lengths for SHA-1/224/256/384/512 */
@ -71,185 +70,61 @@ extern "C" {
/*** SHA-224/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 union _SHA_CTX {
/* SHA-1 uses this part of the union: */
struct {
uint32_t state[5];
uint64_t bitcount;
uint8_t buffer[64];
cm_sha2_uint32_t state[5];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s1;
/* SHA-224 and SHA-256 use this part of the union: */
struct {
uint32_t state[8];
uint64_t bitcount;
uint8_t buffer[64];
cm_sha2_uint32_t state[8];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s256;
/* SHA-384 and SHA-512 use this part of the union: */
struct {
uint64_t state[8];
uint64_t bitcount[2];
uint8_t buffer[128];
cm_sha2_uint64_t state[8];
cm_sha2_uint64_t bitcount[2];
cm_sha2_uint8_t buffer[128];
} s512;
} SHA_CTX;
#else /* SHA2_USE_INTTYPES_H */
typedef union _SHA_CTX {
/* SHA-1 uses this part of the union: */
struct {
u_int32_t state[5];
u_int64_t bitcount;
u_int8_t buffer[64];
} s1;
/* SHA-224 and SHA-256 use this part of the union: */
struct {
u_int32_t state[8];
u_int64_t bitcount;
u_int8_t buffer[64];
} s256;
/* SHA-384 and SHA-512 use this part of the union: */
struct {
u_int64_t state[8];
u_int64_t bitcount[2];
u_int8_t buffer[128];
} s512;
} SHA_CTX;
#endif /* SHA2_USE_INTTYPES_H */
/*** SHA-256/384/512 Function Prototypes ******************************/
#ifndef NOPROTO
#ifdef SHA2_USE_INTTYPES_H
void SHA1_Init(SHA_CTX*);
void SHA1_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA1_Final(uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
void SHA1_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA1_Final(cm_sha2_uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const uint8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const cm_sha2_uint8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
void SHA224_Init(SHA_CTX*);
void SHA224_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA224_Final(uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
void SHA224_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA224_Final(cm_sha2_uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const uint8_t*, size_t, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const cm_sha2_uint8_t*, size_t, char[SHA224_DIGEST_STRING_LENGTH]);
void SHA256_Init(SHA_CTX*);
void SHA256_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
void SHA256_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA256_Final(cm_sha2_uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const cm_sha2_uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
void SHA384_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA384_Final(cm_sha2_uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const cm_sha2_uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA_CTX*);
void SHA512_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
void SHA512_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA512_Final(cm_sha2_uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
char* SHA512_End(SHA_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 SHA1_Init(SHA_CTX*);
void SHA1_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA1_Final(u_int8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const u_int8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
void SHA224_Init(SHA_CTX*);
void SHA224_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA224_Final(u_int8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const u_int8_t*, size_t, char[SHA224_DIGEST_STRING_LENGTH]);
void SHA256_Init(SHA_CTX*);
void SHA256_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA_CTX*);
void SHA512_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
char* SHA512_End(SHA_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 SHA1_Init();
void SHA1_Update();
void SHA1_Final();
char* SHA1_End();
char* SHA1_Data();
void SHA224_Init();
void SHA224_Update();
void SHA224_Final();
char* SHA224_End();
char* SHA224_Data();
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 */
char* SHA512_Data(const cm_sha2_uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
#ifdef __cplusplus
}