|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
/*********************************************************************
|
|
|
|
|
/*********************************************************************
|
|
|
|
|
* Filename: sha256.c |
|
|
|
|
* Author: Brad Conte (brad AT bradconte.com) |
|
|
|
|
* Copyright: |
|
|
|
|
@ -29,7 +29,7 @@
|
|
|
|
|
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) |
|
|
|
|
|
|
|
|
|
/**************************** VARIABLES *****************************/ |
|
|
|
|
static const WORD k[64] = { |
|
|
|
|
static const uint32_t k[64] = { |
|
|
|
|
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
|
|
|
|
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
|
|
|
|
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
|
|
|
|
@ -41,9 +41,9 @@ static const WORD k[64] = {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/*********************** FUNCTION DEFINITIONS ***********************/ |
|
|
|
|
void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) |
|
|
|
|
static void sc_sha256_transform(SC_SHA256_CTX *ctx, const uint8_t data[]) |
|
|
|
|
{ |
|
|
|
|
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; |
|
|
|
|
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; |
|
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < 16; ++i, j += 4) |
|
|
|
|
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); |
|
|
|
|
@ -82,7 +82,7 @@ void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
|
|
|
|
|
ctx->state[7] += h; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void sha256_init(SHA256_CTX *ctx) |
|
|
|
|
void sc_sha256_init(SC_SHA256_CTX *ctx) |
|
|
|
|
{ |
|
|
|
|
ctx->datalen = 0; |
|
|
|
|
ctx->bitlen = 0; |
|
|
|
|
@ -96,24 +96,24 @@ void sha256_init(SHA256_CTX *ctx)
|
|
|
|
|
ctx->state[7] = 0x5be0cd19; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) |
|
|
|
|
void sc_sha256_update(SC_SHA256_CTX *ctx, const uint8_t data[], size_t len) |
|
|
|
|
{ |
|
|
|
|
WORD i; |
|
|
|
|
uint32_t i; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) { |
|
|
|
|
ctx->data[ctx->datalen] = data[i]; |
|
|
|
|
ctx->datalen++; |
|
|
|
|
if (ctx->datalen == 64) { |
|
|
|
|
sha256_transform(ctx, ctx->data); |
|
|
|
|
sc_sha256_transform(ctx, ctx->data); |
|
|
|
|
ctx->bitlen += 512; |
|
|
|
|
ctx->datalen = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void sha256_final(SHA256_CTX *ctx, BYTE hash[]) |
|
|
|
|
void sc_sha256_final(SC_SHA256_CTX *ctx, uint8_t hash[]) |
|
|
|
|
{ |
|
|
|
|
WORD i; |
|
|
|
|
uint32_t i; |
|
|
|
|
|
|
|
|
|
i = ctx->datalen; |
|
|
|
|
|
|
|
|
|
@ -127,7 +127,7 @@ void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
|
|
|
|
ctx->data[i++] = 0x80; |
|
|
|
|
while (i < 64) |
|
|
|
|
ctx->data[i++] = 0x00; |
|
|
|
|
sha256_transform(ctx, ctx->data); |
|
|
|
|
sc_sha256_transform(ctx, ctx->data); |
|
|
|
|
memset(ctx->data, 0, 56); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -141,7 +141,7 @@ void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
|
|
|
|
ctx->data[58] = ctx->bitlen >> 40; |
|
|
|
|
ctx->data[57] = ctx->bitlen >> 48; |
|
|
|
|
ctx->data[56] = ctx->bitlen >> 56; |
|
|
|
|
sha256_transform(ctx, ctx->data); |
|
|
|
|
sc_sha256_transform(ctx, ctx->data); |
|
|
|
|
|
|
|
|
|
// Since this implementation uses little endian byte ordering and SHA uses big endian,
|
|
|
|
|
// reverse all the bytes when copying the final state to the output hash.
|
|
|
|
|
|