Skip to content

Commit

Permalink
[cryptolib, hmac] Update cryptolib + tests for driver changes
Browse files Browse the repository at this point in the history
Signed-off-by: Fatih Balli <[email protected]>
  • Loading branch information
ballifatih committed May 22, 2024
1 parent c6405e9 commit d78658f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
19 changes: 9 additions & 10 deletions sw/device/lib/crypto/impl/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,18 @@ static status_t check_digest_len(otcrypto_hash_digest_t digest) {
OT_WARN_UNUSED_RESULT
static status_t hmac_sha256(otcrypto_const_byte_buf_t message,
otcrypto_hash_digest_t digest) {
HARDENED_CHECK_EQ(digest.len, kHmacDigestNumWords);
HARDENED_CHECK_EQ(digest.len, kHmacSha256DigestWords);
HARDENED_CHECK_EQ(digest.mode, kOtcryptoHashModeSha256);

// Initialize the hardware.
hmac_sha_init();
hmac_ctx_t hwip_ctx;
hmac_digest_t hmac_digest = {
.len = kHmacSha256DigestBytes,
};
HARDENED_TRY(hmac_init(&hwip_ctx, kHmacModeSha256, /*key=*/NULL));
HARDENED_TRY(hmac_update(&hwip_ctx, message.data, message.len));
HARDENED_TRY(hmac_final(&hwip_ctx, &hmac_digest));

// Pass the message and check the length.
hmac_update(message.data, message.len);

// Retrieve the digest and copy it to the destination buffer.
hmac_digest_t hmac_digest;
hmac_final(&hmac_digest);
hardened_memcpy(digest.data, hmac_digest.digest, kHmacDigestNumWords);
hardened_memcpy(digest.data, hmac_digest.digest, kHmacSha256DigestWords);

return OTCRYPTO_OK;
}
Expand Down
6 changes: 3 additions & 3 deletions sw/device/tests/crypto/sha256_functest.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ static const uint8_t kExactBlockExpDigest[] = {
*/
static status_t run_test(otcrypto_const_byte_buf_t msg,
const uint32_t *exp_digest) {
uint32_t act_digest[kHmacDigestNumWords];
uint32_t act_digest[kHmacSha256DigestWords];
otcrypto_hash_digest_t digest_buf = {
.data = act_digest,
.len = kHmacDigestNumWords,
.len = kHmacSha256DigestWords,
.mode = kOtcryptoHashModeSha256,
};
TRY(otcrypto_hash(msg, digest_buf));
TRY_CHECK_ARRAYS_EQ(act_digest, exp_digest, kHmacDigestNumWords);
TRY_CHECK_ARRAYS_EQ(act_digest, exp_digest, kHmacSha256DigestWords);
return OK_STATUS();
}

Expand Down

0 comments on commit d78658f

Please sign in to comment.