Skip to content

Commit 7e54a21

Browse files
h-filalinasahlpa
authored andcommitted
[sw/crypto] Clear HMAC ctx struct
At certain points the HMAC ctx struct has to be wiped to avoid leaking secrets. The sensitive fields need to be overwritten by randomness and the non-sensitive fields are just zeroized. Signed-off-by: Hakim Filali <[email protected]> (cherry picked from commit 7389d6c)
1 parent 6788e8d commit 7e54a21

File tree

8 files changed

+23
-0
lines changed

8 files changed

+23
-0
lines changed

sw/device/lib/crypto/drivers/hmac.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ static status_t oneshot(const uint32_t cfg, const hmac_key_t *key,
460460
// Check that the block is idle.
461461
HARDENED_TRY(ensure_idle());
462462

463+
// Make sure that the entropy complex is configured correctly.
464+
HARDENED_TRY(entropy_complex_check());
465+
463466
// Configure the HMAC block.
464467
abs_mmio_write32(kHmacBaseAddr + HMAC_CFG_REG_OFFSET, cfg);
465468

@@ -751,6 +754,9 @@ hardened_bool_t hmac_key_integrity_checksum_check(const hmac_key_t *key) {
751754
}
752755

753756
status_t hmac_update(hmac_ctx_t *ctx, const uint8_t *data, size_t len) {
757+
// Make sure that the entropy complex is configured correctly.
758+
HARDENED_TRY(entropy_complex_check());
759+
754760
// If we don't have enough new bytes to fill a block, just update the partial
755761
// block and return.
756762
size_t block_bytelen = ctx->msg_block_wordlen * sizeof(uint32_t);
@@ -806,6 +812,9 @@ status_t hmac_update(hmac_ctx_t *ctx, const uint8_t *data, size_t len) {
806812
}
807813

808814
status_t hmac_final(hmac_ctx_t *ctx, uint32_t *digest) {
815+
// Make sure that the entropy complex is configured correctly.
816+
HARDENED_TRY(entropy_complex_check());
817+
809818
// Retore context will restore the context and also hit start or continue
810819
// button as necessary.
811820
HARDENED_TRY(context_restore(ctx));

sw/device/silicon_creator/manuf/base/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ opentitan_binary(
6464
"//sw/device/lib/arch:device",
6565
"//sw/device/lib/base:abs_mmio",
6666
"//sw/device/lib/base:macros",
67+
"//sw/device/lib/crypto/drivers:entropy",
6768
"//sw/device/lib/dif:flash_ctrl",
6869
"//sw/device/lib/dif:lc_ctrl",
6970
"//sw/device/lib/dif:otp_ctrl",
@@ -102,6 +103,7 @@ opentitan_binary(
102103
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
103104
"//sw/device/lib/arch:device",
104105
"//sw/device/lib/base:macros",
106+
"//sw/device/lib/crypto/drivers:entropy",
105107
"//sw/device/lib/dif:flash_ctrl",
106108
"//sw/device/lib/dif:gpio",
107109
"//sw/device/lib/dif:lc_ctrl",
@@ -181,6 +183,7 @@ opentitan_test(
181183
"//sw/device/lib/arch:device",
182184
"//sw/device/lib/base:abs_mmio",
183185
"//sw/device/lib/base:macros",
186+
"//sw/device/lib/crypto/drivers:entropy",
184187
"//sw/device/lib/dif:flash_ctrl",
185188
"//sw/device/lib/dif:gpio",
186189
"//sw/device/lib/dif:otp_ctrl",
@@ -236,6 +239,7 @@ cc_library(
236239
srcs = ["tpm_personalize_ext.c"],
237240
deps = [
238241
":personalize_ext",
242+
"//sw/device/lib/crypto/drivers:entropy",
239243
"//sw/device/lib/dif:flash_ctrl",
240244
"//sw/device/lib/runtime:print",
241245
"//sw/device/lib/testing/test_framework:status",

sw/device/silicon_creator/manuf/base/ft_personalize.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@ bool test_main(void) {
11961196
pinmux_testutils_init(&pinmux);
11971197
CHECK_STATUS_OK(configure_ate_gpio_indicators());
11981198
CHECK_DIF_OK(dif_gpio_write(&gpio, kGpioPinTestStart, true));
1199+
CHECK_STATUS_OK(entropy_complex_init());
11991200
ujson_t uj = ujson_ottf_console();
12001201

12011202
// Read the reset reason directly from the RSTMGR.

sw/device/silicon_creator/manuf/base/sram_cp_provision.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "sw/device/lib/arch/device.h"
88
#include "sw/device/lib/base/abs_mmio.h"
9+
#include "sw/device/lib/crypto/drivers/entropy.h"
910
#include "sw/device/lib/dif/dif_flash_ctrl.h"
1011
#include "sw/device/lib/dif/dif_lc_ctrl.h"
1112
#include "sw/device/lib/dif/dif_otp_ctrl.h"
@@ -224,6 +225,7 @@ bool test_main(void) {
224225
CHECK_STATUS_OK(configure_ate_gpio_indicators());
225226
CHECK_DIF_OK(dif_gpio_write(&gpio, kGpioPinTestStart, true));
226227
ottf_console_init();
228+
CHECK_STATUS_OK(entropy_complex_init());
227229
ujson_t uj = ujson_ottf_console();
228230

229231
// Extract factory data from flash info page 0.

sw/device/silicon_creator/manuf/base/sram_cp_provision_functest.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdint.h>
66

77
#include "sw/device/lib/arch/device.h"
8+
#include "sw/device/lib/crypto/drivers/entropy.h"
89
#include "sw/device/lib/dif/dif_flash_ctrl.h"
910
#include "sw/device/lib/dif/dif_gpio.h"
1011
#include "sw/device/lib/dif/dif_lc_ctrl.h"
@@ -118,6 +119,7 @@ static status_t prep_flash_info_page_0(manuf_cp_test_data_t *test_data) {
118119
bool test_main(void) {
119120
// Initialize peripherals, pinmux, and console.
120121
CHECK_STATUS_OK(peripheral_handles_init());
122+
CHECK_STATUS_OK(entropy_complex_init());
121123
CHECK_STATUS_OK(configure_ate_gpio_indicators());
122124
ottf_console_init();
123125
ujson_t uj = ujson_ottf_console();

sw/device/silicon_creator/manuf/base/sram_ft_individualize.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "sw/device/lib/arch/device.h"
88
#include "sw/device/lib/base/abs_mmio.h"
9+
#include "sw/device/lib/crypto/drivers/entropy.h"
910
#include "sw/device/lib/dif/dif_flash_ctrl.h"
1011
#include "sw/device/lib/dif/dif_gpio.h"
1112
#include "sw/device/lib/dif/dif_otp_ctrl.h"
@@ -174,6 +175,7 @@ static status_t provision(ujson_t *uj) {
174175

175176
bool test_main(void) {
176177
CHECK_STATUS_OK(peripheral_handles_init());
178+
CHECK_STATUS_OK(entropy_complex_init());
177179
CHECK_STATUS_OK(configure_ate_gpio_indicators());
178180
ujson_t uj;
179181
#ifndef ATE

sw/device/silicon_creator/manuf/lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ opentitan_test(
226226
"//hw/ip/otp_ctrl/data:otp_ctrl_c_regs",
227227
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
228228
"//sw/device/lib/base:status",
229+
"//sw/device/lib/crypto/drivers:entropy",
229230
"//sw/device/lib/dif:flash_ctrl",
230231
"//sw/device/lib/dif:otp_ctrl",
231232
"//sw/device/lib/dif:rstmgr",

sw/device/silicon_creator/manuf/lib/individualize_sw_cfg_functest.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include "sw/device/lib/base/status.h"
6+
#include "sw/device/lib/crypto/drivers/entropy.h"
67
#include "sw/device/lib/dif/dif_flash_ctrl.h"
78
#include "sw/device/lib/dif/dif_otp_ctrl.h"
89
#include "sw/device/lib/dif/dif_rstmgr.h"
@@ -172,6 +173,7 @@ static void sw_reset(void) {
172173

173174
bool test_main(void) {
174175
CHECK_STATUS_OK(peripheral_handles_init());
176+
CHECK_STATUS_OK(entropy_complex_init());
175177

176178
// Provision CREATOR_SW_CFG partition.
177179
if (!status_ok(manuf_individualize_device_creator_sw_cfg_check(&otp_ctrl))) {

0 commit comments

Comments
 (0)