mirror of
https://github.com/Team-Neptune/NSAInstaller.git
synced 2024-11-27 14:40:50 +00:00
"fix" crypto stuff
This commit is contained in:
parent
51a35b43af
commit
bcda570fa0
79
include/util/crypto.hpp
Normal file → Executable file
79
include/util/crypto.hpp
Normal file → Executable file
@ -62,11 +62,16 @@ namespace Crypto
|
||||
class AesCtr
|
||||
{
|
||||
public:
|
||||
AesCtr();
|
||||
AesCtr(u64 iv);
|
||||
AesCtr() : m_high(0), m_low(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline u64& high() { return m_high; }
|
||||
inline u64& low() { return m_low; }
|
||||
AesCtr(u64 iv) : m_high(swapEndian(iv)), m_low(0)
|
||||
{
|
||||
}
|
||||
|
||||
u64& high() { return m_high; }
|
||||
u64& low() { return m_low; }
|
||||
private:
|
||||
u64 m_high;
|
||||
u64 m_low;
|
||||
@ -76,25 +81,73 @@ namespace Crypto
|
||||
class Aes128Ctr
|
||||
{
|
||||
public:
|
||||
Aes128Ctr(const u8* key, const AesCtr& iv);
|
||||
virtual ~Aes128Ctr();
|
||||
Aes128Ctr(const u8* key, const AesCtr& iv)
|
||||
{
|
||||
counter = iv;
|
||||
aes128CtrContextCreate(&ctx, key, &iv);
|
||||
seek(0);
|
||||
}
|
||||
|
||||
void seek(u64 offset);
|
||||
void encrypt(void *dst, const void *src, size_t l);
|
||||
void decrypt(void *dst, const void *src, size_t l);
|
||||
virtual ~Aes128Ctr()
|
||||
{
|
||||
}
|
||||
|
||||
void seek(u64 offset)
|
||||
{
|
||||
counter.low() = swapEndian(offset >> 4);
|
||||
aes128CtrContextResetCtr(&ctx, &counter);
|
||||
}
|
||||
|
||||
void encrypt(void *dst, const void *src, size_t l)
|
||||
{
|
||||
aes128CtrCrypt(&ctx, dst, src, l);
|
||||
}
|
||||
|
||||
void decrypt(void *dst, const void *src, size_t l)
|
||||
{
|
||||
encrypt(dst, src, l);
|
||||
}
|
||||
protected:
|
||||
AesCtr counter;
|
||||
|
||||
Aes128CtrContext ctx;
|
||||
};
|
||||
|
||||
class AesXtr
|
||||
{
|
||||
public:
|
||||
AesXtr(const u8* key);
|
||||
virtual ~AesXtr();
|
||||
AesXtr(const u8* key, bool is_encryptor)
|
||||
{
|
||||
aes128XtsContextCreate(&ctx, key, key + 0x10, is_encryptor);
|
||||
}
|
||||
|
||||
void encrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size);
|
||||
void decrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size);
|
||||
virtual ~AesXtr()
|
||||
{
|
||||
}
|
||||
|
||||
void encrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
|
||||
{
|
||||
for (size_t i = 0; i < l; i += sector_size)
|
||||
{
|
||||
aes128XtsContextResetSector(&ctx, sector++, true);
|
||||
aes128XtsEncrypt(&ctx, dst, src, sector_size);
|
||||
|
||||
dst = (u8*)dst + sector_size;
|
||||
src = (const u8*)src + sector_size;
|
||||
}
|
||||
}
|
||||
|
||||
void decrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
|
||||
{
|
||||
for (size_t i = 0; i < l; i += sector_size)
|
||||
{
|
||||
aes128XtsContextResetSector(&ctx, sector++, true);
|
||||
aes128XtsDecrypt(&ctx, dst, src, sector_size);
|
||||
|
||||
dst = (u8*)dst + sector_size;
|
||||
src = (const u8*)src + sector_size;
|
||||
}
|
||||
}
|
||||
protected:
|
||||
Aes128XtsContext ctx;
|
||||
};
|
||||
|
@ -121,8 +121,8 @@ namespace tin::install::nsp
|
||||
{
|
||||
tin::install::NcaHeader header;
|
||||
ncaFile.Read(0, &header, 0xc00);
|
||||
Crypto::AesXtr crypto(Crypto::Keys().headerKey);
|
||||
crypto.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false);
|
||||
decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
|
||||
if (header.magic != MAGIC_NCA3)
|
||||
THROW_FORMAT("Invalid NCA magic");
|
||||
|
@ -96,8 +96,8 @@ namespace tin::install::nsp
|
||||
{
|
||||
tin::install::NcaHeader header;
|
||||
m_remoteNSP->BufferNCAHeader(&header, ncaId);
|
||||
Crypto::AesXtr crypto(Crypto::Keys().headerKey);
|
||||
crypto.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false);
|
||||
decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
|
||||
if (header.magic != MAGIC_NCA3)
|
||||
THROW_FORMAT("Invalid NCA magic");
|
||||
|
@ -409,15 +409,30 @@ u64 NcaWriter::write(const u8* ptr, u64 sz)
|
||||
{
|
||||
tin::install::NcaHeader header;
|
||||
memcpy(&header, m_buffer.data(), sizeof(header));
|
||||
Crypto::AesXtr crypto(Crypto::Keys().headerKey);
|
||||
crypto.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
Crypto::AesXtr decryptor(Crypto::Keys().headerKey, false);
|
||||
Crypto::AesXtr encryptor(Crypto::Keys().headerKey, true);
|
||||
decryptor.decrypt(&header, &header, sizeof(header), 0, 0x200);
|
||||
|
||||
if (header.magic != MAGIC_NCA3)
|
||||
throw std::runtime_error("Invalid NCA magic");
|
||||
if (header.magic == MAGIC_NCA3)
|
||||
{
|
||||
if(isOpen())
|
||||
{
|
||||
m_contentStorage->CreatePlaceholder(m_ncaId, *(NcmPlaceHolderId*)&m_ncaId, header.nca_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw "Invalid NCA magic";
|
||||
}
|
||||
|
||||
if (header.distribution == 1)
|
||||
{
|
||||
header.distribution = 0;
|
||||
}
|
||||
encryptor.encrypt(m_buffer.data(), &header, sizeof(header), 0, 0x200);
|
||||
|
||||
if(isOpen())
|
||||
{
|
||||
m_contentStorage->CreatePlaceholder(m_ncaId, *(NcmPlaceHolderId*)&m_ncaId, header.nca_size);
|
||||
m_contentStorage->WritePlaceholder(*(NcmPlaceHolderId*)&m_ncaId, 0, m_buffer.data(), m_buffer.size());
|
||||
}
|
||||
}
|
||||
|
@ -86,72 +86,4 @@ bool Crypto::rsa2048PssVerify(const void *data, size_t len, const unsigned char
|
||||
sha256CalculateHash(validate_hash, validate_buf, 0x48);
|
||||
|
||||
return memcmp(h_buf, validate_hash, 0x20) == 0;
|
||||
}
|
||||
|
||||
Crypto::AesCtr::AesCtr() : m_high(0), m_low(0)
|
||||
{
|
||||
}
|
||||
|
||||
Crypto::AesCtr::AesCtr(u64 iv) : m_high(swapEndian(iv)), m_low(0)
|
||||
{
|
||||
}
|
||||
|
||||
Crypto::Aes128Ctr::Aes128Ctr(const u8* key, const AesCtr& iv)
|
||||
{
|
||||
counter = iv;
|
||||
aes128CtrContextCreate(&this->ctx, key, &iv);
|
||||
seek(0);
|
||||
}
|
||||
|
||||
Crypto::Aes128Ctr::~Aes128Ctr()
|
||||
{
|
||||
}
|
||||
|
||||
void Crypto::Aes128Ctr::seek(u64 offset)
|
||||
{
|
||||
counter.low() = swapEndian(offset >> 4);
|
||||
aes128CtrContextResetCtr(&this->ctx, &counter);
|
||||
}
|
||||
|
||||
void Crypto::Aes128Ctr::encrypt(void *dst, const void *src, size_t l)
|
||||
{
|
||||
aes128CtrCrypt(&this->ctx, dst, src, l);
|
||||
}
|
||||
|
||||
void Crypto::Aes128Ctr::decrypt(void *dst, const void *src, size_t l)
|
||||
{
|
||||
encrypt(dst, src, l);
|
||||
}
|
||||
|
||||
Crypto::AesXtr::AesXtr(const u8* key)
|
||||
{
|
||||
aes128XtsContextCreate(&this->ctx, key, key + 0x10, false);
|
||||
}
|
||||
|
||||
Crypto::AesXtr::~AesXtr()
|
||||
{
|
||||
}
|
||||
|
||||
void Crypto::AesXtr::encrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
|
||||
{
|
||||
for (size_t i = 0; i < l; i += sector_size)
|
||||
{
|
||||
aes128XtsContextResetSector(&this->ctx, sector++, true);
|
||||
aes128XtsEncrypt(&this->ctx, dst, src, sector_size);
|
||||
|
||||
dst = (u8*)dst + sector_size;
|
||||
src = (const u8*)src + sector_size;
|
||||
}
|
||||
}
|
||||
|
||||
void Crypto::AesXtr::decrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
|
||||
{
|
||||
for (size_t i = 0; i < l; i += sector_size)
|
||||
{
|
||||
aes128XtsContextResetSector(&this->ctx, sector++, true);
|
||||
aes128XtsDecrypt(&this->ctx, dst, src, sector_size);
|
||||
|
||||
dst = (u8*)dst + sector_size;
|
||||
src = (const u8*)src + sector_size;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user