DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
aes_decryptor.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_decryptor.h"
8 
9 #include <openssl/aes.h>
10 
11 #include "packager/base/logging.h"
12 
13 namespace {
14 
15 // AES defines three key sizes: 128, 192 and 256 bits.
16 bool IsKeySizeValidForAes(size_t key_size) {
17  return key_size == 16 || key_size == 24 || key_size == 32;
18 }
19 
20 } // namespace
21 
22 namespace edash_packager {
23 namespace media {
24 
25 AesCbcDecryptor::AesCbcDecryptor(CbcPaddingScheme padding_scheme,
26  bool chain_across_calls)
27  : padding_scheme_(padding_scheme),
28  chain_across_calls_(chain_across_calls) {
29  if (padding_scheme_ != kNoPadding) {
30  CHECK(!chain_across_calls) << "cipher block chain across calls only makes "
31  "sense if the padding_scheme is kNoPadding.";
32  }
33 }
34 
35 AesCbcDecryptor::~AesCbcDecryptor() {}
36 
37 bool AesCbcDecryptor::InitializeWithIv(const std::vector<uint8_t>& key,
38  const std::vector<uint8_t>& iv) {
39  if (!IsKeySizeValidForAes(key.size())) {
40  LOG(ERROR) << "Invalid AES key size: " << key.size();
41  return false;
42  }
43 
44  CHECK_EQ(AES_set_decrypt_key(key.data(), key.size() * 8, mutable_aes_key()),
45  0);
46  return SetIv(iv);
47 }
48 
49 bool AesCbcDecryptor::SetIv(const std::vector<uint8_t>& iv) {
50  if (iv.size() != AES_BLOCK_SIZE) {
51  LOG(ERROR) << "Invalid IV size: " << iv.size();
52  return false;
53  }
54 
55  set_iv(iv);
56  return true;
57 }
58 
59 bool AesCbcDecryptor::CryptInternal(const uint8_t* ciphertext,
60  size_t ciphertext_size,
61  uint8_t* plaintext,
62  size_t* plaintext_size) {
63  DCHECK(plaintext_size);
64  DCHECK(aes_key());
65  // Plaintext size is the same as ciphertext size except for pkcs5 padding.
66  // Will update later if using pkcs5 padding. For pkcs5 padding, we still
67  // need at least |ciphertext_size| bytes for intermediate operation.
68  if (*plaintext_size < ciphertext_size) {
69  LOG(ERROR) << "Expecting output size of at least " << ciphertext_size
70  << " bytes.";
71  return false;
72  }
73  *plaintext_size = ciphertext_size;
74 
75  if (ciphertext_size == 0) {
76  if (padding_scheme_ == kPkcs5Padding) {
77  LOG(ERROR) << "Expected ciphertext to be at least " << AES_BLOCK_SIZE
78  << " bytes with Pkcs5 padding.";
79  return false;
80  }
81  return true;
82  }
83  DCHECK(plaintext);
84 
85  std::vector<uint8_t> local_iv(iv());
86  const size_t residual_block_size = ciphertext_size % AES_BLOCK_SIZE;
87  if (residual_block_size == 0) {
88  AES_cbc_encrypt(ciphertext, plaintext, ciphertext_size, aes_key(),
89  local_iv.data(), AES_DECRYPT);
90  if (chain_across_calls_)
91  set_iv(local_iv);
92  if (padding_scheme_ != kPkcs5Padding)
93  return true;
94 
95  // Strip off PKCS5 padding bytes.
96  const uint8_t num_padding_bytes = plaintext[ciphertext_size - 1];
97  if (num_padding_bytes > AES_BLOCK_SIZE) {
98  LOG(ERROR) << "Padding length is too large : "
99  << static_cast<int>(num_padding_bytes);
100  return false;
101  }
102  *plaintext_size -= num_padding_bytes;
103  return true;
104  } else if (padding_scheme_ != kCtsPadding) {
105  LOG(ERROR) << "Expecting cipher text size to be multiple of "
106  << AES_BLOCK_SIZE << ", got " << ciphertext_size;
107  return false;
108  }
109 
110  DCHECK(!chain_across_calls_);
111  DCHECK_EQ(padding_scheme_, kCtsPadding);
112  if (ciphertext_size < AES_BLOCK_SIZE) {
113  // Don't have a full block, leave unencrypted.
114  memcpy(plaintext, ciphertext, ciphertext_size);
115  return true;
116  }
117 
118  // AES-CBC decrypt everything up to the next-to-last full block.
119  const size_t cbc_size = ciphertext_size - residual_block_size;
120  if (cbc_size > AES_BLOCK_SIZE) {
121  AES_cbc_encrypt(ciphertext, plaintext, cbc_size - AES_BLOCK_SIZE, aes_key(),
122  local_iv.data(), AES_DECRYPT);
123  }
124 
125  const uint8_t* next_to_last_ciphertext_block =
126  ciphertext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
127  uint8_t* next_to_last_plaintext_block =
128  plaintext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
129 
130  // Determine what the last IV should be so that we can "skip ahead" in the
131  // CBC decryption.
132  std::vector<uint8_t> last_iv(
133  ciphertext + ciphertext_size - residual_block_size,
134  ciphertext + ciphertext_size);
135  last_iv.resize(AES_BLOCK_SIZE, 0);
136 
137  // Decrypt the next-to-last block using the IV determined above. This decrypts
138  // the residual block bits.
139  AES_cbc_encrypt(next_to_last_ciphertext_block, next_to_last_plaintext_block,
140  AES_BLOCK_SIZE, aes_key(), last_iv.data(), AES_DECRYPT);
141 
142  // Swap back the residual block bits and the next-to-last block.
143  if (plaintext == ciphertext) {
144  std::swap_ranges(next_to_last_plaintext_block,
145  next_to_last_plaintext_block + residual_block_size,
146  next_to_last_plaintext_block + AES_BLOCK_SIZE);
147  } else {
148  memcpy(next_to_last_plaintext_block + AES_BLOCK_SIZE,
149  next_to_last_plaintext_block, residual_block_size);
150  memcpy(next_to_last_plaintext_block,
151  next_to_last_ciphertext_block + AES_BLOCK_SIZE, residual_block_size);
152  }
153 
154  // Decrypt the next-to-last full block.
155  AES_cbc_encrypt(next_to_last_plaintext_block, next_to_last_plaintext_block,
156  AES_BLOCK_SIZE, aes_key(), local_iv.data(), AES_DECRYPT);
157  return true;
158 }
159 
160 } // namespace media
161 } // namespace edash_packager
bool SetIv(const std::vector< uint8_t > &iv) override
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
AesCbcDecryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls)
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:60