//----------------------------------------------------------------------------- // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // See LICENSE.txt for the text of the license. //----------------------------------------------------------------------------- // HID Config Card (JCOP / GlobalPlatform SCP02) simulation helpers. // Handles I-block dispatch for tagType=16 in SimulateIso14443aTag and the // A0 D4 jam logic for SniffIso14443a. //----------------------------------------------------------------------------- #include "secc.h" #include #include "proxmark3_arm.h" #include "dbprint.h" #include "BigBuf.h" // DMA_BUFFER_SIZE, MAX_PARITY_SIZE #include "crc16.h" // AddCrc14A, CheckCrc14A #include "fpgaloader.h" // FpgaWriteConfWord, FpgaSetupSscDma #include "desfire_crypto.h" // tdes_nxp_send #include "mbedtls/des.h" // mbedtls_des_*, mbedtls_des3_* #include "iso14443a.h" // ReaderTransmit, ReaderReceive, iso14a_get/set_timeout, iso14a_get/toggle_pcb_blocknum, MAX_ISO14A_TIMEOUT #include "appmain.h" // tearoff_hook, send_wtx #include "util.h" // data_available, BUTTON_PRESS #include "cmd.h" // reply_ng #include "pm3_cmd.h" // CMD_HF_HIDCONFIG_SIM, CMD_HF_HIDCONFIG_SNIFF #include "dbprint.h" // Dbprintf, LED_* #include "ticks.h" // WDT_HIT #include "protocols.h" // ISO14443A_CMD_* constants // --------------------------------------------------------------------------- // Internal constants // --------------------------------------------------------------------------- // Fixed card challenge used in SCP02 INITIALIZE UPDATE responses. static const uint8_t hid_cc[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; // --------------------------------------------------------------------------- // Custom APDU response table and SCP02 key (loaded from payload) // --------------------------------------------------------------------------- static hid_apdu_entry_t s_apdu_table[HID_APDU_MAX_ENTRIES]; static uint8_t s_apdu_count = 0; static uint8_t s_scp02_key[16] = {0}; // Jam config — set by SniffHIDConfigCard before entering sniff loop. // Length 0 means "use built-in default". static uint8_t s_jam_apdu[HID_JAM_MAX_APDU]; static uint8_t s_jam_apdu_len = 0; static uint8_t s_jam_resp[HID_JAM_MAX_RESP]; static uint8_t s_jam_resp_len = 0; static const uint8_t s_jam_apdu_default[5] = {0xA0, 0xD4, 0x00, 0x00, 0x00}; static const uint8_t s_jam_resp_default[4] = {0x00, 0x00, 0x90, 0x00}; void hid_config_card_set_apdu_table(const hid_apdu_entry_t *table, uint8_t count) { s_apdu_count = (count > HID_APDU_MAX_ENTRIES) ? HID_APDU_MAX_ENTRIES : count; memcpy(s_apdu_table, table, s_apdu_count * sizeof(hid_apdu_entry_t)); } static void hid_config_card_set_scp02_key(const uint8_t *key) { memcpy(s_scp02_key, key, 16); } // --------------------------------------------------------------------------- // Internal crypto // --------------------------------------------------------------------------- // Compute GlobalPlatform SCP02 card cryptogram. // card_cryptogram = Retail_MAC(S-ENC, host_challenge || card_challenge || 0x80 || 0x00*7) // Uses hardcoded master key 404142...4F and SN = 0x0001. static void compute_card_cryptogram(const uint8_t *host_challenge, uint8_t *out) { static const uint8_t SN[2] = {0x00, 0x01}; // Derive S-ENC: 3DES-CBC(K, zero_IV, {0x01, 0x82, SN0, SN1, 0x00*12}) uint8_t s_enc[16]; { uint8_t deriv[16] = {0x01, 0x82, SN[0], SN[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t iv[8] = {0}; tdes_nxp_send(deriv, s_enc, 16, s_scp02_key, iv, 2); } // Retail MAC over HC || CC || 0x80 || 0x00*7 mbedtls_des_context des_ctx; mbedtls_des3_context des3_ctx; mbedtls_des_init(&des_ctx); mbedtls_des3_init(&des3_ctx); mbedtls_des_setkey_enc(&des_ctx, s_enc); mbedtls_des3_set2key_enc(&des3_ctx, s_enc); uint8_t x[8] = {0}; uint8_t tmp[8]; // Block 1: host challenge for (int i = 0; i < 8; i++) tmp[i] = host_challenge[i] ^ x[i]; mbedtls_des_crypt_ecb(&des_ctx, tmp, x); // Block 2: card challenge for (int i = 0; i < 8; i++) tmp[i] = hid_cc[i] ^ x[i]; mbedtls_des_crypt_ecb(&des_ctx, tmp, x); // Block 3: 0x80 || 0x00*7 (ISO 9797-1 Method 2 padding) tmp[0] = 0x80 ^ x[0]; for (int i = 1; i < 8; i++) tmp[i] = x[i]; mbedtls_des3_crypt_ecb(&des3_ctx, tmp, out); mbedtls_des_free(&des_ctx); mbedtls_des3_free(&des3_ctx); } // --------------------------------------------------------------------------- // I-block handler (called from SimulateIso14443aTag for tagType=16) // --------------------------------------------------------------------------- bool hid_config_card_handle_iblock(const uint8_t *cmd, int len, tag_response_info_t *ri) { uint8_t pcb = cmd[0]; bool has_cid = (pcb & 0x08) != 0; // Payload starts after PCB [+ CID] int off = has_cid ? 2 : 1; // offset to APDU CLA byte // Echo PCB and mirror CID from the reader frame. ri->response[0] = pcb; if (has_cid) ri->response[1] = cmd[1]; // mirror CID byte from reader // Base pointer for APDU payload in the response (after PCB [CID]) uint8_t *rsp = &ri->response[off]; // ----- Check custom APDU table first (overrides hardcoded responses) ----- // cmd[off] is the start of the APDU payload; len includes trailing CRC (2 bytes). // apdu_mask bit i=1 → exact match; bit i=0 → wildcard: // apdu[i]=0x00 → any single byte (**); apdu[i]=0x01 → length-prefix skip (##). for (int i = 0; i < s_apdu_count; i++) { if (s_apdu_table[i].apdu_len == 0) continue; bool match = true; int recv_pos = 0; for (int j = 0; j < (int)s_apdu_table[i].apdu_len && match; j++) { bool is_exact = (s_apdu_table[i].apdu_mask[j / 8] & (1u << (j % 8))) != 0; if (off + recv_pos >= len - 2) { match = false; break; } if (is_exact) { if (cmd[off + recv_pos] != s_apdu_table[i].apdu[j]) match = false; recv_pos++; } else if (s_apdu_table[i].apdu[j] == 0x01) { // ## : read length byte, skip 1 + N bytes total uint8_t payload_len = cmd[off + recv_pos]; recv_pos += 1 + payload_len; } else { // ** : skip one byte recv_pos++; } } if (match) { memcpy(rsp, s_apdu_table[i].resp, s_apdu_table[i].resp_len); ri->response_n = off + s_apdu_table[i].resp_len; return true; } } // ----- SELECT AID (INS=0xA4, P1=0x04) ----- // no-CID frame: off=1, INS at cmd[2], Lc at cmd[4+1]=cmd[5], AID[6] at cmd[1+5+6]=cmd[12] // CID frame: off=2, INS at cmd[3], Lc at cmd[3+3]=cmd[6], AID[6] at cmd[2+5+6]=cmd[13] if (!has_cid && len >= 17 && cmd[2] == 0xA4 && cmd[3] == 0x04 && cmd[5] == 0x0A) { if (cmd[12] == 0x17) { rsp[0] = 0x6A; rsp[1] = 0x82; // File Not Found } else { rsp[0] = 0x90; rsp[1] = 0x00; } ri->response_n = off + 2; return true; } if (has_cid && len >= 18 && cmd[3] == 0xA4 && cmd[4] == 0x04 && cmd[6] == 0x0A) { if (cmd[13] == 0x17) { rsp[0] = 0x6A; rsp[1] = 0x82; // File Not Found } else { rsp[0] = 0x90; rsp[1] = 0x00; } ri->response_n = off + 2; return true; } // ----- A0 D4 00 00 00 (HID proprietary) ----- if (has_cid && len == 9 && cmd[2] == 0xA0 && cmd[3] == 0xD4 && cmd[4] == 0x00 && cmd[5] == 0x00 && cmd[6] == 0x00) { rsp[0] = 0x00; rsp[1] = 0x00; rsp[2] = 0x90; rsp[3] = 0x00; ri->response_n = off + 4; return true; } // ----- INITIALIZE UPDATE (INS=0x50) ----- // CID frame: INS at cmd[3], host challenge at cmd[off+4] = cmd[6] if (has_cid && len >= 17 && cmd[3] == 0x50) { uint8_t cryptogram[8]; compute_card_cryptogram(&cmd[off + 4], cryptogram); memset(rsp, 0x00, 10); // key diversification data rsp[10] = 0xFF; // key version (JCOP factory default) rsp[11] = 0x02; // SCP02 memcpy(rsp + 12, hid_cc, 8); // card challenge memcpy(rsp + 20, cryptogram, 8); // card cryptogram rsp[28] = 0x90; rsp[29] = 0x00; ri->response_n = off + 30; return true; } // ----- EXTERNAL AUTH and all other APDUs: generic 90 00 ----- rsp[0] = 0x90; rsp[1] = 0x00; ri->response_n = off + 2; return true; } // --------------------------------------------------------------------------- // Sniff jam handler (called from SniffIso14443a when param bit 2 is set) // --------------------------------------------------------------------------- bool hid_config_card_jam(const uint8_t *cmd, int len, uint8_t *dma_buf) { uint8_t pcb = cmd[0]; // Only act on I-blocks (bits 7:6 == 00) if ((pcb & 0xC0) != 0x00) return false; int off = (pcb & 0x08) ? 2 : 1; // skip CID if present // Select active APDU pattern and response (custom or default) const uint8_t *match = (s_jam_apdu_len > 0) ? s_jam_apdu : s_jam_apdu_default; int match_len = (s_jam_apdu_len > 0) ? (int)s_jam_apdu_len : (int)sizeof(s_jam_apdu_default); const uint8_t *resp_data = (s_jam_resp_len > 0) ? s_jam_resp : s_jam_resp_default; int resp_data_len = (s_jam_resp_len > 0) ? (int)s_jam_resp_len : (int)sizeof(s_jam_resp_default); if (len < off + match_len + 2) // off + APDU pattern + 2-byte CRC return false; if (memcmp(cmd + off, match, match_len) != 0) return false; // Build jam response: PCB [CID] CRC CRC uint8_t resp[2 + HID_JAM_MAX_RESP + 2]; // PCB + optional CID + payload + CRC int rlen = 0; resp[rlen++] = pcb; if (pcb & 0x08) resp[rlen++] = cmd[1]; // mirror CID memcpy(resp + rlen, resp_data, resp_data_len); rlen += resp_data_len; AddCrc14A(resp, rlen); rlen += 2; EmSendCmdEx(resp, rlen, false); // Restore sniffer FPGA mode and re-arm DMA FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER); FpgaSetupSscDma(dma_buf, DMA_BUFFER_SIZE); return true; } // --------------------------------------------------------------------------- // CID-aware ISO 14443-4 APDU exchange for HID Config Card reader interaction. // Sends I-blocks with PCB=0x0A (CID present, CID=0) as negotiated in RATS. // Strips PCB+CID from received responses before returning. // API mirrors iso14_apdu(). // --------------------------------------------------------------------------- int hid_config_card_iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, uint16_t data_len, uint8_t *res) { uint8_t parity[MAX_PARITY_SIZE] = {0}; uint8_t *real_cmd = BigBuf_calloc(cmd_len + 5); // PCB(1) + CID(1) + APDU + CRC(2) if (cmd_len) { real_cmd[0] = 0x0A; // I-block, CID present (bit 3), block number in bit 0 if (send_chaining) real_cmd[0] |= 0x10; real_cmd[0] |= iso14a_get_pcb_blocknum(); real_cmd[1] = 0x00; // CID = 0 (as negotiated in RATS) memcpy(real_cmd + 2, cmd, cmd_len); } else { real_cmd[0] = 0xAA; // R-block ACK + CID present real_cmd[0] |= iso14a_get_pcb_blocknum(); real_cmd[1] = 0x00; // CID = 0 } AddCrc14A(real_cmd, cmd_len + 2); // PCB + CID + APDU ReaderTransmit(real_cmd, cmd_len + 4, NULL); // PCB(1) + CID(1) + APDU + CRC(2) if (tearoff_hook() == PM3_ETEAROFF) { BigBuf_free(); return -1; } size_t len = ReaderReceive(data, data_len, parity); uint8_t *data_bytes = (uint8_t *)data; if (len == 0) { BigBuf_free(); return 0; } uint32_t save_timeout = iso14a_get_timeout(); // S-Block WTX while (len && ((data_bytes[0] & 0xF2) == 0xF2)) { if (BUTTON_PRESS() || data_available()) { BigBuf_free(); return -3; } send_wtx(38); data_bytes[1] &= 0x3F; iso14a_set_timeout(MAX(data_bytes[1] * save_timeout, MAX_ISO14A_TIMEOUT)); AddCrc14A(data_bytes, len - 2); ReaderTransmit(data_bytes, len, NULL); len = ReaderReceive(data_bytes, data_len, parity); } iso14a_set_timeout(save_timeout); // Toggle block number on valid I-block or R(ACK) if (len >= 3 && ((data_bytes[0] & 0xC0) == 0 || (data_bytes[0] & 0xD0) == 0x80) && (data_bytes[0] & 0x01) == iso14a_get_pcb_blocknum()) { iso14a_toggle_pcb_blocknum(); } if (res) *res = data_bytes[0]; if (len >= 3 && !CheckCrc14A(data_bytes, len)) { BigBuf_free(); return -1; } if (len) { // Strip PCB and CID from the front of the response int header_len = ((data_bytes[0] & 0x08) != 0) ? 2 : 1; len -= header_len; memmove(data_bytes, data_bytes + header_len, len); } BigBuf_free(); return len; } // --------------------------------------------------------------------------- // Full HID Config Card simulation (own loop, no iso14443a.c hooks) // --------------------------------------------------------------------------- void SimulateHIDConfigCard(const hid_sim_payload_t *payload) { hid_config_card_set_apdu_table(payload->apdu_table, payload->apdu_count); hid_config_card_set_scp02_key(payload->scp02_key); // Command buffers uint8_t receivedCmd[MAX_FRAME_SIZE]; uint8_t receivedCmdPar[MAX_PARITY_SIZE]; BigBuf_free_keep_EM(); #define HID_SIM_DYNAMIC_RESPONSE_SIZE 64 #define HID_SIM_DYNAMIC_MODULATION_SIZE 512 uint8_t *dyn_resp = BigBuf_calloc(HID_SIM_DYNAMIC_RESPONSE_SIZE); uint8_t *dyn_mod = BigBuf_calloc(HID_SIM_DYNAMIC_MODULATION_SIZE); if (dyn_resp == NULL || dyn_mod == NULL) { BigBuf_free_keep_EM(); reply_ng(CMD_HF_HIDCONFIG_SIM, PM3_EMALLOC, NULL, 0); return; } tag_response_info_t dynamic_response_info = { .response = dyn_resp, .response_n = 0, .modulation = dyn_mod, .modulation_n = 0 }; // Build flags: use provided UID, override ATQA/SAK/ATS with HID values. uint16_t flags = payload->flags | FLAG_ATS_IN_DATA | FLAG_ATQA_IN_DATA | FLAG_SAK_IN_DATA; uint8_t uid[10]; memcpy(uid, payload->uid, sizeof(uid)); uint16_t atqa_val = ((uint16_t)payload->atqa[0] << 8) | payload->atqa[1]; iso14a_set_atqa_sak_override(atqa_val, payload->sak); tag_response_info_t *responses; uint32_t cuid; uint8_t pages; if (SimulateIso14443aInit(4, flags, uid, (uint8_t *)payload->ats, payload->ats_len, &responses, &cuid, &pages, NULL) == false) { BigBuf_free_keep_EM(); reply_ng(CMD_HF_HIDCONFIG_SIM, PM3_EINIT, NULL, 0); return; } iso14443a_setup(FPGA_HF_ISO14443A_TAGSIM_LISTEN); iso14a_set_timeout(201400); int len = 0; clear_trace(); set_tracing(true); LED_A_ON(); uint8_t cardINTERACTIONS = 0; bool finished = false; while (finished == false) { WDT_HIT(); tag_response_info_t *p_response = NULL; if (GetIso14443aCommandFromReader(receivedCmd, sizeof(receivedCmd), receivedCmdPar, &len) == false) { finished = true; break; } dynamic_response_info.response_n = 0; dynamic_response_info.modulation_n = 0; if (receivedCmd[0] == ISO14443A_CMD_REQA && len == 1) { p_response = &responses[RESP_INDEX_ATQA]; } else if (receivedCmd[0] == ISO14443A_CMD_WUPA && len == 1) { p_response = &responses[RESP_INDEX_ATQA]; } else if (receivedCmd[1] == 0x20 && receivedCmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && len == 2) { p_response = &responses[RESP_INDEX_UIDC1]; } else if (receivedCmd[1] == 0x70 && receivedCmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && len == 9) { p_response = &responses[RESP_INDEX_SAKC1]; } else if (receivedCmd[0] == ISO14443A_CMD_RATS && len == 4) { p_response = &responses[RESP_INDEX_ATS]; } else if (receivedCmd[0] == ISO14443A_CMD_HALT && len == 4) { p_response = NULL; } else { // ISO 14443-4 I-block dispatch — HID-specific handling switch (receivedCmd[0]) { case 0x02: case 0x03: case 0x0A: case 0x0B: { hid_config_card_handle_iblock(receivedCmd, len, &dynamic_response_info); if (dynamic_response_info.response_n > 0) { // Mirror CID from reader frame when CID bit is set in PCB if (receivedCmd[0] & 0x08) dynamic_response_info.response[1] = receivedCmd[1]; AddCrc14A(dynamic_response_info.response, dynamic_response_info.response_n); dynamic_response_info.response_n += 2; if (prepare_tag_modulation(&dynamic_response_info, HID_SIM_DYNAMIC_MODULATION_SIZE)) p_response = &dynamic_response_info; } cardINTERACTIONS++; if (payload->exitAfter > 0 && cardINTERACTIONS >= payload->exitAfter) finished = true; break; } default: break; } } if (p_response != NULL) EmSendPrecompiledCmd(p_response); } set_tracing(false); LED_A_OFF(); BigBuf_free_keep_EM(); reply_ng(CMD_HF_HIDCONFIG_SIM, PM3_SUCCESS, NULL, 0); } // --------------------------------------------------------------------------- // HID Config Card sniff with optional A0 D4 jamming // --------------------------------------------------------------------------- void SniffHIDConfigCard(const hid_sniff_payload_t *payload) { // Configure jam pattern and response before entering the sniff loop. if (payload->apdu_len > 0 && payload->apdu_len <= HID_JAM_MAX_APDU) { memcpy(s_jam_apdu, payload->apdu, payload->apdu_len); s_jam_apdu_len = payload->apdu_len; } else { s_jam_apdu_len = 0; // use default A0 D4 00 00 00 } if (payload->resp_len > 0 && payload->resp_len <= HID_JAM_MAX_RESP) { memcpy(s_jam_resp, payload->resp, payload->resp_len); s_jam_resp_len = payload->resp_len; } else { s_jam_resp_len = 0; // use default 00 00 90 00 } // Delegate to SniffIso14443a. // When param bit 0x04 is set, SniffIso14443a calls hid_config_card_jam() // inline after each decoded reader frame (see iso14443a.c). SniffIso14443a(payload->param); }