Files
threeSD/src/core/quick_decryptor.h
T
zhupengfei 49ddd86b7a Add CIA building
Quite a lot of code, yeah.

The built CIA is almost identical to GM9, with the following differences:
1. Paddings are zeroed out
2. Title key is not written (GM9 gets it from support data/ticket db)
3. Ticket content index is slightly different (GM9 likely takes it from the legit ticket, while we are building a fake one)

The 2, 3 points can be fixed probably.
2020-08-07 08:58:09 +08:00

90 lines
2.7 KiB
C++

// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <atomic>
#include <memory>
#include <string>
#include "common/common_types.h"
#include "common/thread.h"
#include "core/key/key.h"
namespace Core {
/// (current_size, total_size)
using ProgressCallback = std::function<void(std::size_t, std::size_t)>;
/**
* Helper that reads, decrypts and writes data. This uses three threads to process the data
* and call progress callbacks occasionally.
*/
class QuickDecryptor {
public:
explicit QuickDecryptor();
~QuickDecryptor();
/**
* Decrypts and writes a file.
*
* @param source Source file
* @param size Size to read, decrypt and write
* @param destination Destination file
* @param callback Progress callback
* @param decrypt Whether to perform decryption or not
* @param key AES Key for decryption
* @param ctr AES CTR for decryption
* @param aes_seek_pos The position to seek to for decryption.
*/
bool DecryptAndWriteFile(std::shared_ptr<FileUtil::IOFile> source, std::size_t size,
std::shared_ptr<FileUtil::IOFile> destination,
const ProgressCallback& callback = [](std::size_t, std::size_t) {},
bool decrypt = false, Core::Key::AESKey key = {},
Core::Key::AESKey ctr = {}, std::size_t aes_seek_pos = 0);
void DataReadLoop();
void DataDecryptLoop();
void DataWriteLoop();
void Abort();
/// Reset the imported_size counter for this content and set a new total_size.
void Reset(std::size_t total_size);
private:
static constexpr std::size_t BufferSize = 16 * 1024; // 16 KB
std::shared_ptr<FileUtil::IOFile> source;
std::shared_ptr<FileUtil::IOFile> destination;
bool decrypt{};
Core::Key::AESKey key;
Core::Key::AESKey ctr;
std::size_t aes_seek_pos;
// Total size of this content, may consist of multiple files
std::size_t total_size{};
// Total size of the current file to process
std::size_t current_total_size{};
// Total imported size for this content
std::size_t imported_size{};
std::array<std::array<u8, BufferSize>, 3> buffers;
std::array<Common::Event, 3> data_read_event;
std::array<Common::Event, 3> data_decrypted_event;
std::array<Common::Event, 3> data_written_event;
std::unique_ptr<std::thread> read_thread;
std::unique_ptr<std::thread> decrypt_thread;
std::unique_ptr<std::thread> write_thread;
ProgressCallback callback;
Common::Event completion_event;
bool is_good{true};
std::atomic_bool is_running{false};
};
} // namespace Core