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