Files
NDecrypt/NDecrypt.Core/ThreeDSProcessor.cs

1304 lines
50 KiB
C#
Raw Normal View History

using System;
using System.IO;
2025-02-19 15:47:05 -05:00
using System.Text;
2025-09-29 07:47:32 -04:00
using SabreTools.Data.Models.N3DS;
2025-10-07 12:56:23 -04:00
using SabreTools.IO.Encryption;
2024-10-12 00:30:02 -04:00
using SabreTools.IO.Extensions;
2024-11-13 21:11:26 -05:00
using SabreTools.Serialization.Wrappers;
2025-09-29 07:47:32 -04:00
using static SabreTools.Data.Models.N3DS.Constants;
2024-10-13 23:59:44 -04:00
namespace NDecrypt.Core
{
2026-03-22 01:35:00 -04:00
// TODO: Strip this out when Serialization is updated
2026-03-22 00:41:08 -04:00
public class ThreeDSProcessor : ICartProcessor
{
2021-09-26 21:36:55 -07:00
/// <summary>
2026-03-22 00:38:38 -04:00
/// AES Hardware Constant
2021-09-26 21:36:55 -07:00
/// </summary>
2026-03-22 00:38:38 -04:00
/// TODO: Validate this value on assignment
public byte[] AESHardwareConstant { get; set; } = [];
/// <summary>
/// KeyX 0x18 (New 3DS 9.3)
/// </summary>
public byte[] KeyX0x18
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedKeyX0x18))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// Dev KeyX 0x18 (New 3DS 9.3)
/// </summary>
public byte[] DevKeyX0x18
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedDevKeyX0x18))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// KeyX 0x1B (New 3DS 9.6)
/// </summary>
public byte[] KeyX0x1B
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedKeyX0x1B))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// Dev KeyX 0x1B New 3DS 9.6)
/// </summary>
public byte[] DevKeyX0x1B
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedDevKeyX0x1B))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// KeyX 0x25 (> 7.x)
/// </summary>
public byte[] KeyX0x25
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedKeyX0x25))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// Dev KeyX 0x25 (> 7.x)
/// </summary>
public byte[] DevKeyX0x25
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedDevKeyX0x25))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// KeyX 0x2C (< 6.x)
/// </summary>
public byte[] KeyX0x2C
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedKeyX0x2C))
return;
// Assign the validated value
field = value;
}
} = [];
/// <summary>
/// Dev KeyX 0x2C (< 6.x)
/// </summary>
public byte[] DevKeyX0x2C
{
get;
set
{
// Ignore missing key data
if (value.Length == 0)
return;
// Validate the key data
2026-03-22 01:57:12 -04:00
var cipher = AESCTR.CreateEncryptionCipher(value, TestIV);
2026-03-22 00:38:38 -04:00
byte[] actual = cipher.ProcessBytes(TestPattern);
if (!actual.EqualsExactly(ExpectedDevKeyX0x2C))
return;
// Assign the validated value
field = value;
}
} = [];
2021-09-26 21:36:55 -07:00
2024-10-12 02:20:17 -04:00
/// <summary>
/// Indicates if development images are expected
/// </summary>
private readonly bool _development;
2026-03-22 00:38:38 -04:00
#region Internal Test Values
/// <summary>
/// Initial value for key validation tests
/// </summary>
private static readonly byte[] TestIV =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
];
/// <summary>
/// Pattern to use for key validation tests
/// </summary>
private static readonly byte[] TestPattern =
[
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
];
/// <summary>
/// Expected output value for KeyX0x18
/// </summary>
private static readonly byte[] ExpectedKeyX0x18 =
[
0x06, 0xF1, 0xB2, 0x3B, 0x12, 0xAD, 0x80, 0xC1,
0x13, 0xC6, 0x18, 0x3D, 0x27, 0xB8, 0xB9, 0x95,
0x49, 0x73, 0x59, 0x82, 0xEF, 0xFE, 0x16, 0x48,
0x91, 0x2A, 0x89, 0x55, 0x9A, 0xDC, 0x3C, 0xA0,
0x84, 0x46, 0x14, 0xE0, 0x16, 0x59, 0x8E, 0x4F,
0xC2, 0x6C, 0x52, 0xA4, 0x7D, 0xAD, 0x4F, 0x23,
0xF1, 0xC6, 0x99, 0x44, 0x39, 0xB7, 0x42, 0xF0,
0x1F, 0xBB, 0x02, 0xF6, 0x0A, 0x8A, 0xC2, 0x9A,
];
/// <summary>
/// Expected output value for DevKeyX0x18
/// </summary>
private static readonly byte[] ExpectedDevKeyX0x18 =
[
0x99, 0x6E, 0x3C, 0x54, 0x97, 0x3C, 0xEA, 0xE8,
0xBA, 0xAE, 0x18, 0x5C, 0x93, 0x27, 0x65, 0x50,
0xF6, 0x6D, 0x67, 0xD7, 0xEF, 0xBD, 0x7C, 0xCB,
0x8A, 0xC1, 0x1A, 0x54, 0xFC, 0x3B, 0x8B, 0x3A,
0x0E, 0xE5, 0xEF, 0x27, 0x4A, 0x73, 0x7E, 0x0A,
0x2E, 0x2E, 0x9D, 0xAF, 0x6C, 0x03, 0xF2, 0x91,
0xC4, 0xFA, 0x73, 0xFD, 0x6B, 0xA0, 0x07, 0xD4,
0x75, 0x5B, 0x6F, 0x2E, 0x8B, 0x68, 0x4C, 0xD1,
];
/// <summary>
/// Expected output value for KeyX0x1B
/// </summary>
private static readonly byte[] ExpectedKeyX0x1B =
[
0x0A, 0xE4, 0x79, 0x02, 0x1B, 0xFA, 0x25, 0x4B,
0x2D, 0x92, 0x4F, 0xA8, 0x41, 0x59, 0xCE, 0x10,
0x09, 0xE6, 0x08, 0x61, 0x23, 0xC7, 0xD2, 0x30,
0x84, 0x37, 0xD5, 0x49, 0x42, 0x94, 0xB2, 0x70,
0x6A, 0xF3, 0x75, 0xB0, 0x1F, 0x4F, 0xA1, 0xCE,
0x03, 0xA2, 0x6A, 0x19, 0x5D, 0x32, 0x0D, 0xB5,
0x79, 0xCD, 0xFD, 0xF0, 0xDE, 0x49, 0x26, 0x2D,
0x29, 0x36, 0x30, 0x69, 0x8B, 0x45, 0xE1, 0xFC,
];
/// <summary>
/// Expected output value for DevKeyX0x1B
/// </summary>
private static readonly byte[] ExpectedDevKeyX0x1B =
[
0x16, 0x4F, 0xD9, 0x58, 0xC9, 0x20, 0xB3, 0xED,
0xC4, 0xEB, 0x57, 0x39, 0x10, 0xEF, 0xA8, 0xCC,
0xE5, 0x49, 0xBF, 0x52, 0x10, 0xA9, 0xCC, 0xE1,
0x65, 0x3B, 0x2D, 0x51, 0x45, 0xFB, 0x60, 0x52,
0x3E, 0x29, 0xEB, 0xEB, 0x3F, 0xF2, 0x76, 0x08,
0x00, 0x05, 0x7F, 0x64, 0x29, 0x4A, 0x17, 0x22,
0x56, 0x7F, 0x49, 0x94, 0x1A, 0x8C, 0x56, 0x35,
0x38, 0xBE, 0xA4, 0x2E, 0x58, 0xD3, 0x81, 0x8C,
];
/// <summary>
/// Expected output value for KeyX0x25
/// </summary>
private static readonly byte[] ExpectedKeyX0x25 =
[
0x37, 0xBC, 0x73, 0xD6, 0xEE, 0x73, 0xE0, 0x94,
0x42, 0x84, 0x74, 0xE5, 0xD8, 0xFB, 0x5F, 0x65,
0xF4, 0xCF, 0x2E, 0xC1, 0x43, 0x48, 0x6C, 0xAA,
0xC8, 0xF9, 0x96, 0xE6, 0x33, 0xDD, 0xE7, 0xBF,
0xD2, 0x21, 0x89, 0x39, 0x13, 0xD1, 0xEC, 0xCA,
0x1D, 0x5D, 0x1F, 0x77, 0x95, 0xD2, 0x8B, 0x27,
0x92, 0x79, 0xC5, 0x1D, 0x72, 0xA7, 0x28, 0x57,
0x41, 0x0E, 0x46, 0xB8, 0x80, 0x7B, 0x7C, 0x0D,
];
/// <summary>
/// Expected output value for DevKeyX0x25
/// </summary>
private static readonly byte[] ExpectedDevKeyX0x25 =
[
0x71, 0x65, 0x30, 0xF2, 0x68, 0xEC, 0x65, 0x0A,
0x8C, 0x9E, 0xC5, 0x5A, 0xFA, 0x37, 0x8E, 0xDA,
0x7B, 0x58, 0x3B, 0x66, 0x7C, 0x9D, 0x16, 0xD9,
0x2D, 0x8F, 0xCF, 0x04, 0x66, 0x7F, 0x27, 0x41,
0xBF, 0x5F, 0x1E, 0x11, 0x4C, 0xD6, 0xB9, 0x0A,
0xC5, 0x42, 0xCF, 0x2B, 0x87, 0x6B, 0xD4, 0x72,
0x4D, 0x9C, 0x29, 0x2E, 0xF8, 0xB0, 0x6F, 0x22,
0x35, 0x5B, 0x96, 0x83, 0xD1, 0xE4, 0x5E, 0xDB,
];
/// <summary>
/// Expected output value for KeyX0x2C
/// </summary>
private static readonly byte[] ExpectedKeyX0x2C =
[
0xAE, 0x44, 0x20, 0xDB, 0xA5, 0x96, 0xDC, 0xF3,
0xD8, 0x23, 0x9E, 0x3C, 0x44, 0x73, 0x3D, 0xCD,
0x07, 0xD5, 0xF8, 0xD0, 0xC6, 0xB3, 0x5A, 0x80,
0xB5, 0x5A, 0x55, 0x30, 0x5D, 0x4A, 0xBE, 0x61,
0xBF, 0xEF, 0x64, 0x17, 0x28, 0xD6, 0x26, 0x52,
0x42, 0x4D, 0x8F, 0x1C, 0xBC, 0x63, 0xD3, 0x91,
0x7D, 0xA6, 0x4F, 0xAF, 0x26, 0x38, 0x60, 0xEE,
0x79, 0x92, 0x2F, 0xD8, 0xCA, 0x4E, 0xE7, 0xEC,
];
/// <summary>
/// Expected output value for DevKeyX0x2C
/// </summary>
private static readonly byte[] ExpectedDevKeyX0x2C =
[
0x5F, 0x73, 0xD5, 0x9A, 0x67, 0xFF, 0x8C, 0x12,
0x31, 0x58, 0x0B, 0x58, 0x46, 0xFE, 0x05, 0x16,
0x92, 0xE4, 0x84, 0x06, 0x18, 0x9B, 0x58, 0x91,
0xE7, 0xF8, 0xCD, 0xA9, 0x95, 0xAC, 0x07, 0xCD,
0x43, 0x20, 0x7A, 0x8C, 0xCC, 0xAB, 0x48, 0x50,
0x29, 0x2F, 0x96, 0x73, 0xB0, 0xD9, 0xE5, 0xCB,
0xE6, 0x9A, 0x0D, 0xF7, 0xD0, 0x1E, 0xC2, 0xEC,
0xC1, 0xE2, 0x8E, 0xEE, 0x89, 0xB9, 0xB1, 0x97,
];
#endregion
2026-03-22 00:41:08 -04:00
public ThreeDSProcessor(bool development)
{
2024-10-12 02:20:17 -04:00
_development = development;
}
2026-03-22 00:25:19 -04:00
#region Common
/// <summary>
/// Get KeyX value for a crypto method and development status combination
/// </summary>
private byte[] GetKeyXForCryptoMethod(CryptoMethod method)
{
switch (method)
{
case CryptoMethod.Original:
Console.WriteLine("Encryption Method: Key 0x2C");
2026-03-22 00:38:38 -04:00
return _development ? DevKeyX0x2C : KeyX0x2C;
2026-03-22 00:25:19 -04:00
case CryptoMethod.Seven:
Console.WriteLine("Encryption Method: Key 0x25");
2026-03-22 00:38:38 -04:00
return _development ? DevKeyX0x25 : KeyX0x25;
2026-03-22 00:25:19 -04:00
case CryptoMethod.NineThree:
Console.WriteLine("Encryption Method: Key 0x18");
2026-03-22 00:38:38 -04:00
return _development ? DevKeyX0x18 : KeyX0x18;
2026-03-22 00:25:19 -04:00
case CryptoMethod.NineSix:
Console.WriteLine("Encryption Method: Key 0x1B");
2026-03-22 00:38:38 -04:00
return _development ? DevKeyX0x1B : KeyX0x1B;
2026-03-22 00:25:19 -04:00
// This should never happen
default:
Console.WriteLine("Encryption Method: UNSUPPORTED");
return [];
}
}
#endregion
#region Decrypt
2024-10-12 00:59:07 -04:00
/// <inheritdoc/>
2025-09-06 18:09:54 -04:00
public bool DecryptFile(string input, string? output, bool force)
{
try
{
// If the output is provided, copy the input file
2026-01-25 17:20:06 -05:00
if (output is not null)
2025-09-06 19:35:51 -04:00
File.Copy(input, output, overwrite: true);
else
output = input;
// Open the output file for processing
using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
2024-08-08 15:49:36 -04:00
2024-08-08 14:01:42 -04:00
// Deserialize the cart information
2025-09-06 18:04:24 -04:00
var cart = N3DS.Create(reader);
2026-01-25 17:20:06 -05:00
if (cart?.Model is null)
{
2024-08-08 14:01:42 -04:00
Console.WriteLine("Error: Not a 3DS cart image!");
return false;
}
2024-10-13 23:08:48 -04:00
// Decrypt all 8 NCCH partitions
2025-09-06 18:04:24 -04:00
DecryptAllPartitions(cart, force, reader, writer);
return true;
}
catch
{
Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed.");
2021-09-26 23:20:06 -07:00
Console.WriteLine("Please check that the file was a valid 3DS or New 3DS cart image and try again.");
return false;
}
2020-12-15 13:30:28 -08:00
}
/// <summary>
2024-10-13 23:05:34 -04:00
/// Decrypt all partitions in the partition table of an NCSD header
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-12 01:32:32 -04:00
/// <param name="force">Indicates if the operation should be forced</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
private void DecryptAllPartitions(N3DS cart, bool force, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-08-08 11:53:47 -04:00
// Check the partitions table
2026-01-25 17:20:06 -05:00
if (cart.PartitionsTable is null || cart.Partitions is null)
2024-08-08 11:53:47 -04:00
{
2024-08-08 15:49:36 -04:00
Console.WriteLine("Invalid partitions table!");
return;
2024-08-08 11:53:47 -04:00
}
2024-08-08 15:49:36 -04:00
// Iterate over all 8 NCCH partitions
2024-10-13 20:36:42 -04:00
for (int p = 0; p < 8; p++)
2020-12-15 13:30:28 -08:00
{
2024-11-14 13:43:07 -05:00
var partition = cart.Partitions[p];
2026-01-25 17:20:06 -05:00
if (partition is null || partition.MagicID != NCCHMagicNumber)
2024-08-08 15:49:36 -04:00
{
2024-10-13 20:36:42 -04:00
Console.WriteLine($"Partition {p} Not found... Skipping...");
2024-08-08 15:49:36 -04:00
continue;
}
2020-12-15 13:30:28 -08:00
2024-11-14 13:43:07 -05:00
// Check the partition has data
var partitionEntry = cart.PartitionsTable[p];
2026-01-25 17:20:06 -05:00
if (partitionEntry is null || partitionEntry.Length == 0)
2024-11-14 13:43:07 -05:00
{
Console.WriteLine($"Partition {p} No data... Skipping...");
continue;
}
2024-10-13 23:05:34 -04:00
// Decrypt the partition, if possible
if (ShouldDecryptPartition(cart, p, force))
2025-09-06 18:04:24 -04:00
DecryptPartition(cart, p, reader, writer);
2020-12-15 13:30:28 -08:00
}
}
2024-10-13 20:36:42 -04:00
/// <summary>
2024-10-13 23:01:36 -04:00
/// Determine if the current partition should be decrypted
/// </summary>s
2024-11-13 21:11:26 -05:00
private static bool ShouldDecryptPartition(N3DS cart, int index, bool force)
2024-10-13 20:36:42 -04:00
{
// If we're forcing the operation, tell the user
if (force)
{
Console.WriteLine($"Partition {index} is not verified due to force flag being set.");
return true;
}
// If we're not forcing the operation, check if the 'NoCrypto' bit is set
2024-11-13 21:11:26 -05:00
else if (cart.PossiblyDecrypted(index))
2024-10-13 20:36:42 -04:00
{
2024-10-13 23:01:36 -04:00
Console.WriteLine($"Partition {index}: Already Decrypted?...");
2024-10-13 20:36:42 -04:00
return false;
}
// By default, it passes
return true;
}
2020-12-15 13:30:28 -08:00
/// <summary>
2024-10-13 22:56:32 -04:00
/// Decrypt a single partition
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:36:42 -04:00
/// <param name="index">Index of the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
private void DecryptPartition(N3DS cart, int index, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-10-13 22:56:32 -04:00
// Determine the keys needed for this partition
2026-03-22 01:06:36 -04:00
PartitionKeys? keys = GetDecryptionKeys(cart, index);
if (keys == null)
{
Console.WriteLine($"Partition {index} could not generate keys. Skipping...");
return;
}
2020-12-15 13:30:28 -08:00
2024-10-13 22:56:32 -04:00
// Decrypt the parts of the partition
2026-03-22 01:06:36 -04:00
DecryptExtendedHeader(cart, index, keys, reader, writer);
DecryptExeFS(cart, index, keys, reader, writer);
DecryptRomFS(cart, index, keys, reader, writer);
2024-10-13 22:49:31 -04:00
2024-10-13 22:56:32 -04:00
// Update the flags
2025-09-06 18:04:24 -04:00
UpdateDecryptCryptoAndMasks(cart, index, writer);
2024-10-13 22:56:32 -04:00
}
2024-10-13 22:49:31 -04:00
2020-12-15 13:30:28 -08:00
/// <summary>
2024-10-13 22:49:31 -04:00
/// Determine the set of keys to be used for decryption
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
private PartitionKeys? GetDecryptionKeys(N3DS cart, int index)
2020-12-15 13:30:28 -08:00
{
2024-10-13 11:48:30 -04:00
// Get the partition
2024-11-14 11:44:41 -05:00
var partition = cart.Partitions?[index];
2026-01-25 17:20:06 -05:00
if (partition?.Flags is null)
2026-03-22 01:06:36 -04:00
return null;
2020-12-15 13:30:28 -08:00
2024-10-13 11:48:30 -04:00
// Get partition-specific values
2026-03-22 00:25:19 -04:00
byte[]? signature = partition.RSA2048Signature;
2024-11-14 11:44:41 -05:00
BitMasks masks = cart.GetBitMasks(index);
CryptoMethod method = cart.GetCryptoMethod(index);
2020-12-15 13:30:28 -08:00
2024-10-13 11:48:30 -04:00
// Get the partition keys
2026-03-22 00:25:19 -04:00
byte[] keyX = GetKeyXForCryptoMethod(method);
2026-03-22 00:38:38 -04:00
byte[] keyX0x2C = _development ? DevKeyX0x2C : KeyX0x2C;
2026-03-22 01:06:36 -04:00
return new PartitionKeys(signature, masks, AESHardwareConstant, keyX, keyX0x2C);
2020-12-15 13:30:28 -08:00
}
/// <summary>
2024-10-13 22:10:59 -04:00
/// Decrypt the extended header, if it exists
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool DecryptExtendedHeader(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-08-08 15:49:36 -04:00
// Get required offsets
2024-11-13 21:11:26 -05:00
uint partitionOffset = cart.GetPartitionOffset(index);
2025-09-06 18:04:24 -04:00
if (partitionOffset == 0 || partitionOffset > reader.Length)
2024-10-13 22:10:59 -04:00
{
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} No Data... Skipping...");
2024-10-13 22:10:59 -04:00
return false;
}
2024-08-08 15:49:36 -04:00
2024-11-13 21:11:26 -05:00
uint extHeaderSize = cart.GetExtendedHeaderSize(index);
2024-10-13 22:10:59 -04:00
if (extHeaderSize == 0)
2020-12-15 13:30:28 -08:00
{
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} No Extended Header... Skipping...");
2024-10-13 22:10:59 -04:00
return false;
}
2020-12-15 13:30:28 -08:00
2024-10-13 22:10:59 -04:00
// Seek to the extended header
2025-09-06 18:04:24 -04:00
reader.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
writer.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
2020-12-15 13:30:28 -08:00
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index}: Decrypting - ExHeader");
2024-08-08 16:06:40 -04:00
2024-10-13 22:10:59 -04:00
// Create the Plain AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateDecryptionCipher(keys.NormalKey2C, cart.PlainIV(index));
2024-10-13 22:10:59 -04:00
// Process the extended header
2026-03-21 23:20:40 -04:00
AESCTR.PerformOperation(CXTExtendedDataHeaderLength, cipher, reader, writer, null);
2024-08-08 16:06:40 -04:00
#if NET6_0_OR_GREATER
2024-10-13 22:10:59 -04:00
// In .NET 6.0, this operation is not picked up by the reader, so we have to force it to reload its buffer
2025-09-06 18:04:24 -04:00
reader.Seek(0, SeekOrigin.Begin);
2024-08-08 16:06:40 -04:00
#endif
2025-09-06 18:04:24 -04:00
writer.Flush();
2024-10-13 22:10:59 -04:00
return true;
2020-12-15 13:30:28 -08:00
}
2024-08-08 15:49:36 -04:00
2020-12-15 13:30:28 -08:00
/// <summary>
2024-10-13 20:52:04 -04:00
/// Decrypt the ExeFS, if it exists
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool DecryptExeFS(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-10-13 22:49:31 -04:00
// Validate the ExeFS
2024-11-14 21:39:20 -05:00
uint exeFsHeaderOffset = cart.GetExeFSOffset(index);
2025-09-06 18:04:24 -04:00
if (exeFsHeaderOffset == 0 || exeFsHeaderOffset > reader.Length)
2024-10-13 20:52:04 -04:00
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return false;
2024-10-13 20:52:04 -04:00
}
2024-11-13 21:11:26 -05:00
uint exeFsSize = cart.GetExeFSSize(index);
2024-10-13 22:49:31 -04:00
if (exeFsSize == 0)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return false;
}
2024-10-13 20:52:04 -04:00
// Decrypt the filename table
2026-03-22 01:06:36 -04:00
DecryptExeFSFilenameTable(cart, index, keys, reader, writer);
2024-10-13 20:52:04 -04:00
// For all but the original crypto method, process each of the files in the table
2024-11-14 11:44:41 -05:00
if (cart.GetCryptoMethod(index) != CryptoMethod.Original)
2026-03-22 01:06:36 -04:00
DecryptExeFSFileEntries(cart, index, keys, reader, writer);
2024-10-13 20:52:04 -04:00
2024-11-14 21:39:20 -05:00
// Get the ExeFS files offset
uint exeFsFilesOffset = exeFsHeaderOffset + cart.MediaUnitSize;
2024-10-13 20:52:04 -04:00
// Seek to the ExeFS
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsFilesOffset, SeekOrigin.Begin);
writer.Seek(exeFsFilesOffset, SeekOrigin.Begin);
2020-12-15 13:30:28 -08:00
// Create the ExeFS AES cipher for this partition
2024-11-14 00:26:58 -05:00
uint ctroffsetE = cart.MediaUnitSize / 0x10;
2025-09-30 18:09:17 -04:00
byte[] exefsIVWithOffset = cart.ExeFSIV(index).Add(ctroffsetE);
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateDecryptionCipher(keys.NormalKey2C, exefsIVWithOffset);
2020-12-15 13:30:28 -08:00
// Setup and perform the decryption
2024-11-14 21:39:20 -05:00
exeFsSize -= cart.MediaUnitSize;
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(exeFsSize,
cipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting - {s}"));
2024-10-13 20:52:04 -04:00
return true;
2020-12-15 13:30:28 -08:00
}
/// <summary>
2024-10-13 20:52:04 -04:00
/// Decrypt the ExeFS Filename Table
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private void DecryptExeFSFilenameTable(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
// Get ExeFS offset
2024-11-13 21:11:26 -05:00
uint exeFsOffset = cart.GetExeFSOffset(index);
2025-09-06 18:04:24 -04:00
if (exeFsOffset == 0 || exeFsOffset > reader.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return;
}
2024-08-08 15:49:36 -04:00
2024-08-08 16:06:40 -04:00
// Seek to the ExeFS header
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsOffset, SeekOrigin.Begin);
writer.Seek(exeFsOffset, SeekOrigin.Begin);
2020-12-15 13:30:28 -08:00
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} ExeFS: Decrypting - ExeFS Filename Table");
2020-12-15 13:30:28 -08:00
2024-08-08 16:06:40 -04:00
// Create the ExeFS AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateDecryptionCipher(keys.NormalKey2C, cart.ExeFSIV(index));
2024-08-08 16:06:40 -04:00
// Process the filename table
2025-09-06 18:04:24 -04:00
byte[] readBytes = reader.ReadBytes((int)cart.MediaUnitSize);
2024-11-14 21:25:38 -05:00
byte[] processedBytes = cipher.ProcessBytes(readBytes);
2025-09-06 18:04:24 -04:00
writer.Write(processedBytes);
#if NET6_0_OR_GREATER
// In .NET 6.0, this operation is not picked up by the reader, so we have to force it to reload its buffer
2025-09-06 18:04:24 -04:00
reader.Seek(0, SeekOrigin.Begin);
#endif
2025-09-06 18:04:24 -04:00
writer.Flush();
2020-12-15 13:30:28 -08:00
}
/// <summary>
2024-10-13 20:52:04 -04:00
/// Decrypt the ExeFS file entries
2020-12-15 13:30:28 -08:00
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private void DecryptExeFSFileEntries(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2026-01-25 17:20:06 -05:00
if (cart.ExeFSHeaders is null || index < 0 || index > cart.ExeFSHeaders.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return;
}
2024-08-08 15:49:36 -04:00
// Reread the decrypted ExeFS header
uint exeFsHeaderOffset = cart.GetExeFSOffset(index);
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsHeaderOffset, SeekOrigin.Begin);
2025-09-29 07:47:32 -04:00
cart.ExeFSHeaders[index] = SabreTools.Serialization.Readers.N3DS.ParseExeFSHeader(reader);
2024-11-13 21:11:26 -05:00
// Get the ExeFS header
2024-11-14 11:44:41 -05:00
var exeFsHeader = cart.ExeFSHeaders[index];
2026-01-25 17:20:06 -05:00
if (exeFsHeader?.FileHeaders is null)
2020-12-15 13:30:28 -08:00
{
2024-11-14 11:56:18 -05:00
Console.WriteLine($"Partition {index} ExeFS header does not exist. Skipping...");
2024-10-13 20:52:04 -04:00
return;
2020-12-15 13:30:28 -08:00
}
2024-08-08 16:06:40 -04:00
// Get the ExeFS files offset
2024-11-13 21:11:26 -05:00
uint exeFsFilesOffset = exeFsHeaderOffset + cart.MediaUnitSize;
// Loop through and process all headers
for (int i = 0; i < exeFsHeader.FileHeaders.Length; i++)
2020-12-15 13:30:28 -08:00
{
2024-11-13 21:11:26 -05:00
// Only attempt to process code binary files
if (!cart.IsCodeBinary(index, i))
continue;
// Get the file header
var fileHeader = exeFsHeader.FileHeaders[i];
2026-01-25 17:20:06 -05:00
if (fileHeader is null)
2024-10-13 20:52:04 -04:00
continue;
2020-12-15 13:30:28 -08:00
2024-10-13 20:52:04 -04:00
// Create the ExeFS AES ciphers for this partition
2024-11-13 21:11:26 -05:00
uint ctroffset = (fileHeader.FileOffset + cart.MediaUnitSize) / 0x10;
2025-09-30 18:09:17 -04:00
byte[] exefsIVWithOffsetForHeader = cart.ExeFSIV(index).Add(ctroffset);
2026-03-22 01:06:36 -04:00
var firstCipher = AESCTR.CreateDecryptionCipher(keys.NormalKey, exefsIVWithOffsetForHeader);
var secondCipher = AESCTR.CreateEncryptionCipher(keys.NormalKey2C, exefsIVWithOffsetForHeader);
2020-12-15 13:30:28 -08:00
2024-10-13 20:52:04 -04:00
// Seek to the file entry
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsFilesOffset + fileHeader.FileOffset, SeekOrigin.Begin);
writer.Seek(exeFsFilesOffset + fileHeader.FileOffset, SeekOrigin.Begin);
// Setup and perform the encryption
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(fileHeader.FileSize,
firstCipher,
secondCipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} ExeFS: Decrypting - {fileHeader.FileName}...{s}"));
2024-10-13 20:52:04 -04:00
}
2020-12-15 13:30:28 -08:00
}
/// <summary>
/// Decrypt the RomFS, if it exists
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool DecryptRomFS(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-10-13 22:10:59 -04:00
// Validate the RomFS
2024-11-13 21:11:26 -05:00
uint romFsOffset = cart.GetRomFSOffset(index);
2025-09-06 18:04:24 -04:00
if (romFsOffset == 0 || romFsOffset > reader.Length)
2020-12-15 13:30:28 -08:00
{
2024-10-13 20:37:54 -04:00
Console.WriteLine($"Partition {index} RomFS: No Data... Skipping...");
2024-08-08 16:06:40 -04:00
return false;
2020-12-15 13:30:28 -08:00
}
2024-11-13 21:11:26 -05:00
uint romFsSize = cart.GetRomFSSize(index);
2024-10-13 22:10:59 -04:00
if (romFsSize == 0)
{
Console.WriteLine($"Partition {index} RomFS: No Data... Skipping...");
return false;
}
2024-08-08 16:06:40 -04:00
// Seek to the RomFS
2025-09-06 18:04:24 -04:00
reader.Seek(romFsOffset, SeekOrigin.Begin);
writer.Seek(romFsOffset, SeekOrigin.Begin);
2024-08-08 16:06:40 -04:00
// Create the RomFS AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateDecryptionCipher(keys.NormalKey, cart.RomFSIV(index));
2024-08-08 16:06:40 -04:00
// Setup and perform the decryption
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(romFsSize,
cipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} RomFS: Decrypting - {s}"));
2020-12-15 13:30:28 -08:00
2024-08-08 16:06:40 -04:00
return true;
2020-12-15 13:30:28 -08:00
}
/// <summary>
/// Update the CryptoMethod and BitMasks for the decrypted partition
/// </summary>
2024-08-08 15:49:36 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="writer">Stream representing the output</param>
private static void UpdateDecryptCryptoAndMasks(N3DS cart, int index, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-08-08 15:49:36 -04:00
// Get required offsets
2024-11-13 21:11:26 -05:00
uint partitionOffset = cart.GetPartitionOffset(index);
2024-08-08 15:49:36 -04:00
2024-08-08 16:15:19 -04:00
// Seek to the CryptoMethod location
2025-09-06 18:04:24 -04:00
writer.Seek(partitionOffset + 0x18B, SeekOrigin.Begin);
2024-08-08 16:15:19 -04:00
// Write the new CryptoMethod
2025-09-06 18:04:24 -04:00
writer.Write((byte)CryptoMethod.Original);
writer.Flush();
2020-12-15 13:30:28 -08:00
2024-08-08 16:06:40 -04:00
// Seek to the BitMasks location
2025-09-06 18:04:24 -04:00
writer.Seek(partitionOffset + 0x18F, SeekOrigin.Begin);
2024-08-08 16:06:40 -04:00
// Write the new BitMasks flag
2024-11-14 11:44:41 -05:00
BitMasks flag = cart.GetBitMasks(index);
2020-12-15 13:30:28 -08:00
flag &= (BitMasks)((byte)(BitMasks.FixedCryptoKey | BitMasks.NewKeyYGenerator) ^ 0xFF);
flag |= BitMasks.NoCrypto;
2025-09-06 18:04:24 -04:00
writer.Write((byte)flag);
writer.Flush();
2020-12-15 13:30:28 -08:00
}
#endregion
#region Encrypt
/// <inheritdoc/>
2025-09-06 18:09:54 -04:00
public bool EncryptFile(string input, string? output, bool force)
{
try
{
// If the output is provided, copy the input file
2026-01-25 17:20:06 -05:00
if (output is not null)
2025-09-06 19:35:51 -04:00
File.Copy(input, output, overwrite: true);
else
output = input;
// Open the output file for processing
using var reader = File.Open(output, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var writer = File.Open(output, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
// Deserialize the cart information
2025-09-06 18:04:24 -04:00
var cart = N3DS.Create(reader);
2026-01-25 17:20:06 -05:00
if (cart?.Model is null)
{
Console.WriteLine("Error: Not a 3DS cart image!");
return false;
}
// Encrypt all 8 NCCH partitions
2025-09-06 18:04:24 -04:00
EncryptAllPartitions(cart, force, reader, writer);
return true;
}
catch
{
Console.WriteLine($"An error has occurred. {output} may be corrupted if it was partially processed.");
Console.WriteLine("Please check that the file was a valid 3DS or New 3DS cart image and try again.");
return false;
}
}
2024-10-13 23:05:34 -04:00
/// <summary>
/// Encrypt all partitions in the partition table of an NCSD header
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="force">Indicates if the operation should be forced</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
private void EncryptAllPartitions(N3DS cart, bool force, Stream reader, Stream writer)
2024-10-13 23:05:34 -04:00
{
// Check the partitions table
2026-01-25 17:20:06 -05:00
if (cart.PartitionsTable is null || cart.Partitions is null)
2024-10-13 23:05:34 -04:00
{
Console.WriteLine("Invalid partitions table!");
return;
}
// Iterate over all 8 NCCH partitions
for (int p = 0; p < 8; p++)
{
// Check the partition exists
2024-11-14 13:43:07 -05:00
var partition = cart.Partitions[p];
2026-01-25 17:20:06 -05:00
if (partition is null || partition.MagicID != NCCHMagicNumber)
2024-10-13 23:05:34 -04:00
{
Console.WriteLine($"Partition {p} Not found... Skipping...");
continue;
}
2024-11-14 13:43:07 -05:00
// Check the partition has data
var partitionEntry = cart.PartitionsTable[p];
2026-01-25 17:20:06 -05:00
if (partitionEntry is null || partitionEntry.Length == 0)
2024-11-14 13:43:07 -05:00
{
Console.WriteLine($"Partition {p} No data... Skipping...");
continue;
}
2024-10-13 23:05:34 -04:00
// Encrypt the partition, if possible
if (ShouldEncryptPartition(cart, p, force))
2025-09-06 18:04:24 -04:00
EncryptPartition(cart, p, reader, writer);
2024-10-13 23:05:34 -04:00
}
}
2024-10-13 23:01:36 -04:00
/// <summary>
/// Determine if the current partition should be encrypted
/// </summary>
2024-11-13 21:11:26 -05:00
private static bool ShouldEncryptPartition(N3DS cart, int index, bool force)
2024-10-13 23:01:36 -04:00
{
// If we're forcing the operation, tell the user
if (force)
{
Console.WriteLine($"Partition {index} is not verified due to force flag being set.");
return true;
}
// If we're not forcing the operation, check if the 'NoCrypto' bit is set
2024-11-13 21:11:26 -05:00
else if (!cart.PossiblyDecrypted(index))
2024-10-13 23:01:36 -04:00
{
Console.WriteLine($"Partition {index}: Already Encrypted?...");
return false;
}
// By default, it passes
return true;
}
2024-10-13 22:56:32 -04:00
/// <summary>
/// Encrypt a single partition
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="index">Index of the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
private void EncryptPartition(N3DS cart, int index, Stream reader, Stream writer)
2024-10-13 22:56:32 -04:00
{
// Determine the keys needed for this partition
2026-03-22 01:06:36 -04:00
PartitionKeys? keys = GetEncryptionKeys(cart, index);
if (keys == null)
{
Console.WriteLine($"Partition {index} could not generate keys. Skipping...");
return;
}
2024-10-13 22:56:32 -04:00
// Encrypt the parts of the partition
2026-03-22 01:06:36 -04:00
EncryptExtendedHeader(cart, index, keys, reader, writer);
EncryptExeFS(cart, index, keys, reader, writer);
EncryptRomFS(cart, index, keys, reader, writer);
2024-10-13 22:56:32 -04:00
// Update the flags
2025-09-06 18:04:24 -04:00
UpdateEncryptCryptoAndMasks(cart, index, writer);
2024-10-13 22:56:32 -04:00
}
2024-10-13 22:49:31 -04:00
/// <summary>
/// Determine the set of keys to be used for encryption
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
private PartitionKeys? GetEncryptionKeys(N3DS cart, int index)
2024-10-13 22:49:31 -04:00
{
// Get the partition
2024-11-14 11:44:41 -05:00
var partition = cart.Partitions?[index];
2026-01-25 17:20:06 -05:00
if (partition is null)
2026-03-22 01:06:36 -04:00
return null;
2024-10-13 22:49:31 -04:00
2024-11-14 11:44:41 -05:00
// Get the backup header
var backupHeader = cart.BackupHeader;
2026-01-25 17:20:06 -05:00
if (backupHeader?.Flags is null)
2026-03-22 01:06:36 -04:00
return null;
2024-11-14 11:44:41 -05:00
2024-10-13 22:49:31 -04:00
// Get partition-specific values
2026-03-22 00:25:19 -04:00
byte[]? signature = partition.RSA2048Signature;
2024-11-14 11:44:41 -05:00
BitMasks masks = backupHeader.Flags.BitMasks;
2024-10-13 22:49:31 -04:00
CryptoMethod method = backupHeader.Flags.CryptoMethod;
// Get the partition keys
2026-03-22 00:25:19 -04:00
byte[] keyX = GetKeyXForCryptoMethod(method);
2026-03-22 00:38:38 -04:00
byte[] keyX0x2C = _development ? DevKeyX0x2C : KeyX0x2C;
2026-03-22 01:06:36 -04:00
return new PartitionKeys(signature, masks, AESHardwareConstant, keyX, keyX0x2C);
2024-10-13 22:49:31 -04:00
}
2024-10-13 22:10:59 -04:00
/// <summary>
/// Encrypt the extended header, if it exists
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool EncryptExtendedHeader(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2024-10-13 22:10:59 -04:00
{
// Get required offsets
2024-11-13 21:11:26 -05:00
uint partitionOffset = cart.GetPartitionOffset(index);
2025-09-06 18:04:24 -04:00
if (partitionOffset == 0 || partitionOffset > reader.Length)
2024-10-13 22:10:59 -04:00
{
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} No Data... Skipping...");
2024-10-13 22:10:59 -04:00
return false;
}
2024-11-13 21:11:26 -05:00
uint extHeaderSize = cart.GetExtendedHeaderSize(index);
2024-10-13 22:10:59 -04:00
if (extHeaderSize == 0)
{
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} No Extended Header... Skipping...");
2024-10-13 22:10:59 -04:00
return false;
}
// Seek to the extended header
2025-09-06 18:04:24 -04:00
reader.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
writer.Seek(partitionOffset + 0x200, SeekOrigin.Begin);
2024-10-13 22:10:59 -04:00
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index}: Encrypting - ExHeader");
2024-10-13 22:10:59 -04:00
// Create the Plain AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateEncryptionCipher(keys.NormalKey2C, cart.PlainIV(index));
2024-10-13 22:10:59 -04:00
// Process the extended header
2026-03-21 23:20:40 -04:00
AESCTR.PerformOperation(CXTExtendedDataHeaderLength, cipher, reader, writer, null);
2024-10-13 22:10:59 -04:00
#if NET6_0_OR_GREATER
// In .NET 6.0, this operation is not picked up by the reader, so we have to force it to reload its buffer
2025-09-06 18:04:24 -04:00
reader.Seek(0, SeekOrigin.Begin);
2024-10-13 22:10:59 -04:00
#endif
2025-09-06 18:04:24 -04:00
writer.Flush();
2024-10-13 22:10:59 -04:00
return true;
}
2020-12-15 13:30:28 -08:00
/// <summary>
/// Encrypt the ExeFS, if it exists
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool EncryptExeFS(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2026-01-25 17:20:06 -05:00
if (cart.ExeFSHeaders is null || index < 0 || index > cart.ExeFSHeaders.Length)
2020-12-15 13:30:28 -08:00
{
2024-10-13 20:37:54 -04:00
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return false;
2020-12-15 13:30:28 -08:00
}
2024-11-13 21:11:26 -05:00
// Get the ExeFS header
2024-11-14 11:44:41 -05:00
var exefsHeader = cart.ExeFSHeaders[index];
2026-01-25 17:20:06 -05:00
if (exefsHeader is null)
2024-10-13 22:49:31 -04:00
{
2024-11-14 11:56:18 -05:00
Console.WriteLine($"Partition {index} ExeFS header does not exist. Skipping...");
2024-10-13 22:49:31 -04:00
return false;
}
2020-12-15 13:30:28 -08:00
// For all but the original crypto method, process each of the files in the table
2024-11-14 11:44:41 -05:00
var backupHeader = cart.BackupHeader;
2024-08-08 15:49:36 -04:00
if (backupHeader!.Flags!.CryptoMethod != CryptoMethod.Original)
2026-03-22 01:06:36 -04:00
EncryptExeFSFileEntries(cart, index, keys, reader, writer);
2020-12-15 13:30:28 -08:00
// Encrypt the filename table
2026-03-22 01:06:36 -04:00
EncryptExeFSFilenameTable(cart, index, keys, reader, writer);
2020-12-15 13:30:28 -08:00
2024-11-14 21:39:20 -05:00
// Get the ExeFS files offset
2024-11-14 21:17:35 -05:00
uint exeFsHeaderOffset = cart.GetExeFSOffset(index);
uint exeFsFilesOffset = exeFsHeaderOffset + cart.MediaUnitSize;
2024-10-13 20:52:04 -04:00
// Seek to the ExeFS
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsFilesOffset, SeekOrigin.Begin);
writer.Seek(exeFsFilesOffset, SeekOrigin.Begin);
2024-10-13 20:52:04 -04:00
// Create the ExeFS AES cipher for this partition
2024-11-14 00:26:58 -05:00
uint ctroffsetE = cart.MediaUnitSize / 0x10;
2025-09-30 18:09:17 -04:00
byte[] exefsIVWithOffset = cart.ExeFSIV(index).Add(ctroffsetE);
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateEncryptionCipher(keys.NormalKey2C, exefsIVWithOffset);
2024-10-13 20:52:04 -04:00
// Setup and perform the encryption
2024-11-14 21:39:20 -05:00
uint exeFsSize = cart.GetExeFSSize(index) - cart.MediaUnitSize;
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(exeFsSize,
cipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting - {s}"));
2024-10-13 20:52:04 -04:00
return true;
}
/// <summary>
/// Encrypt the ExeFS Filename Table
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private void EncryptExeFSFilenameTable(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2024-10-13 20:52:04 -04:00
{
// Get ExeFS offset
2024-11-13 21:11:26 -05:00
uint exeFsOffset = cart.GetExeFSOffset(index);
2025-09-06 18:04:24 -04:00
if (exeFsOffset == 0 || exeFsOffset > reader.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return;
}
2024-10-13 20:52:04 -04:00
// Seek to the ExeFS header
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsOffset, SeekOrigin.Begin);
writer.Seek(exeFsOffset, SeekOrigin.Begin);
2024-10-13 20:52:04 -04:00
2024-11-14 13:43:07 -05:00
Console.WriteLine($"Partition {index} ExeFS: Encrypting - ExeFS Filename Table");
2024-10-13 20:52:04 -04:00
// Create the ExeFS AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateEncryptionCipher(keys.NormalKey2C, cart.ExeFSIV(index));
2024-10-13 20:52:04 -04:00
// Process the filename table
2025-09-06 18:04:24 -04:00
byte[] readBytes = reader.ReadBytes((int)cart.MediaUnitSize);
2024-11-14 21:25:38 -05:00
byte[] processedBytes = cipher.ProcessBytes(readBytes);
2025-09-06 18:04:24 -04:00
writer.Write(processedBytes);
2024-10-13 20:52:04 -04:00
#if NET6_0_OR_GREATER
// In .NET 6.0, this operation is not picked up by the reader, so we have to force it to reload its buffer
2025-09-06 18:04:24 -04:00
reader.Seek(0, SeekOrigin.Begin);
2024-10-13 20:52:04 -04:00
#endif
2025-09-06 18:04:24 -04:00
writer.Flush();
2024-10-13 20:52:04 -04:00
}
/// <summary>
/// Encrypt the ExeFS file entries
/// </summary>
/// <param name="cart">Cart representing the 3DS file</param>
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private void EncryptExeFSFileEntries(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2024-10-13 20:52:04 -04:00
{
// Get ExeFS offset
2024-11-13 21:11:26 -05:00
uint exeFsHeaderOffset = cart.GetExeFSOffset(index);
2025-09-06 18:04:24 -04:00
if (exeFsHeaderOffset == 0 || exeFsHeaderOffset > reader.Length)
{
Console.WriteLine($"Partition {index} ExeFS: No Data... Skipping...");
return;
}
2024-10-13 20:52:04 -04:00
// Get to the start of the files
2024-11-13 21:11:26 -05:00
uint exeFsFilesOffset = exeFsHeaderOffset + cart.MediaUnitSize;
2024-10-13 20:52:04 -04:00
// If the header failed to read, log and return
2024-11-14 11:56:18 -05:00
var exeFsHeader = cart.ExeFSHeaders?[index];
2026-01-25 17:20:06 -05:00
if (exeFsHeader?.FileHeaders is null)
2024-10-13 20:52:04 -04:00
{
2024-11-14 11:56:18 -05:00
Console.WriteLine($"Partition {index} ExeFS header does not exist. Skipping...");
2024-10-13 20:52:04 -04:00
return;
}
2024-11-13 21:11:26 -05:00
// Loop through and process all headers
for (int i = 0; i < exeFsHeader.FileHeaders.Length; i++)
2024-10-13 20:52:04 -04:00
{
2024-11-13 21:11:26 -05:00
// Only attempt to process code binary files
if (!cart.IsCodeBinary(index, i))
continue;
// Get the file header
var fileHeader = exeFsHeader.FileHeaders[i];
2026-01-25 17:20:06 -05:00
if (fileHeader is null)
2024-10-13 20:52:04 -04:00
continue;
// Create the ExeFS AES ciphers for this partition
2024-11-13 21:11:26 -05:00
uint ctroffset = (fileHeader.FileOffset + cart.MediaUnitSize) / 0x10;
2025-09-30 18:09:17 -04:00
byte[] exefsIVWithOffsetForHeader = cart.ExeFSIV(index).Add(ctroffset);
2026-03-22 01:06:36 -04:00
var firstCipher = AESCTR.CreateEncryptionCipher(keys.NormalKey, exefsIVWithOffsetForHeader);
var secondCipher = AESCTR.CreateDecryptionCipher(keys.NormalKey2C, exefsIVWithOffsetForHeader);
2024-10-13 20:52:04 -04:00
// Seek to the file entry
2025-09-06 18:04:24 -04:00
reader.Seek(exeFsFilesOffset + fileHeader.FileOffset, SeekOrigin.Begin);
writer.Seek(exeFsFilesOffset + fileHeader.FileOffset, SeekOrigin.Begin);
// Setup and perform the encryption
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(fileHeader.FileSize,
firstCipher,
secondCipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} ExeFS: Encrypting - {fileHeader.FileName}...{s}"));
2024-10-13 20:52:04 -04:00
}
2020-12-15 13:30:28 -08:00
}
/// <summary>
/// Encrypt the RomFS, if it exists
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2026-03-22 01:06:36 -04:00
/// <param name="keys">Keys for the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="reader">Stream representing the input</param>
/// <param name="writer">Stream representing the output</param>
2026-03-22 01:06:36 -04:00
private bool EncryptRomFS(N3DS cart, int index, PartitionKeys keys, Stream reader, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-10-13 22:10:59 -04:00
// Validate the RomFS
2024-11-13 21:11:26 -05:00
uint romFsOffset = cart.GetRomFSOffset(index);
2025-09-06 18:04:24 -04:00
if (romFsOffset == 0 || romFsOffset > reader.Length)
2020-12-15 13:30:28 -08:00
{
2024-10-13 20:37:54 -04:00
Console.WriteLine($"Partition {index} RomFS: No Data... Skipping...");
2024-08-08 16:06:40 -04:00
return false;
2020-12-15 13:30:28 -08:00
}
2024-11-13 21:11:26 -05:00
uint romFsSize = cart.GetRomFSSize(index);
2024-10-13 22:10:59 -04:00
if (romFsSize == 0)
{
Console.WriteLine($"Partition {index} RomFS: No Data... Skipping...");
return false;
}
// Seek to the RomFS
2025-09-06 18:04:24 -04:00
reader.Seek(romFsOffset, SeekOrigin.Begin);
writer.Seek(romFsOffset, SeekOrigin.Begin);
2020-12-15 13:30:28 -08:00
2024-10-13 23:31:13 -04:00
// Force setting encryption keys for partitions 1 and above
2024-10-13 20:37:54 -04:00
if (index > 0)
2020-12-15 13:30:28 -08:00
{
2024-11-14 11:44:41 -05:00
var backupHeader = cart.BackupHeader;
2026-03-22 01:06:36 -04:00
keys.SetRomFSValues(backupHeader.Flags.BitMasks,
2026-03-22 00:38:38 -04:00
hardwareConstant: AESHardwareConstant,
keyX0x2C: _development ? DevKeyX0x2C : KeyX0x2C);
2020-12-15 13:30:28 -08:00
}
2024-08-08 16:06:40 -04:00
// Create the RomFS AES cipher for this partition
2026-03-22 01:06:36 -04:00
var cipher = AESCTR.CreateEncryptionCipher(keys.NormalKey, cart.RomFSIV(index));
2020-12-15 13:30:28 -08:00
// Setup and perform the decryption
2025-10-07 12:56:23 -04:00
AESCTR.PerformOperation(romFsSize,
cipher,
2025-09-06 18:04:24 -04:00
reader,
writer,
2025-11-24 10:07:12 -05:00
s => Console.WriteLine($"\rPartition {index} RomFS: Encrypting - {s}"));
2024-08-08 16:06:40 -04:00
return true;
2020-12-15 13:30:28 -08:00
}
/// <summary>
/// Update the CryptoMethod and BitMasks for the encrypted partition
/// </summary>
2024-08-08 14:55:07 -04:00
/// <param name="cart">Cart representing the 3DS file</param>
2024-10-13 20:37:54 -04:00
/// <param name="index">Index of the partition</param>
2025-09-06 18:04:24 -04:00
/// <param name="writer">Stream representing the output</param>
private static void UpdateEncryptCryptoAndMasks(N3DS cart, int index, Stream writer)
2020-12-15 13:30:28 -08:00
{
2024-08-08 15:49:36 -04:00
// Get required offsets
2024-11-13 21:11:26 -05:00
uint partitionOffset = cart.GetPartitionOffset(index);
2024-08-08 15:49:36 -04:00
// Get the backup header
2024-11-14 11:44:41 -05:00
var backupHeader = cart.BackupHeader;
2026-01-25 17:20:06 -05:00
if (backupHeader?.Flags is null)
2024-11-14 11:44:41 -05:00
return;
2024-08-08 15:49:36 -04:00
2024-08-08 16:15:19 -04:00
// Seek to the CryptoMethod location
2025-09-06 18:04:24 -04:00
writer.Seek(partitionOffset + 0x18B, SeekOrigin.Begin);
2024-08-08 15:49:36 -04:00
2024-08-08 16:15:19 -04:00
// Write the new CryptoMethod
// - For partitions 1 and up, set crypto-method to 0x00
// - If partition 0, restore crypto-method from backup flags
2024-11-14 11:44:41 -05:00
byte cryptoMethod = index > 0 ? (byte)CryptoMethod.Original : (byte)backupHeader.Flags.CryptoMethod;
2025-09-06 18:04:24 -04:00
writer.Write(cryptoMethod);
writer.Flush();
2020-12-15 13:30:28 -08:00
2024-08-08 16:06:40 -04:00
// Seek to the BitMasks location
2025-09-06 18:04:24 -04:00
writer.Seek(partitionOffset + 0x18F, SeekOrigin.Begin);
2024-08-08 16:06:40 -04:00
// Write the new BitMasks flag
2024-11-14 11:44:41 -05:00
BitMasks flag = cart.GetBitMasks(index);
2020-12-15 13:30:28 -08:00
flag &= (BitMasks.FixedCryptoKey | BitMasks.NewKeyYGenerator | BitMasks.NoCrypto) ^ (BitMasks)0xFF;
2024-11-14 11:44:41 -05:00
flag |= (BitMasks.FixedCryptoKey | BitMasks.NewKeyYGenerator) & backupHeader.Flags.BitMasks;
2025-09-06 18:04:24 -04:00
writer.Write((byte)flag);
writer.Flush();
2020-12-15 13:30:28 -08:00
}
#endregion
2025-02-19 15:47:05 -05:00
#region Info
/// <inheritdoc/>
public string? GetInformation(string filename)
{
2025-02-19 15:47:05 -05:00
try
{
// Open the file for reading
using var input = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Deserialize the cart information
var cart = N3DS.Create(input);
2026-01-25 17:20:06 -05:00
if (cart?.Model is null)
2025-02-19 15:47:05 -05:00
return "Error: Not a 3DS cart image!";
// Get a string builder for the status
var sb = new StringBuilder();
// Iterate over all 8 NCCH partitions
for (int p = 0; p < 8; p++)
{
bool decrypted = cart.PossiblyDecrypted(p);
sb.AppendLine($"\tPartition {p}: {(decrypted ? "Decrypted" : "Encrypted")}");
2025-02-19 15:47:05 -05:00
}
// Return the status for all partitions
return sb.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
#endregion
}
}