nstool/lib/libcrypto/include/crypto/AesCtrStream.h

165 lines
4.3 KiB
C
Raw Normal View History

2017-07-02 15:18:59 +00:00
#pragma once
#include <string>
#include <vector>
#include <fnd/Exception.h>
#include <crypto/aes.h>
2017-07-02 15:18:59 +00:00
namespace crypto
{
namespace aes
2017-07-02 15:18:59 +00:00
{
class AesCtrStream
2017-07-02 15:18:59 +00:00
{
public:
AesCtrStream();
~AesCtrStream();
2017-07-02 15:18:59 +00:00
void seek(size_t offset);
void read(size_t size, uint8_t* out);
void read(size_t offset, size_t size, uint8_t* out);
void write(size_t size, const uint8_t* in);
void write(size_t offset, size_t size, const uint8_t* in);
2017-07-02 15:18:59 +00:00
void AddRegion(size_t start, size_t end, const uint8_t aes_key[kAes128KeySize], const uint8_t aes_ctr[kAesBlockSize]);
2017-07-02 15:18:59 +00:00
protected:
// Virtual methods for implementation of seek/read/write
virtual void seek_internal(size_t offset) = 0;
virtual void read_internal(size_t size, size_t& read_len, uint8_t* out) = 0;
virtual void write_internal(size_t size, size_t& write_len, const uint8_t* in) = 0;
2017-07-02 15:18:59 +00:00
private:
const std::string kModuleName = "AES_CTR_STREAM";
static const size_t kIoBufferLen = 0x10000;
2017-07-02 15:18:59 +00:00
// private implementation of crypto region
class CryptRegion
2017-07-02 15:18:59 +00:00
{
public:
// stubbed constructor
CryptRegion() :
start_(0),
end_(0),
is_plaintext_(true)
{
CleanUp();
}
2017-07-02 15:18:59 +00:00
// plaintext constructor
CryptRegion(size_t start, size_t end) :
start_(start),
end_(end),
is_plaintext_(true)
2017-07-02 15:18:59 +00:00
{
CleanUp();
2017-07-02 15:18:59 +00:00
}
// encrypted constructor
CryptRegion(size_t start, size_t end, const uint8_t aes_key[kAes128KeySize], const uint8_t aes_ctr[kAesBlockSize]) :
start_(start),
end_(end),
is_plaintext_(false)
2017-07-02 15:18:59 +00:00
{
CleanUp();
memcpy(aes_key_, aes_key, kAes128KeySize);
memcpy(ctr_init_, aes_ctr, kAesBlockSize);
memcpy(ctr_, ctr_init_, kAesBlockSize);
2017-07-02 15:18:59 +00:00
}
// destructor
~CryptRegion()
2017-07-02 15:18:59 +00:00
{
CleanUp();
}
2017-07-02 15:18:59 +00:00
size_t start() const { return start_; }
size_t end() const { return end_; }
size_t size() const { return end_ - start_; }
size_t remaining_size(size_t start) const { return end_ - start; }
const uint8_t* aes_key() const { return aes_key_; }
uint8_t* aes_ctr() { return ctr_; }
2017-07-02 15:18:59 +00:00
bool is_in_region(size_t start) const { return start >= start_ && start < end_; }
bool is_in_region(size_t start, size_t end) const { return is_in_region(start) && end > start_ && end <= end_; }
2017-07-02 15:18:59 +00:00
void UpdateAesCtr(size_t start)
{
if (is_in_region(start))
AesIncrementCounter(ctr_init_, ((start - start_) >> 4), ctr_);
2017-07-02 15:18:59 +00:00
}
void GenerateXorpad(size_t start, size_t size, uint8_t* out)
{
// don't operate if requested size exceeds region size
if (is_in_region(start, start + size) == false)
{
return;
}
if (is_plaintext_ == true)
{
memset(out, 0, size);
return;
}
// parameters
size_t block_offset = (start - start_) & 0xf;
size_t block_num = size >> 4;
for (size_t pos = 0; pos < block_num; pos += (kPadBufferLen >> 4))
{
// clear pad buffer
memset(pad_buffer_, 0, kPadBufferCapacity);
// encrypt pad buffer to create xorpad
UpdateAesCtr(start + (pos << 4));
AesCtr(pad_buffer_, kPadBufferCapacity, aes_key(), aes_ctr(), pad_buffer_);
// determine the number of blocks to copy to xorpad
size_t copy_size = kPadBufferLen < ((block_num - pos) << 4) ? kPadBufferLen : ((block_num - pos) << 4);
// copy
memcpy(out + (pos << 4), pad_buffer_ + block_offset, copy_size);
}
}
private:
static const size_t kPadBufferLen = 0x10000;
static const size_t kPadBufferCapacity = kPadBufferLen + kAesBlockSize; // has an extra block to accomodate non block aligned starts
size_t start_;
size_t end_;
bool is_plaintext_;
uint8_t aes_key_[kAes128KeySize];
uint8_t ctr_init_[kAesBlockSize];
uint8_t ctr_[kAesBlockSize];
uint8_t pad_buffer_[kPadBufferCapacity];
void CleanUp()
{
memset(aes_key_, 0, kAes128KeySize);
memset(ctr_init_, 0, kAesBlockSize);
memset(ctr_, 0, kAesBlockSize);
}
};
2017-07-02 15:18:59 +00:00
inline void xor_data(size_t size, const uint8_t* data1, const uint8_t* data2, uint8_t* out)
2017-07-02 15:18:59 +00:00
{
for (size_t idx = 0; idx < size; idx++)
{
out[idx] = data1[idx] ^ data2[idx];
}
2017-07-02 15:18:59 +00:00
}
// Crypto Regions
size_t offset_;
std::vector<CryptRegion> regions_;
2017-07-02 15:18:59 +00:00
// IO Buffer
uint8_t io_buffer_[kIoBufferLen];
uint8_t pad_buffer_[kIoBufferLen];
2017-07-02 15:18:59 +00:00
void GenerateXorPad(size_t start);
};
}
2017-07-02 15:18:59 +00:00
}