From bb486e43183bfbf3ac9a237cef0737577ddac1bb Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Fri, 18 Dec 2009 20:32:25 +0000 Subject: [PATCH] Import libfreefare into trunk. --- AUTHORS | 0 COPYING | 165 +++++++++++++ ChangeLog | 0 Makefile.am | 11 + NEWS | 0 README | 0 configure.ac | 30 +++ mifare_classic.c | 618 +++++++++++++++++++++++++++++++++++++++++++++++ mifare_classic.h | 70 ++++++ 9 files changed, 894 insertions(+) create mode 100644 AUTHORS create mode 100644 COPYING create mode 100644 ChangeLog create mode 100644 Makefile.am create mode 100644 NEWS create mode 100644 README create mode 100644 configure.ac create mode 100644 mifare_classic.c create mode 100644 mifare_classic.h diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e69de29 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..cca7fc2 --- /dev/null +++ b/COPYING @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..f0370b1 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,11 @@ + +ACLOCAL_AMFLAGS = -I m4 + +AM_CFLAGS = -std=c99 @LIBNFC_CFLAGS@ +AM_LDFLAGS = @LIBNFC_LIBS@ + +lib_LTLIBRARIES = libfreefare.la + +libfreefare_la_SOURCES = mifare_classic.c +libfreefare_la_HEADERS = mifare_classic.h +libfreefare_ladir = $(includedir) diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..3c9508d --- /dev/null +++ b/configure.ac @@ -0,0 +1,30 @@ +AC_INIT([libfreefare], [0.0.0]) + +AC_CONFIG_MACRO_DIR([m4]) + +AC_PROG_CC +AC_PROG_CXX + +AC_PROG_LIBTOOL + +AM_INIT_AUTOMAKE + +m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_TYPE_INT16_T +AC_TYPE_INT32_T +AC_TYPE_UINT8_T +AC_TYPE_UINT16_T +AC_TYPE_UINT32_T +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MALLOC +AC_FUNC_REALLOC + +# Checks for pkg-config modules. +PKG_CHECK_MODULES([LIBNFC], [libnfc], [], [AC_MSG_ERROR([libnfc is mandatory.])]) + +AC_OUTPUT([Makefile]) diff --git a/mifare_classic.c b/mifare_classic.c new file mode 100644 index 0000000..d7a79e4 --- /dev/null +++ b/mifare_classic.c @@ -0,0 +1,618 @@ +/*- + * Copyright (C) 2009, Romain Tartiere, Romuald Conty. + * + * 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 + * + * $Id$ + */ + +#define _BSD_SOURCE +#include +#include +#include +#include + +#include + +#include "mifare_classic.h" + +struct mifare_classic_tag { + nfc_device_t *device; + nfc_iso14443a_info_t info; + int active; + + /* + * The following block numbers are on 2 bytes in order to use invalid + * address and avoid false cache hit with inconsistent data. + */ + struct { + int16_t sector_trailer_block_number; + uint16_t sector_access_bits; + int16_t block_number; + uint8_t block_access_bits; + } cached_access_bits; +}; + +union mifare_classic_block { + unsigned char data[16]; + struct { + uint32_t value; + uint32_t value_; + uint32_t value__; + MifareClassicBlockNumber address; + MifareClassicBlockNumber address_; + MifareClassicBlockNumber address__; + MifareClassicBlockNumber address___; + } value; +}; + +typedef unsigned char MifareClassicAccessBits; + +unsigned char mifare_data_access_permissions[] = { +/* + * [ Key A ] [ Key B ] + * | | + * ,----------- r(ead) | + * |,---------- w(rite) | + * ||,--------- d(ecrement) | + * |||,-------- i(ncrement) | + * |||| | + * |||| ,------------------------ r + * ,----- C3 |||| |,----------------------- w + * |,---- C2 |||| ||,---------------------- d + * ||,--- C1 |||| |||,--------------------- i + * ||| |||| |||| + * 0b000 0b 1111 1111 */ 0xff, +/* 0b001 0b 1000 1100 */ 0x8c, +/* 0b010 0b 1000 1000 */ 0x88, +/* 0b011 0b 1010 1111 */ 0xaf, +/* 0b100 0b 1010 1010 */ 0xaa, +/* 0b101 0b 0000 1000 */ 0x08, +/* 0b110 0b 0000 1100 */ 0x0c, +/* 0b111 0b 0000 0000 */ 0x00 +}; + +uint16_t mifare_trailer_access_permissions[] = { +/* + * [ Key A ] [ Access bits ] [ Key B ] + * | | | + * ,----------- read A | | + * |,---------- read B | | + * ||,--------- write A | | + * |||,-------- write B | | + * |||| | | + * |||| ,----------------------- read A | + * |||| |,---------------------- read B | + * |||| ||,--------------------- write A | + * |||| |||,-------------------- write B | + * |||| |||| | + * |||| |||| ,----------------------------------- read A + * ,----- C3 |||| |||| |,---------------------------------- read B + * |,---- C2 |||| |||| ||,--------------------------------- write A + * ||,--- C1 |||| |||| |||,-------------------------------- write B + * ||| |||| |||| |||| + * 0b000 0b 0010 1000 1010*/ 0x28a, +/* 0b001 0b 0001 1100 0000*/ 0x1c0, +/* 0b010 0b 0000 1000 1000*/ 0x088, +/* 0b011 0b 0000 1100 0000*/ 0x0c0, +/* 0b100 0b 0010 1010 1010*/ 0x2aa, +/* 0b101 0b 0000 1101 0000*/ 0x0d0, +/* 0b110 0b 0001 1101 0001*/ 0x1d1, +/* 0b111 0b 0000 1100 0000*/ 0x0c0 +}; + + +/* + * Private functions + */ + +int get_block_access_bits (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicAccessBits *block_access_bits); + + +/* + * MIFARE card communication preparation functions + * + * The following functions send NFC commands to the initiator to prepare + * communication with a MIFARE card, and perform required cleannups after using + * the target. + */ + +/* + * Get a list of the MIFARE card near to the provided NFC initiator. + * + * The list can be freed using the mifare_classic_free_tags() function. + */ +MifareClassicTag * +mifare_classic_get_tags (nfc_device_t *device) +{ + MifareClassicTag *tags = NULL; + int tag_count = 0; + + nfc_initiator_init(device); + + // Drop the field for a while + nfc_configure(device,NDO_ACTIVATE_FIELD,false); + + // Let the reader only try once to find a tag + nfc_configure(device,NDO_INFINITE_SELECT,false); + + // Configure the CRC and Parity settings + nfc_configure(device,NDO_HANDLE_CRC,true); + nfc_configure(device,NDO_HANDLE_PARITY,true); + + // Enable field so more power consuming cards can power themselves up + nfc_configure(device,NDO_ACTIVATE_FIELD,true); + + // Poll for a ISO14443A (MIFARE) tag + nfc_target_info_t target_info; + + while (nfc_initiator_select_tag(device,NM_ISO14443A_106,NULL,0,&target_info)) { + + // Ensure the target is a MIFARE classic tag. + if (!((target_info.nai.abtAtqa[0] == 0x00) && + (target_info.nai.abtAtqa[1] == 0x04) && + (target_info.nai.btSak == 0x08)) && /* NXP MIFARE Classic 1K */ + !((target_info.nai.abtAtqa[0] == 0x00) && + (target_info.nai.abtAtqa[1] == 0x02) && + (target_info.nai.btSak == 0x18)) && /* NXP MIFARE Classic 4K */ + !((target_info.nai.abtAtqa[0] == 0x00) && + (target_info.nai.abtAtqa[1] == 0x02) && + (target_info.nai.btSak == 0x38))) /* Nokia MIFARE Classic 4K - emulated */ + continue; + + tag_count++; + + /* (Re)Allocate memory for the found MIFARE classic array */ + if (!tags) { + if (!(tags = malloc ((tag_count) * sizeof (MifareClassicTag) + sizeof (void *)))) { + return NULL; + } + } else { + MifareClassicTag *p = realloc (tags, (tag_count) * sizeof (MifareClassicTag) + sizeof (void *)); + if (p) + tags = p; + else + return p; // FAIL! Return what has been found so far. + } + + /* Allocate memory for the found MIFARE classic tag */ + if (!(tags[tag_count-1] = malloc (sizeof (struct mifare_classic_tag)))) { + return tags; // FAIL! Return what has been found before. + } + (tags[tag_count-1])->device = device; + (tags[tag_count-1])->info = target_info.nai; + (tags[tag_count-1])->active = 0; + tags[tag_count] = NULL; + + nfc_initiator_deselect_tag (device); + } + + return tags; +} + +/* + * Free the provided tag list. + */ +void +mifare_classic_free_tags (MifareClassicTag *tags) +{ + if (tags) { + for (int i=0; tags[i]; i++) { + free (tags[i]); + } + free (tags); + } +} + +/* + * Establish connection to the provided tag. + */ +int +mifare_classic_connect (MifareClassicTag tag) +{ + if (tag->active) { + errno = EINVAL; + return -1; + } + + nfc_target_info_t pnti; + if (nfc_initiator_select_tag (tag->device, NM_ISO14443A_106, tag->info.abtUid, 4, &pnti)) { + tag->active = 1; + } + return 0; +} + +/* + * Terminate connection with the provided tag. + */ +int +mifare_classic_disconnect (MifareClassicTag tag) +{ + if (!(tag->active)) { + errno = EINVAL; + return -1; + } + + if (nfc_initiator_deselect_tag (tag->device)) { + tag->active = 0; + } + return 0; +} + + +/* + * Card manipulation functions + * + * The following functions perform direct communication with the connected + * MIFARE card. + */ + +/* + * Send an authentification command to the provided MIFARE target. + */ +int +mifare_classic_authenticate (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicKey key, MifareClassicKeyType key_type) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[12]; + command[0] = (key_type == MFC_KEY_A) ? MC_AUTH_A : MC_AUTH_B; + command[1] = block; + memcpy (&(command[2]), key, 6); + memcpy (&(command[8]), tag->info.abtUid, 4); + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + tag->cached_access_bits.sector_trailer_block_number = -1; + tag->cached_access_bits.sector_access_bits = 0x00; + + // No result. The MIFARE tag just ACKed. + return 0; +} + +/* + * Read data from the provided MIFARE target. + */ +int +mifare_classic_read (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicBlock *data) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[2]; + command[0] = MC_READ; + command[1] = block; + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), *data, &n))) { + errno = EIO; + return -1; + } + + return 0; +} + +int +mifare_classic_init_value (MifareClassicTag tag, MifareClassicBlockNumber block, int32_t value, MifareClassicBlockNumber adr) +{ + union mifare_classic_block b; + + b.value.value = value; + b.value.value_ = ~value; + b.value.value__ = value; + + b.value.address = adr; + b.value.address_ = ~adr; + b.value.address__ = adr; + b.value.address___ = ~adr; + + if (mifare_classic_write (tag, block, b.data) < 0) + return -1; + + return 0; +} + +int +mifare_classic_read_value (MifareClassicTag tag, MifareClassicBlockNumber block, int32_t *value, MifareClassicBlockNumber *adr) +{ + MifareClassicBlock data; + if (mifare_classic_read (tag, block, &data) < 0) + return -1; + + union mifare_classic_block b = *((union mifare_classic_block *)(&data)); + + + if ((b.value.value != (~b.value.value_)) || (b.value.value != b.value.value__)) { + errno = EIO; + return -1; + } + + if ((b.value.address != (unsigned char)(~b.value.address_)) || (b.value.address != b.value.address__) || (b.value.address_ != b.value.address___)) { + errno = EIO; + return -1; + } + + if (value) + *value = le32toh (b.value.value); + + if (adr) + *adr = b.value.address; + + return 0; +} + +/* + * Write data to the provided MIFARE target. + */ +int +mifare_classic_write (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicBlock data) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[2 + sizeof (MifareClassicBlock)]; + command[0] = MC_WRITE; + command[1] = block; + memcpy (&(command[2]), data, sizeof (MifareClassicBlock)); + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + // No result. The MIFARE tag just ACKed. + return 0; +} + +/* + * Increment the given value block by the provided amount into the internal + * data register. + */ +int +mifare_classic_increment (MifareClassicTag tag, MifareClassicBlockNumber block, uint32_t amount) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[6]; + command[0] = MC_INCREMENT; + command[1] = block; + int32_t le_amount = htole32 (amount); + memcpy(&(command[2]), &le_amount, sizeof (le_amount)); + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + // No result. The MIFARE tag just ACKed. + return 0; +} + +/* + * Decrement the given value block by the provided amount into the internal + * data register. + */ +int +mifare_classic_decrement (MifareClassicTag tag, MifareClassicBlockNumber block, uint32_t amount) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[6]; + command[0] = MC_DECREMENT; + command[1] = block; + int32_t le_amount = htole32 (amount); + memcpy(&(command[2]), &le_amount, sizeof (le_amount)); + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + // No result. The MIFARE tag just ACKed. + return 0; +} + +/* + * Store the provided block to the internal data register. + */ +int +mifare_classic_restore (MifareClassicTag tag, MifareClassicBlockNumber block) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[2]; + /* XXX Should be MC_RESTORE according to the MIFARE documentation. */ + command[0] = MC_STORE; + command[1] = block; + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + // No result. The MIFARE tag just ACKed. + return 0; +} + +/* + * Store the internal data register to the provided block. + */ +int +mifare_classic_transfer (MifareClassicTag tag, MifareClassicBlockNumber block) +{ + if (!tag->active) { + errno = EINVAL; + return -1; + } + + unsigned char command[2]; + command[0] = MC_TRANSFER; + command[1] = block; + + // Send command + size_t n; + if (!(nfc_initiator_transceive_dep_bytes (tag->device, command, sizeof (command), NULL, &n))) { + errno = EIO; + return -1; + } + + // No result. The MIFARE tag just ACKed. + return 0; +} + + +/* + * Access bit manipulation functions + * + * The following functions provide a convenient API for reading MIFARE card + * access bits. A cache system makes these functions query a single time the + * MIFARE card regardless of the number of information requested between two + * authentifications (i.e. for the current sector). + */ + +/* + * Fetch access bits for a given block from the block's sector's trailing + * block. + */ +int +get_block_access_bits (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicAccessBits *block_access_bits) +{ + /* + * The first block which holds the manufacturer block seems to have + * inconsistent access bits. + */ + if (block == 0) { + errno = EINVAL; + return -1; + } + + uint16_t sector_access_bits, sector_access_bits_; + + MifareClassicBlockNumber trailer = ((block) / 4) * 4 + 3; + + if (tag->cached_access_bits.sector_trailer_block_number == trailer) { + /* cache hit! */ + sector_access_bits = tag->cached_access_bits.sector_access_bits; + } else { + + MifareClassicBlock trailer_data; + if (mifare_classic_read (tag, trailer, &trailer_data) < 0) { + return -1; + } + + sector_access_bits_ = trailer_data[6] | ((trailer_data[7] & 0x0f) << 8) | 0xf000; + sector_access_bits = ((trailer_data[7] & 0xf0) >> 4) | (trailer_data[8] << 4); + + if (sector_access_bits != (uint16_t) ~sector_access_bits_) { + /* Sector locked */ + errno = EIO; + return -1; + } + tag->cached_access_bits.sector_trailer_block_number = trailer; + tag->cached_access_bits.block_number = -1; + tag->cached_access_bits.sector_access_bits = sector_access_bits; + } + + if (tag->cached_access_bits.block_number == block) { + /* cache hit! */ + *block_access_bits = tag->cached_access_bits.block_access_bits; + } else { + *block_access_bits = 0; + /* ,-------C3 + * |,------C2 + * ||,---- C1 + * ||| */ + uint16_t block_access_bits_mask = 0x0111 << (block % 4); + /* ||| + * ||`---------------. + * |`---------------.| + * `---------------.|| + * ||| */ + if (sector_access_bits & block_access_bits_mask & 0x000f) *block_access_bits |= 0x01; /* C1 */ + if (sector_access_bits & block_access_bits_mask & 0x00f0) *block_access_bits |= 0x02; /* C2 */ + if (sector_access_bits & block_access_bits_mask & 0x0f00) *block_access_bits |= 0x04; /* C3 */ + + tag->cached_access_bits.block_access_bits = *block_access_bits; + } + + return 0; +} + +/* + * Get information about the trailer block. + */ +int +mifare_classic_get_trailer_block_permission (MifareClassicTag tag, MifareClassicBlockNumber block, uint16_t permission, MifareClassicKeyType key_type) +{ + MifareClassicAccessBits access_bits; + if (get_block_access_bits (tag, block, &access_bits) < 0) { + return -1; + } + + if (tag->cached_access_bits.sector_trailer_block_number == block) { + return (mifare_trailer_access_permissions[access_bits] & (permission) << ((key_type == MFC_KEY_A) ? 1 : 0)) ? 1 : 0; + } else { + errno = EINVAL; + return -1; + } +} + +/* + * Get information about data blocks. + */ +int +mifare_classic_get_data_block_permission (MifareClassicTag tag, MifareClassicBlockNumber block, unsigned char permission, MifareClassicKeyType key_type) +{ + MifareClassicAccessBits access_bits; + if (get_block_access_bits (tag, block, &access_bits) < 0) { + return -1; + } + + if (tag->cached_access_bits.sector_trailer_block_number != block) { + return ((mifare_data_access_permissions[access_bits] & (permission << ( (key_type == MFC_KEY_A) ? 4 : 0 ))) ? 1 : 0); + } else { + errno = EINVAL; + return -1; + } +} diff --git a/mifare_classic.h b/mifare_classic.h new file mode 100644 index 0000000..3daa8c2 --- /dev/null +++ b/mifare_classic.h @@ -0,0 +1,70 @@ +/*- + * Copyright (C) 2009, Romain Tartiere, Romuald Conty. + * + * 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 + * + * $Id$ + */ + +#ifndef __MIFARE_CLASSIC_H__ +#define __MIFARE_CLASSIC_H__ + +struct mifare_classic_tag; +typedef struct mifare_classic_tag *MifareClassicTag; + +// struct mifare_block; +// typedef struct mifare_block *MifareClassicBlock; +typedef unsigned char MifareClassicBlock[16]; + +typedef unsigned char MifareClassicBlockNumber; + +typedef enum { MFC_KEY_A, MFC_KEY_B } MifareClassicKeyType; +typedef unsigned char MifareClassicKey[6]; + +MifareClassicTag *mifare_classic_get_tags (nfc_device_t *device); +void mifare_classic_free_tags (MifareClassicTag *tags); + +int mifare_classic_connect (MifareClassicTag tag); +int mifare_classic_disconnect (MifareClassicTag tag); + +int mifare_classic_authenticate (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicKey key, MifareClassicKeyType key_type); + +int mifare_classic_read (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicBlock *data); +int mifare_classic_init_value (MifareClassicTag tag, MifareClassicBlockNumber block, int32_t value, MifareClassicBlockNumber adr); +int mifare_classic_read_value (MifareClassicTag tag, MifareClassicBlockNumber block, int32_t *value, MifareClassicBlockNumber *adr); +int mifare_classic_write (MifareClassicTag tag, MifareClassicBlockNumber block, MifareClassicBlock data); + + +int mifare_classic_get_trailer_block_permission (MifareClassicTag tag, MifareClassicBlockNumber block, uint16_t permission, MifareClassicKeyType key_type); +int mifare_classic_get_data_block_permission (MifareClassicTag tag, MifareClassicBlockNumber block, unsigned char permission, MifareClassicKeyType key_type); +int mifare_classic_increment (MifareClassicTag tag, MifareClassicBlockNumber block, uint32_t amount); +int mifare_classic_decrement (MifareClassicTag tag, MifareClassicBlockNumber block, uint32_t amount); +int mifare_classic_restore (MifareClassicTag tag, MifareClassicBlockNumber block); +int mifare_classic_transfer (MifareClassicTag tag, MifareClassicBlockNumber block); + + +/* MIFARE Classic Access Bits */ +#define MCAB_R 0x8 +#define MCAB_W 0x4 +#define MCAB_D 0x2 +#define MCAB_I 0x1 + +#define MCAB_READ_KEYA 0x400 +#define MCAB_WRITE_KEYA 0x100 +#define MCAB_READ_ACCESS_BITS 0x040 +#define MCAB_WRITE_ACCESS_BITS 0x010 +#define MCAB_READ_KEYB 0x004 +#define MCAB_WRITE_KEYB 0x001 + +#endif /* !__MIFARE_CLASSIC_H__ */