Skip to content

Commit

Permalink
Ver 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
miyabe committed Jan 29, 2023
1 parent 904f62b commit 97fb9e5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 95 deletions.
23 changes: 12 additions & 11 deletions EncFSy_lib/EncFSFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ namespace EncFS {
//printf("read %d %d %d %d\n", fileIv, this->lastBlockNum, off, len);

// Calculate block position.
size_t blockSize = encfs.getBlockSize();
size_t blockHeaderSize = encfs.getHeaderSize();
size_t blockDataSize = blockSize - blockHeaderSize;
const size_t blockSize = encfs.getBlockSize();
const size_t blockHeaderSize = encfs.getHeaderSize();
const size_t blockDataSize = blockSize - blockHeaderSize;
size_t shift = off % blockDataSize;
size_t blockNum = off / blockDataSize;
size_t lastBlockNum = (off + len - 1) / blockDataSize;
const size_t lastBlockNum = (off + len - 1) / blockDataSize;

int32_t copiedLen = 0;
// Copy from buffer.
Expand All @@ -99,7 +99,7 @@ namespace EncFS {
}

size_t blocksOffset = blockNum * blockSize;
size_t blocksLength = (lastBlockNum + 1) * blockSize - blocksOffset;
const size_t blocksLength = (lastBlockNum + 1) * blockSize - blocksOffset;
if (encfs.isUniqueIV()) {
blocksOffset += EncFS::EncFSVolume::HEADER_SIZE;
}
Expand Down Expand Up @@ -158,7 +158,6 @@ namespace EncFS {
SetLastError(ERROR_FILE_CORRUPT);
return -1;
}
// wprintf(L"Write %s %ld %ld\n", FileName, off, len);

if (off > fileSize) {
// Expand file.
Expand All @@ -169,17 +168,19 @@ namespace EncFS {
}

// Calculate position.
size_t blockSize = encfs.getBlockSize();
size_t blockHeaderSize = encfs.getHeaderSize();
size_t blockDataSize = blockSize - blockHeaderSize;
const size_t blockSize = encfs.getBlockSize();
const size_t blockHeaderSize = encfs.getHeaderSize();
const size_t blockDataSize = blockSize - blockHeaderSize;
size_t shift = off % blockDataSize;
size_t blockNum = off / blockDataSize;
size_t lastBlockNum = (off + len - 1) / blockDataSize;
const size_t lastBlockNum = (off + len - 1) / blockDataSize;
size_t blocksOffset = blockNum * blockSize;
size_t blocksLength = (lastBlockNum + 1) * blockSize - blocksOffset;
const size_t blocksLength = (lastBlockNum + 1) * blockSize - blocksOffset;
if (encfs.isUniqueIV()) {
blocksOffset += EncFS::EncFSVolume::HEADER_SIZE;
}
// wprintf(L"Write %s off=%ld len=%ld blockDataSize=%ld shift=%ld blockNum=%ld lastBlockNum=%ld blocksOffset=%ld blocksLength=%ld\n",
// FileName, off, len, blockDataSize, shift, blockNum, lastBlockNum, blocksOffset, blocksLength);

// Seek for write,
LARGE_INTEGER distanceToMove;
Expand Down
17 changes: 7 additions & 10 deletions EncFSy_lib/EncFSVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
}

// getPaddedDecFilename
size_t padBytesSize = 16;
const size_t padBytesSize = 16;
size_t padLen = padBytesSize - (plainFileName.size() % padBytesSize);
if (padLen == 0) {
padLen = padBytesSize;
Expand Down Expand Up @@ -450,7 +450,7 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
}
}

size_t padLen = plainFileName[plainFileName.size() - 1];
const size_t padLen = plainFileName[plainFileName.size() - 1];
for (size_t i = 0; i < padLen; ++i) {
if (plainFileName[plainFileName.size() - padLen + i] != padLen) {
throw EncFSInvalidBlockException();
Expand Down Expand Up @@ -517,7 +517,7 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

int64_t EncFSVolume::toDecodedLength(const int64_t encodedLength) {
int64_t size = encodedLength;
int64_t headerSize = this->getHeaderSize();
const int64_t headerSize = this->getHeaderSize();
if (size < (this->uniqueIV ? HEADER_SIZE : 0) + headerSize) {
return 0;
}
Expand Down Expand Up @@ -595,9 +595,8 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>


void EncFSVolume::codeBlock(const int64_t fileIv, const int64_t blockNum, const bool encode, const string &srcBlock, string &destBlock) {
int64_t iv = blockNum ^ fileIv;

size_t headerSize = this->getHeaderSize();
const int64_t iv = blockNum ^ fileIv;
const size_t headerSize = this->getHeaderSize();

if (encode) {
// 暗号化
Expand Down Expand Up @@ -628,12 +627,11 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
}

string blockIv;
longToBytesByBE(blockIv, iv);
if (block.size() == this->blockSize) {
longToBytesByBE(blockIv, iv);
blockCipher(this->volumeHmac, this->hmacLock, this->volumeKey, this->volumeIv, blockIv, this->aesCbcEnc, this->aesCbcEncLock, block, destBlock);
}
else {
longToBytesByBE(blockIv, iv);
streamEncrypt(this->volumeHmac, this->hmacLock, this->volumeKey, this->volumeIv, blockIv, this->aesCfbEnc, this->aesCfbEncLock, block, destBlock);
}
}
Expand All @@ -653,12 +651,11 @@ R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
}
}
string blockIv;
longToBytesByBE(blockIv, iv);
if (srcBlock.size() == this->blockSize) {
longToBytesByBE(blockIv, iv);
blockCipher(this->volumeHmac, this->hmacLock, this->volumeKey, this->volumeIv, blockIv, this->aesCbcDec, this->aesCbcDecLock, srcBlock, destBlock);
}
else {
longToBytesByBE(blockIv, iv);
streamDecrypt(this->volumeHmac, this->hmacLock, this->volumeKey, this->volumeIv, blockIv, this->aesCfbDec, this->aesCfbDecLock, srcBlock, destBlock);
}

Expand Down
Loading

0 comments on commit 97fb9e5

Please sign in to comment.