ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_secure.c
Go to the documentation of this file.
1
14
15#include "ra8_secure.h"
16
17#include <stddef.h>
18#include <stdint.h>
19
20bool ra8_ct_equal(const void* a, const void* b, size_t len)
21{
22 if (a == nullptr) {
23 return false;
24 }
25 if (b == nullptr) {
26 return false;
27 }
28 const uint8_t* pa = (const uint8_t*)a;
29 const uint8_t* pb = (const uint8_t*)b;
30 uint8_t diff = 0U;
31 for (size_t i = 0U; i < len; ++i) {
32 diff = (uint8_t)(diff | (uint8_t)(pa[i] ^ pb[i]));
33 }
34 return (diff == 0U);
35}
36
37void ra8_secure_memzero(void* ptr, size_t len)
38{
39 if (ptr == nullptr) {
40 return;
41 }
42 if (len == 0U) {
43 return;
44 }
45 /* Write through a volatile pointer so the store cannot be dead-store
46 * eliminated: unlike ``memset``, a volatile access is an observable side
47 * effect the optimiser must preserve even when ``ptr`` is about to leave
48 * scope. */
49 volatile uint8_t* dst = (volatile uint8_t*)ptr;
50 for (size_t i = 0U; i < len; ++i) {
51 dst[i] = 0U;
52 }
53}
bool ra8_ct_equal(const void *a, const void *b, size_t len)
Constant-time equality of two byte buffers.
Definition ra8_secure.c:20
void ra8_secure_memzero(void *ptr, size_t len)
Securely zero a buffer such that the write cannot be optimised away.
Definition ra8_secure.c:37
Secure-comparison primitives for the crypto / secure-boot paths.