private static void LANuncipherV1(string[] args, bool DEBUG) { //conversion des arguments string input_frame = args[3]; byte[] kmacbytes = Hex2Bytes(args[4].Substring(0, 32)); byte[] kencbytes = Hex2Bytes(args[5].Substring(0, 32)); //décodage l6 string l6 = input_frame.Substring(22, input_frame.Length - 26); string l6ctrl = Convert.ToString(byte.Parse(l6.Substring(0, 2), System.Globalization.NumberStyles.HexNumber), 2).PadLeft(8, '0'); string lblL6Vers = l6ctrl.Substring(0, 3); string lblL6Wts = l6ctrl.Substring(3, 1); string lblL6KeySel = l6ctrl.Substring(4, 4); //system.Console.WriteLine("Clé n° : " + Convert.ToInt32(lblL6KeySel.ToString(), 2).ToString()); //Décodage trame int cfield = Int32.Parse(input_frame.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); string lblCField = cfield.ToString("X2"); string lblMField = input_frame.Substring(4, 4); string lblAField = input_frame.Substring(8, 12); string lblClField = input_frame.Substring(20, 2); string lblL6OprID = l6.Substring(2, 2); string lblL6Cpt = l6.Substring(4, 4); string lblL6App = l6.Substring(8, 2); string lblL6HashKmac = l6.Substring(l6.Length - 4, 4); if (CRC_OK(input_frame, DEBUG) && hashKmac_OK(input_frame, kmacbytes, lblL6Vers, DEBUG) && hashKenc_OK(input_frame, kencbytes, lblL6Vers, DEBUG)) { string l7 = l6.Substring(10, l6.Length - 26); string txtIV = (lblMField.ToString() + lblAField.ToString() + lblL6Cpt.ToString() + lblCField.ToString()).PadRight(32, '0'); byte[] l7decipheredbytes = CAES128.AES_CTR(Hex2Bytes(l7), kencbytes, Hex2Bytes(txtIV)); string l7deciphered = Bytes2Hex(l7decipheredbytes, l7decipheredbytes.Length); if (DEBUG) { System.Console.WriteLine("IV: " + txtIV + Environment.NewLine + Environment.NewLine + "L7 ciphered: " + l7); } System.Console.WriteLine("L7 unciphered: " + l7deciphered); } else { System.Console.WriteLine("Frame unciphering error"); } }
private static void NFCDATAcipher(string[] args, bool DEBUG) { string data = args[1]; byte[] kencbytes = Hex2Bytes(args[2].Substring(0, 32)); string IV = (args[3].ToString().PadRight(32, '0')); //chiffrement byte[] DATAcipheredbytes = CAES128.AES_CTR(Hex2Bytes(data), kencbytes, Hex2Bytes(IV)); string DATAciphered = Bytes2Hex(DATAcipheredbytes, DATAcipheredbytes.Length); //System.Console.WriteLine("LAN L7 ciphered: " + l7ciphered); System.Console.WriteLine("CipheredData " + DATAciphered); }
private static void LANcipherV0(string[] args, bool DEBUG) { string lblL6Vers = args[2]; string l7 = args[3]; byte[] kencbytes = Hex2Bytes(args[4].Substring(0, 32)); string lblMField = args[5]; string lblAField = args[6]; string lblL6Cpt = args[7]; string lblCField = args[8]; string lblL6Wts = "1"; string lblL6KeySel = Convert.ToString(Convert.ToInt32(args[9], 10), 2).PadLeft(4, '0'); string lblL6Ctrl = Convert.ToInt32(lblL6Vers + lblL6Wts + lblL6KeySel, 2).ToString("X2"); //System.Console.WriteLine("lblL6Ctrl : " + lblL6Ctrl); //System.Console.WriteLine("l7: " + l7 + Environment.NewLine + "lblMField: " + lblMField + Environment.NewLine + "lblAField: " + lblAField + Environment.NewLine + "lblL6Cpt: " + lblL6Cpt + Environment.NewLine + "lblCField: " + lblCField + Environment.NewLine + "l7: " + l7 + Environment.NewLine); //IV string txtIV = (lblMField.ToString() + lblAField.ToString() + lblL6Cpt.ToString() + lblCField.ToString()).PadRight(32, '0'); //System.Console.WriteLine("computed IV: " + txtIV); //chiffrement L7 byte[] l7cipheredbytes = CAES128.AES_CTR(Hex2Bytes(l7), kencbytes, Hex2Bytes(txtIV)); string l7ciphered = Bytes2Hex(l7cipheredbytes, l7cipheredbytes.Length); //System.Console.WriteLine("LAN L7 ciphered: " + l7ciphered); string lblCIField = "B4"; // imposée par la LAN string lblL6HashKenc = "00000000"; //recalcule après string lblL6Tstamp = "0000"; //recalculé par le K string lblL6HashKmac = "0000"; //recalculé par le K string CRC = "0000"; //recalculé par le K //L-field Int32 lfield_calc = 1 + 2 + 6 + 1 + 1 + 2 + l7.Length / 2 + 4 + lblL6Tstamp.Length / 2 + 2 + 2; // longeur de la trame du C-field au CRC inclu string lblLField = lfield_calc.ToString("X2"); lblL6HashKenc = Bytes2Hex(CAES128.AES_CMAC(Hex2Bytes(lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6Cpt + l7ciphered), kencbytes), 4); //System.Console.WriteLine("lblL6HashKenc " + lblL6HashKenc); string L2 = lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6Cpt + l7ciphered + lblL6HashKenc + lblL6Tstamp + lblL6HashKmac + CRC; System.Console.WriteLine("L2 " + L2); }
private static void NFCuncipher(string[] args, bool DEBUG) { //input_NFCframe kmob NFCUid (8o) string input_NFCframe = args[2]; byte[] kmobbytes = Hex2Bytes(args[3].Substring(0, 32)); string NFCuid = args[4]; string NFCLfield = input_NFCframe.Substring(0, 2); string NFCcpt = input_NFCframe.Substring(2, 4); string NFCCfield = input_NFCframe.Substring(6, 2); string NFCNFCver = input_NFCframe.Substring(8, 2); string NFCdata = input_NFCframe.Substring(10, input_NFCframe.Length - 22); string NFChashkmob = input_NFCframe.Substring(input_NFCframe.Length - 12, 8); string NFCCRC = input_NFCframe.Substring(input_NFCframe.Length - 4, 4); /* * System.Console.WriteLine("NFCLfield " + NFCLfield); * System.Console.WriteLine("NFCcpt " + NFCcpt); * System.Console.WriteLine("NFCCfield " + NFCCfield); * System.Console.WriteLine("NFCNFCver " + NFCNFCver); * System.Console.WriteLine("NFCdata " + NFCdata); * System.Console.WriteLine("NFChashkmob " + NFChashkmob); * System.Console.WriteLine("NFCCRC " + NFCCRC); */ //CRC vérification if (CRC_OK(input_NFCframe, DEBUG)) { //hashKmob vérification string NFCinputhashkmob = (NFCuid.ToString() + NFCLfield.ToString() + NFCcpt.ToString() + NFCCfield.ToString() + NFCNFCver.ToString() + NFCdata.ToString()); string NFChashkmobcomputed = Bytes2Hex(CAES128.AES_CMAC(Hex2Bytes(NFCinputhashkmob), kmobbytes), 4); if (DEBUG) { System.Console.WriteLine("NFChashkmobcomputed " + NFChashkmobcomputed.ToString().ToUpper() + " NFChashkmob_inframe " + NFChashkmob); } if (NFChashkmobcomputed.ToString().ToUpper() == NFChashkmob) { //déchiffrement //calcul IV string NFCIV = (NFCuid.ToString() + NFCcpt.ToString()).PadRight(32, '0'); if (DEBUG) { System.Console.WriteLine("NFCIV: " + NFCIV); } byte[] NFCdecipheredbytes = CAES128.AES_CTR(Hex2Bytes(NFCdata), kmobbytes, Hex2Bytes(NFCIV)); string NFCl7deciphered = Bytes2Hex(NFCdecipheredbytes, NFCdecipheredbytes.Length); System.Console.WriteLine("NFC L7: " + NFCl7deciphered); } else { System.Console.WriteLine("Error HashKmob: HashKmob computed = " + NFChashkmobcomputed.ToString().ToUpper() + " HashKmob in frame = " + NFCinputhashkmob); } } }
private static void LANcipherV1(string[] args, bool DEBUG) { string lblCField = args[8]; string lblMField = args[5]; string lblAField = args[6]; string lblCIField = args[12]; //"B4"; imposée par la LAN string lblL6Vers = args[2]; string lblL6Wts = "0"; //champs reserved du L6Ctrl fixé à 0 string lblL6KeySel = Convert.ToString(Convert.ToInt32(args[9], 10), 2).PadLeft(4, '0'); string lblL6Ctrl = Convert.ToInt32(lblL6Vers + lblL6Wts + lblL6KeySel, 2).ToString("X2"); string lblL6OprID = args[10]; string lblL6Cpt = args[7]; string lblL6App = args[11]; string l7 = args[3]; string lblL6HashKenc = "00000000"; //recalcule après string lblL6Tstamp = args[13]; //string lblL6Tstamp = "0000"; //recalculé par le K string lblL6HashKmac = "0000"; //recalculé par le K string CRC = "0000"; //recalculé par le K byte[] kencbytes = Hex2Bytes(args[4].Substring(0, 32)); byte[] kmacbytes = Hex2Bytes(args[14].Substring(0, 32)); //System.Console.WriteLine("l7: " + l7 + Environment.NewLine + "lblMField: " + lblMField + Environment.NewLine + "lblAField: " + lblAField + Environment.NewLine + "lblL6Cpt: " + lblL6Cpt + Environment.NewLine + "lblCField: " + lblCField + Environment.NewLine + "l7: " + l7 + Environment.NewLine); //IV string txtIV = (lblMField.ToString() + lblAField.ToString() + lblL6Cpt.ToString() + lblCField.ToString()).PadRight(32, '0'); if (DEBUG) { System.Console.WriteLine("computed IV: " + txtIV); } //chiffrement L7 byte[] l7cipheredbytes = CAES128.AES_CTR(Hex2Bytes(l7), kencbytes, Hex2Bytes(txtIV)); string l7ciphered = Bytes2Hex(l7cipheredbytes, l7cipheredbytes.Length); if (DEBUG) { System.Console.WriteLine("LAN L7 ciphered: " + l7ciphered); } //L-field Int32 lfield_calc = 1 + 2 + 6 + 1 + 1 + 1 + 2 + 1 + l7.Length / 2 + 4 + lblL6Tstamp.Length / 2 + 2 + 2; // longeur de la trame du C-field au CRC inclu string lblLField = lfield_calc.ToString("X2"); //L6HashKenc string block0Kenc = lblMField + lblAField + lblL6Cpt + "000000000000"; lblL6HashKenc = Bytes2Hex(CAES128.AES_CMAC(Hex2Bytes(block0Kenc + l7ciphered), kencbytes), 4); if (DEBUG) { System.Console.WriteLine("lblL6HashKenc " + lblL6HashKenc); } //L6HashKmac string HashKmacInput = lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp; string block0Kmac = lblMField + lblAField + "0000000000000000"; string Cmacinput = block0Kmac + HashKmacInput; if (DEBUG) { System.Console.WriteLine("full Cmacinput = " + Cmacinput); } byte[] Cmacinputbytes = Hex2Bytes(Cmacinput); lblL6HashKmac = Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 2); if (DEBUG) { System.Console.WriteLine("lblL6HashKmac = " + lblL6HashKmac); } //CRC string CRCinput = lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp + lblL6HashKmac; byte[] CRCinputbytes = Hex2Bytes(CRCinput); short CRCcomputed = CLibCRC.CalculateCRC16EN13757(CRCinputbytes); CRC = CRCcomputed.ToString("X2"); if (DEBUG) { System.Console.WriteLine("CRC computed = " + CRC); } string L2 = lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp + lblL6HashKmac + CRC; System.Console.WriteLine("L2 " + L2); }