Skip to content

Commit

Permalink
AESNI: add implementation with intrinsics
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gilles-peskine-arm committed Mar 15, 2023
1 parent 7e67bd5 commit d671917
Show file tree
Hide file tree
Showing 2 changed files with 355 additions and 1 deletion.
17 changes: 17 additions & 0 deletions library/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ 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;
if (delta != 0) {
ctx->rk_offset = 4 - delta / 4; // 16 bytes = 4 uint32_t
}
RK = ctx->buf + ctx->rk_offset;
return mbedtls_aesni_setkey_enc((unsigned char *) RK, key, keybits);
}
#endif
Expand Down Expand Up @@ -643,6 +650,16 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
if (aes_padlock_ace) {
ctx->rk_offset = MBEDTLS_PADLOCK_ALIGN16(ctx->buf) - ctx->buf;
}
#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) {
ctx->rk_offset = 4 - delta / 4; // 16 bytes = 4 uint32_t
}
}
#endif
RK = ctx->buf + ctx->rk_offset;

Expand Down
Loading

0 comments on commit d671917

Please sign in to comment.