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
This commit is contained in:
KongQun Yang 2019-10-22 13:53:10 -07:00
parent c80f053ba2
commit cb8b27e491
2 changed files with 39 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "packager/media/base/aes_cryptor.h" #include "packager/media/base/aes_cryptor.h"
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/crypto.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/rand.h> #include <openssl/rand.h>
@ -31,7 +32,9 @@ namespace media {
AesCryptor::AesCryptor(ConstantIvFlag constant_iv_flag) AesCryptor::AesCryptor(ConstantIvFlag constant_iv_flag)
: aes_key_(new AES_KEY), : aes_key_(new AES_KEY),
constant_iv_flag_(constant_iv_flag), constant_iv_flag_(constant_iv_flag),
num_crypt_bytes_(0) {} num_crypt_bytes_(0) {
CRYPTO_library_init();
}
AesCryptor::~AesCryptor() {} AesCryptor::~AesCryptor() {}

View File

@ -625,5 +625,40 @@ INSTANTIATE_TEST_CASE_P(CbcTestCases,
AesCbcCryptorVerificationTest, AesCbcCryptorVerificationTest,
::testing::ValuesIn(kCbcTestCases)); ::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<uint8_t>(i);
}
protected:
AesCbcEncryptor cbc_encryptor_;
AesCtrEncryptor ctr_encryptor_;
std::vector<uint8_t> key_;
std::vector<uint8_t> iv_;
std::vector<uint8_t> plaintext_;
};
TEST_F(AesPerformanceTest, AesCbc) {
ASSERT_TRUE(cbc_encryptor_.InitializeWithIv(key_, iv_));
std::vector<uint8_t> 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<uint8_t> encrypted;
for (int i = 0; i < 0x100; i++)
ASSERT_TRUE(ctr_encryptor_.Crypt(plaintext_, &encrypted));
}
} // namespace media } // namespace media
} // namespace shaka } // namespace shaka