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 
16 struct aes_key_st;
17 typedef struct aes_key_st AES_KEY;
18 
19 namespace edash_packager {
20 namespace media {
21 
22 // AES cryptor interface. Inherited by various AES encryptor and decryptor
23 // implementations.
24 class AesCryptor {
25  public:
26  AesCryptor();
27  virtual ~AesCryptor();
28 
31  virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
32  const std::vector<uint8_t>& iv) = 0;
33 
39  bool Crypt(const std::vector<uint8_t>& text,
40  std::vector<uint8_t>* crypt_text);
41  bool Crypt(const std::string& text, std::string* crypt_text);
43  bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
44  size_t crypt_text_size = text_size;
45  return CryptInternal(text, text_size, crypt_text, &crypt_text_size);
46  }
48 
51  virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
52 
56  virtual void UpdateIv() = 0;
57 
59  const std::vector<uint8_t>& iv() const { return iv_; }
60 
61  protected:
62  void set_iv(const std::vector<uint8_t>& iv) { iv_ = iv; }
63  const AES_KEY* aes_key() const { return aes_key_.get(); }
64  AES_KEY* mutable_aes_key() { return aes_key_.get(); }
65 
66  private:
67  // Internal implementation of crypt function.
68  // |text| points to the input text.
69  // |text_size| is the size of input text.
70  // |crypt_text| points to the output encrypted or decrypted text, depends on
71  // whether it is an encryption or decryption. |text| and |crypt_text| can
72  // point to the same address for in place encryption/decryption.
73  // |crypt_text_size| contains the size of |crypt_text| and it will be updated
74  // to contain the actual encrypted/decrypted size for |crypt_text| on success.
75  // Return false if the input |crypt_text_size| is not large enough to hold the
76  // output |crypt_text| or if there is any error in encryption/decryption.
77  virtual bool CryptInternal(const uint8_t* text,
78  size_t text_size,
79  uint8_t* crypt_text,
80  size_t* crypt_text_size) = 0;
81 
82  // |size| specifies the input text size.
83  // Return the number of padding bytes needed.
84  // Note: No paddings should be needed except for pkcs5-cbc encryptor.
85  virtual size_t NumPaddingBytes(size_t size) const;
86 
87  // Initialization vector, with size 8 or 16.
88  std::vector<uint8_t> iv_;
89  // Openssl AES_KEY.
90  scoped_ptr<AES_KEY> aes_key_;
91 
92  DISALLOW_COPY_AND_ASSIGN(AesCryptor);
93 };
94 
95 } // namespace media
96 } // namespace edash_packager
97 
98 #endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
99 
100 
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:59
bool Crypt(const uint8_t *text, size_t text_size, uint8_t *crypt_text)
Definition: aes_cryptor.h:43