2010-11-17 15:27:11 +01:00
|
|
|
/*-
|
2013-03-10 16:15:23 +01:00
|
|
|
* Free/Libre Near Field Communication (NFC) library
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2013-03-10 16:15:23 +01:00
|
|
|
* Libnfc historical contributors:
|
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2009-2013 Romuald Conty
|
|
|
|
* Copyright (C) 2010-2012 Romain Tartière
|
|
|
|
* Copyright (C) 2010-2013 Philippe Teuwen
|
|
|
|
* Copyright (C) 2012-2013 Ludovic Rousseau
|
2013-07-17 13:57:56 +02:00
|
|
|
* See AUTHORS file for a more comprehensive list of contributors.
|
2013-03-10 16:15:23 +01:00
|
|
|
* Additional contributors of this file:
|
2017-03-23 17:04:02 +01:00
|
|
|
* Copyright (C) 2017 Adam Laurie
|
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.
|
2010-06-15 17:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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.h
|
2010-06-15 17:33:22 +02:00
|
|
|
* @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LIBNFC_MIFARE_H_
|
2010-09-07 19:51:03 +02:00
|
|
|
# define _LIBNFC_MIFARE_H_
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
# include <nfc/nfc-types.h>
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
// Compiler directive, set struct alignment to 1 uint8_t for compatibility
|
2010-09-07 19:51:03 +02:00
|
|
|
# pragma pack(1)
|
2010-06-15 17:33:22 +02:00
|
|
|
|
|
|
|
typedef enum {
|
2010-09-07 19:51:03 +02:00
|
|
|
MC_AUTH_A = 0x60,
|
|
|
|
MC_AUTH_B = 0x61,
|
|
|
|
MC_READ = 0x30,
|
|
|
|
MC_WRITE = 0xA0,
|
|
|
|
MC_TRANSFER = 0xB0,
|
|
|
|
MC_DECREMENT = 0xC0,
|
|
|
|
MC_INCREMENT = 0xC1,
|
|
|
|
MC_STORE = 0xC2
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifare_cmd;
|
|
|
|
|
|
|
|
// MIFARE command params
|
2011-11-23 16:55:40 +01:00
|
|
|
struct mifare_param_auth {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtKey[6];
|
2012-06-03 23:02:54 +02:00
|
|
|
uint8_t abtAuthUid[4];
|
2011-11-23 16:55:40 +01:00
|
|
|
};
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
struct mifare_param_data {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtData[16];
|
2011-11-23 16:55:40 +01:00
|
|
|
};
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
struct mifare_param_value {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtValue[4];
|
2011-11-23 16:55:40 +01:00
|
|
|
};
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2017-02-17 11:31:13 +01:00
|
|
|
struct mifare_param_trailer {
|
|
|
|
uint8_t abtKeyA[6];
|
|
|
|
uint8_t abtAccessBits[4];
|
|
|
|
uint8_t abtKeyB[6];
|
|
|
|
};
|
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
typedef union {
|
2011-11-23 16:55:40 +01:00
|
|
|
struct mifare_param_auth mpa;
|
|
|
|
struct mifare_param_data mpd;
|
|
|
|
struct mifare_param_value mpv;
|
2017-02-17 11:31:13 +01:00
|
|
|
struct mifare_param_trailer mpt;
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifare_param;
|
|
|
|
|
|
|
|
// Reset struct alignment to default
|
2010-09-07 19:51:03 +02:00
|
|
|
# pragma pack()
|
2010-06-15 17:33:22 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
bool 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
|
|
|
// Compiler directive, set struct alignment to 1 uint8_t for compatibility
|
2010-09-07 19:51:03 +02:00
|
|
|
# pragma pack(1)
|
2010-06-15 17:33:22 +02:00
|
|
|
|
|
|
|
// MIFARE Classic
|
|
|
|
typedef struct {
|
2013-06-13 20:14:22 +02:00
|
|
|
uint8_t abtUID[4]; // beware for 7bytes UID it goes over next fields
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t btBCC;
|
2013-06-13 20:14:22 +02:00
|
|
|
uint8_t btSAK; // beware it's not always exactly SAK
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtATQA[2];
|
2013-06-13 20:14:22 +02:00
|
|
|
uint8_t abtManufacturer[8];
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifare_classic_block_manufacturer;
|
|
|
|
|
|
|
|
typedef struct {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtData[16];
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifare_classic_block_data;
|
|
|
|
|
|
|
|
typedef struct {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtKeyA[6];
|
|
|
|
uint8_t abtAccessBits[4];
|
|
|
|
uint8_t abtKeyB[6];
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifare_classic_block_trailer;
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
mifare_classic_block_manufacturer mbm;
|
|
|
|
mifare_classic_block_data mbd;
|
|
|
|
mifare_classic_block_trailer mbt;
|
|
|
|
} mifare_classic_block;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
mifare_classic_block amb[256];
|
|
|
|
} mifare_classic_tag;
|
|
|
|
|
|
|
|
// MIFARE Ultralight
|
|
|
|
typedef struct {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t sn0[3];
|
|
|
|
uint8_t btBCC0;
|
|
|
|
uint8_t sn1[4];
|
|
|
|
uint8_t btBCC1;
|
|
|
|
uint8_t internal;
|
|
|
|
uint8_t lock[2];
|
|
|
|
uint8_t otp[4];
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifareul_block_manufacturer;
|
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
// MIFARE Ultralight EV1 MF0UL11 Config Pages
|
2017-03-23 17:04:02 +01:00
|
|
|
typedef struct {
|
|
|
|
uint8_t mod;
|
2017-04-01 00:25:14 +02:00
|
|
|
uint8_t rfui1[2];
|
2017-03-23 17:04:02 +01:00
|
|
|
uint8_t auth0;
|
|
|
|
uint8_t access;
|
|
|
|
uint8_t vctid;
|
2017-04-01 00:25:14 +02:00
|
|
|
uint8_t rfui2[2];
|
2017-03-23 17:04:02 +01:00
|
|
|
uint8_t pwd[4];
|
|
|
|
uint8_t pack[2];
|
2017-04-01 00:25:14 +02:00
|
|
|
uint8_t rfui3[2];
|
|
|
|
} mifareul_block_config11;
|
|
|
|
|
|
|
|
// MIFARE Ultralight EV1 MF0UL21 ConfigA Pages
|
|
|
|
typedef struct {
|
|
|
|
uint8_t lock[3];
|
|
|
|
uint8_t rfui0;
|
|
|
|
uint8_t mod;
|
|
|
|
uint8_t rfui1[2];
|
|
|
|
uint8_t auth0;
|
|
|
|
uint8_t access;
|
|
|
|
uint8_t vctid;
|
2017-03-23 17:04:02 +01:00
|
|
|
uint8_t rfui2[2];
|
2017-04-01 00:25:14 +02:00
|
|
|
uint8_t pwd[4];
|
|
|
|
} mifareul_block_config21A;
|
|
|
|
|
|
|
|
// MIFARE Ultralight EV1 MF0UL21 ConfigB Pages
|
|
|
|
typedef struct {
|
|
|
|
uint8_t pack[2];
|
|
|
|
uint8_t rfui3[2];
|
|
|
|
uint8_t dummy[12];
|
|
|
|
} mifareul_block_config21B;
|
2017-03-23 17:04:02 +01:00
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
typedef struct {
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtData[16];
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifareul_block_data;
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
mifareul_block_manufacturer mbm;
|
|
|
|
mifareul_block_data mbd;
|
2017-04-01 00:25:14 +02:00
|
|
|
mifareul_block_config11 mbc11;
|
|
|
|
mifareul_block_config21A mbc21a;
|
|
|
|
mifareul_block_config21B mbc21b;
|
2010-06-15 17:33:22 +02:00
|
|
|
} mifareul_block;
|
|
|
|
|
2017-03-23 17:04:02 +01:00
|
|
|
// standard UL tag - 1 manuf block + 3 user blocks
|
2010-06-15 17:33:22 +02:00
|
|
|
typedef struct {
|
|
|
|
mifareul_block amb[4];
|
|
|
|
} mifareul_tag;
|
|
|
|
|
2017-03-23 17:04:02 +01:00
|
|
|
// UL EV1 MF0UL11 tag - 1 manuf block + 3 user blocks + 1 config block
|
|
|
|
typedef struct {
|
|
|
|
mifareul_block amb[5];
|
|
|
|
} mifareul_ev1_mf0ul11_tag;
|
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
// UL EV1 MF0UL21 tag - 1 manuf block + 8 user blocks + 1/4 lock block + 1 config block
|
2017-03-23 17:04:02 +01:00
|
|
|
typedef struct {
|
2017-04-01 00:25:14 +02:00
|
|
|
mifareul_block amb[11];
|
2017-03-23 17:04:02 +01:00
|
|
|
} mifareul_ev1_mf0ul21_tag;
|
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
// Reset struct alignment to default
|
2010-09-07 19:51:03 +02:00
|
|
|
# pragma pack()
|
2010-06-15 17:33:22 +02:00
|
|
|
|
|
|
|
#endif // _LIBNFC_MIFARE_H_
|