Import sha2 implementation 1.1 from Aaron D. Gifford

Update cm_sha2.[hc] from sha2.[hc] in "sha2-1.1-ALPHA.tgz" downloaded
today from

  http://www.aarongifford.com/computers/sha.html

with trivial whitespace cleanup.  This adds SHA-224 support.
This commit is contained in:
Brad King 2011-06-27 14:35:09 -04:00
parent 9912c41c17
commit 8251b20d4b
2 changed files with 953 additions and 350 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,9 @@
/* /*
* FILE: sha2.h * FILE: sha2.h
* AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ * AUTHOR: Aaron D. Gifford
* http://www.aarongifford.com/computers/sha.html
* *
* Copyright (c) 2000-2001, Aaron D. Gifford * Copyright (c) 2000-2003, Aaron D. Gifford
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -29,7 +30,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ * $Id: sha2.h,v 1.4 2004/01/07 19:06:18 adg Exp $
*/ */
#ifndef __SHA2_H__ #ifndef __SHA2_H__
@ -54,27 +55,30 @@ extern "C" {
#endif /* SHA2_USE_INTTYPES_H */ #endif /* SHA2_USE_INTTYPES_H */
/*** SHA-256/384/512 Various Length Definitions ***********************/ /*** SHA-224/256/384/512 Various Length Definitions *******************/
#define SHA256_BLOCK_LENGTH 64
#define SHA256_DIGEST_LENGTH 32 /* Digest lengths for SHA-1/224/256/384/512 */
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) #define SHA1_DIGEST_LENGTH 20
#define SHA384_BLOCK_LENGTH 128 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
#define SHA384_DIGEST_LENGTH 48 #define SHA224_DIGEST_LENGTH 28
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) #define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
#define SHA512_BLOCK_LENGTH 128 #define SHA256_DIGEST_LENGTH 32
#define SHA512_DIGEST_LENGTH 64 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) #define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
/*** SHA-256/384/512 Context Structures *******************************/ /*** SHA-224/256/384/512 Context Structures ***************************/
/* NOTE: If your architecture does not define either u_intXX_t types or /* 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 * uintXX_t (from inttypes.h), you may need to define things by hand
* for your system: * for your system:
*/ */
#if 0 #if 0
typedef unsigned char u_int8_t; /* 1-byte (8-bits) */ typedef unsigned char u_int8_t; /* 1-byte (8-bits) */
typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */ typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */
typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */ typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
#endif #endif
/* /*
* Most BSD systems already define u_intXX_t types, as does Linux. * Most BSD systems already define u_intXX_t types, as does Linux.
@ -94,81 +98,139 @@ typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
*/ */
#ifdef SHA2_USE_INTTYPES_H #ifdef SHA2_USE_INTTYPES_H
typedef struct _SHA256_CTX { typedef union _SHA_CTX {
uint32_t state[8]; /* SHA-1 uses this part of the union: */
uint64_t bitcount; struct {
uint8_t buffer[SHA256_BLOCK_LENGTH]; uint32_t state[5];
} SHA256_CTX; uint64_t bitcount;
typedef struct _SHA512_CTX { uint8_t buffer[64];
uint64_t state[8]; } s1;
uint64_t bitcount[2];
uint8_t buffer[SHA512_BLOCK_LENGTH]; /* SHA-224 and SHA-256 use this part of the union: */
} SHA512_CTX; struct {
uint32_t state[8];
uint64_t bitcount;
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];
} s512;
} SHA_CTX;
#else /* SHA2_USE_INTTYPES_H */ #else /* SHA2_USE_INTTYPES_H */
typedef struct _SHA256_CTX { typedef union _SHA_CTX {
u_int32_t state[8]; /* SHA-1 uses this part of the union: */
u_int64_t bitcount; struct {
u_int8_t buffer[SHA256_BLOCK_LENGTH]; u_int32_t state[5];
} SHA256_CTX; u_int64_t bitcount;
typedef struct _SHA512_CTX { u_int8_t buffer[64];
u_int64_t state[8]; } s1;
u_int64_t bitcount[2];
u_int8_t buffer[SHA512_BLOCK_LENGTH]; /* SHA-224 and SHA-256 use this part of the union: */
} SHA512_CTX; 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 */ #endif /* SHA2_USE_INTTYPES_H */
typedef SHA512_CTX SHA384_CTX;
/*** SHA-256/384/512 Function Prototypes ******************************/ /*** SHA-256/384/512 Function Prototypes ******************************/
#ifndef NOPROTO #ifndef NOPROTO
#ifdef SHA2_USE_INTTYPES_H #ifdef SHA2_USE_INTTYPES_H
void SHA256_Init(SHA256_CTX *); void SHA1_Init(SHA_CTX*);
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); void SHA1_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); void SHA1_Final(uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const 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*);
char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const 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*);
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 uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA384_CTX*); void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); void SHA384_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 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 uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
void SHA512_Init(SHA512_CTX*); void SHA512_Init(SHA_CTX*);
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); void SHA512_Update(SHA_CTX*, const uint8_t*, size_t);
void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
#else /* SHA2_USE_INTTYPES_H */ #else /* SHA2_USE_INTTYPES_H */
void SHA256_Init(SHA256_CTX *); void SHA1_Init(SHA_CTX*);
void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t); void SHA1_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); void SHA1_Final(u_int8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 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]); char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA384_CTX*); void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t); void SHA384_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
char* SHA384_Data(const u_int8_t*, size_t, 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_Init(SHA_CTX*);
void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t); void SHA512_Update(SHA_CTX*, const u_int8_t*, size_t);
void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
#endif /* SHA2_USE_INTTYPES_H */ #endif /* SHA2_USE_INTTYPES_H */
#else /* NOPROTO */ #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_Init();
void SHA256_Update(); void SHA256_Update();
void SHA256_Final(); void SHA256_Final();
@ -189,7 +251,7 @@ char* SHA512_Data();
#endif /* NOPROTO */ #endif /* NOPROTO */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */