From cb8b27e491ea08028e6db98ae9b5ad1e488eb1ba Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Tue, 22 Oct 2019 13:53:10 -0700 Subject: [PATCH] Properly initialize crypto engine to enable HWAES Call CRYPTO_library_init to properly initialize crypto engine, which enables AES-NI (Hardware AES) if it is supported by CPU. Also added a performance benchmark test. Closes #198. Change-Id: I962a2da588d2f4f6cbe00c83ecc9a832db0e6042 --- packager/media/base/aes_cryptor.cc | 5 ++- packager/media/base/aes_cryptor_unittest.cc | 35 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packager/media/base/aes_cryptor.cc b/packager/media/base/aes_cryptor.cc index acbb8911af..dd3cee7e39 100644 --- a/packager/media/base/aes_cryptor.cc +++ b/packager/media/base/aes_cryptor.cc @@ -7,6 +7,7 @@ #include "packager/media/base/aes_cryptor.h" #include +#include #include #include @@ -31,7 +32,9 @@ namespace media { AesCryptor::AesCryptor(ConstantIvFlag constant_iv_flag) : aes_key_(new AES_KEY), constant_iv_flag_(constant_iv_flag), - num_crypt_bytes_(0) {} + num_crypt_bytes_(0) { + CRYPTO_library_init(); +} AesCryptor::~AesCryptor() {} diff --git a/packager/media/base/aes_cryptor_unittest.cc b/packager/media/base/aes_cryptor_unittest.cc index 8fe02e88dc..fd40c1ff74 100644 --- a/packager/media/base/aes_cryptor_unittest.cc +++ b/packager/media/base/aes_cryptor_unittest.cc @@ -625,5 +625,40 @@ INSTANTIATE_TEST_CASE_P(CbcTestCases, AesCbcCryptorVerificationTest, ::testing::ValuesIn(kCbcTestCases)); +class AesPerformanceTest : public ::testing::Test { + public: + AesPerformanceTest() + : cbc_encryptor_(kNoPadding, AesCryptor::kUseConstantIv), + key_(kAesKey, kAesKey + arraysize(kAesKey)), + iv_(kAesIv, kAesIv + arraysize(kAesIv)) {} + + void SetUp() override { + plaintext_.resize(0x10000); + for (size_t i = 0; i < plaintext_.size(); i++) + plaintext_[i] = static_cast(i); + } + + protected: + AesCbcEncryptor cbc_encryptor_; + AesCtrEncryptor ctr_encryptor_; + std::vector key_; + std::vector iv_; + std::vector plaintext_; +}; + +TEST_F(AesPerformanceTest, AesCbc) { + ASSERT_TRUE(cbc_encryptor_.InitializeWithIv(key_, iv_)); + std::vector encrypted; + for (int i = 0; i < 0x100; i++) + ASSERT_TRUE(cbc_encryptor_.Crypt(plaintext_, &encrypted)); +} + +TEST_F(AesPerformanceTest, AesCtr) { + ASSERT_TRUE(ctr_encryptor_.InitializeWithIv(key_, iv_)); + std::vector encrypted; + for (int i = 0; i < 0x100; i++) + ASSERT_TRUE(ctr_encryptor_.Crypt(plaintext_, &encrypted)); +} + } // namespace media } // namespace shaka