Adjust stuct order for better packing / smaller accessor code size

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2023-06-24 18:06:45 +01:00
parent 1b8a3b16c4
commit 92cf6e52d3
2 changed files with 345 additions and 350 deletions

View File

@ -275,13 +275,14 @@ typedef struct mbedtls_cipher_info_t {
/** Name of the cipher. */ /** Name of the cipher. */
const char *MBEDTLS_PRIVATE(name); const char *MBEDTLS_PRIVATE(name);
/** Index to LUT for base cipher information and functions. */ /** The block size, in bytes. */
unsigned int MBEDTLS_PRIVATE(base_idx) : 5; unsigned int MBEDTLS_PRIVATE(block_size) : 5;
/** Full cipher identifier (as per mbedtls_cipher_type_t). /** IV or nonce size, in Bytes.
* For example, MBEDTLS_CIPHER_AES_256_CBC. * For ciphers that accept variable IV sizes,
* this is the recommended size.
*/ */
unsigned int MBEDTLS_PRIVATE(type) : 7; unsigned int MBEDTLS_PRIVATE(iv_size) : 3;
/** The cipher mode (as per mbedtls_cipher_mode_t). /** The cipher mode (as per mbedtls_cipher_mode_t).
* For example, MBEDTLS_MODE_CBC. * For example, MBEDTLS_MODE_CBC.
@ -294,11 +295,10 @@ typedef struct mbedtls_cipher_info_t {
*/ */
unsigned int MBEDTLS_PRIVATE(key_bitlen) : 4; unsigned int MBEDTLS_PRIVATE(key_bitlen) : 4;
/** IV or nonce size, in Bytes. /** Full cipher identifier (as per mbedtls_cipher_type_t).
* For ciphers that accept variable IV sizes, * For example, MBEDTLS_CIPHER_AES_256_CBC.
* this is the recommended size.
*/ */
unsigned int MBEDTLS_PRIVATE(iv_size) : 3; unsigned int MBEDTLS_PRIVATE(type) : 8; // only need 7 bits, but it retains byte alignment
/** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
* MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
@ -306,8 +306,8 @@ typedef struct mbedtls_cipher_info_t {
*/ */
unsigned int MBEDTLS_PRIVATE(flags) : 2; unsigned int MBEDTLS_PRIVATE(flags) : 2;
/** The block size, in bytes. */ /** Index to LUT for base cipher information and functions. */
unsigned int MBEDTLS_PRIVATE(block_size) : 2; unsigned int MBEDTLS_PRIVATE(base_idx) : 5;
} mbedtls_cipher_info_t; } mbedtls_cipher_info_t;
@ -315,7 +315,6 @@ typedef struct mbedtls_cipher_info_t {
* These are used to more compactly represent the fields above. */ * These are used to more compactly represent the fields above. */
#define MBEDTLS_KEY_BITLEN_SHIFT 6 #define MBEDTLS_KEY_BITLEN_SHIFT 6
#define MBEDTLS_IV_SIZE_SHIFT 2 #define MBEDTLS_IV_SIZE_SHIFT 2
#define MBEDTLS_CIPHER_BLOCK_SIZE_UNPACK(n) (n == 0 ? 1 : (n == 1 ? 8 : 16))
/** /**
* Generic cipher context. * Generic cipher context.
*/ */
@ -548,8 +547,7 @@ static inline size_t mbedtls_cipher_info_get_block_size(
return 0; return 0;
} }
int packed = info->MBEDTLS_PRIVATE(block_size); return (size_t) (info->MBEDTLS_PRIVATE(block_size));
return (size_t) (MBEDTLS_CIPHER_BLOCK_SIZE_UNPACK(packed));
} }
/** /**
@ -690,8 +688,7 @@ static inline unsigned int mbedtls_cipher_get_block_size(
return 0; return 0;
} }
int packed = ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(block_size); return (unsigned int) ctx->MBEDTLS_PRIVATE(cipher_info)->MBEDTLS_PRIVATE(block_size);
return (unsigned int) MBEDTLS_CIPHER_BLOCK_SIZE_UNPACK(packed);
} }
/** /**

File diff suppressed because it is too large Load Diff