Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sw/device/lib/crypto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_library(
"//sw/device/lib/crypto/impl:aes",
"//sw/device/lib/crypto/impl:aes_gcm",
"//sw/device/lib/crypto/impl:cmac",
"//sw/device/lib/crypto/impl:cmvp",
"//sw/device/lib/crypto/impl:config",
"//sw/device/lib/crypto/impl:cryptolib_build_info",
"//sw/device/lib/crypto/impl:drbg",
Expand Down
1 change: 1 addition & 0 deletions sw/device/lib/crypto/configs/crypto_fips_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ otcrypto_ecdsa_p384_hash_sign_verify
otcrypto_ecdsa_p384_hash_verify
otcrypto_rsa_hash_sign_verify
otcrypto_rsa_hash_verify
otcrypto_cmvp_service_indicator
88 changes: 88 additions & 0 deletions sw/device/lib/crypto/impl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cc_library(
srcs = ["aes.c"],
hdrs = ["//sw/device/lib/crypto/include:aes.h"],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand All @@ -51,6 +52,7 @@ cc_library(
srcs = ["aes_gcm.c"],
hdrs = ["//sw/device/lib/crypto/include:aes_gcm.h"],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand Down Expand Up @@ -83,6 +85,7 @@ cc_library(
srcs = ["drbg.c"],
hdrs = ["//sw/device/lib/crypto/include:drbg.h"],
deps = [
":cmvp",
":config",
":integrity",
":status",
Expand All @@ -99,6 +102,7 @@ cc_library(
srcs = ["cmac.c"],
hdrs = ["//sw/device/lib/crypto/include:cmac.h"],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand All @@ -122,6 +126,7 @@ cc_library(
}),
target_compatible_with = [OPENTITAN_CPU],
deps = [
":cmvp",
":config",
":hmac",
":integrity",
Expand All @@ -140,6 +145,7 @@ cc_library(
hdrs = ["//sw/device/lib/crypto/include:ecc_p256.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":cmvp",
":config",
":hash",
":keyblob",
Expand All @@ -155,6 +161,7 @@ cc_library(
hdrs = ["//sw/device/lib/crypto/include:ecc_p384.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":cmvp",
":config",
":hash",
":keyblob",
Expand All @@ -169,6 +176,7 @@ cc_library(
srcs = ["hkdf.c"],
hdrs = ["//sw/device/lib/crypto/include:hkdf.h"],
deps = [
":cmvp",
":config",
":cryptolib_build_info",
":hmac",
Expand Down Expand Up @@ -203,6 +211,7 @@ cc_library(
"//conditions:default": [],
}),
deps = [
":cmvp",
":state",
":status",
"//sw/device/lib/base:hardened",
Expand Down Expand Up @@ -266,6 +275,7 @@ cc_library(
srcs = ["kdf_ctr.c"],
hdrs = ["//sw/device/lib/crypto/include:kdf_ctr.h"],
deps = [
":cmvp",
":config",
":hmac",
":integrity",
Expand Down Expand Up @@ -308,6 +318,7 @@ cc_library(
srcs = ["key_transport.c"],
hdrs = ["//sw/device/lib/crypto/include:key_transport.h"],
deps = [
":cmvp",
":config",
":status",
"//sw/device/lib/crypto/drivers:aes",
Expand Down Expand Up @@ -339,6 +350,7 @@ cc_library(
srcs = ["kmac_kdf.c"],
hdrs = ["//sw/device/lib/crypto/include:kmac_kdf.h"],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand All @@ -363,6 +375,7 @@ cc_library(
"//conditions:default": [],
}),
deps = [
":cmvp",
":config",
":hash",
":integrity",
Expand All @@ -386,6 +399,7 @@ cc_library(
"//conditions:default": [],
}),
deps = [
":cmvp",
":cryptolib_build_info",
":entropy_src",
":integrity",
Expand Down Expand Up @@ -432,6 +446,49 @@ dual_cc_library(
),
)

dual_cc_library(
name = "cmvp",
srcs = dual_inputs(
device = ["cmvp.c"],
host = ["cmvp.c"],
),
hdrs = [
"cmvp.h",
"//sw/device/lib/crypto/include:cmvp.h",
],
defines = select({
"//sw/device/lib/crypto/configs:hashed": ["FIPS_MODE"],
"//conditions:default": [],
}),
deps = dual_inputs(
shared = [
":state",
":status",
"//sw/device/lib/crypto/include:datatypes",
],
),
)

dual_cc_library(
name = "cmvp_fips",
srcs = dual_inputs(
device = ["cmvp.c"],
host = ["cmvp.c"],
),
hdrs = [
"cmvp.h",
"//sw/device/lib/crypto/include:cmvp.h",
],
defines = ["FIPS_MODE"],
deps = dual_inputs(
shared = [
":state",
":status",
"//sw/device/lib/crypto/include:datatypes",
],
),
)

cc_library(
name = "hash",
srcs = ["hash.c"],
Expand All @@ -454,6 +511,7 @@ cc_library(
],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":cmvp",
":config",
":integrity",
":status",
Expand All @@ -473,6 +531,7 @@ cc_library(
],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":cmvp",
":config",
":status",
"//sw/device/lib/base:hardened",
Expand Down Expand Up @@ -533,6 +592,7 @@ cc_library(
"//sw/device/lib/crypto/include:hmac.h",
],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand All @@ -551,6 +611,7 @@ cc_library(
"//sw/device/lib/crypto/include:kmac.h",
],
deps = [
":cmvp",
":config",
":integrity",
":keyblob",
Expand All @@ -573,9 +634,36 @@ cc_library(
],
)

cc_test(
name = "cmvp_unittest",
srcs = ["cmvp_unittest.cc"],
defines = ["FIPS_MODE"],
deps = [
":cmvp_fips",
":state",
":status",
"//sw/device/lib/crypto/include:datatypes",
"@googletest//:gtest_main",
],
)

cc_test(
name = "cmvp_nofips_unittest",
srcs = ["cmvp_unittest.cc"],
deps = [
":cmvp",
":state",
":status",
"//sw/device/lib/crypto/include:datatypes",
"@googletest//:gtest_main",
],
)

test_suite(
name = "impl_unittest_suite",
tests = [
":cmvp_nofips_unittest",
":cmvp_unittest",
":integrity_unittest",
":key_transport_unittest",
":keyblob_unittest",
Expand Down
2 changes: 2 additions & 0 deletions sw/device/lib/crypto/impl/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "sw/device/lib/base/memory.h"
#include "sw/device/lib/crypto/drivers/aes.h"
#include "sw/device/lib/crypto/drivers/keymgr.h"
#include "sw/device/lib/crypto/impl/cmvp.h"
#include "sw/device/lib/crypto/impl/keyblob.h"
#include "sw/device/lib/crypto/impl/state.h"
#include "sw/device/lib/crypto/impl/status.h"
Expand Down Expand Up @@ -618,6 +619,7 @@ otcrypto_status_t otcrypto_aes(otcrypto_blinded_key_t *key,
const otcrypto_const_byte_buf_t *cipher_input,
otcrypto_aes_padding_t aes_padding,
otcrypto_byte_buf_t *cipher_output) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
// Check for NULL pointers in input pointers and data buffers.
if (key == NULL || key->keyblob == NULL ||
Expand Down
9 changes: 9 additions & 0 deletions sw/device/lib/crypto/impl/aes_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
#include "sw/device/lib/crypto/impl/aes_gcm/aes_gcm.h"
#include "sw/device/lib/crypto/impl/aes_gcm/ghash.h"
#include "sw/device/lib/crypto/impl/cmvp.h"
#include "sw/device/lib/crypto/impl/keyblob.h"
#include "sw/device/lib/crypto/impl/state.h"
#include "sw/device/lib/crypto/impl/status.h"
Expand Down Expand Up @@ -281,6 +282,7 @@ otcrypto_status_t otcrypto_aes_gcm_encrypt(
const otcrypto_const_word32_buf_t *iv, const otcrypto_const_byte_buf_t *aad,
otcrypto_aes_gcm_tag_len_t tag_len, otcrypto_byte_buf_t *ciphertext,
otcrypto_word32_buf_t *auth_tag) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_ENCRYPT);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
// Check for NULL pointers in input pointers and required-nonzero-length data
// buffers.
Expand Down Expand Up @@ -340,6 +342,7 @@ otcrypto_status_t otcrypto_aes_gcm_decrypt(
otcrypto_aes_gcm_tag_len_t tag_len,
const otcrypto_const_word32_buf_t *auth_tag, otcrypto_byte_buf_t *plaintext,
hardened_bool_t *success) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_DECRYPT);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
// Check for NULL pointers in input pointers and required-nonzero-length data
// buffers.
Expand Down Expand Up @@ -392,6 +395,7 @@ otcrypto_status_t otcrypto_aes_gcm_decrypt(
otcrypto_status_t otcrypto_aes_gcm_encrypt_init(
otcrypto_blinded_key_t *key, const otcrypto_const_word32_buf_t *iv,
otcrypto_aes_gcm_context_t *ctx) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_ENCRYPT_INIT);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (key == NULL || key->keyblob == NULL || iv == NULL || iv->data == NULL ||
ctx == NULL) {
Expand Down Expand Up @@ -428,6 +432,7 @@ otcrypto_status_t otcrypto_aes_gcm_encrypt_init(
otcrypto_status_t otcrypto_aes_gcm_decrypt_init(
otcrypto_blinded_key_t *key, const otcrypto_const_word32_buf_t *iv,
otcrypto_aes_gcm_context_t *ctx) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_DECRYPT_INIT);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (key == NULL || key->keyblob == NULL || iv == NULL || iv->data == NULL ||
ctx == NULL) {
Expand Down Expand Up @@ -463,6 +468,7 @@ otcrypto_status_t otcrypto_aes_gcm_decrypt_init(

otcrypto_status_t otcrypto_aes_gcm_update_aad(
otcrypto_aes_gcm_context_t *ctx, const otcrypto_const_byte_buf_t *aad) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_UPDATE_AAD);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || aad == NULL || aad->data == NULL) {
return OTCRYPTO_BAD_ARGS;
Expand Down Expand Up @@ -500,6 +506,7 @@ otcrypto_status_t otcrypto_aes_gcm_update_aad(
otcrypto_status_t otcrypto_aes_gcm_update_encrypted_data(
otcrypto_aes_gcm_context_t *ctx, const otcrypto_const_byte_buf_t *input,
otcrypto_byte_buf_t *output, size_t *output_bytes_written) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_UPDATE_ENCRYPTED_DATA);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || input == NULL || input->data == NULL || output == NULL ||
output->data == NULL || output_bytes_written == NULL) {
Expand Down Expand Up @@ -557,6 +564,7 @@ otcrypto_status_t otcrypto_aes_gcm_encrypt_final(
otcrypto_aes_gcm_context_t *ctx, otcrypto_aes_gcm_tag_len_t tag_len,
otcrypto_byte_buf_t *ciphertext, size_t *ciphertext_bytes_written,
otcrypto_word32_buf_t *auth_tag) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_ENCRYPT_FINAL);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || ciphertext_bytes_written == NULL || auth_tag == NULL ||
auth_tag->data == NULL) {
Expand Down Expand Up @@ -612,6 +620,7 @@ otcrypto_status_t otcrypto_aes_gcm_decrypt_final(
const otcrypto_const_word32_buf_t *auth_tag,
otcrypto_aes_gcm_tag_len_t tag_len, otcrypto_byte_buf_t *plaintext,
size_t *plaintext_bytes_written, hardened_bool_t *success) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_AES_GCM_DECRYPT_FINAL);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || plaintext_bytes_written == NULL || auth_tag == NULL ||
auth_tag->data == NULL || success == NULL) {
Expand Down
17 changes: 17 additions & 0 deletions sw/device/lib/crypto/impl/cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "sw/device/lib/base/hardened_memory.h"
#include "sw/device/lib/crypto/drivers/aes.h"
#include "sw/device/lib/crypto/drivers/keymgr.h"
#include "sw/device/lib/crypto/impl/cmvp.h"
#include "sw/device/lib/crypto/impl/keyblob.h"
#include "sw/device/lib/crypto/impl/state.h"
#include "sw/device/lib/crypto/impl/status.h"
Expand Down Expand Up @@ -342,12 +343,19 @@ OT_WARN_UNUSED_RESULT
otcrypto_status_t otcrypto_cmac(const otcrypto_blinded_key_t *key,
const otcrypto_const_byte_buf_t *input_message,
otcrypto_word32_buf_t *tag) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_CMAC);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (tag == NULL || tag->data == NULL || input_message == NULL ||
(input_message->data == NULL && input_message->len != 0)) {
return OTCRYPTO_BAD_ARGS;
}
#endif
// Tag length must be between 2 and 4 words (64 to 128 bits) per SP 800-38B.
if (launder32(tag->len) < 2 || launder32(tag->len) > kAesBlockNumWords) {
return OTCRYPTO_BAD_ARGS;
}
HARDENED_CHECK_GE(tag->len, 2);
HARDENED_CHECK_LE(tag->len, kAesBlockNumWords);

// Clear AES unconditionally, and clear sideload if requested.
uint32_t hw_cleanup_guard __attribute__((cleanup(aes_wipe_guard))) = 1;
Expand Down Expand Up @@ -435,6 +443,7 @@ otcrypto_status_t otcrypto_cmac(const otcrypto_blinded_key_t *key,

otcrypto_status_t otcrypto_cmac_init(otcrypto_cmac_context_t *ctx,
const otcrypto_blinded_key_t *key) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_CMAC_INIT);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL) {
return OTCRYPTO_BAD_ARGS;
Expand Down Expand Up @@ -501,6 +510,7 @@ otcrypto_status_t otcrypto_cmac_init(otcrypto_cmac_context_t *ctx,
otcrypto_status_t otcrypto_cmac_update(
otcrypto_cmac_context_t *const ctx,
const otcrypto_const_byte_buf_t *input_message) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_CMAC_UPDATE);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || input_message == NULL) {
return OTCRYPTO_BAD_ARGS;
Expand Down Expand Up @@ -554,11 +564,18 @@ otcrypto_status_t otcrypto_cmac_update(

otcrypto_status_t otcrypto_cmac_final(otcrypto_cmac_context_t *const ctx,
otcrypto_word32_buf_t *tag) {
OTCRYPTO_SET_CMVP_INDICATOR(OTCRYPTO_FUNCTION_CMAC_FINAL);
#ifndef OTCRYPTO_DISABLE_NULL_CHECKS
if (ctx == NULL || tag == NULL || tag->data == NULL) {
return OTCRYPTO_BAD_ARGS;
}
#endif
// Tag length must be between 2 and 4 words (64 to 128 bits) per SP 800-38B.
if (launder32(tag->len) < 2 || launder32(tag->len) > kAesBlockNumWords) {
return OTCRYPTO_BAD_ARGS;
}
HARDENED_CHECK_GE(tag->len, 2);
HARDENED_CHECK_LE(tag->len, kAesBlockNumWords);

uint32_t hw_cleanup_guard __attribute__((cleanup(aes_wipe_guard))) = 1;
barrier32(hw_cleanup_guard);
Expand Down
Loading
Loading