|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Secure-side sealed-key import implementation. More...
#include <stdint.h>#include "key_import_internal.h"#include "key_vault.h"#include "ra8_attributes.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_secure.h"#include "sec_cmac_internal.h"Go to the source code of this file.
Enumerations | |
| enum | rotate_mask_t : uint8_t { k_rotate_mask_5bit = 31U } |
| 5-bit rotate-amount mask (mod 32). More... | |
| enum | ra8_key_import_internal_t : uint8_t { k_handle_rotate_bits = 13U , k_salt_reroll_rot = 7U } |
| Handle-obfuscation shift constants. More... | |
| enum | ra8_key_import_mask_t : uint32_t { k_initial_salt = 0xA5A5A5A5U , k_handle_high_bit_mask = 0x80000000U , k_salt_reroll_xor = 0xDEADBEEFU } |
| 32-bit handle/salt mixing masks and seeds. More... | |
Functions | |
| static uint32_t | internal_rotate_left_32 (uint32_t value, uint8_t amount) |
| Rotate a 32-bit value left by amount bits (mod 32). | |
| static uint32_t | internal_handle_for_slot (uint16_t slot) |
| Compute the opaque NS-side handle for a vault slot index. | |
| static ra8_err_t | internal_verify_cmac (const uint8_t *blob) |
| Verify the trailing AES-CMAC of a sealed key blob. | |
| ra8_err_t | priv_ra8_key_import_reset (void) |
| Reset the import allocator and reroll the per-boot salt. | |
| ra8_err_t | priv_ra8_key_import_seal (const uint8_t *blob, uint32_t blob_len, uint32_t *out_handle) |
| Verify, store, and assign an opaque handle for a sealed key blob. | |
| ra8_err_t | priv_ra8_key_import_resolve (uint32_t handle, uint16_t *out_slot) |
| Resolve a previously issued handle back to its vault slot. | |
| ra8_err_t | priv_ra8_key_import_build_blob (const uint8_t *material, uint8_t *out_blob) |
| Build a sealed key blob from a raw 32-byte key (provisioning + test). | |
Variables | |
| static const char * | s_tag = "KEYIMP" |
| static uint16_t | s_slot_used = 0U |
| One bit per vault slot: 1 if currently allocated. | |
| static uint32_t | s_salt = (uint32_t)k_initial_salt |
| Per-boot 32-bit salt used for handle obfuscation. | |
Secure-side sealed-key import implementation.
Sits between the NSC veneer ra8_nsc_key_import and the underlying ra8_key_vault_store. The job here is twofold:
The KAK is provisioned once, secure-side, via ra8_key_vault_set_mac_key and read back only through ra8_key_vault_load_mac_key – it lives in vault storage that is separate from the NS-importable slot array, so nothing the Non-Secure world can reach ever keys the MAC.
Definition in file key_import.c.
| enum ra8_key_import_internal_t : uint8_t |
Handle-obfuscation shift constants.
Rotating the 32-bit salt before XORing into the 16-bit slot decorrelates the low 16 bits of the salt from the high 16, so a caller who can observe many handles cannot recover the salt by differencing them.
| Enumerator | |
|---|---|
| k_handle_rotate_bits | Salt rotate amount before slot XOR. |
| k_salt_reroll_rot | Salt reroll rotate amount. |
Definition at line 63 of file key_import.c.
| enum ra8_key_import_mask_t : uint32_t |
32-bit handle/salt mixing masks and seeds.
| Enumerator | |
|---|---|
| k_initial_salt | Boot salt seed. |
| k_handle_high_bit_mask | Forces a non-zero handle. |
| k_salt_reroll_xor | Salt reroll mixing const. |
Definition at line 69 of file key_import.c.
| enum rotate_mask_t : uint8_t |
5-bit rotate-amount mask (mod 32).
| Enumerator | |
|---|---|
| k_rotate_mask_5bit | Rotate mask 5bit. |
Definition at line 49 of file key_import.c.
|
static |
Compute the opaque NS-side handle for a vault slot index.
Mixes the per-boot salt with the slot index so two boots vend different handles for the same slot, then forces bit 31 high so the value never collides with the reserved zero sentinel.
| [in] | slot | Slot index (0..k_ra8_key_vault_slots-1). |
| Always | a value with bit 31 set, never 0. |
Definition at line 152 of file key_import.c.
References internal_rotate_left_32(), k_handle_high_bit_mask, k_handle_rotate_bits, RA8_INTERNAL, and s_salt.
Referenced by priv_ra8_key_import_resolve(), and priv_ra8_key_import_seal().
|
static |
Rotate a 32-bit value left by amount bits (mod 32).
Used by both the salt rerolling step and the handle mixing so the bit distribution is well spread for sparse inputs.
| [in] | value | Source 32-bit word. |
| [in] | amount | Bit count; only the low 5 bits are used. |
| ``value`` | when amount mod 32 == 0. |
Definition at line 122 of file key_import.c.
References k_rotate_mask_5bit, and RA8_INTERNAL.
Referenced by internal_handle_for_slot(), and priv_ra8_key_import_reset().
|
static |
Verify the trailing AES-CMAC of a sealed key blob.
Loads the KAK from the vault, recomputes the AES-CMAC over the 32 key bytes via priv_ra8_sec_cmac_verify, and reports whether it matches the trailing k_ra8_key_import_mac_bytes of the blob. The KAK copy is wiped before return so no key material lingers on the secure stack.
| [in] | blob | Sealed key blob; k_ra8_key_import_blob_bytes long. |
| k_ra8_ok | CMAC authentic under the vault KAK. |
| k_ra8_err_invalid_arg | CMAC mismatch (blob tampered / wrong KAK). |
| k_ra8_err_not_found | No KAK provisioned in the vault. |
Definition at line 185 of file key_import.c.
References k_ra8_key_import_key_bytes, k_ra8_key_import_mac_bytes, k_ra8_key_vault_mac_key_bytes, k_ra8_ok, priv_ra8_sec_cmac_verify(), RA8_INTERNAL, ra8_key_vault_load_mac_key(), and ra8_secure_memzero().
Referenced by priv_ra8_key_import_seal().
|
nodiscard |
Build a sealed key blob from a raw 32-byte key (provisioning + test).
Build a sealed blob from a raw key (provisioning + test helper).
Copies the key bytes verbatim, computes the AES-CMAC over them with the vault KAK via priv_ra8_sec_cmac_compute, and writes the trailing k_ra8_key_import_mac_bytes of the blob. Provided so provisioning and unit tests can package a key the import path will accept.
| [in] | material | Raw 32-byte key material. |
| [out] | out_blob | Receives k_ra8_key_import_blob_bytes of output. |
| k_ra8_ok | Blob written. |
| k_ra8_err_null_ptr | material or out_blob was NULL. |
| k_ra8_err_not_found | No KAK provisioned in the vault. |
Definition at line 369 of file key_import.c.
References k_ra8_key_import_key_bytes, k_ra8_key_vault_mac_key_bytes, k_ra8_ok, priv_ra8_sec_cmac_compute(), RA8_CHECK_NULL_PTR, ra8_key_vault_load_mac_key(), ra8_secure_memzero(), and s_tag.
|
nodiscard |
Reset the import allocator and reroll the per-boot salt.
Reset the import table (drops every handle) and re-seed the salt.
Clears every s_slot_used bit and rotates the salt with a fixed mixing constant so successive resets vend different handles for the same slot index. Falls back to the boot seed if the rerolled salt happens to be zero.
| k_ra8_ok | Always; the operation cannot fail. |
Definition at line 224 of file key_import.c.
References internal_rotate_left_32(), k_initial_salt, k_ra8_ok, k_salt_reroll_rot, k_salt_reroll_xor, s_salt, and s_slot_used.
|
nodiscard |
Resolve a previously issued handle back to its vault slot.
Resolve a handle to the underlying slot index.
Walks the live slot bitmap and recomputes the per-slot handle until a match is found. The slot index never leaves the secure world via the handle itself; this function is the only place that performs the inverse mapping.
| [in] | handle | Opaque handle previously returned by priv_ra8_key_import_seal. |
| [out] | out_slot | Receives the resolved slot index on success. |
| k_ra8_ok | Handle matched a live slot. |
| k_ra8_err_null_ptr | out_slot was NULL. |
| k_ra8_err_not_found | Handle does not match any live slot. |
Definition at line 329 of file key_import.c.
References internal_handle_for_slot(), k_ra8_err_not_found, k_ra8_key_vault_slots, k_ra8_ok, RA8_CHECK_NULL_PTR, s_slot_used, and s_tag.
|
nodiscard |
Verify, store, and assign an opaque handle for a sealed key blob.
Validate a sealed blob and import the key into a free slot.
Validates the blob length, checks the AES-CMAC, allocates the lowest free vault slot, copies the key into the vault, and returns an opaque handle that the NS world can later present to the SHA-256 challenge primitive without ever learning the slot index.
| [in] | blob | Sealed key blob. |
| [in] | blob_len | Length of blob; must equal k_ra8_key_import_blob_bytes. |
| [out] | out_handle | Receives the opaque handle on success. |
| k_ra8_ok | Key sealed and handle issued. |
| k_ra8_err_null_ptr | blob or out_handle was NULL. |
| k_ra8_err_invalid_size | blob_len did not match expected size. |
| k_ra8_err_invalid_arg | CMAC verification failed. |
| k_ra8_err_not_found | No KAK provisioned in the vault. |
| k_ra8_err_no_mem | All vault slots are in use. |
Definition at line 268 of file key_import.c.
References internal_handle_for_slot(), internal_verify_cmac(), k_ra8_err_invalid_size, k_ra8_err_no_mem, k_ra8_key_import_blob_bytes, k_ra8_key_vault_slots, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_key_vault_store(), s_slot_used, and s_tag.
|
static |
Per-boot 32-bit salt used for handle obfuscation.
Refreshed on every priv_ra8_key_import_reset call. The chosen value is intentionally non-zero so the handle for slot 0 never collides with k_ra8_key_import_handle_zero.
Definition at line 99 of file key_import.c.
Referenced by internal_handle_for_slot(), and priv_ra8_key_import_reset().
|
static |
One bit per vault slot: 1 if currently allocated.
Bit i of s_slot_used is set when slot i has been imported via priv_ra8_key_import_seal. priv_ra8_key_import_reset clears the entire mask.
Definition at line 86 of file key_import.c.
Referenced by priv_ra8_key_import_reset(), priv_ra8_key_import_resolve(), and priv_ra8_key_import_seal().
|
static |
Definition at line 46 of file key_import.c.