diff options
author | Vadim Sukhomlinov <sukhomlinov@google.com> | 2021-07-30 08:40:32 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-08-12 14:18:48 +0000 |
commit | 7ddbd2a9eab0dc54897d6b5bb8ee1d4b3be1fe27 (patch) | |
tree | 43356bb71d38ea7f5ea1639855ac3b322d460176 /board/cr50/dcrypto/hmac_drbg.c | |
parent | 43f6e7be087720507e57cf27e9460aae64c3b69a (diff) | |
download | chrome-ec-7ddbd2a9eab0dc54897d6b5bb8ee1d4b3be1fe27.tar.gz |
cr50: drop cryptoc for SHA1/SHA2 supportstabilize-14150.882.B-cr50_stabstabilize-14150.881.B-cr50_stabstabilize-14150.74.B-cr50_stabstabilize-14150.734.B-cr50_stabstabilize-14150.64.B-cr50_stabstabilize-14150.43.B-cr50_stabstabilize-14150.376.B-cr50_stabrelease-R94-14150.B-cr50_stabrelease-R94-14150.49.B-cr50_stab
To implement FIPS module we need to bring many crypto functions in the
module boundary. Unfortunately, cryptoc is a third-party library used
by dcrypto code in cr50. Cryptoc is also not well-maintained and shared
with other projects. While just making local copy of cryptoc would
solve an issue, it's suboptimal as prevents from many optimizations and
improvements.
Provided SHA & HMAC implementations from Ti50 project. This provides
better performance (500us vs. 670us earlier for HMAC DRBG) and reduce
code size. This implementation also enables stack use savings when
only specific digest is needed. Earlier SHA512 context was allocated
when only SHA256 is needed greatly increasing stack consumption for
code using HMAC_DRBG and others.
However, it introduce subtle API changes which require handling.
As for tests, since core implementation is hardware-independent, make
it available for BOARD=host too.
Before change (with cryptoc):
*** 12368 bytes in flash and 5784 bytes in RAM
After:
*** 13136 bytes in flash and 5796 bytes in RAM
BUG=b:138578318
TEST=make BOARD=cr50 CRYPTO_TEST=1; test/tpm_test/tpmtest.py
Signed-off-by: Vadim Sukhomlinov <sukhomlinov@google.com>
Change-Id: I2ff5362aee9078ce83dc1f8081943a5101d5f666
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3064201
Reviewed-by: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
Tested-by: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Auto-Submit: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Commit-Queue: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Diffstat (limited to 'board/cr50/dcrypto/hmac_drbg.c')
-rw-r--r-- | board/cr50/dcrypto/hmac_drbg.c | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/board/cr50/dcrypto/hmac_drbg.c b/board/cr50/dcrypto/hmac_drbg.c index d601e721de..85c0fe863a 100644 --- a/board/cr50/dcrypto/hmac_drbg.c +++ b/board/cr50/dcrypto/hmac_drbg.c @@ -14,30 +14,28 @@ /* V = HMAC(K, V) */ static void update_v(const uint32_t *k, uint32_t *v) { - LITE_HMAC_CTX ctx; + struct hmac_sha256_ctx ctx; - DCRYPTO_HMAC_SHA256_init(&ctx, k, SHA256_DIGEST_SIZE); - HASH_update(&ctx.hash, v, SHA256_DIGEST_SIZE); - memcpy(v, DCRYPTO_HMAC_final(&ctx), SHA256_DIGEST_SIZE); + HMAC_SHA256_hw_init(&ctx, k, SHA256_DIGEST_SIZE); + HMAC_SHA256_update(&ctx, v, SHA256_DIGEST_SIZE); + memcpy(v, HMAC_SHA256_final(&ctx), SHA256_DIGEST_SIZE); } /* K = HMAC(K, V || tag || p0 || p1 || p2) */ /* V = HMAC(K, V) */ -static void update_kv(uint32_t *k, uint32_t *v, uint8_t tag, - const void *p0, size_t p0_len, - const void *p1, size_t p1_len, +static void update_kv(uint32_t *k, uint32_t *v, uint8_t tag, const void *p0, + size_t p0_len, const void *p1, size_t p1_len, const void *p2, size_t p2_len) { - LITE_HMAC_CTX ctx; - - DCRYPTO_HMAC_SHA256_init(&ctx, k, SHA256_DIGEST_SIZE); - HASH_update(&ctx.hash, v, SHA256_DIGEST_SIZE); - HASH_update(&ctx.hash, &tag, 1); - HASH_update(&ctx.hash, p0, p0_len); - HASH_update(&ctx.hash, p1, p1_len); - HASH_update(&ctx.hash, p2, p2_len); - memcpy(k, DCRYPTO_HMAC_final(&ctx), SHA256_DIGEST_SIZE); - + struct hmac_sha256_ctx ctx; + + HMAC_SHA256_hw_init(&ctx, k, SHA256_DIGEST_SIZE); + HMAC_SHA256_update(&ctx, v, SHA256_DIGEST_SIZE); + HMAC_SHA256_update(&ctx, &tag, 1); + HMAC_SHA256_update(&ctx, p0, p0_len); + HMAC_SHA256_update(&ctx, p1, p1_len); + HMAC_SHA256_update(&ctx, p2, p2_len); + memcpy(k, HMAC_SHA256_final(&ctx), SHA256_DIGEST_SIZE); update_v(k, v); } @@ -179,7 +177,7 @@ static int cmd_rfc6979(int argc, char **argv) static const char message[] = "sample"; static struct drbg_ctx drbg; - static HASH_CTX ctx; + static struct sha256_ctx ctx; int result; static const uint8_t priv_from_rfc[] = { 0xC9, 0xAF, 0xA9, 0xD8, 0x45, 0xBA, 0x75, 0x16, @@ -197,9 +195,9 @@ static int cmd_rfc6979(int argc, char **argv) p256_int *reference_k = (p256_int *)k_from_rfc; /* h1 = H(m) */ - DCRYPTO_SHA256_init(&ctx, 1); - HASH_update(&ctx, message, sizeof(message) - 1); - memcpy(&h1, HASH_final(&ctx), SHA256_DIGEST_SIZE); + SHA256_hw_init(&ctx); + SHA256_update(&ctx, message, sizeof(message) - 1); + memcpy(&h1, SHA256_final(&ctx)->b8, SHA256_DIGEST_SIZE); hmac_drbg_init_rfc6979(&drbg, x, &h1); do { |