DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
aes_cryptor.h
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 #ifndef PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
8 #define PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
9 
10 #include <string>
11 #include <vector>
12 
13 #include "packager/base/macros.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 #include "packager/media/base/fourccs.h"
16 
17 struct aes_key_st;
18 typedef struct aes_key_st AES_KEY;
19 
20 namespace edash_packager {
21 namespace media {
22 
23 // AES cryptor interface. Inherited by various AES encryptor and decryptor
24 // implementations.
25 class AesCryptor {
26  public:
27  enum ConstantIvFlag {
28  kUseConstantIv,
29  kDontUseConstantIv,
30  };
31 
38  explicit AesCryptor(ConstantIvFlag constant_iv_flag);
39  virtual ~AesCryptor();
40 
43  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
44  const std::vector<uint8_t>& iv) = 0;
45 
51  bool Crypt(const std::vector<uint8_t>& text,
52  std::vector<uint8_t>* crypt_text);
53  bool Crypt(const std::string& text, std::string* crypt_text);
55  bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
56  size_t crypt_text_size = text_size;
57  return Crypt(text, text_size, crypt_text, &crypt_text_size);
58  }
59  bool Crypt(const uint8_t* text,
60  size_t text_size,
61  uint8_t* crypt_text,
62  size_t* crypt_text_size) {
63  if (constant_iv_flag_ == kUseConstantIv)
64  SetIvInternal();
65  else
66  num_crypt_bytes_ += text_size;
67  return CryptInternal(text, text_size, crypt_text, crypt_text_size);
68  }
70 
73  bool SetIv(const std::vector<uint8_t>& iv);
74 
78  void UpdateIv();
79 
81  const std::vector<uint8_t>& iv() const { return iv_; }
82 
87  static bool GenerateRandomIv(FourCC protection_scheme,
88  std::vector<uint8_t>* iv);
89 
90  protected:
91  const AES_KEY* aes_key() const { return aes_key_.get(); }
92  AES_KEY* mutable_aes_key() { return aes_key_.get(); }
93 
94  private:
95  // Internal implementation of crypt function.
96  // |text| points to the input text.
97  // |text_size| is the size of input text.
98  // |crypt_text| points to the output encrypted or decrypted text, depends on
99  // whether it is an encryption or decryption. |text| and |crypt_text| can
100  // point to the same address for in place encryption/decryption.
101  // |crypt_text_size| contains the size of |crypt_text| and it will be updated
102  // to contain the actual encrypted/decrypted size for |crypt_text| on success.
103  // Return false if the input |crypt_text_size| is not large enough to hold the
104  // output |crypt_text| or if there is any error in encryption/decryption.
105  virtual bool CryptInternal(const uint8_t* text,
106  size_t text_size,
107  uint8_t* crypt_text,
108  size_t* crypt_text_size) = 0;
109 
110  // Internal implementation of SetIv, which setup internal iv.
111  virtual void SetIvInternal() = 0;
112 
113  // |size| specifies the input text size.
114  // Return the number of padding bytes needed.
115  // Note: No paddings should be needed except for pkcs5-cbc encryptor.
116  virtual size_t NumPaddingBytes(size_t size) const;
117 
118  // Openssl AES_KEY.
119  scoped_ptr<AES_KEY> aes_key_;
120 
121  // Indicates whether a constant iv is used. Internal iv will be reset to
122  // |iv_| before calling Crypt if that is the case.
123  const ConstantIvFlag constant_iv_flag_;
124  // Initialization vector from by SetIv or InitializeWithIv, with size 8 or 16
125  // bytes.
126  std::vector<uint8_t> iv_;
127  // Tracks number of crypt bytes. It is used to calculate how many blocks
128  // should iv advance in UpdateIv(). It will be reset to 0 after iv is updated.
129  size_t num_crypt_bytes_;
130 
131  DISALLOW_COPY_AND_ASSIGN(AesCryptor);
132 };
133 
134 } // namespace media
135 } // namespace edash_packager
136 
137 #endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
138 
139 
AesCryptor(ConstantIvFlag constant_iv_flag)
Definition: aes_cryptor.cc:32
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:109
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:81
bool Crypt(const uint8_t *text, size_t text_size, uint8_t *crypt_text)
Definition: aes_cryptor.h:55
bool SetIv(const std::vector< uint8_t > &iv)
Definition: aes_cryptor.cc:69