Skip to content

Commit 6c97f93

Browse files
RobinKastbergcarlescufi
authored andcommitted
iar: nrf_security: stack aligned variables
Currently IAR doesn't support stack aligned variables, we do it "manually" instead. Signed-off-by: Robin Kastberg <[email protected]>
1 parent b13bdf0 commit 6c97f93

File tree

1 file changed

+21
-7
lines changed
  • subsys/nrf_security/src/drivers/cracen/cracenpsa/src

1 file changed

+21
-7
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/ctr_drbg.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@
3434
/** @brief Magic number to signal that PRNG context is initialized */
3535
#define CRACEN_PRNG_INITIALIZED (0xA5B4A5B4)
3636

37+
/* IAR Doesn't support aligned stack variables */
38+
#define ALIGN_UP(value, alignment) \
39+
(((value) + (alignment) - 1) & ~((alignment) - 1))
40+
41+
#ifdef __IAR_SYSTEMS_ICC__
42+
#define ALIGN_ON_STACK(type, var, size, alignment) \
43+
type var##base[(size) + ((alignment)/sizeof(type))]; \
44+
type * var = (type *)ALIGN_UP((intptr_t)var##base, alignment)
45+
#else
46+
#define ALIGN_ON_STACK(type, var, size, alignment) \
47+
type var[size] __attribute__((aligned(alignment)));
48+
#endif
49+
3750
/*
3851
* This driver uses a global context and discards the context passed from the user. We do that
3952
* because we are not aware of a requirement for multiple PRNG contexts from the users of the
@@ -109,18 +122,19 @@ static psa_status_t ctr_drbg_update(uint8_t *data)
109122
{
110123
psa_status_t status = SX_OK;
111124

112-
char temp[CRACEN_PRNG_ENTROPY_SIZE + CRACEN_PRNG_NONCE_SIZE] __aligned(
113-
CONFIG_DCACHE_LINE_SIZE);
125+
const size_t temp_sizeof = CRACEN_PRNG_ENTROPY_SIZE + CRACEN_PRNG_NONCE_SIZE;
126+
ALIGN_ON_STACK(char, temp, temp_sizeof, CONFIG_DCACHE_LINE_SIZE);
127+
114128
size_t temp_length = 0;
115-
_Static_assert(sizeof(temp) % SX_BLKCIPHER_AES_BLK_SZ == 0, "");
129+
_Static_assert(temp_sizeof % SX_BLKCIPHER_AES_BLK_SZ == 0, "");
116130

117131
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
118132

119133
psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
120134
psa_set_key_bits(&attr, PSA_BYTES_TO_BITS(sizeof(prng.key)));
121135
psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_ENCRYPT);
122136

123-
while (temp_length < sizeof(temp)) {
137+
while (temp_length < temp_sizeof) {
124138
cracen_be_add(prng.V, SX_BLKCIPHER_AES_BLK_SZ, 1);
125139

126140
status = sx_blkcipher_ecb_simple(prng.key, sizeof(prng.key), prng.V, sizeof(prng.V),
@@ -134,7 +148,7 @@ static psa_status_t ctr_drbg_update(uint8_t *data)
134148
}
135149

136150
if (data) {
137-
cracen_xorbytes(temp, data, sizeof(temp));
151+
cracen_xorbytes(temp, data, temp_sizeof);
138152
}
139153

140154
memcpy(prng.key, temp, sizeof(prng.key));
@@ -252,11 +266,11 @@ psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output,
252266

253267
while (len_left > 0) {
254268
size_t cur_len = MIN(len_left, SX_BLKCIPHER_AES_BLK_SZ);
255-
char temp[SX_BLKCIPHER_AES_BLK_SZ] __aligned(CONFIG_DCACHE_LINE_SIZE);
269+
ALIGN_ON_STACK(char, temp, SX_BLKCIPHER_AES_BLK_SZ, CONFIG_DCACHE_LINE_SIZE);
256270

257271
cracen_be_add(prng.V, SX_BLKCIPHER_AES_BLK_SZ, 1);
258272
status = sx_blkcipher_ecb_simple(prng.key, sizeof(prng.key), prng.V, sizeof(prng.V),
259-
temp, sizeof(temp));
273+
temp, SX_BLKCIPHER_AES_BLK_SZ);
260274

261275
if (status != PSA_SUCCESS) {
262276
nrf_security_mutex_unlock(cracen_prng_trng_mutex);

0 commit comments

Comments
 (0)