Shaka Packager SDK
Loading...
Searching...
No Matches
aes_cryptor.h
1// Copyright 2016 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#ifndef PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
8#define PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
9
10#include <cstdint>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include <mbedtls/cipher.h>
16
17#include <packager/macros/classes.h>
18#include <packager/media/base/fourccs.h>
19
20namespace shaka {
21namespace media {
22
23// AES cryptor interface. Inherited by various AES encryptor and decryptor
24// implementations.
26 public:
27 enum ConstantIvFlag {
28 kUseConstantIv,
29 kDontUseConstantIv,
30 };
31
38 explicit AesCryptor(ConstantIvFlag constant_iv_flag);
39 virtual ~AesCryptor();
40
43 virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
44 const std::vector<uint8_t>& iv) = 0;
45
46 virtual size_t RequiredOutputSize(size_t plaintext_size) {
47 return plaintext_size;
48 }
49
55 bool Crypt(const std::vector<uint8_t>& text,
56 std::vector<uint8_t>* crypt_text);
57 bool Crypt(const std::string& text, std::string* crypt_text);
59 bool Crypt(const uint8_t* text, size_t text_size, uint8_t* crypt_text) {
60 size_t crypt_text_size = text_size;
61 return Crypt(text, text_size, crypt_text, &crypt_text_size);
62 }
63 bool Crypt(const uint8_t* text,
64 size_t text_size,
65 uint8_t* crypt_text,
66 size_t* crypt_text_size) {
67 if (constant_iv_flag_ == kUseConstantIv)
68 SetIvInternal();
69 else
70 num_crypt_bytes_ += text_size;
71 return CryptInternal(text, text_size, crypt_text, crypt_text_size);
72 }
74
78 bool SetIv(const std::vector<uint8_t>& iv);
79
83 void UpdateIv();
84
86 const std::vector<uint8_t>& iv() const { return iv_; }
87
89 bool use_constant_iv() const { return constant_iv_flag_ == kUseConstantIv; }
90
95 static bool GenerateRandomIv(FourCC protection_scheme,
96 std::vector<uint8_t>* iv);
97
98 protected:
99 enum CipherMode {
100 kCtrMode,
101 kCbcMode,
102 };
103
104 // mbedTLS cipher context.
105 mbedtls_cipher_context_t cipher_ctx_;
106
107 bool SetupCipher(size_t key_size, CipherMode mode);
108
109 private:
110 // Internal implementation of crypt function.
111 // |text| points to the input text.
112 // |text_size| is the size of input text.
113 // |crypt_text| points to the output encrypted or decrypted text, depends on
114 // whether it is an encryption or decryption. |text| and |crypt_text| can
115 // point to the same address for in place encryption/decryption.
116 // |crypt_text_size| contains the size of |crypt_text| and it will be updated
117 // to contain the actual encrypted/decrypted size for |crypt_text| on success.
118 // Return false if the input |crypt_text_size| is not large enough to hold the
119 // output |crypt_text| or if there is any error in encryption/decryption.
120 virtual bool CryptInternal(const uint8_t* text,
121 size_t text_size,
122 uint8_t* crypt_text,
123 size_t* crypt_text_size) = 0;
124
125 // Internal implementation of SetIv, which setup internal iv.
126 virtual void SetIvInternal() = 0;
127
128 // |size| specifies the input text size.
129 // Return the number of padding bytes needed.
130 // Note: No paddings should be needed except for pkcs5-cbc encryptor.
131 virtual size_t NumPaddingBytes(size_t size) const;
132
133 // Indicates whether a constant iv is used. Internal iv will be reset to
134 // |iv_| before calling Crypt if that is the case.
135 const ConstantIvFlag constant_iv_flag_;
136 // Initialization vector from by SetIv or InitializeWithIv, with size 8 or 16
137 // bytes.
138 std::vector<uint8_t> iv_;
139 // Tracks number of crypt bytes. It is used to calculate how many blocks
140 // should iv advance in UpdateIv(). It will be reset to 0 after iv is updated.
141 size_t num_crypt_bytes_;
142
143 DISALLOW_COPY_AND_ASSIGN(AesCryptor);
144};
145
146} // namespace media
147} // namespace shaka
148
149#endif // PACKAGER_MEDIA_BASE_AES_CRYPTOR_H_
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
virtual bool InitializeWithIv(const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv)=0
bool SetIv(const std::vector< uint8_t > &iv)
const std::vector< uint8_t > & iv() const
Definition aes_cryptor.h:86
bool use_constant_iv() const
Definition aes_cryptor.h:89
bool Crypt(const uint8_t *text, size_t text_size, uint8_t *crypt_text)
Definition aes_cryptor.h:59
All the methods that are virtual are virtual for mocking.