Shaka Packager SDK
aes_cryptor.h
1 // Copyright 2016 Google LLC. 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 <mbedtls/cipher.h>
15 
16 #include <packager/macros/classes.h>
17 #include <packager/media/base/fourccs.h>
18 
19 namespace shaka {
20 namespace media {
21 
22 // AES cryptor interface. Inherited by various AES encryptor and decryptor
23 // implementations.
24 class AesCryptor {
25  public:
26  enum ConstantIvFlag {
27  kUseConstantIv,
28  kDontUseConstantIv,
29  };
30 
37  explicit AesCryptor(ConstantIvFlag constant_iv_flag);
38  virtual ~AesCryptor();
39 
42  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
43  const std::vector<uint8_t>& iv) = 0;
44 
45  virtual size_t RequiredOutputSize(size_t plaintext_size) {
46  return plaintext_size;
47  }
48 
54  bool Crypt(const std::vector<uint8_t>& text,
55  std::vector<uint8_t>* crypt_text);
56  bool Crypt(const std::string& text, std::string* crypt_text);
58  bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
59  size_t crypt_text_size = text_size;
60  return Crypt(text, text_size, crypt_text, &crypt_text_size);
61  }
62  bool Crypt(const uint8_t* text,
63  size_t text_size,
64  uint8_t* crypt_text,
65  size_t* crypt_text_size) {
66  if (constant_iv_flag_ == kUseConstantIv)
67  SetIvInternal();
68  else
69  num_crypt_bytes_ += text_size;
70  return CryptInternal(text, text_size, crypt_text, crypt_text_size);
71  }
73 
77  bool SetIv(const std::vector<uint8_t>& iv);
78 
82  void UpdateIv();
83 
85  const std::vector<uint8_t>& iv() const { return iv_; }
86 
88  bool use_constant_iv() const { return constant_iv_flag_ == kUseConstantIv; }
89 
94  static bool GenerateRandomIv(FourCC protection_scheme,
95  std::vector<uint8_t>* iv);
96 
97  protected:
98  enum CipherMode {
99  kCtrMode,
100  kCbcMode,
101  };
102 
103  // mbedTLS cipher context.
104  mbedtls_cipher_context_t cipher_ctx_;
105 
106  bool SetupCipher(size_t key_size, CipherMode mode);
107 
108  private:
109  // Internal implementation of crypt function.
110  // |text| points to the input text.
111  // |text_size| is the size of input text.
112  // |crypt_text| points to the output encrypted or decrypted text, depends on
113  // whether it is an encryption or decryption. |text| and |crypt_text| can
114  // point to the same address for in place encryption/decryption.
115  // |crypt_text_size| contains the size of |crypt_text| and it will be updated
116  // to contain the actual encrypted/decrypted size for |crypt_text| on success.
117  // Return false if the input |crypt_text_size| is not large enough to hold the
118  // output |crypt_text| or if there is any error in encryption/decryption.
119  virtual bool CryptInternal(const uint8_t* text,
120  size_t text_size,
121  uint8_t* crypt_text,
122  size_t* crypt_text_size) = 0;
123 
124  // Internal implementation of SetIv, which setup internal iv.
125  virtual void SetIvInternal() = 0;
126 
127  // |size| specifies the input text size.
128  // Return the number of padding bytes needed.
129  // Note: No paddings should be needed except for pkcs5-cbc encryptor.
130  virtual size_t NumPaddingBytes(size_t size) const;
131 
132  // Indicates whether a constant iv is used. Internal iv will be reset to
133  // |iv_| before calling Crypt if that is the case.
134  const ConstantIvFlag constant_iv_flag_;
135  // Initialization vector from by SetIv or InitializeWithIv, with size 8 or 16
136  // bytes.
137  std::vector<uint8_t> iv_;
138  // Tracks number of crypt bytes. It is used to calculate how many blocks
139  // should iv advance in UpdateIv(). It will be reset to 0 after iv is updated.
140  size_t num_crypt_bytes_;
141 
142  DISALLOW_COPY_AND_ASSIGN(AesCryptor);
143 };
144 
145 } // namespace media
146 } // namespace shaka
147 
148 #endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:85
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:110
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
AesCryptor(ConstantIvFlag constant_iv_flag)
Definition: aes_cryptor.cc:32
bool SetIv(const std::vector< uint8_t > &iv)
Definition: aes_cryptor.cc:70
bool use_constant_iv() const
Definition: aes_cryptor.h:88
bool Crypt(const uint8_t *text, size_t text_size, uint8_t *crypt_text)
Definition: aes_cryptor.h:58
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66