51#include "psa/crypto.h"
60[[maybe_unused]]
static const char*
const s_ra8_psa_tag =
"ra8_psa_crypto";
85 if (handle ==
nullptr) {
90 if ((handle < base) || (handle >= end)) {
122#ifndef RA8_OFF_TARGET
123 const psa_status_t st = psa_crypto_init();
124 if (st != PSA_SUCCESS) {
147#ifndef RA8_OFF_TARGET
156#ifndef RA8_OFF_TARGET
157 mbedtls_psa_crypto_free();
164#ifndef RA8_OFF_TARGET
171 return PSA_KEY_TYPE_AES;
173 return PSA_KEY_TYPE_HMAC;
175 return PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1);
177 return PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1);
180 return PSA_KEY_TYPE_RAW_DATA;
191 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
193 return PSA_ALG_SHA_256;
203 psa_key_usage_t out = 0;
205 out |= PSA_KEY_USAGE_SIGN_HASH;
208 out |= PSA_KEY_USAGE_VERIFY_HASH;
211 out |= PSA_KEY_USAGE_ENCRYPT;
214 out |= PSA_KEY_USAGE_DECRYPT;
217 out |= PSA_KEY_USAGE_DERIVE;
228 psa_key_attributes_t pa = psa_key_attributes_init();
233 psa_key_id_t kid = 0;
234 const psa_status_t st = psa_import_key(&pa, data, data_len, &kid);
235 if (st != PSA_SUCCESS) {
249 if (out_handle !=
nullptr) {
250 *out_handle =
nullptr;
252 if ((out_handle ==
nullptr) || (
attr ==
nullptr) || (data ==
nullptr) || (data_len == 0U)) {
266 if (slot ==
nullptr) {
270#ifndef RA8_OFF_TARGET
295#ifndef RA8_OFF_TARGET
296 (void)psa_destroy_key(handle->
psa_id);
305 const uint8_t* input,
311 if (out_len !=
nullptr) {
314 if ((out ==
nullptr) || (out_len ==
nullptr)) {
317 if ((input ==
nullptr) && (input_len != 0U)) {
331 ra8_psa_fake_sha256_oneshot(input, input_len, out);
333 size_t produced = 0U;
334 const psa_status_t st =
335 psa_hash_compute(PSA_ALG_SHA_256, input, input_len, out, out_cap, &produced);
336 if (st != PSA_SUCCESS) {
354 if (sig_len !=
nullptr) {
357 if ((hash ==
nullptr) || (sig ==
nullptr) || (sig_len ==
nullptr)) {
385 ra8_psa_fake_sha256_oneshot(buf, handle->
key_len + hash_len, digest);
386 (void)
memcpy(sig, digest,
sizeof(digest));
387 *sig_len =
sizeof(digest);
389 size_t produced = 0U;
390 const psa_status_t st = psa_sign_hash(handle->
psa_id,
391 PSA_ALG_ECDSA(PSA_ALG_SHA_256),
397 if (st != PSA_SUCCESS) {
413 if ((hash ==
nullptr) || (sig ==
nullptr)) {
440 ra8_psa_fake_sha256_oneshot(buf, handle->
key_len + hash_len, digest);
444 diff |= (uint8_t)(digest[i] ^ sig[i]);
448 const psa_status_t st =
449 psa_verify_hash(handle->
psa_id, PSA_ALG_ECDSA(PSA_ALG_SHA_256), hash, hash_len, sig, sig_len);
450 if (st == PSA_SUCCESS) {
453 if (st == PSA_ERROR_INVALID_SIGNATURE) {
463 const uint8_t* nonce,
467 const uint8_t* plain,
471 const size_t* out_len)
473 if ((nonce ==
nullptr) || (out ==
nullptr) || (out_len ==
nullptr)) {
476 if (((plain ==
nullptr) && (plain_len != 0U)) || ((aad ==
nullptr) && (aad_len != 0U))) {
495 const uint8_t* nonce,
499 const uint8_t* plain,
505 if (out_len !=
nullptr) {
525 const ra8_err_t ser = ra8_psa_fake_aead_encrypt(handle,
538 size_t produced = 0U;
539 const psa_status_t st = psa_aead_encrypt(handle->
psa_id,
550 if (st != PSA_SUCCESS) {
562 const uint8_t* nonce,
566 const uint8_t* cipher,
570 const size_t* out_len,
571 size_t* out_plain_len)
573 if ((nonce ==
nullptr) || (cipher ==
nullptr) || (out_len ==
nullptr)) {
576 if ((aad ==
nullptr) && (aad_len != 0U)) {
591 if ((out ==
nullptr) && (plain_len != 0U)) {
594 if (out_cap < plain_len) {
597 *out_plain_len = plain_len;
603 const uint8_t* nonce,
607 const uint8_t* cipher,
613 if (out_len !=
nullptr) {
616 size_t plain_len = 0U;
634 const ra8_err_t sdr = ra8_psa_fake_aead_decrypt(handle,
647 size_t produced = 0U;
648 const psa_status_t st = psa_aead_decrypt(handle->
psa_id,
659 if (st == PSA_ERROR_INVALID_SIGNATURE) {
662 if (st != PSA_SUCCESS) {
698 if (out ==
nullptr) {
712 for (
size_t i = 0U; i < out_len; ++i) {
722 const psa_status_t st = psa_generate_random(out, out_len);
723 if (st != PSA_SUCCESS) {
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
static bool s_initialized
True once display_init has succeeded.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
@ k_ra8_err_crc_mismatch
CRC mismatch detected on received data.
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_exists
Item already exists – cannot create again.
@ k_ra8_err_not_initialized
Module not initialized – _init() not yet called successfully.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_hw_error
Generic hardware fault detected (error flag set, fault interrupt).
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_error(tag, message)
RA8 log error.
static ra8_err_t internal_aead_decrypt_check(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *cipher, size_t cipher_len, const uint8_t *out, size_t out_cap, const size_t *out_len, size_t *out_plain_len)
ra8_err_t ra8_psa_hash_compute(ra8_psa_alg_t alg, const uint8_t *input, size_t input_len, uint8_t *out, size_t out_cap, size_t *out_len)
Compute a one-shot SHA-256 digest.
static ra8_err_t internal_aead_encrypt_check(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *plain, size_t plain_len, const uint8_t *out, size_t out_cap, const size_t *out_len)
static struct ra8_psa_key_handle s_key_pool[k_ra8_psa_max_keys]
Per-handle state pool sized at compile time.
static psa_algorithm_t internal_map_alg(ra8_psa_alg_t alg)
ra8_err_t ra8_psa_key_destroy(ra8_psa_key_t handle)
Destroy a previously-imported key.
ra8_err_t ra8_psa_aead_decrypt(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *cipher, size_t cipher_len, uint8_t *out, size_t out_cap, size_t *out_len)
Decrypt + verify-tag an AES-GCM buffer.
ra8_err_t ra8_psa_crypto_init(void)
One-shot facade initialisation.
ra8_err_t ra8_psa_key_import(ra8_psa_key_t *out_handle, const ra8_psa_key_attr_t *attr, const uint8_t *data, size_t data_len)
Import a raw-byte key into the static pool.
ra8_err_t ra8_psa_verify_hash(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *hash, size_t hash_len, const uint8_t *sig, size_t sig_len)
Verify an ECDSA signature over a pre-computed hash.
ra8_err_t ra8_psa_sign_hash(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *hash, size_t hash_len, uint8_t *sig, size_t sig_cap, size_t *sig_len)
Sign a pre-computed hash with a private ECDSA key.
ra8_err_t ra8_psa_crypto_random(uint8_t *out, size_t out_len)
Fill out with cryptographically secure bytes.
static psa_key_type_t internal_map_key_type(ra8_psa_key_type_t type)
ra8_err_t ra8_psa_crypto_deinit(void)
Symmetric tear-down for ra8_psa_crypto_init.
static psa_key_usage_t internal_map_usage(ra8_psa_key_usage_t usage)
static bool internal_handle_valid(ra8_psa_key_t handle)
static ra8_err_t internal_psa_import_into_slot(struct ra8_psa_key_handle *slot, const ra8_psa_key_attr_t *attr, const uint8_t *data, size_t data_len)
static const char *const s_ra8_psa_tag
Logging tag prefix used by every ra8_psa_crypto log line.
static struct ra8_psa_key_handle * internal_alloc_slot(void)
Locate the first free slot in the pool.
ra8_err_t ra8_psa_aead_encrypt(ra8_psa_key_t handle, ra8_psa_alg_t alg, const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *plain, size_t plain_len, uint8_t *out, size_t out_cap, size_t *out_len)
Encrypt + authenticate a buffer with AES-GCM.
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_psa_key_usage_t
Bitmask of allowed operations on an imported key.
@ k_ra8_psa_usage_encrypt
Allow ra8_psa_aead_encrypt.
@ k_ra8_psa_usage_derive
Allow KDF-style derivation (future).
@ k_ra8_psa_usage_verify
Allow ra8_psa_verify_hash.
@ k_ra8_psa_usage_none
No usage allowed (placeholder).
@ k_ra8_psa_usage_decrypt
Allow ra8_psa_aead_decrypt.
@ k_ra8_psa_usage_sign
Allow ra8_psa_sign_hash.
struct ra8_psa_key_handle * ra8_psa_key_t
Opaque PSA key handle (typed pointer into the static pool).
ra8_psa_key_type_t
Project-local enum mirroring the PSA key-type families we care about.
@ k_ra8_psa_key_type_ecc_p256_pub
ECDSA P-256 public key (uncompressed).
@ k_ra8_psa_key_type_raw
Raw octet string (HKDF input).
@ k_ra8_psa_key_type_aes
Symmetric AES key.
@ k_ra8_psa_key_type_hmac
HMAC key (any hash).
@ k_ra8_psa_key_type_ecc_p256_priv
ECDSA P-256 private key.
ra8_psa_alg_t
Algorithm selector passed to sign / verify / AEAD operations.
@ k_ra8_psa_alg_none
Sentinel for "unset".
@ k_ra8_psa_alg_ecdsa_sha_256
ECDSA over SHA-256 (FIPS 186-4).
@ k_ra8_psa_alg_aes_gcm
AES-GCM AEAD (NIST SP 800-38D).
@ k_ra8_psa_alg_sha_256
SHA-256 hash (FIPS 180-4).
@ k_ra8_psa_max_keys
Maximum simultaneous key handles handed out by the pool.
@ k_ra8_psa_gcm_tag_len
AES-GCM authentication tag length (16 octets).
@ k_ra8_psa_max_key_bytes
Maximum imported raw-key length in bytes (P-384 + AES-256).
@ k_ra8_psa_sha256_len
SHA-256 digest length (RFC 6234, Section 4.1).
@ k_ra8_psa_gcm_nonce_len
AES-GCM nonce length used by ra8_psa_aead_* (NIST SP 800-38D).
Module-private definitions shared across the ra8_psa_crypto TUs.
@ k_xs32_seed
Default seed.
@ k_xs32_byte_m
Mask isolating the low byte of state.
@ k_xs32_shr_b
Right shift.
@ k_xs32_shl_c
Second left shift.
@ k_xs32_shl_a
First left shift.
Attributes describing a key being imported.
Forward declaration of the static-pool slot type.
psa_key_id_t psa_id
Underlying PSA key identifier.
uint8_t key[k_ra8_psa_max_key_bytes]
Raw key material (fake).
ra8_psa_key_attr_t attr
Cached caller attributes.
size_t key_len
Bytes valid in key.
bool in_use
Slot allocated.