public static AmiiboTag FromNtagData(byte[] data) { return(new AmiiboTag(new ArraySegment <byte>(NtagHelpers.GetInternalTag(data))) { IsDecrypted = false }); }
public bool Unpack(byte[] tag, byte[] plain) { byte[] internalBytes = NtagHelpers.GetInternalTag(tag); // Generate keys KeygenDerivedkeys dataKeys = GenerateKey(this.data, internalBytes); KeygenDerivedkeys tagKeys = GenerateKey(this.tag, internalBytes); // Decrypt dataKeys.Cipher(internalBytes, plain, false); // Init OpenSSL HMAC context HMac hmacCtx = new HMac(new Sha256Digest()); // Regenerate tag HMAC. Note: order matters, data HMAC depends on tag HMAC! hmacCtx.Init(new KeyParameter(tagKeys.hmacKey)); hmacCtx.BlockUpdate(plain, 0x1D4, 0x34); hmacCtx.DoFinal(plain, HMAC_POS_TAG); // Regenerate data HMAC hmacCtx.Init(new KeyParameter(dataKeys.hmacKey)); hmacCtx.BlockUpdate(plain, 0x029, 0x1DF); hmacCtx.DoFinal(plain, HMAC_POS_DATA); return (NativeHelpers.MemCmp(plain, internalBytes, HMAC_POS_DATA, 32) && NativeHelpers.MemCmp(plain, internalBytes, HMAC_POS_TAG, 32)); }
public void Pack(byte[] plain, byte[] tag) { byte[] cipher = new byte[NtagHelpers.NFC3D_AMIIBO_SIZE]; // Generate keys var tagKeys = GenerateKey(this.tag, plain); var dataKeys = GenerateKey(this.data, plain); // Init OpenSSL HMAC context HMac hmacCtx = new HMac(new Sha256Digest()); // Generate tag HMAC hmacCtx.Init(new KeyParameter(tagKeys.hmacKey)); hmacCtx.BlockUpdate(plain, 0x1D4, 0x34); hmacCtx.DoFinal(cipher, HMAC_POS_TAG); // Generate data HMAC hmacCtx.Init(new KeyParameter(dataKeys.hmacKey)); hmacCtx.BlockUpdate(plain, 0x029, 0x18B); // Data hmacCtx.BlockUpdate(cipher, HMAC_POS_TAG, 0x20); // Tag HMAC hmacCtx.BlockUpdate(plain, 0x1D4, 0x34); // Tag hmacCtx.DoFinal(cipher, HMAC_POS_DATA); // Encrypt dataKeys.Cipher(plain, cipher, true); // Convert back to hardware NtagHelpers.InternalToTag(cipher, tag); }
public static Title FromTitleID(string data) { return(Title.FromTitleID(NtagHelpers.StringToByteArrayFastest(data))); }
static int Main(string[] args) { try { p.Parse(args); } catch (OptionException e) { Console.Write("amiitool: "); Console.WriteLine(e.Message); Console.WriteLine("Try `amiitool --help' for more information."); return(1); } if (showHelp || !(doEncrypt ^ doDecrypt) || keyFile == null) { ShowHelp(); return(1); } AmiiboKeys amiiboKeys = AmiiboKeys.LoadKeys(keyFile); if (amiiboKeys == null) { Console.Error.WriteLine("Could not load keys from \"{0}\"", keyFile); return(5); } byte[] original = new byte[NTAG215_SIZE]; byte[] modified = new byte[NtagHelpers.NFC3D_AMIIBO_SIZE]; Stream file; try { file = File.OpenRead(inputFile); } catch (Exception ex) { Console.Error.WriteLine("Could not open input file: {0}", ex.Message); return(3); } int readBytes = 0; try { using (var reader = new BinaryReader(file)) { readBytes = reader.Read(original, 0, original.Length); if (readBytes < NtagHelpers.NFC3D_AMIIBO_SIZE) { throw new Exception("Wrong length"); } } } catch (Exception ex) { Console.Error.WriteLine("Could not read from input: {0}", ex.Message); return(3); } if (doEncrypt) { amiiboKeys.Pack(original, modified); } else { if (!amiiboKeys.Unpack(original, modified)) { Console.Error.WriteLine("!!! WARNING !!!: Tag signature was NOT valid"); if (!deactivateSignatureCheck) { return(6); } } var amiiboTag1 = AmiiboTag.FromInternalTag(modified); var amiiboTag2 = AmiiboTag.FromInternalTag(NtagHelpers.GetInternalTag(original)); } file = Console.OpenStandardOutput(); if (outputFile != null) { try { file = File.OpenWrite(outputFile); } catch (Exception ex) { Console.Error.WriteLine("Could not open output file: {0}", ex.Message); return(4); } } try { using (var writer = new BinaryWriter(file)) { writer.Write(modified, 0, modified.Length); if (readBytes > modified.Length) { writer.Write(original, modified.Length, readBytes - modified.Length); } } } catch (Exception ex) { Console.Error.WriteLine("CouldCould not write to output: {0}", ex.Message); return(3); } return(0); }