34
34
/** @brief Magic number to signal that PRNG context is initialized */
35
35
#define CRACEN_PRNG_INITIALIZED (0xA5B4A5B4)
36
36
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
+
37
50
/*
38
51
* This driver uses a global context and discards the context passed from the user. We do that
39
52
* 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)
109
122
{
110
123
psa_status_t status = SX_OK ;
111
124
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
+
114
128
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 , "" );
116
130
117
131
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT ;
118
132
119
133
psa_set_key_type (& attr , PSA_KEY_TYPE_AES );
120
134
psa_set_key_bits (& attr , PSA_BYTES_TO_BITS (sizeof (prng .key )));
121
135
psa_set_key_usage_flags (& attr , PSA_KEY_USAGE_ENCRYPT );
122
136
123
- while (temp_length < sizeof ( temp ) ) {
137
+ while (temp_length < temp_sizeof ) {
124
138
cracen_be_add (prng .V , SX_BLKCIPHER_AES_BLK_SZ , 1 );
125
139
126
140
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)
134
148
}
135
149
136
150
if (data ) {
137
- cracen_xorbytes (temp , data , sizeof ( temp ) );
151
+ cracen_xorbytes (temp , data , temp_sizeof );
138
152
}
139
153
140
154
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,
252
266
253
267
while (len_left > 0 ) {
254
268
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 );
256
270
257
271
cracen_be_add (prng .V , SX_BLKCIPHER_AES_BLK_SZ , 1 );
258
272
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 );
260
274
261
275
if (status != PSA_SUCCESS ) {
262
276
nrf_security_mutex_unlock (cracen_prng_trng_mutex );
0 commit comments