DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
aes_decryptor.h
1 // Copyright 2016 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 Decryptor implementation using openssl.
8 
9 #ifndef MEDIA_BASE_AES_DECRYPTOR_H_
10 #define MEDIA_BASE_AES_DECRYPTOR_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 #include "packager/media/base/aes_encryptor.h"
18 
19 struct aes_key_st;
20 typedef struct aes_key_st AES_KEY;
21 
22 namespace edash_packager {
23 namespace media {
24 
25 class AesDecryptor {
26  public:
27  AesDecryptor();
28  virtual ~AesDecryptor();
29 
30  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
31  const std::vector<uint8_t>& iv) = 0;
32 
36  virtual bool Decrypt(const uint8_t* ciphertext,
37  size_t ciphertext_size,
38  uint8_t* plaintext) = 0;
39 
40  virtual bool Decrypt(const std::vector<uint8_t>& ciphertext,
41  std::vector<uint8_t>* plaintext) = 0;
42 
43  virtual bool Decrypt(const std::string& ciphertext,
44  std::string* plaintext) = 0;
46 
49  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
50 
51  const std::vector<uint8_t>& iv() const { return iv_; }
52 
53  protected:
54  // Initialization vector, with size 8 or 16.
55  std::vector<uint8_t> iv_;
56  // Openssl AES_KEY.
57  scoped_ptr<AES_KEY> aes_key_;
58 
59  private:
60  DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
61 };
62 
63 // Class which implements AES-CTR counter-mode decryption.
64 class AesCtrDecryptor : public AesDecryptor {
65  public:
67  ~AesCtrDecryptor() override;
68 
71  bool InitializeWithIv(const std::vector<uint8_t>& key,
72  const std::vector<uint8_t>& iv) override;
73 
74  bool Decrypt(const uint8_t* ciphertext,
75  size_t ciphertext_size,
76  uint8_t* plaintext) override;
77 
78  bool Decrypt(const std::vector<uint8_t>& ciphertext,
79  std::vector<uint8_t>* plaintext) override;
80 
81  bool Decrypt(const std::string& ciphertext, std::string* plaintext) override;
82 
83  bool SetIv(const std::vector<uint8_t>& iv) override;
85 
86  uint32_t block_offset() const { return encryptor_->block_offset(); }
87 
88  private:
89  scoped_ptr<AesCtrEncryptor> encryptor_;
90 
91  DISALLOW_COPY_AND_ASSIGN(AesCtrDecryptor);
92 };
93 
94 // Class which implements AES-CBC (Cipher block chaining) decryption with
95 // PKCS#5 padding.
97  public:
99  ~AesCbcPkcs5Decryptor() override;
100 
103  bool InitializeWithIv(const std::vector<uint8_t>& key,
104  const std::vector<uint8_t>& iv) override;
105 
106  bool Decrypt(const uint8_t* ciphertext,
107  size_t ciphertext_size,
108  uint8_t* plaintext) override;
109 
110  bool Decrypt(const std::vector<uint8_t>& ciphertext,
111  std::vector<uint8_t>* plaintext) override;
112 
113  bool Decrypt(const std::string& ciphertext, std::string* plaintext) override;
114 
115  bool SetIv(const std::vector<uint8_t>& iv) override;
117 
118  private:
119  DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Decryptor);
120 };
121 
122 // Class which implements AES-CBC (Cipher block chaining) decryption with
123 // Ciphertext stealing.
125  public:
127  ~AesCbcCtsDecryptor() override;
128 
131  bool InitializeWithIv(const std::vector<uint8_t>& key,
132  const std::vector<uint8_t>& iv) override;
133 
134  bool Decrypt(const uint8_t* ciphertext,
135  size_t ciphertext_size,
136  uint8_t* plaintext) override;
137 
138  bool Decrypt(const std::vector<uint8_t>& ciphertext,
139  std::vector<uint8_t>* plaintext) override;
140 
141  bool Decrypt(const std::string& ciphertext, std::string* plaintext) override;
142 
143  bool SetIv(const std::vector<uint8_t>& iv) override;
145 
146  private:
147  DISALLOW_COPY_AND_ASSIGN(AesCbcCtsDecryptor);
148 };
149 
150 } // namespace media
151 } // namespace edash_packager
152 
153 #endif // MEDIA_BASE_AES_DECRYPTOR_H_
virtual bool SetIv(const std::vector< uint8_t > &iv)=0
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