diff --git a/armsrc/appmain.c b/armsrc/appmain.c index e07016406..52f4e30a6 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2297,15 +2297,28 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES: { - MifareAcquireEncryptedNonces(packet->oldarg[0], packet->oldarg[1], packet->oldarg[2], packet->data.asBytes); + if (packet->length != sizeof(mf_acquire_nonces_t)) { + reply_ng(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, PM3_EINVARG, NULL, 0); + break; + } + MifareAcquireEncryptedNonces((mf_acquire_nonces_t *)packet->data.asBytes); break; } case CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES: { - MifareAcquireStaticEncryptedNonces(packet->oldarg[0], packet->data.asBytes, true, packet->oldarg[1], packet->oldarg[2]); + if (packet->length != sizeof(mf_acquire_nonces_t)) { + reply_ng(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, PM3_EINVARG, NULL, 0); + break; + } + mf_acquire_nonces_t *payload = (mf_acquire_nonces_t *)packet->data.asBytes; + MifareAcquireStaticEncryptedNonces(payload->flags, payload->key, true, payload->blockno, payload->keytype); break; } case CMD_HF_MIFARE_ACQ_NONCES: { - MifareAcquireNonces(packet->oldarg[0], packet->oldarg[2]); + if (packet->length != sizeof(mf_acquire_nonces_t)) { + reply_ng(CMD_HF_MIFARE_ACQ_NONCES, PM3_EINVARG, NULL, 0); + break; + } + MifareAcquireNonces((mf_acquire_nonces_t *)packet->data.asBytes); break; } case CMD_HF_MIFARE_NESTED: { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 15470dc43..ab45ebdc2 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -985,7 +985,8 @@ static int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, const uint8_t ) ? 1 : 0; } -void MifareAcquireNonces(uint32_t arg0, uint32_t flags) { +void MifareAcquireNonces(const mf_acquire_nonces_t *payload) { + uint32_t flags = payload->flags; uint8_t uid[10] = {0x00}; uint8_t answer[MAX_MIFARE_FRAME_SIZE] = {0x00}; @@ -995,8 +996,8 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) { int16_t isOK = 0; uint16_t num_nonces = 0; uint8_t cascade_levels = 0; - uint8_t blockNo = arg0 & 0xff; - uint8_t keyType = (arg0 >> 8) & 0xff; + uint8_t blockNo = payload->blockno; + uint8_t keyType = payload->keytype; bool initialize = flags & 0x0001; bool field_off = flags & 0x0004; bool have_uid = false; @@ -1014,7 +1015,7 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) { LED_C_ON(); - while (num_nonces < PM3_CMD_DATA_SIZE / 4) { + while (num_nonces < MFC_MAX_NONCES) { // Test if the action was cancelled if (BUTTON_PRESS()) { @@ -1071,7 +1072,13 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) { LED_C_OFF(); LED_B_ON(); - reply_old(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf)); + uint8_t respbuf[PM3_CMD_DATA_SIZE] = {0x00}; + mf_nonces_resp_t *response = (mf_nonces_resp_t *)respbuf; + response->cuid = cuid; + response->num_nonces = num_nonces; + uint16_t noncelen = MIN((uint16_t)(num_nonces * 4), (uint16_t)(MFC_MAX_NONCES * 4)); + memcpy(response->nonces, buf, noncelen); + reply_ng(CMD_HF_MIFARE_ACQ_NONCES, isOK, respbuf, sizeof(mf_nonces_resp_t) + noncelen); LED_B_OFF(); if (g_dbglevel >= DBG_DEBUG) DbpString("AcquireNonces finished"); @@ -1089,7 +1096,8 @@ void MifareAcquireNonces(uint32_t arg0, uint32_t flags) { // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on // Computer and Communications Security, 2015 //----------------------------------------------------------------------------- -void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain) { +void MifareAcquireEncryptedNonces(const mf_acquire_nonces_t *payload) { + uint32_t flags = payload->flags; struct Crypto1State mpcs = {0, 0}; struct Crypto1State *pcs; @@ -1100,16 +1108,16 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t par_enc[1] = {0x00}; uint8_t buf[PM3_CMD_DATA_SIZE] = {0x00}; - uint64_t ui64Key = bytes_to_num(datain, 6); + uint64_t ui64Key = bytes_to_num(payload->key, 6); uint32_t cuid = 0; int16_t isOK = PM3_SUCCESS; uint16_t num_nonces = 0; uint8_t nt_par_enc = 0; uint8_t cascade_levels = 0; - uint8_t blockNo = arg0 & 0xff; - uint8_t keyType = (arg0 >> 8) & 0xff; - uint8_t targetBlockNo = arg1 & 0xff; - uint8_t targetKeyType = (arg1 >> 8) & 0xff; + uint8_t blockNo = payload->blockno; + uint8_t keyType = payload->keytype; + uint8_t targetBlockNo = payload->trg_blockno; + uint8_t targetKeyType = payload->trg_keytype; bool initialize = flags & 0x0001; bool slow = flags & 0x0002; bool field_off = flags & 0x0004; @@ -1131,7 +1139,7 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t prev_enc_nt[] = {0, 0, 0, 0}; uint8_t prev_counter = 0; - for (uint16_t i = 0; i <= PM3_CMD_DATA_SIZE - 9;) { + for (uint16_t i = 0; i <= (MFC_MAX_NONCES * 4) - 9;) { // Test if the action was cancelled if (BUTTON_PRESS()) { @@ -1223,7 +1231,13 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, LED_C_OFF(); crypto1_deinit(pcs); LED_B_ON(); - reply_old(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf)); + uint8_t respbuf[PM3_CMD_DATA_SIZE] = {0x00}; + mf_nonces_resp_t *response = (mf_nonces_resp_t *)respbuf; + response->cuid = cuid; + response->num_nonces = num_nonces; + uint16_t noncelen = MIN((uint16_t)(num_nonces * 4), (uint16_t)(MFC_MAX_NONCES * 4)); + memcpy(response->nonces, buf, noncelen); + reply_ng(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, isOK, respbuf, sizeof(mf_nonces_resp_t) + noncelen); LED_B_OFF(); if (field_off) { @@ -1508,7 +1522,12 @@ out: crypto1_deinit(pcs); LED_B_ON(); if (reply) { - reply_mix(CMD_ACK, isOK, cuid, 0, BigBuf_get_EM_addr() + CARD_MEMORY_RF08S_OFFSET, MIFARE_BLOCK_SIZE * (MIFARE_1K_MAXSECTOR + 1)); + uint8_t respbuf[sizeof(mf_nonces_resp_t) + (MIFARE_BLOCK_SIZE *(MIFARE_1K_MAXSECTOR + 1))] = {0x00}; + mf_nonces_resp_t *response = (mf_nonces_resp_t *)respbuf; + response->cuid = cuid; + response->num_nonces = 0; + memcpy(response->nonces, BigBuf_get_EM_addr() + CARD_MEMORY_RF08S_OFFSET, MIFARE_BLOCK_SIZE * (MIFARE_1K_MAXSECTOR + 1)); + reply_ng(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, isOK, respbuf, sizeof(respbuf)); } LED_B_OFF(); diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index fb0181465..09e90151b 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -37,9 +37,9 @@ void MifareU3PassChkKeys(mful_3passchk_t *packet); void MifareNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8_t targetKeyType, bool calibrate, uint8_t *key); void MifareStaticNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8_t targetKeyType, uint8_t *key, uint8_t forceDetectDist); -void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain); +void MifareAcquireEncryptedNonces(const mf_acquire_nonces_t *payload); int MifareAcquireStaticEncryptedNonces(uint32_t flags, const uint8_t *key, bool reply, uint8_t first_block_no, uint8_t first_key_type); -void MifareAcquireNonces(uint32_t arg0, uint32_t flags); +void MifareAcquireNonces(const mf_acquire_nonces_t *payload); void MifareChkKeys(uint8_t *datain, uint8_t reserved_mem); void MifareChkKeys_fast(const mf_chkkeys_fast_t *payload); void MifareChkKeys_file(uint8_t *fn); diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 786b8808d..da1fa1acc 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -6792,15 +6792,24 @@ static int CmdHF14AMfice(const char *Cmd) { flags |= initialize ? 0x0001 : 0; flags |= slow ? 0x0002 : 0; clearCommandBuffer(); - SendCommandMIX(CMD_HF_MIFARE_ACQ_NONCES, blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, flags, NULL, 0); + mf_acquire_nonces_t payload = { + .blockno = blockNo, + .keytype = keyType, + .trg_blockno = trgBlockNo, + .trg_keytype = trgKeyType, + .flags = flags, + }; + SendCommandNG(CMD_HF_MIFARE_ACQ_NONCES, (uint8_t *)&payload, sizeof(payload)); - if (WaitForResponseTimeout(CMD_ACK, &resp, 3000) == false) { + if (WaitForResponseTimeout(CMD_HF_MIFARE_ACQ_NONCES, &resp, 3000) == false) { goto out; } - if (resp.oldarg[0]) goto out; + if (resp.status != PM3_SUCCESS) goto out; + if (resp.length < sizeof(mf_nonces_resp_t)) goto out; - uint32_t items = resp.oldarg[2]; - fwrite(resp.data.asBytes, 1, items * 4, fnonces); + const mf_nonces_resp_t *nresp = (const mf_nonces_resp_t *)resp.data.asBytes; + uint32_t items = nresp->num_nonces; + fwrite(nresp->nonces, 1, items * 4, fnonces); fflush(fnonces); total_num_nonces += items; @@ -6824,7 +6833,14 @@ out: } clearCommandBuffer(); - SendCommandMIX(CMD_HF_MIFARE_ACQ_NONCES, blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, 4, NULL, 0); + mf_acquire_nonces_t off_payload = { + .blockno = blockNo, + .keytype = keyType, + .trg_blockno = trgBlockNo, + .trg_keytype = trgKeyType, + .flags = 4, + }; + SendCommandNG(CMD_HF_MIFARE_ACQ_NONCES, (uint8_t *)&off_payload, sizeof(off_payload)); return PM3_SUCCESS; } */ @@ -11879,34 +11895,42 @@ static int CmdHF14AMfISEN(const char *Cmd) { if (collect_fm11rf08s) { uint64_t t1 = msclock(); uint32_t flags = collect_fm11rf08s_with_data | (collect_fm11rf08s_without_backdoor << 1); - SendCommandMIX(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, flags, blockn, keytype, key, sizeof(key)); - if (WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { - if (resp.oldarg[0] != PM3_SUCCESS) { + mf_acquire_nonces_t snpayload = { + .blockno = blockn, + .keytype = keytype, + .flags = flags, + }; + memcpy(snpayload.key, key, sizeof(snpayload.key)); + SendCommandNG(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, (uint8_t *)&snpayload, sizeof(snpayload)); + if (WaitForResponseTimeout(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, &resp, 2500)) { + if (resp.status != PM3_SUCCESS || resp.length < sizeof(mf_nonces_resp_t)) { return NONCE_FAIL; } } else { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); return PM3_ETIMEOUT; } + + const uint8_t *snonces = ((const mf_nonces_resp_t *)resp.data.asBytes)->nonces; uint8_t num_sectors = MIFARE_1K_MAXSECTOR + 1; iso14a_fm11rf08s_nonces_with_data_t nonces_dump = {0}; for (uint8_t sec = 0; sec < num_sectors; sec++) { // reconstruct full nt uint32_t nt; - nt = bytes_to_num(resp.data.asBytes + ((sec * 2) * 8), 2); + nt = bytes_to_num(snonces + ((sec * 2) * 8), 2); nt = nt << 16 | prng_successor(nt, 16); num_to_bytes(nt, 4, nonces_dump.nt[sec][0]); - nt = bytes_to_num(resp.data.asBytes + (((sec * 2) + 1) * 8), 2); + nt = bytes_to_num(snonces + (((sec * 2) + 1) * 8), 2); nt = nt << 16 | prng_successor(nt, 16); num_to_bytes(nt, 4, nonces_dump.nt[sec][1]); } for (uint8_t sec = 0; sec < num_sectors; sec++) { - memcpy(nonces_dump.nt_enc[sec][0], resp.data.asBytes + ((sec * 2) * 8) + 4, 4); - memcpy(nonces_dump.nt_enc[sec][1], resp.data.asBytes + (((sec * 2) + 1) * 8) + 4, 4); + memcpy(nonces_dump.nt_enc[sec][0], snonces + ((sec * 2) * 8) + 4, 4); + memcpy(nonces_dump.nt_enc[sec][1], snonces + (((sec * 2) + 1) * 8) + 4, 4); } for (uint8_t sec = 0; sec < num_sectors; sec++) { - nonces_dump.par_err[sec][0] = resp.data.asBytes[((sec * 2) * 8) + 2]; - nonces_dump.par_err[sec][1] = resp.data.asBytes[(((sec * 2) + 1) * 8) + 2]; + nonces_dump.par_err[sec][0] = snonces[((sec * 2) * 8) + 2]; + nonces_dump.par_err[sec][1] = snonces[(((sec * 2) + 1) * 8) + 2]; } if (collect_fm11rf08s_with_data) { int bytes = MIFARE_1K_MAXBLOCK * MFBLOCK_SIZE; diff --git a/client/src/cmdhfmfhard.c b/client/src/cmdhfmfhard.c index f7c42cd99..c54482637 100644 --- a/client/src/cmdhfmfhard.c +++ b/client/src/cmdhfmfhard.c @@ -1681,23 +1681,37 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_ flags |= initialize ? 0x0001 : 0; flags |= slow ? 0x0002 : 0; flags |= field_off ? 0x0004 : 0; + mf_acquire_nonces_t payload = { + .blockno = blockNo, + .keytype = keyType, + .trg_blockno = trgBlockNo, + .trg_keytype = trgKeyType, + .flags = flags, + }; + memcpy(payload.key, key, sizeof(payload.key)); + clearCommandBuffer(); - SendCommandMIX(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, flags, key, 6); + SendCommandNG(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, (uint8_t *)&payload, sizeof(payload)); if (initialize) { - if (WaitForResponseTimeout(CMD_ACK, &resp, 3000) == false) { + if (WaitForResponseTimeout(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, &resp, 3000) == false) { DropField(); return PM3_ETIMEOUT; } // error during nested_hard - if (resp.oldarg[0]) { + if (resp.status != PM3_SUCCESS) { DropField(); - return resp.oldarg[0]; + return resp.status; } - cuid = resp.oldarg[1]; + if (resp.length < sizeof(mf_nonces_resp_t)) { + DropField(); + return PM3_ESOFT; + } + + cuid = ((const mf_nonces_resp_t *)resp.data.asBytes)->cuid; if (nonce_file_write && fnonces == NULL) { if ((fnonces = fopen(filename, "wb")) == NULL) { @@ -1718,8 +1732,9 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_ if (initialize == false) { - uint16_t num_sampled_nonces = resp.oldarg[2]; - uint8_t *bufp = resp.data.asBytes; + const mf_nonces_resp_t *nresp = (const mf_nonces_resp_t *)resp.data.asBytes; + uint16_t num_sampled_nonces = nresp->num_nonces; + const uint8_t *bufp = nresp->nonces; for (uint16_t i = 0; i < num_sampled_nonces; i += 2) { uint32_t nt_enc1 = bytes_to_num(bufp, 4); @@ -1797,7 +1812,7 @@ static int acquire_nonces(uint8_t blockNo, uint8_t keyType, uint8_t *key, uint8_ if (initialize == false) { - if (WaitForResponseTimeout(CMD_ACK, &resp, 3000) == false) { + if (WaitForResponseTimeout(CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES, &resp, 3000) == false) { if (nonce_file_write) { fclose(fnonces); } diff --git a/client/src/cmdhfmfsen.c b/client/src/cmdhfmfsen.c index 040dc95e8..a20a4542d 100644 --- a/client/src/cmdhfmfsen.c +++ b/client/src/cmdhfmfsen.c @@ -669,31 +669,40 @@ static int fm11_collect_nonces_ex(const uint8_t *key, uint32_t flags, uint8_t fi } memcpy(card, (iso14a_card_select_t *)resp.data.asBytes, sizeof(iso14a_card_select_t)); + mf_acquire_nonces_t payload = { + .blockno = first_block_no, + .keytype = first_key_type, + .flags = flags, + }; + memcpy(payload.key, key, sizeof(payload.key)); + clearCommandBuffer(); - SendCommandMIX(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, flags, first_block_no, first_key_type, key, MIFARE_KEY_SIZE); - if (WaitForResponseTimeout(CMD_ACK, &resp, 2500) == false) { + SendCommandNG(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, (uint8_t *)&payload, sizeof(payload)); + if (WaitForResponseTimeout(CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES, &resp, 2500) == false) { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); return PM3_ETIMEOUT; } - if (resp.oldarg[0] != PM3_SUCCESS) { + if (resp.status != PM3_SUCCESS || resp.length < sizeof(mf_nonces_resp_t)) { return PM3_ESOFT; } + const uint8_t *snonces = ((const mf_nonces_resp_t *)resp.data.asBytes)->nonces; + memset(nonces, 0, sizeof(*nonces)); for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { - uint32_t nt = bytes_to_num(resp.data.asBytes + ((sec * 2) * 8), 2); + uint32_t nt = bytes_to_num(snonces + ((sec * 2) * 8), 2); nt = (nt << 16) | prng_successor(nt, 16); num_to_bytes(nt, 4, nonces->nt[sec][0]); - nt = bytes_to_num(resp.data.asBytes + (((sec * 2) + 1) * 8), 2); + nt = bytes_to_num(snonces + (((sec * 2) + 1) * 8), 2); nt = (nt << 16) | prng_successor(nt, 16); num_to_bytes(nt, 4, nonces->nt[sec][1]); } for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { - memcpy(nonces->nt_enc[sec][0], resp.data.asBytes + ((sec * 2) * 8) + 4, 4); - memcpy(nonces->nt_enc[sec][1], resp.data.asBytes + (((sec * 2) + 1) * 8) + 4, 4); - nonces->par_err[sec][0] = resp.data.asBytes[((sec * 2) * 8) + 2]; - nonces->par_err[sec][1] = resp.data.asBytes[(((sec * 2) + 1) * 8) + 2]; + memcpy(nonces->nt_enc[sec][0], snonces + ((sec * 2) * 8) + 4, 4); + memcpy(nonces->nt_enc[sec][1], snonces + (((sec * 2) + 1) * 8) + 4, 4); + nonces->par_err[sec][0] = snonces[((sec * 2) * 8) + 2]; + nonces->par_err[sec][1] = snonces[(((sec * 2) + 1) * 8) + 2]; } if ((flags & 1) == 0) { diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index c42b307f2..2d2d40543 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -399,6 +399,30 @@ typedef struct { uint8_t key[6]; } PACKED mf_readsector_t; +// Request for the three nonce acquisition commands: +// CMD_HF_MIFARE_ACQ_NONCES, ACQ_ENCRYPTED_NONCES, ACQ_STATIC_ENCRYPTED_NONCES +// Replaces oldargs that bit-packed block/keytype pairs into 16 bit halves. +// The static variant reads blockno/keytype as its first block/key type and +// ignores the trg_ fields. +typedef struct { + uint8_t blockno; + uint8_t keytype; + uint8_t trg_blockno; + uint8_t trg_keytype; + uint32_t flags; + uint8_t key[6]; +} PACKED mf_acquire_nonces_t; + +// Reply for the same three commands. isOK travels in the NG status field. +typedef struct { + uint32_t cuid; + uint16_t num_nonces; + uint8_t nonces[]; +} PACKED mf_nonces_resp_t; + +// most 4 byte nonces that fit alongside the reply header in one frame +#define MFC_MAX_NONCES ((PM3_CMD_DATA_SIZE - sizeof(mf_nonces_resp_t)) / 4) + // CMD_HF_MIFARE_CHKKEYS_FAST payload. // Replaces three bit-packed oldargs: // arg0 = sectorcnt | firstchunk<<8 | lastchunk<<12 | singlesector_params<<16