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 bool hashKenc_OK(string input_frame, byte[] kencbytes, string lblL6Vers, bool DEBUG) { //hashKenc de la trame string lblL6HashKenc; string Cencinput = ""; if (lblL6Vers == "000") { 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 lblL6Wts = l6ctrl.Substring(3, 1); if (DEBUG) { System.Console.WriteLine(" l6 = " + l6 + " l6ctrl = " + l6ctrl + " lblL6Wts = " + lblL6Wts); } //si L6TStamp présent if (lblL6Wts.ToString() == "1") { //lblL6TStamp = l6.Substring(l6.Length - 8, 4); lblL6HashKenc = l6.Substring(l6.Length - 16, 8); Cencinput = input_frame.Substring(0, input_frame.Length - 20); } //si L6Tstamp absent else { //lblL6TStamp = ""; lblL6HashKenc = l6.Substring(l6.Length - 12, 8); Cencinput = input_frame.Substring(0, input_frame.Length - 16); } // System.Console.WriteLine("Cencinput = " + Cencinput + " lblL6HashKenc = " + lblL6HashKenc); //calcul du hashKenc à partir de la trame byte[] Cencinputbytes = Hex2Bytes(Cencinput); string lblL6HashKenccomputed = Bytes2Hex(CAES128.AES_CMAC(Cencinputbytes, kencbytes), 4); if (DEBUG) { System.Console.WriteLine(Environment.NewLine + "full hashKenc computed = " + Bytes2Hex(CAES128.AES_CMAC(Cencinputbytes, kencbytes), 16)); } if (lblL6HashKenccomputed.ToString().ToUpper() == lblL6HashKenc.ToString().ToUpper()) { if (DEBUG) { System.Console.WriteLine("hashKenc computed = " + lblL6HashKenccomputed + " hashKenc in frame = " + lblL6HashKenc + Environment.NewLine + "hashKenc OK" + Environment.NewLine); } return(true); } else { System.Console.WriteLine("Error hashKenc: hashKenc computed = " + lblL6HashKenccomputed + " hashKenc in frame = " + lblL6HashKenc); if (DEBUG) { return(true); } else { return(false); } } } else if (lblL6Vers == "001") { //calcul du hashKenc à partir de la trame lblL6HashKenc = input_frame.Substring(input_frame.Length - 20, 8); string L7 = input_frame.Substring(32, input_frame.Length - 52); //L7 string lblMField = input_frame.Substring(4, 4); string lblAField = input_frame.Substring(8, 12); string lblL6Cpt = input_frame.Substring(26, 4); string block0 = lblMField + lblAField + lblL6Cpt + "000000000000"; Cencinput = block0 + L7; if (DEBUG) { System.Console.WriteLine("full Cencinput in frame = " + Cencinput); } byte[] Cencinputbytes = Hex2Bytes(Cencinput); string lblL6HashKenccomputed = Bytes2Hex(CAES128.AES_CMAC(Cencinputbytes, kencbytes), 4); if (DEBUG) { System.Console.WriteLine("full hashKenc computed = " + Bytes2Hex(CAES128.AES_CMAC(Cencinputbytes, kencbytes), 16)); } if (lblL6HashKenccomputed.ToString().ToUpper() == lblL6HashKenc.ToString().ToUpper()) { if (DEBUG) { System.Console.WriteLine("hashKenc computed = " + lblL6HashKenccomputed + " hashKenc in frame = " + lblL6HashKenc + Environment.NewLine + "hashKenc OK" + Environment.NewLine); } return(true); } else { System.Console.WriteLine("Error hashKenc: hashKenc computed = " + lblL6HashKenccomputed + " hashKenc in frame = " + lblL6HashKenc); if (DEBUG) { return(true); } else { return(false); } } } else { return(false); } }
private static bool hashKmac_OK(string input_frame, byte[] kmacbytes, string lblL6Vers, bool DEBUG) { //hashKmac de la trame string l6 = input_frame.Substring(22, input_frame.Length - 26); string lblL6HashKmac = l6.Substring(l6.Length - 4, 4); if (lblL6Vers == "000") { //calcul du hashKmac à partir de la trame string Cmacinput = input_frame.Substring(0, input_frame.Length - 8); byte[] Cmacinputbytes = Hex2Bytes(Cmacinput); string lblL6HashKmaccomputed = Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 2); if (DEBUG) { System.Console.WriteLine(Environment.NewLine + "full hashKmac computed = " + Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 16)); } if (lblL6HashKmaccomputed.ToString().ToUpper() == lblL6HashKmac.ToString().ToUpper()) { if (DEBUG) { System.Console.WriteLine("hashKmac computed = " + lblL6HashKmaccomputed + " hashKmac in frame = " + lblL6HashKmac + Environment.NewLine + "hashKmac OK"); } return(true); } else { System.Console.WriteLine("Error hashKmac: hashKmac computed = " + lblL6HashKmaccomputed + " hashKmac in frame = " + lblL6HashKmac); if (DEBUG) { return(true); } else { return(false); } } } else if (lblL6Vers == "001") { //calcul du hashKmac à partir de la trame string lblMField = input_frame.Substring(4, 4); string lblAField = input_frame.Substring(8, 12); string block0 = lblMField + lblAField + "0000000000000000"; string Cmacinput = block0 + l6.Substring(0, l6.Length - 4); if (DEBUG) { System.Console.WriteLine("full Cmacinput in frame = " + Cmacinput); } byte[] Cmacinputbytes = Hex2Bytes(Cmacinput); string lblL6HashKmaccomputed = Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 2); if (DEBUG) { System.Console.WriteLine(Environment.NewLine + "full hashKmac computed = " + Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 16)); } if (lblL6HashKmaccomputed.ToString().ToUpper() == lblL6HashKmac.ToString().ToUpper()) { if (DEBUG) { System.Console.WriteLine("hashKmac computed = " + lblL6HashKmaccomputed + " hashKmac in frame = " + lblL6HashKmac + Environment.NewLine + "hashKmac OK"); } return(true); } else { System.Console.WriteLine("Error hashKmac: hashKmac computed = " + lblL6HashKmaccomputed + " hashKmac in frame = " + lblL6HashKmac + Environment.NewLine); if (DEBUG) { return(true); } else { return(false); } } } else { return(false); } }
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); }