nstool/lib/libhac/source/NcaUtils.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2018-08-07 07:17:51 +00:00
#include <nn/hac/NcaUtils.h>
2018-04-15 02:40:22 +00:00
void nn::hac::NcaUtils::decryptContentArchiveHeader(const byte_t* src, byte_t* dst, const fnd::aes::sAesXts128Key& key)
2018-04-15 02:40:22 +00:00
{
byte_t tweak[fnd::aes::kAesBlockSize];
2018-04-15 02:40:22 +00:00
// decrypt main header
2018-08-07 07:17:51 +00:00
byte_t raw_hdr[nn::hac::nca::kSectorSize];
fnd::aes::AesXtsMakeTweak(tweak, 1);
fnd::aes::AesXtsDecryptSector(src + sectorToOffset(1), nn::hac::nca::kSectorSize, key.key[0], key.key[1], tweak, raw_hdr);
2018-04-15 02:40:22 +00:00
bool useNca2SectorIndex = ((nn::hac::sContentArchiveHeader*)(raw_hdr))->st_magic.get() == nn::hac::nca::kNca2StructMagic;
2018-04-15 02:40:22 +00:00
// decrypt whole header
2018-08-07 07:17:51 +00:00
for (size_t i = 0; i < nn::hac::nca::kHeaderSectorNum; i++)
2018-04-15 02:40:22 +00:00
{
fnd::aes::AesXtsMakeTweak(tweak, (i > 1 && useNca2SectorIndex)? 0 : i);
fnd::aes::AesXtsDecryptSector(src + sectorToOffset(i), nn::hac::nca::kSectorSize, key.key[0], key.key[1], tweak, dst + sectorToOffset(i));
2018-04-15 02:40:22 +00:00
}
}
2018-08-07 07:17:51 +00:00
byte_t nn::hac::NcaUtils::getMasterKeyRevisionFromKeyGeneration(byte_t key_generation)
2018-04-15 02:40:22 +00:00
{
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;
2018-05-01 05:21:28 +00:00
}
2018-08-07 07:17:51 +00:00
void nn::hac::NcaUtils::getNcaPartitionAesCtr(const nn::hac::sNcaFsHeader* hdr, byte_t* ctr)
2018-05-01 05:21:28 +00:00
{
for (size_t i = 0; i < 8; i++)
2018-05-01 05:21:28 +00:00
{
ctr[7-i] = hdr->aes_ctr_upper[i];
ctr[15-i] = 0;
2018-05-01 05:21:28 +00:00
}
2018-04-15 02:40:22 +00:00
}