29#include "mbedtls/memory_buffer_alloc.h"
30#include "psa/crypto.h"
39typedef enum : uint32_t {
46typedef enum : uint8_t {
77 v = (uint8_t)((c -
'a') + (
int)
k_hex_ten);
78 }
else if (c >=
'A') {
79 v = (uint8_t)((c -
'A') + (
int)
k_hex_ten);
81 v = (uint8_t)(c -
'0');
87static void unhex(
const char* hex, uint8_t* out,
size_t n)
89 for (
size_t i = 0U; i < n; ++i) {
92 out[i] = (uint8_t)((
int)hi << (
int)
k_hex_shift) | lo;
100 unhex(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", want,
k_sha_len);
103 psa_status_t st = psa_hash_compute(PSA_ALG_SHA_256,
104 (
const uint8_t*)
"abc",
109 return (st == PSA_SUCCESS) && (got_len == (size_t)
k_sha_len) &&
115gcm_enc_ok(psa_key_id_t kid,
const uint8_t* nonce,
const uint8_t* pt,
const uint8_t* want_ct)
119 if (psa_aead_encrypt(kid,
129 &n) != PSA_SUCCESS) {
140gcm_dec_ok(psa_key_id_t kid,
const uint8_t* nonce,
const uint8_t* want_ct,
const uint8_t* pt)
144 if (psa_aead_decrypt(kid,
154 &n) != PSA_SUCCESS) {
171 unhex(
"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e24"
172 "49a6b525b16aedf5aa0de657ba637b391aafd255",
176 unhex(
"42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5a"
177 "ac84aa051ba30b396a0aac973d58e091473f59854d5c2af327cd64a62cf35abd2ba6fab4",
181 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
182 psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
183 psa_set_key_algorithm(&attr, PSA_ALG_GCM);
184 psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
185 psa_key_id_t kid = 0;
186 if (psa_import_key(&attr, key,
sizeof(key), &kid) != PSA_SUCCESS) {
190 bool ok =
gcm_enc_ok(kid, nonce, pt, want_ct);
194 (void)psa_destroy_key(kid);
203 unhex(
"60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6",
206 unhex(
"7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299",
210 unhex(
"EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716",
213 unhex(
"F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8",
219 (void)psa_hash_compute(PSA_ALG_SHA_256,
220 (
const uint8_t*)
"sample",
226 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
227 psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_VERIFY_HASH);
228 psa_set_key_algorithm(&attr, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
229 psa_set_key_type(&attr, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
230 psa_key_id_t kid = 0;
231 if (psa_import_key(&attr, pub,
sizeof(pub), &kid) != PSA_SUCCESS) {
236 if (psa_verify_hash(kid, PSA_ALG_ECDSA(PSA_ALG_SHA_256), hash,
sizeof(hash), sig,
sizeof(sig)) !=
241 if (psa_verify_hash(kid, PSA_ALG_ECDSA(PSA_ALG_SHA_256), hash,
sizeof(hash), sig,
sizeof(sig)) !=
242 PSA_ERROR_INVALID_SIGNATURE) {
245 (void)psa_destroy_key(kid);
252 __asm__
volatile(
"wfi");
258 uint32_t cpuclk0_hz = 0U;
279 if (psa_crypto_init() != PSA_SUCCESS) {
void main(void)
Secure fallback main entry point.
static const uint8_t k_kat_diag_init_ok[]
static bool gcm_dec_ok(psa_key_id_t kid, const uint8_t *nonce, const uint8_t *want_ct, const uint8_t *pt)
AES-128-GCM decrypt of want_ct under kid must equal pt.
static bool kat_aes_gcm(void)
GCM spec Test Case 3: AES-128-GCM encrypt then decrypt round-trip.
static bool kat_sha256(void)
FIPS 180-4 SHA-256("abc").
static uint8_t s_kat_heap[k_kat_heap_bytes]
Static heap tf-psa's mbedtls_calloc draws from (no libc heap on target).
kat_const_t
Demo tunables.
@ k_kat_period_ms
Kat period ms.
@ k_kat_heap_bytes
64 KiB static heap for tf-psa mbedtls_calloc.
kat_size_t
Buffer/length sizing constants for the vectors.
@ k_hex_ten
Hex digit A/a == 10.
@ k_gcm_ct
64 ciphertext + 16 tag.
@ k_hex_shift
High-nibble shift.
@ k_abc_len
strlen("abc").
@ k_ec_point
0x04 || X || Y.
@ k_smpl_len
strlen("sample").
@ k_ec_coord
P-256 coordinate width.
static const uint8_t k_kat_diag_boot[]
static const uint8_t k_kat_diag_init_fail[]
static uint8_t hex_nibble(char c)
One hex digit -> nibble (ASCII order: '0'-'9' < 'A'-'F' < 'a'-'f').
static void kat_panic_halt(void)
static bool gcm_enc_ok(psa_key_id_t kid, const uint8_t *nonce, const uint8_t *pt, const uint8_t *want_ct)
AES-128-GCM encrypt of pt under kid must equal want_ct.
static const uint8_t k_kat_msg_ok[]
static bool kat_ecdsa_p256(void)
RFC 6979 A.2.5 ECDSA-P256/SHA-256 verify of "sample" + a tamper check.
static const uint8_t k_kat_msg_fail[]
static void unhex(const char *hex, uint8_t *out, size_t n)
Decode 2*n hex chars into out (n bytes).
static void kat_setup_or_halt(void)
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_toggle(ra8_board_led_id_t led)
Toggle led's output state.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.