diff --git a/armsrc/epa.c b/armsrc/epa.c index d2bdeb8dd..aff1d4bb4 100644 --- a/armsrc/epa.c +++ b/armsrc/epa.c @@ -312,8 +312,14 @@ static void EPA_PACE_Collect_Nonce_Abort(uint32_t cmd, uint8_t step, int func_re // power down the field EPA_Finish(); - // send the USB packet - reply_mix(cmd, step, func_return, 0, 0, 0); + // send the full struct including a zeroed timing array: EPA_PACE_Simulate + // aborts through here too and its client reads timings unconditionally + uint8_t buf[sizeof(epa_result_t) + (5 * sizeof(uint32_t))] = {0}; + epa_result_t *response = (epa_result_t *)buf; + response->step = step; + response->len = 0; + response->func_return = (int16_t)func_return; + reply_ng(cmd, PM3_ESOFT, buf, sizeof(buf)); } //----------------------------------------------------------------------------- @@ -385,7 +391,13 @@ void EPA_PACE_Collect_Nonce(const PacketCommandNG *c) { EPA_Finish(); // save received information - reply_mix(CMD_HF_EPA_COLLECT_NONCE, 0, func_return, 0, nonce, func_return); + uint8_t noncebuf[sizeof(epa_result_t) + 256] = {0}; + epa_result_t *nresponse = (epa_result_t *)noncebuf; + nresponse->step = 0; + nresponse->len = (uint8_t)func_return; + nresponse->func_return = (int16_t)func_return; + memcpy(noncebuf + sizeof(epa_result_t), nonce, func_return); + reply_ng(CMD_HF_EPA_COLLECT_NONCE, PM3_SUCCESS, noncebuf, sizeof(epa_result_t) + func_return); } //----------------------------------------------------------------------------- @@ -498,26 +510,53 @@ static int EPA_PACE_MSE_Set_AT(const pace_version_info_t pace_version_info, uint //----------------------------------------------------------------------------- // Perform the PACE protocol by replaying given APDUs //----------------------------------------------------------------------------- +static void reply_epa_result(uint16_t cmd, uint8_t step, int func_return, const uint32_t *timings) { + uint8_t buf[sizeof(epa_result_t) + (5 * sizeof(uint32_t))] = {0}; + epa_result_t *response = (epa_result_t *)buf; + response->step = step; + response->len = 0; + response->func_return = (int16_t)func_return; + // always send the full timing array, zeroed when there is nothing measured + // yet, so the client never reads past the end of a short reply + uint16_t tlen = 5 * sizeof(uint32_t); + if (timings) { + memcpy(buf + sizeof(epa_result_t), timings, tlen); + } + reply_ng(cmd, (step == 0) ? PM3_SUCCESS : PM3_ESOFT, buf, sizeof(epa_result_t) + tlen); +} + void EPA_PACE_Replay(const PacketCommandNG *c) { uint32_t timings[ARRAYLEN(apdu_lengths_replay)] = {0}; + if (c->length < sizeof(epa_replay_t)) { + reply_ng(CMD_HF_EPA_REPLAY, PM3_EINVARG, NULL, 0); + return; + } + + const epa_replay_t *payload = (const epa_replay_t *)c->data.asBytes; + // if an APDU has been passed, save it - if (c->oldarg[0] != 0) { + if (payload->apdu_num != 0) { + // make sure it's not too big - if (c->oldarg[2] > apdus_replay[c->oldarg[0] - 1].len) { - reply_mix(CMD_ACK, 1, 0, 0, NULL, 0); + if ((payload->len > apdus_replay[payload->apdu_num - 1].len) || + (payload->len > (c->length - sizeof(epa_replay_t)))) { + reply_ng(CMD_HF_EPA_REPLAY, PM3_EINVARG, NULL, 0); + return; } - memcpy(apdus_replay[c->oldarg[0] - 1].data + c->oldarg[1], - c->data.asBytes, - c->oldarg[2]); + + memcpy(apdus_replay[payload->apdu_num - 1].data + payload->offset, + payload->data, + payload->len); + // save/update APDU length - if (c->oldarg[1] == 0) { - apdu_lengths_replay[c->oldarg[0] - 1] = c->oldarg[2]; + if (payload->offset == 0) { + apdu_lengths_replay[payload->apdu_num - 1] = payload->len; } else { - apdu_lengths_replay[c->oldarg[0] - 1] += c->oldarg[2]; + apdu_lengths_replay[payload->apdu_num - 1] += payload->len; } - reply_mix(CMD_ACK, 0, 0, 0, NULL, 0); + reply_ng(CMD_HF_EPA_REPLAY, PM3_SUCCESS, NULL, 0); return; } @@ -528,7 +567,7 @@ void EPA_PACE_Replay(const PacketCommandNG *c) { func_return = EPA_Setup(); if (func_return != 0) { EPA_Finish(); - reply_mix(CMD_ACK, 2, func_return, 0, NULL, 0); + reply_epa_result(CMD_HF_EPA_REPLAY, 2, func_return, NULL); return; } @@ -553,12 +592,12 @@ void EPA_PACE_Replay(const PacketCommandNG *c) { || response_apdu[func_return - 4] != 0x90 || response_apdu[func_return - 3] != 0x00)) { EPA_Finish(); - reply_mix(CMD_ACK, 3 + i, func_return, 0, timings, 20); + reply_epa_result(CMD_HF_EPA_REPLAY, 3 + i, func_return, timings); return; } } EPA_Finish(); - reply_mix(CMD_ACK, 0, 0, 0, timings, 20); + reply_epa_result(CMD_HF_EPA_REPLAY, 0, 0, timings); return; } diff --git a/client/src/cmdhfepa.c b/client/src/cmdhfepa.c index 54dcbcc23..a2fb98d17 100644 --- a/client/src/cmdhfepa.c +++ b/client/src/cmdhfepa.c @@ -76,10 +76,17 @@ static int CmdHFEPACollectPACENonces(const char *Cmd) { WaitForResponse(CMD_HF_EPA_COLLECT_NONCE, &resp); // check if command failed - if (resp.oldarg[0] != 0) { - PrintAndLogEx(FAILED, "Error in step %" PRId64 ", Return code: %" PRId64, resp.oldarg[0], resp.oldarg[1]); + if (resp.length < sizeof(epa_result_t)) { + PrintAndLogEx(FAILED, "Short reply from device"); + return PM3_ESOFT; + } + + const epa_result_t *r = (const epa_result_t *)resp.data.asBytes; + + if (r->step != 0) { + PrintAndLogEx(FAILED, "Error in step %u, Return code: %d", r->step, r->func_return); } else { - size_t nonce_length = resp.oldarg[1]; + size_t nonce_length = r->len; char *nonce = (char *) calloc(2 * nonce_length + 1, sizeof(uint8_t)); if (nonce == NULL) { @@ -88,7 +95,7 @@ static int CmdHFEPACollectPACENonces(const char *Cmd) { } // print nonce - PrintAndLogEx(SUCCESS, "Length: %zu, Nonce: %s", nonce_length, sprint_hex_inrow(resp.data.asBytes, nonce_length)); + PrintAndLogEx(SUCCESS, "Length: %zu, Nonce: %s", nonce_length, sprint_hex_inrow(resp.data.asBytes + sizeof(epa_result_t), nonce_length)); free(nonce); } if (i < n - 1) { @@ -172,12 +179,19 @@ static int CmdHFEPAPACEReplay(const char *Cmd) { clearCommandBuffer(); // arg0: APDU number // arg1: offset into the APDU - SendCommandMIX(CMD_HF_EPA_REPLAY, i + 1, j * sizeof(data), packet_length, data, packet_length); + uint8_t ubuf[sizeof(epa_replay_t) + sizeof(data)] = {0}; + epa_replay_t *upayload = (epa_replay_t *)ubuf; + upayload->apdu_num = i + 1; + upayload->offset = j * sizeof(data); + upayload->len = packet_length; + memcpy(upayload->data, data, packet_length); + + SendCommandNG(CMD_HF_EPA_REPLAY, ubuf, sizeof(epa_replay_t) + packet_length); if (WaitForResponseTimeout(CMD_HF_EPA_REPLAY, &resp, 2500) == false) { PrintAndLogEx(WARNING, "command time out"); return PM3_ETIMEOUT; } - if (resp.oldarg[0] != 0) { + if (resp.status != PM3_SUCCESS) { PrintAndLogEx(WARNING, "Transfer of APDU #%d Part %d failed!", i, j); return PM3_ESOFT; } @@ -186,24 +200,32 @@ static int CmdHFEPAPACEReplay(const char *Cmd) { // now perform the replay clearCommandBuffer(); - SendCommandMIX(CMD_HF_EPA_REPLAY, 0, 0, 0, NULL, 0); - WaitForResponse(CMD_ACK, &resp); + epa_replay_t run = {0}; + SendCommandNG(CMD_HF_EPA_REPLAY, (uint8_t *)&run, sizeof(run)); + WaitForResponse(CMD_HF_EPA_REPLAY, &resp); - if (resp.oldarg[0] != 0) { - PrintAndLogEx(SUCCESS, "\nPACE replay failed in step %u!", (uint32_t)resp.oldarg[0]); + if (resp.length < sizeof(epa_result_t) + (5 * sizeof(uint32_t))) { + PrintAndLogEx(WARNING, "Short reply from device"); + return PM3_ESOFT; + } + + const epa_result_t *rr = (const epa_result_t *)resp.data.asBytes; + + if (rr->step != 0) { + PrintAndLogEx(SUCCESS, "\nPACE replay failed in step %u!", rr->step); PrintAndLogEx(SUCCESS, "Measured times:"); - PrintAndLogEx(SUCCESS, "MSE Set AT: %u us", resp.data.asDwords[0]); - PrintAndLogEx(SUCCESS, "GA Get Nonce: %u us", resp.data.asDwords[1]); - PrintAndLogEx(SUCCESS, "GA Map Nonce: %u us", resp.data.asDwords[2]); - PrintAndLogEx(SUCCESS, "GA Perform Key Agreement: %u us", resp.data.asDwords[3]); - PrintAndLogEx(SUCCESS, "GA Mutual Authenticate: %u us", resp.data.asDwords[4]); + PrintAndLogEx(SUCCESS, "MSE Set AT: %u us", rr->timings[0]); + PrintAndLogEx(SUCCESS, "GA Get Nonce: %u us", rr->timings[1]); + PrintAndLogEx(SUCCESS, "GA Map Nonce: %u us", rr->timings[2]); + PrintAndLogEx(SUCCESS, "GA Perform Key Agreement: %u us", rr->timings[3]); + PrintAndLogEx(SUCCESS, "GA Mutual Authenticate: %u us", rr->timings[4]); } else { PrintAndLogEx(SUCCESS, "PACE replay successful!"); - PrintAndLogEx(SUCCESS, "MSE Set AT: %u us", resp.data.asDwords[0]); - PrintAndLogEx(SUCCESS, "GA Get Nonce: %u us", resp.data.asDwords[1]); - PrintAndLogEx(SUCCESS, "GA Map Nonce: %u us", resp.data.asDwords[2]); - PrintAndLogEx(SUCCESS, "GA Perform Key Agreement: %u us", resp.data.asDwords[3]); - PrintAndLogEx(SUCCESS, "GA Mutual Authenticate: %u us", resp.data.asDwords[4]); + PrintAndLogEx(SUCCESS, "MSE Set AT: %u us", rr->timings[0]); + PrintAndLogEx(SUCCESS, "GA Get Nonce: %u us", rr->timings[1]); + PrintAndLogEx(SUCCESS, "GA Map Nonce: %u us", rr->timings[2]); + PrintAndLogEx(SUCCESS, "GA Perform Key Agreement: %u us", rr->timings[3]); + PrintAndLogEx(SUCCESS, "GA Mutual Authenticate: %u us", rr->timings[4]); } return PM3_SUCCESS; } @@ -240,14 +262,20 @@ static int CmdHFEPAPACESimulate(const char *Cmd) { clearCommandBuffer(); - SendCommandMIX(CMD_HF_EPA_PACE_SIMULATE, 0, 0, 0, pwd, plen); + SendCommandNG(CMD_HF_EPA_PACE_SIMULATE, pwd, plen); PacketResponseNG resp; - WaitForResponse(CMD_ACK, &resp); + WaitForResponse(CMD_HF_EPA_PACE_SIMULATE, &resp); - uint32_t *data = resp.data.asDwords; + if (resp.length < sizeof(epa_result_t) + (5 * sizeof(uint32_t))) { + PrintAndLogEx(WARNING, "Short reply from device"); + return PM3_ESOFT; + } - if (resp.oldarg[0] != 0) { - PrintAndLogEx(INFO, "\nPACE failed in step %u!", (uint32_t)resp.oldarg[0]); + const epa_result_t *sr = (const epa_result_t *)resp.data.asBytes; + const uint32_t *data = sr->timings; + + if (sr->step != 0) { + PrintAndLogEx(INFO, "\nPACE failed in step %u!", sr->step); PrintAndLogEx(INFO, "MSE Set AT: %u us", data[0]); PrintAndLogEx(INFO, "GA Get Nonce: %u us", data[1]); PrintAndLogEx(INFO, "GA Map Nonce: %u us", data[2]); diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 2d2d40543..5172cd7e9 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -392,6 +392,31 @@ typedef struct { uint8_t key[6]; } PACKED mfc_eload_t; +// CMD_HF_EPA_REPLAY payload. +// apdu_num 0 runs the replay, otherwise it is a 1-based APDU index being uploaded. +// Replaces oldargs: arg0 = apdu number, arg1 = offset, arg2 = length +typedef struct { + uint8_t apdu_num; + uint16_t offset; + uint16_t len; + uint8_t data[]; +} PACKED epa_replay_t; + +// Reply for CMD_HF_EPA_REPLAY, CMD_HF_EPA_PACE_SIMULATE and +// CMD_HF_EPA_COLLECT_NONCE. `step` is 0 on success, otherwise the step that +// failed; it used to travel in arg0 of an anonymous CMD_ACK. +// For COLLECT_NONCE, `len` is the nonce length and the nonce follows. +// Not PACKED on purpose: timings[] is uint32_t and must stay 4 byte aligned for +// the ARM7TDMI. 1+1+2 already fills 4 bytes, so there is no implicit padding and +// the layout is identical on both ends. See mf_value_t / t55xx_setconfig_t for +// the same reasoning. +typedef struct { + uint8_t step; + uint8_t len; + int16_t func_return; + uint32_t timings[]; +} epa_result_t; + // CMD_HF_MIFARE_READSC payload typedef struct { uint8_t sectorno;