Skip to content

Commit dd7b0e7

Browse files
committed
[zep fromtree] psa: crypto_extra: improve buffer size computation for static key slots
Take also MAC's key types into account when computing the size of the buffer to store key material in static key slot configuration. Signed-off-by: Valerio Setti <[email protected]> (cherry picked from commit 45574797e7c66dcd99cfeb0e0be5feb291271d1a)
1 parent a93104d commit dd7b0e7

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

include/psa/crypto_extra.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,39 @@ extern "C" {
3333
#endif
3434

3535
/* If the size of static key slots is not explicitly defined by the user, then
36-
* set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE and
37-
* PSA_CIPHER_MAX_KEY_LENGTH.
36+
* set it to the maximum between PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE,
37+
* PSA_CIPHER_MAX_KEY_LENGTH and PSA_MAC_MAX_SIZE.
3838
* See mbedtls_config.h for the definition. */
3939
#if !defined(MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)
40-
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE \
41-
((PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > PSA_CIPHER_MAX_KEY_LENGTH) ? \
42-
PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE : PSA_CIPHER_MAX_KEY_LENGTH)
40+
41+
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE 1
42+
43+
#if PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
44+
#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
45+
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE
46+
#endif
47+
48+
#if PSA_CIPHER_MAX_KEY_LENGTH > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
49+
#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
50+
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_CIPHER_MAX_KEY_LENGTH
51+
#endif
52+
53+
/* For HMAC, it's typical but not mandatory to use a key size that is equal to
54+
* the hash size. */
55+
#if PSA_WANT_ALG_HMAC
56+
#if PSA_HASH_MAX_SIZE > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
57+
#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
58+
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_HASH_MAX_SIZE
59+
#endif
60+
#endif /* PSA_WANT_ALG_HMAC */
61+
62+
#if PSA_WANT_ALG_CMAC
63+
#if PSA_CIPHER_MAX_KEY_LENGTH > MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
64+
#undef MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
65+
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE PSA_CIPHER_MAX_KEY_LENGTH
66+
#endif
67+
#endif /* PSA_WANT_ALG_CMAC */
68+
4369
#endif /* !MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE*/
4470

4571
/** \addtogroup attributes

0 commit comments

Comments
 (0)