2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 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
|
2013-11-12 20:34:58 +00:00
|
|
|
//
|
|
|
|
// AES Encryptor implementation using openssl.
|
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_AES_ENCRYPTOR_H_
|
|
|
|
#define MEDIA_BASE_AES_ENCRYPTOR_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
#include "packager/base/macros.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
2016-04-06 00:19:16 +00:00
|
|
|
#include "packager/media/base/aes_cryptor.h"
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
class AesEncryptor : public AesCryptor {
|
2013-11-12 20:34:58 +00:00
|
|
|
public:
|
2016-03-17 17:03:19 +00:00
|
|
|
AesEncryptor();
|
2016-04-06 00:19:16 +00:00
|
|
|
~AesEncryptor() override;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
/// Initialize the encryptor with specified key and IV.
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true on successful initialization, false otherwise.
|
2016-03-25 18:02:43 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
2016-04-06 00:19:16 +00:00
|
|
|
const std::vector<uint8_t>& iv) override;
|
2016-03-25 18:02:43 +00:00
|
|
|
|
|
|
|
private:
|
2016-03-17 17:03:19 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
|
|
|
|
};
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
// Class which implements AES-CTR counter-mode encryption.
|
2016-03-17 17:03:19 +00:00
|
|
|
class AesCtrEncryptor : public AesEncryptor {
|
|
|
|
public:
|
|
|
|
AesCtrEncryptor();
|
|
|
|
~AesCtrEncryptor() override;
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
/// @name AesCryptor implementation overrides.
|
2016-03-17 17:03:19 +00:00
|
|
|
/// @{
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Update IV for next sample. @a block_offset_ is reset to 0.
|
|
|
|
/// As recommended in ISO/IEC FDIS 23001-7: CENC spec,
|
|
|
|
/// For 64-bit IV size, new_iv = old_iv + 1;
|
|
|
|
/// For 128-bit IV size, new_iv = old_iv + previous_sample_block_count.
|
2016-03-17 17:03:19 +00:00
|
|
|
void UpdateIv() override;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv) override;
|
|
|
|
/// @}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t block_offset() const { return block_offset_; }
|
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;
|
|
|
|
|
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
|
|
|
// Keep track of whether the counter has overflowed.
|
|
|
|
bool counter_overflow_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCtrEncryptor);
|
|
|
|
};
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
enum CbcPaddingScheme {
|
|
|
|
kNoPadding,
|
|
|
|
kPkcs5Padding,
|
|
|
|
kCtsPadding,
|
2014-09-09 22:56:02 +00:00
|
|
|
};
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
const bool kChainAcrossCalls = true;
|
|
|
|
|
|
|
|
// Class which implements AES-CBC (Cipher block chaining) encryption.
|
|
|
|
class AesCbcEncryptor : public AesEncryptor {
|
2014-09-09 22:56:02 +00:00
|
|
|
public:
|
2016-03-25 18:02:43 +00:00
|
|
|
/// @param padding_scheme indicates the padding scheme used. Currently
|
|
|
|
/// supported schemes: kNoPadding, kPkcs5Padding, kCtsPadding.
|
|
|
|
/// @param chain_across_calls indicates whether there is a continuous cipher
|
|
|
|
/// block chain across calls for Encrypt function. If it is false, iv
|
|
|
|
/// is not updated across Encrypt function calls.
|
|
|
|
AesCbcEncryptor(CbcPaddingScheme padding_scheme, bool chain_across_calls);
|
|
|
|
~AesCbcEncryptor() override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
/// @name AesCryptor implementation overrides.
|
2016-03-17 17:03:19 +00:00
|
|
|
/// @{
|
|
|
|
void UpdateIv() override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv) override;
|
|
|
|
/// @}
|
2014-09-09 22:56:02 +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-03-25 18:02:43 +00:00
|
|
|
size_t NumPaddingBytes(size_t size) const override;
|
|
|
|
|
|
|
|
const CbcPaddingScheme padding_scheme_;
|
|
|
|
const bool chain_across_calls_;
|
|
|
|
|
|
|
|
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
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-11-12 20:34:58 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_AES_ENCRYPTOR_H_
|