fix: 'hf iclass loclass' - remove warning on gcc4.9.2 mingw.

don't like the declaration.   mbedtls_des_context ctx_dec = {0};
chg: 'hf iclass loclass t' - added some path tests for key file.
This commit is contained in:
Chris 2018-11-14 15:11:30 +01:00
parent e4cb74fedb
commit 526f50aa9d
4 changed files with 60 additions and 64 deletions

View file

@ -275,18 +275,14 @@ int testMAC()
uint8_t calculated_mac[4] = {0}; uint8_t calculated_mac[4] = {0};
doMAC(cc_nr,div_key, calculated_mac); 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!"); PrintAndLogDevice(SUCCESS, "MAC calculation OK!");
} else {
}else
{
PrintAndLogDevice(FAILED, "FAILED: MAC calculation failed:"); PrintAndLogDevice(FAILED, "FAILED: MAC calculation failed:");
printarr(" Calculated_MAC", calculated_mac, 4); printarr(" Calculated_MAC", calculated_mac, 4);
printarr(" Correct_MAC ", correct_MAC, 4); printarr(" Correct_MAC ", correct_MAC, 4);
return 1; return 1;
} }
return 0; return 0;
} }
#endif #endif

View file

@ -205,19 +205,17 @@ int testBitStream()
uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF}; uint8_t input [] = {0xDE,0xAD,0xBE,0xEF,0xDE,0xAD,0xBE,0xEF};
uint8_t output [] = {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}; 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)); pushBit(&out, headBit(&in));
//printf("Bits left: %d\n", bitsLeft(&in)); //printf("Bits left: %d\n", bitsLeft(&in));
//printf("Bits out: %d\n", numBits(&out)); //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"); PrintAndLogDevice(SUCCESS, " Bitstream test 1 ok");
}else } else {
{
PrintAndLogDevice(FAILED, " Bitstream test 1 failed"); PrintAndLogDevice(FAILED, " Bitstream test 1 failed");
uint8_t i; uint8_t i;
for(i = 0 ; i < sizeof(input) ; 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 reverse [] = {0,0,0,0,0,0,0,0};
uint8_t output [] = {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}; BitstreamIn in = { input, sizeof(input) * 8,0};
BitstreamOut out ={ output, 0,0}; BitstreamOut out = { output, 0,0};
BitstreamIn reversed_in ={ reverse, sizeof(input)*8,0}; BitstreamIn reversed_in = { reverse, sizeof(input)*8,0};
BitstreamOut reversed_out ={ reverse,0 ,0}; BitstreamOut reversed_out = { reverse,0 ,0};
while(bitsLeft(&in) > 0) while (bitsLeft(&in) > 0) {
{
pushBit(&reversed_out, tailBit(&in)); pushBit(&reversed_out, tailBit(&in));
} }
while(bitsLeft(&reversed_in) > 0)
{ while (bitsLeft(&reversed_in) > 0) {
pushBit(&out, tailBit(&reversed_in)); 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"); PrintAndLogDevice(SUCCESS, " Bitstream test 2 ok");
}else } else {
{
PrintAndLogDevice(FAILED, " Bitstream test 2 failed"); PrintAndLogDevice(FAILED, " Bitstream test 2 failed");
uint8_t i; 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]); PrintAndLogDevice(NORMAL, " IN %02x, MIDDLE: %02x, OUT %02x", input[i],reverse[i], output[i]);
} }
return 1; return 1;

View file

@ -108,7 +108,7 @@ void permutekey_rev(uint8_t key[8], uint8_t dest[8]) {
* @param val * @param val
* @return * @return
*/ */
uint8_t rr(uint8_t val) { inline uint8_t rr(uint8_t val) {
return val >> 1 | (( val & 1) << 7); return val >> 1 | (( val & 1) << 7);
} }
@ -118,7 +118,7 @@ uint8_t rr(uint8_t val) {
* @param val * @param val
* @return * @return
*/ */
uint8_t rl(uint8_t val) { inline uint8_t rl(uint8_t val) {
return val << 1 | (( val & 0x80) >> 7); return val << 1 | (( val & 0x80) >> 7);
} }
@ -128,7 +128,7 @@ uint8_t rl(uint8_t val) {
* @param val * @param val
* @return * @return
*/ */
uint8_t swap(uint8_t val) { inline uint8_t swap(uint8_t val) {
return ((val >> 4) & 0xFF) | ((val &0xFF) << 4); return ((val >> 4) & 0xFF) | ((val &0xFF) << 4);
} }
@ -172,8 +172,8 @@ void rk(uint8_t *key, uint8_t n, uint8_t *outp_key) {
return; return;
} }
static mbedtls_des_context ctx_enc = {0}; static mbedtls_des_context ctx_enc;
static mbedtls_des_context ctx_dec = {0}; static mbedtls_des_context ctx_dec;
void desdecrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output) { void desdecrypt_iclass(uint8_t *iclass_key, uint8_t *input, uint8_t *output) {
uint8_t key_std_format[8] = {0}; 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 * @return 0 for ok, 1 for failz
*/ */
int calculateMasterKey(uint8_t first16bytes[], uint64_t master_key[] ){ 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 z_0[8] = {0};
uint8_t y_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}; uint8_t result[8] = {0};
// y_0 and z_0 are the first 16 bytes of the keytable // y_0 and z_0 are the first 16 bytes of the keytable
memcpy(y_0,first16bytes,8); memcpy(y_0, first16bytes,8);
memcpy(z_0,first16bytes+8,8); memcpy(z_0, first16bytes+8,8);
// Our DES-implementation uses the standard NIST // Our DES-implementation uses the standard NIST
// format for keys, thus must translate from iclass // 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 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 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 **** **** The 64-bit HS Custom Key Value = 5B7C62C491C11B39 ****
*/ */
uint8_t keytable[128] = {0}; uint8_t keytable[128] = {0};

View file

@ -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}; 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_enc;
static mbedtls_des_context ctx_dec = {0}; static mbedtls_des_context ctx_dec;
static int debug_print = 0; 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 des_encrypted_csn[8] = {0};
uint8_t decrypted[8] = {0}; uint8_t decrypted[8] = {0};
uint8_t div_key[8] = {0}; uint8_t div_key[8] = {0};
int retval = mbedtls_des_crypt_ecb(&ctx_enc,testcase.uid,des_encrypted_csn); int retval = mbedtls_des_crypt_ecb(&ctx_enc, testcase.uid, des_encrypted_csn);
retval |= mbedtls_des_crypt_ecb(&ctx_dec,des_encrypted_csn,decrypted); 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 //Decryption fail
PrintAndLogDevice(FAILED, "Encryption <-> Decryption FAIL"); PrintAndLogDevice(FAILED, "Encryption <-> Decryption FAIL");
printarr("Input", testcase.uid, 8); printarr("Input", testcase.uid, 8);
@ -454,8 +453,7 @@ int testDES(Testcase testcase, mbedtls_des_context ctx_enc, mbedtls_des_context
retval = 1; retval = 1;
} }
if(memcmp(des_encrypted_csn,testcase.t_key,8) != 0) if (memcmp(des_encrypted_csn, testcase.t_key, 8) != 0) {
{
//Encryption fail //Encryption fail
PrintAndLogDevice(FAILED, "Encryption != Expected result"); PrintAndLogDevice(FAILED, "Encryption != Expected result");
printarr("Output", des_encrypted_csn, 8); 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); uint64_t crypted_csn = x_bytes_to_num(des_encrypted_csn,8);
hash0(crypted_csn, div_key); 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 //Key diversification fail
PrintAndLogDevice(FAILED, "Div key != expected result"); PrintAndLogDevice(FAILED, "Div key != expected result");
printarr(" csn ", testcase.uid,8); printarr(" csn ", testcase.uid,8);
@ -575,13 +572,12 @@ Testcase testcases[] ={
}; };
int testKeyDiversificationWithMasterkeyTestcases() { int testKeyDiversificationWithMasterkeyTestcases() {
int error = 0; int i, error = 0;
int i; uint8_t empty[8] = {0};
uint8_t empty[8]={0};
PrintAndLogDevice(INFO, "Testing encryption/decryption"); 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); error += testDES(testcases[i], ctx_enc, ctx_dec);
if (error) if (error)
@ -592,26 +588,26 @@ int testKeyDiversificationWithMasterkeyTestcases() {
} }
void print64bits(char*name, uint64_t val) { 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) uint64_t testCryptedCSN(uint64_t crypted_csn, uint64_t expected)
{ {
int retval = 0; int retval = 0;
uint8_t result[8] = {0}; uint8_t result[8] = {0};
if(debug_print) PrintAndLogDevice(DEBUG, "debug_print %d", debug_print); if (debug_print) PrintAndLogDevice(DEBUG, "debug_print %d", debug_print);
if(debug_print) print64bits(" {csn} ", crypted_csn ); if (debug_print) print64bits(" {csn} ", crypted_csn );
uint64_t crypted_csn_swapped = swapZvalues(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); hash0(crypted_csn, result);
uint64_t resultbyte = x_bytes_to_num(result,8 ); 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 (resultbyte != expected ) {
if(debug_print) { if (debug_print) {
PrintAndLogDevice(NORMAL, "\n"); PrintAndLogDevice(FAILED, "FAIL!"); PrintAndLogDevice(NORMAL, "\n"); PrintAndLogDevice(FAILED, "FAIL!");
print64bits(" expected " , expected ); print64bits(" expected " , expected );
} }
@ -627,9 +623,9 @@ int testDES2(uint64_t csn, uint64_t expected) {
uint8_t input[8] = {0}; uint8_t input[8] = {0};
print64bits(" csn ", csn); 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); uint64_t crypt_csn = x_bytes_to_num(result, 8);
print64bits(" {csn} ", crypt_csn ); print64bits(" {csn} ", crypt_csn );
@ -678,10 +674,21 @@ int doTestsWithKnownInputs() {
static bool readKeyFile(uint8_t key[8]) { static bool readKeyFile(uint8_t key[8]) {
bool retval = false; 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) if (!f)
return retval; return retval;
size_t bytes_read = fread(key, sizeof(uint8_t), 8, f); size_t bytes_read = fread(key, sizeof(uint8_t), 8, f);
if ( bytes_read == 8) if ( bytes_read == 8)
retval = true; retval = true;
@ -696,7 +703,7 @@ int doKeyTests(uint8_t debuglevel) {
PrintAndLogDevice(INFO, "Checking if the master key is present (iclass_key.bin)..."); PrintAndLogDevice(INFO, "Checking if the master key is present (iclass_key.bin)...");
uint8_t key[8] = {0}; 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"); PrintAndLogDevice(FAILED, "Master key not present, will not be able to do all testcases");
} else { } else {