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>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
|
|
|
#include "packager/base/stl_util.h"
|
2013-11-12 20:34:58 +00:00
|
|
|
|
|
|
|
struct aes_key_st;
|
|
|
|
typedef struct aes_key_st AES_KEY;
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-12 20:34:58 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
class AesEncryptor {
|
2013-11-12 20:34:58 +00:00
|
|
|
public:
|
2016-03-17 17:03:19 +00:00
|
|
|
AesEncryptor();
|
|
|
|
virtual ~AesEncryptor();
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the encryptor with specified key and a random generated IV
|
2016-03-17 17:03:19 +00:00
|
|
|
/// of the specified size.
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true on successful initialization, false otherwise.
|
2016-03-17 17:03:19 +00:00
|
|
|
virtual bool InitializeWithRandomIv(const std::vector<uint8_t>& key,
|
|
|
|
uint8_t iv_size);
|
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-17 17:03:19 +00:00
|
|
|
virtual bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) = 0;
|
|
|
|
|
|
|
|
virtual size_t NumPaddingBytes(size_t size) = 0;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
/// @name Various forms of encrypt and decrypt calls.
|
2015-11-18 21:11:31 +00:00
|
|
|
/// The plaintext and ciphertext pointers can be the same address.
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @{
|
2016-03-17 17:03:19 +00:00
|
|
|
virtual bool EncryptData(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext) = 0;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Encrypt(const std::vector<uint8_t>& plaintext,
|
2016-03-17 17:03:19 +00:00
|
|
|
std::vector<uint8_t>* ciphertext);
|
|
|
|
|
|
|
|
bool Encrypt(const std::string& plaintext, std::string* ciphertext);
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
/// Update IV for next sample.
|
|
|
|
/// As recommended in ISO/IEC FDIS 23001-7:
|
|
|
|
/// IV need to be updated per sample for CENC.
|
|
|
|
/// IV need not be unique per sample for CBC mode.
|
|
|
|
virtual void UpdateIv() = 0;
|
|
|
|
|
|
|
|
/// Set IV.
|
|
|
|
/// @return true if successful, false if the input is invalid.
|
|
|
|
virtual bool SetIv(const std::vector<uint8_t>& iv) = 0;
|
|
|
|
|
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Initialization vector, with size 8 or 16.
|
|
|
|
std::vector<uint8_t> iv_;
|
|
|
|
// Openssl AES_KEY.
|
|
|
|
scoped_ptr<AES_KEY> aes_key_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesEncryptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class which implements AES-CTR counter-mode encryption/decryption.
|
|
|
|
class AesCtrEncryptor : public AesEncryptor {
|
|
|
|
public:
|
|
|
|
AesCtrEncryptor();
|
|
|
|
~AesCtrEncryptor() override;
|
|
|
|
|
|
|
|
/// @name AesEncryptor implementation overrides.
|
|
|
|
/// @{
|
|
|
|
/// @param key should be 16 bytes in size as specified in CENC spec.
|
|
|
|
/// @param iv_size should be either 8 or 16 as specified in CENC spec.
|
|
|
|
/// @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
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
size_t NumPaddingBytes(size_t size) override;
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool EncryptData(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext) override;
|
2013-11-12 20:34:58 +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:
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
2014-09-09 22:56:02 +00:00
|
|
|
// Class which implements AES-CBC (Cipher block chaining) encryption with
|
|
|
|
// PKCS#5 padding.
|
2016-03-17 17:03:19 +00:00
|
|
|
class AesCbcPkcs5Encryptor : public AesEncryptor {
|
2013-12-17 00:49:56 +00:00
|
|
|
public:
|
2014-09-09 22:56:02 +00:00
|
|
|
AesCbcPkcs5Encryptor();
|
2016-03-17 17:03:19 +00:00
|
|
|
~AesCbcPkcs5Encryptor() override;
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
/// @name AesEncryptor implementation overrides.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
2016-03-17 17:03:19 +00:00
|
|
|
const std::vector<uint8_t>& iv) override;
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
size_t NumPaddingBytes(size_t size) override;
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool EncryptData(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext) override;
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
void UpdateIv() override;
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv) override;
|
|
|
|
/// @}
|
2013-12-17 00:49:56 +00:00
|
|
|
|
|
|
|
private:
|
2016-03-17 17:03:19 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Encryptor);
|
2014-09-09 22:56:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Class which implements AES-CBC (Cipher block chaining) encryption with
|
|
|
|
// Ciphertext stealing.
|
2016-03-17 17:03:19 +00:00
|
|
|
class AesCbcCtsEncryptor : public AesEncryptor {
|
2014-09-09 22:56:02 +00:00
|
|
|
public:
|
|
|
|
AesCbcCtsEncryptor();
|
2016-03-17 17:03:19 +00:00
|
|
|
~AesCbcCtsEncryptor() override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
/// @name AesEncryptor implementation overrides.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
2016-03-17 17:03:19 +00:00
|
|
|
const std::vector<uint8_t>& iv) override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
size_t NumPaddingBytes(size_t size) override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
bool EncryptData(const uint8_t* plaintext,
|
|
|
|
size_t plaintext_size,
|
|
|
|
uint8_t* ciphertext) override;
|
2014-09-09 22:56:02 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
2016-03-17 17:03:19 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcCtsEncryptor);
|
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_
|