Skip to content

Commit

Permalink
Merge pull request #4287 from randombit/jack/vaes
Browse files Browse the repository at this point in the history
Add support for AVX2-VAES
  • Loading branch information
randombit authored Aug 5, 2024
2 parents 954a758 + e42d659 commit 13edb92
Show file tree
Hide file tree
Showing 6 changed files with 747 additions and 6 deletions.
66 changes: 66 additions & 0 deletions src/lib/block/aes/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,12 @@ void aes_key_schedule(const uint8_t key[],
}

size_t aes_parallelism() {
#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return 8; // pipelined
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return 4; // pipelined
Expand All @@ -757,6 +763,12 @@ size_t aes_parallelism() {
}

const char* aes_provider() {
#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return "vaes";
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return "cpu";
Expand Down Expand Up @@ -813,6 +825,12 @@ bool AES_256::has_keying_material() const {
void AES_128::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_encrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_encrypt_n(in, out, blocks);
Expand All @@ -831,6 +849,12 @@ void AES_128::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
void AES_128::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_decrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_decrypt_n(in, out, blocks);
Expand All @@ -853,6 +877,12 @@ void AES_128::key_schedule(std::span<const uint8_t> key) {
}
#endif

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
Expand All @@ -876,6 +906,12 @@ void AES_128::clear() {
void AES_192::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_encrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_encrypt_n(in, out, blocks);
Expand All @@ -894,6 +930,12 @@ void AES_192::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
void AES_192::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_decrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_decrypt_n(in, out, blocks);
Expand All @@ -916,6 +958,12 @@ void AES_192::key_schedule(std::span<const uint8_t> key) {
}
#endif

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
Expand All @@ -939,6 +987,12 @@ void AES_192::clear() {
void AES_256::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_encrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_encrypt_n(in, out, blocks);
Expand All @@ -957,6 +1011,12 @@ void AES_256::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
void AES_256::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
assert_key_material_set();

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return x86_vaes_decrypt_n(in, out, blocks);
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return hw_aes_decrypt_n(in, out, blocks);
Expand All @@ -979,6 +1039,12 @@ void AES_256::key_schedule(std::span<const uint8_t> key) {
}
#endif

#if defined(BOTAN_HAS_AES_VAES)
if(CPUID::has_avx2_vaes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
Expand Down
15 changes: 15 additions & 0 deletions src/lib/block/aes/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class AES_128 final : public Block_Cipher_Fixed_Params<16, 16> {
void hw_aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

#if defined(BOTAN_HAS_AES_VAES)
void x86_vaes_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
void x86_vaes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

secure_vector<uint32_t> m_EK, m_DK;
};

Expand Down Expand Up @@ -88,6 +93,11 @@ class AES_192 final : public Block_Cipher_Fixed_Params<16, 24> {
void hw_aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

#if defined(BOTAN_HAS_AES_VAES)
void x86_vaes_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
void x86_vaes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

void key_schedule(std::span<const uint8_t> key) override;

secure_vector<uint32_t> m_EK, m_DK;
Expand Down Expand Up @@ -128,6 +138,11 @@ class AES_256 final : public Block_Cipher_Fixed_Params<16, 32> {
void hw_aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

#if defined(BOTAN_HAS_AES_VAES)
void x86_vaes_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
void x86_vaes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

void key_schedule(std::span<const uint8_t> key) override;

secure_vector<uint32_t> m_EK, m_DK;
Expand Down
Loading

0 comments on commit 13edb92

Please sign in to comment.