Shaka Packager SDK
aes_cryptor.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/base/aes_cryptor.h"
8 
9 #include <openssl/aes.h>
10 #include <openssl/crypto.h>
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 
14 #include <string>
15 #include <vector>
16 
17 #include "packager/base/logging.h"
18 
19 namespace {
20 
21 // According to ISO/IEC 23001-7:2016 CENC spec, IV should be either
22 // 64-bit (8-byte) or 128-bit (16-byte).
23 bool IsIvSizeValid(size_t iv_size) {
24  return iv_size == 8 || iv_size == 16;
25 }
26 
27 } // namespace
28 
29 namespace shaka {
30 namespace media {
31 
32 AesCryptor::AesCryptor(ConstantIvFlag constant_iv_flag)
33  : aes_key_(new AES_KEY),
34  constant_iv_flag_(constant_iv_flag),
35  num_crypt_bytes_(0) {
36  CRYPTO_library_init();
37 }
38 
39 AesCryptor::~AesCryptor() {}
40 
41 bool AesCryptor::Crypt(const std::vector<uint8_t>& text,
42  std::vector<uint8_t>* crypt_text) {
43  // Save text size to make it work for in-place conversion, since the
44  // next statement will update the text size.
45  const size_t text_size = text.size();
46  crypt_text->resize(text_size + NumPaddingBytes(text_size));
47  size_t crypt_text_size = crypt_text->size();
48  if (!Crypt(text.data(), text_size, crypt_text->data(), &crypt_text_size)) {
49  return false;
50  }
51  DCHECK_LE(crypt_text_size, crypt_text->size());
52  crypt_text->resize(crypt_text_size);
53  return true;
54 }
55 
56 bool AesCryptor::Crypt(const std::string& text, std::string* crypt_text) {
57  // Save text size to make it work for in-place conversion, since the
58  // next statement will update the text size.
59  const size_t text_size = text.size();
60  crypt_text->resize(text_size + NumPaddingBytes(text_size));
61  size_t crypt_text_size = crypt_text->size();
62  if (!Crypt(reinterpret_cast<const uint8_t*>(text.data()), text_size,
63  reinterpret_cast<uint8_t*>(&(*crypt_text)[0]), &crypt_text_size))
64  return false;
65  DCHECK_LE(crypt_text_size, crypt_text->size());
66  crypt_text->resize(crypt_text_size);
67  return true;
68 }
69 
70 bool AesCryptor::SetIv(const std::vector<uint8_t>& iv) {
71  if (!IsIvSizeValid(iv.size())) {
72  LOG(ERROR) << "Invalid IV size: " << iv.size();
73  return false;
74  }
75  iv_ = iv;
76  num_crypt_bytes_ = 0;
77  SetIvInternal();
78  return true;
79 }
80 
82  if (constant_iv_flag_ == kUseConstantIv)
83  return;
84 
85  uint64_t increment = 0;
86  // As recommended in ISO/IEC 23001-7:2016 CENC spec, for 64-bit (8-byte)
87  // IV_Sizes, initialization vectors for subsequent samples can be created by
88  // incrementing the initialization vector of the previous sample.
89  // For 128-bit (16-byte) IV_Sizes, initialization vectors for subsequent
90  // samples should be created by adding the block count of the previous sample
91  // to the initialization vector of the previous sample.
92  // There is no official recommendation of how IV for next sample should be
93  // generated for CBC mode. We use the same generation algorithm as CTR here.
94  if (iv_.size() == 8) {
95  increment = 1;
96  } else {
97  DCHECK_EQ(16u, iv_.size());
98  increment = (num_crypt_bytes_ + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE;
99  }
100 
101  for (int i = iv_.size() - 1; increment > 0 && i >= 0; --i) {
102  increment += iv_[i];
103  iv_[i] = increment & 0xFF;
104  increment >>= 8;
105  }
106  num_crypt_bytes_ = 0;
107  SetIvInternal();
108 }
109 
110 bool AesCryptor::GenerateRandomIv(FourCC protection_scheme,
111  std::vector<uint8_t>* iv) {
112  // ISO/IEC 23001-7:2016 10.1 and 10.3 For 'cenc' and 'cens'
113  // default_Per_Sample_IV_Size and Per_Sample_IV_Size SHOULD be 8-bytes.
114  // There is no official guideline on the iv size for 'cbc1' and 'cbcs',
115  // but 16-byte provides better security.
116  const size_t iv_size =
117  (protection_scheme == FOURCC_cenc || protection_scheme == FOURCC_cens)
118  ? 8
119  : 16;
120  iv->resize(iv_size);
121  if (RAND_bytes(iv->data(), iv_size) != 1) {
122  LOG(ERROR) << "RAND_bytes failed with error: "
123  << ERR_error_string(ERR_get_error(), NULL);
124  return false;
125  }
126  return true;
127 }
128 
129 size_t AesCryptor::NumPaddingBytes(size_t size) const {
130  // No padding by default.
131  return 0;
132 }
133 
134 } // namespace media
135 } // namespace shaka
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::AesCryptor::UpdateIv
void UpdateIv()
Definition: aes_cryptor.cc:81
shaka::media::AesCryptor::AesCryptor
AesCryptor(ConstantIvFlag constant_iv_flag)
Definition: aes_cryptor.cc:32
shaka::media::AesCryptor::iv
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:82
shaka::media::AesCryptor::SetIv
bool SetIv(const std::vector< uint8_t > &iv)
Definition: aes_cryptor.cc:70
shaka::media::AesCryptor::GenerateRandomIv
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:110