7 #include "packager/media/base/aes_cryptor.h"
9 #include <openssl/aes.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
13 #include "packager/base/logging.h"
14 #include "packager/base/stl_util.h"
16 namespace edash_packager {
19 AesCryptor::AesCryptor() : aes_key_(new AES_KEY) {}
20 AesCryptor::~AesCryptor() {}
22 bool AesCryptor::Crypt(
const std::vector<uint8_t>& text,
23 std::vector<uint8_t>* crypt_text) {
26 const size_t text_size = text.size();
27 crypt_text->resize(text_size + NumPaddingBytes(text_size));
28 size_t crypt_text_size = crypt_text->size();
29 if (!CryptInternal(text.data(), text_size, crypt_text->data(),
33 DCHECK_LE(crypt_text_size, crypt_text->size());
34 crypt_text->resize(crypt_text_size);
38 bool AesCryptor::Crypt(
const std::string& text, std::string* crypt_text) {
41 const size_t text_size = text.size();
42 crypt_text->resize(text_size + NumPaddingBytes(text_size));
43 size_t crypt_text_size = crypt_text->size();
44 if (!CryptInternal(reinterpret_cast<const uint8_t*>(text.data()), text_size,
45 reinterpret_cast<uint8_t*>(string_as_array(crypt_text)),
48 DCHECK_LE(crypt_text_size, crypt_text->size());
49 crypt_text->resize(crypt_text_size);
53 size_t AesCryptor::NumPaddingBytes(
size_t size)
const {
58 bool AesCryptor::GenerateRandomIv(FourCC protection_scheme,
59 std::vector<uint8_t>* iv) {
64 const size_t iv_size =
65 (protection_scheme == FOURCC_cenc || protection_scheme == FOURCC_cens)
69 if (RAND_bytes(iv->data(), iv_size) != 1) {
70 LOG(ERROR) <<
"RAND_bytes failed with error: "
71 << ERR_error_string(ERR_get_error(), NULL);