DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
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 
48  bool Encrypt(const uint8_t* plaintext,
49  size_t plaintext_size,
50  uint8_t* ciphertext);
51 
52  bool Encrypt(const std::vector<uint8_t>& plaintext,
53  std::vector<uint8_t>* ciphertext) {
54  ciphertext->resize(plaintext.size());
55  return Encrypt(&plaintext[0], plaintext.size(), &(*ciphertext)[0]);
56  }
57 
58  bool Encrypt(const std::string& plaintext, std::string* ciphertext) {
59  ciphertext->resize(plaintext.size());
60  return Encrypt(reinterpret_cast<const uint8_t*>(plaintext.data()),
61  plaintext.size(),
62  reinterpret_cast<uint8_t*>(string_as_array(ciphertext)));
63  }
65 
66  // For AES CTR, encryption and decryption are identical.
67  bool Decrypt(const uint8_t* ciphertext,
68  size_t ciphertext_size,
69  uint8_t* plaintext) {
70  return Encrypt(ciphertext, ciphertext_size, plaintext);
71  }
72 
73  bool Decrypt(const std::vector<uint8_t>& ciphertext,
74  std::vector<uint8_t>* plaintext) {
75  return Encrypt(ciphertext, plaintext);
76  }
77 
78  bool Decrypt(const std::string& ciphertext, std::string* plaintext) {
79  return Encrypt(ciphertext, plaintext);
80  }
81 
86  void UpdateIv();
87 
90  bool SetIv(const std::vector<uint8_t>& iv);
91 
92  const std::vector<uint8_t>& iv() const { return iv_; }
93 
94  uint32_t block_offset() const { return block_offset_; }
95 
96  private:
97  // Initialization vector, with size 8 or 16.
98  std::vector<uint8_t> iv_;
99  // Current block offset.
100  uint32_t block_offset_;
101  // Openssl AES_KEY.
102  scoped_ptr<AES_KEY> aes_key_;
103  // Current AES-CTR counter.
104  std::vector<uint8_t> counter_;
105  // Encrypted counter.
106  std::vector<uint8_t> encrypted_counter_;
107  // Keep track of whether the counter has overflowed.
108  bool counter_overflow_;
109 
110  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
111 };
112 
113 // Class which implements AES-CBC (Cipher block chaining) encryption with
114 // PKCS#5 padding.
116  public:
119 
125  bool InitializeWithIv(const std::vector<uint8_t>& key,
126  const std::vector<uint8_t>& iv);
127 
130  void Encrypt(const std::string& plaintext, std::string* ciphertext);
131 
133  bool SetIv(const std::vector<uint8_t>& iv);
134 
135  const std::vector<uint8_t>& iv() const { return iv_; }
136 
137  private:
138  std::vector<uint8_t> iv_;
139  scoped_ptr<AES_KEY> encrypt_key_;
140 
141  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Encryptor);
142 };
143 
144 // Class which implements AES-CBC (Cipher block chaining) decryption with
145 // PKCS#5 padding.
147  public:
150 
156  bool InitializeWithIv(const std::vector<uint8_t>& key,
157  const std::vector<uint8_t>& iv);
158 
162  bool Decrypt(const std::string& ciphertext, std::string* plaintext);
163 
165  bool SetIv(const std::vector<uint8_t>& iv);
166 
167  const std::vector<uint8_t>& iv() const { return iv_; }
168 
169  private:
170  std::vector<uint8_t> iv_;
171  scoped_ptr<AES_KEY> decrypt_key_;
172 
173  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Decryptor);
174 };
175 
176 // Class which implements AES-CBC (Cipher block chaining) encryption with
177 // Ciphertext stealing.
179  public:
182 
188  bool InitializeWithIv(const std::vector<uint8_t>& key,
189  const std::vector<uint8_t>& iv);
190 
196  void Encrypt(const uint8_t* plaintext, size_t size, uint8_t* ciphertext);
197 
201  void Encrypt(const std::vector<uint8_t>& plaintext,
202  std::vector<uint8_t>* ciphertext);
203 
206  bool SetIv(const std::vector<uint8_t>& iv);
207 
208  const std::vector<uint8_t>& iv() const { return iv_; }
209 
210  private:
211  std::vector<uint8_t> iv_;
212  scoped_ptr<AES_KEY> encrypt_key_;
213 
214  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsEncryptor);
215 };
216 
217 // Class which implements AES-CBC (Cipher block chaining) decryption with
218 // Ciphertext stealing.
220  public:
223 
229  bool InitializeWithIv(const std::vector<uint8_t>& key,
230  const std::vector<uint8_t>& iv);
231 
237  void Decrypt(const uint8_t* ciphertext, size_t size, uint8_t* plaintext);
238 
242  void Decrypt(const std::vector<uint8_t>& ciphertext,
243  std::vector<uint8_t>* plaintext);
244 
246  bool SetIv(const std::vector<uint8_t>& iv);
247 
248  const std::vector<uint8_t>& iv() const { return iv_; }
249 
250  private:
251  std::vector<uint8_t> iv_;
252  scoped_ptr<AES_KEY> decrypt_key_;
253 
254  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsDecryptor);
255 };
256 
257 } // namespace media
258 } // namespace edash_packager
259 
260 #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)