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 
29  bool InitializeWithIv(const std::vector<uint8_t>& key,
30  const std::vector<uint8_t>& iv) override;
31 
32  private:
33  DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
34 };
35 
36 // Class which implements AES-CTR counter-mode encryption.
37 class AesCtrEncryptor : public AesEncryptor {
38  public:
40  ~AesCtrEncryptor() override;
41 
48  void UpdateIv() override;
49 
50  bool SetIv(const std::vector<uint8_t>& iv) override;
52 
53  uint32_t block_offset() const { return block_offset_; }
54 
55  private:
56  bool CryptInternal(const uint8_t* plaintext,
57  size_t plaintext_size,
58  uint8_t* ciphertext,
59  size_t* ciphertext_size) override;
60 
61  // Current block offset.
62  uint32_t block_offset_;
63  // Current AES-CTR counter.
64  std::vector<uint8_t> counter_;
65  // Encrypted counter.
66  std::vector<uint8_t> encrypted_counter_;
67  // Keep track of whether the counter has overflowed.
68  bool counter_overflow_;
69 
70  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
71 };
72 
73 enum CbcPaddingScheme {
74  // Residual block is left unencrypted.
75  kNoPadding,
76  // Residual block is padded with pkcs5 and encrypted.
77  kPkcs5Padding,
78  // Residual block and the next-to-last block are encrypted using ciphertext
79  // stealing method.
80  kCtsPadding,
81 };
82 
83 const bool kChainAcrossCalls = true;
84 
85 // Class which implements AES-CBC (Cipher block chaining) encryption.
86 class AesCbcEncryptor : public AesEncryptor {
87  public:
93  AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls);
94  ~AesCbcEncryptor() override;
95 
98  void UpdateIv() override;
99 
100  bool SetIv(const std::vector<uint8_t>& iv) override;
102 
103  private:
104  bool CryptInternal(const uint8_t* plaintext,
105  size_t plaintext_size,
106  uint8_t* ciphertext,
107  size_t* ciphertext_size) override;
108  size_t NumPaddingBytes(size_t size) const override;
109 
110  const CbcPaddingScheme padding_scheme_;
111  const bool chain_across_calls_;
112 
113  DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
114 };
115 
116 } // namespace media
117 } // namespace edash_packager
118 
119 #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)
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:60