Skip to content
Open
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
2 changes: 1 addition & 1 deletion sw/device/lib/crypto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ot_static_library(
deps = [
"//sw/device/lib/crypto/impl:aes",
"//sw/device/lib/crypto/impl:aes_gcm",
"//sw/device/lib/crypto/impl:config",
"//sw/device/lib/crypto/impl:drbg",
"//sw/device/lib/crypto/impl:ecc_curve25519",
"//sw/device/lib/crypto/impl:ecc_p256",
Expand All @@ -26,7 +27,6 @@ ot_static_library(
"//sw/device/lib/crypto/impl:kmac",
"//sw/device/lib/crypto/impl:kmac_kdf",
"//sw/device/lib/crypto/impl:rsa",
"//sw/device/lib/crypto/impl:security_config",
"//sw/device/lib/crypto/impl:sha2",
"//sw/device/lib/crypto/impl:sha3",
"//sw/device/lib/crypto/impl:x25519",
Expand Down
38 changes: 38 additions & 0 deletions sw/device/lib/crypto/drivers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ CRYPTO_EXEC_ENVS = dicts.add(

autogen_cryptolib_build_info(name = "cryptolib_build_info")

cc_library(
name = "alert",
srcs = ["alert.c"],
hdrs = [
"alert.h",
],
deps = [
"//hw/top_earlgrey:alert_handler_c_regs",
"//hw/top_earlgrey/ip/sensor_ctrl/data:sensor_ctrl_c_regs",
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
"//sw/device/lib/base:abs_mmio",
"//sw/device/lib/base:bitfield",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/base:macros",
"//sw/device/lib/crypto/impl:status",
],
)

opentitan_test(
name = "alert_test",
srcs = ["alert_test.c"],
exec_env = CRYPTO_EXEC_ENVS,
verilator = verilator_params(
timeout = "long",
),
deps = [
":alert",
"//sw/device/lib/base:abs_mmio",
"//sw/device/lib/base:bitfield",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/base:macros",
"//sw/device/lib/crypto/impl:status",
"//sw/device/lib/testing/test_framework:check",
"//sw/device/lib/testing/test_framework:ottf_alerts",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)

cc_library(
name = "aes",
srcs = ["aes.c"],
Expand Down
85 changes: 85 additions & 0 deletions sw/device/lib/crypto/drivers/alert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/crypto/drivers/alert.h"

#include "sw/device/lib/base/abs_mmio.h"
#include "sw/device/lib/base/bitfield.h"
#include "sw/device/lib/base/hardened.h"
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/crypto/impl/status.h"

#include "alert_handler_regs.h" // Generated
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
#include "sensor_ctrl_regs.h" // Generated.

uint32_t read_alert_registers(void) {
// Read the sensors
uint32_t sensors_recov_alerts =
abs_mmio_read32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_RECOV_ALERT_REG_OFFSET);
uint32_t sensors_fatal_alerts =
abs_mmio_read32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_FATAL_ALERT_REG_OFFSET);
uint32_t combined_alerts = sensors_recov_alerts | sensors_fatal_alerts;

// Read out the alerts
for (uint32_t alert = 0; alert < ALERT_HANDLER_PARAM_N_ALERTS; alert++) {
uint32_t cause = abs_mmio_read32(TOP_EARLGREY_ALERT_HANDLER_BASE_ADDR +
(alert * sizeof(uint32_t)) +
ALERT_HANDLER_ALERT_CAUSE_0_REG_OFFSET);
combined_alerts |= cause;
}
// Read out the local alerts
for (uint32_t local_alert = 0; local_alert < ALERT_HANDLER_PARAM_N_LOC_ALERT;
local_alert++) {
uint32_t local_cause =
abs_mmio_read32(TOP_EARLGREY_ALERT_HANDLER_BASE_ADDR +
(local_alert * sizeof(uint32_t)) +
ALERT_HANDLER_LOC_ALERT_CAUSE_0_REG_OFFSET);
combined_alerts |= local_cause;
}
return combined_alerts;
}

status_t init_alert_registers(void) {
// Clear the sensors.
abs_mmio_write32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_RECOV_ALERT_REG_OFFSET,
SENSOR_CTRL_RECOV_ALERT_REG_RESVAL);
abs_mmio_write32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_FATAL_ALERT_REG_OFFSET,
SENSOR_CTRL_FATAL_ALERT_REG_RESVAL);
abs_mmio_write32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_ALERT_TEST_REG_OFFSET,
SENSOR_CTRL_ALERT_TEST_REG_RESVAL);
abs_mmio_write32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_ALERT_TRIG_REG_OFFSET,
SENSOR_CTRL_ALERT_TRIG_REG_RESVAL);
abs_mmio_write32(TOP_EARLGREY_SENSOR_CTRL_AON_BASE_ADDR +
SENSOR_CTRL_INTR_STATE_REG_OFFSET,
SENSOR_CTRL_INTR_STATE_REG_RESVAL);

// Clear the alerts.
for (uint32_t alert = 0; alert < ALERT_HANDLER_PARAM_N_ALERTS; alert++) {
abs_mmio_write32(TOP_EARLGREY_ALERT_HANDLER_BASE_ADDR +
(alert * sizeof(uint32_t)) +
ALERT_HANDLER_ALERT_CAUSE_0_REG_OFFSET,
0x1);
}

// Clear the local alerts.
for (uint32_t local_alert = 0; local_alert < ALERT_HANDLER_PARAM_N_LOC_ALERT;
local_alert++) {
abs_mmio_write32(TOP_EARLGREY_ALERT_HANDLER_BASE_ADDR +
(local_alert * sizeof(uint32_t)) +
ALERT_HANDLER_LOC_ALERT_CAUSE_0_REG_OFFSET,
0x1);
}

abs_mmio_write32(TOP_EARLGREY_ALERT_HANDLER_BASE_ADDR +
ALERT_HANDLER_INTR_STATE_REG_OFFSET,
0xffffffff);
return OTCRYPTO_OK;
}
29 changes: 29 additions & 0 deletions sw/device/lib/crypto/drivers/alert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#ifndef OPENTITAN_SW_DEVICE_LIB_CRYPTO_DRIVERS_ALERT_H_
#define OPENTITAN_SW_DEVICE_LIB_CRYPTO_DRIVERS_ALERT_H_

