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 AesEncryptor {
25  public:
26  AesEncryptor();
27  virtual ~AesEncryptor();
28 
32  virtual bool InitializeWithRandomIv(const std::vector<uint8_t>& key,
33  uint8_t iv_size);
34 
37  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
38  const std::vector<uint8_t>& iv) = 0;
39 
40  virtual size_t NumPaddingBytes(size_t size) = 0;
41 
45  virtual bool EncryptData(const uint8_t* plaintext,
46  size_t plaintext_size,
47  uint8_t* ciphertext) = 0;
48 
49  bool Encrypt(const std::vector<uint8_t>& plaintext,
50  std::vector<uint8_t>* ciphertext);
51 
52  bool Encrypt(const std::string& plaintext, std::string* ciphertext);
54 
59  virtual void UpdateIv() = 0;
60 
63  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
64 
65  const std::vector<uint8_t>& iv() const { return iv_; }
66 
67  protected:
68  // Initialization vector, with size 8 or 16.
69  std::vector<uint8_t> iv_;
70  // Openssl AES_KEY.
71  scoped_ptr<AES_KEY> aes_key_;
72 
73  private:
74  DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
75 };
76 
77 // Class which implements AES-CTR counter-mode encryption/decryption.
78 class AesCtrEncryptor : public AesEncryptor {
79  public:
81  ~AesCtrEncryptor() override;
82 
88  bool InitializeWithIv(const std::vector<uint8_t>& key,
89  const std::vector<uint8_t>& iv) override;
90 
91  size_t NumPaddingBytes(size_t size) override;
92 
93  bool EncryptData(const uint8_t* plaintext,
94  size_t plaintext_size,
95  uint8_t* ciphertext) override;
96 
101  void UpdateIv() override;
102 
103  bool SetIv(const std::vector<uint8_t>& iv) override;
105 
106  uint32_t block_offset() const { return block_offset_; }
107 
108  private:
109  // Current block offset.
110  uint32_t block_offset_;
111  // Current AES-CTR counter.
112  std::vector<uint8_t> counter_;
113  // Encrypted counter.
114  std::vector<uint8_t> encrypted_counter_;
115  // Keep track of whether the counter has overflowed.
116  bool counter_overflow_;
117 
118  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
119 };
120 
121 // Class which implements AES-CBC (Cipher block chaining) encryption with
122 // PKCS#5 padding.
124  public:
126  ~AesCbcPkcs5Encryptor() override;
127 
130  bool InitializeWithIv(const std::vector<uint8_t>& key,
131  const std::vector<uint8_t>& iv) override;
132 
133  size_t NumPaddingBytes(size_t size) override;
134 
135  bool EncryptData(const uint8_t* plaintext,
136  size_t plaintext_size,
137  uint8_t* ciphertext) override;
138 
139  void UpdateIv() override;
140 
141  bool SetIv(const std::vector<uint8_t>& iv) override;
143 
144  private:
145  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Encryptor);
146 };
147 
148 // Class which implements AES-CBC (Cipher block chaining) encryption with
149 // Ciphertext stealing.
151  public:
153  ~AesCbcCtsEncryptor() override;
154 
157  bool InitializeWithIv(const std::vector<uint8_t>& key,
158  const std::vector<uint8_t>& iv) override;
159 
160  size_t NumPaddingBytes(size_t size) override;
161 
162  bool EncryptData(const uint8_t* plaintext,
163  size_t plaintext_size,
164  uint8_t* ciphertext) override;
165 
166  void UpdateIv() override;
167 
168  bool SetIv(const std::vector<uint8_t>& iv) override;
170 
171  private:
172  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsEncryptor);
173 };
174 
175 } // namespace media
176 } // namespace edash_packager
177 
178 #endif // MEDIA_BASE_AES_ENCRYPTOR_H_
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
bool SetIv(const std::vector< uint8_t > &iv) override
bool SetIv(const std::vector< uint8_t > &iv) override
bool SetIv(const std::vector< uint8_t > &iv) override
virtual 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) override
virtual bool SetIv(const std::vector< uint8_t > &iv)=0