From 755be05603dbe28948af4cf26a08bcd42c2c5ceb Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 17 Dec 2018 17:09:56 -0500 Subject: [PATCH] board/sg-nfc.c: Use non-hardcoded UID Turns out this does in fact vary between cards, even though its actual value is unimportant. --- board/sg-nfc.c | 24 ++++++++++++++++-------- board/sg-nfc.h | 2 +- cardhook/_com12.c | 12 ++++++++++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/board/sg-nfc.c b/board/sg-nfc.c index 0afd1b0..9936879 100644 --- a/board/sg-nfc.c +++ b/board/sg-nfc.c @@ -215,21 +215,29 @@ static HRESULT sg_nfc_cmd_mifare_poll( const struct sg_req_header *req, struct sg_nfc_resp_mifare_poll *resp) { + uint32_t uid; HRESULT hr; - hr = nfc->ops->mifare_poll(nfc->ops_ctx); + uid = 0; + hr = nfc->ops->mifare_poll(nfc->ops_ctx, &uid); if (hr == S_OK) { + if (uid == 0 || uid == -1) { + sg_nfc_dprintf(nfc, "nfc->ops->mifare_poll returned bad uid\n"); + + return E_UNEXPECTED; + } + sg_nfc_dprintf(nfc, "Mifare card is present\n"); sg_resp_init(&resp->resp, req, sizeof(resp->payload.some)); - resp->payload.some[0] = 0x01; /* Chunk size? */ - resp->payload.some[1] = 0x10; /* Unknown */ - resp->payload.some[2] = 0x04; /* Chunk size? */ - resp->payload.some[3] = 0x52; /* UID byte 0 */ - resp->payload.some[4] = 0xCC; /* UID byte 1 */ - resp->payload.some[5] = 0x55; /* UID byte 2 */ - resp->payload.some[6] = 0x25; /* UID byte 3 */ + resp->payload.some[0] = 0x01; /* Chunk size? */ + resp->payload.some[1] = 0x10; /* Unknown */ + resp->payload.some[2] = 0x04; /* Chunk size? */ + resp->payload.some[3] = uid >> 24; /* UID byte 0 */ + resp->payload.some[4] = uid >> 16; /* UID byte 1 */ + resp->payload.some[5] = uid >> 8; /* UID byte 2 */ + resp->payload.some[6] = uid ; /* UID byte 3 */ return S_OK; } else if (hr == S_FALSE) { diff --git a/board/sg-nfc.h b/board/sg-nfc.h index 507c6a8..9d91cd2 100644 --- a/board/sg-nfc.h +++ b/board/sg-nfc.h @@ -8,7 +8,7 @@ #include "hook/iobuf.h" struct sg_nfc_ops { - HRESULT (*mifare_poll)(void *ctx); + HRESULT (*mifare_poll)(void *ctx, uint32_t *uid); HRESULT (*mifare_read_luid)(void *ctx, uint8_t *luid, size_t nbytes); }; diff --git a/cardhook/_com12.c b/cardhook/_com12.c index a87f14b..78aefe1 100644 --- a/cardhook/_com12.c +++ b/cardhook/_com12.c @@ -13,13 +13,14 @@ #include "hooklib/uart.h" +#include "util/crc.h" #include "util/dprintf.h" #include "util/dump.h" static HRESULT com12_handle_irp(struct irp *irp); static HRESULT com12_handle_irp_locked(struct irp *irp); -static HRESULT com12_mifare_poll(void *ctx); +static HRESULT com12_mifare_poll(void *ctx, uint32_t *uid); static HRESULT com12_mifare_read_luid(void *ctx, uint8_t *luid, size_t nbytes); static HRESULT com12_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b); @@ -117,7 +118,7 @@ static HRESULT com12_handle_irp_locked(struct irp *irp) return hr; } -static HRESULT com12_mifare_poll(void *ctx) +static HRESULT com12_mifare_poll(void *ctx, uint32_t *uid) { HRESULT hr; FILE *f; @@ -152,6 +153,13 @@ static HRESULT com12_mifare_poll(void *ctx) com12_aime_luid[i] = byte; } + /* NOTE: We are just arbitrarily using the CRC32 of the LUID here, real + cards do not work like this! However, neither the application code nor + the network protocol care what the UID is, it just has to be a stable + unique identifier for over-the-air NFC communications. */ + + *uid = crc32(com12_aime_luid, sizeof(com12_aime_luid), 0); + hr = S_OK; end: