2 * sha.h: header file for SHA-1/224/256/384/512 routines
4 * Ref: NIST FIPS PUB 180-4 Secure Hash Standard
6 * Copyright (C) 2003-2017 Mark Shelor, All Rights Reserved
9 * Fri Dec 8 22:44:44 MST 2017
13 #ifndef _INCLUDE_SHA_H_
14 #define _INCLUDE_SHA_H_
18 #define SHA32_MAX 4294967295U
20 #define SHA32_SHR(x, n) ((x) >> (n))
21 #define SHA32_SHL(x, n) ((x) << (n))
23 #define SHA64_SHR(x, n) ((x) >> (n))
24 #define SHA64_SHL(x, n) ((x) << (n))
29 #define SHA_LO32(x) (x)
31 #if USHRT_MAX == SHA32_MAX
32 #define SHA32 unsigned short
33 #define SHA32_CONST(c) c ## U
34 #elif UINT_MAX == SHA32_MAX
35 #define SHA32 unsigned int
36 #define SHA32_CONST(c) c ## U
37 #elif ULONG_MAX == SHA32_MAX
38 #define SHA32 unsigned long
39 #define SHA32_CONST(c) c ## UL
43 #define SHA_LO32(x) ((x) & SHA32_MAX)
45 #define SHA32_SHR(x, n) (SHA_LO32(x) >> (n))
46 #define SHA32 unsigned long
47 #define SHA32_CONST(c) c ## UL
50 #if defined(ULONG_LONG_MAX) || defined(ULLONG_MAX) || defined(HAS_LONG_LONG)
51 #define SHA_ULL_EXISTS
54 #if (((ULONG_MAX >> 16) >> 16) >> 16) >> 15 == 1UL
55 #define SHA64 unsigned long
56 #define SHA64_CONST(c) c ## UL
57 #elif defined(SHA_ULL_EXISTS) && defined(LONGLONGSIZE) && LONGLONGSIZE == 8
58 #define SHA64 unsigned long long
59 #define SHA64_CONST(c) c ## ULL
60 #elif defined(SHA_ULL_EXISTS)
63 #define SHA64_MAX 18446744073709551615ULL
64 #define SHA64_SHR(x, n) (((x) & SHA64_MAX) >> (n))
65 #define SHA64 unsigned long long
66 #define SHA64_CONST(c) c ## ULL
68 /* The following cases detect compilers that
69 * support 64-bit types in a non-standard way */
71 #elif defined(_MSC_VER) /* Microsoft C */
72 #define SHA64 unsigned __int64
73 #define SHA64_CONST(c) (SHA64) c
76 #if defined(SHA64) && !defined(NO_SHA_384_512)
80 #if defined(BYTEORDER) && (BYTEORDER & 0xffff) == 0x4321
81 #if defined(SHA32_ALIGNED)
82 #define SHA32_SCHED(W, b) Copy(b, W, 64, char)
84 #if defined(SHA64) && defined(SHA64_ALIGNED)
85 #define SHA64_SCHED(W, b) Copy(b, W, 128, char)
89 #if !defined(SHA32_SCHED)
90 #define SHA32_SCHED(W, b) { int t; SHA32 *q = W; \
91 for (t = 0; t < 16; t++, b += 4) *q++ = \
92 (SHA32) b[0] << 24 | (SHA32) b[1] << 16 | \
93 (SHA32) b[2] << 8 | (SHA32) b[3]; }
96 #if defined(SHA64) && !defined(SHA64_SCHED)
97 #define SHA64_SCHED(W, b) { int t; SHA64 *q = W; \
98 for (t = 0; t < 16; t++, b += 8) *q++ = \
99 (SHA64) b[0] << 56 | (SHA64) b[1] << 48 | \
100 (SHA64) b[2] << 40 | (SHA64) b[3] << 32 | \
101 (SHA64) b[4] << 24 | (SHA64) b[5] << 16 | \
102 (SHA64) b[6] << 8 | (SHA64) b[7]; }
110 #define SHA512224 512224
111 #define SHA512256 512256
113 #define SHA1_BLOCK_BITS 512
114 #define SHA224_BLOCK_BITS SHA1_BLOCK_BITS
115 #define SHA256_BLOCK_BITS SHA1_BLOCK_BITS
116 #define SHA384_BLOCK_BITS 1024
117 #define SHA512_BLOCK_BITS SHA384_BLOCK_BITS
118 #define SHA512224_BLOCK_BITS SHA512_BLOCK_BITS
119 #define SHA512256_BLOCK_BITS SHA512_BLOCK_BITS
121 #define SHA1_DIGEST_BITS 160
122 #define SHA224_DIGEST_BITS 224
123 #define SHA256_DIGEST_BITS 256
124 #define SHA384_DIGEST_BITS 384
125 #define SHA512_DIGEST_BITS 512
126 #define SHA512224_DIGEST_BITS 224
127 #define SHA512256_DIGEST_BITS 256
129 #define SHA_MAX_BLOCK_BITS SHA512_BLOCK_BITS
130 #define SHA_MAX_DIGEST_BITS SHA512_DIGEST_BITS
131 #define SHA_MAX_HEX_LEN (SHA_MAX_DIGEST_BITS / 4)
132 #define SHA_MAX_BASE64_LEN (1 + (SHA_MAX_DIGEST_BITS / 6))
140 void (*sha)(struct SHA *, unsigned char *);
143 unsigned char block[SHA_MAX_BLOCK_BITS/8];
144 unsigned int blockcnt;
145 unsigned int blocksize;
146 SHA32 lenhh, lenhl, lenlh, lenll;
147 unsigned char digest[SHA_MAX_DIGEST_BITS/8];
148 unsigned int digestlen;
149 char hex[SHA_MAX_HEX_LEN+1];
150 char base64[SHA_MAX_BASE64_LEN+1];
156 unsigned int digestlen;
157 unsigned char key[SHA_MAX_BLOCK_BITS/8];
160 #endif /* _INCLUDE_SHA_H_ */