mirror of
				https://git.suyu.dev/suyu/suyu
				synced 2025-11-04 00:49:02 -06:00 
			
		
		
		
	core: Gut out cryptop, since it doesn't compile with C++17.
This commit is contained in:
		@@ -172,7 +172,7 @@ set(HEADERS
 | 
			
		||||
create_directory_groups(${SRCS} ${HEADERS})
 | 
			
		||||
add_library(core STATIC ${SRCS} ${HEADERS})
 | 
			
		||||
target_link_libraries(core PUBLIC common PRIVATE audio_core dynarmic network video_core)
 | 
			
		||||
target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp fmt lz4_static unicorn)
 | 
			
		||||
target_link_libraries(core PUBLIC Boost::boost PRIVATE fmt lz4_static unicorn)
 | 
			
		||||
if (ENABLE_WEB_SERVICE)
 | 
			
		||||
    target_link_libraries(core PUBLIC json-headers web_service)
 | 
			
		||||
endif()
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,6 @@
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
#include <cryptopp/sha.h>
 | 
			
		||||
#include "common/alignment.h"
 | 
			
		||||
#include "common/file_util.h"
 | 
			
		||||
#include "common/logging/log.h"
 | 
			
		||||
@@ -69,55 +68,7 @@ Loader::ResultStatus TitleMetadata::Load() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Loader::ResultStatus TitleMetadata::Save() {
 | 
			
		||||
    FileUtil::IOFile file(filepath, "wb");
 | 
			
		||||
    if (!file.IsOpen())
 | 
			
		||||
        return Loader::ResultStatus::Error;
 | 
			
		||||
 | 
			
		||||
    if (!file.WriteBytes(&signature_type, sizeof(u32_be)))
 | 
			
		||||
        return Loader::ResultStatus::Error;
 | 
			
		||||
 | 
			
		||||
    // Signature lengths are variable, and the body follows the signature
 | 
			
		||||
    u32 signature_size = GetSignatureSize(signature_type);
 | 
			
		||||
 | 
			
		||||
    if (!file.WriteBytes(tmd_signature.data(), signature_size))
 | 
			
		||||
        return Loader::ResultStatus::Error;
 | 
			
		||||
 | 
			
		||||
    // The TMD body start position is rounded to the nearest 0x40 after the signature
 | 
			
		||||
    size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
 | 
			
		||||
    file.Seek(body_start, SEEK_SET);
 | 
			
		||||
 | 
			
		||||
    // Update our TMD body values and hashes
 | 
			
		||||
    tmd_body.content_count = static_cast<u16>(tmd_chunks.size());
 | 
			
		||||
 | 
			
		||||
    // TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist?
 | 
			
		||||
    // For now we'll just adjust the first index to hold all content chunks
 | 
			
		||||
    // and ensure that no further content info data exists.
 | 
			
		||||
    tmd_body.contentinfo = {};
 | 
			
		||||
    tmd_body.contentinfo[0].index = 0;
 | 
			
		||||
    tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size());
 | 
			
		||||
 | 
			
		||||
    CryptoPP::SHA256 chunk_hash;
 | 
			
		||||
    for (u16 i = 0; i < tmd_body.content_count; i++) {
 | 
			
		||||
        chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk));
 | 
			
		||||
    }
 | 
			
		||||
    chunk_hash.Final(tmd_body.contentinfo[0].hash.data());
 | 
			
		||||
 | 
			
		||||
    CryptoPP::SHA256 contentinfo_hash;
 | 
			
		||||
    for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
 | 
			
		||||
        chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo));
 | 
			
		||||
    }
 | 
			
		||||
    chunk_hash.Final(tmd_body.contentinfo_hash.data());
 | 
			
		||||
 | 
			
		||||
    // Write our TMD body, then write each of our ContentChunks
 | 
			
		||||
    if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
 | 
			
		||||
        return Loader::ResultStatus::Error;
 | 
			
		||||
 | 
			
		||||
    for (u16 i = 0; i < tmd_body.content_count; i++) {
 | 
			
		||||
        ContentChunk chunk = tmd_chunks[i];
 | 
			
		||||
        if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk))
 | 
			
		||||
            return Loader::ResultStatus::Error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    UNIMPLEMENTED();
 | 
			
		||||
    return Loader::ResultStatus::Success;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,11 +3,8 @@
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <cryptopp/aes.h>
 | 
			
		||||
#include <cryptopp/ccm.h>
 | 
			
		||||
#include <cryptopp/cryptlib.h>
 | 
			
		||||
#include <cryptopp/filters.h>
 | 
			
		||||
#include "common/alignment.h"
 | 
			
		||||
#include "common/assert.h"
 | 
			
		||||
#include "common/logging/log.h"
 | 
			
		||||
#include "core/hw/aes/ccm.h"
 | 
			
		||||
#include "core/hw/aes/key.h"
 | 
			
		||||
@@ -15,80 +12,16 @@
 | 
			
		||||
namespace HW {
 | 
			
		||||
namespace AES {
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
 | 
			
		||||
// 3DS uses a non-standard AES-CCM algorithm, so we need to derive a sub class from the standard one
 | 
			
		||||
// and override with the non-standard part.
 | 
			
		||||
using CryptoPP::lword;
 | 
			
