2016-04-06 00:26:20 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "packager/media/base/aes_cryptor.h"
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-04-06 00:26:20 +00:00
|
|
|
#include "packager/base/macros.h"
|
|
|
|
|
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 {
|
|
|
|
/// Use kEncryptIfCryptByteBlockRemaining if the last blocks is exactly the
|
|
|
|
/// same as the number of remaining bytes. IOW
|
|
|
|
/// if (remaining_bytes == encryption_block_bytes)
|
|
|
|
/// encrypt(remaining_data)
|
|
|
|
kEncryptIfCryptByteBlockRemaining,
|
|
|
|
/// Use kSkipIfCryptByteBlockRemaining to not encrypt/decrypt the last
|
|
|
|
/// clocks if it is exactly the same as the number of remaining bytes.
|
|
|
|
/// if (remaining_bytes > encryption_block_bytes) {
|
|
|
|
/// // Since this is the last blocks, this is effectively the same
|
|
|
|
/// // condition as remaining_bytes != encryption_block_bytes.
|
|
|
|
/// 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-04-27 07:51:51 +00:00
|
|
|
bool NeedEncrypt(size_t input_size, size_t target_data_size);
|
|
|
|
|
2016-04-06 00:26:20 +00:00
|
|
|
const uint8_t crypt_byte_block_;
|
|
|
|
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
|