|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Implementation of the ra8_psa_crypto facade over TF-PSA-Crypto. More...
#include "ra8_psa_crypto.h"#include <stddef.h>#include <stdint.h>#include <string.h>#include "ra8_attributes.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_log.h"#include "ra8_psa_crypto_internal.h"#include "psa/crypto.h"Go to the source code of this file.
Functions | |
| static bool | internal_handle_valid (ra8_psa_key_t handle) |
| static struct ra8_psa_key_handle * | internal_alloc_slot (void) |
| Locate the first free slot in the pool. | |
| ra8_err_t | ra8_psa_crypto_init (void) |
| One-shot facade initialisation. | |
| ra8_err_t | ra8_psa_crypto_deinit (void) |
| Symmetric tear-down for ra8_psa_crypto_init. | |
| static psa_key_type_t | internal_map_key_type (ra8_psa_key_type_t type) |
| static psa_algorithm_t | internal_map_alg (ra8_psa_alg_t alg) |
| static psa_key_usage_t | internal_map_usage (ra8_psa_key_usage_t usage) |
| 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) |
| 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_key_destroy (ra8_psa_key_t handle) |
| Destroy a previously-imported key. | |
| 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. | |
| 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_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. | |
| 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) |
| 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. | |
| 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_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_random (uint8_t *out, size_t out_len) |
| Fill out with cryptographically secure bytes. | |
Variables | |
| static const char *const | s_ra8_psa_tag = "ra8_psa_crypto" |
| Logging tag prefix used by every ra8_psa_crypto log line. | |
| static struct ra8_psa_key_handle | s_key_pool [k_ra8_psa_max_keys] |
| Per-handle state pool sized at compile time. | |
| static bool | s_initialized |
| One-shot global init flag protecting the pool. | |
Implementation of the ra8_psa_crypto facade over TF-PSA-Crypto.
Hosts the static key-handle pool and the thin translation layer between PSA Crypto status codes (psa_status_t) and ra8_err_t.
TF-PSA-Crypto is only linked into the firmware build when RA8_USE_MBEDTLS=ON (the same option that pulls in Mbed TLS). The host unit-test build (tests/CMakeLists.txt) defines RA8_OFF_TARGET for every translation unit and intentionally does not link the heavy PSA object library; in that mode this file substitutes a tiny in-memory crypto stand-in:
The public ra8_psa_* surface is identical in either build, so a caller migrating from off-target mode to a real firmware image only has to flip the build flag.
Definition in file ra8_psa_crypto.c.
|
static |
Definition at line 560 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, internal_handle_valid(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_psa_alg_aes_gcm, k_ra8_psa_gcm_nonce_len, k_ra8_psa_gcm_tag_len, k_ra8_psa_usage_decrypt, RA8_INTERNAL, and s_initialized.
Referenced by ra8_psa_aead_decrypt().
|
static |
Definition at line 461 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, internal_handle_valid(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_psa_alg_aes_gcm, k_ra8_psa_gcm_nonce_len, k_ra8_psa_gcm_tag_len, k_ra8_psa_usage_encrypt, RA8_INTERNAL, and s_initialized.
Referenced by ra8_psa_aead_encrypt().
|
static |
Locate the first free slot in the pool.
< Nullptr.
Definition at line 101 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::in_use, k_ra8_psa_max_keys, RA8_INTERNAL, and s_key_pool.
Referenced by ra8_psa_key_import().
|
static |
Definition at line 83 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::in_use, k_ra8_psa_max_keys, RA8_INTERNAL, and s_key_pool.
Referenced by internal_aead_decrypt_check(), internal_aead_encrypt_check(), ra8_psa_key_destroy(), ra8_psa_sign_hash(), and ra8_psa_verify_hash().
|
static |
Definition at line 185 of file ra8_psa_crypto.c.
References k_ra8_psa_alg_aes_gcm, k_ra8_psa_alg_ecdsa_sha_256, k_ra8_psa_alg_none, k_ra8_psa_alg_sha_256, and RA8_INTERNAL.
Referenced by internal_psa_import_into_slot().
|
static |
Definition at line 167 of file ra8_psa_crypto.c.
References k_ra8_psa_key_type_aes, k_ra8_psa_key_type_ecc_p256_priv, k_ra8_psa_key_type_ecc_p256_pub, k_ra8_psa_key_type_hmac, k_ra8_psa_key_type_raw, and RA8_INTERNAL.
Referenced by internal_psa_import_into_slot().
|
static |
Definition at line 201 of file ra8_psa_crypto.c.
References k_ra8_psa_usage_decrypt, k_ra8_psa_usage_derive, k_ra8_psa_usage_encrypt, k_ra8_psa_usage_sign, k_ra8_psa_usage_verify, and RA8_INTERNAL.
Referenced by internal_psa_import_into_slot().
|
static |
Definition at line 223 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, internal_map_alg(), internal_map_key_type(), internal_map_usage(), k_ra8_err_hw_error, k_ra8_ok, ra8_psa_key_handle::psa_id, and RA8_INTERNAL.
Referenced by ra8_psa_key_import().
| 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.
Wraps psa_aead_decrypt (PSA spec Sec 11.5). cipher must be ciphertext concatenated with the 16-octet authentication tag, as produced by ra8_psa_aead_encrypt.
| [in] | handle | Key with k_ra8_psa_usage_decrypt set. |
| [in] | alg | Must be k_ra8_psa_alg_aes_gcm. |
| [in] | nonce | 12-byte nonce. |
| [in] | nonce_len | Length of nonce. |
| [in] | aad | Additional authenticated data. |
| [in] | aad_len | Length of aad. |
| [in] | cipher | Ciphertext || tag input. |
| [in] | cipher_len | Length of cipher (must be >= k_ra8_psa_gcm_tag_len). |
| [out] | out | Buffer for plaintext. |
| [in] | out_cap | Capacity of out. |
| [out] | out_len | Plaintext bytes written. |
| k_ra8_ok | Plaintext recovered, tag valid. |
| k_ra8_err_invalid_arg | NULL pointer / alg mismatch. |
| k_ra8_err_invalid_size | Output buffer too small or input too short to contain a tag. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_crc_mismatch | Tag verification failed (ciphertext / aad / nonce / key was tampered with). |
| k_ra8_err_hw_error | Underlying AEAD reported a fault. |
Definition at line 601 of file ra8_psa_crypto.c.
References internal_aead_decrypt_check(), k_ra8_err_crc_mismatch, k_ra8_err_hw_error, k_ra8_ok, and ra8_psa_key_handle::psa_id.
Referenced by internal_one_round_trip().
| 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.
Wraps psa_aead_encrypt (PSA spec Sec 11.4). Output layout is ciphertext concatenated with the 16-octet authentication tag, so out_cap must be >= plain_len + k_ra8_psa_gcm_tag_len.
| [in] | handle | Key with k_ra8_psa_usage_encrypt set. |
| [in] | alg | Must be k_ra8_psa_alg_aes_gcm. |
| [in] | nonce | 12-byte nonce (must be unique per key per call). |
| [in] | nonce_len | Length of nonce (k_ra8_psa_gcm_nonce_len). |
| [in] | aad | Additional authenticated data (may be NULL when aad_len == 0). |
| [in] | aad_len | Length of aad. |
| [in] | plain | Plaintext input. |
| [in] | plain_len | Length of plain. |
| [out] | out | Buffer for ciphertext || tag. |
| [in] | out_cap | Capacity of out. |
| [out] | out_len | Bytes written (== plain_len + k_ra8_psa_gcm_tag_len on success). |
| k_ra8_ok | Ciphertext + tag written. |
| k_ra8_err_invalid_arg | NULL pointer / alg mismatch. |
| k_ra8_err_invalid_size | Output buffer too small or nonce wrong length. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_hw_error | Underlying AEAD reported a fault. |
Definition at line 493 of file ra8_psa_crypto.c.
References internal_aead_encrypt_check(), k_ra8_err_hw_error, k_ra8_ok, and ra8_psa_key_handle::psa_id.
Referenced by internal_one_round_trip().
| ra8_err_t ra8_psa_crypto_deinit | ( | void | ) |
Symmetric tear-down for ra8_psa_crypto_init.
Destroys every still-imported key, calls mbedtls_psa_crypto_free (or the fake stand-in), and clears the initialized flag so a subsequent ra8_psa_crypto_init succeeds again.
| k_ra8_ok | Facade torn down. |
| k_ra8_err_not_initialized | ra8_psa_crypto_init was never called. |
Definition at line 139 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::in_use, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_psa_max_keys, ra8_psa_key_handle::key, memset(), ra8_psa_key_handle::psa_id, s_initialized, and s_key_pool.
| ra8_err_t ra8_psa_crypto_init | ( | void | ) |
One-shot facade initialisation.
Calls psa_crypto_init (PSA Crypto API spec Sec 8.2), zeroes the key-handle pool, and marks the module ready. Safe to call exactly once per boot; subsequent calls without a matching ra8_psa_crypto_deinit return k_ra8_err_exists.
Algorithm:
| k_ra8_ok | Facade ready. |
| k_ra8_err_exists | Already initialized this boot. |
| k_ra8_err_hw_error | TF-PSA-Crypto initialisation reported a fault. |
Definition at line 116 of file ra8_psa_crypto.c.
References k_ra8_err_exists, k_ra8_err_hw_error, k_ra8_ok, k_ra8_psa_max_keys, ra8_log_error, s_initialized, s_key_pool, and s_ra8_psa_tag.
Referenced by blc_setup_or_halt(), internal_rng_demo_setup_or_halt(), internal_setup_or_halt(), main(), rot_setup_or_halt(), and sb_setup_or_halt().
|
nodiscard |
Fill out with cryptographically secure bytes.
Fill out[0..out_len-1] with cryptographically secure bytes.
See the matching header declaration for the full contract. On the target this delegates to psa_generate_random; in RA8_OFF_TARGET builds a deterministic xorshift32 stream is used so host-side tests are reproducible across runs.
| [out] | out | Destination buffer. |
| [in] | out_len | Number of bytes requested. |
| k_ra8_ok | out filled with out_len bytes. |
| k_ra8_err_invalid_arg | out was NULL. |
| k_ra8_err_invalid_size | out_len was zero. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_hw_error | Underlying psa_generate_random failed. |
Definition at line 696 of file ra8_psa_crypto.c.
References k_ra8_err_hw_error, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_ok, k_xs32_byte_m, k_xs32_seed, k_xs32_shl_a, k_xs32_shl_c, k_xs32_shr_b, ra8_log_error, s_initialized, s_ra8_psa_tag, and s_state.
Referenced by internal_rng_demo_emit_one_line().
| 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.
Wraps psa_hash_compute (PSA spec Sec 10.2.1). The output buffer must be at least k_ra8_psa_sha256_len bytes; *out_len is updated with the bytes actually written (always 32 on success).
| [in] | alg | Hash algorithm; must equal k_ra8_psa_alg_sha_256 for this revision of the facade. |
| [in] | input | Input message bytes. |
| [in] | input_len | Length of input in bytes. |
| [out] | out | Buffer that receives the digest. |
| [in] | out_cap | Capacity of out in bytes (>= k_ra8_psa_sha256_len). |
| [out] | out_len | Bytes actually written. |
| k_ra8_ok | Digest written. |
| k_ra8_err_invalid_arg | Pointer NULL or wrong algorithm. |
| k_ra8_err_invalid_size | out_cap too small. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_hw_error | PSA reported a fatal error. |
Definition at line 304 of file ra8_psa_crypto.c.
References k_ra8_err_hw_error, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_psa_alg_sha_256, k_ra8_psa_sha256_len, and s_initialized.
| ra8_err_t ra8_psa_key_destroy | ( | ra8_psa_key_t | handle | ) |
Destroy a previously-imported key.
Calls psa_destroy_key on the underlying PSA key id, zeroes the slot, and clears the in-use bit. Safe to call on any imported key.
| [in,out] | handle | Key handle previously returned by ra8_psa_key_import. |
| k_ra8_ok | Key destroyed. |
| k_ra8_err_invalid_arg | Handle NULL or not from this pool. |
| k_ra8_err_not_initialized | Facade not initialized. |
Definition at line 286 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::in_use, internal_handle_valid(), k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_ok, ra8_psa_key_handle::key, ra8_psa_key_handle::key_len, memset(), ra8_psa_key_handle::psa_id, and s_initialized.
Referenced by internal_one_round_trip().
| 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.
Allocates the first free slot, copies data into the underlying PSA key store via psa_import_key (PSA spec Sec 9.5), and returns an opaque handle through out_handle.
| [out] | out_handle | Receives the new opaque handle on success. Set to NULL on any non-success return. |
| [in] | attr | Key attributes (type, algorithm, usage). |
| [in] | data | Raw key bytes; layout depends on attr->type. |
| [in] | data_len | Length of data in bytes; must be <= k_ra8_psa_max_key_bytes. |
| k_ra8_ok | Key imported and handle valid. |
| k_ra8_err_invalid_arg | NULL pointer or malformed attributes. |
| k_ra8_err_invalid_size | data_len exceeds the static cap. |
| k_ra8_err_not_initialized | Facade was never initialized. |
| k_ra8_err_no_mem | Pool exhausted (k_ra8_psa_max_keys). |
| k_ra8_err_hw_error | Underlying psa_import_key rejected the key (bad key material). |
Definition at line 244 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, ra8_psa_key_handle::in_use, internal_alloc_slot(), internal_psa_import_into_slot(), k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_no_mem, k_ra8_err_not_initialized, k_ra8_ok, k_ra8_psa_max_key_bytes, k_ra8_psa_usage_none, ra8_psa_key_handle::key, ra8_psa_key_handle::key_len, memcpy(), and s_initialized.
Referenced by internal_one_round_trip().
| 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.
Wraps psa_sign_hash (PSA spec Sec 12.6). Only ECDSA over SHA-256 is exercised; other algorithms return k_ra8_err_not_supported. The caller is responsible for hashing the message first – ra8_psa_sign_hash does not hash hash, it signs the bytes verbatim per the PSA contract.
| [in] | handle | Private-key handle with k_ra8_psa_usage_sign. |
| [in] | alg | Signature algorithm; must be k_ra8_psa_alg_ecdsa_sha_256. |
| [in] | hash | Pre-computed digest bytes. |
| [in] | hash_len | Length of hash (32 for SHA-256). |
| [out] | sig | Output buffer for the signature. |
| [in] | sig_cap | Capacity of sig (>= k_ra8_psa_max_sig_bytes). |
| [out] | sig_len | Bytes actually written. |
| k_ra8_ok | Signature emitted. |
| k_ra8_err_invalid_arg | NULL pointer or alg mismatch. |
| k_ra8_err_invalid_size | Output buffer too small. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_not_supported | Key type / alg not implemented. |
| k_ra8_err_hw_error | Underlying psa_sign_hash failed. |
Definition at line 346 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, internal_handle_valid(), k_ra8_err_hw_error, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_err_not_supported, k_ra8_ok, k_ra8_psa_alg_ecdsa_sha_256, k_ra8_psa_max_key_bytes, k_ra8_psa_sha256_len, k_ra8_psa_usage_sign, ra8_psa_key_handle::key, ra8_psa_key_handle::key_len, memcpy(), ra8_psa_key_handle::psa_id, and s_initialized.
| 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.
Wraps psa_verify_hash (PSA spec Sec 12.7). Returns k_ra8_ok on a valid signature, k_ra8_err_crc_mismatch on a structurally well-formed but invalid signature, and an k_ra8_err_* value for other failures.
| [in] | handle | Public-key handle with k_ra8_psa_usage_verify. |
| [in] | alg | Signature algorithm; must be k_ra8_psa_alg_ecdsa_sha_256. |
| [in] | hash | Pre-computed digest bytes. |
| [in] | hash_len | Length of hash. |
| [in] | sig | Signature bytes. |
| [in] | sig_len | Length of sig. |
| k_ra8_ok | Signature is valid. |
| k_ra8_err_crc_mismatch | Signature is invalid. |
| k_ra8_err_invalid_arg | Pointer NULL or alg mismatch. |
| k_ra8_err_not_initialized | Facade not initialized. |
| k_ra8_err_not_supported | Algorithm not implemented. |
| k_ra8_err_hw_error | Underlying PSA call reported a fault. |
Definition at line 406 of file ra8_psa_crypto.c.
References ra8_psa_key_handle::attr, internal_handle_valid(), k_ra8_err_crc_mismatch, k_ra8_err_hw_error, k_ra8_err_invalid_arg, k_ra8_err_invalid_size, k_ra8_err_not_initialized, k_ra8_err_not_supported, k_ra8_ok, k_ra8_psa_alg_ecdsa_sha_256, k_ra8_psa_max_key_bytes, k_ra8_psa_sha256_len, k_ra8_psa_usage_verify, ra8_psa_key_handle::key, ra8_psa_key_handle::key_len, memcpy(), ra8_psa_key_handle::psa_id, and s_initialized.
|
static |
One-shot global init flag protecting the pool.
Definition at line 75 of file ra8_psa_crypto.c.
|
static |
Per-handle state pool sized at compile time.
Definition at line 72 of file ra8_psa_crypto.c.
Referenced by internal_alloc_slot(), internal_handle_valid(), ra8_psa_crypto_deinit(), and ra8_psa_crypto_init().
|
static |
Logging tag prefix used by every ra8_psa_crypto log line.
Definition at line 60 of file ra8_psa_crypto.c.
Referenced by ra8_psa_crypto_init(), and ra8_psa_crypto_random().