2010-11-17 15:27:11 +01:00
|
|
|
/*-
|
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
* Copyright (C) 2010, Romuald Conty, Romain Tartière
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 2 )Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
|
|
|
*
|
|
|
|
*/
|
2012-01-31 15:28:45 +01:00
|
|
|
/**
|
|
|
|
* @file mifare.c
|
|
|
|
* @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
|
|
|
|
*/
|
2010-06-15 17:33:22 +02:00
|
|
|
#include "mifare.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-08-10 23:00:08 +02:00
|
|
|
#include <nfc/nfc.h>
|
2010-06-15 17:33:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Execute a MIFARE Classic Command
|
|
|
|
* @return Returns true if action was successfully performed; otherwise returns false.
|
|
|
|
* @param pmp Some commands need additional information. This information should be supplied in the mifare_param union.
|
|
|
|
*
|
|
|
|
* The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
|
|
|
|
* @note There are three different types of information (Authenticate, Data and Value).
|
|
|
|
*
|
2012-05-29 02:33:22 +02:00
|
|
|
* First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID.
|
2010-06-15 17:33:22 +02:00
|
|
|
* They are both used to initialize the internal cipher-state of the PN53X chip (http://libnfc.org/hardware/pn53x-chip).
|
2012-05-29 02:33:22 +02:00
|
|
|
* After a successful authentication it will be possible to execute other commands (e.g. Read/Write).
|
2010-06-15 17:33:22 +02:00
|
|
|
* The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
|
|
|
|
*/
|
2010-09-07 19:51:03 +02:00
|
|
|
bool
|
2011-11-25 16:21:10 +01:00
|
|
|
nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
|
2010-06-15 17:33:22 +02:00
|
|
|
{
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtRx[265];
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szParamLen;
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtCmd[265];
|
2012-01-05 15:49:02 +01:00
|
|
|
//bool bEasyFraming;
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
abtCmd[0] = mc; // The MIFARE Classic command
|
|
|
|
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
switch (mc) {
|
2012-05-29 17:52:51 +02:00
|
|
|
// Read and store command have no parameter
|
|
|
|
case MC_READ:
|
|
|
|
case MC_STORE:
|
|
|
|
szParamLen = 0;
|
|
|
|
break;
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2012-05-29 17:52:51 +02:00
|
|
|
// Authenticate command
|
|
|
|
case MC_AUTH_A:
|
|
|
|
case MC_AUTH_B:
|
|
|
|
szParamLen = sizeof (struct mifare_param_auth);
|
|
|
|
break;
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2012-05-29 17:52:51 +02:00
|
|
|
// Data command
|
|
|
|
case MC_WRITE:
|
|
|
|
szParamLen = sizeof (struct mifare_param_data);
|
|
|
|
break;
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2012-05-29 17:52:51 +02:00
|
|
|
// Value command
|
|
|
|
case MC_DECREMENT:
|
|
|
|
case MC_INCREMENT:
|
|
|
|
case MC_TRANSFER:
|
|
|
|
szParamLen = sizeof (struct mifare_param_value);
|
|
|
|
break;
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2012-05-29 17:52:51 +02:00
|
|
|
// Please fix your code, you never should reach this statement
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
2010-06-15 17:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// When available, copy the parameter bytes
|
2010-09-07 19:51:03 +02:00
|
|
|
if (szParamLen)
|
2011-11-24 11:54:42 +01:00
|
|
|
memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen);
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2011-12-19 01:23:21 +01:00
|
|
|
// FIXME: Save and restore bEasyFraming
|
|
|
|
// bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
|
2011-12-14 17:01:00 +01:00
|
|
|
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
|
|
|
|
nfc_perror (pnd, "nfc_device_set_property_bool");
|
2010-10-20 20:11:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-15 17:33:22 +02:00
|
|
|
// Fire the mifare command
|
2011-12-19 01:23:21 +01:00
|
|
|
int res;
|
2012-05-27 23:06:22 +02:00
|
|
|
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) {
|
2011-12-19 01:23:21 +01:00
|
|
|
if (res == NFC_ERFTRANS) {
|
|
|
|
// "Invalid received frame", usual means we are
|
2010-11-15 19:50:53 +01:00
|
|
|
// authenticated on a sector but the requested MIFARE cmd (read, write)
|
|
|
|
// is not permitted by current acces bytes;
|
|
|
|
// So there is nothing to do here.
|
|
|
|
} else {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
|
2010-11-15 19:50:53 +01:00
|
|
|
}
|
2011-12-19 01:23:21 +01:00
|
|
|
// XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
|
2010-08-18 19:22:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-12-19 01:23:21 +01:00
|
|
|
/* XXX
|
2011-12-14 17:01:00 +01:00
|
|
|
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
|
|
|
|
nfc_perror (pnd, "nfc_device_set_property_bool");
|
2010-10-20 20:11:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-12-19 01:23:21 +01:00
|
|
|
*/
|
2010-10-20 20:11:06 +02:00
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
// When we have executed a read command, copy the received bytes into the param
|
2010-08-18 15:53:34 +02:00
|
|
|
if (mc == MC_READ) {
|
2012-05-27 23:06:22 +02:00
|
|
|
if (res == 16) {
|
2010-09-07 19:51:03 +02:00
|
|
|
memcpy (pmp->mpd.abtData, abtRx, 16);
|
2010-08-18 15:53:34 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 17:33:22 +02:00
|
|
|
// Command succesfully executed
|
|
|
|
return true;
|
|
|
|
}
|