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