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 {
|
|
|
|
|
2014-09-09 22:56:02 +00:00
|
|
|
// Class which implements AES-CTR counter-mode encryption/decryption.
|
2013-11-12 20:34:58 +00:00
|
|
|
class AesCtrEncryptor {
|
|
|
|
public:
|
|
|
|
AesCtrEncryptor();
|
|
|
|
~AesCtrEncryptor();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the encryptor with specified key and a random generated IV
|
|
|
|
/// of the specified size. block_offset() is reset to 0 on success.
|
|
|
|
/// @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.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithRandomIv(const std::vector<uint8_t>& key, uint8_t iv_size);
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the encryptor with specified key and IV. block_offset() is
|
|
|
|
/// reset to 0 on success.
|
|
|
|
/// @param key should be 16 bytes in size as specified in CENC spec.
|
|
|
|
/// @param iv should be 8 bytes or 16 bytes in size as specified in CENC spec.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv);
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @name Various forms of encrypt calls.
|
|
|
|
/// block_offset() will be updated according to input plaintext size.
|
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
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Encrypt(const uint8_t* plaintext,
|
2013-11-12 20:34:58 +00:00
|
|
|
size_t plaintext_size,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t* ciphertext);
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Encrypt(const std::vector<uint8_t>& plaintext,
|
|
|
|
std::vector<uint8_t>* ciphertext) {
|
2013-11-12 20:34:58 +00:00
|
|
|
ciphertext->resize(plaintext.size());
|
|
|
|
return Encrypt(&plaintext[0], plaintext.size(), &(*ciphertext)[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Encrypt(const std::string& plaintext, std::string* ciphertext) {
|
|
|
|
ciphertext->resize(plaintext.size());
|
2014-09-30 21:52:21 +00:00
|
|
|
return Encrypt(reinterpret_cast<const uint8_t*>(plaintext.data()),
|
2013-11-12 20:34:58 +00:00
|
|
|
plaintext.size(),
|
2014-09-30 21:52:21 +00:00
|
|
|
reinterpret_cast<uint8_t*>(string_as_array(ciphertext)));
|
2013-11-12 20:34:58 +00:00
|
|
|
}
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-11-12 20:34:58 +00:00
|
|
|
|
|
|
|
// For AES CTR, encryption and decryption are identical.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Decrypt(const uint8_t* ciphertext,
|
2013-11-12 20:34:58 +00:00
|
|
|
size_t ciphertext_size,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t* plaintext) {
|
2013-11-12 20:34:58 +00:00
|
|
|
return Encrypt(ciphertext, ciphertext_size, plaintext);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Decrypt(const std::vector<uint8_t>& ciphertext,
|
|
|
|
std::vector<uint8_t>* plaintext) {
|
2013-11-12 20:34:58 +00:00
|
|
|
return Encrypt(ciphertext, plaintext);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Decrypt(const std::string& ciphertext, std::string* plaintext) {
|
|
|
|
return Encrypt(ciphertext, plaintext);
|
|
|
|
}
|
|
|
|
|
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.
|
2013-11-12 20:34:58 +00:00
|
|
|
void UpdateIv();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Set IV. @a block_offset_ is reset to 0 on success.
|
|
|
|
/// @return true if successful, false if the input is invalid.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2013-11-12 20:34:58 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
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:
|
|
|
|
// Initialization vector, with size 8 or 16.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv_;
|
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
|
|
|
// Openssl AES_KEY.
|
|
|
|
scoped_ptr<AES_KEY> aes_key_;
|
|
|
|
// 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.
|
|
|
|
class AesCbcPkcs5Encryptor {
|
2013-12-17 00:49:56 +00:00
|
|
|
public:
|
2014-09-09 22:56:02 +00:00
|
|
|
AesCbcPkcs5Encryptor();
|
|
|
|
~AesCbcPkcs5Encryptor();
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the encryptor with specified key and IV.
|
|
|
|
/// @param key should be 128 bits or 192 bits or 256 bits in size as defined
|
|
|
|
/// in AES spec.
|
|
|
|
/// @param iv should be 16 bytes in size.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv);
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param plaintext will be PKCS5 padded before being encrypted.
|
|
|
|
/// @param ciphertext should not be NULL.
|
2013-12-17 00:49:56 +00:00
|
|
|
void Encrypt(const std::string& plaintext, std::string* ciphertext);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true if successful, false if the input is invalid.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
2013-12-17 00:49:56 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv_;
|
2013-12-17 00:49:56 +00:00
|
|
|
scoped_ptr<AES_KEY> encrypt_key_;
|
|
|
|
|
2014-09-09 22:56:02 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Encryptor);
|
2013-12-17 00:49:56 +00:00
|
|
|
};
|
|
|
|
|
2014-09-09 22:56:02 +00:00
|
|
|
// Class which implements AES-CBC (Cipher block chaining) decryption with
|
|
|
|
// PKCS#5 padding.
|
|
|
|
class AesCbcPkcs5Decryptor {
|
2013-12-17 00:49:56 +00:00
|
|
|
public:
|
2014-09-09 22:56:02 +00:00
|
|
|
AesCbcPkcs5Decryptor();
|
|
|
|
~AesCbcPkcs5Decryptor();
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the decryptor with specified key and IV.
|
|
|
|
/// @param key should be 128 bits or 192 bits or 256 bits in size as defined
|
|
|
|
/// in AES spec.
|
|
|
|
/// @param iv should be 16 bytes in size.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv);
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param ciphertext is expected to be padded with PKCS5 padding.
|
|
|
|
/// @param plaintext should not be NULL.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-12-17 00:49:56 +00:00
|
|
|
bool Decrypt(const std::string& ciphertext, std::string* plaintext);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true if successful, false if the input is invalid.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2013-12-17 00:49:56 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
2013-12-17 00:49:56 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv_;
|
2013-12-17 00:49:56 +00:00
|
|
|
scoped_ptr<AES_KEY> decrypt_key_;
|
|
|
|
|
2014-09-09 22:56:02 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcPkcs5Decryptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class which implements AES-CBC (Cipher block chaining) encryption with
|
|
|
|
// Ciphertext stealing.
|
|
|
|
class AesCbcCtsEncryptor {
|
|
|
|
public:
|
|
|
|
AesCbcCtsEncryptor();
|
|
|
|
~AesCbcCtsEncryptor();
|
|
|
|
|
|
|
|
/// Initialize the encryptor with specified key and IV.
|
|
|
|
/// @param key should be 128 bits or 192 bits or 256 bits in size as defined
|
|
|
|
/// in AES spec.
|
|
|
|
/// @param iv should be 16 bytes in size.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @param plaintext points to the data to be encrypted.
|
|
|
|
/// @param size is the number of bytes to be encrypted. If less than 16
|
|
|
|
/// bytes, it will be copied in the clear.
|
|
|
|
/// @param ciphertext should not be NULL. The buffer should be at least
|
|
|
|
/// @a size bytes in length.
|
2014-09-30 21:52:21 +00:00
|
|
|
void Encrypt(const uint8_t* plaintext, size_t size, uint8_t* ciphertext);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @param plaintext contains the data to be encrypted. If less than 16
|
|
|
|
/// bytes in size, it will be copied in the clear.
|
|
|
|
/// @param ciphertext should not be NULL. Caller retains ownership.
|
2014-09-30 21:52:21 +00:00
|
|
|
void Encrypt(const std::vector<uint8_t>& plaintext,
|
|
|
|
std::vector<uint8_t>* ciphertext);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @param iv is the initialization vector. Should be 16 bytes in size.
|
|
|
|
/// @return true if successful, false if the input is invalid.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv_;
|
2014-09-09 22:56:02 +00:00
|
|
|
scoped_ptr<AES_KEY> encrypt_key_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcCtsEncryptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class which implements AES-CBC (Cipher block chaining) decryption with
|
|
|
|
// Ciphertext stealing.
|
|
|
|
class AesCbcCtsDecryptor {
|
|
|
|
public:
|
|
|
|
AesCbcCtsDecryptor();
|
|
|
|
~AesCbcCtsDecryptor();
|
|
|
|
|
|
|
|
/// Initialize the decryptor with specified key and IV.
|
|
|
|
/// @param key should be 128 bits or 192 bits or 256 bits in size as defined
|
|
|
|
/// in AES spec.
|
|
|
|
/// @param iv should be 16 bytes in size.
|
|
|
|
/// @return true on successful initialization, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @param ciphertext points to the data to be decrypted.
|
|
|
|
/// @param size is the number of bytes to be decrypted. If less than 16
|
|
|
|
/// bytes, it will be copied in the clear.
|
|
|
|
/// @param plaintext should not be NULL. The buffer should be at least
|
|
|
|
/// @a size bytes in length.
|
2014-09-30 21:52:21 +00:00
|
|
|
void Decrypt(const uint8_t* ciphertext, size_t size, uint8_t* plaintext);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @param ciphertext contains the data to be decrypted. If less than 16
|
|
|
|
/// bytes in size, it will be copied in the clear.
|
|
|
|
/// @param plaintext should not be NULL. Caller retains ownership.
|
2014-09-30 21:52:21 +00:00
|
|
|
void Decrypt(const std::vector<uint8_t>& ciphertext,
|
|
|
|
std::vector<uint8_t>* plaintext);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
/// @return true if successful, false if the input is invalid.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool SetIv(const std::vector<uint8_t>& iv);
|
2014-09-09 22:56:02 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const std::vector<uint8_t>& iv() const { return iv_; }
|
2014-09-09 22:56:02 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> iv_;
|
2014-09-09 22:56:02 +00:00
|
|
|
scoped_ptr<AES_KEY> decrypt_key_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AesCbcCtsDecryptor);
|
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_
|