From 17db2f11122893bcacbf10a5f4b9c27ca2019cd3 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 26 Feb 2016 09:48:57 +0100 Subject: [PATCH] card_emu: Fix PTS checksum verification If the checksum doesn't match, the card should not apply any Fi/Di changes, not respond anything and simply expect the next TPDU. --- firmware/src_simtrace/card_emu.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/firmware/src_simtrace/card_emu.c b/firmware/src_simtrace/card_emu.c index ec9fdb8c..37f3e515 100644 --- a/firmware/src_simtrace/card_emu.c +++ b/firmware/src_simtrace/card_emu.c @@ -184,6 +184,22 @@ static int serialize_pts(uint8_t *out, const uint8_t *in) return i; } +static uint8_t csum_pts(const uint8_t *in) +{ + uint8_t out[6]; + int len = serialize_pts(out, in); + uint8_t csum = 0; + int i; + + /* we don't include the PCK byte in the checksumming process */ + len -= 1; + + for (i = 0; i < len; i++) + csum = csum ^ out[i]; + + return csum; +} + static void flush_pts(struct card_handle *ch) { struct req_ctx *rctx; @@ -348,7 +364,12 @@ process_byte_pts(struct card_handle *ch, uint8_t byte) break; case PTS_S_WAIT_REQ_PCK: ch->pts.req[_PCK] = byte; - /* FIXME: check PCK */ + if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) { + TRACE_DEBUG("Error in PTS Checksum!\n"); + /* Wait for the next TPDU */ + set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); + return ISO_S_WAIT_TPDU; + } /* FIXME: check if proposal matches capabilities in ATR */ memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp)); break;