Shaka Packager SDK
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 PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
10 #define PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
11 
12 #include <string>
13 #include <vector>
14 
15 #include "packager/base/macros.h"
16 #include "packager/media/base/aes_cryptor.h"
17 
18 namespace shaka {
19 namespace media {
20 
21 class AesEncryptor : public AesCryptor {
22  public:
29  explicit AesEncryptor(ConstantIvFlag constant_iv_flag);
30  ~AesEncryptor() override;
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 
47  uint32_t block_offset() const { return block_offset_; }
48 
49  private:
50  bool CryptInternal(const uint8_t* plaintext,
51  size_t plaintext_size,
52  uint8_t* ciphertext,
53  size_t* ciphertext_size) override;
54  void SetIvInternal() override;
55 
56  // Current block offset.
57  uint32_t block_offset_;
58  // Current AES-CTR counter.
59  std::vector<uint8_t> counter_;
60  // Encrypted counter.
61  std::vector<uint8_t> encrypted_counter_;
62 
63  DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
64 };
65 
66 enum CbcPaddingScheme {
67  // Residual block is left unencrypted.
68  kNoPadding,
69  // Residual block is padded with pkcs5 and encrypted.
70  kPkcs5Padding,
71  // Residual block and the next-to-last block are encrypted using ciphertext
72  // stealing method.
73  kCtsPadding,
74 };
75 
76 // Class which implements AES-CBC (Cipher block chaining) encryption.
77 class AesCbcEncryptor : public AesEncryptor {
78  public:
83  explicit AesCbcEncryptor(CbcPaddingScheme padding_scheme);
84 
92  AesCbcEncryptor(CbcPaddingScheme padding_scheme,
93  ConstantIvFlag constant_iv_flag);
94 
95  ~AesCbcEncryptor() override;
96 
97  private:
98  bool CryptInternal(const uint8_t* plaintext,
99  size_t plaintext_size,
100  uint8_t* ciphertext,
101  size_t* ciphertext_size) override;
102  void SetIvInternal() override;
103  size_t NumPaddingBytes(size_t size) const override;
104 
105  const CbcPaddingScheme padding_scheme_;
106  // 16-byte internal iv for crypto operations.
107  std::vector<uint8_t> internal_iv_;
108 
109  DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
110 };
111 
112 } // namespace media
113 } // namespace shaka
114 
115 #endif // PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
shaka::media::AesEncryptor
Definition: aes_encryptor.h:21
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::AesCbcEncryptor::AesCbcEncryptor
AesCbcEncryptor(CbcPaddingScheme padding_scheme)
Definition: aes_encryptor.cc:99
shaka::media::AesCryptor::iv
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:82
shaka::media::AesCbcEncryptor
Definition: aes_encryptor.h:77
shaka::media::AesCryptor
Definition: aes_cryptor.h:25
shaka::media::AesCtrEncryptor
Definition: aes_encryptor.h:42
shaka::media::AesEncryptor::AesEncryptor
AesEncryptor(ConstantIvFlag constant_iv_flag)
Definition: aes_encryptor.cc:35
shaka::media::AesEncryptor::InitializeWithIv
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
Definition: aes_encryptor.cc:39