vb2_sha: Add sha256 extend
authorDaisuke Nojiri <dnojiri@chromium.org>
Fri, 20 May 2016 02:09:10 +0000 (19:09 -0700)
committerchrome-bot <chrome-bot@chromium.org>
Sat, 21 May 2016 03:49:35 +0000 (20:49 -0700)
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>
firmware/2lib/2sha256.c
firmware/2lib/include/2sha.h
tests/vb2_sha_tests.c

index fd41258..d2cf672 100644 (file)
@@ -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;
+       }
+}
index 0077f37..38b8a81 100644 (file)
@@ -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)
index c3185ca..9423635 100644 (file)
@@ -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)