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); Array.Copy(tag, 0x208, plain, 0x208, 0x014); return (NativeHelpers.MemCmp(plain, internalBytes, HMAC_POS_DATA, 32) && NativeHelpers.MemCmp(plain, internalBytes, HMAC_POS_TAG, 32)); }
protected bool Equals(KeygenDerivedkeys other) { return (NativeHelpers.MemCmp(aesKey, other.aesKey, 0, aesKey.Length) && NativeHelpers.MemCmp(aesIV, other.aesIV, 0, aesIV.Length) && NativeHelpers.MemCmp(hmacKey, other.hmacKey, 0, hmacKey.Length)); }