From 83810dcc4791413e7714cb8885d8e20ad31d424d Mon Sep 17 00:00:00 2001 From: Joost Rijneveld Date: Mon, 11 Mar 2019 16:33:47 +0100 Subject: [PATCH] Expose incremental SHA2 API --- common/sha2.c | 164 ++++++++++++++++++++++++--------------------- common/sha2.h | 15 ++++- test/common/sha2.c | 62 ++++++++++++----- 3 files changed, 143 insertions(+), 98 deletions(-) diff --git a/common/sha2.c b/common/sha2.c index 8756ce48..a6eb7226 100644 --- a/common/sha2.c +++ b/common/sha2.c @@ -485,16 +485,60 @@ static const uint8_t iv_512[64] = { 0x6b, 0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79 }; -int sha256(uint8_t *out, const uint8_t *in, size_t inlen) { - uint8_t h[32]; - uint8_t padded[128]; - uint64_t bytes = inlen; - +void sha256_inc_init(uint8_t *state) { for (size_t i = 0; i < 32; ++i) { - h[i] = iv_256[i]; + state[i] = iv_256[i]; } + for (size_t i = 32; i < 40; ++i) { + state[i] = 0; + } +} - crypto_hashblocks_sha256(h, in, inlen); +void sha384_inc_init(uint8_t *state) { + for (size_t i = 0; i < 64; ++i) { + state[i] = iv_384[i]; + } + for (size_t i = 64; i < 72; ++i) { + state[i] = 0; + } +} + +void sha512_inc_init(uint8_t *state) { + for (size_t i = 0; i < 64; ++i) { + state[i] = iv_512[i]; + } + for (size_t i = 64; i < 72; ++i) { + state[i] = 0; + } +} + +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 32); + + crypto_hashblocks_sha256(state, in, 64 * inblocks); + bytes += 64 * inblocks; + + store_bigendian_64(state + 32, bytes); +} + +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + uint64_t bytes = load_bigendian_64(state + 64); + + crypto_hashblocks_sha256(state, in, 128 * inblocks); + bytes += 128 * inblocks; + + store_bigendian_64(state + 64, bytes); +} + +void sha384_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks) { + sha512_inc_blocks(state, in, inblocks); +} + +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + uint8_t padded[128]; + uint64_t bytes = load_bigendian_64(state + 32) + inlen; + + crypto_hashblocks_sha256(state, in, inlen); in += inlen; inlen &= 63; in -= inlen; @@ -516,7 +560,7 @@ int sha256(uint8_t *out, const uint8_t *in, size_t inlen) { padded[61] = (uint8_t) (bytes >> 13); padded[62] = (uint8_t) (bytes >> 5); padded[63] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha256(h, padded, 64); + crypto_hashblocks_sha256(state, padded, 64); } else { for (size_t i = inlen + 1; i < 120; ++i) { padded[i] = 0; @@ -529,26 +573,19 @@ int sha256(uint8_t *out, const uint8_t *in, size_t inlen) { padded[125] = (uint8_t) (bytes >> 13); padded[126] = (uint8_t) (bytes >> 5); padded[127] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha256(h, padded, 128); + crypto_hashblocks_sha256(state, padded, 128); } for (size_t i = 0; i < 32; ++i) { - out[i] = h[i]; + out[i] = state[i]; } - - return 0; } -int sha384(uint8_t *out, const uint8_t *in, size_t inlen) { - uint8_t h[64]; +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { uint8_t padded[256]; - uint64_t bytes = inlen; + uint64_t bytes = load_bigendian_64(state + 64) + inlen; - for (size_t i = 0; i < 64; ++i) { - h[i] = iv_384[i]; - } - - crypto_hashblocks_sha512(h, in, inlen); + crypto_hashblocks_sha512(state, in, inlen); in += inlen; inlen &= 127; in -= inlen; @@ -571,7 +608,7 @@ int sha384(uint8_t *out, const uint8_t *in, size_t inlen) { padded[125] = (uint8_t) (bytes >> 13); padded[126] = (uint8_t) (bytes >> 5); padded[127] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha512(h, padded, 128); + crypto_hashblocks_sha512(state, padded, 128); } else { for (size_t i = inlen + 1; i < 247; ++i) { padded[i] = 0; @@ -585,68 +622,39 @@ int sha384(uint8_t *out, const uint8_t *in, size_t inlen) { padded[253] = (uint8_t) (bytes >> 13); padded[254] = (uint8_t) (bytes >> 5); padded[255] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha512(h, padded, 256); + crypto_hashblocks_sha512(state, padded, 256); } + for (size_t i = 0; i < 64; ++i) { + out[i] = state[i]; + } +} + +void sha384_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen) { + sha512_inc_finalize(state, state, in, inlen); + for (size_t i = 0; i < 48; ++i) { - out[i] = h[i]; + out[i] = state[i]; } - - return 0; } -int sha512(uint8_t *out, const uint8_t *in, size_t inlen) { - uint8_t h[64]; - uint8_t padded[256]; - uint64_t bytes = inlen; +void sha256(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[40]; - for (size_t i = 0; i < 64; ++i) { - h[i] = iv_512[i]; - } - - crypto_hashblocks_sha512(h, in, inlen); - in += inlen; - inlen &= 127; - in -= inlen; - - for (size_t i = 0; i < inlen; ++i) { - padded[i] = in[i]; - } - padded[inlen] = 0x80; - - if (inlen < 112) { - for (size_t i = inlen + 1; i < 119; ++i) { - padded[i] = 0; - } - padded[119] = (uint8_t) (bytes >> 61); - padded[120] = (uint8_t) (bytes >> 53); - padded[121] = (uint8_t) (bytes >> 45); - padded[122] = (uint8_t) (bytes >> 37); - padded[123] = (uint8_t) (bytes >> 29); - padded[124] = (uint8_t) (bytes >> 21); - padded[125] = (uint8_t) (bytes >> 13); - padded[126] = (uint8_t) (bytes >> 5); - padded[127] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha512(h, padded, 128); - } else { - for (size_t i = inlen + 1; i < 247; ++i) { - padded[i] = 0; - } - padded[247] = (uint8_t) (bytes >> 61); - padded[248] = (uint8_t) (bytes >> 53); - padded[249] = (uint8_t) (bytes >> 45); - padded[250] = (uint8_t) (bytes >> 37); - padded[251] = (uint8_t) (bytes >> 29); - padded[252] = (uint8_t) (bytes >> 21); - padded[253] = (uint8_t) (bytes >> 13); - padded[254] = (uint8_t) (bytes >> 5); - padded[255] = (uint8_t) (bytes << 3); - crypto_hashblocks_sha512(h, padded, 256); - } - - for (size_t i = 0; i < 64; ++i) { - out[i] = h[i]; - } - - return 0; + sha256_inc_init(state); + sha256_inc_finalize(out, state, in, inlen); +} + +void sha384(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[72]; + + sha384_inc_init(state); + sha384_inc_finalize(out, state, in, inlen); +} + +void sha512(uint8_t *out, const uint8_t *in, size_t inlen) { + uint8_t state[72]; + + sha512_inc_init(state); + sha512_inc_finalize(out, state, in, inlen); } diff --git a/common/sha2.h b/common/sha2.h index 64c05b2a..f40a1d32 100644 --- a/common/sha2.h +++ b/common/sha2.h @@ -4,10 +4,19 @@ #include #include -int sha256(uint8_t *out, const uint8_t *in, size_t inlen); +void sha256_inc_init(uint8_t *state); +void sha256_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha256_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha256(uint8_t *out, const uint8_t *in, size_t inlen); -int sha384(uint8_t *out, const uint8_t *in, size_t inlen); +void sha384_inc_init(uint8_t *state); +void sha384_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha384_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha384(uint8_t *out, const uint8_t *in, size_t inlen); -int sha512(uint8_t *out, const uint8_t *in, size_t inlen); +void sha512_inc_init(uint8_t *state); +void sha512_inc_blocks(uint8_t *state, const uint8_t *in, size_t inblocks); +void sha512_inc_finalize(uint8_t *out, uint8_t *state, const uint8_t *in, size_t inlen); +void sha512(uint8_t *out, const uint8_t *in, size_t inlen); #endif diff --git a/test/common/sha2.c b/test/common/sha2.c index b2aa20b6..aba24ac2 100644 --- a/test/common/sha2.c +++ b/test/common/sha2.c @@ -5,35 +5,62 @@ #include #include -const unsigned char plaintext[44] = "The quick brown fox jumps over the lazy dog"; +const unsigned char plaintext[113] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; const unsigned char expected_256[32] = { - 0xd7, 0xa8, 0xfb, 0xb3, 0x07, 0xd7, 0x80, 0x94, 0x69, 0xca, 0x9a, 0xbc, - 0xb0, 0x08, 0x2e, 0x4f, 0x8d, 0x56, 0x51, 0xe4, 0x6d, 0x3c, 0xdb, 0x76, - 0x2d, 0x02, 0xd0, 0xbf, 0x37, 0xc9, 0xe5, 0x92, + 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, + 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, + 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 }; const unsigned char expected_384[48] = { - 0xca, 0x73, 0x7f, 0x10, 0x14, 0xa4, 0x8f, 0x4c, 0x0b, 0x6d, 0xd4, 0x3c, - 0xb1, 0x77, 0xb0, 0xaf, 0xd9, 0xe5, 0x16, 0x93, 0x67, 0x54, 0x4c, 0x49, - 0x40, 0x11, 0xe3, 0x31, 0x7d, 0xbf, 0x9a, 0x50, 0x9c, 0xb1, 0xe5, 0xdc, - 0x1e, 0x85, 0xa9, 0x41, 0xbb, 0xee, 0x3d, 0x7f, 0x2a, 0xfb, 0xc9, 0xb1, + 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7, + 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, + 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a, + 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }; const unsigned char expected_512[64] = { - 0x07, 0xe5, 0x47, 0xd9, 0x58, 0x6f, 0x6a, 0x73, 0xf7, 0x3f, 0xba, 0xc0, - 0x43, 0x5e, 0xd7, 0x69, 0x51, 0x21, 0x8f, 0xb7, 0xd0, 0xc8, 0xd7, 0x88, - 0xa3, 0x09, 0xd7, 0x85, 0x43, 0x6b, 0xbb, 0x64, 0x2e, 0x93, 0xa2, 0x52, - 0xa9, 0x54, 0xf2, 0x39, 0x12, 0x54, 0x7d, 0x1e, 0x8a, 0x3b, 0x5e, 0xd6, - 0xe1, 0xbf, 0xd7, 0x09, 0x78, 0x21, 0x23, 0x3f, 0xa0, 0x53, 0x8f, 0x3d, - 0xb8, 0x54, 0xfe, 0xe6, + 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, + 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, + 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, + 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, + 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, + 0x87, 0x4b, 0xe9, 0x09 }; +static int test_sha256_incremental(void) { + unsigned char output[32]; + uint8_t state[40]; + int i = 0; + + sha256_inc_init(state); + sha256_inc_blocks(state, plaintext, 1); + sha256_inc_finalize(output, state, plaintext + 64, 112 - 64); + + if (memcmp(expected_256, output, 32)) { + printf("ERROR sha256 incremental did not match sha256.\n"); + printf(" Expected: "); + for (i = 0; i < 32; i++) { + printf("%02X", expected_256[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 32; i++) { + printf("%02X", output[i]); + } + printf("\n"); + return 1; + } + + return 0; +} + static int test_sha256(void) { unsigned char output[32]; int i = 0; - sha256(output, plaintext, 43); + sha256(output, plaintext, 112); if (memcmp(expected_256, output, 32)) { printf("ERROR sha256 output did not match test vector.\n"); @@ -57,7 +84,7 @@ static int test_sha384(void) { unsigned char output[48]; int i = 0; - sha384(output, plaintext, 43); + sha384(output, plaintext, 112); if (memcmp(expected_384, output, 48)) { printf("ERROR sha384 output did not match test vector.\n"); @@ -81,7 +108,7 @@ static int test_sha512(void) { unsigned char output[64]; int i = 0; - sha512(output, plaintext, 43); + sha512(output, plaintext, 112); if (memcmp(expected_512, output, 64)) { printf("ERROR sha512 output did not match test vector.\n"); @@ -104,6 +131,7 @@ static int test_sha512(void) { int main(void) { int result = 0; result += test_sha256(); + result += test_sha256_incremental(); result += test_sha384(); result += test_sha512();