diff options
-rw-r--r-- | firmware/2lib/2sha256.c | 18 | ||||
-rw-r--r-- | firmware/2lib/include/2sha.h | 9 | ||||
-rw-r--r-- | tests/vb2_sha_tests.c | 17 |
3 files changed, 40 insertions, 4 deletions
diff --git a/firmware/2lib/2sha256.c b/firmware/2lib/2sha256.c index fd41258c..d2cf6726 100644 --- a/firmware/2lib/2sha256.c +++ b/firmware/2lib/2sha256.c @@ -314,3 +314,21 @@ void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest) UNPACK32(ctx->h[7], &digest[28]); #endif /* !UNROLL_LOOPS */ } + +void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to) +{ + struct vb2_sha256_context dc; + int i; + + for (i = 0; i < 8; i++) { + PACK32(from, &dc.h[i]); + from += 4; + } + + vb2_sha256_transform(&dc, by, 1); + + for (i = 0; i < 8; i++) { + UNPACK32(dc.h[i], to); + to += 4; + } +} diff --git a/firmware/2lib/include/2sha.h b/firmware/2lib/include/2sha.h index 0077f379..38b8a816 100644 --- a/firmware/2lib/include/2sha.h +++ b/firmware/2lib/include/2sha.h @@ -133,6 +133,15 @@ void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest); void vb2_sha512_finalize(struct vb2_sha512_context *ctx, uint8_t *digest); /** + * Hash-extend data + * + * @param from Hash to be extended. It has to be the hash size. + * @param by Block to be extended by. It has to be the hash block size. + * @param to Destination for extended data + */ +void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to); + +/** * Convert vb2_crypto_algorithm to vb2_hash_algorithm. * * @param algorithm Crypto algorithm (vb2_crypto_algorithm) diff --git a/tests/vb2_sha_tests.c b/tests/vb2_sha_tests.c index c3185cac..94236355 100644 --- a/tests/vb2_sha_tests.c +++ b/tests/vb2_sha_tests.c @@ -50,13 +50,18 @@ void sha256_tests(void) uint8_t digest[VB2_SHA256_DIGEST_SIZE]; uint8_t *test_inputs[3]; struct vb2_sha256_context ctx; - uint8_t expect_multiple[VB2_SHA256_DIGEST_SIZE] = - { + const uint8_t expect_multiple[VB2_SHA256_DIGEST_SIZE] = { 0x07, 0x08, 0xb4, 0xca, 0x46, 0x4c, 0x40, 0x39, 0x07, 0x06, 0x88, 0x80, 0x30, 0x55, 0x5d, 0x86, 0x0e, 0x4a, 0x0d, 0x2b, 0xc6, 0xc4, 0x87, 0x39, - 0x2c, 0x16, 0x55, 0xb0, 0x82, 0x13, 0x16, 0x29 - }; + 0x2c, 0x16, 0x55, 0xb0, 0x82, 0x13, 0x16, 0x29 }; + const uint8_t extend_from[VB2_SHA256_DIGEST_SIZE] = { 0x00, }; + const uint8_t extend_by[VB2_SHA256_BLOCK_SIZE] = { 0x00, }; + const uint8_t expected_extend[VB2_SHA256_DIGEST_SIZE] = { + 0x7c, 0xa5, 0x16, 0x14, 0x42, 0x5c, 0x3b, 0xa8, 0xce, 0x54, + 0xdd, 0x2f, 0xc2, 0x02, 0x0a, 0xe7, 0xb6, 0xe5, 0x74, 0xd1, + 0x98, 0x13, 0x6d, 0x0f, 0xae, 0x7e, 0x26, 0xcc, 0xbf, 0x0b, + 0xe7, 0xa6 }; int i; test_inputs[0] = (uint8_t *) oneblock_msg; @@ -90,6 +95,10 @@ void sha256_tests(void) TEST_EQ(vb2_hash_block_size(VB2_HASH_SHA256), VB2_SHA256_BLOCK_SIZE, "vb2_hash_block_size(VB2_HASH_SHA256)"); + + /* Test SHA256 hash extend */ + vb2_sha256_extend(extend_from, extend_by, digest); + TEST_SUCC(memcmp(digest, expected_extend, sizeof(digest)), NULL); } void sha512_tests(void) |