From 54703035880367449a849f8b972fa469dea67cee Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 16 Oct 2024 01:48:36 +0200 Subject: [PATCH] prepare MifareAcquireStaticEncryptedNonces for standalone --- armsrc/appmain.c | 2 +- armsrc/mifarecmd.c | 17 +++++++++++------ armsrc/mifarecmd.h | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 823968ba2..96cf50d31 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1774,7 +1774,7 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_ACQ_STATIC_ENCRYPTED_NONCES: { - MifareAcquireStaticEncryptedNonces(packet->oldarg[0], packet->data.asBytes); + MifareAcquireStaticEncryptedNonces(packet->oldarg[0], packet->data.asBytes, true); break; } case CMD_HF_MIFARE_ACQ_NONCES: { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index be6c63e6d..c6efda5f0 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -1036,8 +1036,7 @@ void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, // acquire static encrypted nonces in order to perform the attack described in // Philippe Teuwen, "MIFARE Classic: exposing the static encrypted nonce variant" //----------------------------------------------------------------------------- -void MifareAcquireStaticEncryptedNonces(uint32_t flags, uint8_t *key) { - +int MifareAcquireStaticEncryptedNonces(uint32_t flags, const uint8_t *key, bool reply) { struct Crypto1State mpcs = {0, 0}; struct Crypto1State *pcs; pcs = &mpcs; @@ -1090,7 +1089,8 @@ void MifareAcquireStaticEncryptedNonces(uint32_t flags, uint8_t *key) { iso14a_card_select_t card_info; if (iso14443a_select_card(uid, &card_info, &cuid, true, 0, true) == 0) { if (g_dbglevel >= DBG_ERROR) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (ALL)"); - continue; + isOK = PM3_ERFTRANS; + goto out; } switch (card_info.uidlen) { case 4 : @@ -1109,7 +1109,8 @@ void MifareAcquireStaticEncryptedNonces(uint32_t flags, uint8_t *key) { } else { // no need for anticollision. We can directly select the card if (iso14443a_fast_select_card(uid, cascade_levels) == 0) { if (g_dbglevel >= DBG_ERROR) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (UID)"); - continue; + isOK = PM3_ERFTRANS; + goto out; } } @@ -1156,7 +1157,8 @@ void MifareAcquireStaticEncryptedNonces(uint32_t flags, uint8_t *key) { if (iso14443a_fast_select_card(uid, cascade_levels) == 0) { if (g_dbglevel >= DBG_ERROR) Dbprintf("AcquireStaticEncryptedNonces: Can't select card (UID)"); - continue; + isOK = PM3_ERFTRANS; + goto out; } if (mifare_classic_authex_cmd(pcs, cuid, blockNo, MIFARE_AUTH_KEYA + keyType + 4, ui64Key, AUTH_FIRST, &nt1, NULL, NULL, NULL, false, false)) { if (g_dbglevel >= DBG_ERROR) Dbprintf("AcquireStaticEncryptedNonces: Auth1 error"); @@ -1191,12 +1193,15 @@ out: LED_C_OFF(); crypto1_deinit(pcs); LED_B_ON(); - reply_old(CMD_ACK, isOK, cuid, 0, BigBuf_get_EM_addr() + CARD_MEMORY_RF08S_OFFSET, MIFARE_BLOCK_SIZE * (MIFARE_1K_MAXSECTOR + 1)); + if (reply) { + reply_old(CMD_ACK, isOK, cuid, 0, BigBuf_get_EM_addr() + CARD_MEMORY_RF08S_OFFSET, MIFARE_BLOCK_SIZE * (MIFARE_1K_MAXSECTOR + 1)); + } LED_B_OFF(); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); set_tracing(false); + return isOK; } diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index a46982a55..8f19528c2 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -37,7 +37,7 @@ void MifareNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8 void MifareStaticNested(uint8_t blockNo, uint8_t keyType, uint8_t targetBlockNo, uint8_t targetKeyType, uint8_t *key); void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain); -void MifareAcquireStaticEncryptedNonces(uint32_t flags, uint8_t *key); +int MifareAcquireStaticEncryptedNonces(uint32_t flags, const uint8_t *key, bool reply); void MifareAcquireNonces(uint32_t arg0, uint32_t flags); void MifareChkKeys(uint8_t *datain, uint8_t reserved_mem); void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);