diff --git a/client/loclass/cipher.c b/client/loclass/cipher.c index 942abd437..af11ce4cc 100644 --- a/client/loclass/cipher.c +++ b/client/loclass/cipher.c @@ -275,18 +275,14 @@ int testMAC() uint8_t calculated_mac[4] = {0}; doMAC(cc_nr,div_key, calculated_mac); - if(memcmp(calculated_mac, correct_MAC,4) == 0) - { + if (memcmp(calculated_mac, correct_MAC,4) == 0) { PrintAndLogDevice(SUCCESS, "MAC calculation OK!"); - - }else - { + } else { PrintAndLogDevice(FAILED, "FAILED: MAC calculation failed:"); printarr(" Calculated_MAC", calculated_mac, 4); printarr(" Correct_MAC ", correct_MAC, 4); - return 1; -} - + return 1; + } return 0; } #endif diff --git a/client/loclass/cipherutils.c b/client/loclass/cipherutils.c index a6f7e92d1..e22c88894 100644 --- a/client/loclass/cipherutils.c +++ b/client/loclass/cipherutils.c @@ -205,19 +205,17 @@ int testBitStream() uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF}; uint8_t output [] = {0,0,0,0,0,0,0,0}; BitstreamIn in = { input, sizeof(input) * 8,0}; - BitstreamOut out ={ output, 0,0} + BitstreamOut out = { output, 0,0} ; - while(bitsLeft(&in) > 0) - { + while (bitsLeft(&in) > 0) { pushBit(&out, headBit(&in)); //printf("Bits left: %d\n", bitsLeft(&in)); //printf("Bits out: %d\n", numBits(&out)); } - if(memcmp(input, output, sizeof(input)) == 0) - { + + if(memcmp(input, output, sizeof(input)) == 0) { PrintAndLogDevice(SUCCESS, " Bitstream test 1 ok"); - }else - { + } else { PrintAndLogDevice(FAILED, " Bitstream test 1 failed"); uint8_t i; for(i = 0 ; i < sizeof(input) ; i++) @@ -235,27 +233,24 @@ int testReversedBitstream() uint8_t reverse [] = {0,0,0,0,0,0,0,0}; uint8_t output [] = {0,0,0,0,0,0,0,0}; BitstreamIn in = { input, sizeof(input) * 8,0}; - BitstreamOut out ={ output, 0,0}; - BitstreamIn reversed_in ={ reverse, sizeof(input)*8,0}; - BitstreamOut reversed_out ={ reverse,0 ,0}; + BitstreamOut out = { output, 0,0}; + BitstreamIn reversed_in = { reverse, sizeof(input)*8,0}; + BitstreamOut reversed_out = { reverse,0 ,0}; - while(bitsLeft(&in) > 0) - { + while (bitsLeft(&in) > 0) { pushBit(&reversed_out, tailBit(&in)); } - while(bitsLeft(&reversed_in) > 0) - { + + while (bitsLeft(&reversed_in) > 0) { pushBit(&out, tailBit(&reversed_in)); } - if(memcmp(input, output, sizeof(input)) == 0) - { + + if (memcmp(input, output, sizeof(input)) == 0) { PrintAndLogDevice(SUCCESS, " Bitstream test 2 ok"); - }else - { + } else { PrintAndLogDevice(FAILED, " Bitstream test 2 failed"); uint8_t i; - for(i = 0 ; i < sizeof(input) ; i++) - { + for (i = 0 ; i < sizeof(input) ; i++) { PrintAndLogDevice(NORMAL, " IN %02x, MIDDLE: %02x, OUT %02x", input[i],reverse[i], output[i]); } return 1; diff --git a/client/loclass/elite_crack.c b/client/loclass/elite_crack.c index 068fdf59f..2002e348f 100644 --- a/client/loclass/elite_crack.c +++ b/client/loclass/elite_crack.c @@ -108,7 +108,7 @@ void permutekey_rev(uint8_t key[8], uint8_t dest[8]) { * @param val * @return */ -uint8_t rr(uint8_t val) { +inline uint8_t rr(uint8_t val) { return val >> 1 | (( val & 1) << 7); } @@ -118,7 +118,7 @@ uint8_t rr(uint8_t val) { * @param val * @return */ -uint8_t rl(uint8_t val) { +inline uint8_t rl(uint8_t val) { return val << 1 | (( val & 0x80) >> 7); } @@ -128,7 +128,7 @@ uint8_t rl(uint8_t val) { * @param val * @return */ -uint8_t swap(uint8_t val) { +inline uint8_t swap(uint8_t val) { return ((val >> 4) & 0xFF) | ((val &0xFF) << 4); } @@ -172,8 +172,8 @@ void rk(uint8_t *key, uint8_t n, uint8_t *outp_key) { return; } -static mbedtls_des_context ctx_enc = {0}; -static mbedtls_des_context ctx_dec = {0}; +static mbedtls_des_context ctx_enc; +static mbedtls_des_context ctx_dec; void desdecrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output) { uint8_t key_std_format[8] = {0}; @@ -431,7 +431,7 @@ int bruteforceItem(dumpdata item, uint16_t keytable[]) { * @return 0 for ok, 1 for failz */ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){ - mbedtls_des_context ctx_e = {0}; + mbedtls_des_context ctx_e; uint8_t z_0[8] = {0}; uint8_t y_0[8] = {0}; @@ -441,8 +441,8 @@ int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){ uint8_t result[8] = {0}; // y_0 and z_0 are the first 16 bytes of the keytable - memcpy(y_0,first16bytes,8); - memcpy(z_0,first16bytes+8,8); + memcpy(y_0, first16bytes,8); + memcpy(z_0, first16bytes+8,8); // Our DES-implementation uses the standard NIST // format for keys, thus must translate from iclass @@ -672,8 +672,6 @@ int testElite() { 60 3F 88 6F B8 6C 2C 93 0D 69 2C D5 20 3C C1 61 95 70 43 08 A0 2F FE B3 26 D7 98 0B 34 7B 47 70 A0 AB - - **** The 64-bit HS Custom Key Value = 5B7C62C491C11B39 **** */ uint8_t keytable[128] = {0}; diff --git a/client/loclass/ikeys.c b/client/loclass/ikeys.c index 6a68d80b1..aefbbaf99 100644 --- a/client/loclass/ikeys.c +++ b/client/loclass/ikeys.c @@ -69,8 +69,8 @@ From "Dismantling iclass": uint8_t pi[35] = {0x0F,0x17,0x1B,0x1D,0x1E,0x27,0x2B,0x2D,0x2E,0x33,0x35,0x39,0x36,0x3A,0x3C,0x47,0x4B,0x4D,0x4E,0x53,0x55,0x56,0x59,0x5A,0x5C,0x63,0x65,0x66,0x69,0x6A,0x6C,0x71,0x72,0x74,0x78}; -static mbedtls_des_context ctx_enc = {0}; -static mbedtls_des_context ctx_dec = {0}; +static mbedtls_des_context ctx_enc; +static mbedtls_des_context ctx_dec; static int debug_print = 0; @@ -442,11 +442,10 @@ int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context uint8_t des_encrypted_csn[8] = {0}; uint8_t decrypted[8] = {0}; uint8_t div_key[8] = {0}; - int retval = mbedtls_des_crypt_ecb(&ctx_enc,testcase.uid,des_encrypted_csn); - retval |= mbedtls_des_crypt_ecb(&ctx_dec,des_encrypted_csn,decrypted); + int retval = mbedtls_des_crypt_ecb(&ctx_enc, testcase.uid, des_encrypted_csn); + retval |= mbedtls_des_crypt_ecb(&ctx_dec, des_encrypted_csn, decrypted); - if(memcmp(testcase.uid,decrypted,8) != 0) - { + if (memcmp(testcase.uid, decrypted, 8) != 0) { //Decryption fail PrintAndLogDevice(FAILED, "Encryption <-> Decryption FAIL"); printarr("Input", testcase.uid, 8); @@ -454,8 +453,7 @@ int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context retval = 1; } - if(memcmp(des_encrypted_csn,testcase.t_key,8) != 0) - { + if (memcmp(des_encrypted_csn, testcase.t_key, 8) != 0) { //Encryption fail PrintAndLogDevice(FAILED, "Encryption != Expected result"); printarr("Output", des_encrypted_csn, 8); @@ -465,8 +463,7 @@ int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context uint64_t crypted_csn = x_bytes_to_num(des_encrypted_csn,8); hash0(crypted_csn, div_key); - if(memcmp(div_key, testcase.div_key ,8) != 0) - { + if (memcmp(div_key, testcase.div_key, 8) != 0) { //Key diversification fail PrintAndLogDevice(FAILED, "Div key != expected result"); printarr(" csn ", testcase.uid,8); @@ -575,13 +572,12 @@ Testcase testcases[] ={ }; int testKeyDiversificationWithMasterkeyTestcases() { - int error = 0; - int i; - uint8_t empty[8]={0}; + int i, error = 0; + uint8_t empty[8] = {0}; PrintAndLogDevice(INFO, "Testing encryption/decryption"); - for (i = 0; memcmp(testcases+i, empty, 8); i++) + for (i = 0; memcmp(testcases+i, empty, 8); i++) error += testDES(testcases[i], ctx_enc, ctx_dec); if (error) @@ -592,26 +588,26 @@ int testKeyDiversificationWithMasterkeyTestcases() { } void print64bits(char*name, uint64_t val) { - printf("%s%08x%08x\n",name,(uint32_t) (val >> 32) ,(uint32_t) (val & 0xFFFFFFFF)); + printf("%s%08x%08x\n", name, (uint32_t) (val >> 32) ,(uint32_t) (val & 0xFFFFFFFF)); } uint64_t testCryptedCSN(uint64_t crypted_csn, uint64_t expected) { int retval = 0; uint8_t result[8] = {0}; - if(debug_print) PrintAndLogDevice(DEBUG, "debug_print %d", debug_print); - if(debug_print) print64bits(" {csn} ", crypted_csn ); + if (debug_print) PrintAndLogDevice(DEBUG, "debug_print %d", debug_print); + if (debug_print) print64bits(" {csn} ", crypted_csn ); uint64_t crypted_csn_swapped = swapZvalues(crypted_csn); - if(debug_print) print64bits(" {csn-revz} ", crypted_csn_swapped); + if (debug_print) print64bits(" {csn-revz} ", crypted_csn_swapped); hash0(crypted_csn, result); uint64_t resultbyte = x_bytes_to_num(result,8 ); - if(debug_print) print64bits(" hash0 " , resultbyte ); + if (debug_print) print64bits(" hash0 " , resultbyte ); - if(resultbyte != expected ) { - if(debug_print) { + if (resultbyte != expected ) { + if (debug_print) { PrintAndLogDevice(NORMAL, "\n"); PrintAndLogDevice(FAILED, "FAIL!"); print64bits(" expected " , expected ); } @@ -627,9 +623,9 @@ int testDES2(uint64_t csn, uint64_t expected) { uint8_t input[8] = {0}; print64bits(" csn ", csn); - x_num_to_bytes(csn, 8,input); + x_num_to_bytes(csn, 8, input); - mbedtls_des_crypt_ecb(&ctx_enc,input, result); + mbedtls_des_crypt_ecb(&ctx_enc, input, result); uint64_t crypt_csn = x_bytes_to_num(result, 8); print64bits(" {csn} ", crypt_csn ); @@ -678,10 +674,21 @@ int doTestsWithKnownInputs() { static bool readKeyFile(uint8_t key[8]) { bool retval = false; - FILE *f = fopen("iclass_key.bin", "rb"); + + //Test a few variants + char filename[30]; + if (fileExists("iclass_key.bin")){ + sprintf(filename, "%s.bin", "iclass_key"); + } else if (fileExists("loclass/iclass_key.bin")){ + sprintf(filename, "%s.bin", "loclass/iclass_key"); + } else if (fileExists("client/loclass/iclass_key.bin")){ + sprintf(filename, "%s.bin", "client/loclass/iclass_key"); + } + + FILE *f = fopen(filename, "rb"); if (!f) return retval; - + size_t bytes_read = fread(key, sizeof(uint8_t), 8, f); if ( bytes_read == 8) retval = true; @@ -696,7 +703,7 @@ int doKeyTests(uint8_t debuglevel) { PrintAndLogDevice(INFO, "Checking if the master key is present (iclass_key.bin)..."); uint8_t key[8] = {0}; - if (readKeyFile(key)) { + if (!readKeyFile(key)) { PrintAndLogDevice(FAILED, "Master key not present, will not be able to do all testcases"); } else {