2020-04-16 01:06:41 +01:00
/*
2020-07-03 10:31:22 +01:00
* nca . h
2020-04-16 01:06:41 +01:00
*
2020-07-03 10:31:22 +01:00
* Copyright ( c ) 2020 , DarkMatterCore < pabloacurielz @ gmail . com > .
*
* This file is part of nxdumptool ( https : //github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool is free software ; you can redistribute it and / or modify it
2020-04-16 01:06:41 +01:00
* under the terms and conditions of the GNU General Public License ,
* version 2 , as published by the Free Software Foundation .
*
2020-07-03 10:31:22 +01:00
* nxdumptool is distributed in the hope it will be useful , but WITHOUT
2020-04-16 01:06:41 +01:00
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License for
* more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2020-04-11 06:28:26 +01:00
# pragma once
# ifndef __NCA_H__
# define __NCA_H__
2020-04-20 11:39:41 +01:00
# include "tik.h"
2020-04-11 06:28:26 +01:00
2020-04-21 11:23:33 +01:00
# define NCA_HEADER_LENGTH 0x400
# define NCA_FS_HEADER_LENGTH 0x200
# define NCA_FS_HEADER_COUNT 4
# define NCA_FULL_HEADER_LENGTH (NCA_HEADER_LENGTH + (NCA_FS_HEADER_LENGTH * NCA_FS_HEADER_COUNT))
2020-04-11 06:28:26 +01:00
2020-04-21 11:23:33 +01:00
# define NCA_NCA0_MAGIC 0x4E434130 /* "NCA0" */
# define NCA_NCA2_MAGIC 0x4E434132 /* "NCA2" */
# define NCA_NCA3_MAGIC 0x4E434133 /* "NCA3" */
2020-04-11 06:28:26 +01:00
2020-04-26 09:35:01 +01:00
# define NCA_HIERARCHICAL_SHA256_LAYER_COUNT 2
2020-04-21 11:23:33 +01:00
# define NCA_IVFC_MAGIC 0x49564643 /* "IVFC" */
2020-04-27 23:37:15 +01:00
# define NCA_IVFC_LAYER_COUNT 7
2020-04-26 09:35:01 +01:00
# define NCA_IVFC_HASH_DATA_LAYER_COUNT 5
# define NCA_IVFC_BLOCK_SIZE(x) (1 << (x))
2020-04-11 06:28:26 +01:00
2020-04-21 11:23:33 +01:00
# define NCA_BKTR_MAGIC 0x424B5452 /* "BKTR" */
2020-04-11 06:28:26 +01:00
2020-04-21 11:23:33 +01:00
# define NCA_FS_ENTRY_BLOCK_SIZE 0x200
# define NCA_FS_ENTRY_BLOCK_OFFSET(x) ((u64)(x) * NCA_FS_ENTRY_BLOCK_SIZE)
2020-04-11 06:28:26 +01:00
2020-04-21 11:23:33 +01:00
# define NCA_AES_XTS_SECTOR_SIZE 0x200
# define NCA_NCA0_FS_HEADER_AES_XTS_SECTOR(x) (((x) - NCA_HEADER_LENGTH) >> 9)
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaDistributionType_Download = 0 ,
NcaDistributionType_GameCard = 1
} NcaDistributionType ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
2020-05-01 16:06:24 +01:00
NcaContentType_Program = 0 ,
NcaContentType_Meta = 1 ,
NcaContentType_Control = 2 ,
NcaContentType_Manual = 3 ,
NcaContentType_Data = 4 ,
NcaContentType_PublicData = 5
2020-04-15 21:50:07 +01:00
} NcaContentType ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaKeyGenerationOld_100_230 = 0 ,
NcaKeyGenerationOld_300 = 2
} NcaKeyGenerationOld ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaKeyAreaEncryptionKeyIndex_Application = 0 ,
NcaKeyAreaEncryptionKeyIndex_Ocean = 1 ,
NcaKeyAreaEncryptionKeyIndex_System = 2
} NcaKeyAreaEncryptionKeyIndex ;
2020-04-11 06:28:26 +01:00
2020-04-27 23:37:15 +01:00
typedef struct {
u8 relstep ;
u8 micro ;
u8 minor ;
u8 major ;
} NcaSdkAddOnVersion ;
/// 'NcaKeyGeneration_Current' will always point to the last known key generation value.
2020-04-11 06:28:26 +01:00
typedef enum {
2020-04-28 03:33:38 +01:00
NcaKeyGeneration_301_302 = 3 ,
NcaKeyGeneration_400_410 = 4 ,
NcaKeyGeneration_500_510 = 5 ,
NcaKeyGeneration_600_610 = 6 ,
NcaKeyGeneration_620 = 7 ,
NcaKeyGeneration_700_801 = 8 ,
NcaKeyGeneration_810_811 = 9 ,
NcaKeyGeneration_900_901 = 10 ,
2020-07-03 10:31:22 +01:00
NcaKeyGeneration_910_1004 = 11 ,
NcaKeyGeneration_Current = NcaKeyGeneration_910_1004
2020-04-15 21:50:07 +01:00
} NcaKeyGeneration ;
typedef struct {
u32 start_block_offset ; ///< Expressed in NCA_FS_ENTRY_BLOCK_SIZE blocks.
u32 end_block_offset ; ///< Expressed in NCA_FS_ENTRY_BLOCK_SIZE blocks.
u8 enable_entry ;
u8 reserved [ 0x7 ] ;
} NcaFsEntry ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u8 hash [ SHA256_HASH_SIZE ] ;
} NcaFsHash ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u8 key [ 0x10 ] ;
2020-04-19 23:44:22 +01:00
} NcaKey ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaFsType_RomFs = 0 ,
NcaFsType_PartitionFs = 1
} NcaFsType ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaHashType_Auto = 0 ,
NcaHashType_None = 1 ,
NcaHashType_HierarchicalSha256 = 2 , ///< Used by NcaFsType_PartitionFs.
NcaHashType_HierarchicalIntegrity = 3 ///< Used by NcaFsType_RomFs.
} NcaHashType ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
typedef enum {
NcaEncryptionType_Auto = 0 ,
NcaEncryptionType_None = 1 ,
NcaEncryptionType_AesXts = 2 ,
NcaEncryptionType_AesCtr = 3 ,
2020-04-29 13:59:28 +01:00
NcaEncryptionType_AesCtrEx = 4
2020-04-15 21:50:07 +01:00
} NcaEncryptionType ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u64 offset ;
u64 size ;
} NcaHierarchicalSha256LayerInfo ;
2020-04-11 06:28:26 +01:00
2020-04-20 11:39:41 +01:00
/// Used for NcaFsType_PartitionFs and NCA0 NcaFsType_RomFsRomFS.
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u8 master_hash [ SHA256_HASH_SIZE ] ;
u32 hash_block_size ;
u32 layer_count ;
NcaHierarchicalSha256LayerInfo hash_data_layer_info ;
NcaHierarchicalSha256LayerInfo hash_target_layer_info ;
} NcaHierarchicalSha256 ;
2020-04-11 06:28:26 +01:00
typedef struct {
u64 offset ;
u64 size ;
2020-04-21 11:23:33 +01:00
u32 block_size ; ///< Use NCA_IVFC_BLOCK_SIZE to calculate the actual block size using this value.
2020-04-15 21:50:07 +01:00
u8 reserved [ 0x4 ] ;
} NcaHierarchicalIntegrityLayerInfo ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
/// Used for NcaFsType_RomFs.
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-26 09:35:01 +01:00
u32 magic ; ///< "IVFC".
2020-04-15 21:50:07 +01:00
u32 version ;
u32 master_hash_size ;
u32 layer_count ;
2020-04-26 09:35:01 +01:00
NcaHierarchicalIntegrityLayerInfo hash_data_layer_info [ NCA_IVFC_HASH_DATA_LAYER_COUNT ] ;
2020-04-15 21:50:07 +01:00
NcaHierarchicalIntegrityLayerInfo hash_target_layer_info ;
u8 signature_salt [ 0x20 ] ;
u8 master_hash [ 0x20 ] ;
} NcaHierarchicalIntegrity ;
2020-04-11 06:28:26 +01:00
typedef struct {
union {
struct {
2020-04-20 11:39:41 +01:00
///< Used if hash_type == NcaHashType_HierarchicalSha256 (NcaFsType_PartitionFs and NCA0 NcaFsType_RomFs).
2020-04-15 21:50:07 +01:00
NcaHierarchicalSha256 hierarchical_sha256 ;
u8 reserved_1 [ 0xB0 ] ;
2020-04-11 06:28:26 +01:00
} ;
struct {
2020-04-15 21:50:07 +01:00
///< Used if hash_type == NcaHashType_HierarchicalIntegrity (NcaFsType_RomFs).
NcaHierarchicalIntegrity hierarchical_integrity ;
u8 reserved_2 [ 0x18 ] ;
2020-04-11 06:28:26 +01:00
} ;
} ;
2020-04-15 21:50:07 +01:00
} NcaHashInfo ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u32 magic ; ///< "BKTR".
u32 bucket_count ;
u32 entry_count ;
u8 reserved [ 0x4 ] ;
} NcaBucketTreeHeader ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
/// Only used for NcaEncryptionType_AesCtrEx (PatchRomFs).
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u64 indirect_offset ;
u64 indirect_size ;
NcaBucketTreeHeader indirect_header ;
u64 aes_ctr_ex_offset ;
u64 aes_ctr_ex_size ;
NcaBucketTreeHeader aes_ctr_ex_header ;
} NcaPatchInfo ;
2020-04-11 06:28:26 +01:00
2020-04-15 21:50:07 +01:00
/// Format unknown.
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u8 unknown [ 0x30 ] ;
} NcaSparseInfo ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-04-15 21:50:07 +01:00
u16 version ;
u8 fs_type ; ///< NcaFsType.
u8 hash_type ; ///< NcaHashType.
u8 encryption_type ; ///< NcaEncryptionType.
u8 reserved_1 [ 0x3 ] ;
NcaHashInfo hash_info ;
NcaPatchInfo patch_info ;
union {
u8 section_ctr [ 0x8 ] ;
struct {
u32 generation ;
u32 secure_value ;
} ;
} ;
NcaSparseInfo sparse_info ;
u8 reserved_2 [ 0x88 ] ;
} NcaFsHeader ;
typedef struct {
2020-04-27 23:37:15 +01:00
u8 main_signature [ 0x100 ] ; ///< RSA-PSS signature over header with fixed key.
u8 acid_signature [ 0x100 ] ; ///< RSA-PSS signature over header with key in NPDM.
u32 magic ; ///< "NCA0" / "NCA2" / "NCA3".
u8 distribution_type ; ///< NcaDistributionType.
u8 content_type ; ///< NcaContentType.
u8 key_generation_old ; ///< NcaKeyGenerationOld.
u8 kaek_index ; ///< NcaKeyAreaEncryptionKeyIndex.
2020-04-15 21:50:07 +01:00
u64 content_size ;
u64 program_id ;
u32 content_index ;
2020-04-27 23:37:15 +01:00
NcaSdkAddOnVersion sdk_addon_version ;
u8 key_generation ; ///< NcaKeyGeneration.
2020-04-15 21:50:07 +01:00
u8 main_signature_key_generation ;
u8 reserved_1 [ 0xE ] ;
2020-04-27 23:37:15 +01:00
FsRightsId rights_id ; ///< Used for titlekey crypto.
NcaFsEntry fs_entries [ 4 ] ; ///< Start and end offsets for each NCA FS section.
NcaFsHash fs_hashes [ 4 ] ; ///< SHA-256 hashes calculated over each NCA FS section header.
NcaKey encrypted_keys [ 4 ] ; ///< Only the encrypted key at index #2 is used. The other three are zero filled before the key area is encrypted.
2020-04-15 21:50:07 +01:00
u8 reserved_2 [ 0xC0 ] ;
2020-04-27 23:37:15 +01:00
NcaFsHeader fs_headers [ 4 ] ; /// NCA FS section headers.
2020-04-15 21:50:07 +01:00
} NcaHeader ;
2020-04-11 06:28:26 +01:00
2020-04-20 11:39:41 +01:00
typedef enum {
NcaVersion_Nca0 = 0 ,
NcaVersion_Nca2 = 1 ,
NcaVersion_Nca3 = 2
} NcaVersion ;
2020-04-11 06:28:26 +01:00
2020-04-20 11:39:41 +01:00
typedef enum {
2020-04-22 21:53:20 +01:00
NcaFsSectionType_PartitionFs = 0 , ///< NcaFsType_PartitionFs + NcaHashType_HierarchicalSha256.
NcaFsSectionType_RomFs = 1 , ///< NcaFsType_RomFs + NcaHashType_HierarchicalIntegrity.
NcaFsSectionType_PatchRomFs = 2 , ///< NcaFsType_RomFs + NcaHashType_HierarchicalIntegrity + NcaEncryptionType_AesCtrEx.
NcaFsSectionType_Nca0RomFs = 3 , ///< NcaFsType_RomFs + NcaHashType_HierarchicalSha256 + NcaVersion_Nca0.
NcaFsSectionType_Invalid = 4
} NcaFsSectionType ;
2020-04-11 06:28:26 +01:00
typedef struct {
2020-07-06 01:10:07 +01:00
bool enabled ;
2020-04-22 21:53:20 +01:00
void * nca_ctx ; ///< NcaContext. Used to perform NCA reads.
2020-04-20 11:39:41 +01:00
u8 section_num ;
2020-04-21 11:23:33 +01:00
u64 section_offset ;
u64 section_size ;
2020-04-22 21:53:20 +01:00
u8 section_type ; ///< NcaFsSectionType.
u8 encryption_type ; ///< NcaEncryptionType.
2020-04-20 11:39:41 +01:00
NcaFsHeader * header ;
2020-04-22 21:53:20 +01:00
u8 ctr [ 0x10 ] ; ///< Used to update the AES CTR context IV based on the desired offset.
2020-04-20 11:39:41 +01:00
Aes128CtrContext ctr_ctx ;
2020-04-22 21:53:20 +01:00
Aes128XtsContext xts_decrypt_ctx ;
Aes128XtsContext xts_encrypt_ctx ;
2020-04-21 11:23:33 +01:00
} NcaFsSectionContext ;
2020-04-15 21:50:07 +01:00
typedef struct {
u8 storage_id ; ///< NcmStorageId.
NcmContentStorage * ncm_storage ; ///< Pointer to a NcmContentStorage instance. Used to read NCA data.
2020-04-16 11:13:11 +01:00
u64 gamecard_offset ; ///< Used to read NCA data from a gamecard using a FsStorage instance when storage_id == NcmStorageId_GameCard.
2020-04-21 11:23:33 +01:00
NcmContentId content_id ; ///< Also used to read NCA data.
char content_id_str [ 0x21 ] ;
2020-05-03 00:40:50 +01:00
u8 hash [ 0x20 ] ; ///< Manually calculated (if needed).
2020-04-15 21:50:07 +01:00
char hash_str [ 0x41 ] ;
u8 format_version ; ///< NcaVersion.
2020-05-03 00:40:50 +01:00
u8 content_type ; ///< NcmContentType. Retrieved from NcmContentInfo.
u64 content_size ; ///< Retrieved from NcmContentInfo.
2020-04-15 21:50:07 +01:00
u8 key_generation ; ///< NcaKeyGenerationOld / NcaKeyGeneration. Retrieved from the decrypted header.
2020-05-03 00:40:50 +01:00
u8 id_offset ; ///< Retrieved from NcmContentInfo.
2020-04-15 21:50:07 +01:00
bool rights_id_available ;
2020-07-06 01:10:07 +01:00
bool titlekey_retrieved ;
2020-04-21 11:23:33 +01:00
u8 titlekey [ 0x10 ] ;
2020-04-15 21:50:07 +01:00
bool dirty_header ;
2020-04-20 11:39:41 +01:00
NcaHeader header ;
2020-04-21 11:23:33 +01:00
NcaFsSectionContext fs_contexts [ 4 ] ;
2020-04-20 11:39:41 +01:00
NcaKey decrypted_keys [ 4 ] ;
2020-04-15 21:50:07 +01:00
} NcaContext ;
2020-04-11 06:28:26 +01:00
2020-04-28 09:58:17 +01:00
typedef struct {
u64 offset ; ///< New layer data offset (relative to the start of the NCA content file).
u64 size ; ///< New layer data size.
u8 * data ; ///< New layer data.
} NcaHashInfoLayerPatch ;
typedef struct {
NcaHashInfoLayerPatch hash_data_layer_patch ;
NcaHashInfoLayerPatch hash_target_layer_patch ;
} NcaHierarchicalSha256Patch ;
typedef struct {
NcaHashInfoLayerPatch hash_data_layer_patch [ NCA_IVFC_HASH_DATA_LAYER_COUNT ] ;
NcaHashInfoLayerPatch hash_target_layer_patch ;
} NcaHierarchicalIntegrityPatch ;
2020-04-22 21:53:20 +01:00
/// Functions to control the internal heap buffer used by NCA FS section crypto operations.
2020-04-21 11:23:33 +01:00
/// Must be called at startup.
bool ncaAllocateCryptoBuffer ( void ) ;
void ncaFreeCryptoBuffer ( void ) ;
2020-04-24 10:38:13 +01:00
/// Initializes a NCA context.
2020-04-21 11:23:33 +01:00
/// If 'storage_id' != NcmStorageId_GameCard, the 'ncm_storage' argument must point to a valid NcmContentStorage instance, previously opened using the same NcmStorageId value.
/// If 'storage_id' == NcmStorageId_GameCard, the 'hfs_partition_type' argument must be a valid GameCardHashFileSystemPartitionType value.
2020-07-03 10:31:22 +01:00
/// If the NCA holds a populated Rights ID field, and if the Ticket element pointed to by 'tik' hasn't been filled, ticket data will be retrieved.
2020-07-06 01:10:07 +01:00
/// If ticket data can't be retrieved, the context will still be initialized, but anything that involves working with plaintext FS section blocks won't be possible (e.g. ncaReadFsSection()).
2020-05-03 00:40:50 +01:00
bool ncaInitializeContext ( NcaContext * out , u8 storage_id , NcmContentStorage * ncm_storage , u8 hfs_partition_type , const NcmContentInfo * content_info , Ticket * tik ) ;
2020-04-21 11:23:33 +01:00
2020-04-22 21:53:20 +01:00
/// Reads raw encrypted data from a NCA using an input context, previously initialized by ncaInitializeContext().
2020-04-28 00:30:35 +01:00
/// Input offset must be relative to the start of the NCA content file.
2020-04-22 21:53:20 +01:00
bool ncaReadContentFile ( NcaContext * ctx , void * out , u64 read_size , u64 offset ) ;
2020-04-19 23:44:22 +01:00
2020-04-22 21:53:20 +01:00
/// Reads decrypted data from a NCA FS section using an input context.
2020-04-21 11:23:33 +01:00
/// Input offset must be relative to the start of the NCA FS section.
2020-04-30 09:25:03 +01:00
/// If dealing with Patch RomFS sections, this function should only be used when *not* reading BKTR AesCtrEx storage data. Use ncaReadAesCtrExStorageFromBktrSection() for that.
2020-04-21 11:23:33 +01:00
bool ncaReadFsSection ( NcaFsSectionContext * ctx , void * out , u64 read_size , u64 offset ) ;
2020-04-30 09:25:03 +01:00
/// Reads decrypted BKTR AesCtrEx storage data from a NCA Patch RomFS section using an input context and a AesCtrEx CTR value.
/// Input offset must be relative to the start of the NCA FS section.
bool ncaReadAesCtrExStorageFromBktrSection ( NcaFsSectionContext * ctx , void * out , u64 read_size , u64 offset , u32 ctr_val ) ;
2020-04-22 21:53:20 +01:00
/// Returns a pointer to a heap-allocated buffer used to encrypt the input plaintext data, based on the encryption type used by the input NCA FS section, as well as its offset and size.
/// Input offset must be relative to the start of the NCA FS section.
/// Output size and offset are guaranteed to be aligned to the AES sector size used by the encryption type from the FS section.
2020-04-29 10:54:40 +01:00
/// Output offset is relative to the start of the NCA content file, making it easier to use the output encrypted block to seamlessly replace data while dumping a NCA.
2020-04-30 09:25:03 +01:00
/// This function isn't compatible with Patch RomFS sections.
2020-04-26 09:35:01 +01:00
void * ncaGenerateEncryptedFsSectionBlock ( NcaFsSectionContext * ctx , const void * data , u64 data_size , u64 data_offset , u64 * out_block_size , u64 * out_block_offset ) ;
2020-04-21 11:23:33 +01:00
2020-04-28 09:58:17 +01:00
/// Generates HierarchicalSha256 FS section patch data, which can be used to replace NCA data in content dumping operations.
/// Input offset must be relative to the start of the HierarchicalSha256 hash target layer (actual underlying FS).
/// Bear in mind that this function recalculates both the NcaHashInfo block master hash and the NCA FS header hash from the NCA header, and enables the 'dirty_header' flag from the NCA context.
2020-04-29 10:54:40 +01:00
/// As such, this function is not designed to generate more than one patch per HierarchicalSha256 FS section.
2020-04-28 09:58:17 +01:00
bool ncaGenerateHierarchicalSha256Patch ( NcaFsSectionContext * ctx , const void * data , u64 data_size , u64 data_offset , NcaHierarchicalSha256Patch * out ) ;
/// Cleanups a previously generated NcaHierarchicalSha256Patch.
NX_INLINE void ncaFreeHierarchicalSha256Patch ( NcaHierarchicalSha256Patch * patch )
{
if ( ! patch ) return ;
if ( patch - > hash_data_layer_patch . data ) free ( patch - > hash_data_layer_patch . data ) ;
if ( patch - > hash_target_layer_patch . data ) free ( patch - > hash_target_layer_patch . data ) ;
memset ( patch , 0 , sizeof ( NcaHierarchicalSha256Patch ) ) ;
}
2020-04-29 10:54:40 +01:00
/// Generates HierarchicalIntegrity FS section patch data, which can be used to replace NCA data in content dumping operations.
/// Input offset must be relative to the start of the HierarchicalIntegrity hash target layer (actual underlying FS).
/// Bear in mind that this function recalculates both the NcaHashInfo block master hash and the NCA FS header hash from the NCA header, and enables the 'dirty_header' flag from the NCA context.
/// As such, this function is not designed to generate more than one patch per HierarchicalIntegrity FS section.
bool ncaGenerateHierarchicalIntegrityPatch ( NcaFsSectionContext * ctx , const void * data , u64 data_size , u64 data_offset , NcaHierarchicalIntegrityPatch * out ) ;
2020-04-28 09:58:17 +01:00
/// Cleanups a previously generated NcaHierarchicalIntegrityPatch.
NX_INLINE void ncaFreeHierarchicalIntegrityPatch ( NcaHierarchicalIntegrityPatch * patch )
{
if ( ! patch ) return ;
2020-04-29 10:54:40 +01:00
for ( u8 i = 0 ; i < ( NCA_IVFC_HASH_DATA_LAYER_COUNT + 1 ) ; i + + )
2020-04-28 09:58:17 +01:00
{
2020-04-29 10:54:40 +01:00
NcaHashInfoLayerPatch * layer_patch = ( i < NCA_IVFC_HASH_DATA_LAYER_COUNT ? & ( patch - > hash_data_layer_patch [ i ] ) : & ( patch - > hash_target_layer_patch ) ) ;
if ( layer_patch - > data ) free ( layer_patch - > data ) ;
2020-04-28 09:58:17 +01:00
}
memset ( patch , 0 , sizeof ( NcaHierarchicalIntegrityPatch ) ) ;
}
2020-04-21 11:23:33 +01:00
2020-04-19 23:44:22 +01:00
2020-04-21 11:23:33 +01:00
bool ncaEncryptKeyArea ( NcaContext * nca_ctx ) ;
bool ncaEncryptHeader ( NcaContext * ctx ) ;
2020-04-19 23:44:22 +01:00
2020-04-21 11:23:33 +01:00
2020-05-03 00:40:50 +01:00
/// Miscellaneous functions.
2020-04-21 11:23:33 +01:00
2020-04-26 09:35:01 +01:00
NX_INLINE void ncaConvertNcmContentSizeToU64 ( const u8 * size , u64 * out )
2020-04-15 21:50:07 +01:00
{
2020-04-24 10:38:13 +01:00
if ( ! size | | ! out ) return ;
* out = 0 ;
memcpy ( out , size , 6 ) ;
2020-04-15 21:50:07 +01:00
}
2020-04-11 06:28:26 +01:00
2020-04-26 09:35:01 +01:00
NX_INLINE void ncaConvertU64ToNcmContentSize ( const u64 * size , u8 * out )
2020-04-15 21:50:07 +01:00
{
2020-04-22 21:53:20 +01:00
if ( size & & out ) memcpy ( out , size , 6 ) ;
2020-04-15 21:50:07 +01:00
}
2020-04-11 06:28:26 +01:00
2020-04-26 09:35:01 +01:00
NX_INLINE void ncaSetDownloadDistributionType ( NcaContext * ctx )
2020-04-15 21:50:07 +01:00
{
2020-04-24 10:38:13 +01:00
if ( ! ctx | | ctx - > header . distribution_type = = NcaDistributionType_Download ) return ;
ctx - > header . distribution_type = NcaDistributionType_Download ;
ctx - > dirty_header = true ;
2020-04-15 21:50:07 +01:00
}
2020-04-11 06:28:26 +01:00
2020-04-26 09:35:01 +01:00
NX_INLINE void ncaWipeRightsId ( NcaContext * ctx )
2020-04-15 21:50:07 +01:00
{
2020-04-24 10:38:13 +01:00
if ( ! ctx | | ! ctx - > rights_id_available ) return ;
memset ( & ( ctx - > header . rights_id ) , 0 , sizeof ( FsRightsId ) ) ;
ctx - > dirty_header = true ;
2020-04-15 21:50:07 +01:00
}
2020-04-11 06:28:26 +01:00
2020-04-26 09:35:01 +01:00
NX_INLINE bool ncaValidateHierarchicalSha256Offsets ( NcaHierarchicalSha256 * hierarchical_sha256 , u64 section_size )
{
if ( ! hierarchical_sha256 | | ! section_size | | ! hierarchical_sha256 - > hash_block_size | | hierarchical_sha256 - > layer_count ! = NCA_HIERARCHICAL_SHA256_LAYER_COUNT ) return false ;
for ( u8 i = 0 ; i < NCA_HIERARCHICAL_SHA256_LAYER_COUNT ; i + + )
{
NcaHierarchicalSha256LayerInfo * layer_info = ( i = = 0 ? & ( hierarchical_sha256 - > hash_data_layer_info ) : & ( hierarchical_sha256 - > hash_target_layer_info ) ) ;
if ( layer_info - > offset > = section_size | | ! layer_info - > size | | ( layer_info - > offset + layer_info - > size ) > section_size ) return false ;
}
return true ;
}
NX_INLINE bool ncaValidateHierarchicalIntegrityOffsets ( NcaHierarchicalIntegrity * hierarchical_integrity , u64 section_size )
{
if ( ! hierarchical_integrity | | ! section_size | | __builtin_bswap32 ( hierarchical_integrity - > magic ) ! = NCA_IVFC_MAGIC | | ! hierarchical_integrity - > master_hash_size | | \
2020-04-27 23:37:15 +01:00
hierarchical_integrity - > layer_count ! = NCA_IVFC_LAYER_COUNT ) return false ;
2020-04-26 09:35:01 +01:00
for ( u8 i = 0 ; i < ( NCA_IVFC_HASH_DATA_LAYER_COUNT + 1 ) ; i + + )
{
NcaHierarchicalIntegrityLayerInfo * layer_info = ( i < NCA_IVFC_HASH_DATA_LAYER_COUNT ? & ( hierarchical_integrity - > hash_data_layer_info [ i ] ) : & ( hierarchical_integrity - > hash_target_layer_info ) ) ;
if ( layer_info - > offset > = section_size | | ! layer_info - > size | | ! layer_info - > block_size | | ( layer_info - > offset + layer_info - > size ) > section_size ) return false ;
}
return true ;
}
2020-04-15 21:50:07 +01:00
# endif /* __NCA_H__ */