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:
30  explicit AesEncryptor(ConstantIvFlag constant_iv_flag);
31  ~AesEncryptor() override;
32 
35  bool InitializeWithIv(const std::vector<uint8_t>& key,
36  const std::vector<uint8_t>& iv) override;
37 
38  private:
39  DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
40 };
41 
42 // Class which implements AES-CTR counter-mode encryption.
43 class AesCtrEncryptor : public AesEncryptor {
44  public:
46  ~AesCtrEncryptor() override;
47 
48  uint32_t block_offset() const { return block_offset_; }
49 
50  private:
51  bool CryptInternal(const uint8_t* plaintext,
52  size_t plaintext_size,
53  uint8_t* ciphertext,
54  size_t* ciphertext_size) override;
55  void SetIvInternal() override;
56 
57  // Current block offset.
58  uint32_t block_offset_;
59  // Current AES-CTR counter.
60  std::vector<uint8_t> counter_;
61  // Encrypted counter.
62  std::vector<uint8_t> encrypted_counter_;
63 
64  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
65 };
66 
67 enum CbcPaddingScheme {
68  // Residual block is left unencrypted.
69  kNoPadding,
70  // Residual block is padded with pkcs5 and encrypted.
71  kPkcs5Padding,
72  // Residual block and the next-to-last block are encrypted using ciphertext
73  // stealing method.
74  kCtsPadding,
75 };
76 
77 // Class which implements AES-CBC (Cipher block chaining) encryption.
78 class AesCbcEncryptor : public AesEncryptor {
79  public:
84  explicit AesCbcEncryptor(CbcPaddingScheme padding_scheme);
85 
93  AesCbcEncryptor(CbcPaddingScheme padding_scheme,
94  ConstantIvFlag constant_iv_flag);
95 
96  ~AesCbcEncryptor() override;
97 
98  private:
99  bool CryptInternal(const uint8_t* plaintext,
100  size_t plaintext_size,
101  uint8_t* ciphertext,
102  size_t* ciphertext_size) override;
103  void SetIvInternal() override;
104  size_t NumPaddingBytes(size_t size) const override;
105 
106  const CbcPaddingScheme padding_scheme_;
107  // 16-byte internal iv for crypto operations.
108  std::vector<uint8_t> internal_iv_;
109 
110  DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
111 };
112 
113 } // namespace media
114 } // namespace edash_packager
115 
116 #endif // MEDIA_BASE_AES_ENCRYPTOR_H_
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
AesEncryptor(ConstantIvFlag constant_iv_flag)
AesCbcEncryptor(CbcPaddingScheme padding_scheme)
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:81