DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
aes_encryptor.h
1 // Copyright 2014 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 // AES Encryptor implementation using openssl.
8 
9 #ifndef MEDIA_BASE_AES_ENCRYPTOR_H_
10 #define MEDIA_BASE_AES_ENCRYPTOR_H_
11 
12 #include <string>
13 #include <vector>
14 
15 #include "packager/base/memory/scoped_ptr.h"
16 #include "packager/base/stl_util.h"
17 
18 struct aes_key_st;
19 typedef struct aes_key_st AES_KEY;
20 
21 namespace edash_packager {
22 namespace media {
23 
24 // Class which implements AES-CTR counter-mode encryption/decryption.
26  public:
28  ~AesCtrEncryptor();
29 
35  bool InitializeWithRandomIv(const std::vector<uint8_t>& key, uint8_t iv_size);
36 
42  bool InitializeWithIv(const std::vector<uint8_t>& key,
43  const std::vector<uint8_t>& iv);
44 
49  bool Encrypt(const uint8_t* plaintext,
50  size_t plaintext_size,
51  uint8_t* ciphertext);
52 
53  bool Encrypt(const std::vector<uint8_t>& plaintext,
54  std::vector<uint8_t>* ciphertext) {
55  ciphertext->resize(plaintext.size());
56  return Encrypt(&plaintext[0], plaintext.size(), &(*ciphertext)[0]);
57  }
58 
59  bool Encrypt(const std::string& plaintext, std::string* ciphertext) {
60  ciphertext->resize(plaintext.size());
61  return Encrypt(reinterpret_cast<const uint8_t*>(plaintext.data()),
62  plaintext.size(),
63  reinterpret_cast<uint8_t*>(string_as_array(ciphertext)));
64  }
66 
67  // For AES CTR, encryption and decryption are identical.
68  bool Decrypt(const uint8_t* ciphertext,
69  size_t ciphertext_size,
70  uint8_t* plaintext) {
71  return Encrypt(ciphertext, ciphertext_size, plaintext);
72  }
73 
74  bool Decrypt(const std::vector<uint8_t>& ciphertext,
75  std::vector<uint8_t>* plaintext) {
76  return Encrypt(ciphertext, plaintext);
77  }
78 
79  bool Decrypt(const std::string& ciphertext, std::string* plaintext) {
80  return Encrypt(ciphertext, plaintext);
81  }
82 
87  void UpdateIv();
88 
91  bool SetIv(const std::vector<uint8_t>& iv);
92 
93  const std::vector<uint8_t>& iv() const { return iv_; }
94 
95  uint32_t block_offset() const { return block_offset_; }
96 
97  private:
98  // Initialization vector, with size 8 or 16.
99  std::vector<uint8_t> iv_;
100  // Current block offset.
101  uint32_t block_offset_;
102  // Openssl AES_KEY.
103  scoped_ptr<AES_KEY> aes_key_;
104  // Current AES-CTR counter.
105  std::vector<uint8_t> counter_;
106  // Encrypted counter.
107  std::vector<uint8_t> encrypted_counter_;
108  // Keep track of whether the counter has overflowed.
109  bool counter_overflow_;
110 
111  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
112 };
113 
114 // Class which implements AES-CBC (Cipher block chaining) encryption with
115 // PKCS#5 padding.
117  public:
120 
126  bool InitializeWithIv(const std::vector<uint8_t>& key,
127  const std::vector<uint8_t>& iv);
128 
131  void Encrypt(const std::string& plaintext, std::string* ciphertext);
132 
134  bool SetIv(const std::vector<uint8_t>& iv);
135 
136  const std::vector<uint8_t>& iv() const { return iv_; }
137 
138  private:
139  std::vector<uint8_t> iv_;
140  scoped_ptr<AES_KEY> encrypt_key_;
141 
142  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Encryptor);
143 };
144 
145 // Class which implements AES-CBC (Cipher block chaining) decryption with
146 // PKCS#5 padding.
148  public:
151 
157  bool InitializeWithIv(const std::vector<uint8_t>& key,
158  const std::vector<uint8_t>& iv);
159 
163  bool Decrypt(const std::string& ciphertext, std::string* plaintext);
164 
166  bool SetIv(const std::vector<uint8_t>& iv);
167 
168  const std::vector<uint8_t>& iv() const { return iv_; }
169 
170  private:
171  std::vector<uint8_t> iv_;
172  scoped_ptr<AES_KEY> decrypt_key_;
173 
174  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Decryptor);
175 };
176 
177 // Class which implements AES-CBC (Cipher block chaining) encryption with
178 // Ciphertext stealing.
180  public:
183 
189  bool InitializeWithIv(const std::vector<uint8_t>& key,
190  const std::vector<uint8_t>& iv);
191 
197  void Encrypt(const uint8_t* plaintext, size_t size, uint8_t* ciphertext);
198 
202  void Encrypt(const std::vector<uint8_t>& plaintext,
203  std::vector<uint8_t>* ciphertext);
204 
207  bool SetIv(const std::vector<uint8_t>& iv);
208 
209  const std::vector<uint8_t>& iv() const { return iv_; }
210 
211  private:
212  std::vector<uint8_t> iv_;
213  scoped_ptr<AES_KEY> encrypt_key_;
214 
215  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsEncryptor);
216 };
217 
218 // Class which implements AES-CBC (Cipher block chaining) decryption with
219 // Ciphertext stealing.
221  public:
224 
230  bool InitializeWithIv(const std::vector<uint8_t>& key,
231  const std::vector<uint8_t>& iv);
232 
238  void Decrypt(const uint8_t* ciphertext, size_t size, uint8_t* plaintext);
239 
243  void Decrypt(const std::vector<uint8_t>& ciphertext,
244  std::vector<uint8_t>* plaintext);
245 
247  bool SetIv(const std::vector<uint8_t>& iv);
248 
249  const std::vector<uint8_t>& iv() const { return iv_; }
250 
251  private:
252  std::vector<uint8_t> iv_;
253  scoped_ptr<AES_KEY> decrypt_key_;
254 
255  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsDecryptor);
256 };
257 
258 } // namespace media
259 } // namespace edash_packager
260 
261 #endif // MEDIA_BASE_AES_ENCRYPTOR_H_
void Encrypt(const uint8_t *plaintext, size_t size, uint8_t *ciphertext)
bool SetIv(const std::vector< uint8_t > &iv)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)
void Decrypt(const uint8_t *ciphertext, size_t size, uint8_t *plaintext)
bool SetIv(const std::vector< uint8_t > &iv)
bool SetIv(const std::vector< uint8_t > &iv)
bool SetIv(const std::vector< uint8_t > &iv)
bool SetIv(const std::vector< uint8_t > &iv)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)
bool Decrypt(const std::string &ciphertext, std::string *plaintext)
void Encrypt(const std::string &plaintext, std::string *ciphertext)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)
bool InitializeWithRandomIv(const std::vector< uint8_t > &key, uint8_t iv_size)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)