2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-12 16:52:26 +02:00
|
|
|
* @file nfc-anticol.c
|
2010-10-14 13:53:27 +02:00
|
|
|
* @brief Generates one ISO14443-A anti-collision process "by-hand"
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stddef.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-05-27 16:05:07 +02:00
|
|
|
#include <stdbool.h>
|
2009-04-29 14:47:41 +02:00
|
|
|
#include <string.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc-messages.h>
|
2010-04-16 18:38:57 +02:00
|
|
|
#include "nfc-utils.h"
|
2009-09-08 12:25:59 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
#define SAK_FLAG_ATS_SUPPORTED 0x20
|
|
|
|
|
2009-11-10 10:47:59 +01:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtRx[MAX_FRAME_LEN];
|
2009-10-02 11:52:02 +02:00
|
|
|
static size_t szRxBits;
|
2010-10-12 16:56:42 +02:00
|
|
|
static size_t szRx;
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtUid[10];
|
2009-10-02 11:52:02 +02:00
|
|
|
static size_t szUidLen = 4;
|
2010-09-07 19:51:03 +02:00
|
|
|
static nfc_device_t *pnd;
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
bool quiet_output = false;
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// ISO14443A Anti-Collision Commands
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t abtReqa[1] = { 0x26 };
|
|
|
|
byte_t abtSelectAll[2] = { 0x93, 0x20 };
|
|
|
|
byte_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
byte_t abtRats[4] = { 0xe0, 0x50, 0xbc, 0xa5 };
|
|
|
|
byte_t abtHalt[4] = { 0x50, 0x00, 0x57, 0xcd };
|
|
|
|
|
2010-09-20 17:55:55 +02:00
|
|
|
static bool
|
2010-09-07 19:51:03 +02:00
|
|
|
transmit_bits (const byte_t * pbtTx, const size_t szTxBits)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2010-09-20 17:55:55 +02:00
|
|
|
printf ("Sent bits: ");
|
2010-09-07 19:51:03 +02:00
|
|
|
print_hex_bits (pbtTx, szTxBits);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Transmit the bit frame command, we don't use the arbitrary parity feature
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL))
|
|
|
|
return false;
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Show received answer
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2010-09-20 17:55:55 +02:00
|
|
|
printf ("Received bits: ");
|
2010-09-07 19:51:03 +02:00
|
|
|
print_hex_bits (abtRx, szRxBits);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-20 17:55:55 +02:00
|
|
|
static bool
|
2010-10-12 16:56:42 +02:00
|
|
|
transmit_bytes (const byte_t * pbtTx, const size_t szTx)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2010-09-20 17:55:55 +02:00
|
|
|
printf ("Sent bits: ");
|
2010-10-12 16:56:42 +02:00
|
|
|
print_hex (pbtTx, szTx);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Transmit the command bytes
|
2010-10-12 16:56:42 +02:00
|
|
|
if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx))
|
2010-09-07 19:51:03 +02:00
|
|
|
return false;
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Show received answer
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2010-09-20 17:55:55 +02:00
|
|
|
printf ("Received bits: ");
|
2010-10-12 16:56:42 +02:00
|
|
|
print_hex (abtRx, szRx);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static void
|
|
|
|
print_usage (char *argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("Usage: %s [OPTIONS]\n", argv[0]);
|
|
|
|
printf ("Options:\n");
|
|
|
|
printf ("\t-h\tHelp. Print this message.\n");
|
|
|
|
printf ("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
int arg;
|
2009-08-23 11:50:46 +02:00
|
|
|
|
|
|
|
// Get commandline options
|
2010-09-07 19:51:03 +02:00
|
|
|
for (arg = 1; arg < argc; arg++) {
|
|
|
|
if (0 == strcmp (argv[arg], "-h")) {
|
|
|
|
print_usage (argv);
|
2010-09-20 17:55:55 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 19:51:03 +02:00
|
|
|
} else if (0 == strcmp (argv[arg], "-q")) {
|
|
|
|
INFO ("%s", "Quiet mode.");
|
2009-08-26 12:57:38 +02:00
|
|
|
quiet_output = true;
|
2009-09-07 12:15:34 +02:00
|
|
|
} else {
|
2010-09-07 19:51:03 +02:00
|
|
|
ERR ("%s is not supported option.", argv[arg]);
|
|
|
|
print_usage (argv);
|
2010-09-20 17:55:55 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Try to open the NFC reader
|
2010-09-07 19:51:03 +02:00
|
|
|
pnd = nfc_connect (NULL);
|
2009-09-07 12:15:34 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!pnd) {
|
|
|
|
printf ("Error connecting NFC reader\n");
|
2010-09-20 17:55:55 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
2010-09-20 17:55:55 +02:00
|
|
|
|
|
|
|
// Initialise NFC device as "initiator"
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_initiator_init (pnd);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Drop the field for a while
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
|
2010-08-18 19:22:13 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-09-20 17:55:55 +02:00
|
|
|
|
2010-08-18 19:22:13 +02:00
|
|
|
// Configure the CRC
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) {
|
2010-08-18 19:22:13 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Configure parity settings
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
|
2010-08-18 19:22:13 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-09-20 17:55:55 +02:00
|
|
|
// Use raw send/receive methods
|
|
|
|
if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) {
|
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Disable 14443-4 autoswitching
|
|
|
|
if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) {
|
2010-08-18 19:22:13 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-09-23 18:03:05 +02:00
|
|
|
// Force 14443-A mode
|
|
|
|
if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) {
|
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2010-09-20 17:55:55 +02:00
|
|
|
// Enable field so more power consuming cards can power themselves up
|
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-09-03 18:13:36 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 14:37:43 +02:00
|
|
|
printf ("Connected to NFC reader: %s\n\n", pnd->acName);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Send the 7 bits request command specified in ISO 14443A (0x26)
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!transmit_bits (abtReqa, 7)) {
|
|
|
|
printf ("Error: No tag available\n");
|
|
|
|
nfc_disconnect (pnd);
|
2009-04-29 14:47:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Anti-collision
|
2010-09-07 19:51:03 +02:00
|
|
|
transmit_bytes (abtSelectAll, 2);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Save the UID
|
2010-09-07 19:51:03 +02:00
|
|
|
memcpy (abtUid, abtRx, 4);
|
|
|
|
memcpy (abtSelectTag + 2, abtRx, 5);
|
|
|
|
append_iso14443a_crc (abtSelectTag, 7);
|
|
|
|
transmit_bytes (abtSelectTag, 9);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Test if we are dealing with a 4 bytes uid
|
2010-09-07 19:51:03 +02:00
|
|
|
if (abtUid[0] != 0x88) {
|
2009-10-02 11:52:02 +02:00
|
|
|
szUidLen = 4;
|
2009-04-29 14:47:41 +02:00
|
|
|
} else {
|
|
|
|
// We have to do the anti-collision for cascade level 2
|
|
|
|
abtSelectAll[0] = 0x95;
|
|
|
|
abtSelectTag[0] = 0x95;
|
|
|
|
|
|
|
|
// Anti-collision
|
2010-09-07 19:51:03 +02:00
|
|
|
transmit_bytes (abtSelectAll, 2);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Save the UID
|
2010-09-07 19:51:03 +02:00
|
|
|
memcpy (abtUid + 4, abtRx, 4);
|
|
|
|
memcpy (abtSelectTag + 2, abtRx, 5);
|
|
|
|
append_iso14443a_crc (abtSelectTag, 7);
|
|
|
|
transmit_bytes (abtSelectTag, 9);
|
2009-10-02 11:52:02 +02:00
|
|
|
szUidLen = 7;
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Request ATS, this only applies to tags that support ISO 14443A-4
|
2010-09-07 19:51:03 +02:00
|
|
|
if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED)
|
|
|
|
transmit_bytes (abtRats, 4);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Done, halt the tag now
|
2010-09-07 19:51:03 +02:00
|
|
|
transmit_bytes (abtHalt, 4);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\nFound tag with UID: ");
|
2010-04-17 00:38:45 +02:00
|
|
|
if (szUidLen == 7) {
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("%02x%02x%02x", abtUid[6], abtUid[5], abtUid[4]);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("%02x%02x%02x%02x\n", abtUid[3], abtUid[2], abtUid[1], abtUid[0]);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_disconnect (pnd);
|
2009-04-29 14:47:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|