vb2_sha: Add sha256 extend

This patch adds vb2_sha256_extend, which extends a hash using a given block.

BUG=chrome-os-partner:51907
BRANCH=tot
TEST=make runtests

Change-Id: I512674f18dffc55692907c85b19ff19df88a5eeb
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/346234
Commit-Ready: Daisuke Nojiri <dnojiri@google.com>
Tested-by: Daisuke Nojiri <dnojiri@google.com>
Reviewed-by: Randall Spangler <rspangler@google.com>
This commit is contained in:
Daisuke Nojiri
2016-05-19 19:09:10 -07:00
committed by chrome-bot
parent 6b5c4e961f
commit 31d756465d
3 changed files with 40 additions and 4 deletions

View File

@@ -314,3 +314,21 @@ void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest)
UNPACK32(ctx->h[7], &digest[28]); UNPACK32(ctx->h[7], &digest[28]);
#endif /* !UNROLL_LOOPS */ #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;
}
}

View File

@@ -132,6 +132,15 @@ void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest);
void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest); void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest);
void vb2_sha512_finalize(struct vb2_sha512_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. * Convert vb2_crypto_algorithm to vb2_hash_algorithm.
* *

View File

@@ -50,13 +50,18 @@ void sha256_tests(void)
uint8_t digest[VB2_SHA256_DIGEST_SIZE]; uint8_t digest[VB2_SHA256_DIGEST_SIZE];
uint8_t *test_inputs[3]; uint8_t *test_inputs[3];
struct vb2_sha256_context ctx; 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, 0x08, 0xb4, 0xca, 0x46, 0x4c, 0x40, 0x39,
0x07, 0x06, 0x88, 0x80, 0x30, 0x55, 0x5d, 0x86, 0x07, 0x06, 0x88, 0x80, 0x30, 0x55, 0x5d, 0x86,
0x0e, 0x4a, 0x0d, 0x2b, 0xc6, 0xc4, 0x87, 0x39, 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; int i;
test_inputs[0] = (uint8_t *) oneblock_msg; 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, TEST_EQ(vb2_hash_block_size(VB2_HASH_SHA256), VB2_SHA256_BLOCK_SIZE,
"vb2_hash_block_size(VB2_HASH_SHA256)"); "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) void sha512_tests(void)