DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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 
11 #include "packager/base/logging.h"
12 #include "packager/base/stl_util.h"
13 
14 namespace edash_packager {
15 namespace media {
16 
17 AesCryptor::AesCryptor() : aes_key_(new AES_KEY) {}
18 AesCryptor::~AesCryptor() {}
19 
20 bool AesCryptor::Crypt(const std::vector<uint8_t>& text,
21  std::vector<uint8_t>* crypt_text) {
22  // Save text size to make it work for in-place conversion, since the
23  // next statement will update the text size.
24  const size_t text_size = text.size();
25  crypt_text->resize(text_size + NumPaddingBytes(text_size));
26  size_t crypt_text_size = crypt_text->size();
27  if (!CryptInternal(text.data(), text_size, crypt_text->data(),
28  &crypt_text_size)) {
29  return false;
30  }
31  DCHECK_LE(crypt_text_size, crypt_text->size());
32  crypt_text->resize(crypt_text_size);
33  return true;
34 }
35 
36 bool AesCryptor::Crypt(const std::string& text, std::string* crypt_text) {
37  // Save text size to make it work for in-place conversion, since the
38  // next statement will update the text size.
39  const size_t text_size = text.size();
40  crypt_text->resize(text_size + NumPaddingBytes(text_size));
41  size_t crypt_text_size = crypt_text->size();
42  if (!CryptInternal(reinterpret_cast<const uint8_t*>(text.data()), text_size,
43  reinterpret_cast<uint8_t*>(string_as_array(crypt_text)),
44  &crypt_text_size))
45  return false;
46  DCHECK_LE(crypt_text_size, crypt_text->size());
47  crypt_text->resize(crypt_text_size);
48  return true;
49 }
50 
51 size_t AesCryptor::NumPaddingBytes(size_t size) const {
52  // No padding by default.
53  return 0;
54 }
55 
56 } // namespace media
57 } // namespace edash_packager
58 
59