#include <stdint.h>

#include "sw/device/lib/crypto/include/datatypes.h"

/**
* Read the alert, the sensors, and the local alert registers.
*
* @return The OR of the alerts, sensors, and local alerts.
* Zero means no alerts were triggered.
*/
OT_WARN_UNUSED_RESULT
uint32_t read_alert_registers(void);

/**
* Clear the alert, the sensors, and the local alert registers.
*
* @return OK when the alerts are cleared.
*/
OT_WARN_UNUSED_RESULT
status_t init_alert_registers(void);

#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_DRIVERS_ALERT_H_
30 changes: 30 additions & 0 deletions sw/device/lib/crypto/drivers/alert_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/crypto/drivers/alert.h"

#include "sw/device/lib/base/abs_mmio.h"
#include "sw/device/lib/base/memory.h"
#include "sw/device/lib/crypto/impl/status.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_alerts.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"

static status_t run_register_test(void) {
// Clear the alerts.
CHECK_STATUS_OK(init_alert_registers());
// Read the alerts, expect there to be none.
CHECK(read_alert_registers() == 0);

return OTCRYPTO_OK;
}

OTTF_DEFINE_TEST_CONFIG();

bool test_main(void) {
CHECK_STATUS_OK(run_register_test());

return true;
}
29 changes: 18 additions & 11 deletions sw/device/lib/crypto/impl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ cc_library(
srcs = ["aes.c"],
hdrs = ["//sw/device/lib/crypto/include:aes.h"],
deps = [
":config",
":integrity",
":keyblob",
":security_config",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/base:memory",
"//sw/device/lib/crypto/drivers:aes",
Expand All @@ -43,9 +43,9 @@ cc_library(
srcs = ["aes_gcm.c"],
hdrs = ["//sw/device/lib/crypto/include:aes_gcm.h"],
deps = [
":config",
":integrity",
":keyblob",
":security_config",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/base:memory",
"//sw/device/lib/crypto/drivers:aes",
Expand All @@ -72,6 +72,7 @@ cc_library(
srcs = ["drbg.c"],
hdrs = ["//sw/device/lib/crypto/include:drbg.h"],
deps = [
":config",
":integrity",
":status",
"//sw/device/lib/base:hardened_memory",
Expand All @@ -88,10 +89,10 @@ cc_library(
hdrs = ["//sw/device/lib/crypto/include:ecc_curve25519.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":config",
":hmac",
":integrity",
":keyblob",
":security_config",
":sha2",
":status",
"//sw/device/lib/base:hardened_memory",
Expand All @@ -107,8 +108,8 @@ cc_library(
hdrs = ["//sw/device/lib/crypto/include:ecc_p256.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":config",
":keyblob",
":security_config",
"//sw/device/lib/crypto/drivers:entropy",
"//sw/device/lib/crypto/drivers:hmac",
"//sw/device/lib/crypto/impl/ecc:p256",
Expand All @@ -122,8 +123,8 @@ cc_library(
hdrs = ["//sw/device/lib/crypto/include:ecc_p384.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":config",
":keyblob",
":security_config",
"//sw/device/lib/crypto/drivers:entropy",
"//sw/device/lib/crypto/drivers:hmac",
"//sw/device/lib/crypto/impl/ecc:p384",
Expand All @@ -136,6 +137,7 @@ cc_library(
srcs = ["hkdf.c"],
hdrs = ["//sw/device/lib/crypto/include:hkdf.h"],
deps = [
":config",
":hmac",
":integrity",
":keyblob",
Expand All @@ -161,6 +163,7 @@ cc_library(
srcs = ["kdf_ctr.c"],
hdrs = ["//sw/device/lib/crypto/include:kdf_ctr.h"],
deps = [
":config",
":hmac",
":integrity",
":keyblob",
Expand Down Expand Up @@ -200,6 +203,7 @@ cc_library(
srcs = ["key_transport.c"],
hdrs = ["//sw/device/lib/crypto/include:key_transport.h"],
deps = [
":config",
":status",
"//sw/device/lib/crypto/drivers:rv_core_ibex",
"//sw/device/lib/crypto/impl:drbg",
Expand All @@ -224,6 +228,7 @@ cc_library(
srcs = ["kmac_kdf.c"],
hdrs = ["//sw/device/lib/crypto/include:kmac_kdf.h"],
deps = [
":config",
":integrity",
":keyblob",
":status",
Expand All @@ -240,8 +245,8 @@ cc_library(
srcs = ["rsa.c"],
hdrs = ["//sw/device/lib/crypto/include:rsa.h"],
deps = [
":config",
":integrity",
":security_config",
":status",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/crypto/drivers:entropy",
Expand All @@ -254,9 +259,9 @@ cc_library(
)

cc_library(
name = "security_config",
srcs = ["security_config.c"],
hdrs = ["//sw/device/lib/crypto/include:security_config.h"],
name = "config",
srcs = ["config.c"],
hdrs = ["//sw/device/lib/crypto/include:config.h"],
deps = [
":integrity",
":status",
Expand All @@ -275,6 +280,7 @@ cc_library(
],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":config",
":integrity",
":status",
"//sw/device/lib/base:hardened",
Expand All @@ -293,6 +299,7 @@ cc_library(
],
target_compatible_with = [OPENTITAN_CPU],
deps = [
":config",
":status",
"//sw/device/lib/base:hardened",
"//sw/device/lib/base:hardened_memory",
Expand Down Expand Up @@ -346,9 +353,9 @@ cc_library(
"//sw/device/lib/crypto/include:hmac.h",
],
deps = [
":config",
":integrity",
":keyblob",
":security_config",
":sha2",
"//sw/device/lib/base:hardened",
"//sw/device/lib/base:hardened_memory",
Expand All @@ -365,9 +372,9 @@ cc_library(
"//sw/device/lib/crypto/include:kmac.h",
],
deps = [
":config",
":integrity",
":keyblob",
":security_config",
"//sw/device/lib/base:hardened",
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/crypto/drivers:kmac",
Expand Down
Loading
Loading