nstool/lib/libnx/include/nx/NcaHeader.h

182 lines
4.1 KiB
C
Raw Normal View History

2017-07-05 08:57:14 +00:00
#pragma once
#include <string>
2017-07-05 08:57:14 +00:00
#include <fnd/types.h>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
2017-07-05 08:57:14 +00:00
#include <crypto/aes.h>
#include <crypto/sha.h>
#include <fnd/ISerialiseableBinary.h>
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
namespace nx
2017-07-05 08:57:14 +00:00
{
class NcaHeader :
public fnd::ISerialiseableBinary
2017-07-05 08:57:14 +00:00
{
2017-07-06 11:17:21 +00:00
public:
enum FormatVersion
{
NCA2_FORMAT,
NCA3_FORMAT
};
2017-07-18 14:17:32 +00:00
enum DistributionType
{
DIST_DOWNLOAD,
DIST_GAME_CARD
};
enum ContentType
{
TYPE_PROGRAM,
TYPE_META,
TYPE_CONTROL,
TYPE_MANUAL,
TYPE_DATA,
};
2018-03-18 09:08:42 +00:00
enum KeyBankIndex
2017-07-18 14:17:32 +00:00
{
2018-03-18 09:08:42 +00:00
KEY_AESXTS_0,
KEY_AESXTS_1,
KEY_AESCTR,
2017-07-18 14:17:32 +00:00
KEY_UNUSED_3,
};
2017-07-06 11:17:21 +00:00
struct sSection
{
2018-03-22 05:26:22 +00:00
uint64_t offset;
uint64_t size;
2017-07-06 11:17:21 +00:00
crypto::sha::sSha256Hash hash;
const sSection& operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
hash = other.hash;
return *this;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size) \
&& (hash == other.hash);
}
bool operator!=(const sSection& other) const
{
2018-03-21 12:29:08 +00:00
return !operator==(other);
2017-07-06 11:17:21 +00:00
}
};
2017-07-18 14:17:32 +00:00
static const size_t kBlockSize = 0x200;
2017-07-06 11:17:21 +00:00
NcaHeader();
NcaHeader(const NcaHeader& other);
2018-03-22 05:26:22 +00:00
NcaHeader(const byte_t* bytes, size_t len);
2017-07-06 11:17:21 +00:00
bool operator==(const NcaHeader& other) const;
bool operator!=(const NcaHeader& other) const;
void operator=(const NcaHeader& other);
// to be used after export
2018-03-22 05:26:22 +00:00
const byte_t* getBytes() const;
2017-07-06 11:17:21 +00:00
size_t getSize() const;
// export/import binary
void exportBinary();
2018-03-22 05:26:22 +00:00
void importBinary(const byte_t* bytes, size_t len);
2017-07-06 11:17:21 +00:00
// variables
void clear();
2018-03-18 09:08:42 +00:00
FormatVersion getFormatVersion() const;
void setFormatVersion(FormatVersion ver);
2017-07-18 14:17:32 +00:00
DistributionType getDistributionType() const;
void setDistributionType(DistributionType type);
ContentType getContentType() const;
void setContentType(ContentType type);
2018-03-18 09:08:42 +00:00
byte_t getCryptoType() const;
void setCryptoType(byte_t type);
byte_t getKaekIndex() const;
void setKaekIndex(byte_t index);
2018-03-22 05:26:22 +00:00
uint64_t getNcaSize() const;
void setNcaSize(uint64_t size);
uint64_t getProgramId() const;
void setProgramId(uint64_t program_id);
uint32_t getContentIndex() const;
void setContentIndex(uint32_t index);
uint32_t getSdkAddonVersion() const;
void setSdkAddonVersion(uint32_t version);
2017-07-06 11:17:21 +00:00
const fnd::List<sSection>& getSections() const;
void addSection(const sSection& section);
2017-07-18 14:17:32 +00:00
const fnd::List<crypto::aes::sAes128Key>& getEncAesKeys() const;
void addEncAesKey(const crypto::aes::sAes128Key& key);
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
private:
const std::string kModuleName = "NCA_HEADER";
2018-03-18 09:08:42 +00:00
const std::string kNca2Sig = "NCA2";
const std::string kNca3Sig = "NCA3";
2017-07-06 11:17:21 +00:00
static const size_t kSectionNum = 4;
static const size_t kAesKeyNum = 4;
2018-03-22 05:26:22 +00:00
static const uint32_t kDefaultSdkAddonVersion = 721920;
2017-07-18 14:17:32 +00:00
enum ProgramPartitionId
{
SECTION_CODE,
SECTION_DATA,
SECTION_LOGO,
};
2017-07-05 08:57:14 +00:00
#pragma pack (push, 1)
2017-07-06 11:17:21 +00:00
struct sNcaHeader
2017-07-05 08:57:14 +00:00
{
char signature[4];
byte_t distribution_type;
byte_t content_type;
2018-03-18 09:08:42 +00:00
byte_t crypto_type; // KeyGeneration
byte_t key_area_encryption_key_index;
le_uint64_t nca_size;
le_uint64_t program_id;
le_uint32_t content_index;
le_uint32_t sdk_addon_version;
2018-03-18 09:08:42 +00:00
byte_t crypto_type_2;
byte_t reserved_2[0xf];
byte_t rights_id[0x10];
2017-07-06 11:17:21 +00:00
struct sNcaSection
{
le_uint32_t start; // block units
le_uint32_t end; // block units
byte_t enabled;
byte_t reserved[7];
} section[kSectionNum];
crypto::sha::sSha256Hash section_hash[kSectionNum];
crypto::aes::sAes128Key enc_aes_key[kAesKeyNum];
2017-07-06 11:17:21 +00:00
};
#pragma pack (pop)
2017-07-05 08:57:14 +00:00
2017-07-06 11:17:21 +00:00
// binary
fnd::MemoryBlob mBinaryBlob;
// data
2018-03-18 09:08:42 +00:00
FormatVersion mFormatVersion;
2017-07-18 14:17:32 +00:00
DistributionType mDistributionType;
ContentType mContentType;
2018-03-18 09:08:42 +00:00
byte_t mCryptoType;
byte_t mKaekIndex;
2018-03-22 05:26:22 +00:00
uint64_t mNcaSize;
uint64_t mProgramId;
uint32_t mContentIndex;
uint32_t mSdkAddonVersion;
2017-07-06 11:17:21 +00:00
fnd::List<sSection> mSections;
2017-07-18 14:17:32 +00:00
fnd::List<crypto::aes::sAes128Key> mEncAesKeys;
2017-07-06 11:17:21 +00:00
2018-03-22 05:26:22 +00:00
uint64_t blockNumToSize(uint32_t block_num) const;
uint32_t sizeToBlockNum(uint64_t real_size) const;
2017-07-06 11:17:21 +00:00
bool isEqual(const NcaHeader& other) const;
void copyFrom(const NcaHeader& other);
2017-07-05 08:57:14 +00:00
};
2017-07-06 11:17:21 +00:00
}