mirror of
https://github.com/jakcron/nstool
synced 2024-11-15 02:06:40 +00:00
[nx] Add NcaUtils
This commit is contained in:
parent
2573ad4b47
commit
ee04bcdc8d
2 changed files with 62 additions and 0 deletions
13
lib/libnx/include/nx/NcaUtils.h
Normal file
13
lib/libnx/include/nx/NcaUtils.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
#include <nx/nca.h>
|
||||
|
||||
namespace nx
|
||||
{
|
||||
class NcaUtils
|
||||
{
|
||||
public:
|
||||
static inline size_t sectorToOffset(size_t sector_index) { return sector_index * nx::nca::kSectorSize; }
|
||||
static void decryptNcaHeader(const byte_t* src, byte_t* dst, const crypto::aes::sAesXts128Key& key);
|
||||
static byte_t getMasterKeyRevisionFromKeyGeneration(byte_t key_generation);
|
||||
};
|
||||
}
|
49
lib/libnx/source/NcaUtils.cpp
Normal file
49
lib/libnx/source/NcaUtils.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <nx/NcaUtils.h>
|
||||
|
||||
void nx::NcaUtils::decryptNcaHeader(const byte_t* src, byte_t* dst, const crypto::aes::sAesXts128Key& key)
|
||||
{
|
||||
byte_t tweak[crypto::aes::kAesBlockSize];
|
||||
|
||||
// decrypt main header
|
||||
byte_t raw_hdr[nx::nca::kSectorSize];
|
||||
crypto::aes::AesXtsMakeTweak(tweak, 1);
|
||||
crypto::aes::AesXtsDecryptSector(src + sectorToOffset(1), nx::nca::kSectorSize, key.key[0], key.key[1], tweak, raw_hdr);
|
||||
|
||||
bool useNca2SectorIndex = memcmp(((nx::sNcaHeader*)(raw_hdr))->signature, nx::nca::kNca2Sig.c_str(), 4) == 0;
|
||||
|
||||
// decrypt whole header
|
||||
for (size_t i = 0; i < nx::nca::kHeaderSectorNum; i++)
|
||||
{
|
||||
crypto::aes::AesXtsMakeTweak(tweak, (i > 1 && useNca2SectorIndex)? 0 : i);
|
||||
crypto::aes::AesXtsDecryptSector(src + sectorToOffset(i), nx::nca::kSectorSize, key.key[0], key.key[1], tweak, dst + sectorToOffset(i));
|
||||
}
|
||||
}
|
||||
|
||||
byte_t nx::NcaUtils::getMasterKeyRevisionFromKeyGeneration(byte_t key_generation)
|
||||
{
|
||||
byte_t masterkey_rev;
|
||||
|
||||
switch (key_generation)
|
||||
{
|
||||
case(0):
|
||||
case(1):
|
||||
masterkey_rev = 0;
|
||||
break;
|
||||
case(2):
|
||||
masterkey_rev = 1;
|
||||
break;
|
||||
case(3):
|
||||
masterkey_rev = 2;
|
||||
break;
|
||||
case(4):
|
||||
masterkey_rev = 3;
|
||||
break;
|
||||
case(5):
|
||||
masterkey_rev = 4;
|
||||
break;
|
||||
default:
|
||||
masterkey_rev = key_generation - 1;
|
||||
}
|
||||
|
||||
return masterkey_rev;
|
||||
}
|
Loading…
Reference in a new issue