PSA operation structures: move less-used fields to the end

Move fields around to have fewer accesses outside the 128-element Thumb
direct access window.

In psa_hkdf_key_derivation_t, move the large fields (output_block, prk,
hmac) after the state bit-fields. Experimentally, it's slightly better
to put hmac last.

In aead_operation_t, tag_length was outside the window. The details depend
on the sizes of contexts included in ctx. Make the large ctx be the last
field.

In mbedtls_psa_hmac_operation_t, the opad field is outside the window when
SHA-512 is enabled. Moving opad before hash_ctx only saves 4 bytes and made
the structure clumsy, so I left it alone.

Results (arm-none-eabi-gcc 7.3.1, build_arm_none_eabi_gcc_m0plus build):
library/psa_crypto.o: 16246 -> 16166 (diff: 80)
library/psa_crypto_aead.o: 952 -> 928 (diff: 24)

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-11-08 22:12:47 +01:00
parent 2290afc2d4
commit f5d7eef11f
2 changed files with 6 additions and 6 deletions

View file

@ -175,9 +175,6 @@ typedef struct
{ {
uint8_t *info; uint8_t *info;
size_t info_length; size_t info_length;
psa_mac_operation_t hmac;
uint8_t prk[PSA_HASH_MAX_SIZE];
uint8_t output_block[PSA_HASH_MAX_SIZE];
#if PSA_HASH_MAX_SIZE > 0xff #if PSA_HASH_MAX_SIZE > 0xff
#error "PSA_HASH_MAX_SIZE does not fit in uint8_t" #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
#endif #endif
@ -185,6 +182,9 @@ typedef struct
uint8_t block_number; uint8_t block_number;
unsigned int state : 2; unsigned int state : 2;
unsigned int info_set : 1; unsigned int info_set : 1;
uint8_t output_block[PSA_HASH_MAX_SIZE];
uint8_t prk[PSA_HASH_MAX_SIZE];
psa_mac_operation_t hmac;
} psa_hkdf_key_derivation_t; } psa_hkdf_key_derivation_t;
#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */ #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */

View file

@ -32,6 +32,8 @@
typedef struct typedef struct
{ {
psa_algorithm_t core_alg;
uint8_t tag_length;
union union
{ {
unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
@ -45,11 +47,9 @@ typedef struct
mbedtls_chachapoly_context chachapoly; mbedtls_chachapoly_context chachapoly;
#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
} ctx; } ctx;
psa_algorithm_t core_alg;
uint8_t tag_length;
} aead_operation_t; } aead_operation_t;
#define AEAD_OPERATION_INIT {{0}, 0, 0} #define AEAD_OPERATION_INIT {0, 0, {0}}
static void psa_aead_abort_internal( aead_operation_t *operation ) static void psa_aead_abort_internal( aead_operation_t *operation )
{ {