Shaka Packager SDK
Loading...
Searching...
No Matches
aes_encryptor.h
1// Copyright 2014 Google LLC. 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 mbedtls.
8
9#ifndef PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
10#define PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
11
12#include <cstdint>
13#include <string>
14#include <vector>
15
16#include <packager/macros/classes.h>
17#include <packager/media/base/aes_cryptor.h>
18
19namespace shaka {
20namespace media {
21
22// Class which implements AES-CTR counter-mode encryption.
24 public:
26 ~AesCtrEncryptor() override;
27
28 uint32_t block_offset() const { return block_offset_; }
29
32 bool InitializeWithIv(const std::vector<uint8_t>& key,
33 const std::vector<uint8_t>& iv) override;
34
35 private:
36 bool CryptInternal(const uint8_t* plaintext,
37 size_t plaintext_size,
38 uint8_t* ciphertext,
39 size_t* ciphertext_size) override;
40 void SetIvInternal() override;
41
42 // Current block offset.
43 uint32_t block_offset_;
44 // Current AES-CTR counter.
45 std::vector<uint8_t> counter_;
46 // Encrypted counter.
47 std::vector<uint8_t> encrypted_counter_;
48
49 DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
50};
51
52enum CbcPaddingScheme {
53 // Residual block is left unencrypted.
54 kNoPadding,
55 // Residual block is padded with pkcs5 and encrypted.
56 kPkcs5Padding,
57 // Residual block and the next-to-last block are encrypted using ciphertext
58 // stealing method.
59 kCtsPadding,
60};
61
62// Class which implements AES-CBC (Cipher block chaining) encryption.
64 public:
69 explicit AesCbcEncryptor(CbcPaddingScheme padding_scheme);
70
78 AesCbcEncryptor(CbcPaddingScheme padding_scheme,
79 ConstantIvFlag constant_iv_flag);
80
81 ~AesCbcEncryptor() override;
82
85 bool InitializeWithIv(const std::vector<uint8_t>& key,
86 const std::vector<uint8_t>& iv) override;
87
88 size_t RequiredOutputSize(size_t plaintext_size) override;
89
90 private:
91 bool CryptInternal(const uint8_t* plaintext,
92 size_t plaintext_size,
93 uint8_t* ciphertext,
94 size_t* ciphertext_size) override;
95 void SetIvInternal() override;
96 size_t NumPaddingBytes(size_t size) const override;
97
98 void CbcEncryptBlocks(const uint8_t* plaintext,
99 size_t plaintext_size,
100 uint8_t* ciphertext,
101 uint8_t* iv);
102
103 const CbcPaddingScheme padding_scheme_;
104 // 16-byte internal iv for crypto operations.
105 std::vector<uint8_t> internal_iv_;
106
107 DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
108};
109
110} // namespace media
111} // namespace shaka
112
113#endif // PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
const std::vector< uint8_t > & iv() const
Definition aes_cryptor.h:86
bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv) override
All the methods that are virtual are virtual for mocking.