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