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 
32  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
33  const std::vector<uint8_t>& iv) = 0;
34 
38  bool Decrypt(const std::vector<uint8_t>& ciphertext,
39  std::vector<uint8_t>* plaintext);
40  bool Decrypt(const std::string& ciphertext, std::string* plaintext);
41  bool Decrypt(const uint8_t* ciphertext,
42  size_t ciphertext_size,
43  uint8_t* plaintext) {
44  size_t plaintext_size;
45  return DecryptInternal(ciphertext, ciphertext_size, plaintext,
46  &plaintext_size);
47  }
49 
52  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
53 
54  protected:
62  virtual bool DecryptInternal(const uint8_t* ciphertext,
63  size_t ciphertext_size,
64  uint8_t* plaintext,
65  size_t* plaintext_size) = 0;
66 
67  private:
68  DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
69 };
70 
71 // Class which implements AES-CTR counter-mode decryption.
72 class AesCtrDecryptor : public AesDecryptor {
73  public:
75  ~AesCtrDecryptor() override;
76 
79  bool InitializeWithIv(const std::vector<uint8_t>& key,
80  const std::vector<uint8_t>& iv) override;
81 
82  bool SetIv(const std::vector<uint8_t>& iv) override;
84 
85  uint32_t block_offset() const { return encryptor_->block_offset(); }
86 
87  protected:
88  bool DecryptInternal(const uint8_t* ciphertext,
89  size_t ciphertext_size,
90  uint8_t* plaintext,
91  size_t* plaintext_size) override;
92 
93  private:
94  scoped_ptr<AesCtrEncryptor> encryptor_;
95 
96  DISALLOW_COPY_AND_ASSIGN(AesCtrDecryptor);
97 };
98 
99 // Class which implements AES-CBC (Cipher block chaining) decryption.
101  public:
107  AesCbcDecryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls);
108  ~AesCbcDecryptor() override;
109 
112  bool InitializeWithIv(const std::vector<uint8_t>& key,
113  const std::vector<uint8_t>& iv) override;
114 
115  bool SetIv(const std::vector<uint8_t>& iv) override;
117 
118  protected:
119  bool DecryptInternal(const uint8_t* ciphertext,
120  size_t ciphertext_size,
121  uint8_t* plaintext,
122  size_t* plaintext_size) override;
123 
124  private:
125  // Openssl AES_KEY.
126  scoped_ptr<AES_KEY> aes_key_;
127  // Initialization vector, must be 16 for CBC.
128  std::vector<uint8_t> iv_;
129  const CbcPaddingScheme padding_scheme_;
130  const bool chain_across_calls_;
131 
132  DISALLOW_COPY_AND_ASSIGN(AesCbcDecryptor);
133 };
134 
135 } // namespace media
136 } // namespace edash_packager
137 
138 #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 DecryptInternal(const uint8_t *ciphertext, size_t ciphertext_size, uint8_t *plaintext, size_t *plaintext_size) override
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
virtual bool DecryptInternal(const uint8_t *ciphertext, size_t ciphertext_size, uint8_t *plaintext, size_t *plaintext_size)=0
bool DecryptInternal(const uint8_t *ciphertext, size_t ciphertext_size, uint8_t *plaintext, size_t *plaintext_size) override
AesCbcDecryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls)
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override