DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
aes_cryptor.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 #ifndef PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
8 #define PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
9 
10 #include <string>
11 #include <vector>
12 
13 #include "packager/base/macros.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 #include "packager/media/base/fourccs.h"
16 
17 struct aes_key_st;
18 typedef struct aes_key_st AES_KEY;
19 
20 namespace edash_packager {
21 namespace media {
22 
23 // AES cryptor interface. Inherited by various AES encryptor and decryptor
24 // implementations.
25 class AesCryptor {
26  public:
27  AesCryptor();
28  virtual ~AesCryptor();
29 
32  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
33  const std::vector<uint8_t>& iv) = 0;
34 
40  bool Crypt(const std::vector<uint8_t>& text,
41  std::vector<uint8_t>* crypt_text);
42  bool Crypt(const std::string& text, std::string* crypt_text);
44  bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
45  size_t crypt_text_size = text_size;
46  return CryptInternal(text, text_size, crypt_text, &crypt_text_size);
47  }
49 
52  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
53 
57  virtual void UpdateIv() = 0;
58 
60  const std::vector<uint8_t>& iv() const { return iv_; }
61 
66  static bool GenerateRandomIv(FourCC protection_scheme,
67  std::vector<uint8_t>* iv);
68 
69  protected:
70  void set_iv(const std::vector<uint8_t>& iv) { iv_ = iv; }
71  const AES_KEY* aes_key() const { return aes_key_.get(); }
72  AES_KEY* mutable_aes_key() { return aes_key_.get(); }
73 
74  private:
75  // Internal implementation of crypt function.
76  // |text| points to the input text.
77  // |text_size| is the size of input text.
78  // |crypt_text| points to the output encrypted or decrypted text, depends on
79  // whether it is an encryption or decryption. |text| and |crypt_text| can
80  // point to the same address for in place encryption/decryption.
81  // |crypt_text_size| contains the size of |crypt_text| and it will be updated
82  // to contain the actual encrypted/decrypted size for |crypt_text| on success.
83  // Return false if the input |crypt_text_size| is not large enough to hold the
84  // output |crypt_text| or if there is any error in encryption/decryption.
85  virtual bool CryptInternal(const uint8_t* text,
86  size_t text_size,
87  uint8_t* crypt_text,
88  size_t* crypt_text_size) = 0;
89 
90  // |size| specifies the input text size.
91  // Return the number of padding bytes needed.
92  // Note: No paddings should be needed except for pkcs5-cbc encryptor.
93  virtual size_t NumPaddingBytes(size_t size) const;
94 
95  // Initialization vector, with size 8 or 16.
96  std::vector<uint8_t> iv_;
97  // Openssl AES_KEY.
98  scoped_ptr<AES_KEY> aes_key_;
99 
100  DISALLOW_COPY_AND_ASSIGN(AesCryptor);
101 };
102 
103 } // namespace media
104 } // namespace edash_packager
105 
106 #endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
107 
108 
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:58
virtual bool SetIv(const std::vector< uint8_t > &iv)=0
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:60
bool Crypt(const uint8_t *text, size_t text_size, uint8_t *crypt_text)
Definition: aes_cryptor.h:44