Skip to content

Commit c087be8

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Adjust the size of hash operation in Cracen
This updates the hash operation size depending on the enabled algorithms. This is an optimization which will reduce stack usage for use cases which only require a subset of the enabled algorithms. Notice that the nrf-psa-crypto-user-config.h header is used directly here instead of the psa/crypto.h. This is done because including the psa/crypto.h creates circular dependencies in the headers. The sxsymcrypt is in a lower layer than then PSA crypto driver layer so it makes sense to avoid going from a lower layer in the driver to a higher one. The PSA configuration is still being used since this is the only way to do crypto configuration. Since the nrf-psa-crypto-user-config.h is a static configuration file without any dependencies it is reasonable to use it inside the lower layers of the driver. Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent d5e3320 commit c087be8

File tree

1 file changed

+29
-1
lines changed
  • subsys/nrf_security/src/drivers/cracen/sxsymcrypt/include/sxsymcrypt

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,37 @@
1010
#include <stddef.h>
1111
#include <stdint.h>
1212
#include "internal.h"
13+
#include "nrf-psa-crypto-user-config.h"
1314

14-
#ifndef SX_HASH_PRIV_SZ
15+
/* These are not magic numbers, the number here is the size in bytes of the
16+
* extramem field of sxhash. The extra memory holds the data for saving/resuming
17+
* the state and should have the size of statesz + maxpadsz.
18+
* The size here is the MAX of this sum from the enabled algorithms.
19+
*
20+
* !!! ORDER MATTERS !!!
21+
*/
22+
#if defined(PSA_NEED_CRACEN_SHA3_224)
1523
#define SX_HASH_PRIV_SZ 344
24+
#elif defined(PSA_NEED_CRACEN_SHA3_256)
25+
/* SHAKE256 has the same size but doesn't have a PSA_NEED yet */
26+
#define SX_HASH_PRIV_SZ 336
27+
#elif defined(PSA_NEED_CRACEN_SHA3_384)
28+
#define SX_HASH_PRIV_SZ 304
29+
#elif defined(PSA_NEED_CRACEN_SHA3_512)
30+
#define SX_HASH_PRIV_SZ 272
31+
#elif defined(PSA_NEED_CRACEN_SHA_512) || defined(PSA_NEED_CRACEN_SHA_384)
32+
#define SX_HASH_PRIV_SZ 208
33+
#elif defined(PSA_NEED_CRACEN_SHA_256) || defined(PSA_NEED_CRACEN_SHA_224)
34+
/* SM3 has the same size but doesn't have a PSA_NEED yet */
35+
#define SX_HASH_PRIV_SZ 104
36+
#elif defined(PSA_NEED_CRACEN_SHA_1)
37+
#define SX_HASH_PRIV_SZ 92
38+
#else
39+
/* A default value is needed to avoid building failures when no hash is
40+
* enabled. A small number is used because it will enforce a runtime failure
41+
* if the sx_hash APIs are called while no algorithm is enabled.
42+
*/
43+
#define SX_HASH_PRIV_SZ 1
1644
#endif
1745

1846
struct sx_digesttags {

0 commit comments

Comments
 (0)