|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Application-level PSA Crypto facade over tf-psa-crypto. More...
Go to the source code of this file.
Data Structures | |
| struct | ra8_psa_key_attr |
Typedefs | |
| typedef struct ra8_psa_key_handle * | ra8_psa_key_t |
| Opaque PSA key handle (typed pointer into the static pool). | |
| typedef struct ra8_psa_key_attr | ra8_psa_key_attr_t |
Functions | |
| 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. | |
| 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. | |
| 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. | |
| 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[0..out_len-1] with cryptographically secure bytes. | |
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_psa_crypto is a thin, project-shaped wrapper around the vendored TF-PSA-Crypto 1.x library at libs/third_party/tf-psa-crypto. Where ra8_tls provides session- oriented TLS, this module exposes the application-level PSA Crypto primitives (key import / destroy, signing, hashing, AEAD) behind a small ra8_psa_* API that returns ra8_err_t.
The facade has three jobs:
* +---------------------------+ ra8_psa_sign_hash, ra8_psa_aead_encrypt * | App (OTA / boot / pairing)| * +-------------+-------------+ * | * v * +---------------------------+ ra8_psa_crypto (this header) * | TF-PSA-Crypto 1.x | * +-------------+-------------+ * | * v (optional, when present) * +---------------------------+ ra8_rsip hardware accelerator * | Silicon AES / SHA / ECDSA | * +---------------------------+ *
Definition in file ra8_psa_crypto.h.
| typedef struct ra8_psa_key_attr ra8_psa_key_attr_t |
| typedef struct ra8_psa_key_handle* ra8_psa_key_t |
Opaque PSA key handle (typed pointer into the static pool).
NULL is the sentinel for "uninitialized handle". The only legal way to obtain a non-NULL value is ra8_psa_key_import; passing any other pointer to ra8_psa_* yields k_ra8_err_invalid_arg.
Definition at line 211 of file ra8_psa_crypto.h.
| enum ra8_psa_alg_t : uint8_t |
Algorithm selector passed to sign / verify / AEAD operations.
Definition at line 159 of file ra8_psa_crypto.h.
| enum ra8_psa_key_type_t : uint8_t |
Project-local enum mirroring the PSA key-type families we care about.
Mirrors the subset of PSA_KEY_TYPE_* codes we actually use, mapped back to canonical PSA values inside ra8_psa_crypto.c. Keeping the tag in our own namespace lets callers avoid a transitive include of psa/crypto_values.h.
Definition at line 145 of file ra8_psa_crypto.h.
| enum ra8_psa_key_usage_t : uint32_t |
Bitmask of allowed operations on an imported key.
Equivalent to PSA's psa_key_usage_t flags, restricted to the subset this facade exposes.
| Enumerator | |
|---|---|
| k_ra8_psa_usage_none | No usage allowed (placeholder). |
| k_ra8_psa_usage_sign | Allow ra8_psa_sign_hash. |
| k_ra8_psa_usage_verify | Allow ra8_psa_verify_hash. |
| k_ra8_psa_usage_encrypt | Allow ra8_psa_aead_encrypt. |
| k_ra8_psa_usage_decrypt | Allow ra8_psa_aead_decrypt. |
| k_ra8_psa_usage_derive | Allow KDF-style derivation (future). |
Definition at line 176 of file ra8_psa_crypto.h.
| enum ra8_psa_limits_t : uint8_t |
Static-pool sizing constants for the PSA Crypto facade.
Sized to comfortably cover the worst-case number of long-lived keys we expect in a single boot: a device identity key, two OTA verify keys (current + rollover), a TLS client key, a BLE LTK, and a handful of ephemeral session keys. Increasing the bound only costs a few bytes of .bss per slot.
| Enumerator | |
|---|---|
| k_ra8_psa_max_keys | Maximum simultaneous key handles handed out by the pool. NASA Power of 10 Rule 3 cap: any further import returns k_ra8_err_no_mem. |
| 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). |
| k_ra8_psa_gcm_tag_len | AES-GCM authentication tag length (16 octets). |
| k_ra8_psa_max_sig_bytes | Maximum ECDSA signature length we ever emit (P-384 raw r||s). |
Definition at line 94 of file ra8_psa_crypto.h.
| 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[0..out_len-1] with cryptographically secure bytes.
Wraps psa_generate_random (PSA Crypto API spec, ARM IHI 0086 v1.1.0, Section 10.4 "Random number generation"). On the target the underlying PSA implementation pulls entropy from the RSIP TRNG; in RA8_OFF_TARGET builds a deterministic xorshift32 stream seeded from the call index is used so host-side tests are reproducible.
| [out] | out | Destination buffer that receives out_len bytes. |
| [in] | out_len | Number of bytes to fill; must be > 0. |
| 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. |
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.