Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "packager/base/macros.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 shaka {
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 
84  bool use_constant_iv() const { return constant_iv_flag_ == kUseConstantIv; }
85 
90  static bool GenerateRandomIv(FourCC protection_scheme,
91  std::vector<uint8_t>* iv);
92 
93  protected:
94  const AES_KEY* aes_key() const { return aes_key_.get(); }
95  AES_KEY* mutable_aes_key() { return aes_key_.get(); }
96 
97  private:
98  // Internal implementation of crypt function.
99  // |text| points to the input text.
100  // |text_size| is the size of input text.
101  // |crypt_text| points to the output encrypted or decrypted text, depends on
102  // whether it is an encryption or decryption. |text| and |crypt_text| can
103  // point to the same address for in place encryption/decryption.
104  // |crypt_text_size| contains the size of |crypt_text| and it will be updated
105  // to contain the actual encrypted/decrypted size for |crypt_text| on success.
106  // Return false if the input |crypt_text_size| is not large enough to hold the
107  // output |crypt_text| or if there is any error in encryption/decryption.
108  virtual bool CryptInternal(const uint8_t* text,
109  size_t text_size,
110  uint8_t* crypt_text,
111  size_t* crypt_text_size) = 0;
112 
113  // Internal implementation of SetIv, which setup internal iv.
114  virtual void SetIvInternal() = 0;
115 
116  // |size| specifies the input text size.
117  // Return the number of padding bytes needed.
118  // Note: No paddings should be needed except for pkcs5-cbc encryptor.
119  virtual size_t NumPaddingBytes(size_t size) const;
120 
121  // Openssl AES_KEY.
122  std::unique_ptr<AES_KEY> aes_key_;
123 
124  // Indicates whether a constant iv is used. Internal iv will be reset to
125  // |iv_| before calling Crypt if that is the case.
126  const ConstantIvFlag constant_iv_flag_;
127  // Initialization vector from by SetIv or InitializeWithIv, with size 8 or 16
128  // bytes.
129  std::vector<uint8_t> iv_;
130  // Tracks number of crypt bytes. It is used to calculate how many blocks
131  // should iv advance in UpdateIv(). It will be reset to 0 after iv is updated.
132  size_t num_crypt_bytes_;
133 
134  DISALLOW_COPY_AND_ASSIGN(AesCryptor);
135 };
136 
137 } // namespace media
138 } // namespace shaka
139 
140 #endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
141 
142 
AesCryptor(ConstantIvFlag constant_iv_flag)
Definition: aes_cryptor.cc:31
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
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:107
bool SetIv(const std::vector< uint8_t > &iv)
Definition: aes_cryptor.cc:67
bool use_constant_iv() const
Definition: aes_cryptor.h:84