Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Read and write DS extended header including validate and regenerate HMAC #17

Merged
merged 8 commits into from
Apr 11, 2022

Conversation

pleonex
Copy link
Member

@pleonex pleonex commented Apr 11, 2022

Description

  • Read and write DS extended header: new header fields in DS games launched after the DSi. Including...
    • HMACs: validate and re-generate. Except ARM9/7 HMAC as it requires to implement the secure area encryption. Validated and re-created via the new class TwilightHMacGenerator.
    • RSA header signature: validate. We cannot re-generate as the private key is unknown. Validated via new class TwilightSigner
  • New DsiKeyStore class to specify the keys for HMAC and RSA signature. The documentation of the fields explains how to fill it. It is a class easily serializable into YAML or JSON.
  • Convert UnitCode, DsiCryptoFlags into an enum with the possible values.
  • Regenerate ROM header CRC-16
  • Rename RomInfo into ProgramInfo to match variable names.
  • The class HashInfo is used now for CRC, HMAC and Signatures.

Example

The Binary2NitroRom converter accepts the optional parameter DsiKeyStore. If provided, the HMAC and Signature will be validated while reading the ROM.

DsiKeyStore keys = ...; // Create in code or deserialize from YAML/JSON.

using Node node = NodeFactory.FromFile(romPath, FileOpenMode.Read);
node.TransformWith<Binary2NitroRom, DsiKeyStore>(keys);

NitroRom rom = node.GetFormatAs<NitroRom>();
ProgramInfo programInfo = rom.Information;
bool isDsi = programInfo.UnitCode != DeviceUnitKind.DS;

if (isDsi || programInfo.ProgramFeatures.HasFlag(DsiRomFeatures.BannerSigned)) {
    programInfo.BannerMac.Status.Should().Be(HashStatus.Valid);
}

if (programInfo.ProgramFeatures.HasFlag(DsiRomFeatures.ProgramSigned)) {
    programInfo.OverlaysMac.Status.Should().Be(HashStatus.Valid);
    programInfo.Signature.Status.Should().Be(HashStatus.Valid);
}

if (isDsi) {
    programInfo.OverlaysMac.IsNull.Should().BeTrue();
    programInfo.ProgramMac.IsNull.Should().BeTrue();
    programInfo.Signature.Status.Should().Be(HashStatus.Valid);
}

The NitroRom2Binary converter accepts the optional parameter NitroRom2BinaryParams. If provided with keys, it will re-generate the above HMACs.

Related to #9, #12

@pleonex pleonex added the enhancement New feature or request label Apr 11, 2022
@pleonex pleonex added this to the vNext milestone Apr 11, 2022
@pleonex pleonex self-assigned this Apr 11, 2022
@pleonex pleonex merged commit 05d566f into main Apr 11, 2022
@pleonex pleonex deleted the feature/ds-extendedHeader branch April 11, 2022 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant