2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-12 20:34:58 +00:00
|
|
|
//
|
2022-11-02 15:34:06 +00:00
|
|
|
// AES Encryptor implementation using mbedtls.
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
|
2013-11-12 20:34:58 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/aes_cryptor.h>
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
// Class which implements AES-CTR counter-mode encryption.
|
2022-11-02 15:34:06 +00:00
|
|
|
class AesCtrEncryptor : public AesCryptor {
|
2016-03-17 17:03:19 +00:00
|
|
|
public:
|
|
|
|
AesCtrEncryptor();
|
|
|
|
~AesCtrEncryptor() override;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t block_offset() const { return block_offset_; }
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
/// Initialize the encryptor with specified key and IV.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) override;
|
|
|
|
|
2013-11-12 20:34:58 +00:00
|
|
|
private:
|
2016-04-06 00:19:16 +00:00
|
|
|
bool CryptInternal(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext,
|
|
|
|
size_t* ciphertext_size) override;
|
2016-04-13 17:52:41 +00:00
|
|
|
void SetIvInternal() override;
|
2016-04-06 00:19:16 +00:00
|
|
|
|
2013-11-12 20:34:58 +00:00
|
|
|
// Current block offset.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t block_offset_;
|
2013-11-12 20:34:58 +00:00
|
|
|
// Current AES-CTR counter.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> counter_;
|
2013-11-12 20:34:58 +00:00
|
|
|
// Encrypted counter.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> encrypted_counter_;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
|
|
|
|
};
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
enum CbcPaddingScheme {
|
2016-04-13 23:43:55 +00:00
|
|
|
// Residual block is left unencrypted.
|
2016-03-25 18:02:43 +00:00
|
|
|
kNoPadding,
|
2016-04-13 23:43:55 +00:00
|
|
|
// Residual block is padded with pkcs5 and encrypted.
|
2016-03-25 18:02:43 +00:00
|
|
|
kPkcs5Padding,
|
2016-04-13 23:43:55 +00:00
|
|
|
// Residual block and the next-to-last block are encrypted using ciphertext
|
|
|
|
// stealing method.
|
2016-03-25 18:02:43 +00:00
|
|
|
kCtsPadding,
|
2014-09-09 22:56:02 +00:00
|
|
|
};
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
// Class which implements AES-CBC (Cipher block chaining) encryption.
|
2022-11-02 15:34:06 +00:00
|
|
|
class AesCbcEncryptor : public AesCryptor {
|
2014-09-09 22:56:02 +00:00
|
|
|
public:
|
2016-04-13 17:52:41 +00:00
|
|
|
/// Creates a AesCbcEncryptor with continous cipher block chain across Crypt
|
|
|
|
/// calls, i.e. AesCbcEncryptor(padding_scheme, kDontUseConstantIv).
|
2016-03-25 18:02:43 +00:00
|
|
|
/// @param padding_scheme indicates the padding scheme used. Currently
|
|
|
|
/// supported schemes: kNoPadding, kPkcs5Padding, kCtsPadding.
|
2016-04-13 17:52:41 +00:00
|
|
|
explicit AesCbcEncryptor(CbcPaddingScheme padding_scheme);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
/// @param padding_scheme indicates the padding scheme used. Currently
|
|
|
|
/// supported schemes: kNoPadding, kPkcs5Padding, kCtsPadding.
|
|
|
|
/// @param constant_iv_flag indicates whether a constant iv is used,
|
|
|
|
/// kUseConstantIv means that the same iv is used for all Crypt calls
|
|
|
|
/// until iv is changed via SetIv; otherwise, iv is updated internally
|
|
|
|
/// and there is a continuous cipher block chain across Crypt calls
|
|
|
|
/// util iv is changed explicitly via SetIv or UpdateIv functions.
|
|
|
|
AesCbcEncryptor(CbcPaddingScheme padding_scheme,
|
|
|
|
ConstantIvFlag constant_iv_flag);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
~AesCbcEncryptor() override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
/// Initialize the encryptor with specified key and IV.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) override;
|
|
|
|
|
2023-08-31 23:59:46 +00:00
|
|
|
size_t RequiredOutputSize(size_t plaintext_size) override;
|
2023-07-14 15:19:01 +00:00
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
private:
|
|
|
|
bool CryptInternal(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext,
|
|
|
|
size_t* ciphertext_size) override;
|
2016-04-13 17:52:41 +00:00
|
|
|
void SetIvInternal() override;
|
2016-03-25 18:02:43 +00:00
|
|
|
size_t NumPaddingBytes(size_t size) const override;
|
|
|
|
|
2022-11-02 15:34:06 +00:00
|
|
|
void CbcEncryptBlocks(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext);
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
const CbcPaddingScheme padding_scheme_;
|
2016-04-13 17:52:41 +00:00
|
|
|
// 16-byte internal iv for crypto operations.
|
|
|
|
std::vector<uint8_t> internal_iv_;
|
2016-03-25 18:02:43 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcEncryptor);
|
2013-12-17 00:49:56 +00:00
|
|
|
};
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-01-07 18:32:23 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_AES_ENCRYPTOR_H_
|