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  bool InitializeWithRandomIv(const std::vector<uint8_t>& key,
33  uint8_t iv_size);
34 
37  bool InitializeWithIv(const std::vector<uint8_t>& key,
38  const std::vector<uint8_t>& iv);
39 
42  bool Encrypt(const std::vector<uint8_t>& plaintext,
43  std::vector<uint8_t>* ciphertext);
44  bool Encrypt(const std::string& plaintext, std::string* ciphertext);
45  bool Encrypt(const uint8_t* plaintext,
46  size_t plaintext_size,
47  uint8_t* ciphertext) {
48  return EncryptInternal(plaintext, plaintext_size, ciphertext);
49  }
51 
55  virtual void UpdateIv() = 0;
56 
59  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
60 
62  const std::vector<uint8_t>& iv() const { return iv_; }
63 
64  protected:
70  virtual bool EncryptInternal(const uint8_t* plaintext,
71  size_t plaintext_size,
72  uint8_t* ciphertext) = 0;
75  virtual size_t NumPaddingBytes(size_t size) const = 0;
76 
77  void set_iv(const std::vector<uint8_t>& iv) { iv_ = iv; }
78  AES_KEY* aes_key() const { return aes_key_.get(); }
79 
80  private:
81  // Initialization vector, with size 8 or 16.
82  std::vector<uint8_t> iv_;
83  // Openssl AES_KEY.
84  scoped_ptr<AES_KEY> aes_key_;
85 
86  DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
87 };
88 
89 // Class which implements AES-CTR counter-mode encryption.
90 class AesCtrEncryptor : public AesEncryptor {
91  public:
93  ~AesCtrEncryptor() override;
94 
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  protected:
109  bool EncryptInternal(const uint8_t* plaintext,
110  size_t plaintext_size,
111  uint8_t* ciphertext) override;
112  size_t NumPaddingBytes(size_t size) const override;
113 
114  private:
115  // Current block offset.
116  uint32_t block_offset_;
117  // Current AES-CTR counter.
118  std::vector<uint8_t> counter_;
119  // Encrypted counter.
120  std::vector<uint8_t> encrypted_counter_;
121  // Keep track of whether the counter has overflowed.
122  bool counter_overflow_;
123 
124  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
125 };
126 
127 enum CbcPaddingScheme {
128  kNoPadding,
129  kPkcs5Padding,
130  kCtsPadding,
131 };
132 
133 const bool kChainAcrossCalls = true;
134 
135 // Class which implements AES-CBC (Cipher block chaining) encryption.
137  public:
143  AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls);
144  ~AesCbcEncryptor() override;
145 
148  void UpdateIv() override;
149 
150  bool SetIv(const std::vector<uint8_t>& iv) override;
152 
153  protected:
154  bool EncryptInternal(const uint8_t* plaintext,
155  size_t plaintext_size,
156  uint8_t* ciphertext) override;
157  size_t NumPaddingBytes(size_t size) const override;
158 
159  private:
160  const CbcPaddingScheme padding_scheme_;
161  const bool chain_across_calls_;
162 
163  DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
164 };
165 
166 } // namespace media
167 } // namespace edash_packager
168 
169 #endif // MEDIA_BASE_AES_ENCRYPTOR_H_
bool EncryptInternal(const uint8_t *plaintext, size_t plaintext_size, uint8_t *ciphertext) override
size_t NumPaddingBytes(size_t size) const override
bool SetIv(const std::vector< uint8_t > &iv) override
size_t NumPaddingBytes(size_t size) const override
const std::vector< uint8_t > & iv() const
Definition: aes_encryptor.h:62
virtual bool EncryptInternal(const uint8_t *plaintext, size_t plaintext_size, uint8_t *ciphertext)=0
bool EncryptInternal(const uint8_t *plaintext, size_t plaintext_size, uint8_t *ciphertext) override
bool SetIv(const std::vector< uint8_t > &iv) override
AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls)
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)
virtual size_t NumPaddingBytes(size_t size) const =0
virtual bool SetIv(const std::vector< uint8_t > &iv)=0