diff --git a/trunk/ConsoleDataMigration/sources/tools/ExportedDataDecrypter/main.cpp b/trunk/ConsoleDataMigration/sources/tools/ExportedDataDecrypter/main.cpp index 3e7c6da..d07c99f 100644 --- a/trunk/ConsoleDataMigration/sources/tools/ExportedDataDecrypter/main.cpp +++ b/trunk/ConsoleDataMigration/sources/tools/ExportedDataDecrypter/main.cpp @@ -376,24 +376,49 @@ bool DecryptDirectory(const wchar_t * from_path, const wchar_t* to_path, void* b else { bool readDone = false;; - // CMACまで読んだかどうか + + // 復号化 + result = swAesCtrContext.Decrypt(reinterpret_cast(buf) + bufSize / 2, buf, readSize); + COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + + // SHA256Hash確認のためCMACまで読んだかどうか調べる + s32 readSizeforCmac = readSize; if (sdFileSize - nn::crypto::AES_CMAC_MAC_SIZE < totalReadSize) { - readSize -= totalReadSize - (sdFileSize - nn::crypto::AES_CMAC_MAC_SIZE); + // 最大CMACぶんのサイズを減らす + readSizeforCmac -= totalReadSize - (sdFileSize - nn::crypto::AES_CMAC_MAC_SIZE); readDone = true; } - context.Update(buf, readSize); + + if (readSizeforCmac != 0) + { + context.Update(buf, readSizeforCmac); + } + else + { + ret_value = CalculateAndCompareCmac(&context, sdCmac); + if (!ret_value) + { + COMMON_LOGGER( + "********** Verification Failed ********** %s\n", common::GetCharStr(target_from.str().c_str())); + s_VerifyFail++; + } + else + { + COMMON_LOGGER("Success %s\n", common::GetCharStr(target_from.str().c_str())); + s_VerifySuccess++; + } + break; + } s32 sdWriteSize = readSize; // パディングまで読んでいたら書き込みサイズを減らす if (dec.size + sizeof(dec) < totalReadSize) { - sdWriteSize -= totalReadSize - dec.size; + sdWriteSize -= totalReadSize - (dec.size + sizeof(dec)); } - // 復号化して書き込み - result = swAesCtrContext.Decrypt(reinterpret_cast(buf) + bufSize / 2, buf, readSize); - COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); + // 書き込み result = sdOutFile.TryWrite(&writeSize, reinterpret_cast(buf) + bufSize / 2, sdWriteSize, true); COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result); @@ -438,12 +463,19 @@ void DecryptThreadFunc() result = common::SdMountManager::Mount(); size_t bufSize = common::GetAllocatableSize(AES_BLOCK_SIZE * 2); + if(bufSize > common::FILE_COPY_HEAP_SIZE) + { + bufSize = common::FILE_COPY_HEAP_SIZE; + } common::SdReaderWriter sdWriter; common::HeapManager heap(bufSize, AES_BLOCK_SIZE * 2); void* buf = heap.GetAddr(); if (buf != NULL) { + result = nn::fs::TryDeleteDirectoryRecursively( + DECRYPT_ROOT_DIRECTORY_PATH); + // ディレクトリが無ければ作る nn::fs::Directory dir; result = dir.TryInitialize(DECRYPT_ROOT_DIRECTORY_PATH);