1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

AESNI: add implementation with intrinsics

As of this commit, to use the intrinsics for MBEDTLS_AESNI_C:

* With MSVC, this should be the default.
* With Clang, build with `clang -maes -mpclmul` or equivalent.
* With GCC, build with `gcc -mpclmul -msse2` or equivalent.

In particular, for now, with a GCC-like compiler, when building specifically
for a target that supports both the AES and GCM instructions, the old
implementation using assembly is selected.

This method for platform selection will likely be improved in the future.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine
2023-03-10 22:37:11 +01:00
committed by Tom Cosgrove
parent 2c8ad9400b
commit e7dc21fabb
2 changed files with 357 additions and 1 deletions

View File

@ -552,6 +552,14 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
#if defined(MBEDTLS_AESNI_HAVE_CODE)
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)) {
/* The intrinsics-based implementation needs 16-byte alignment
* for the round key array. */
unsigned delta = (uintptr_t) ctx->buf & 0x0000000f;
size_t rk_offset = 0;
if (delta != 0) {
rk_offset = 4 - delta / 4; // 16 bytes = 4 uint32_t
}
ctx->rk = RK = ctx->buf + rk_offset;
return mbedtls_aesni_setkey_enc((unsigned char *) ctx->rk, key, keybits);
}
#endif
@ -665,6 +673,17 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
goto exit;
}
#endif
#if defined(MBEDTLS_AESNI_HAVE_CODE)
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)) {
/* The intrinsics-based implementation needs 16-byte alignment
* for the round key array. */
unsigned delta = (uintptr_t) ctx->buf & 0x0000000f;
if (delta != 0) {
size_t rk_offset = 4 - delta / 4; // 16 bytes = 4 uint32_t
ctx->rk = RK = ctx->buf + rk_offset;
}
}
#endif
SK = cty.rk + cty.nr * 4;