X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/b35e04a7c6a8b6c99fcdc8f45e387437cbae4f08..27d06e044795c0b0ea4d34b10bccfa41d57f77fc:/armsrc/mifareutil.c diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index 38ca934a..ab04aee4 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -21,7 +21,7 @@ #include "iso14443crc.h" #include "iso14443a.h" #include "crapto1/crapto1.h" -#include "polarssl/des.h" +#include "mbedtls/des.h" int MF_DBGLEVEL = MF_DBG_ALL; @@ -296,7 +296,7 @@ int mifare_ultra_auth(uint8_t *keybytes){ /// 3des2k - des3_context ctx = { 0x00 }; + mbedtls_des3_context ctx = { {0} }; uint8_t random_a[8] = {1,1,1,1,1,1,1,1}; uint8_t random_b[8] = {0x00}; uint8_t enc_random_b[8] = {0x00}; @@ -321,9 +321,9 @@ int mifare_ultra_auth(uint8_t *keybytes){ // decrypt nonce. // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV ); - des3_set2key_dec(&ctx, key); - des3_crypt_cbc(&ctx // des3_context - , DES_DECRYPT // int mode + mbedtls_des3_set2key_dec(&ctx, key); + mbedtls_des3_crypt_cbc(&ctx // des3_context + , MBEDTLS_DES_DECRYPT // int mode , sizeof(random_b) // length , IV // iv[8] , enc_random_b // input @@ -350,9 +350,9 @@ int mifare_ultra_auth(uint8_t *keybytes){ // encrypt out, in, length, key, iv //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b); - des3_set2key_enc(&ctx, key); - des3_crypt_cbc(&ctx // des3_context - , DES_ENCRYPT // int mode + mbedtls_des3_set2key_enc(&ctx, key); + mbedtls_des3_crypt_cbc(&ctx // des3_context + , MBEDTLS_DES_ENCRYPT // int mode , sizeof(rnd_ab) // length , enc_random_b // iv[8] , rnd_ab // input @@ -372,9 +372,9 @@ int mifare_ultra_auth(uint8_t *keybytes){ // decrypt out, in, length, key, iv // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b); - des3_set2key_dec(&ctx, key); - des3_crypt_cbc(&ctx // des3_context - , DES_DECRYPT // int mode + mbedtls_des3_set2key_dec(&ctx, key); + mbedtls_des3_crypt_cbc(&ctx // des3_context + , MBEDTLS_DES_DECRYPT // int mode , 8 // length , enc_random_b // iv[8] , enc_resp // input @@ -405,31 +405,48 @@ int mifare_ultra_auth(uint8_t *keybytes){ return 1; } + +#define MFU_MAX_RETRIES 5 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData) { uint16_t len; uint8_t bt[2]; uint8_t receivedAnswer[MAX_FRAME_SIZE]; uint8_t receivedAnswerPar[MAX_PARITY_SIZE]; - + uint8_t retries; + int result = 0; - len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL); - if (len == 1) { - if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); - return 1; - } - if (len != 18) { - if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len); - return 2; + for (retries = 0; retries < MFU_MAX_RETRIES; retries++) { + len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL); + if (len == 1) { + if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); + result = 1; + continue; + } + if (len != 18) { + if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len); + result = 2; + continue; + } + + memcpy(bt, receivedAnswer + 16, 2); + AppendCrc14443a(receivedAnswer, 16); + if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) { + if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error."); + result = 3; + continue; + } + + // No errors encountered; don't retry + result = 0; + break; } - - memcpy(bt, receivedAnswer + 16, 2); - AppendCrc14443a(receivedAnswer, 16); - if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) { - if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error."); - return 3; + + if (result != 0) { + Dbprintf("Cmd Error: too many retries; read failed"); + return result; } - + memcpy(blockData, receivedAnswer, 14); return 0; }