SendCommand(&c);
}
+int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
+ uint16_t cmdc = 0;
+ *dataoutlen = 0;
+
+ if (activateField) {
+ UsbCommand resp;
+
+ // Anticollision + SELECT card
+ UsbCommand ca = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_CLEAR_TRACE, 0, 0}};
+ SendCommand(&ca);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
+ return 1;
+ }
+
+ // check result
+ if (resp.arg[0] == 0) {
+ PrintAndLog("14aRAW ERROR: No card in field.");
+ return 1;
+ }
+
+ if (resp.arg[0] != 1 && resp.arg[0] != 2) {
+ PrintAndLog("14aRAW ERROR: card not in iso14443-4. res=%d.", resp.arg[0]);
+ return 1;
+ }
+
+ if (resp.arg[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
+ // get ATS
+ UsbCommand cr = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, 2, 0}};
+ uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
+ memcpy(cr.d.asBytes, rats, 2);
+ SendCommand(&cr);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
+ return 1;
+ }
+
+ if (resp.arg[0] <= 0) { // ats_len
+ PrintAndLog("14aRAW ERROR: Can't get ATS.");
+ return 1;
+ }
+ }
+ }
+
+ if (leaveSignalON)
+ cmdc |= ISO14A_NO_DISCONNECT;
+
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | cmdc, (datainlen & 0xFFFF), 0}};
+ memcpy(c.d.asBytes, datain, datainlen);
+ SendCommand(&c);
+
+ uint8_t *recv;
+ UsbCommand resp;
+
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
+ recv = resp.d.asBytes;
+ int iLen = resp.arg[0];
+
+ *dataoutlen = iLen - 2;
+ if (*dataoutlen < 0)
+ *dataoutlen = 0;
+
+ if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
+ PrintAndLog("14aRAW ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
+ return 2;
+ }
+
+ memcpy(dataout, recv, *dataoutlen);
+
+ if(!iLen) {
+ PrintAndLog("14aRAW ERROR: No card response.");
+ return 1;
+ }
+
+ // CRC Check
+ if (iLen == -1) {
+ PrintAndLog("14aRAW ERROR: ISO 14443A CRC error.");
+ return 3;
+ }
+
+
+ } else {
+ PrintAndLog("14aRAW ERROR: Reply timeout.");
+ return 4;
+ }
+
+ return 0;
+}
+
int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
uint16_t cmdc = 0;
#include "mifare.h"\r
#include "mfkey.h"\r
#include "hardnested/hardnested_bf_core.h"\r
+#include "cliparser/cliparser.h"\r
+#include "cmdhf14a.h"\r
+#include <polarssl/aes.h>\r
\r
#define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up\r
\r
return tryDecryptWord(param_get32ex(Cmd,0,0,16),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),data,len/2);\r
}\r
\r
+int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){\r
+ uint8_t iiv[16] = {0};\r
+ if (iv)\r
+ memcpy(iiv, iv, 16);\r
+ \r
+ aes_context aes;\r
+ aes_init(&aes);\r
+ if (aes_setkey_enc(&aes, key, 128))\r
+ return 1;\r
+ if (aes_crypt_cbc(&aes, AES_ENCRYPT, length, iiv, input, output))\r
+ return 2;\r
+ aes_free(&aes);\r
+\r
+ return 0;\r
+}\r
+\r
+int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){\r
+ uint8_t iiv[16] = {0};\r
+ if (iv)\r
+ memcpy(iiv, iv, 16);\r
+ \r
+ aes_context aes;\r
+ aes_init(&aes);\r
+ if (aes_setkey_dec(&aes, key, 128))\r
+ return 1;\r
+ if (aes_crypt_cbc(&aes, AES_DECRYPT, length, iiv, input, output))\r
+ return 2;\r
+ aes_free(&aes);\r
+\r
+ return 0;\r
+}\r
+\r
+int CmdHF14AMfAuth4(const char *cmd) {\r
+ uint8_t keyn[20] = {0};\r
+ int keynlen = 0;\r
+ uint8_t key[16] = {0};\r
+ int keylen = 0;\r
+ uint8_t data[257] = {0};\r
+ int datalen = 0;\r
+ \r
+ uint8_t Rnd1[17] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00};\r
+ uint8_t Rnd2[17] = {0};\r
+ \r
+ \r
+ CLIParserInit("hf mf auth4", \r
+ "Executes AES authentication command in ISO14443-4", \r
+ "Usage:\n\thf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication\n"\r
+ "\thf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication\n");\r
+\r
+ void* argtable[] = {\r
+ arg_param_begin,\r
+ arg_str1(NULL, NULL, "<Key Num (HEX 2 bytes)>", NULL),\r
+ arg_str1(NULL, NULL, "<Key Value (HEX 16 bytes)>", NULL),\r
+ arg_param_end\r
+ };\r
+ CLIExecWithReturn(cmd, argtable, true);\r
+ \r
+ CLIGetStrWithReturn(1, keyn, &keynlen);\r
+ CLIGetStrWithReturn(2, key, &keylen);\r
+ CLIParserFree();\r
+ \r
+ if (keynlen != 2) {\r
+ PrintAndLog("ERROR: <Key Num> must be 2 bytes long instead of: %d", keynlen);\r
+ return 1;\r
+ }\r
+ \r
+ if (keylen != 16) {\r
+ PrintAndLog("ERROR: <Key Value> must be 16 bytes long instead of: %d", keylen);\r
+ return 1;\r
+ }\r
+\r
+ uint8_t cmd1[] = {0x0a, 0x00, 0x70, keyn[1], keyn[0], 0x00};\r
+ int res = ExchangeRAW14a(cmd1, sizeof(cmd1), true, true, data, sizeof(data), &datalen);\r
+ if (res) {\r
+ PrintAndLog("ERROR exchande raw error: %d", res);\r
+ return 2;\r
+ }\r
+ \r
+ PrintAndLog("<phase1: %s", sprint_hex(data, datalen));\r
+ \r
+ if (datalen < 3) {\r
+ PrintAndLog("ERROR: card response length: %d", datalen);\r
+ return 3;\r
+ }\r
+ \r
+ if (data[0] != 0x0a || data[1] != 0x00) {\r
+ PrintAndLog("ERROR: card response. Framing error. :%s", sprint_hex(data, 2));\r
+ return 3;\r
+ }\r
+\r
+ if (data[2] != 0x90) {\r
+ PrintAndLog("ERROR: card response error: %02x", data[2]);\r
+ return 3;\r
+ }\r
+\r
+ if (datalen != 19) {\r
+ PrintAndLog("ERROR: card response must be 16 bytes long instead of: %d", datalen);\r
+ return 3;\r
+ }\r
+ \r
+ aes_decode(NULL, key, &data[3], Rnd2, 16);\r
+ Rnd2[16] = Rnd2[0];\r
+ PrintAndLog("Rnd2: %s", sprint_hex(Rnd2, 16));\r
+\r
+ uint8_t cmd2[35] = {0};\r
+ cmd2[0] = 0x0b;\r
+ cmd2[1] = 0x00;\r
+ cmd2[2] = 0x72;\r
+\r
+ uint8_t raw[32] = {0};\r
+ memmove(raw, Rnd1, 16);\r
+ memmove(&raw[16], &Rnd2[1], 16);\r
+\r
+ aes_encode(NULL, key, raw, &cmd2[3], 32);\r
+ PrintAndLog(">phase2: %s", sprint_hex(cmd2, 35));\r
+ \r
+ res = ExchangeRAW14a(cmd2, sizeof(cmd2), false, false, data, sizeof(data), &datalen);\r
+ if (res) {\r
+ PrintAndLog("ERROR exchande raw error: %d", res);\r
+ DropField();\r
+ return 4;\r
+ }\r
+ \r
+ PrintAndLog("<phase2: %s", sprint_hex(data, datalen));\r
+\r
+ aes_decode(NULL, key, &data[3], raw, 32);\r
+ PrintAndLog("res: %s", sprint_hex(raw, 32));\r
+ \r
+ PrintAndLog("Rnd1`: %s", sprint_hex(&raw[4], 16));\r
+ if (memcmp(&raw[4], &Rnd1[1], 16)) {\r
+ PrintAndLog("\nERROR: Authentication FAILED. rnd not equal");\r
+ PrintAndLog("rnd1 reader: %s", sprint_hex(&Rnd1[1], 16));\r
+ PrintAndLog("rnd1 card: %s", sprint_hex(&raw[4], 16));\r
+ DropField();\r
+ return 5;\r
+ }\r
+\r
+ DropField();\r
+ PrintAndLog("\nAuthentication OK");\r
+ \r
+ return 0;\r
+}\r
+\r
static command_t CommandTable[] =\r
{\r
{"help", CmdHelp, 1, "This help"},\r
{"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
{"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
{"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
+ {"auth4", CmdHF14AMfAuth4, 0, "ISO14443-4 AES authentication"},\r
{"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
{"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
{"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},\r