/// <summary> /// Authenticate with key0 /// </summary> private void btnAuthKeyZero_Click(object sender, RoutedEventArgs e) { //Gen2.NXP.AES.Tam2Authentication tam2Auth; Gen2.NXP.AES.Tam1Authentication tam1Auth; ushort[] Key0, Ichallenge, returnedIchallenge; byte[] Response, Challenge; lblDataValue.Content = ""; lblTam2DataValue.Content = ""; lblChallenge1Value.Content = ""; switch (cbxReadDataBank.Text) { case "TID": MemoryProfile = Gen2.NXP.AES.Profile.TID; break; case "User": MemoryProfile = Gen2.NXP.AES.Profile.USER; break; case "EPC": MemoryProfile = Gen2.NXP.AES.Profile.EPC; break; } if (txtbxVerifyKeyZero.Text.Length == 32) { byte[] KeytoWrite = ByteFormat.FromHex(txtbxVerifyKeyZero.Text); Key0 = new ushort[KeytoWrite.Length / 2]; Key0 = ByteConv.ToU16s(KeytoWrite); } else { MessageBox.Show("Please input valid Key0", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] randmNumber = ByteFormat.FromHex(lblRandomChallengeValue.Content.ToString().Replace(" ", "")); Ichallenge = new ushort[randmNumber.Length / 2]; Ichallenge = ByteConv.ToU16s(randmNumber); try { if ((bool)chkReadData.IsChecked && (bool)chkReadBuffer.IsChecked) { //ReadBuffer with Tam2 MessageBox.Show("Operation is not supported", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } else if (chkReadData.IsChecked == false && chkReadBuffer.IsChecked == false) { //Authentication with tam1 tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0, true); Gen2.NXP.AES.Authenticate auth = new Gen2.NXP.AES.Authenticate(tam1Auth); Response = (byte[])objReader.ExecuteTagOp(auth, searchSelect); Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key0)).Skip(6).Take(10).ToArray(); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); returnedIchallenge = new ushort[Challenge.Length / 2]; returnedIchallenge = ByteConv.ToU16s(Challenge); byte[] generatedIChallenge = new byte[10]; byte[] tempBuffer = new byte[16]; Array.Copy(Response, 0, generatedIChallenge, 0, 10); Array.Copy(Response, 10, tempBuffer, 0, 16); byte[] receivedIChallenge = DecryptIchallenge(tempBuffer, ByteConv.ConvertFromUshortArray(Key0).ToArray()); Array.Copy(receivedIChallenge, 6, receivedIChallenge, 0, 10); Array.Resize(ref receivedIChallenge, 10); if (generatedIChallenge.SequenceEqual(receivedIChallenge)) { MessageBox.Show("Tag Successfully Authenticated", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Tag Failed Authentication; Confirm Verification Key is Correct.", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Warning); } } else if (chkReadData.IsChecked == false && chkReadBuffer.IsChecked == true) { //Read Buffer with Tam1 tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0, true); Gen2.ReadBuffer buffer = new Gen2.NXP.AES.ReadBuffer(0, 128, tam1Auth); Response = (byte[])objReader.ExecuteTagOp(buffer, searchSelect); Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key0)).Skip(6).Take(10).ToArray(); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); lblDataValue.Content = ""; byte[] generatedIChallenge = new byte[10]; byte[] tempBuffer = new byte[16]; Array.Copy(Response, 0, generatedIChallenge, 0, 10); Array.Copy(Response, 10, tempBuffer, 0, 16); byte[] receivedIChallenge = DecryptIchallenge(tempBuffer, ByteConv.ConvertFromUshortArray(Key0)); Array.Copy(receivedIChallenge, 6, receivedIChallenge, 0, 10); Array.Resize(ref receivedIChallenge, 10); if (generatedIChallenge.SequenceEqual(receivedIChallenge)) { MessageBox.Show("Tag Successfully Authenticated", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Tag Failed Authentication; Confirm Verification Key is Correct.", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Warning); } } else { //Authenticate with tam2 MessageBox.Show("Operation is not supported", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } }
/// <summary> /// Authenticate with key1 /// </summary> private void btnAuthKeyOne_Click(object sender, RoutedEventArgs e) { Gen2.NXP.AES.Tam2Authentication tam2Auth; Gen2.NXP.AES.Tam1Authentication tam1Auth; ushort[] Key1, Ichallenge, returnedIchallenge; byte[] Response, Challenge; lblDataValue.Content = ""; lblTam2DataValue.Content = ""; lblChallenge1Value.Content = ""; switch (cbxReadDataBank.Text) { case "TID": MemoryProfile = Gen2.NXP.AES.Profile.TID; break; case "User": MemoryProfile = Gen2.NXP.AES.Profile.USER; break; case "EPC": MemoryProfile = Gen2.NXP.AES.Profile.EPC; break; } if (txtbxVerifyKeyOne.Text.Length == 32) { byte[] KeytoWrite = ByteFormat.FromHex(txtbxVerifyKeyOne.Text); Key1 = new ushort[KeytoWrite.Length / 2]; Key1 = ByteConv.ToU16s(KeytoWrite); } else { MessageBox.Show("Please input valid Key1", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] randmNumber = ByteFormat.FromHex(lblRandomChallengeValue.Content.ToString().Replace(" ", "")); Ichallenge = new ushort[randmNumber.Length / 2]; Ichallenge = ByteConv.ToU16s(randmNumber); try { if ((bool)chkReadData.IsChecked && (bool)chkReadBuffer.IsChecked) { //ReadBuffer with Tam2 tam2Auth = new Gen2.NXP.AES.Tam2Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Ichallenge, MemoryProfile, (ushort)Convert.ToUInt32(txtReadStartAddr.Text), (ushort)Convert.ToUInt32(txtReadLength.Text), false); Gen2.ReadBuffer buffer = new Gen2.NXP.AES.ReadBuffer(0, 256, tam2Auth); Response = (byte[])objReader.ExecuteTagOp(buffer, searchSelect); //swap the bytes for (int i = 0; i < Response.Length; i += 2) { SwapBytes(ref Response, i, i + 1); } Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1)).Skip(6).Take(10).ToArray(); lblDataValue.Content = DecryptCustomData(Response.Skip(16).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1), Response.Skip(0).Take(16).ToArray()); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); } else if (chkReadData.IsChecked == false && chkReadBuffer.IsChecked == false) { //Auth with tam1 tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Ichallenge, true); Gen2.NXP.AES.Authenticate auth = new Gen2.NXP.AES.Authenticate(tam1Auth); Response = (byte[])objReader.ExecuteTagOp(auth, searchSelect); Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1)).Skip(6).Take(10).ToArray(); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); } else if (chkReadData.IsChecked == false && chkReadBuffer.IsChecked == true) { //Readbuffer with Tam1 tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Ichallenge, false); Gen2.ReadBuffer buffer = new Gen2.NXP.AES.ReadBuffer(0, 128, tam1Auth); Response = (byte[])objReader.ExecuteTagOp(buffer, searchSelect); //swap the bytes for (int i = 0; i < Response.Length; i += 2) { SwapBytes(ref Response, i, i + 1); } Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1)).Skip(6).Take(10).ToArray(); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); lblDataValue.Content = ""; } else { //Authenticate with tam2 tam2Auth = new Gen2.NXP.AES.Tam2Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Ichallenge, MemoryProfile, (ushort)Convert.ToUInt32(txtReadStartAddr.Text), (ushort)Convert.ToUInt32(txtReadLength.Text), true); Gen2.NXP.AES.Authenticate auth = new Gen2.NXP.AES.Authenticate(tam2Auth); Response = (byte[])objReader.ExecuteTagOp(auth, searchSelect); if (Response != null) { lblTam2DataValue.Content = ByteFormat.ToHex(Response, "", " "); } lblTam2DataValue.Content = DecryptCustomData(Response.Skip(16).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1), Response.Skip(0).Take(16).ToArray()); Challenge = DecryptIchallenge(Response.Skip(0).Take(16).ToArray(), ByteConv.ConvertFromUshortArray(Key1)).Skip(6).Take(10).ToArray(); lblChallenge1Value.Content = ByteFormat.ToHex(Challenge, "", " "); } returnedIchallenge = new ushort[Challenge.Length / 2]; returnedIchallenge = ByteConv.ToU16s(Challenge); if (returnedIchallenge.SequenceEqual(Ichallenge)) { MessageBox.Show("Tag Successfully Authenticated", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Tag Failed Authentication; Confirm Verification Key is Correct.", "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Warning); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Universal Reader Assistant", MessageBoxButton.OK, MessageBoxImage.Error); return; } }
static void Main(string[] args) { // Program setup if (1 > args.Length) { Usage(); } for (int nextarg = 1; nextarg < args.Length; nextarg++) { string arg = args[nextarg]; if (arg.Equals("--ant")) { if (null != antennaList) { Console.WriteLine("Duplicate argument: --ant specified more than once"); Usage(); } antennaList = ParseAntennaList(args, nextarg); nextarg++; } else { Console.WriteLine("Argument {0}:\"{1}\" is not recognized", nextarg, arg); Usage(); } } try { // Create Reader object, connecting to physical device. // Wrap reader in a "using" block to get automatic // reader shutdown (using IDisposable interface). using (r = Reader.Create(args[0])) { //Uncomment this line to add default transport listener. //r.Transport += r.SimpleTransportListener; r.Connect(); if (Reader.Region.UNSPEC == (Reader.Region)r.ParamGet("/reader/region/id")) { Reader.Region[] supportedRegions = (Reader.Region[])r.ParamGet("/reader/region/supportedRegions"); if (supportedRegions.Length < 1) { throw new FAULT_INVALID_REGION_Exception(); } r.ParamSet("/reader/region/id", supportedRegions[0]); } if (r.isAntDetectEnabled(antennaList)) { Console.WriteLine("Module doesn't has antenna detection support please provide antenna list"); Usage(); } // Create a simplereadplan which uses the antenna list created above SimpleReadPlan plan = new SimpleReadPlan(antennaList, TagProtocol.GEN2, null, null, 1000); // Set the created readplan r.ParamSet("/reader/read/plan", plan); //Use first antenna for operation if (antennaList != null) { r.ParamSet("/reader/tagop/antenna", antennaList[0]); } ushort[] Key0 = new ushort[] { 0x0123, 0x4567, 0x89AB, 0xCDEF, 0x0123, 0x4567, 0x89AB, 0xCDEF }; ushort[] Key1 = new ushort[] { 0x1122, 0x3344, 0x5566, 0x7788, 0x1122, 0x3344, 0x5566, 0x7788 }; // Change enable filter to true inorder to enable filter. bool enableFilter = false; Gen2.Select filter = null; if (enableFilter) { filter = new Gen2.Select(false, Gen2.Bank.EPC, 32, 96, new byte[] { 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF }); } bool SendRawData = true; bool _isNMV2DTag = false; byte[] Response; Gen2.NXP.AES.Tam1Authentication tam1Auth; Gen2.NXP.AES.Tam2Authentication tam2Auth; //Uncomment this to enable Authenticate with TAM1 with key0 for NXPUCODE AES tag //tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0, SendRawData); //Gen2.NXP.AES.Authenticate auth = new Gen2.NXP.AES.Authenticate(tam1Auth); //Response = (byte[])r.ExecuteTagOp(auth, filter); //if (SendRawData) //{ // parseTAM1Response(Response, ByteConv.ConvertFromUshortArray(Key0)); //} //else //{ // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(Response, "", " ")); //} //Uncomment this to enable Authenticate with TAM1 with Key1 //tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, SendRawData); //Gen2.NXP.AES.Authenticate tam1AuthKey1 = new Gen2.NXP.AES.Authenticate(tam1Auth); //Response = (byte[])r.ExecuteTagOp(tam1AuthKey1, filter); //if (SendRawData) //{ // parseTAM1Response(Response, ByteConv.ConvertFromUshortArray(Key1)); //} //else //{ // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(Response, "", " ")); //} //Uncomment this to enable Authenticate with TAM2 with key1 ushort Offset = 0; ushort BlockCount = 1; //supported protMode value is 1 for NXPUCODE AES ushort ProtMode = 1; tam2Auth = new Gen2.NXP.AES.Tam2Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Gen2.NXP.AES.Profile.EPC, Offset, BlockCount, ProtMode, SendRawData); Gen2.NXP.AES.Authenticate tam2AuthKey1 = new Gen2.NXP.AES.Authenticate(tam2Auth); Response = (byte[])r.ExecuteTagOp(tam2AuthKey1, filter); if (SendRawData) { parseTAM2Response(Response, ByteConv.ConvertFromUshortArray(Key1), ProtMode); } else { byte[] generatedIChallenge = new byte[10]; byte[] dataRequested = new byte[16]; Array.Copy(Response, 0, generatedIChallenge, 0, 10); Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(generatedIChallenge, "", " ")); Array.Copy(Response, 10, dataRequested, 0, 16); Console.WriteLine("Data :" + ByteFormat.ToHex(dataRequested, "", " ")); } // Embedded tag operations #region EmbeddedTagOperations { //Uncomment this to execute embedded tagop for TAM1 Authentication using Key0 //tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0, SendRawData); //Gen2.NXP.AES.Authenticate embeddedauth = new Gen2.NXP.AES.Authenticate(tam1Auth); //Response = performEmbeddedOperation(filter, embeddedauth); //if (SendRawData) //{ //parseTAM1Response(Response, ByteConv.ConvertFromUshortArray(Key0)); //} //else //{ //Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(Response, "", " ")); //} //Uncomment this to execute embedded tagop for Authenticate with TAM1 using Key1 //tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, SendRawData); //Gen2.NXP.AES.Authenticate embeddedtam1AuthKey1 = new Gen2.NXP.AES.Authenticate(tam1Auth); //Response = performEmbeddedOperation(filter, embeddedtam1AuthKey1); //if (SendRawData) //{ // parseTAM1Response(Response, ByteConv.ConvertFromUshortArray(Key1)); //} //else //{ // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(Response, "", " ")); //} //Uncomment this to execute embedded tagop for Authenticate with TAM2 with key1 //ushort EmbeddedOffset = 0; //ushort EmbeddedBlockCount = 1; ////supported protMode value is 1 for NXPUCODE AES //ushort EmbeddedProtMode = 1; //tam2Auth = new Gen2.NXP.AES.Tam2Authentication(Gen2.NXP.AES.KeyId.KEY1, Key1, Gen2.NXP.AES.Profile.EPC, // EmbeddedOffset, EmbeddedBlockCount, EmbeddedProtMode, SendRawData); //Gen2.NXP.AES.Authenticate embeddedtam2AuthKey1 = new Gen2.NXP.AES.Authenticate(tam2Auth); //Response = performEmbeddedOperation(filter, embeddedtam2AuthKey1); //if (SendRawData) //{ // parseTAM2Response(Response, ByteConv.ConvertFromUshortArray(Key1), EmbeddedProtMode); //} //else //{ // byte[] generatedIChallenge = new byte[10]; // byte[] dataRequested = new byte[16]; // Array.Copy(Response, 0, generatedIChallenge, 0, 10); // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(generatedIChallenge, "", " ")); // Array.Copy(Response, 10, dataRequested, 0, 16); // Console.WriteLine("Data :" + ByteFormat.ToHex(dataRequested, "", " ")); //} } #endregion EmbeddedTagOperations //Enable flag _isNMV2DTag for TAM1/TAM2 Authentication with KEY0 for NMV2D Tag if (_isNMV2DTag) { // NMV2D tag only supports KEY0 // Uncomment this to enable TAM1 Authentication with KEY0 //ushort[] Key0_NMV2D = new ushort[] { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; //tam1Auth = new Gen2.NXP.AES.Tam1Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0_NMV2D, SendRawData); //Gen2.NXP.AES.Authenticate tam1AuthKey0 = new Gen2.NXP.AES.Authenticate(tam1Auth); //Response = (byte[])r.ExecuteTagOp(tam1AuthKey0, filter); //if (SendRawData) //{ // parseTAM1Response(Response, ByteConv.ConvertFromUshortArray(Key0_NMV2D)); //} //else //{ // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(Response, "", " ")); //} //TAM2 Authentication with KEY0 //ushort offset = 0; //ushort blockCount = 1; ////supported protMode values are 0x00, 0x01, 0x02, 0x03 //ushort protMode = 0; //tam2Auth = new Gen2.NXP.AES.Tam2Authentication(Gen2.NXP.AES.KeyId.KEY0, Key0_NMV2D, Gen2.NXP.AES.Profile.EPC, // offset, blockCount, protMode, SendRawData); //Gen2.NXP.AES.Authenticate tam2AuthKey0 = new Gen2.NXP.AES.Authenticate(tam2Auth); //Response = (byte[])r.ExecuteTagOp(tam2AuthKey0, filter); //if (SendRawData) //{ // parseTAM2Response(Response, ByteConv.ConvertFromUshortArray(Key0_NMV2D), protMode); //} //else //{ // byte[] generatedIChallenge = new byte[10]; // byte[] dataRequested = new byte[16]; // Array.Copy(Response, 0, generatedIChallenge, 0, 10); // Console.WriteLine("Generated Ichallenge:" + ByteFormat.ToHex(generatedIChallenge, "", " ")); // Array.Copy(Response, 10, dataRequested, 0, 16); // Console.WriteLine("Data :" + ByteFormat.ToHex(dataRequested, "", " ")); // } } } } catch (ReaderException re) { Console.WriteLine("Error: " + re.Message); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }