2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-04-06 00:26:20 +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
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <packager/media/base/aes_cryptor.h>
|
2016-04-06 00:26:20 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-04-06 00:26:20 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
/// Implements pattern-based encryption/decryption.
|
|
|
|
class AesPatternCryptor : public AesCryptor {
|
|
|
|
public:
|
2016-04-27 07:51:51 +00:00
|
|
|
/// Enumerator for controling encrytion/decryption mode for the last
|
|
|
|
/// encryption/decrytion block(s).
|
|
|
|
enum PatternEncryptionMode {
|
2018-08-28 23:28:26 +00:00
|
|
|
/// Use kEncryptIfCryptByteBlockRemaining to encrypt all the full 16-byte
|
|
|
|
/// blocks even if the remaining bytes are less than encryption_block_bytes.
|
|
|
|
/// IOW:
|
|
|
|
/// if (remaining_bytes <= encryption_block_bytes)
|
|
|
|
/// encrypt(block_aligned_remaining_data)
|
2016-04-27 07:51:51 +00:00
|
|
|
kEncryptIfCryptByteBlockRemaining,
|
2018-08-28 23:28:26 +00:00
|
|
|
/// Use kSkipIfCryptByteBlockRemaining to not encrypt/decrypt the remaining
|
|
|
|
/// bytes if it is less than or equal to encryption_block_bytes.
|
|
|
|
/// if (remaining_bytes > encryption_block_bytes)
|
2016-04-27 07:51:51 +00:00
|
|
|
/// encrypt().
|
|
|
|
/// Use this mode for HLS SAMPLE-AES.
|
|
|
|
kSkipIfCryptByteBlockRemaining,
|
|
|
|
};
|
|
|
|
|
2016-04-06 00:26:20 +00:00
|
|
|
/// @param crypt_byte_block indicates number of encrypted blocks (16-byte) in
|
|
|
|
/// pattern based encryption.
|
|
|
|
/// @param skip_byte_block indicates number of unencrypted blocks (16-byte)
|
|
|
|
/// in pattern based encryption.
|
2016-04-27 07:51:51 +00:00
|
|
|
/// @param encryption_mode is used to determine the behavior for the last
|
|
|
|
/// block.
|
2016-04-06 00:26:20 +00:00
|
|
|
/// @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 can be incremented
|
|
|
|
/// (for counter mode) or chained (for cipher block chaining mode)
|
|
|
|
/// internally inside Crypt call, i.e. iv will be updated across Crypt
|
|
|
|
/// calls.
|
|
|
|
/// @param cryptor points to an AesCryptor instance which performs the actual
|
2016-05-26 22:44:37 +00:00
|
|
|
/// encryption/decryption. Note that @a cryptor shall not use constant
|
|
|
|
/// iv.
|
2016-04-06 00:26:20 +00:00
|
|
|
AesPatternCryptor(uint8_t crypt_byte_block,
|
|
|
|
uint8_t skip_byte_block,
|
2016-04-27 07:51:51 +00:00
|
|
|
PatternEncryptionMode encryption_mode,
|
2016-04-06 00:26:20 +00:00
|
|
|
ConstantIvFlag constant_iv_flag,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<AesCryptor> cryptor);
|
2016-04-06 00:26:20 +00:00
|
|
|
~AesPatternCryptor() override;
|
|
|
|
|
|
|
|
/// @name AesCryptor implementation overrides.
|
|
|
|
/// @{
|
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) override;
|
|
|
|
/// @}
|
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
private:
|
2016-04-06 00:26:20 +00:00
|
|
|
bool CryptInternal(const uint8_t* text,
|
|
|
|
size_t text_size,
|
|
|
|
uint8_t* crypt_text,
|
|
|
|
size_t* crypt_text_size) override;
|
2016-04-13 17:52:41 +00:00
|
|
|
void SetIvInternal() override;
|
2016-04-06 00:26:20 +00:00
|
|
|
|
2016-10-13 21:29:15 +00:00
|
|
|
uint8_t crypt_byte_block_;
|
2016-04-06 00:26:20 +00:00
|
|
|
const uint8_t skip_byte_block_;
|
2016-04-27 07:51:51 +00:00
|
|
|
const PatternEncryptionMode encryption_mode_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<AesCryptor> cryptor_;
|
2016-04-06 00:26:20 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesPatternCryptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|