mirror of
https://github.com/FirebirdSQL/firebird.git
synced 2025-01-23 06:03:02 +01:00
SHA2 Refactoring
This commit is contained in:
parent
b0c5f20341
commit
40bc3c37d2
@ -112,18 +112,16 @@
|
|||||||
|
|
||||||
#define SHA256_EXP(a, b, c, d, e, f, g, h, j) \
|
#define SHA256_EXP(a, b, c, d, e, f, g, h, j) \
|
||||||
{ \
|
{ \
|
||||||
t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
|
const uint32 t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) + sha256_k[j] + w[j]; \
|
||||||
+ sha256_k[j] + w[j]; \
|
const uint32 t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
|
||||||
t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
|
|
||||||
wv[d] += t1; \
|
wv[d] += t1; \
|
||||||
wv[h] = t1 + t2; \
|
wv[h] = t1 + t2; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \
|
#define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \
|
||||||
{ \
|
{ \
|
||||||
t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
|
const uint64 t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) + sha512_k[j] + w[j]; \
|
||||||
+ sha512_k[j] + w[j]; \
|
const uint64 t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
|
||||||
t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
|
|
||||||
wv[d] += t1; \
|
wv[d] += t1; \
|
||||||
wv[h] = t1 + t2; \
|
wv[h] = t1 + t2; \
|
||||||
}
|
}
|
||||||
@ -131,27 +129,27 @@
|
|||||||
namespace Firebird {
|
namespace Firebird {
|
||||||
|
|
||||||
|
|
||||||
sha2_base::uint32 sha224_h0[8] =
|
sha2_types::uint32 sha224_h0[8] =
|
||||||
{0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
|
{0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
|
||||||
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4};
|
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4};
|
||||||
|
|
||||||
sha2_base::uint32 sha256_h0[8] =
|
sha2_types::uint32 sha256_h0[8] =
|
||||||
{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
||||||
|
|
||||||
sha2_base::uint64 sha384_h0[8] =
|
sha2_types::uint64 sha384_h0[8] =
|
||||||
{0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
|
{0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
|
||||||
0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
|
0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
|
||||||
0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
|
0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
|
||||||
0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
|
0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
|
||||||
|
|
||||||
sha2_base::uint64 sha512_h0[8] =
|
sha2_types::uint64 sha512_h0[8] =
|
||||||
{0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
|
{0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
|
||||||
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
|
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
|
||||||
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
|
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
|
||||||
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
|
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
|
||||||
|
|
||||||
sha2_base::uint32 sha256_k[64] =
|
sha2_types::uint32 sha256_k[64] =
|
||||||
{0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
{0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||||
@ -169,7 +167,7 @@ sha2_base::uint32 sha256_k[64] =
|
|||||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
||||||
|
|
||||||
sha2_base::uint64 sha512_k[80] =
|
sha2_types::uint64 sha512_k[80] =
|
||||||
{0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
|
{0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
|
||||||
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
||||||
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
|
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
|
||||||
@ -211,63 +209,42 @@ sha2_base::uint64 sha512_k[80] =
|
|||||||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
|
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
|
||||||
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
|
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
|
||||||
|
|
||||||
/* sha2_base */
|
/* SHA-256 context structure */
|
||||||
|
|
||||||
void sha2_base::getHash(unsigned char *digest)
|
void sha256_ctx::transf(const unsigned char* message,
|
||||||
{
|
|
||||||
sha_final(digest);
|
|
||||||
reset();
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifndef NIST_COMPLIANCY_TESTS
|
|
||||||
void sha2_base::getHash(UCharBuffer& h)
|
|
||||||
{
|
|
||||||
sha_final(h.getBuffer(get_DigestSize()));
|
|
||||||
reset();
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* SHA-256 functions */
|
|
||||||
|
|
||||||
sha256::sha256() : sha2_base()
|
|
||||||
{
|
|
||||||
sha256_init(&ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sha256::sha256_transf(sha256_ctx * ctx, const unsigned char *message,
|
|
||||||
unsigned int block_nb)
|
unsigned int block_nb)
|
||||||
{
|
{
|
||||||
|
typedef sha2_types::uint32 uint32;
|
||||||
|
|
||||||
|
sha256_ctx* const ctx=this;
|
||||||
|
|
||||||
uint32 w[64];
|
uint32 w[64];
|
||||||
uint32 wv[8];
|
uint32 wv[8];
|
||||||
uint32 t1, t2;
|
|
||||||
const unsigned char *sub_block;
|
for (int i = 0; i < (int) block_nb; i++)
|
||||||
int i;
|
{
|
||||||
|
const unsigned char* const sub_block = message + (i << 6);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
int j;
|
for (int j = 0; j < 16; j++)
|
||||||
#endif
|
{
|
||||||
|
|
||||||
for (i = 0; i < (int) block_nb; i++) {
|
|
||||||
sub_block = message + (i << 6);
|
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
|
||||||
for (j = 0; j < 16; j++) {
|
|
||||||
PACK32(&sub_block[j << 2], &w[j]);
|
PACK32(&sub_block[j << 2], &w[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 16; j < 64; j++) {
|
for (int j = 16; j < 64; j++)
|
||||||
|
{
|
||||||
SHA256_SCR(j);
|
SHA256_SCR(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
wv[j] = ctx->h[j];
|
wv[j] = ctx->h[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 64; j++) {
|
for (int j = 0; j < 64; j++)
|
||||||
t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
|
{
|
||||||
+ sha256_k[j] + w[j];
|
const uint32 t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j];
|
||||||
t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
|
const uint32 t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
|
||||||
wv[7] = wv[6];
|
wv[7] = wv[6];
|
||||||
wv[6] = wv[5];
|
wv[6] = wv[5];
|
||||||
wv[5] = wv[4];
|
wv[5] = wv[4];
|
||||||
@ -278,7 +255,8 @@ void sha256::sha256_transf(sha256_ctx * ctx, const unsigned char *message,
|
|||||||
wv[0] = t1 + t2;
|
wv[0] = t1 + t2;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
ctx->h[j] += wv[j];
|
ctx->h[j] += wv[j];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -350,11 +328,13 @@ void sha256::sha256_transf(sha256_ctx * ctx, const unsigned char *message,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha256::sha256_init(sha256_ctx * ctx)
|
/* SHA-256 functions */
|
||||||
|
|
||||||
|
void sha256_traits::sha_init(sha256_ctx* ctx)
|
||||||
{
|
{
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
int i;
|
for (int i = 0; i < 8; i++)
|
||||||
for (i = 0; i < 8; i++) {
|
{
|
||||||
ctx->h[i] = sha256_h0[i];
|
ctx->h[i] = sha256_h0[i];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -368,64 +348,52 @@ void sha256::sha256_init(sha256_ctx * ctx)
|
|||||||
ctx->tot_len = 0;
|
ctx->tot_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha256::sha256_update(sha256_ctx * ctx, const unsigned char *message,
|
void sha256_traits::sha_update(sha256_ctx* ctx, const unsigned char* message,
|
||||||
unsigned int len)
|
unsigned int len)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len;
|
||||||
unsigned int new_len, rem_len, tmp_len;
|
unsigned int rem_len = len < tmp_len ? len : tmp_len;
|
||||||
const unsigned char *shifted_message;
|
|
||||||
|
|
||||||
tmp_len = SHA256_BLOCK_SIZE - ctx->len;
|
|
||||||
rem_len = len < tmp_len ? len : tmp_len;
|
|
||||||
|
|
||||||
memcpy(&ctx->block[ctx->len], message, rem_len);
|
memcpy(&ctx->block[ctx->len], message, rem_len);
|
||||||
|
|
||||||
if (ctx->len + len < SHA256_BLOCK_SIZE) {
|
if (ctx->len + len < SHA256_BLOCK_SIZE)
|
||||||
|
{
|
||||||
ctx->len += len;
|
ctx->len += len;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_len = len - rem_len;
|
const unsigned int new_len = len - rem_len;
|
||||||
block_nb = new_len / SHA256_BLOCK_SIZE;
|
const unsigned int block_nb = new_len / SHA256_BLOCK_SIZE;
|
||||||
|
|
||||||
shifted_message = message + rem_len;
|
const unsigned char* const shifted_message = message + rem_len;
|
||||||
|
|
||||||
sha256_transf(ctx, ctx->block, 1);
|
ctx->transf(ctx->block, 1);
|
||||||
sha256_transf(ctx, shifted_message, block_nb);
|
ctx->transf(shifted_message, block_nb);
|
||||||
|
|
||||||
rem_len = new_len % SHA256_BLOCK_SIZE;
|
rem_len = new_len % SHA256_BLOCK_SIZE;
|
||||||
|
|
||||||
memcpy(ctx->block, &shifted_message[block_nb << 6],
|
memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len);
|
||||||
rem_len);
|
|
||||||
|
|
||||||
ctx->len = rem_len;
|
ctx->len = rem_len;
|
||||||
ctx->tot_len += (block_nb + 1) << 6;
|
ctx->tot_len += (block_nb + 1) << 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha256::sha256_final(sha256_ctx * ctx, unsigned char *digest)
|
void sha256_traits::sha_final(sha256_ctx* ctx, unsigned char* digest)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) < (ctx->len % SHA256_BLOCK_SIZE)));
|
||||||
unsigned int pm_len;
|
|
||||||
unsigned int len_b;
|
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
const unsigned int len_b = (ctx->tot_len + ctx->len) << 3;
|
||||||
int i;
|
const unsigned int pm_len = block_nb << 6;
|
||||||
#endif
|
|
||||||
|
|
||||||
block_nb = (1 + ((SHA256_BLOCK_SIZE - 9)
|
|
||||||
< (ctx->len % SHA256_BLOCK_SIZE)));
|
|
||||||
|
|
||||||
len_b = (ctx->tot_len + ctx->len) << 3;
|
|
||||||
pm_len = block_nb << 6;
|
|
||||||
|
|
||||||
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
||||||
ctx->block[ctx->len] = 0x80;
|
ctx->block[ctx->len] = 0x80;
|
||||||
UNPACK32(len_b, ctx->block + pm_len - 4);
|
UNPACK32(len_b, ctx->block + pm_len - 4);
|
||||||
|
|
||||||
sha256_transf(ctx, ctx->block, block_nb);
|
ctx->transf(ctx->block, block_nb);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
for (i = 0 ; i < 8; i++) {
|
for (int i = 0 ; i < 8; i++)
|
||||||
|
{
|
||||||
UNPACK32(ctx->h[i], &digest[i << 2]);
|
UNPACK32(ctx->h[i], &digest[i << 2]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -440,42 +408,42 @@ void sha256::sha256_final(sha256_ctx * ctx, unsigned char *digest)
|
|||||||
#endif /* !UNROLL_LOOPS */
|
#endif /* !UNROLL_LOOPS */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SHA-512 functions */
|
/* SHA-512 context structure */
|
||||||
|
|
||||||
sha512::sha512() : sha2_base()
|
void sha512_ctx::transf(const unsigned char* message,
|
||||||
{
|
|
||||||
sha512_init(&ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sha512::sha512_transf(sha512_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int block_nb)
|
unsigned int block_nb)
|
||||||
{
|
{
|
||||||
|
typedef sha2_types::uint64 uint64;
|
||||||
|
|
||||||
|
sha512_ctx* const ctx=this;
|
||||||
|
|
||||||
uint64 w[80];
|
uint64 w[80];
|
||||||
uint64 wv[8];
|
uint64 wv[8];
|
||||||
uint64 t1, t2;
|
|
||||||
const unsigned char *sub_block;
|
|
||||||
int i, j;
|
|
||||||
|
|
||||||
for (i = 0; i < (int) block_nb; i++) {
|
for (int i = 0; i < (int) block_nb; i++)
|
||||||
sub_block = message + (i << 7);
|
{
|
||||||
|
const unsigned char* const sub_block = message + (i << 7);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
for (j = 0; j < 16; j++) {
|
for (int j = 0; j < 16; j++)
|
||||||
|
{
|
||||||
PACK64(&sub_block[j << 3], &w[j]);
|
PACK64(&sub_block[j << 3], &w[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 16; j < 80; j++) {
|
for (int j = 16; j < 80; j++)
|
||||||
|
{
|
||||||
SHA512_SCR(j);
|
SHA512_SCR(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
wv[j] = ctx->h[j];
|
wv[j] = ctx->h[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 80; j++) {
|
for (int j = 0; j < 80; j++)
|
||||||
t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
|
{
|
||||||
+ sha512_k[j] + w[j];
|
const uint64 t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha512_k[j] + w[j];
|
||||||
t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
|
const uint64 t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
|
||||||
wv[7] = wv[6];
|
wv[7] = wv[6];
|
||||||
wv[6] = wv[5];
|
wv[6] = wv[5];
|
||||||
wv[5] = wv[4];
|
wv[5] = wv[4];
|
||||||
@ -486,7 +454,8 @@ void sha512::sha512_transf(sha512_ctx *ctx, const unsigned char *message,
|
|||||||
wv[0] = t1 + t2;
|
wv[0] = t1 + t2;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < 8; j++) {
|
for (int j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
ctx->h[j] += wv[j];
|
ctx->h[j] += wv[j];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -521,9 +490,10 @@ void sha512::sha512_transf(sha512_ctx *ctx, const unsigned char *message,
|
|||||||
wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
|
wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
|
||||||
wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
|
wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
|
||||||
|
|
||||||
j = 0;
|
int j = 0;
|
||||||
|
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
SHA512_EXP(0,1,2,3,4,5,6,7,j); j++;
|
SHA512_EXP(0,1,2,3,4,5,6,7,j); j++;
|
||||||
SHA512_EXP(7,0,1,2,3,4,5,6,j); j++;
|
SHA512_EXP(7,0,1,2,3,4,5,6,j); j++;
|
||||||
SHA512_EXP(6,7,0,1,2,3,4,5,j); j++;
|
SHA512_EXP(6,7,0,1,2,3,4,5,j); j++;
|
||||||
@ -542,11 +512,13 @@ void sha512::sha512_transf(sha512_ctx *ctx, const unsigned char *message,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha512::sha512_init(sha512_ctx *ctx)
|
/* SHA-512 functions */
|
||||||
|
|
||||||
|
void sha512_traits::sha_init(sha_ctx* ctx)
|
||||||
{
|
{
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
int i;
|
for (int i = 0; i < 8; i++)
|
||||||
for (i = 0; i < 8; i++) {
|
{
|
||||||
ctx->h[i] = sha512_h0[i];
|
ctx->h[i] = sha512_h0[i];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -560,64 +532,52 @@ void sha512::sha512_init(sha512_ctx *ctx)
|
|||||||
ctx->tot_len = 0;
|
ctx->tot_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha512::sha512_update(sha512_ctx *ctx, const unsigned char *message,
|
void sha512_traits::sha_update(sha_ctx* ctx, const unsigned char* message,
|
||||||
unsigned int len)
|
unsigned int len)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int tmp_len = SHA512_BLOCK_SIZE - ctx->len;
|
||||||
unsigned int new_len, rem_len, tmp_len;
|
unsigned int rem_len = len < tmp_len ? len : tmp_len;
|
||||||
const unsigned char *shifted_message;
|
|
||||||
|
|
||||||
tmp_len = SHA512_BLOCK_SIZE - ctx->len;
|
|
||||||
rem_len = len < tmp_len ? len : tmp_len;
|
|
||||||
|
|
||||||
memcpy(&ctx->block[ctx->len], message, rem_len);
|
memcpy(&ctx->block[ctx->len], message, rem_len);
|
||||||
|
|
||||||
if (ctx->len + len < SHA512_BLOCK_SIZE) {
|
if (ctx->len + len < SHA512_BLOCK_SIZE)
|
||||||
|
{
|
||||||
ctx->len += len;
|
ctx->len += len;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_len = len - rem_len;
|
const unsigned int new_len = len - rem_len;
|
||||||
block_nb = new_len / SHA512_BLOCK_SIZE;
|
const unsigned int block_nb = new_len / SHA512_BLOCK_SIZE;
|
||||||
|
|
||||||
shifted_message = message + rem_len;
|
const unsigned char* const shifted_message = message + rem_len;
|
||||||
|
|
||||||
sha512_transf(ctx, ctx->block, 1);
|
ctx->transf(ctx->block, 1);
|
||||||
sha512_transf(ctx, shifted_message, block_nb);
|
ctx->transf(shifted_message, block_nb);
|
||||||
|
|
||||||
rem_len = new_len % SHA512_BLOCK_SIZE;
|
rem_len = new_len % SHA512_BLOCK_SIZE;
|
||||||
|
|
||||||
memcpy(ctx->block, &shifted_message[block_nb << 7],
|
memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len);
|
||||||
rem_len);
|
|
||||||
|
|
||||||
ctx->len = rem_len;
|
ctx->len = rem_len;
|
||||||
ctx->tot_len += (block_nb + 1) << 7;
|
ctx->tot_len += (block_nb + 1) << 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha512::sha512_final(sha512_ctx *ctx, unsigned char *digest)
|
void sha512_traits::sha_final(sha_ctx* ctx, unsigned char* digest)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) < (ctx->len % SHA512_BLOCK_SIZE));
|
||||||
unsigned int pm_len;
|
|
||||||
unsigned int len_b;
|
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
const unsigned int len_b = (ctx->tot_len + ctx->len) << 3;
|
||||||
int i;
|
const unsigned int pm_len = block_nb << 7;
|
||||||
#endif
|
|
||||||
|
|
||||||
block_nb = 1 + ((SHA512_BLOCK_SIZE - 17)
|
|
||||||
< (ctx->len % SHA512_BLOCK_SIZE));
|
|
||||||
|
|
||||||
len_b = (ctx->tot_len + ctx->len) << 3;
|
|
||||||
pm_len = block_nb << 7;
|
|
||||||
|
|
||||||
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
||||||
ctx->block[ctx->len] = 0x80;
|
ctx->block[ctx->len] = 0x80;
|
||||||
UNPACK32(len_b, ctx->block + pm_len - 4);
|
UNPACK32(len_b, ctx->block + pm_len - 4);
|
||||||
|
|
||||||
sha512_transf(ctx, ctx->block, block_nb);
|
ctx->transf(ctx->block, block_nb);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
for (i = 0 ; i < 8; i++) {
|
for (int i = 0 ; i < 8; i++)
|
||||||
|
{
|
||||||
UNPACK64(ctx->h[i], &digest[i << 3]);
|
UNPACK64(ctx->h[i], &digest[i << 3]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -634,16 +594,11 @@ void sha512::sha512_final(sha512_ctx *ctx, unsigned char *digest)
|
|||||||
|
|
||||||
/* SHA-384 functions */
|
/* SHA-384 functions */
|
||||||
|
|
||||||
sha384::sha384() : sha512()
|
void sha384_traits::sha_init(sha_ctx *ctx)
|
||||||
{
|
|
||||||
sha384_init(&ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sha384::sha384_init(sha384_ctx *ctx)
|
|
||||||
{
|
{
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
int i;
|
for (int i = 0; i < 8; i++)
|
||||||
for (i = 0; i < 8; i++) {
|
{
|
||||||
ctx->h[i] = sha384_h0[i];
|
ctx->h[i] = sha384_h0[i];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -657,64 +612,52 @@ void sha384::sha384_init(sha384_ctx *ctx)
|
|||||||
ctx->tot_len = 0;
|
ctx->tot_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha384::sha384_update(sha384_ctx *ctx,const unsigned char *message,
|
void sha384_traits::sha_update(sha_ctx* ctx,const unsigned char* message,
|
||||||
unsigned int len)
|
unsigned int len)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int tmp_len = SHA384_BLOCK_SIZE - ctx->len;
|
||||||
unsigned int new_len, rem_len, tmp_len;
|
unsigned int rem_len = len < tmp_len ? len : tmp_len;
|
||||||
const unsigned char *shifted_message;
|
|
||||||
|
|
||||||
tmp_len = SHA384_BLOCK_SIZE - ctx->len;
|
|
||||||
rem_len = len < tmp_len ? len : tmp_len;
|
|
||||||
|
|
||||||
memcpy(&ctx->block[ctx->len], message, rem_len);
|
memcpy(&ctx->block[ctx->len], message, rem_len);
|
||||||
|
|
||||||
if (ctx->len + len < SHA384_BLOCK_SIZE) {
|
if (ctx->len + len < SHA384_BLOCK_SIZE)
|
||||||
|
{
|
||||||
ctx->len += len;
|
ctx->len += len;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_len = len - rem_len;
|
const unsigned int new_len = len - rem_len;
|
||||||
block_nb = new_len / SHA384_BLOCK_SIZE;
|
const unsigned int block_nb = new_len / SHA384_BLOCK_SIZE;
|
||||||
|
|
||||||
shifted_message = message + rem_len;
|
const unsigned char* const shifted_message = message + rem_len;
|
||||||
|
|
||||||
sha512_transf(ctx, ctx->block, 1);
|
ctx->transf(ctx->block, 1);
|
||||||
sha512_transf(ctx, shifted_message, block_nb);
|
ctx->transf(shifted_message, block_nb);
|
||||||
|
|
||||||
rem_len = new_len % SHA384_BLOCK_SIZE;
|
rem_len = new_len % SHA384_BLOCK_SIZE;
|
||||||
|
|
||||||
memcpy(ctx->block, &shifted_message[block_nb << 7],
|
memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len);
|
||||||
rem_len);
|
|
||||||
|
|
||||||
ctx->len = rem_len;
|
ctx->len = rem_len;
|
||||||
ctx->tot_len += (block_nb + 1) << 7;
|
ctx->tot_len += (block_nb + 1) << 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha384::sha384_final(sha384_ctx *ctx, unsigned char *digest)
|
void sha384_traits::sha_final(sha_ctx* ctx, unsigned char* digest)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int block_nb = (1 + ((SHA384_BLOCK_SIZE - 17) < (ctx->len % SHA384_BLOCK_SIZE)));
|
||||||
unsigned int pm_len;
|
|
||||||
unsigned int len_b;
|
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
const unsigned int len_b = (ctx->tot_len + ctx->len) << 3;
|
||||||
int i;
|
const unsigned int pm_len = block_nb << 7;
|
||||||
#endif
|
|
||||||
|
|
||||||
block_nb = (1 + ((SHA384_BLOCK_SIZE - 17)
|
|
||||||
< (ctx->len % SHA384_BLOCK_SIZE)));
|
|
||||||
|
|
||||||
len_b = (ctx->tot_len + ctx->len) << 3;
|
|
||||||
pm_len = block_nb << 7;
|
|
||||||
|
|
||||||
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
||||||
ctx->block[ctx->len] = 0x80;
|
ctx->block[ctx->len] = 0x80;
|
||||||
UNPACK32(len_b, ctx->block + pm_len - 4);
|
UNPACK32(len_b, ctx->block + pm_len - 4);
|
||||||
|
|
||||||
sha512_transf(ctx, ctx->block, block_nb);
|
ctx->transf(ctx->block, block_nb);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
for (i = 0 ; i < 6; i++) {
|
for (int i = 0 ; i < 6; i++)
|
||||||
|
{
|
||||||
UNPACK64(ctx->h[i], &digest[i << 3]);
|
UNPACK64(ctx->h[i], &digest[i << 3]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -729,16 +672,11 @@ void sha384::sha384_final(sha384_ctx *ctx, unsigned char *digest)
|
|||||||
|
|
||||||
/* SHA-224 functions */
|
/* SHA-224 functions */
|
||||||
|
|
||||||
sha224::sha224() : sha256()
|
void sha224_traits::sha_init(sha_ctx* ctx)
|
||||||
{
|
|
||||||
sha224_init(&ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sha224::sha224_init(sha224_ctx *ctx)
|
|
||||||
{
|
{
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
int i;
|
for (int i = 0; i < 8; i++)
|
||||||
for (i = 0; i < 8; i++) {
|
{
|
||||||
ctx->h[i] = sha224_h0[i];
|
ctx->h[i] = sha224_h0[i];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -752,64 +690,52 @@ void sha224::sha224_init(sha224_ctx *ctx)
|
|||||||
ctx->tot_len = 0;
|
ctx->tot_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha224::sha224_update(sha224_ctx *ctx, const unsigned char *message,
|
void sha224_traits::sha_update(sha_ctx* ctx, const unsigned char* message,
|
||||||
unsigned int len)
|
unsigned int len)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int tmp_len = SHA224_BLOCK_SIZE - ctx->len;
|
||||||
unsigned int new_len, rem_len, tmp_len;
|
unsigned int rem_len = len < tmp_len ? len : tmp_len;
|
||||||
const unsigned char *shifted_message;
|
|
||||||
|
|
||||||
tmp_len = SHA224_BLOCK_SIZE - ctx->len;
|
|
||||||
rem_len = len < tmp_len ? len : tmp_len;
|
|
||||||
|
|
||||||
memcpy(&ctx->block[ctx->len], message, rem_len);
|
memcpy(&ctx->block[ctx->len], message, rem_len);
|
||||||
|
|
||||||
if (ctx->len + len < SHA224_BLOCK_SIZE) {
|
if (ctx->len + len < SHA224_BLOCK_SIZE)
|
||||||
|
{
|
||||||
ctx->len += len;
|
ctx->len += len;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_len = len - rem_len;
|
const unsigned int new_len = len - rem_len;
|
||||||
block_nb = new_len / SHA224_BLOCK_SIZE;
|
const unsigned int block_nb = new_len / SHA224_BLOCK_SIZE;
|
||||||
|
|
||||||
shifted_message = message + rem_len;
|
const unsigned char* const shifted_message = message + rem_len;
|
||||||
|
|
||||||
sha256_transf(ctx,ctx->block, 1);
|
ctx->transf(ctx->block, 1);
|
||||||
sha256_transf(ctx, shifted_message, block_nb);
|
ctx->transf(shifted_message, block_nb);
|
||||||
|
|
||||||
rem_len = new_len % SHA224_BLOCK_SIZE;
|
rem_len = new_len % SHA224_BLOCK_SIZE;
|
||||||
|
|
||||||
memcpy(ctx->block, &shifted_message[block_nb << 6],
|
memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len);
|
||||||
rem_len);
|
|
||||||
|
|
||||||
ctx->len = rem_len;
|
ctx->len = rem_len;
|
||||||
ctx->tot_len += (block_nb + 1) << 6;
|
ctx->tot_len += (block_nb + 1) << 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sha224::sha224_final(sha224_ctx *ctx, unsigned char *digest)
|
void sha224_traits::sha_final(sha_ctx* ctx, unsigned char* digest)
|
||||||
{
|
{
|
||||||
unsigned int block_nb;
|
const unsigned int block_nb = (1 + ((SHA224_BLOCK_SIZE - 9) < (ctx->len % SHA224_BLOCK_SIZE)));
|
||||||
unsigned int pm_len;
|
|
||||||
unsigned int len_b;
|
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
const unsigned int len_b = (ctx->tot_len + ctx->len) << 3;
|
||||||
int i;
|
const unsigned int pm_len = block_nb << 6;
|
||||||
#endif
|
|
||||||
|
|
||||||
block_nb = (1 + ((SHA224_BLOCK_SIZE - 9)
|
|
||||||
< (ctx->len % SHA224_BLOCK_SIZE)));
|
|
||||||
|
|
||||||
len_b = (ctx->tot_len + ctx->len) << 3;
|
|
||||||
pm_len = block_nb << 6;
|
|
||||||
|
|
||||||
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
|
||||||
ctx->block[ctx->len] = 0x80;
|
ctx->block[ctx->len] = 0x80;
|
||||||
UNPACK32(len_b, ctx->block + pm_len - 4);
|
UNPACK32(len_b, ctx->block + pm_len - 4);
|
||||||
|
|
||||||
sha256_transf(ctx, ctx->block, block_nb);
|
ctx->transf(ctx->block, block_nb);
|
||||||
|
|
||||||
#ifndef UNROLL_LOOPS
|
#ifndef UNROLL_LOOPS
|
||||||
for (i = 0 ; i < 7; i++) {
|
for (int i = 0 ; i < 7; i++)
|
||||||
|
{
|
||||||
UNPACK32(ctx->h[i], &digest[i << 2]);
|
UNPACK32(ctx->h[i], &digest[i << 2]);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -832,20 +758,22 @@ void sha224::sha224_final(sha224_ctx *ctx, unsigned char *digest)
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void test(const char *vector, unsigned char *digest,
|
void test(const char *vector, unsigned char *digest, unsigned int digest_size)
|
||||||
unsigned int digest_size)
|
|
||||||
{
|
{
|
||||||
char output[2 * SHA_MAX_DIGEST_SIZE + 1];
|
char output[2 * SHA_MAX_DIGEST_SIZE + 1];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
output[2 * digest_size] = '\0';
|
output[2 * digest_size] = '\0';
|
||||||
|
|
||||||
for (i = 0; i < (int) digest_size ; i++) {
|
for (i = 0; i < (int) digest_size ; i++)
|
||||||
|
{
|
||||||
sprintf(output + 2 * i, "%02x", digest[i]);
|
sprintf(output + 2 * i, "%02x", digest[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("H: %s\n", output);
|
printf("H: %s\n", output);
|
||||||
if (strcmp(vector, output)) {
|
|
||||||
|
if (strcmp(vector, output))
|
||||||
|
{
|
||||||
fprintf(stderr, "Test failed.\n");
|
fprintf(stderr, "Test failed.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -899,10 +827,13 @@ int main(void)
|
|||||||
unsigned char digest[SHA512_DIGEST_SIZE];
|
unsigned char digest[SHA512_DIGEST_SIZE];
|
||||||
|
|
||||||
message3 = (unsigned char *) malloc(message3_len);
|
message3 = (unsigned char *) malloc(message3_len);
|
||||||
if (message3 == NULL) {
|
|
||||||
|
if (message3 == NULL)
|
||||||
|
{
|
||||||
fprintf(stderr, "Can't allocate memory\n");
|
fprintf(stderr, "Can't allocate memory\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(message3, 'a', message3_len);
|
memset(message3, 'a', message3_len);
|
||||||
printf("SHA-2 FIPS 180-2 Validation tests\n\n");
|
printf("SHA-2 FIPS 180-2 Validation tests\n\n");
|
||||||
printf("SHA-224 Test vectors\n");
|
printf("SHA-224 Test vectors\n");
|
||||||
@ -951,4 +882,3 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif /* TEST_VECTORS */
|
#endif /* TEST_VECTORS */
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ namespace Firebird {
|
|||||||
* digest from an arbitrary length message.
|
* digest from an arbitrary length message.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class SHA>void get_digest(const unsigned char *message, size_t len, unsigned char *digest)
|
template<class SHA>void get_digest(const unsigned char* message, size_t len, unsigned char* digest)
|
||||||
{
|
{
|
||||||
SHA sha;
|
SHA sha;
|
||||||
sha.process(len, message);
|
sha.process(len, message);
|
||||||
@ -113,158 +113,160 @@ template<class SHA> void hashBased64(Firebird::string& hash, const Firebird::str
|
|||||||
* by calling the "reset" method.
|
* by calling the "reset" method.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
class sha224_traits;
|
||||||
|
class sha256_traits;
|
||||||
|
class sha384_traits;
|
||||||
|
class sha512_traits;
|
||||||
|
|
||||||
|
class sha2_types
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef unsigned char uint8;
|
||||||
|
typedef unsigned int uint32;
|
||||||
|
typedef unsigned long long uint64;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class SHA_TRAITS>
|
||||||
class sha2_base : public GlobalStorage {
|
class sha2_base : public GlobalStorage {
|
||||||
#else
|
#else
|
||||||
class sha2_base {
|
class sha2_base {
|
||||||
#endif
|
#endif
|
||||||
|
private:
|
||||||
|
sha2_base(const sha2_base&);
|
||||||
|
sha2_base& operator = (const sha2_base&);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
sha2_base() {};
|
sha2_base()
|
||||||
|
{
|
||||||
|
SHA_TRAITS::sha_init(&m_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
virtual ~sha2_base() {};
|
virtual ~sha2_base() {};
|
||||||
|
|
||||||
virtual const unsigned int get_DigestSize()=0;
|
|
||||||
virtual const unsigned int get_BlockSize()=0;
|
|
||||||
|
|
||||||
typedef unsigned char uint8;
|
|
||||||
typedef unsigned int uint32;
|
|
||||||
typedef unsigned long long uint64;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void sha_init() {};
|
|
||||||
virtual void sha_update(const unsigned char *message, unsigned int len)=0;
|
|
||||||
virtual void sha_final(unsigned char *digest)=0;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void reset() {sha_init();};
|
void reset()
|
||||||
|
{
|
||||||
|
SHA_TRAITS::sha_init(&m_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
void process(size_t length, const void* bytes)
|
void process(size_t length, const void* bytes)
|
||||||
{
|
{
|
||||||
sha_update(static_cast<const unsigned char*>(bytes), length);
|
SHA_TRAITS::sha_update(&m_ctx, static_cast<const unsigned char*>(bytes), length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(size_t length, const unsigned char* message)
|
void process(size_t length, const unsigned char* message)
|
||||||
{
|
{
|
||||||
sha_update(message, length);
|
SHA_TRAITS::sha_update(&m_ctx, message, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(const char* str)
|
void process(const char* str)
|
||||||
{
|
{
|
||||||
process(strlen(str), str);
|
SHA_TRAITS::sha_update(&m_ctx, reinterpret_cast<const unsigned char*>(str), strlen(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
void getHash(unsigned char *digest);
|
void getHash(unsigned char* digest)
|
||||||
|
{
|
||||||
|
SHA_TRAITS::sha_final(&m_ctx, digest);
|
||||||
|
SHA_TRAITS::sha_init(&m_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef NIST_COMPLIANCY_TESTS
|
#ifndef NIST_COMPLIANCY_TESTS
|
||||||
void process(const UCharBuffer& bytes)
|
void process(const UCharBuffer& bytes)
|
||||||
{
|
{
|
||||||
process(bytes.getCount(), bytes.begin());
|
SHA_TRAITS::sha_update(&m_ctx, bytes.begin(), bytes.getCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
void getHash(UCharBuffer& h);
|
void getHash(UCharBuffer& h)
|
||||||
|
{
|
||||||
|
SHA_TRAITS::sha_final(&m_ctx, h.getBuffer(SHA_TRAITS::get_DigestSize()));
|
||||||
|
SHA_TRAITS::sha_init(&m_ctx);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
typename SHA_TRAITS::sha_ctx m_ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
class sha256 : public sha2_base {
|
typedef sha2_base<sha224_traits> sha224;
|
||||||
public:
|
typedef sha2_base<sha256_traits> sha256;
|
||||||
sha256();
|
typedef sha2_base<sha384_traits> sha384;
|
||||||
const unsigned int get_DigestSize() {return SHA256_DIGEST_SIZE;};
|
typedef sha2_base<sha512_traits> sha512;
|
||||||
const unsigned int get_BlockSize() {return SHA256_BLOCK_SIZE;};
|
|
||||||
|
|
||||||
protected:
|
struct sha256_ctx {
|
||||||
typedef struct {
|
|
||||||
unsigned int tot_len;
|
unsigned int tot_len;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
unsigned char block[2 * SHA256_BLOCK_SIZE];
|
unsigned char block[2 * SHA256_BLOCK_SIZE];
|
||||||
uint32 h[8];
|
sha2_types::uint32 h[8];
|
||||||
} sha256_ctx;
|
|
||||||
|
|
||||||
private:
|
void transf(const unsigned char* message, unsigned int block_nb);
|
||||||
void sha256_init(sha256_ctx * ctx);
|
|
||||||
void sha256_update(sha256_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int len);
|
|
||||||
void sha256_final(sha256_ctx *ctx, unsigned char *digest);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
sha256_ctx ctx;
|
|
||||||
|
|
||||||
void sha256_transf(sha256_ctx * ctx, const unsigned char *message,
|
|
||||||
unsigned int block_nb);
|
|
||||||
void sha_init() {sha256_init(&ctx);};
|
|
||||||
void sha_update(const unsigned char *message, unsigned int len) {sha256_update(&ctx,message,len);};
|
|
||||||
void sha_final(unsigned char *digest) {sha256_final(&ctx,digest);};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class sha224 : public sha256 {
|
class sha256_traits: private sha2_types {
|
||||||
public:
|
public:
|
||||||
sha224();
|
typedef sha256_ctx sha_ctx;
|
||||||
const unsigned int get_DigestSize() {return SHA224_DIGEST_SIZE;};
|
|
||||||
const unsigned int get_BlockSize() {return SHA224_BLOCK_SIZE;};
|
|
||||||
|
|
||||||
private:
|
public:
|
||||||
typedef sha256_ctx sha224_ctx;
|
static unsigned int get_DigestSize() {return SHA256_DIGEST_SIZE;};
|
||||||
void sha224_init(sha224_ctx *ctx);
|
|
||||||
void sha224_update(sha224_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int len);
|
|
||||||
void sha224_final(sha224_ctx *ctx, unsigned char *digest);
|
|
||||||
|
|
||||||
protected:
|
static void sha_init(sha_ctx* ctx);
|
||||||
void sha_init() {sha224_init(&ctx);};
|
|
||||||
void sha_update(const unsigned char *message, unsigned int len) {sha224_update(&ctx, message, len);};
|
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
|
||||||
void sha_final(unsigned char *digest) {sha224_final(&ctx,digest);};
|
|
||||||
|
static void sha_final(sha_ctx *ctx, unsigned char* digest);
|
||||||
};
|
};
|
||||||
|
|
||||||
class sha512 : public sha2_base {
|
class sha224_traits: private sha2_types {
|
||||||
public:
|
public:
|
||||||
sha512();
|
typedef sha256_ctx sha_ctx;
|
||||||
const unsigned int get_DigestSize() {return SHA512_DIGEST_SIZE;};
|
|
||||||
const unsigned int get_BlockSize() {return SHA512_BLOCK_SIZE;};
|
|
||||||
|
|
||||||
protected:
|
public:
|
||||||
typedef struct {
|
static unsigned int get_DigestSize() {return SHA224_DIGEST_SIZE;};
|
||||||
|
|
||||||
|
static void sha_init(sha_ctx* ctx);
|
||||||
|
|
||||||
|
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
|
||||||
|
|
||||||
|
static void sha_final(sha_ctx* ctx, unsigned char* digest);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sha512_ctx{
|
||||||
unsigned int tot_len;
|
unsigned int tot_len;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
unsigned char block[2 * SHA512_BLOCK_SIZE];
|
unsigned char block[2 * SHA512_BLOCK_SIZE];
|
||||||
uint64 h[8];
|
sha2_types::uint64 h[8];
|
||||||
} sha512_ctx;
|
|
||||||
|
|
||||||
private:
|
void transf(const unsigned char* message, unsigned int block_nb);
|
||||||
void sha512_init(sha512_ctx *ctx);
|
|
||||||
void sha512_update(sha512_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int len);
|
|
||||||
void sha512_final(sha512_ctx *ctx, unsigned char *digest);
|
|
||||||
protected:
|
|
||||||
|
|
||||||
sha512_ctx ctx;
|
|
||||||
|
|
||||||
void sha512_transf(sha512_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int block_nb);
|
|
||||||
void sha_init() {sha512_init(&ctx);};
|
|
||||||
void sha_update(const unsigned char *message, unsigned int len) {sha512_update(&ctx, message, len);};
|
|
||||||
void sha_final(unsigned char *digest) {sha512_final(&ctx, digest);};
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class sha384 : public sha512 {
|
class sha512_traits: private sha2_types {
|
||||||
public:
|
public:
|
||||||
sha384();
|
typedef sha512_ctx sha_ctx;
|
||||||
const unsigned int get_DigestSize() {return SHA384_DIGEST_SIZE;};
|
|
||||||
const unsigned int get_BlockSize() {return SHA384_BLOCK_SIZE;};
|
|
||||||
|
|
||||||
private:
|
public:
|
||||||
typedef sha512_ctx sha384_ctx;
|
static unsigned int get_DigestSize() {return SHA512_DIGEST_SIZE;};
|
||||||
void sha384_init(sha384_ctx *ctx);
|
|
||||||
void sha384_update(sha384_ctx *ctx, const unsigned char *message,
|
|
||||||
unsigned int len);
|
|
||||||
void sha384_final(sha384_ctx *ctx, unsigned char *digest);
|
|
||||||
|
|
||||||
protected:
|
static void sha_init(sha_ctx* ctx);
|
||||||
void sha_init() {sha384_init(&ctx);};
|
|
||||||
void sha_update(const unsigned char *message, unsigned int len) {sha384_update(&ctx,message,len);};
|
|
||||||
void sha_final(unsigned char *digest) {sha384_final(&ctx, digest);};
|
|
||||||
|
|
||||||
|
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
|
||||||
|
|
||||||
|
static void sha_final(sha_ctx* ctx, unsigned char* digest);
|
||||||
|
};
|
||||||
|
|
||||||
|
class sha384_traits: private sha2_types {
|
||||||
|
public:
|
||||||
|
typedef sha512_ctx sha_ctx;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static unsigned int get_DigestSize() {return SHA384_DIGEST_SIZE;};
|
||||||
|
|
||||||
|
static void sha_init(sha_ctx* ctx);
|
||||||
|
|
||||||
|
static void sha_update(sha_ctx* ctx, const unsigned char* message, unsigned int len);
|
||||||
|
|
||||||
|
static void sha_final(sha_ctx* ctx, unsigned char* digest);
|
||||||
};
|
};
|
||||||
|
|
||||||
} //Firebird
|
} //Firebird
|
||||||
|
|
||||||
#endif /* !_SHA2_H */
|
#endif /* !_SHA2_H */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user