summaryrefslogtreecommitdiff
path: root/board/cr50/usb_spi.c
diff options
context:
space:
mode:
authorVadim Sukhomlinov <sukhomlinov@google.com>2021-07-30 08:40:32 -0700
committerCommit Bot <commit-bot@chromium.org>2021-08-12 14:18:48 +0000
commit7ddbd2a9eab0dc54897d6b5bb8ee1d4b3be1fe27 (patch)
tree43356bb71d38ea7f5ea1639855ac3b322d460176 /board/cr50/usb_spi.c
parent43f6e7be087720507e57cf27e9460aae64c3b69a (diff)
downloadchrome-ec-7ddbd2a9eab0dc54897d6b5bb8ee1d4b3be1fe27.tar.gz
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/usb_spi.c')
-rw-r--r--board/cr50/usb_spi.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/board/cr50/usb_spi.c b/board/cr50/usb_spi.c
index 8988ed30e1..6876272297 100644
--- a/board/cr50/usb_spi.c
+++ b/board/cr50/usb_spi.c
@@ -20,6 +20,7 @@
#include "tpm_registers.h"
#include "tpm_vendor_cmds.h"
#include "usb_spi.h"
+#include "usb_spi_board.h"
#include "watchdog.h"
#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)
@@ -670,19 +671,20 @@ static enum vendor_cmd_rc spi_hash_dump(uint8_t *dest, uint32_t offset,
return VENDOR_RC_SUCCESS;
}
-int usb_spi_sha256_start(HASH_CTX *ctx)
+int usb_spi_sha256_start(struct sha256_ctx *ctx)
{
if (get_spi_bus_user() != SPI_BUS_USER_HASH) {
CPRINTS("%s: not enabled", __func__);
return EC_ERROR_BUSY;
}
- DCRYPTO_SHA256_init(ctx, 0);
+ SHA256_hw_init(ctx);
return EC_SUCCESS;
}
-int usb_spi_sha256_update(HASH_CTX *ctx, uint32_t offset, uint32_t size)
+int usb_spi_sha256_update(struct sha256_ctx *ctx, uint32_t offset,
+ uint32_t size)
{
uint8_t data[SPI_HASH_CHUNK_SIZE];
@@ -695,7 +697,7 @@ int usb_spi_sha256_update(HASH_CTX *ctx, uint32_t offset, uint32_t size)
return VENDOR_RC_READ_FLASH_FAIL;
}
/* Update hash */
- HASH_update(ctx, data, this_chunk);
+ SHA256_update(ctx, data, this_chunk);
/* Kick the watchdog every 128 chunks. */
if (((size / SPI_HASH_CHUNK_SIZE) % 128) == 127) {
@@ -709,12 +711,13 @@ int usb_spi_sha256_update(HASH_CTX *ctx, uint32_t offset, uint32_t size)
return EC_SUCCESS;
}
-void usb_spi_sha256_final(HASH_CTX *ctx, void *digest, size_t digest_size)
+void usb_spi_sha256_final(struct sha256_ctx *ctx, void *digest,
+ size_t digest_size)
{
size_t copy_size;
copy_size = MIN(digest_size, SHA256_DIGEST_SIZE);
- memcpy(digest, HASH_final(ctx), copy_size);
+ memcpy(digest, SHA256_final(ctx), copy_size);
if (copy_size < digest_size)
memset((uint8_t *)digest + copy_size, 0,
@@ -724,7 +727,7 @@ void usb_spi_sha256_final(HASH_CTX *ctx, void *digest, size_t digest_size)
static enum vendor_cmd_rc spi_hash_sha256(uint8_t *dest, uint32_t offset,
uint32_t size)
{
- HASH_CTX sha;
+ struct sha256_ctx sha;
CPRINTS("%s: 0x%x 0x%x", __func__, offset, size);
if (size > MAX_SPI_HASH_SIZE)