+/*****************************************************************************
+ * WARNING
+ *
+ * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
+ *
+ * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
+ * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
+ * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
+ *
+ * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
+ *
+ *****************************************************************************
+ *
+ * This file is part of loclass. It is a reconstructon of the cipher engine
+ * used in iClass, and RFID techology.
+ *
+ * The implementation is based on the work performed by
+ * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
+ * Milosch Meriac in the paper "Dismantling IClass".
+ *
+ * Copyright (C) 2014 Martin Holst Swende
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation, or, at your option, any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with loclass. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ ****************************************************************************/
+
- uint8_t key_std_format[8] = {0};
- permutekey_rev(iclass_key, key_std_format);
- des_setkey_dec( &ctx_dec, key_std_format);
- des_crypt_ecb(&ctx_dec,input,output);
+ uint8_t key_std_format[8] = {0};
+ permutekey_rev(iclass_key, key_std_format);
+ mbedtls_des_setkey_dec( &ctx_dec, key_std_format);
+ mbedtls_des_crypt_ecb(&ctx_dec,input,output);
- uint8_t key_std_format[8] = {0};
- permutekey_rev(iclass_key, key_std_format);
- des_setkey_enc( &ctx_enc, key_std_format);
- des_crypt_ecb(&ctx_enc,input,output);
+ uint8_t key_std_format[8] = {0};
+ permutekey_rev(iclass_key, key_std_format);
+ mbedtls_des_setkey_enc( &ctx_enc, key_std_format);
+ mbedtls_des_crypt_ecb(&ctx_enc,input,output);
- uint8_t key64_negated[8] = {0};
- uint8_t z[8][8]={{0},{0}};
- uint8_t temp_output[8]={0};
- //calculate complement of key
- int i;
- for(i=0;i<8;i++)
- key64_negated[i]= ~key64[i];
-
- // Once again, key is on iclass-format
- desencrypt_iclass(key64, key64_negated, z[0]);
-
- prnlog("\nHigh security custom key (Kcus):");
- printvar("z0 ", z[0],8);
-
- uint8_t y[8][8]={{0},{0}};
-
- // y[0]=DES_dec(z[0],~key)
- // Once again, key is on iclass-format
- desdecrypt_iclass(z[0], key64_negated, y[0]);
- printvar("y0 ", y[0],8);
-
- for(i=1; i<8; i++)
- {
-
- // z [i] = DES dec (rk(K cus , i), z [i−1] )
- rk(key64, i, temp_output);
- //y [i] = DES enc (rk(K cus , i), y [i−1] )
-
- desdecrypt_iclass(temp_output,z[i-1], z[i]);
- desencrypt_iclass(temp_output,y[i-1], y[i]);
-
- }
- if(outp_keytable != NULL)
- {
- for(i = 0 ; i < 8 ; i++)
- {
- memcpy(outp_keytable+i*16,y[i],8);
- memcpy(outp_keytable+8+i*16,z[i],8);
- }
- }else
- {
- printarr_human_readable("hash2", outp_keytable,128);
- }
+ uint8_t key64_negated[8] = {0};
+ uint8_t z[8][8]={{0},{0}};
+ uint8_t temp_output[8]={0};
+ //calculate complement of key
+ int i;
+ for(i=0;i<8;i++)
+ key64_negated[i]= ~key64[i];
+
+ // Once again, key is on iclass-format
+ desencrypt_iclass(key64, key64_negated, z[0]);
+
+ prnlog("\nHigh security custom key (Kcus):");
+ printvar("z0 ", z[0],8);
+
+ uint8_t y[8][8]={{0},{0}};
+
+ // y[0]=DES_dec(z[0],~key)
+ // Once again, key is on iclass-format
+ desdecrypt_iclass(z[0], key64_negated, y[0]);
+ printvar("y0 ", y[0],8);
+
+ for(i=1; i<8; i++)
+ {
+
+ // z [i] = DES dec (rk(K cus , i), z [i−1] )
+ rk(key64, i, temp_output);
+ //y [i] = DES enc (rk(K cus , i), y [i−1] )
+
+ desdecrypt_iclass(temp_output,z[i-1], z[i]);
+ desencrypt_iclass(temp_output,y[i-1], y[i]);
+
+ }
+ if(outp_keytable != NULL)
+ {
+ for(i = 0 ; i < 8 ; i++)
+ {
+ memcpy(outp_keytable+i*16,y[i],8);
+ memcpy(outp_keytable+8+i*16,z[i],8);
+ }
+ }else
+ {
+ printarr_human_readable("hash2", outp_keytable,128);
+ }
- key_sel[0] = keytable[key_index[0]] & 0xFF;key_sel[1] = keytable[key_index[1]] & 0xFF;
- key_sel[2] = keytable[key_index[2]] & 0xFF;key_sel[3] = keytable[key_index[3]] & 0xFF;
- key_sel[4] = keytable[key_index[4]] & 0xFF;key_sel[5] = keytable[key_index[5]] & 0xFF;
- key_sel[6] = keytable[key_index[6]] & 0xFF;key_sel[7] = keytable[key_index[7]] & 0xFF;
+ key_sel[0] = keytable[key_index[0]] & 0xFF;
+ key_sel[1] = keytable[key_index[1]] & 0xFF;
+ key_sel[2] = keytable[key_index[2]] & 0xFF;
+ key_sel[3] = keytable[key_index[3]] & 0xFF;
+ key_sel[4] = keytable[key_index[4]] & 0xFF;
+ key_sel[5] = keytable[key_index[5]] & 0xFF;
+ key_sel[6] = keytable[key_index[6]] & 0xFF;
+ key_sel[7] = keytable[key_index[7]] & 0xFF;
+int _testHash1()
+{
+ uint8_t csn[8]= {0x01,0x02,0x03,0x04,0xF7,0xFF,0x12,0xE0};
+ uint8_t k[8] = {0};
+ hash1(csn, k);
+ uint8_t expected[8] = {0x7E,0x72,0x2F,0x40,0x2D,0x02,0x51,0x42};
+ if(memcmp(k,expected,8) != 0)
+ {
+ prnlog("Error with hash1!");
+ printarr("calculated", k, 8);
+ printarr("expected", expected, 8);
+ return 1;
+ }
+ return 0;
+}
- */
- uint8_t keytable[128] = {0};
- hash2(k_cus, keytable);
- printarr_human_readable("Hash2", keytable, 128);
- if(keytable[3] == 0xA1 && keytable[0x30] == 0xA3 && keytable[0x6F] == 0x95)
- {
- prnlog("[+] Hash2 looks fine...");
- }
-
- prnlog("[+] Testing key diversification ...");
+ */
+ uint8_t keytable[128] = {0};
+ hash2(k_cus, keytable);
+ printarr_human_readable("Hash2", keytable, 128);
+ if(keytable[3] == 0xA1 && keytable[0x30] == 0xA3 && keytable[0x6F] == 0x95)
+ {
+ prnlog("[+] Hash2 looks fine...");
+ }