2018-10-06 08:45:09 +00:00
|
|
|
#include <fnd/AesCtrWrappedIFile.h>
|
2018-05-11 09:51:14 +00:00
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
fnd::AesCtrWrappedIFile::AesCtrWrappedIFile(const fnd::SharedPtr<fnd::IFile>& file, const fnd::aes::sAes128Key& key, const fnd::aes::sAesIvCtr& ctr) :
|
2018-05-21 12:48:59 +00:00
|
|
|
mFile(file),
|
|
|
|
mKey(key),
|
2018-05-22 11:45:40 +00:00
|
|
|
mBaseCtr(ctr),
|
|
|
|
mFileOffset(0)
|
2018-05-21 12:48:59 +00:00
|
|
|
{
|
2018-05-27 10:16:42 +00:00
|
|
|
mCache.alloc(kCacheSizeAllocSize);
|
2018-05-21 12:48:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
size_t fnd::AesCtrWrappedIFile::size()
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
2018-09-23 03:29:22 +00:00
|
|
|
return (*mFile)->size();
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
void fnd::AesCtrWrappedIFile::seek(size_t offset)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
2018-05-22 11:45:40 +00:00
|
|
|
mFileOffset = offset;
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
void fnd::AesCtrWrappedIFile::read(byte_t* out, size_t len)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
2018-05-27 10:16:42 +00:00
|
|
|
size_t read_len;
|
|
|
|
size_t read_pos;
|
2018-05-11 09:51:14 +00:00
|
|
|
|
2018-05-27 10:16:42 +00:00
|
|
|
size_t cache_reads = (len / kCacheSize) + ((len % kCacheSize) != 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < cache_reads; i++)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
2018-06-24 15:01:16 +00:00
|
|
|
read_len = _MIN(len - (i * kCacheSize), kCacheSize);
|
2018-05-27 10:16:42 +00:00
|
|
|
read_pos = ((mFileOffset >> 4) << 4) + (i * kCacheSize);
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
(*mFile)->seek(read_pos);
|
|
|
|
(*mFile)->read(mCache.data(), kCacheSizeAllocSize);
|
2018-05-27 10:16:42 +00:00
|
|
|
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::aes::AesIncrementCounter(mBaseCtr.iv, read_pos>>4, mCurrentCtr.iv);
|
|
|
|
fnd::aes::AesCtr(mCache.data(), kCacheSizeAllocSize, mKey.key, mCurrentCtr.iv, mCache.data());
|
2018-05-27 10:16:42 +00:00
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
memcpy(out + (i * kCacheSize), mCache.data() + (mFileOffset & 0xf), read_len);
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|
2018-05-22 11:45:40 +00:00
|
|
|
|
|
|
|
seek(mFileOffset + len);
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
void fnd::AesCtrWrappedIFile::read(byte_t* out, size_t offset, size_t len)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
|
|
|
seek(offset);
|
|
|
|
read(out, len);
|
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
void fnd::AesCtrWrappedIFile::write(const byte_t* in, size_t len)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
2018-05-27 10:16:42 +00:00
|
|
|
size_t write_len;
|
|
|
|
size_t write_pos;
|
|
|
|
|
|
|
|
size_t cache_writes = (len / kCacheSize) + ((len % kCacheSize) != 0);
|
2018-05-22 11:45:40 +00:00
|
|
|
|
2018-05-27 10:16:42 +00:00
|
|
|
for (size_t i = 0; i < cache_writes; i++)
|
|
|
|
{
|
2018-06-24 15:01:16 +00:00
|
|
|
write_len = _MIN(len - (i * kCacheSize), kCacheSize);
|
2018-05-27 10:16:42 +00:00
|
|
|
write_pos = ((mFileOffset >> 4) << 4) + (i * kCacheSize);
|
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
memcpy(mCache.data() + (mFileOffset & 0xf), in + (i * kCacheSize), write_len);
|
2018-05-27 10:16:42 +00:00
|
|
|
|
2018-08-07 08:35:03 +00:00
|
|
|
fnd::aes::AesIncrementCounter(mBaseCtr.iv, write_pos>>4, mCurrentCtr.iv);
|
|
|
|
fnd::aes::AesCtr(mCache.data(), kCacheSizeAllocSize, mKey.key, mCurrentCtr.iv, mCache.data());
|
2018-05-27 10:16:42 +00:00
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
(*mFile)->seek(write_pos);
|
|
|
|
(*mFile)->write(mCache.data(), kCacheSizeAllocSize);
|
2018-05-27 10:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seek(mFileOffset + len);
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
void fnd::AesCtrWrappedIFile::write(const byte_t* in, size_t offset, size_t len)
|
2018-05-11 09:51:14 +00:00
|
|
|
{
|
|
|
|
seek(offset);
|
2018-05-27 10:16:42 +00:00
|
|
|
write(in, len);
|
2018-05-11 09:51:14 +00:00
|
|
|
}
|