		||||
using CryptoPP::AES;
 | 
			
		||||
using CryptoPP::CCM_Final;
 | 
			
		||||
using CryptoPP::CCM_Base;
 | 
			
		||||
template <bool T_IsEncryption>
 | 
			
		||||
class CCM_3DSVariant_Final : public CCM_Final<AES, CCM_MAC_SIZE, T_IsEncryption> {
 | 
			
		||||
public:
 | 
			
		||||
    void UncheckedSpecifyDataLengths(lword header_length, lword message_length,
 | 
			
		||||
                                     lword footer_length) override {
 | 
			
		||||
        // 3DS uses the aligned size to generate B0 for authentication, instead of the original size
 | 
			
		||||
        lword aligned_message_length = Common::AlignUp(message_length, AES_BLOCK_SIZE);
 | 
			
		||||
        CCM_Base::UncheckedSpecifyDataLengths(header_length, aligned_message_length, footer_length);
 | 
			
		||||
        CCM_Base::m_messageLength = message_length; // restore the actual message size
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class CCM_3DSVariant {
 | 
			
		||||
public:
 | 
			
		||||
    using Encryption = CCM_3DSVariant_Final<true>;
 | 
			
		||||
    using Decryption = CCM_3DSVariant_Final<false>;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace
 | 
			
		||||
 | 
			
		||||
std::vector<u8> EncryptSignCCM(const std::vector<u8>& pdata, const CCMNonce& nonce,
 | 
			
		||||
                               size_t slot_id) {
 | 
			
		||||
    if (!IsNormalKeyAvailable(slot_id)) {
 | 
			
		||||
        LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id);
 | 
			
		||||
    }
 | 
			
		||||
    const AESKey normal = GetNormalKey(slot_id);
 | 
			
		||||
    std::vector<u8> cipher(pdata.size() + CCM_MAC_SIZE);
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        CCM_3DSVariant::Encryption e;
 | 
			
		||||
        e.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE);
 | 
			
		||||
        e.SpecifyDataLengths(0, pdata.size(), 0);
 | 
			
		||||
        CryptoPP::ArraySource as(pdata.data(), pdata.size(), true,
 | 
			
		||||
                                 new CryptoPP::AuthenticatedEncryptionFilter(
 | 
			
		||||
                                     e, new CryptoPP::ArraySink(cipher.data(), cipher.size())));
 | 
			
		||||
    } catch (const CryptoPP::Exception& e) {
 | 
			
		||||
        LOG_ERROR(HW_AES, "FAILED with: %s", e.what());
 | 
			
		||||
    }
 | 
			
		||||
    return cipher;
 | 
			
		||||
    UNIMPLEMENTED();
 | 
			
		||||
    return {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<u8> DecryptVerifyCCM(const std::vector<u8>& cipher, const CCMNonce& nonce,
 | 
			
		||||
                                 size_t slot_id) {
 | 
			
		||||
    if (!IsNormalKeyAvailable(slot_id)) {
 | 
			
		||||
        LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id);
 | 
			
		||||
    }
 | 
			
		||||
    const AESKey normal = GetNormalKey(slot_id);
 | 
			
		||||
    const std::size_t pdata_size = cipher.size() - CCM_MAC_SIZE;
 | 
			
		||||
    std::vector<u8> pdata(pdata_size);
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        CCM_3DSVariant::Decryption d;
 | 
			
		||||
        d.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE);
 | 
			
		||||
        d.SpecifyDataLengths(0, pdata_size, 0);
 | 
			
		||||
        CryptoPP::AuthenticatedDecryptionFilter df(
 | 
			
		||||
            d, new CryptoPP::ArraySink(pdata.data(), pdata_size));
 | 
			
		||||
        CryptoPP::ArraySource as(cipher.data(), cipher.size(), true, new CryptoPP::Redirector(df));
 | 
			
		||||
        if (!df.GetLastResult()) {
 | 
			
		||||
            LOG_ERROR(HW_AES, "FAILED");
 | 
			
		||||
            return {};
 | 
			
		||||
        }
 | 
			
		||||
    } catch (const CryptoPP::Exception& e) {
 | 
			
		||||
        LOG_ERROR(HW_AES, "FAILED with: %s", e.what());
 | 
			
		||||
        return {};
 | 
			
		||||
    }
 | 
			
		||||
    return pdata;
 | 
			
		||||
    UNIMPLEMENTED();
 | 
			
		||||
    return {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace AES
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,6 @@
 | 
			
		||||
// Refer to the license.txt file included.
 | 
			
		||||
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <cryptopp/osrng.h>
 | 
			
		||||
 | 
			
		||||
#include "common/assert.h"
 | 
			
		||||
#include "common/file_util.h"
 | 
			
		||||
@@ -34,8 +33,6 @@ static const char* CpuVendorToStr(Common::CPUVendor vendor) {
 | 
			
		||||
 | 
			
		||||
static u64 GenerateTelemetryId() {
 | 
			
		||||
    u64 telemetry_id{};
 | 
			
		||||
    CryptoPP::AutoSeededRandomPool rng;
 | 
			
		||||
    rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&telemetry_id), sizeof(u64));
 | 
			
		||||
    return telemetry_id;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user