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/macros.h"
16 #include "packager/base/memory/scoped_ptr.h"
17 #include "packager/media/base/aes_cryptor.h"
18 
19 namespace edash_packager {
20 namespace media {
21 
22 class AesEncryptor : public AesCryptor {
23  public:
24  AesEncryptor();
25  ~AesEncryptor() override;
26 
30  bool InitializeWithRandomIv(const std::vector<uint8_t>& key, uint8_t iv_size);
31 
34  bool InitializeWithIv(const std::vector<uint8_t>& key,
35  const std::vector<uint8_t>& iv) override;
36 
37  private:
38  DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
39 };
40 
41 // Class which implements AES-CTR counter-mode encryption.
42 class AesCtrEncryptor : public AesEncryptor {
43  public:
45  ~AesCtrEncryptor() override;
46 
53  void UpdateIv() override;
54 
55  bool SetIv(const std::vector<uint8_t>& iv) override;
57 
58  uint32_t block_offset() const { return block_offset_; }
59 
60  private:
61  bool CryptInternal(const uint8_t* plaintext,
62  size_t plaintext_size,
63  uint8_t* ciphertext,
64  size_t* ciphertext_size) override;
65 
66  // Current block offset.
67  uint32_t block_offset_;
68  // Current AES-CTR counter.
69  std::vector<uint8_t> counter_;
70  // Encrypted counter.
71  std::vector<uint8_t> encrypted_counter_;
72  // Keep track of whether the counter has overflowed.
73  bool counter_overflow_;
74 
75  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
76 };
77 
78 enum CbcPaddingScheme {
79  kNoPadding,
80  kPkcs5Padding,
81  kCtsPadding,
82 };
83 
84 const bool kChainAcrossCalls = true;
85 
86 // Class which implements AES-CBC (Cipher block chaining) encryption.
87 class AesCbcEncryptor : public AesEncryptor {
88  public:
94  AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls);
95  ~AesCbcEncryptor() override;
96 
99  void UpdateIv() override;
100 
101  bool SetIv(const std::vector<uint8_t>& iv) override;
103 
104  private:
105  bool CryptInternal(const uint8_t* plaintext,
106  size_t plaintext_size,
107  uint8_t* ciphertext,
108  size_t* ciphertext_size) override;
109  size_t NumPaddingBytes(size_t size) const override;
110 
111  const CbcPaddingScheme padding_scheme_;
112  const bool chain_across_calls_;
113 
114  DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
115 };
116 
117 } // namespace media
118 } // namespace edash_packager
119 
120 #endif // MEDIA_BASE_AES_ENCRYPTOR_H_
bool InitializeWithIv(const std::vector< uint8_t > &key, 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
AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls)
bool InitializeWithRandomIv(const std::vector< uint8_t > &key, uint8_t iv_size)
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:59