10 private readonly
byte[] _key;
12 private readonly
byte[] _area1Counter;
14 private readonly
byte[] _area2Counter;
22 ReadOnlySpan<byte> key,
23 ReadOnlySpan<byte> area1Counter,
24 ReadOnlySpan<byte> area2Counter,
27 ValidateBlock(key, nameof(key));
28 ValidateBlock(area1Counter, nameof(area1Counter));
29 ValidateBlock(area2Counter, nameof(area2Counter));
30 if (!Enum.IsDefined(keyMode))
32 throw new ArgumentOutOfRangeException(nameof(keyMode), keyMode,
"Unknown modcrypt key provenance.");
36 _area1Counter = area1Counter.ToArray();
37 _area2Counter = area2Counter.ToArray();
46 public ReadOnlyMemory<byte>
ExportKey() => _key.ToArray();
61 ArgumentNullException.ThrowIfNull(header);
63 throw new ArgumentException(
"Modcrypt requires a DSi-enhanced or DSi-exclusive header.", nameof(header));
89 ArgumentNullException.ThrowIfNull(header);
91 throw new ArgumentException(
"Modcrypt requires a DSi-enhanced or DSi-exclusive header.", nameof(header));
94 throw new ArgumentException(
95 "A header selecting its public modcrypt key cannot accept a secure-key override.",
99 ValidateBlock(secureNormalKey, nameof(secureNormalKey));
108 internal ReadOnlyMemory<byte> Key => _key;
113 internal ReadOnlyMemory<byte> GetCounter(
NdsModcryptArea area) => area
switch
117 _ =>
throw new ArgumentOutOfRangeException(nameof(area), area,
"Unknown modcrypt area."),
123 private static void ValidateBlock(ReadOnlySpan<byte> value,
string parameterName)
125 if (value.Length != NdsModcrypt.BlockSize)
127 throw new ArgumentException(
"A modcrypt key or counter must contain exactly sixteen bytes.", parameterName);
NdsModcryptKeyMode KeyMode
Preserves whether the normal key was public header data or supplied by an external secure-key authori...
ReadOnlyMemory< byte > ExportCounter(NdsModcryptArea area)
Returns an independent copy of the initial counter selected for one declared modcrypt area.
ReadOnlyMemory< byte > ExportKey()
Returns an independent copy of the resolved AES key so callers can persist or clear it on their own t...
static NdsModcryptContext FromHeader(NdsHeader header)
Resolves public header-key mode automatically and otherwise derives the normal key from the public mo...
NdsModcryptContext(ReadOnlySpan< byte > key, ReadOnlySpan< byte > area1Counter, ReadOnlySpan< byte > area2Counter, NdsModcryptKeyMode keyMode=NdsModcryptKeyMode.SecureNormalKey)
Copies already resolved key and counter material after enforcing the fixed AES block boundary.
static NdsModcryptContext FromHeader(NdsHeader header, ReadOnlySpan< byte > secureNormalKey)
Uses an explicitly resolved secure normal key instead of applying the built-in modcrypt KeyX/KeyY rec...