7 #include "packager/media/base/aes_cryptor.h" 9 #include <openssl/aes.h> 10 #include <openssl/err.h> 11 #include <openssl/rand.h> 16 #include "packager/base/logging.h" 22 bool IsIvSizeValid(
size_t iv_size) {
23 return iv_size == 8 || iv_size == 16;
32 : aes_key_(new AES_KEY),
33 constant_iv_flag_(constant_iv_flag),
34 num_crypt_bytes_(0) {}
36 AesCryptor::~AesCryptor() {}
38 bool AesCryptor::Crypt(
const std::vector<uint8_t>& text,
39 std::vector<uint8_t>* crypt_text) {
42 const size_t text_size = text.size();
43 crypt_text->resize(text_size + NumPaddingBytes(text_size));
44 size_t crypt_text_size = crypt_text->size();
45 if (!Crypt(text.data(), text_size, crypt_text->data(), &crypt_text_size)) {
48 DCHECK_LE(crypt_text_size, crypt_text->size());
49 crypt_text->resize(crypt_text_size);
53 bool AesCryptor::Crypt(
const std::string& text, std::string* crypt_text) {
56 const size_t text_size = text.size();
57 crypt_text->resize(text_size + NumPaddingBytes(text_size));
58 size_t crypt_text_size = crypt_text->size();
59 if (!Crypt(reinterpret_cast<const uint8_t*>(text.data()), text_size,
60 reinterpret_cast<uint8_t*>(&(*crypt_text)[0]), &crypt_text_size))
62 DCHECK_LE(crypt_text_size, crypt_text->size());
63 crypt_text->resize(crypt_text_size);
68 if (!IsIvSizeValid(iv.size())) {
69 LOG(ERROR) <<
"Invalid IV size: " << iv.size();
79 if (constant_iv_flag_ == kUseConstantIv)
82 uint64_t increment = 0;
91 if (iv_.size() == 8) {
94 DCHECK_EQ(16u, iv_.size());
95 increment = (num_crypt_bytes_ + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE;
98 for (
int i = iv_.size() - 1; increment > 0 && i >= 0; --i) {
100 iv_[i] = increment & 0xFF;
103 num_crypt_bytes_ = 0;
108 std::vector<uint8_t>*
iv) {
113 const size_t iv_size =
114 (protection_scheme == FOURCC_cenc || protection_scheme == FOURCC_cens)
118 if (RAND_bytes(iv->data(), iv_size) != 1) {
119 LOG(ERROR) <<
"RAND_bytes failed with error: " 120 << ERR_error_string(ERR_get_error(), NULL);
126 size_t AesCryptor::NumPaddingBytes(
size_t size)
const {
All the methods that are virtual are virtual for mocking.