diff options
author | Lei Zhang <thestig@chromium.org> | 2017-04-03 16:48:53 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-04-04 00:14:05 +0000 |
commit | bb4a1bc09f92f781deefae5dc187fef21d6957fd (patch) | |
tree | 8da1748ec743998ab63546eb62ef0156ed91582c /core/fdrm/crypto | |
parent | 7b9d82312413a3cd2dc7422313bb634eaa687a87 (diff) | |
download | pdfium-bb4a1bc09f92f781deefae5dc187fef21d6957fd.tar.xz |
Drop C linkage in fx_crypt code.
Move as much as possible into anonymous namespaces.
BUG=pdfium:388
Change-Id: I7cca042736372c52105ed2ef1f9945a8d72c3db2
Reviewed-on: https://pdfium-review.googlesource.com/3557
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fdrm/crypto')
-rw-r--r-- | core/fdrm/crypto/fx_crypt.cpp | 122 | ||||
-rw-r--r-- | core/fdrm/crypto/fx_crypt.h | 7 | ||||
-rw-r--r-- | core/fdrm/crypto/fx_crypt_aes.cpp | 85 | ||||
-rw-r--r-- | core/fdrm/crypto/fx_crypt_sha.cpp | 539 |
4 files changed, 372 insertions, 381 deletions
diff --git a/core/fdrm/crypto/fx_crypt.cpp b/core/fdrm/crypto/fx_crypt.cpp index d1a1aae471..693b2ef1cf 100644 --- a/core/fdrm/crypto/fx_crypt.cpp +++ b/core/fdrm/crypto/fx_crypt.cpp @@ -6,60 +6,6 @@ #include "core/fdrm/crypto/fx_crypt.h" -#ifdef __cplusplus -extern "C" { -#endif - -void CRYPT_ArcFourSetup(CRYPT_rc4_context* s, - const uint8_t* key, - uint32_t length) { - int i, j, k, *m, a; - s->x = 0; - s->y = 0; - m = s->m; - for (i = 0; i < 256; i++) { - m[i] = i; - } - j = k = 0; - for (i = 0; i < 256; i++) { - a = m[i]; - j = (j + a + key[k]) & 0xFF; - m[i] = m[j]; - m[j] = a; - if (++k >= (int)length) { - k = 0; - } - } -} - -void CRYPT_ArcFourCrypt(CRYPT_rc4_context* s, - unsigned char* data, - uint32_t length) { - int i, x, y, *m, a, b; - x = s->x; - y = s->y; - m = s->m; - for (i = 0; i < (int)length; i++) { - x = (x + 1) & 0xFF; - a = m[x]; - y = (y + a) & 0xFF; - m[x] = b = m[y]; - m[y] = a; - data[i] ^= m[(a + b) & 0xFF]; - } - s->x = x; - s->y = y; -} - -void CRYPT_ArcFourCryptBlock(uint8_t* pData, - uint32_t size, - const uint8_t* key, - uint32_t keylen) { - CRYPT_rc4_context s; - CRYPT_ArcFourSetup(&s, key, keylen); - CRYPT_ArcFourCrypt(&s, pData, size); -} - #define GET_UINT32(n, b, i) \ { \ (n) = (uint32_t)((uint8_t*)b)[(i)] | \ @@ -75,6 +21,13 @@ void CRYPT_ArcFourCryptBlock(uint8_t* pData, (((uint8_t*)b)[(i) + 3]) = (uint8_t)(((n) >> 24) & 0xFF); \ } +namespace { + +const uint8_t md5_padding[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + void md5_process(CRYPT_md5_context* ctx, const uint8_t data[64]) { uint32_t A, B, C, D, X[16]; GET_UINT32(X[0], data, 0); @@ -181,6 +134,58 @@ void md5_process(CRYPT_md5_context* ctx, const uint8_t data[64]) { ctx->state[3] += D; } +} // namespace + +void CRYPT_ArcFourSetup(CRYPT_rc4_context* s, + const uint8_t* key, + uint32_t length) { + int i, j, k, *m, a; + s->x = 0; + s->y = 0; + m = s->m; + for (i = 0; i < 256; i++) { + m[i] = i; + } + j = k = 0; + for (i = 0; i < 256; i++) { + a = m[i]; + j = (j + a + key[k]) & 0xFF; + m[i] = m[j]; + m[j] = a; + if (++k >= (int)length) { + k = 0; + } + } +} + +void CRYPT_ArcFourCrypt(CRYPT_rc4_context* s, + unsigned char* data, + uint32_t length) { + int i, x, y, *m, a, b; + x = s->x; + y = s->y; + m = s->m; + for (i = 0; i < (int)length; i++) { + x = (x + 1) & 0xFF; + a = m[x]; + y = (y + a) & 0xFF; + m[x] = b = m[y]; + m[y] = a; + data[i] ^= m[(a + b) & 0xFF]; + } + s->x = x; + s->y = y; +} + +void CRYPT_ArcFourCryptBlock(uint8_t* pData, + uint32_t size, + const uint8_t* key, + uint32_t keylen) { + CRYPT_rc4_context s; + CRYPT_ArcFourSetup(&s, key, keylen); + CRYPT_ArcFourCrypt(&s, pData, size); +} + void CRYPT_MD5Start(CRYPT_md5_context* ctx) { ctx->total[0] = 0; ctx->total[1] = 0; @@ -220,11 +225,6 @@ void CRYPT_MD5Update(CRYPT_md5_context* ctx, } } -const uint8_t md5_padding[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - void CRYPT_MD5Finish(CRYPT_md5_context* ctx, uint8_t digest[16]) { uint32_t last, padn; uint8_t msglen[8]; @@ -248,7 +248,3 @@ void CRYPT_MD5Generate(const uint8_t* input, CRYPT_MD5Update(&ctx, input, length); CRYPT_MD5Finish(&ctx, digest); } - -#ifdef __cplusplus -}; -#endif diff --git a/core/fdrm/crypto/fx_crypt.h b/core/fdrm/crypto/fx_crypt.h index a498f4e19b..cfbe119ddb 100644 --- a/core/fdrm/crypto/fx_crypt.h +++ b/core/fdrm/crypto/fx_crypt.h @@ -9,10 +9,6 @@ #include "core/fxcrt/fx_system.h" -#ifdef __cplusplus -extern "C" { -#endif - typedef struct { int32_t x; int32_t y; @@ -109,8 +105,5 @@ void CRYPT_SHA512Finish(void* context, uint8_t digest[64]); void CRYPT_SHA512Generate(const uint8_t* data, uint32_t size, uint8_t digest[64]); -#ifdef __cplusplus -}; -#endif #endif // CORE_FDRM_CRYPTO_FX_CRYPT_H_ diff --git a/core/fdrm/crypto/fx_crypt_aes.cpp b/core/fdrm/crypto/fx_crypt_aes.cpp index e900dbd3e4..671dc65f31 100644 --- a/core/fdrm/crypto/fx_crypt_aes.cpp +++ b/core/fdrm/crypto/fx_crypt_aes.cpp @@ -6,9 +6,6 @@ #include "core/fdrm/crypto/fx_crypt.h" -#ifdef __cplusplus -extern "C" { -#endif #define MAX_NR 14 #define MAX_NK 8 #define MAX_NB 8 @@ -25,6 +22,7 @@ extern "C" { (cp)[1] = (value) >> 16; \ (cp)[0] = (value) >> 24; \ } while (0) + struct AESContext { unsigned int keysched[(MAX_NR + 1) * MAX_NB]; unsigned int invkeysched[(MAX_NR + 1) * MAX_NB]; @@ -33,7 +31,10 @@ struct AESContext { unsigned int iv[MAX_NB]; int Nb, Nr; }; -static const unsigned char Sbox[256] = { + +namespace { + +const unsigned char Sbox[256] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, @@ -56,7 +57,7 @@ static const unsigned char Sbox[256] = { 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}; -static const unsigned char Sboxinv[256] = { +const unsigned char Sboxinv[256] = { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, @@ -79,7 +80,7 @@ static const unsigned char Sboxinv[256] = { 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d}; -static const unsigned int E0[256] = { +const unsigned int E0[256] = { 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, @@ -124,7 +125,7 @@ static const unsigned int E0[256] = { 0x844242c6, 0xd06868b8, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a, }; -static const unsigned int E1[256] = { +const unsigned int E1[256] = { 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, @@ -169,7 +170,7 @@ static const unsigned int E1[256] = { 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616, }; -static const unsigned int E2[256] = { +const unsigned int E2[256] = { 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, @@ -214,7 +215,7 @@ static const unsigned int E2[256] = { 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16, }; -static const unsigned int E3[256] = { +const unsigned int E3[256] = { 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, @@ -259,7 +260,7 @@ static const unsigned int E3[256] = { 0x4242c684, 0x6868b8d0, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, }; -static const unsigned int D0[256] = { +const unsigned int D0[256] = { 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, @@ -304,7 +305,7 @@ static const unsigned int D0[256] = { 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742, }; -static const unsigned int D1[256] = { +const unsigned int D1[256] = { 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, @@ -349,7 +350,7 @@ static const unsigned int D1[256] = { 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857, }; -static const unsigned int D2[256] = { +const unsigned int D2[256] = { 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, @@ -394,7 +395,7 @@ static const unsigned int D2[256] = { 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8, }; -static const unsigned int D3[256] = { +const unsigned int D3[256] = { 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, @@ -461,9 +462,9 @@ static const unsigned int D3[256] = { (Sbox[(block[(i + C1) % Nb] >> 16) & 0xFF] << 16) | \ (Sbox[(block[(i + C2) % Nb] >> 8) & 0xFF] << 8) | \ (Sbox[(block[(i + C3) % Nb]) & 0xFF])) -static void aes_encrypt_nb_4(AESContext* ctx, unsigned int* block) { +void aes_encrypt_nb_4(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 1, C2 = 2, C3 = 3, Nb = 4; + const int C1 = 1, C2 = 2, C3 = 3, Nb = 4; unsigned int* keysched = ctx->keysched; unsigned int newstate[4]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -488,9 +489,9 @@ static void aes_encrypt_nb_4(AESContext* ctx, unsigned int* block) { MOVEWORD(3); ADD_ROUND_KEY_4; } -static void aes_encrypt_nb_6(AESContext* ctx, unsigned int* block) { +void aes_encrypt_nb_6(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 1, C2 = 2, C3 = 3, Nb = 6; + const int C1 = 1, C2 = 2, C3 = 3, Nb = 6; unsigned int* keysched = ctx->keysched; unsigned int newstate[6]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -523,9 +524,9 @@ static void aes_encrypt_nb_6(AESContext* ctx, unsigned int* block) { MOVEWORD(5); ADD_ROUND_KEY_6; } -static void aes_encrypt_nb_8(AESContext* ctx, unsigned int* block) { +void aes_encrypt_nb_8(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 1, C2 = 3, C3 = 4, Nb = 8; + const int C1 = 1, C2 = 3, C3 = 4, Nb = 8; unsigned int* keysched = ctx->keysched; unsigned int newstate[8]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -578,9 +579,9 @@ static void aes_encrypt_nb_8(AESContext* ctx, unsigned int* block) { (Sboxinv[(block[(i + C1) % Nb] >> 16) & 0xFF] << 16) | \ (Sboxinv[(block[(i + C2) % Nb] >> 8) & 0xFF] << 8) | \ (Sboxinv[(block[(i + C3) % Nb]) & 0xFF])) -static void aes_decrypt_nb_4(AESContext* ctx, unsigned int* block) { +void aes_decrypt_nb_4(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 4 - 1, C2 = 4 - 2, C3 = 4 - 3, Nb = 4; + const int C1 = 4 - 1, C2 = 4 - 2, C3 = 4 - 3, Nb = 4; unsigned int* keysched = ctx->invkeysched; unsigned int newstate[4]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -605,9 +606,9 @@ static void aes_decrypt_nb_4(AESContext* ctx, unsigned int* block) { MOVEWORD(3); ADD_ROUND_KEY_4; } -static void aes_decrypt_nb_6(AESContext* ctx, unsigned int* block) { +void aes_decrypt_nb_6(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 6 - 1, C2 = 6 - 2, C3 = 6 - 3, Nb = 6; + const int C1 = 6 - 1, C2 = 6 - 2, C3 = 6 - 3, Nb = 6; unsigned int* keysched = ctx->invkeysched; unsigned int newstate[6]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -640,9 +641,9 @@ static void aes_decrypt_nb_6(AESContext* ctx, unsigned int* block) { MOVEWORD(5); ADD_ROUND_KEY_6; } -static void aes_decrypt_nb_8(AESContext* ctx, unsigned int* block) { +void aes_decrypt_nb_8(AESContext* ctx, unsigned int* block) { int i; - static const int C1 = 8 - 1, C2 = 8 - 3, C3 = 8 - 4, Nb = 8; + const int C1 = 8 - 1, C2 = 8 - 3, C3 = 8 - 4, Nb = 8; unsigned int* keysched = ctx->invkeysched; unsigned int newstate[8]; for (i = 0; i < ctx->Nr - 1; i++) { @@ -685,10 +686,10 @@ static void aes_decrypt_nb_8(AESContext* ctx, unsigned int* block) { } #undef MAKEWORD #undef LASTWORD -static void aes_setup(AESContext* ctx, - int blocklen, - const unsigned char* key, - int keylen) { +void aes_setup(AESContext* ctx, + int blocklen, + const unsigned char* key, + int keylen) { int i, j, Nk, rconst; ASSERT(blocklen == 16 || blocklen == 24 || blocklen == 32); ASSERT(keylen == 16 || keylen == 24 || keylen == 32); @@ -752,13 +753,13 @@ static void aes_setup(AESContext* ctx, } } } -static void aes_decrypt(AESContext* ctx, unsigned int* block) { +void aes_decrypt(AESContext* ctx, unsigned int* block) { ctx->decrypt(ctx, block); } -static void aes_decrypt_cbc(unsigned char* dest, - const unsigned char* src, - int len, - AESContext* ctx) { +void aes_decrypt_cbc(unsigned char* dest, + const unsigned char* src, + int len, + AESContext* ctx) { unsigned int iv[4], x[4], ct[4]; int i; ASSERT((len & 15) == 0); @@ -778,13 +779,13 @@ static void aes_decrypt_cbc(unsigned char* dest, } memcpy(ctx->iv, iv, sizeof(iv)); } -static void aes_encrypt(AESContext* ctx, unsigned int* block) { +void aes_encrypt(AESContext* ctx, unsigned int* block) { ctx->encrypt(ctx, block); } -static void aes_encrypt_cbc(unsigned char* dest, - const unsigned char* src, - int len, - AESContext* ctx) { +void aes_encrypt_cbc(unsigned char* dest, + const unsigned char* src, + int len, + AESContext* ctx) { unsigned int iv[4]; int i; ASSERT((len & 15) == 0); @@ -803,6 +804,9 @@ static void aes_encrypt_cbc(unsigned char* dest, } memcpy(ctx->iv, iv, sizeof(iv)); } + +} // namespace + void CRYPT_AESSetKey(void* context, uint32_t blocklen, const uint8_t* key, @@ -828,6 +832,3 @@ void CRYPT_AESEncrypt(void* context, uint32_t len) { aes_encrypt_cbc(dest, src, len, (AESContext*)context); } -#ifdef __cplusplus -}; -#endif diff --git a/core/fdrm/crypto/fx_crypt_sha.cpp b/core/fdrm/crypto/fx_crypt_sha.cpp index f79b831277..e0643d547f 100644 --- a/core/fdrm/crypto/fx_crypt_sha.cpp +++ b/core/fdrm/crypto/fx_crypt_sha.cpp @@ -6,18 +6,89 @@ #include "core/fdrm/crypto/fx_crypt.h" -#ifdef __cplusplus -extern "C" { -#endif #define rol(x, y) (((x) << (y)) | (((unsigned int)x) >> (32 - y))) -static void SHA_Core_Init(unsigned int h[5]) { + +#define GET_UINT32(n, b, i) \ + { \ + (n) = ((uint32_t)(b)[(i)] << 24) | ((uint32_t)(b)[(i) + 1] << 16) | \ + ((uint32_t)(b)[(i) + 2] << 8) | ((uint32_t)(b)[(i) + 3]); \ + } +#define PUT_UINT32(n, b, i) \ + { \ + (b)[(i)] = (uint8_t)((n) >> 24); \ + (b)[(i) + 1] = (uint8_t)((n) >> 16); \ + (b)[(i) + 2] = (uint8_t)((n) >> 8); \ + (b)[(i) + 3] = (uint8_t)((n)); \ + } + +#define SHA384_F0(x, y, z) ((x & y) | (z & (x | y))) +#define SHA384_F1(x, y, z) (z ^ (x & (y ^ z))) +#define SHA384_SHR(x, n) (x >> n) +#define SHA384_ROTR(x, n) (SHA384_SHR(x, n) | x << (64 - n)) +#define SHA384_S0(x) (SHA384_ROTR(x, 1) ^ SHA384_ROTR(x, 8) ^ SHA384_SHR(x, 7)) +#define SHA384_S1(x) \ + (SHA384_ROTR(x, 19) ^ SHA384_ROTR(x, 61) ^ SHA384_SHR(x, 6)) +#define SHA384_S2(x) \ + (SHA384_ROTR(x, 28) ^ SHA384_ROTR(x, 34) ^ SHA384_ROTR(x, 39)) +#define SHA384_S3(x) \ + (SHA384_ROTR(x, 14) ^ SHA384_ROTR(x, 18) ^ SHA384_ROTR(x, 41)) +#define SHA384_P(a, b, c, d, e, f, g, h, x, K) \ + { \ + temp1 = h + SHA384_S3(e) + SHA384_F1(e, f, g) + K + x; \ + temp2 = SHA384_S2(a) + SHA384_F0(a, b, c); \ + d += temp1; \ + h = temp1 + temp2; \ + } +#define SHA384_R(t) \ + (W[t] = SHA384_S1(W[t - 2]) + W[t - 7] + SHA384_S0(W[t - 15]) + W[t - 16]) + +#define GET_FX_64WORD(n, b, i) \ + { \ + (n) = ((uint64_t)(b)[(i)] << 56) | ((uint64_t)(b)[(i) + 1] << 48) | \ + ((uint64_t)(b)[(i) + 2] << 40) | ((uint64_t)(b)[(i) + 3] << 32) | \ + ((uint64_t)(b)[(i) + 4] << 24) | ((uint64_t)(b)[(i) + 5] << 16) | \ + ((uint64_t)(b)[(i) + 6] << 8) | ((uint64_t)(b)[(i) + 7]); \ + } +#define PUT_UINT64(n, b, i) \ + { \ + (b)[(i)] = (uint8_t)((n) >> 56); \ + (b)[(i) + 1] = (uint8_t)((n) >> 48); \ + (b)[(i) + 2] = (uint8_t)((n) >> 40); \ + (b)[(i) + 3] = (uint8_t)((n) >> 32); \ + (b)[(i) + 4] = (uint8_t)((n) >> 24); \ + (b)[(i) + 5] = (uint8_t)((n) >> 16); \ + (b)[(i) + 6] = (uint8_t)((n) >> 8); \ + (b)[(i) + 7] = (uint8_t)((n)); \ + } + +#define SHR(x, n) ((x & 0xFFFFFFFF) >> n) +#define ROTR(x, n) (SHR(x, n) | (x << (32 - n))) +#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) +#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) +#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) +#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) +#define F0(x, y, z) ((x & y) | (z & (x | y))) +#define F1(x, y, z) (z ^ (x & (y ^ z))) +#define R(t) (W[t] = S1(W[t - 2]) + W[t - 7] + S0(W[t - 15]) + W[t - 16]) +#define P(a, b, c, d, e, f, g, h, x, K) \ + { \ + temp1 = h + S3(e) + F1(e, f, g) + K + x; \ + temp2 = S2(a) + F0(a, b, c); \ + d += temp1; \ + h = temp1 + temp2; \ + } + +namespace { + +void SHA_Core_Init(unsigned int h[5]) { h[0] = 0x67452301; h[1] = 0xefcdab89; h[2] = 0x98badcfe; h[3] = 0x10325476; h[4] = 0xc3d2e1f0; } -static void SHATransform(unsigned int* digest, unsigned int* block) { + +void SHATransform(unsigned int* digest, unsigned int* block) { unsigned int w[80]; unsigned int a, b, c, d, e; int t; @@ -73,110 +144,7 @@ static void SHATransform(unsigned int* digest, unsigned int* block) { digest[4] += e; } -void CRYPT_SHA1Start(CRYPT_sha1_context* s) { - SHA_Core_Init(s->h); - s->blkused = 0; - s->lenhi = s->lenlo = 0; -} - -void CRYPT_SHA1Update(CRYPT_sha1_context* s, - const uint8_t* data, - uint32_t size) { - unsigned char* q = (unsigned char*)data; - unsigned int wordblock[16]; - int len = size; - unsigned int lenw = len; - int i; - s->lenlo += lenw; - s->lenhi += (s->lenlo < lenw); - if (s->blkused && s->blkused + len < 64) { - memcpy(s->block + s->blkused, q, len); - s->blkused += len; - } else { - while (s->blkused + len >= 64) { - memcpy(s->block + s->blkused, q, 64 - s->blkused); - q += 64 - s->blkused; - len -= 64 - s->blkused; - for (i = 0; i < 16; i++) { - wordblock[i] = (((unsigned int)s->block[i * 4 + 0]) << 24) | - (((unsigned int)s->block[i * 4 + 1]) << 16) | - (((unsigned int)s->block[i * 4 + 2]) << 8) | - (((unsigned int)s->block[i * 4 + 3]) << 0); - } - SHATransform(s->h, wordblock); - s->blkused = 0; - } - memcpy(s->block, q, len); - s->blkused = len; - } -} - -void CRYPT_SHA1Finish(CRYPT_sha1_context* s, uint8_t digest[20]) { - int i; - int pad; - unsigned char c[64]; - unsigned int lenhi, lenlo; - if (s->blkused >= 56) { - pad = 56 + 64 - s->blkused; - } else { - pad = 56 - s->blkused; - } - lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3)); - lenlo = (s->lenlo << 3); - memset(c, 0, pad); - c[0] = 0x80; - CRYPT_SHA1Update(s, c, pad); - c[0] = (lenhi >> 24) & 0xFF; - c[1] = (lenhi >> 16) & 0xFF; - c[2] = (lenhi >> 8) & 0xFF; - c[3] = (lenhi >> 0) & 0xFF; - c[4] = (lenlo >> 24) & 0xFF; - c[5] = (lenlo >> 16) & 0xFF; - c[6] = (lenlo >> 8) & 0xFF; - c[7] = (lenlo >> 0) & 0xFF; - CRYPT_SHA1Update(s, c, 8); - for (i = 0; i < 5; i++) { - digest[i * 4] = (s->h[i] >> 24) & 0xFF; - digest[i * 4 + 1] = (s->h[i] >> 16) & 0xFF; - digest[i * 4 + 2] = (s->h[i] >> 8) & 0xFF; - digest[i * 4 + 3] = (s->h[i]) & 0xFF; - } -} -void CRYPT_SHA1Generate(const uint8_t* data, - uint32_t size, - uint8_t digest[20]) { - CRYPT_sha1_context s; - CRYPT_SHA1Start(&s); - CRYPT_SHA1Update(&s, data, size); - CRYPT_SHA1Finish(&s, digest); -} -#define GET_UINT32(n, b, i) \ - { \ - (n) = ((uint32_t)(b)[(i)] << 24) | ((uint32_t)(b)[(i) + 1] << 16) | \ - ((uint32_t)(b)[(i) + 2] << 8) | ((uint32_t)(b)[(i) + 3]); \ - } -#define PUT_UINT32(n, b, i) \ - { \ - (b)[(i)] = (uint8_t)((n) >> 24); \ - (b)[(i) + 1] = (uint8_t)((n) >> 16); \ - (b)[(i) + 2] = (uint8_t)((n) >> 8); \ - (b)[(i) + 3] = (uint8_t)((n)); \ - } - -void CRYPT_SHA256Start(CRYPT_sha256_context* ctx) { - ctx->total[0] = 0; - ctx->total[1] = 0; - ctx->state[0] = 0x6A09E667; - ctx->state[1] = 0xBB67AE85; - ctx->state[2] = 0x3C6EF372; - ctx->state[3] = 0xA54FF53A; - ctx->state[4] = 0x510E527F; - ctx->state[5] = 0x9B05688C; - ctx->state[6] = 0x1F83D9AB; - ctx->state[7] = 0x5BE0CD19; -} - -static void sha256_process(CRYPT_sha256_context* ctx, const uint8_t data[64]) { +void sha256_process(CRYPT_sha256_context* ctx, const uint8_t data[64]) { uint32_t temp1, temp2, W[64]; uint32_t A, B, C, D, E, F, G, H; GET_UINT32(W[0], data, 0); @@ -195,22 +163,6 @@ static void sha256_process(CRYPT_sha256_context* ctx, const uint8_t data[64]) { GET_UINT32(W[13], data, 52); GET_UINT32(W[14], data, 56); GET_UINT32(W[15], data, 60); -#define SHR(x, n) ((x & 0xFFFFFFFF) >> n) -#define ROTR(x, n) (SHR(x, n) | (x << (32 - n))) -#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) -#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) -#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) -#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) -#define F0(x, y, z) ((x & y) | (z & (x | y))) -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define R(t) (W[t] = S1(W[t - 2]) + W[t - 7] + S0(W[t - 15]) + W[t - 16]) -#define P(a, b, c, d, e, f, g, h, x, K) \ - { \ - temp1 = h + S3(e) + F1(e, f, g) + K + x; \ - temp2 = S2(a) + F0(a, b, c); \ - d += temp1; \ - h = temp1 + temp2; \ - } A = ctx->state[0]; B = ctx->state[1]; C = ctx->state[2]; @@ -293,131 +245,12 @@ static void sha256_process(CRYPT_sha256_context* ctx, const uint8_t data[64]) { ctx->state[7] += H; } -void CRYPT_SHA256Update(CRYPT_sha256_context* ctx, - const uint8_t* input, - uint32_t length) { - if (!length) - return; - - uint32_t left = ctx->total[0] & 0x3F; - uint32_t fill = 64 - left; - ctx->total[0] += length; - ctx->total[0] &= 0xFFFFFFFF; - if (ctx->total[0] < length) { - ctx->total[1]++; - } - if (left && length >= fill) { - memcpy((void*)(ctx->buffer + left), (void*)input, fill); - sha256_process(ctx, ctx->buffer); - length -= fill; - input += fill; - left = 0; - } - while (length >= 64) { - sha256_process(ctx, input); - length -= 64; - input += 64; - } - if (length) { - memcpy((void*)(ctx->buffer + left), (void*)input, length); - } -} - -static const uint8_t sha256_padding[64] = { +const uint8_t sha256_padding[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void CRYPT_SHA256Finish(CRYPT_sha256_context* ctx, uint8_t digest[32]) { - uint32_t last, padn; - uint32_t high, low; - uint8_t msglen[8]; - high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); - low = (ctx->total[0] << 3); - PUT_UINT32(high, msglen, 0); - PUT_UINT32(low, msglen, 4); - last = ctx->total[0] & 0x3F; - padn = (last < 56) ? (56 - last) : (120 - last); - CRYPT_SHA256Update(ctx, sha256_padding, padn); - CRYPT_SHA256Update(ctx, msglen, 8); - PUT_UINT32(ctx->state[0], digest, 0); - PUT_UINT32(ctx->state[1], digest, 4); - PUT_UINT32(ctx->state[2], digest, 8); - PUT_UINT32(ctx->state[3], digest, 12); - PUT_UINT32(ctx->state[4], digest, 16); - PUT_UINT32(ctx->state[5], digest, 20); - PUT_UINT32(ctx->state[6], digest, 24); - PUT_UINT32(ctx->state[7], digest, 28); -} - -void CRYPT_SHA256Generate(const uint8_t* data, - uint32_t size, - uint8_t digest[32]) { - CRYPT_sha256_context ctx; - CRYPT_SHA256Start(&ctx); - CRYPT_SHA256Update(&ctx, data, size); - CRYPT_SHA256Finish(&ctx, digest); -} - -uint64_t FX_ato64i(const char* str) { - ASSERT(str); - uint64_t ret = 0; - int len = (int)FXSYS_strlen(str); - len = len > 16 ? 16 : len; - for (int i = 0; i < len; ++i) { - if (i) { - ret <<= 4; - } - if (str[i] >= '0' && str[i] <= '9') { - ret |= (str[i] - '0') & 0xFF; - } else if (str[i] >= 'a' && str[i] <= 'f') { - ret |= (str[i] - 'a' + 10) & 0xFF; - } else if (str[i] >= 'A' && str[i] <= 'F') { - ret |= (str[i] - 'A' + 10) & 0xFF; - } else { - ASSERT(false); - } - } - return ret; -} - -void CRYPT_SHA384Start(CRYPT_sha384_context* ctx) { - if (!ctx) - return; - - memset(ctx, 0, sizeof(CRYPT_sha384_context)); - ctx->state[0] = FX_ato64i("cbbb9d5dc1059ed8"); - ctx->state[1] = FX_ato64i("629a292a367cd507"); - ctx->state[2] = FX_ato64i("9159015a3070dd17"); - ctx->state[3] = FX_ato64i("152fecd8f70e5939"); - ctx->state[4] = FX_ato64i("67332667ffc00b31"); - ctx->state[5] = FX_ato64i("8eb44a8768581511"); - ctx->state[6] = FX_ato64i("db0c2e0d64f98fa7"); - ctx->state[7] = FX_ato64i("47b5481dbefa4fa4"); -} - -#define SHA384_F0(x, y, z) ((x & y) | (z & (x | y))) -#define SHA384_F1(x, y, z) (z ^ (x & (y ^ z))) -#define SHA384_SHR(x, n) (x >> n) -#define SHA384_ROTR(x, n) (SHA384_SHR(x, n) | x << (64 - n)) -#define SHA384_S0(x) (SHA384_ROTR(x, 1) ^ SHA384_ROTR(x, 8) ^ SHA384_SHR(x, 7)) -#define SHA384_S1(x) \ - (SHA384_ROTR(x, 19) ^ SHA384_ROTR(x, 61) ^ SHA384_SHR(x, 6)) -#define SHA384_S2(x) \ - (SHA384_ROTR(x, 28) ^ SHA384_ROTR(x, 34) ^ SHA384_ROTR(x, 39)) -#define SHA384_S3(x) \ - (SHA384_ROTR(x, 14) ^ SHA384_ROTR(x, 18) ^ SHA384_ROTR(x, 41)) -#define SHA384_P(a, b, c, d, e, f, g, h, x, K) \ - { \ - temp1 = h + SHA384_S3(e) + SHA384_F1(e, f, g) + K + x; \ - temp2 = SHA384_S2(a) + SHA384_F0(a, b, c); \ - d += temp1; \ - h = temp1 + temp2; \ - } -#define SHA384_R(t) \ - (W[t] = SHA384_S1(W[t - 2]) + W[t - 7] + SHA384_S0(W[t - 15]) + W[t - 16]) - -static const uint8_t sha384_padding[128] = { +const uint8_t sha384_padding[128] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -426,7 +259,7 @@ static const uint8_t sha384_padding[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -static const char* const constants[] = { +const char* const constants[] = { "428a2f98d728ae22", "7137449123ef65cd", "b5c0fbcfec4d3b2f", "e9b5dba58189dbbc", "3956c25bf348b538", "59f111f1b605d019", "923f82a4af194f9b", "ab1c5ed5da6d8118", "d807aa98a3030242", @@ -455,26 +288,30 @@ static const char* const constants[] = { "431d67c49c100d4c", "4cc5d4becb3e42b6", "597f299cfc657e2a", "5fcb6fab3ad6faec", "6c44198c4a475817", }; -#define GET_FX_64WORD(n, b, i) \ - { \ - (n) = ((uint64_t)(b)[(i)] << 56) | ((uint64_t)(b)[(i) + 1] << 48) | \ - ((uint64_t)(b)[(i) + 2] << 40) | ((uint64_t)(b)[(i) + 3] << 32) | \ - ((uint64_t)(b)[(i) + 4] << 24) | ((uint64_t)(b)[(i) + 5] << 16) | \ - ((uint64_t)(b)[(i) + 6] << 8) | ((uint64_t)(b)[(i) + 7]); \ - } -#define PUT_UINT64(n, b, i) \ - { \ - (b)[(i)] = (uint8_t)((n) >> 56); \ - (b)[(i) + 1] = (uint8_t)((n) >> 48); \ - (b)[(i) + 2] = (uint8_t)((n) >> 40); \ - (b)[(i) + 3] = (uint8_t)((n) >> 32); \ - (b)[(i) + 4] = (uint8_t)((n) >> 24); \ - (b)[(i) + 5] = (uint8_t)((n) >> 16); \ - (b)[(i) + 6] = (uint8_t)((n) >> 8); \ - (b)[(i) + 7] = (uint8_t)((n)); \ + +uint64_t FX_ato64i(const char* str) { + ASSERT(str); + uint64_t ret = 0; + int len = (int)FXSYS_strlen(str); + len = len > 16 ? 16 : len; + for (int i = 0; i < len; ++i) { + if (i) { + ret <<= 4; + } + if (str[i] >= '0' && str[i] <= '9') { + ret |= (str[i] - '0') & 0xFF; + } else if (str[i] >= 'a' && str[i] <= 'f') { + ret |= (str[i] - 'a' + 10) & 0xFF; + } else if (str[i] >= 'A' && str[i] <= 'F') { + ret |= (str[i] - 'A' + 10) & 0xFF; + } else { + ASSERT(false); + } } + return ret; +} -static void sha384_process(CRYPT_sha384_context* ctx, const uint8_t data[128]) { +void sha384_process(CRYPT_sha384_context* ctx, const uint8_t data[128]) { uint64_t temp1, temp2; uint64_t A, B, C, D, E, F, G, H; uint64_t W[80]; @@ -542,6 +379,174 @@ static void sha384_process(CRYPT_sha384_context* ctx, const uint8_t data[128]) { ctx->state[7] += H; } +} // namespace + +void CRYPT_SHA1Start(CRYPT_sha1_context* s) { + SHA_Core_Init(s->h); + s->blkused = 0; + s->lenhi = s->lenlo = 0; +} + +void CRYPT_SHA1Update(CRYPT_sha1_context* s, + const uint8_t* data, + uint32_t size) { + unsigned char* q = (unsigned char*)data; + unsigned int wordblock[16]; + int len = size; + unsigned int lenw = len; + int i; + s->lenlo += lenw; + s->lenhi += (s->lenlo < lenw); + if (s->blkused && s->blkused + len < 64) { + memcpy(s->block + s->blkused, q, len); + s->blkused += len; + } else { + while (s->blkused + len >= 64) { + memcpy(s->block + s->blkused, q, 64 - s->blkused); + q += 64 - s->blkused; + len -= 64 - s->blkused; + for (i = 0; i < 16; i++) { + wordblock[i] = (((unsigned int)s->block[i * 4 + 0]) << 24) | + (((unsigned int)s->block[i * 4 + 1]) << 16) | + (((unsigned int)s->block[i * 4 + 2]) << 8) | + (((unsigned int)s->block[i * 4 + 3]) << 0); + } + SHATransform(s->h, wordblock); + s->blkused = 0; + } + memcpy(s->block, q, len); + s->blkused = len; + } +} + +void CRYPT_SHA1Finish(CRYPT_sha1_context* s, uint8_t digest[20]) { + int i; + int pad; + unsigned char c[64]; + unsigned int lenhi, lenlo; + if (s->blkused >= 56) { + pad = 56 + 64 - s->blkused; + } else { + pad = 56 - s->blkused; + } + lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3)); + lenlo = (s->lenlo << 3); + memset(c, 0, pad); + c[0] = 0x80; + CRYPT_SHA1Update(s, c, pad); + c[0] = (lenhi >> 24) & 0xFF; + c[1] = (lenhi >> 16) & 0xFF; + c[2] = (lenhi >> 8) & 0xFF; + c[3] = (lenhi >> 0) & 0xFF; + c[4] = (lenlo >> 24) & 0xFF; + c[5] = (lenlo >> 16) & 0xFF; + c[6] = (lenlo >> 8) & 0xFF; + c[7] = (lenlo >> 0) & 0xFF; + CRYPT_SHA1Update(s, c, 8); + for (i = 0; i < 5; i++) { + digest[i * 4] = (s->h[i] >> 24) & 0xFF; + digest[i * 4 + 1] = (s->h[i] >> 16) & 0xFF; + digest[i * 4 + 2] = (s->h[i] >> 8) & 0xFF; + digest[i * 4 + 3] = (s->h[i]) & 0xFF; + } +} +void CRYPT_SHA1Generate(const uint8_t* data, + uint32_t size, + uint8_t digest[20]) { + CRYPT_sha1_context s; + CRYPT_SHA1Start(&s); + CRYPT_SHA1Update(&s, data, size); + CRYPT_SHA1Finish(&s, digest); +} +void CRYPT_SHA256Start(CRYPT_sha256_context* ctx) { + ctx->total[0] = 0; + ctx->total[1] = 0; + ctx->state[0] = 0x6A09E667; + ctx->state[1] = 0xBB67AE85; + ctx->state[2] = 0x3C6EF372; + ctx->state[3] = 0xA54FF53A; + ctx->state[4] = 0x510E527F; + ctx->state[5] = 0x9B05688C; + ctx->state[6] = 0x1F83D9AB; + ctx->state[7] = 0x5BE0CD19; +} + +void CRYPT_SHA256Update(CRYPT_sha256_context* ctx, + const uint8_t* input, + uint32_t length) { + if (!length) + return; + + uint32_t left = ctx->total[0] & 0x3F; + uint32_t fill = 64 - left; + ctx->total[0] += length; + ctx->total[0] &= 0xFFFFFFFF; + if (ctx->total[0] < length) { + ctx->total[1]++; + } + if (left && length >= fill) { + memcpy((void*)(ctx->buffer + left), (void*)input, fill); + sha256_process(ctx, ctx->buffer); + length -= fill; + input += fill; + left = 0; + } + while (length >= 64) { + sha256_process(ctx, input); + length -= 64; + input += 64; + } + if (length) { + memcpy((void*)(ctx->buffer + left), (void*)input, length); + } +} + +void CRYPT_SHA256Finish(CRYPT_sha256_context* ctx, uint8_t digest[32]) { + uint32_t last, padn; + uint32_t high, low; + uint8_t msglen[8]; + high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); + PUT_UINT32(high, msglen, 0); + PUT_UINT32(low, msglen, 4); + last = ctx->total[0] & 0x3F; + padn = (last < 56) ? (56 - last) : (120 - last); + CRYPT_SHA256Update(ctx, sha256_padding, padn); + CRYPT_SHA256Update(ctx, msglen, 8); + PUT_UINT32(ctx->state[0], digest, 0); + PUT_UINT32(ctx->state[1], digest, 4); + PUT_UINT32(ctx->state[2], digest, 8); + PUT_UINT32(ctx->state[3], digest, 12); + PUT_UINT32(ctx->state[4], digest, 16); + PUT_UINT32(ctx->state[5], digest, 20); + PUT_UINT32(ctx->state[6], digest, 24); + PUT_UINT32(ctx->state[7], digest, 28); +} + +void CRYPT_SHA256Generate(const uint8_t* data, + uint32_t size, + uint8_t digest[32]) { + CRYPT_sha256_context ctx; + CRYPT_SHA256Start(&ctx); + CRYPT_SHA256Update(&ctx, data, size); + CRYPT_SHA256Finish(&ctx, digest); +} + +void CRYPT_SHA384Start(CRYPT_sha384_context* ctx) { + if (!ctx) + return; + + memset(ctx, 0, sizeof(CRYPT_sha384_context)); + ctx->state[0] = FX_ato64i("cbbb9d5dc1059ed8"); + ctx->state[1] = FX_ato64i("629a292a367cd507"); + ctx->state[2] = FX_ato64i("9159015a3070dd17"); + ctx->state[3] = FX_ato64i("152fecd8f70e5939"); + ctx->state[4] = FX_ato64i("67332667ffc00b31"); + ctx->state[5] = FX_ato64i("8eb44a8768581511"); + ctx->state[6] = FX_ato64i("db0c2e0d64f98fa7"); + ctx->state[7] = FX_ato64i("47b5481dbefa4fa4"); +} + void CRYPT_SHA384Update(CRYPT_sha384_context* ctx, const uint8_t* input, uint32_t length) { @@ -655,7 +660,3 @@ void CRYPT_SHA512Generate(const uint8_t* data, CRYPT_SHA512Update(&context, data, size); CRYPT_SHA512Finish(&context, digest); } - -#ifdef __cplusplus -}; -#endif |