sha2: Use KWIML fixed-size integer constant macros

Define SHA_UINT32_C and SHA_UINT64_C using the KWIML versions.
Use them in place of hard-coded UL and ULL constant suffixes.
This commit is contained in:
Brad King 2011-11-16 10:48:10 -05:00
parent 2e9c26cf96
commit 23b3df76a0
1 changed files with 121 additions and 102 deletions

View File

@ -101,21 +101,24 @@
typedef cm_sha2_uint8_t sha_byte; /* Exactly 1 byte */ typedef cm_sha2_uint8_t sha_byte; /* Exactly 1 byte */
typedef cm_sha2_uint32_t sha_word32; /* Exactly 4 bytes */ typedef cm_sha2_uint32_t sha_word32; /* Exactly 4 bytes */
typedef cm_sha2_uint64_t sha_word64; /* Exactly 8 bytes */ typedef cm_sha2_uint64_t sha_word64; /* Exactly 8 bytes */
#define SHA_UINT32_C(x) cmIML_INT_UINT32_C(x)
#define SHA_UINT64_C(x) cmIML_INT_UINT64_C(x)
/*** ENDIAN REVERSAL MACROS *******************************************/ /*** ENDIAN REVERSAL MACROS *******************************************/
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
#define REVERSE32(w,x) { \ #define REVERSE32(w,x) { \
sha_word32 tmp = (w); \ sha_word32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \ tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ (x) = ((tmp & SHA_UINT32_C(0xff00ff00)) >> 8) | \
((tmp & SHA_UINT32_C(0x00ff00ff)) << 8); \
} }
#define REVERSE64(w,x) { \ #define REVERSE64(w,x) { \
sha_word64 tmp = (w); \ sha_word64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \ tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \ tmp = ((tmp & SHA_UINT64_C(0xff00ff00ff00ff00)) >> 8) | \
((tmp & 0x00ff00ff00ff00ffULL) << 8); \ ((tmp & SHA_UINT64_C(0x00ff00ff00ff00ff)) << 8); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ (x) = ((tmp & SHA_UINT64_C(0xffff0000ffff0000)) >> 16) | \
((tmp & 0x0000ffff0000ffffULL) << 16); \ ((tmp & SHA_UINT64_C(0x0000ffff0000ffff)) << 16); \
} }
#endif /* BYTE_ORDER == LITTLE_ENDIAN */ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
@ -222,130 +225,146 @@ void SHA512_Internal_Transform(SHA_CTX*, const sha_word64*);
/*** SHA2 INITIAL HASH VALUES AND CONSTANTS ***************************/ /*** SHA2 INITIAL HASH VALUES AND CONSTANTS ***************************/
/* Hash constant words K for SHA-1: */ /* Hash constant words K for SHA-1: */
#define K1_0_TO_19 0x5a827999UL #define K1_0_TO_19 SHA_UINT32_C(0x5a827999)
#define K1_20_TO_39 0x6ed9eba1UL #define K1_20_TO_39 SHA_UINT32_C(0x6ed9eba1)
#define K1_40_TO_59 0x8f1bbcdcUL #define K1_40_TO_59 SHA_UINT32_C(0x8f1bbcdc)
#define K1_60_TO_79 0xca62c1d6UL #define K1_60_TO_79 SHA_UINT32_C(0xca62c1d6)
/* Initial hash value H for SHA-1: */ /* Initial hash value H for SHA-1: */
static const sha_word32 sha1_initial_hash_value[5] = { static const sha_word32 sha1_initial_hash_value[5] = {
0x67452301UL, SHA_UINT32_C(0x67452301),
0xefcdab89UL, SHA_UINT32_C(0xefcdab89),
0x98badcfeUL, SHA_UINT32_C(0x98badcfe),
0x10325476UL, SHA_UINT32_C(0x10325476),
0xc3d2e1f0UL SHA_UINT32_C(0xc3d2e1f0)
}; };
/* Hash constant words K for SHA-224 and SHA-256: */ /* Hash constant words K for SHA-224 and SHA-256: */
static const sha_word32 K256[64] = { static const sha_word32 K256[64] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, SHA_UINT32_C(0x428a2f98), SHA_UINT32_C(0x71374491),
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, SHA_UINT32_C(0xb5c0fbcf), SHA_UINT32_C(0xe9b5dba5),
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, SHA_UINT32_C(0x3956c25b), SHA_UINT32_C(0x59f111f1),
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, SHA_UINT32_C(0x923f82a4), SHA_UINT32_C(0xab1c5ed5),
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, SHA_UINT32_C(0xd807aa98), SHA_UINT32_C(0x12835b01),
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, SHA_UINT32_C(0x243185be), SHA_UINT32_C(0x550c7dc3),
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, SHA_UINT32_C(0x72be5d74), SHA_UINT32_C(0x80deb1fe),
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, SHA_UINT32_C(0x9bdc06a7), SHA_UINT32_C(0xc19bf174),
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, SHA_UINT32_C(0xe49b69c1), SHA_UINT32_C(0xefbe4786),
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, SHA_UINT32_C(0x0fc19dc6), SHA_UINT32_C(0x240ca1cc),
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, SHA_UINT32_C(0x2de92c6f), SHA_UINT32_C(0x4a7484aa),
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, SHA_UINT32_C(0x5cb0a9dc), SHA_UINT32_C(0x76f988da),
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, SHA_UINT32_C(0x983e5152), SHA_UINT32_C(0xa831c66d),
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, SHA_UINT32_C(0xb00327c8), SHA_UINT32_C(0xbf597fc7),
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, SHA_UINT32_C(0xc6e00bf3), SHA_UINT32_C(0xd5a79147),
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL SHA_UINT32_C(0x06ca6351), SHA_UINT32_C(0x14292967),
SHA_UINT32_C(0x27b70a85), SHA_UINT32_C(0x2e1b2138),
SHA_UINT32_C(0x4d2c6dfc), SHA_UINT32_C(0x53380d13),
SHA_UINT32_C(0x650a7354), SHA_UINT32_C(0x766a0abb),
SHA_UINT32_C(0x81c2c92e), SHA_UINT32_C(0x92722c85),
SHA_UINT32_C(0xa2bfe8a1), SHA_UINT32_C(0xa81a664b),
SHA_UINT32_C(0xc24b8b70), SHA_UINT32_C(0xc76c51a3),
SHA_UINT32_C(0xd192e819), SHA_UINT32_C(0xd6990624),
SHA_UINT32_C(0xf40e3585), SHA_UINT32_C(0x106aa070),
SHA_UINT32_C(0x19a4c116), SHA_UINT32_C(0x1e376c08),
SHA_UINT32_C(0x2748774c), SHA_UINT32_C(0x34b0bcb5),
SHA_UINT32_C(0x391c0cb3), SHA_UINT32_C(0x4ed8aa4a),
SHA_UINT32_C(0x5b9cca4f), SHA_UINT32_C(0x682e6ff3),
SHA_UINT32_C(0x748f82ee), SHA_UINT32_C(0x78a5636f),
SHA_UINT32_C(0x84c87814), SHA_UINT32_C(0x8cc70208),
SHA_UINT32_C(0x90befffa), SHA_UINT32_C(0xa4506ceb),
SHA_UINT32_C(0xbef9a3f7), SHA_UINT32_C(0xc67178f2)
}; };
/* Initial hash value H for SHA-224: */ /* Initial hash value H for SHA-224: */
static const sha_word32 sha224_initial_hash_value[8] = { static const sha_word32 sha224_initial_hash_value[8] = {
0xc1059ed8UL, SHA_UINT32_C(0xc1059ed8),
0x367cd507UL, SHA_UINT32_C(0x367cd507),
0x3070dd17UL, SHA_UINT32_C(0x3070dd17),
0xf70e5939UL, SHA_UINT32_C(0xf70e5939),
0xffc00b31UL, SHA_UINT32_C(0xffc00b31),
0x68581511UL, SHA_UINT32_C(0x68581511),
0x64f98fa7UL, SHA_UINT32_C(0x64f98fa7),
0xbefa4fa4UL SHA_UINT32_C(0xbefa4fa4)
}; };
/* Initial hash value H for SHA-256: */ /* Initial hash value H for SHA-256: */
static const sha_word32 sha256_initial_hash_value[8] = { static const sha_word32 sha256_initial_hash_value[8] = {
0x6a09e667UL, SHA_UINT32_C(0x6a09e667),
0xbb67ae85UL, SHA_UINT32_C(0xbb67ae85),
0x3c6ef372UL, SHA_UINT32_C(0x3c6ef372),
0xa54ff53aUL, SHA_UINT32_C(0xa54ff53a),
0x510e527fUL, SHA_UINT32_C(0x510e527f),
0x9b05688cUL, SHA_UINT32_C(0x9b05688c),
0x1f83d9abUL, SHA_UINT32_C(0x1f83d9ab),
0x5be0cd19UL SHA_UINT32_C(0x5be0cd19)
}; };
/* Hash constant words K for SHA-384 and SHA-512: */ /* Hash constant words K for SHA-384 and SHA-512: */
static const sha_word64 K512[80] = { static const sha_word64 K512[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, SHA_UINT64_C(0x428a2f98d728ae22), SHA_UINT64_C(0x7137449123ef65cd),
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, SHA_UINT64_C(0xb5c0fbcfec4d3b2f), SHA_UINT64_C(0xe9b5dba58189dbbc),
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, SHA_UINT64_C(0x3956c25bf348b538), SHA_UINT64_C(0x59f111f1b605d019),
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, SHA_UINT64_C(0x923f82a4af194f9b), SHA_UINT64_C(0xab1c5ed5da6d8118),
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, SHA_UINT64_C(0xd807aa98a3030242), SHA_UINT64_C(0x12835b0145706fbe),
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, SHA_UINT64_C(0x243185be4ee4b28c), SHA_UINT64_C(0x550c7dc3d5ffb4e2),
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, SHA_UINT64_C(0x72be5d74f27b896f), SHA_UINT64_C(0x80deb1fe3b1696b1),
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, SHA_UINT64_C(0x9bdc06a725c71235), SHA_UINT64_C(0xc19bf174cf692694),
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, SHA_UINT64_C(0xe49b69c19ef14ad2), SHA_UINT64_C(0xefbe4786384f25e3),
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, SHA_UINT64_C(0x0fc19dc68b8cd5b5), SHA_UINT64_C(0x240ca1cc77ac9c65),
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, SHA_UINT64_C(0x2de92c6f592b0275), SHA_UINT64_C(0x4a7484aa6ea6e483),
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, SHA_UINT64_C(0x5cb0a9dcbd41fbd4), SHA_UINT64_C(0x76f988da831153b5),
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, SHA_UINT64_C(0x983e5152ee66dfab), SHA_UINT64_C(0xa831c66d2db43210),
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, SHA_UINT64_C(0xb00327c898fb213f), SHA_UINT64_C(0xbf597fc7beef0ee4),
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, SHA_UINT64_C(0xc6e00bf33da88fc2), SHA_UINT64_C(0xd5a79147930aa725),
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, SHA_UINT64_C(0x06ca6351e003826f), SHA_UINT64_C(0x142929670a0e6e70),
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, SHA_UINT64_C(0x27b70a8546d22ffc), SHA_UINT64_C(0x2e1b21385c26c926),
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, SHA_UINT64_C(0x4d2c6dfc5ac42aed), SHA_UINT64_C(0x53380d139d95b3df),
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, SHA_UINT64_C(0x650a73548baf63de), SHA_UINT64_C(0x766a0abb3c77b2a8),
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, SHA_UINT64_C(0x81c2c92e47edaee6), SHA_UINT64_C(0x92722c851482353b),
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, SHA_UINT64_C(0xa2bfe8a14cf10364), SHA_UINT64_C(0xa81a664bbc423001),
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, SHA_UINT64_C(0xc24b8b70d0f89791), SHA_UINT64_C(0xc76c51a30654be30),
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, SHA_UINT64_C(0xd192e819d6ef5218), SHA_UINT64_C(0xd69906245565a910),
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, SHA_UINT64_C(0xf40e35855771202a), SHA_UINT64_C(0x106aa07032bbd1b8),
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, SHA_UINT64_C(0x19a4c116b8d2d0c8), SHA_UINT64_C(0x1e376c085141ab53),
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, SHA_UINT64_C(0x2748774cdf8eeb99), SHA_UINT64_C(0x34b0bcb5e19b48a8),
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, SHA_UINT64_C(0x391c0cb3c5c95a63), SHA_UINT64_C(0x4ed8aa4ae3418acb),
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, SHA_UINT64_C(0x5b9cca4f7763e373), SHA_UINT64_C(0x682e6ff3d6b2b8a3),
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, SHA_UINT64_C(0x748f82ee5defb2fc), SHA_UINT64_C(0x78a5636f43172f60),
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, SHA_UINT64_C(0x84c87814a1f0ab72), SHA_UINT64_C(0x8cc702081a6439ec),
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, SHA_UINT64_C(0x90befffa23631e28), SHA_UINT64_C(0xa4506cebde82bde9),
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, SHA_UINT64_C(0xbef9a3f7b2c67915), SHA_UINT64_C(0xc67178f2e372532b),
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, SHA_UINT64_C(0xca273eceea26619c), SHA_UINT64_C(0xd186b8c721c0c207),
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, SHA_UINT64_C(0xeada7dd6cde0eb1e), SHA_UINT64_C(0xf57d4f7fee6ed178),
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, SHA_UINT64_C(0x06f067aa72176fba), SHA_UINT64_C(0x0a637dc5a2c898a6),
0x113f9804bef90daeULL, 0x1b710b35131c471bULL, SHA_UINT64_C(0x113f9804bef90dae), SHA_UINT64_C(0x1b710b35131c471b),
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, SHA_UINT64_C(0x28db77f523047d84), SHA_UINT64_C(0x32caab7b40c72493),
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, SHA_UINT64_C(0x3c9ebe0a15c9bebc), SHA_UINT64_C(0x431d67c49c100d4c),
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, SHA_UINT64_C(0x4cc5d4becb3e42b6), SHA_UINT64_C(0x597f299cfc657e2a),
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL SHA_UINT64_C(0x5fcb6fab3ad6faec), SHA_UINT64_C(0x6c44198c4a475817)
}; };
/* Initial hash value H for SHA-384 */ /* Initial hash value H for SHA-384 */
static const sha_word64 sha384_initial_hash_value[8] = { static const sha_word64 sha384_initial_hash_value[8] = {
0xcbbb9d5dc1059ed8ULL, SHA_UINT64_C(0xcbbb9d5dc1059ed8),
0x629a292a367cd507ULL, SHA_UINT64_C(0x629a292a367cd507),
0x9159015a3070dd17ULL, SHA_UINT64_C(0x9159015a3070dd17),
0x152fecd8f70e5939ULL, SHA_UINT64_C(0x152fecd8f70e5939),
0x67332667ffc00b31ULL, SHA_UINT64_C(0x67332667ffc00b31),
0x8eb44a8768581511ULL, SHA_UINT64_C(0x8eb44a8768581511),
0xdb0c2e0d64f98fa7ULL, SHA_UINT64_C(0xdb0c2e0d64f98fa7),
0x47b5481dbefa4fa4ULL SHA_UINT64_C(0x47b5481dbefa4fa4)
}; };
/* Initial hash value H for SHA-512 */ /* Initial hash value H for SHA-512 */
static const sha_word64 sha512_initial_hash_value[8] = { static const sha_word64 sha512_initial_hash_value[8] = {
0x6a09e667f3bcc908ULL, SHA_UINT64_C(0x6a09e667f3bcc908),
0xbb67ae8584caa73bULL, SHA_UINT64_C(0xbb67ae8584caa73b),
0x3c6ef372fe94f82bULL, SHA_UINT64_C(0x3c6ef372fe94f82b),
0xa54ff53a5f1d36f1ULL, SHA_UINT64_C(0xa54ff53a5f1d36f1),
0x510e527fade682d1ULL, SHA_UINT64_C(0x510e527fade682d1),
0x9b05688c2b3e6c1fULL, SHA_UINT64_C(0x9b05688c2b3e6c1f),
0x1f83d9abfb41bd6bULL, SHA_UINT64_C(0x1f83d9abfb41bd6b),
0x5be0cd19137e2179ULL SHA_UINT64_C(0x5be0cd19137e2179)
}; };
/* /*