Skip to content

Commit 58a92a8

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Set max enabled hash block size in Cracen
Define the maximum block size of the hash functions based on the enabled algorithms to reduce stack size. Also rename it to the more descriptive SX_HASH_MAX_ENABLED_BLOCK_SIZE. Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent dead287 commit 58a92a8

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa_primitives.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
#include <sxsymcrypt/cmac.h>
1818
#include <sxsymcrypt/internal.h>
1919
#include <sxsymcrypt/trng.h>
20-
21-
/* Max blocksize of the supported algorithms 144 bytes */
22-
#define MAX_HASH_BLOCK_SIZE 144
20+
#include <sxsymcrypt/hashdefs.h>
2321

2422
#define SX_BLKCIPHER_IV_SZ (16U)
2523
#define SX_BLKCIPHER_AES_BLK_SZ (16U)
@@ -141,7 +139,7 @@ struct cracen_hash_operation_s {
141139
size_t bytes_left_for_next_block;
142140

143141
/* Buffer for input data to fill up the next block */
144-
uint8_t input_buffer[MAX_HASH_BLOCK_SIZE];
142+
uint8_t input_buffer[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
145143

146144
/* Flag to know if the Hashing has already started */
147145
bool is_first_block;
@@ -192,13 +190,13 @@ struct cracen_mac_operation_s {
192190
size_t bytes_left_for_next_block;
193191

194192
/* Buffer for input data to fill up the next block */
195-
uint8_t input_buffer[MAX_HASH_BLOCK_SIZE];
193+
uint8_t input_buffer[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
196194

197195
union {
198196
struct {
199197
struct sitask task;
200198

201-
uint8_t workmem[MAX_HASH_BLOCK_SIZE + PSA_HASH_MAX_SIZE];
199+
uint8_t workmem[SX_HASH_MAX_ENABLED_BLOCK_SIZE + PSA_HASH_MAX_SIZE];
202200
} hmac;
203201

204202
struct {
@@ -216,7 +214,7 @@ struct cracen_key_derivation_operation {
216214
psa_algorithm_t alg;
217215
enum cracen_kd_state state;
218216
uint64_t capacity;
219-
uint8_t output_block[MAX_HASH_BLOCK_SIZE];
217+
uint8_t output_block[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
220218
uint8_t output_block_available_bytes;
221219
union{
222220
cracen_mac_operation_t mac_op;
@@ -225,16 +223,16 @@ struct cracen_key_derivation_operation {
225223
union {
226224
struct {
227225
uint8_t blk_counter;
228-
uint8_t prk[MAX_HASH_BLOCK_SIZE];
229-
uint8_t t[MAX_HASH_BLOCK_SIZE];
226+
uint8_t prk[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
227+
uint8_t t[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
230228
char info[CRACEN_HKDF_MAX_INFO_SIZE];
231229
size_t info_length;
232230
bool info_set;
233231
} hkdf;
234232

235233
struct {
236234
uint64_t input_cost;
237-
char password[MAX_HASH_BLOCK_SIZE];
235+
char password[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
238236
size_t password_length;
239237
char salt[CRACEN_PBKDF_MAX_SALT_SIZE];
240238
size_t salt_length;
@@ -274,7 +272,7 @@ struct cracen_key_derivation_operation {
274272
uint8_t label[CRACEN_TLS12_PRF_MAX_LABEL_SIZE];
275273
size_t label_length;
276274
size_t counter;
277-
uint8_t a[MAX_HASH_BLOCK_SIZE];
275+
uint8_t a[SX_HASH_MAX_ENABLED_BLOCK_SIZE];
278276
} tls12;
279277
};
280278
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <cracen/mem_helpers.h>
2020
#include "cracen_psa_primitives.h"
2121

22+
_Static_assert(SX_HASH_MAX_ENABLED_BLOCK_SIZE != 1,
23+
"To compile this file you need at least one hash algorithm enabled in the driver "
24+
"using the PSA_WANT_* configs.");
25+
2226
psa_status_t cracen_hash_compute(psa_algorithm_t alg, const uint8_t *input, size_t input_length,
2327
uint8_t *hash, size_t hash_size, size_t *hash_length)
2428
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212
#include <sxsymcrypt/cmac.h>
1313
#include <sxsymcrypt/hash.h>
14+
#include <sxsymcrypt/hashdefs.h>
1415
#include <sxsymcrypt/keyref.h>
1516
#include <zephyr/sys/__assert.h>
1617
#include "common.h"
@@ -35,7 +36,7 @@ static psa_status_t cracen_hmac_setup(cracen_mac_operation_t *operation,
3536

3637
/* HMAC task creation and configuration. */
3738
si_task_init(&operation->hmac.task, operation->hmac.workmem,
38-
MAX_HASH_BLOCK_SIZE + PSA_HASH_MAX_SIZE);
39+
SX_HASH_MAX_ENABLED_BLOCK_SIZE + PSA_HASH_MAX_SIZE);
3940
si_mac_create_hmac(&operation->hmac.task, sx_hash_algo, key_buffer, key_buffer_size);
4041

4142
/* Wait until the key is processed */

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/include/sxsymcrypt/hashdefs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@
3434
#define SX_HASH_BLOCKSZ_SHA1 64
3535
#define SX_HASH_BLOCKSZ_SM3 64
3636

37+
/*
38+
* !!! ORDER MATTERS !!!
39+
*/
40+
#if defined(PSA_NEED_CRACEN_SHA3_224)
41+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA3_224
42+
#elif defined(PSA_NEED_CRACEN_SHA3_256)
43+
/* SHAKE256 has the same size but doesn't have a PSA_NEED yet */
44+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA3_256
45+
#elif defined(PSA_NEED_CRACEN_SHA_512) || defined(PSA_NEED_CRACEN_SHA_384)
46+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA2_512
47+
#elif defined(PSA_NEED_CRACEN_SHA3_384)
48+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA3_384
49+
#elif defined(PSA_NEED_CRACEN_SHA3_512)
50+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA3_512
51+
#elif defined(PSA_NEED_CRACEN_SHA_256) || defined(PSA_NEED_CRACEN_SHA_224)
52+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA2_256
53+
#elif defined(PSA_NEED_CRACEN_SHA_1)
54+
/* SM3 has the same size but doesn't have a PSA_NEED yet */
55+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE SX_HASH_BLOCKSZ_SHA1
56+
#else
57+
/* A static assert is used in the hash.c against size 1. */
58+
#define SX_HASH_MAX_ENABLED_BLOCK_SIZE 1
59+
#endif
60+
3761
/* These are not magic numbers, the number here is the size in bytes of the
3862
* extramem field of sxhash. The extra memory holds the data for saving/resuming
3963
* the state and should have the size of statesz + maxpadsz.

0 commit comments

Comments
 (0)