mirror of
https://github.com/rvtr/ctr_Repair.git
synced 2025-10-31 13:51:08 -04:00
暗号化無効に。バッファの後半半分にそのままデータをコピーして書き込むように
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@519 385bec56-5757-e545-9c3a-d8741f4650f1
This commit is contained in:
parent
7a747bf781
commit
f1b229ee78
@ -22,6 +22,9 @@ SAMPLED_DEMOS_COMMON_INCLUDE_DIR = $(dir $(HORIZON_ROOT)/../CTR/SampleDemos/co
|
|||||||
INCLUDES += $(SAMPLED_DEMOS_COMMON_INCLUDE_DIR) \
|
INCLUDES += $(SAMPLED_DEMOS_COMMON_INCLUDE_DIR) \
|
||||||
../common
|
../common
|
||||||
|
|
||||||
|
# 暗号化とCMAC付加を無効にする場合有効にする
|
||||||
|
CCFLAGS += -DSKIP_VERIFY
|
||||||
|
|
||||||
SOURCES[] =
|
SOURCES[] =
|
||||||
ConsoleBackup.cpp
|
ConsoleBackup.cpp
|
||||||
Controller.cpp
|
Controller.cpp
|
||||||
|
|||||||
@ -162,12 +162,16 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
nn::crypto::SwAesCtrContext swAesCtrContext;
|
nn::crypto::SwAesCtrContext swAesCtrContext;
|
||||||
swAesCtrContext.Initialize(iv, common::key, sizeof(key));
|
swAesCtrContext.Initialize(iv, common::key, sizeof(key));
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t totalReadSize = 0;
|
size_t totalReadSize = 0;
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
nn::crypto::Sha256Context context;
|
nn::crypto::Sha256Context context;
|
||||||
context.Initialize();
|
context.Initialize();
|
||||||
|
#endif
|
||||||
|
|
||||||
// ファイルサイズをヘッダに書いておく
|
// ファイルサイズをヘッダに書いておく
|
||||||
// 書き込み対象ファイル作成
|
// 書き込み対象ファイル作成
|
||||||
@ -177,6 +181,7 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// フルパスをハッシュに含める
|
// フルパスをハッシュに含める
|
||||||
context.Update(from_path, std::wcslen(from_path) * sizeof(wchar_t));
|
context.Update(from_path, std::wcslen(from_path) * sizeof(wchar_t));
|
||||||
|
|
||||||
@ -191,6 +196,7 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
s32 writeSize;
|
s32 writeSize;
|
||||||
result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false);
|
result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -203,6 +209,7 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
|
|
||||||
if (readsize == 0)
|
if (readsize == 0)
|
||||||
{
|
{
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
NN_LOG("Add CMAC %ls\n", from_path);
|
NN_LOG("Add CMAC %ls\n", from_path);
|
||||||
// SHA256を計算してCMACを付加する
|
// SHA256を計算してCMACを付加する
|
||||||
bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE];
|
bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE];
|
||||||
@ -217,6 +224,7 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
result = to_file.TryFlush();
|
result = to_file.TryFlush();
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -224,6 +232,7 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
{
|
{
|
||||||
NN_LOG("EncryptSize = %d\n", readsize);
|
NN_LOG("EncryptSize = %d\n", readsize);
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
u8 paddingSize = 0;
|
u8 paddingSize = 0;
|
||||||
AddPkcsPadding(&paddingSize, reinterpret_cast<bit8*>(buf), bufSize / 2, &readsize);
|
AddPkcsPadding(&paddingSize, reinterpret_cast<bit8*>(buf), bufSize / 2, &readsize);
|
||||||
|
|
||||||
@ -237,6 +246,13 @@ bool ExportTwlSaveFile(const wchar_t* from_path, const wchar_t* to_path, void* b
|
|||||||
|
|
||||||
// 事前計算したファイルサイズに一致させるためパディング分減算
|
// 事前計算したファイルサイズに一致させるためパディング分減算
|
||||||
readsize -= paddingSize;
|
readsize -= paddingSize;
|
||||||
|
#else
|
||||||
|
// 後半半分にコピーしてから書き込む
|
||||||
|
std::memcpy(reinterpret_cast<bit8*>(buf) + bufSize / 2, buf, readsize);
|
||||||
|
|
||||||
|
result = to_file.TryWrite(&writesize, reinterpret_cast<bit8*>(buf) + bufSize / 2, readsize, false);
|
||||||
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
|
|
||||||
s_FinishedFileSize += readsize;
|
s_FinishedFileSize += readsize;
|
||||||
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
||||||
@ -345,7 +361,9 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
nn::fs::FileInputStream from_file;
|
nn::fs::FileInputStream from_file;
|
||||||
nn::fs::FileStream to_file;
|
nn::fs::FileStream to_file;
|
||||||
s64 filesize;
|
s64 filesize;
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
s64 fileSizeWithoutHeaderAndFooter;
|
s64 fileSizeWithoutHeaderAndFooter;
|
||||||
|
#endif
|
||||||
s32 readsize;
|
s32 readsize;
|
||||||
s32 writesize;
|
s32 writesize;
|
||||||
|
|
||||||
@ -367,12 +385,16 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
nn::crypto::SwAesCtrContext swAesCtrContext;
|
nn::crypto::SwAesCtrContext swAesCtrContext;
|
||||||
swAesCtrContext.Initialize(iv, common::key, sizeof(key));
|
swAesCtrContext.Initialize(iv, common::key, sizeof(key));
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t totalReadSize = 0;
|
size_t totalReadSize = 0;
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
nn::crypto::Sha256Context context;
|
nn::crypto::Sha256Context context;
|
||||||
context.Initialize();
|
context.Initialize();
|
||||||
|
#endif
|
||||||
|
|
||||||
// ファイルサイズをヘッダに書いておく
|
// ファイルサイズをヘッダに書いておく
|
||||||
if (encode)
|
if (encode)
|
||||||
@ -384,6 +406,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// フルパスをハッシュに含める
|
// フルパスをハッシュに含める
|
||||||
context.Update(target_from.str().c_str(), target_from.str().size() * sizeof(wchar_t));
|
context.Update(target_from.str().c_str(), target_from.str().size() * sizeof(wchar_t));
|
||||||
|
|
||||||
@ -398,9 +421,11 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
s32 writeSize;
|
s32 writeSize;
|
||||||
result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false);
|
result = to_file.TryWrite(&writeSize, &enc, sizeof(enc), false);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// ヘッダを読む
|
// ヘッダを読む
|
||||||
// ハッシュの計算は終わっているので復号化のみ
|
// ハッシュの計算は終わっているので復号化のみ
|
||||||
BackupDataHeader header;
|
BackupDataHeader header;
|
||||||
@ -418,6 +443,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
// 書き込み対象ファイル作成
|
// 書き込み対象ファイル作成
|
||||||
result = nn::fs::TryCreateFile(target_tmp.str().c_str(), fileSizeWithoutHeaderAndFooter);
|
result = nn::fs::TryCreateFile(target_tmp.str().c_str(), fileSizeWithoutHeaderAndFooter);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
result = to_file.TryInitialize(target_tmp.str().c_str(),
|
result = to_file.TryInitialize(target_tmp.str().c_str(),
|
||||||
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
nn::fs::OPEN_MODE_READ | nn::fs::OPEN_MODE_WRITE | nn::fs::OPEN_MODE_CREATE);
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
@ -435,6 +461,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
{
|
{
|
||||||
if (encode)
|
if (encode)
|
||||||
{
|
{
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
NN_LOG("Add CMAC %ls\n", target_from.str().c_str());
|
NN_LOG("Add CMAC %ls\n", target_from.str().c_str());
|
||||||
// SHA256を計算してCMACを付加する
|
// SHA256を計算してCMACを付加する
|
||||||
bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE];
|
bit8 sha256Hash[nn::crypto::Sha256Context::HASH_SIZE];
|
||||||
@ -447,11 +474,13 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
|
|
||||||
result = to_file.TryWrite(&writesize, cmac, sizeof(cmac));
|
result = to_file.TryWrite(&writesize, cmac, sizeof(cmac));
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
result = to_file.TryFlush();
|
result = to_file.TryFlush();
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// 復号済みなら検証する
|
// 復号済みなら検証する
|
||||||
if (!encode)
|
if (!encode)
|
||||||
{
|
{
|
||||||
@ -461,6 +490,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -468,6 +498,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
{
|
{
|
||||||
if (encode)
|
if (encode)
|
||||||
{
|
{
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
NN_LOG("EncryptSize = %d\n", readsize);
|
NN_LOG("EncryptSize = %d\n", readsize);
|
||||||
|
|
||||||
u8 paddingSize = 0;
|
u8 paddingSize = 0;
|
||||||
@ -484,7 +515,13 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
|
|
||||||
// 事前計算したファイルサイズに一致させるためパディング分減算
|
// 事前計算したファイルサイズに一致させるためパディング分減算
|
||||||
readsize -= paddingSize;
|
readsize -= paddingSize;
|
||||||
|
#else
|
||||||
|
// 後半半分にコピーしてから書き込む
|
||||||
|
std::memcpy(reinterpret_cast<bit8*>(buf) + bufSize / 2, buf, readsize);
|
||||||
|
|
||||||
|
result = to_file.TryWrite(&writesize, reinterpret_cast<bit8*>(buf) + bufSize / 2, readsize, false);
|
||||||
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
#endif
|
||||||
s_FinishedFileSize += readsize;
|
s_FinishedFileSize += readsize;
|
||||||
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
||||||
NN_LOG(
|
NN_LOG(
|
||||||
@ -492,6 +529,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// ハッシュ検証は通っているので復号化しつつ書き込み
|
// ハッシュ検証は通っているので復号化しつつ書き込み
|
||||||
// パディング以降は書き込まないよう書き込みサイズを変更する
|
// パディング以降は書き込まないよう書き込みサイズを変更する
|
||||||
|
|
||||||
@ -510,12 +548,19 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
|
|
||||||
result = to_file.TryWrite(&writesize, reinterpret_cast<bit8*>(buf) + bufSize / 2, readsize,
|
result = to_file.TryWrite(&writesize, reinterpret_cast<bit8*>(buf) + bufSize / 2, readsize,
|
||||||
false);
|
false);
|
||||||
|
#else
|
||||||
|
// 後半半分にコピーしてから書き込む
|
||||||
|
std::memcpy(reinterpret_cast<bit8*>(buf) + bufSize / 2, buf, readsize);
|
||||||
|
|
||||||
|
result = to_file.TryWrite(&writesize, reinterpret_cast<bit8*>(buf) + bufSize / 2, readsize, false);
|
||||||
|
#endif
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
s_FinishedFileSize += readsize;
|
s_FinishedFileSize += readsize;
|
||||||
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
s_Progress = s_FinishedFileSize * 100 / s_TotalFileSize;
|
||||||
NN_LOG(
|
NN_LOG(
|
||||||
"finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress);
|
"finish = %lld, total = %lld, progress = %lld\n", s_FinishedFileSize, s_TotalFileSize, s_Progress);
|
||||||
|
|
||||||
|
#ifndef SKIP_VERIFY
|
||||||
// 読みきったので次のファイルへ
|
// 読みきったので次のファイルへ
|
||||||
if (readDone)
|
if (readDone)
|
||||||
{
|
{
|
||||||
@ -530,6 +575,7 @@ bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wc
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
COMMON_LOGGER_RETURN_FALSE_IF_FAILED(result);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user