Merge the freefare-desfire branch into trunk.

This commit is contained in:
Romain Tartiere 2010-07-26 21:48:18 +00:00
parent 3f6a142b57
commit 5779d6f945
38 changed files with 4943 additions and 116 deletions

View file

@ -1,69 +1,133 @@
AM_CFLAGS = -I. @LIBNFC_CFLAGS@
AM_CFLAGS = @LIBNFC_CFLAGS@
AM_LDFLAGS = @LIBNFC_LIBS@
lib_LTLIBRARIES = libfreefare.la
# TODO: Remove hexdump.c
# XXX: Remove hexdump.c on systems with a hexdump(1) function in libutil.
libfreefare_la_SOURCES = freefare.c \
hexdump.c \
mifare_classic.c \
mifare_ultralight.c \
mifare_desfire.c \
mifare_desfire_aid.c \
mifare_desfire_authenticate.c \
mifare_desfire_key.c \
mad.c \
desfire_error.c \
mifare_application.c \
tlv.c
# TODO: Remove this
# XXX: Uncomment on FreeBSD to link with libutil which provides hexdump(1).
# libfreefare_la_LIBADD = -lutil
libfreefare_la_HEADERS = freefare.h
libfreefare_ladir = $(includedir)
man_MANS = freefare.3 \
mifare_ultralight.3 \
mifare_classic.3 \
mad.3 \
mifare_application.3 \
mifare_classic.3 \
mifare_desfire.3 \
mifare_desfire_key.3 \
mifare_ultralight.3 \
tlv.3
linkedman = \
freefare.3 freefare_get_tags.3 \
freefare.3 freefare_free_tags.3 \
freefare.3 freefare_get_tag_friendly_name.3 \
freefare.3 freefare_get_tag_type.3 \
freefare.3 freefare_get_tag_uid.3 \
freefare.3 freefare_get_tag_friendly_name.3 \
freefare.3 freefare_free_tags.3 \
mifare_ultralight.3 mifare_ultralight_connect.3 \
mifare_ultralight.3 mifare_ultralight_disconnect.3 \
mifare_ultralight.3 mifare_ultralight_read.3 \
mifare_ultralight.3 mifare_ultralight_write.3 \
mifare_ultralight.3 mifare_ultralight_get_uid.3 \
mifare_classic.3 mifare_classic_connect.3 \
mifare_classic.3 mifare_classic_disconnect.3 \
mifare_classic.3 mifare_classic_authenticate.3 \
mifare_classic.3 mifare_classic_read.3 \
mifare_classic.3 mifare_classic_init_value.3 \
mifare_classic.3 mifare_classic_read_value.3 \
mifare_classic.3 mifare_classic_write.3 \
mifare_classic.3 mifare_classic_increment.3 \
mifare_classic.3 mifare_classic_decrement.3 \
mifare_classic.3 mifare_classic_restore.3 \
mifare_classic.3 mifare_classic_transfer.3 \
mifare_classic.3 mifare_classic_get_trailer_block_permission.3 \
mifare_classic.3 mifare_classic_get_data_block_permission.3 \
mifare_classic.3 mifare_classic_format_sector.3 \
mifare_classic.3 mifare_classic_get_uid.3 \
mifare_classic.3 mifare_classic_trailer_block.3 \
mad.3 mad_new.3 \
mad.3 mad_read.3 \
mad.3 mad_write.3 \
mad.3 mad_get_version.3 \
mad.3 mad_set_version.3 \
mad.3 mad_get_card_publisher_sector.3 \
mad.3 mad_set_card_publisher_sector.3 \
mad.3 mad_get_aid.3 \
mad.3 mad_set_aid.3 \
mad.3 mad_free.3 \
freefare.3 freefare_get_tags.3 \
mad.3 mad_application_read.3 \
mad.3 mad_application_write.3 \
mad.3 mad_free.3 \
mad.3 mad_get_aid.3 \
mad.3 mad_get_card_publisher_sector.3 \
mad.3 mad_get_version.3 \
mad.3 mad_new.3 \
mad.3 mad_read.3 \
mad.3 mad_set_aid.3 \
mad.3 mad_set_card_publisher_sector.3 \
mad.3 mad_set_version.3 \
mad.3 mad_write.3 \
mifare_application.3 mifare_application_alloc.3 \
mifare_application.3 mifare_application_free.3 \
mifare_application.3 mifare_application_find.3 \
tlv.3 tlv_encode.3 \
tlv.3 tlv_decode.3
mifare_application.3 mifare_application_free.3 \
mifare_classic.3 mifare_classic_authenticate.3 \
mifare_classic.3 mifare_classic_connect.3 \
mifare_classic.3 mifare_classic_decrement.3 \
mifare_classic.3 mifare_classic_disconnect.3 \
mifare_classic.3 mifare_classic_format_sector.3 \
mifare_classic.3 mifare_classic_get_data_block_permission.3 \
mifare_classic.3 mifare_classic_get_trailer_block_permission.3 \
mifare_classic.3 mifare_classic_get_uid.3 \
mifare_classic.3 mifare_classic_increment.3 \
mifare_classic.3 mifare_classic_init_value.3 \
mifare_classic.3 mifare_classic_read.3 \
mifare_classic.3 mifare_classic_read_value.3 \
mifare_classic.3 mifare_classic_restore.3 \
mifare_classic.3 mifare_classic_trailer_block.3 \
mifare_classic.3 mifare_classic_transfer.3 \
mifare_classic.3 mifare_classic_write.3 \
mifare_desfire.3 mifare_desfire_abort_transaction.3 \
mifare_desfire.3 mifare_desfire_authenticate.3 \
mifare_desfire.3 mifare_desfire_change_file_settings.3 \
mifare_desfire.3 mifare_desfire_change_key.3 \
mifare_desfire.3 mifare_desfire_change_key_settings.3 \
mifare_desfire.3 mifare_desfire_clear_record_file.3 \
mifare_desfire.3 mifare_desfire_commit_transaction.3 \
mifare_desfire.3 mifare_desfire_connect.3 \
mifare_desfire.3 mifare_desfire_create_application.3 \
mifare_desfire.3 mifare_desfire_create_backup_data_file.3 \
mifare_desfire.3 mifare_desfire_create_cyclic_record_file.3 \
mifare_desfire.3 mifare_desfire_create_linear_record_file.3 \
mifare_desfire.3 mifare_desfire_create_std_data_file.3 \
mifare_desfire.3 mifare_desfire_create_value_file.3 \
mifare_desfire.3 mifare_desfire_credit.3 \
mifare_desfire.3 mifare_desfire_credit_ex.3 \
mifare_desfire.3 mifare_desfire_debit.3 \
mifare_desfire.3 mifare_desfire_debit_ex.3 \
mifare_desfire.3 mifare_desfire_delete_application.3 \
mifare_desfire.3 mifare_desfire_delete_file.3 \
mifare_desfire.3 mifare_desfire_disconnect.3 \
mifare_desfire.3 mifare_desfire_format_picc.3 \
mifare_desfire.3 mifare_desfire_free_application_ids.3 \
mifare_desfire.3 mifare_desfire_get_application_ids.3 \
mifare_desfire.3 mifare_desfire_get_file_ids.3 \
mifare_desfire.3 mifare_desfire_get_file_settings.3 \
mifare_desfire.3 mifare_desfire_get_key_settings.3 \
mifare_desfire.3 mifare_desfire_get_key_version.3 \
mifare_desfire.3 mifare_desfire_get_value.3 \
mifare_desfire.3 mifare_desfire_get_value_ex.3 \
mifare_desfire.3 mifare_desfire_get_version.3 \
mifare_desfire.3 mifare_desfire_limited_credit.3 \
mifare_desfire.3 mifare_desfire_limited_credit_ex.3 \
mifare_desfire.3 mifare_desfire_read_data.3 \
mifare_desfire.3 mifare_desfire_read_data_ex.3 \
mifare_desfire.3 mifare_desfire_read_records.3 \
mifare_desfire.3 mifare_desfire_read_records_ex.3 \
mifare_desfire.3 mifare_desfire_select_application.3 \
mifare_desfire.3 mifare_desfire_write_data.3 \
mifare_desfire.3 mifare_desfire_write_data_ex.3 \
mifare_desfire.3 mifare_desfire_write_record.3 \
mifare_desfire.3 mifare_desfire_write_record_ex.3 \
mifare_desfire_key.3 mifare_desfire_3des_key_new.3 \
mifare_desfire_key.3 mifare_desfire_3des_key_new_with_version.3 \
mifare_desfire_key.3 mifare_desfire_des_key_new.3 \
mifare_desfire_key.3 mifare_desfire_des_key_new_with_version.3 \
mifare_desfire_key.3 mifare_desfire_key_free.3 \
mifare_desfire_key.3 mifare_desfire_key_get_version.3 \
mifare_desfire_key.3 mifare_desfire_key_set_version.3 \
mifare_ultralight.3 mifare_ultralight_connect.3 \
mifare_ultralight.3 mifare_ultralight_disconnect.3 \
mifare_ultralight.3 mifare_ultralight_get_uid.3 \
mifare_ultralight.3 mifare_ultralight_read.3 \
mifare_ultralight.3 mifare_ultralight_write.3 \
tlv.3 tlv_decode.3 \
tlv.3 tlv_encode.3
install-data-hook:
(cd $(DESTDIR)$(man3dir); for i in `echo $(linkedman) | xargs -n2 echo | awk '{print $$2}'`; do rm -f $$i; done; echo $(linkedman) | xargs -n2 $(LN_S))
@ -72,3 +136,4 @@ uninstall-hook:
(cd $(DESTDIR)$(man3dir); for i in `echo $(linkedman) | xargs -n2 echo | awk '{print $$2}'`; do rm -f $$i; done;)
EXTRA_DIST = freefare_internal.h $(man_MANS)
CLEANFILES = *.gcno

View file

@ -0,0 +1,69 @@
/*-
* Copyright (C) 2010, 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 <http://www.gnu.org/licenses/>
*
* $Id$
*/
#include <sys/types.h>
#include <stdlib.h>
#include <freefare.h>
#define EM(e) { e, #e }
static struct error_message {
uint8_t code;
char *message;
} error_messages[] = {
EM(OPERATION_OK),
EM(NO_CHANGES),
EM(OUT_OF_EEPROM_ERROR),
EM(ILLEGAL_COMMAND_CODE),
EM(INTEGRITY_ERROR),
EM(NO_SUCH_KEY),
EM(LENGTH_ERROR),
EM(PERMISSION_ERROR),
EM(PARAMETER_ERROR),
EM(APPLICATION_NOT_FOUND),
EM(APPL_INTEGRITY_ERROR),
EM(AUTHENTICATION_ERROR),
EM(ADDITIONAL_FRAME),
EM(BOUNDARY_ERROR),
EM(PICC_INTEGRITY_ERROR),
EM(COMMAND_ABORTED),
EM(PICC_DISABLED_ERROR),
EM(COUNT_ERROR),
EM(DUPLICATE_ERROR),
EM(EEPROM_ERROR),
EM(FILE_NOT_FOUND),
EM(FILE_INTEGRITY_ERROR),
{ 0, NULL }
};
const char *
desfire_error_lookup (uint8_t code)
{
struct error_message *e = error_messages;
while (e->message) {
if (e->code == code)
return (e->message);
e++;
}
return "Invalid error code";
}

View file

@ -1,4 +1,4 @@
/*
/*-
* Copyright (C) 2010, Romain Tartiere, Romuald Conty.
*
* This program is free software: you can redistribute it and/or modify it
@ -24,10 +24,11 @@
#include "freefare_internal.h"
struct supported_tag supported_tags[] = {
{ { 0x00, 0x44 }, 0x00, ULTRALIGHT, "Mifare UltraLight" },
{ { 0x00, 0x04 }, 0x08, CLASSIC_1K, "Mifare Classic 1k" },
{ { 0x00, 0x02 }, 0x18, CLASSIC_4K, "Mifare Classic 4k" },
{ { 0x00, 0x02 }, 0x38, CLASSIC_4K, "Mifare Classic 4k (Emulated)" },
{ { 0x03, 0x44 }, 0x20, DESFIRE_4K, "Mifare DESFire 4k" },
{ { 0x00, 0x44 }, 0x00, ULTRALIGHT, "Mifare UltraLight" },
};
@ -106,6 +107,9 @@ freefare_get_tags (nfc_device_t *device)
case CLASSIC_4K:
tags[tag_count-1] = mifare_classic_tag_new ();
break;
case DESFIRE_4K:
tags[tag_count-1] = mifare_desfire_tag_new ();
break;
case ULTRALIGHT:
tags[tag_count-1] = mifare_ultralight_tag_new ();
break;
@ -172,6 +176,9 @@ freefare_free_tag (MifareTag tag)
case CLASSIC_4K:
mifare_classic_tag_free (tag);
break;
case DESFIRE_4K:
mifare_desfire_tag_free (tag);
break;
case ULTRALIGHT:
mifare_ultralight_tag_free (tag);
break;

View file

@ -1,5 +1,5 @@
/*-
* Copyright (C) 2009, Romain Tartiere, Romuald Conty.
* Copyright (C) 2009, 2010, 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
@ -41,7 +41,7 @@ enum mifare_tag_type {
// PLUS_X2K,
// PLUS_X4K,
// DESFIRE_2K,
// DESFIRE_4K,
DESFIRE_4K,
// DESFIRE_8K
};
@ -152,6 +152,191 @@ void mifare_application_free (Mad mad, const MadAid aid);
MifareClassicSectorNumber *mifare_application_find (Mad mad, const MadAid aid);
/* File types */
enum mifare_desfire_file_types {
MDFT_STANDARD_DATA_FILE = 0x00,
MDFT_BACKUP_DATA_FILE = 0x01,
MDFT_VALUE_FILE_WITH_BACKUP = 0x02,
MDFT_LINEAR_RECORD_FILE_WITH_BACKUP = 0x03,
MDFT_CYCLIC_RECORD_FILE_WITH_BACKUP = 0x04
};
/* Communication mode */
#define MDCM_PLAIN 0x00
#define MDCM_MACING 0x01
#define MDCM_FULLDES 0x03
/* Access right */
#define MDAR(read,write,read_write,change_access_rights) ( \
(read << 12) | \
(write << 8) | \
(read_write << 4) | \
(change_access_rights) \
)
#define MDAR_READ(ar) (((ar) >> 12) & 0x0f)
#define MDAR_WRITE(ar) (((ar) >> 8) & 0x0f)
#define MDAR_READ_WRITE(ar) (((ar) >> 4) & 0x0f)
#define MDAR_CHANGE_AR(ar) ((ar) & 0x0f)
#define MDAD_KEY0 0x0
#define MDAD_KEY1 0x1
#define MDAD_KEY2 0x2
#define MDAD_KEY3 0x3
#define MDAD_KEY4 0x4
#define MDAD_KEY5 0x5
#define MDAD_KEY6 0x6
#define MDAD_KEY7 0x7
#define MDAD_KEY8 0x8
#define MDAD_KEY9 0x9
#define MDAD_KEY10 0xa
#define MDAD_KEY11 0xb
#define MDAD_KEY12 0xc
#define MDAD_KEY13 0xd
#define MDAR_FREE 0xE
#define MDAR_DENY 0xF
/* Status and error codes */
#define OPERATION_OK 0x00
#define NO_CHANGES 0x0C
#define OUT_OF_EEPROM_ERROR 0x0E
#define ILLEGAL_COMMAND_CODE 0x1C
#define INTEGRITY_ERROR 0x1E
#define NO_SUCH_KEY 0x40
#define LENGTH_ERROR 0x7E
#define PERMISSION_ERROR 0x9D
#define PARAMETER_ERROR 0x9E
#define APPLICATION_NOT_FOUND 0xA0
#define APPL_INTEGRITY_ERROR 0xA1
#define AUTHENTICATION_ERROR 0xAE
#define ADDITIONAL_FRAME 0xAF
#define BOUNDARY_ERROR 0xBE
#define PICC_INTEGRITY_ERROR 0xC1
#define COMMAND_ABORTED 0xCA
#define PICC_DISABLED_ERROR 0xCD
#define COUNT_ERROR 0xCE
#define DUPLICATE_ERROR 0xDE
#define EEPROM_ERROR 0xEE
#define FILE_NOT_FOUND 0xF0
#define FILE_INTEGRITY_ERROR 0xF1
struct mifare_desfire_aid;
typedef struct mifare_desfire_aid *MifareDESFireAID;
MifareDESFireAID mifare_desfire_aid_new (uint8_t application_code, uint8_t function_cluster_code, uint8_t n);
MifareDESFireAID mifare_desfire_aid_new_with_mad_aid (MadAid mad_aid, uint8_t n);
struct mifare_desfire_key;
typedef struct mifare_desfire_key *MifareDESFireKey;
#pragma pack (push)
#pragma pack (1)
struct mifare_desfire_version_info {
struct {
uint8_t vendor_id;
uint8_t type;
uint8_t subtype;
uint8_t version_major;
uint8_t version_minor;
uint8_t storage_size;
uint8_t protocol;
} hardware;
struct {
uint8_t vendor_id;
uint8_t type;
uint8_t subtype;
uint8_t version_major;
uint8_t version_minor;
uint8_t storage_size;
uint8_t protocol;
} software;
uint8_t uid[7];
uint8_t batch_number[5];
uint8_t production_week;
uint8_t production_year;
};
#pragma pack (pop)
struct mifare_desfire_file_settings {
uint8_t file_type;
uint8_t communication_settings;
uint16_t access_rights;
union {
struct {
uint32_t file_size;
} standard_file;
struct {
int32_t lower_limit;
int32_t upper_limit;
int32_t limited_credit_value;
uint8_t limited_credit_enabled;
} value_file;
struct {
uint32_t record_size;
uint32_t max_number_of_records;
uint32_t current_number_of_records;
} linear_record_file;
} settings;
};
int mifare_desfire_connect (MifareTag tag);
int mifare_desfire_disconnect (MifareTag tag);
uint8_t mifare_desfire_get_last_error (MifareTag tag);
int mifare_desfire_authenticate (MifareTag tag, uint8_t key_no, MifareDESFireKey key);
int mifare_desfire_change_key_settings (MifareTag tag, uint8_t settings);
int mifare_desfire_get_key_settings (MifareTag tag, uint8_t *settings, uint8_t *max_keys);
int mifare_desfire_change_key (MifareTag tag, uint8_t key_no, MifareDESFireKey new_key, MifareDESFireKey old_key);
int mifare_desfire_get_key_version (MifareTag tag, uint8_t key_no, uint8_t *version);
int mifare_desfire_create_application (MifareTag tag, MifareDESFireAID aid, uint8_t settings, uint8_t key_no);
int mifare_desfire_delete_application (MifareTag tag, MifareDESFireAID aid);
int mifare_desfire_get_application_ids (MifareTag tag, MifareDESFireAID *aids[], size_t *count);
void mifare_desfire_free_application_ids (MifareDESFireAID aids[]);
int mifare_desfire_select_application (MifareTag tag, MifareDESFireAID aid);
int mifare_desfire_format_picc (MifareTag tag);
int mifare_desfire_get_version (MifareTag tag, struct mifare_desfire_version_info *version_info);
int mifare_desfire_get_file_ids (MifareTag tag, uint8_t *files[], size_t *count);
int mifare_desfire_get_file_settings (MifareTag tag, uint8_t file_no, struct mifare_desfire_file_settings *settings);
int mifare_desfire_change_file_settings (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights);
int mifare_desfire_create_std_data_file (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights, uint32_t file_size);
int mifare_desfire_create_backup_data_file (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights, uint32_t file_size);
int mifare_desfire_create_value_file (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights, int32_t lower_limit, int32_t upper_limit, int32_t value, uint8_t limited_credit_enable);
int mifare_desfire_create_linear_record_file (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights, uint32_t record_size, uint32_t max_number_of_records);
int mifare_desfire_create_cyclic_record_file (MifareTag tag, uint8_t file_no, uint8_t communication_settings, uint16_t access_rights, uint32_t record_size, uint32_t max_number_of_records);
int mifare_desfire_delete_file (MifareTag tag, uint8_t file_no);
ssize_t mifare_desfire_read_data (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data);
ssize_t mifare_desfire_read_data_ex (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data, int cs);
ssize_t mifare_desfire_write_data (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data);
ssize_t mifare_desfire_write_data_ex (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data, int cs);
int mifare_desfire_get_value (MifareTag tag, uint8_t file_no, int32_t *value);
int mifare_desfire_get_value_ex (MifareTag tag, uint8_t file_no, int32_t *value, int cs);
int mifare_desfire_credit (MifareTag tag, uint8_t file_no, int32_t amount);
int mifare_desfire_credit_ex (MifareTag tag, uint8_t file_no, int32_t amount, int cs);
int mifare_desfire_debit (MifareTag tag, uint8_t file_no, int32_t amount);
int mifare_desfire_debit_ex (MifareTag tag, uint8_t file_no, int32_t amount, int cs);
int mifare_desfire_limited_credit (MifareTag tag, uint8_t file_no, int32_t amount);
int mifare_desfire_limited_credit_ex (MifareTag tag, uint8_t file_no, int32_t amount, int cs);
ssize_t mifare_desfire_write_record (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data);
ssize_t mifare_desfire_write_record_ex (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data, int cs);
ssize_t mifare_desfire_read_records (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data);
ssize_t mifare_desfire_read_records_ex (MifareTag tag, uint8_t file_no, off_t offset, size_t length, void *data, int cs);
int mifare_desfire_clear_record_file (MifareTag tag, uint8_t file_no);
int mifare_desfire_commit_transaction (MifareTag tag);
int mifare_desfire_abort_transaction (MifareTag tag);
MifareDESFireKey mifare_desfire_des_key_new (uint8_t value[8]);
MifareDESFireKey mifare_desfire_3des_key_new (uint8_t value[16]);
MifareDESFireKey mifare_desfire_des_key_new_with_version (uint8_t value[8]);
MifareDESFireKey mifare_desfire_3des_key_new_with_version (uint8_t value[16]);
uint8_t mifare_desfire_key_get_version (MifareDESFireKey key);
void mifare_desfire_key_set_version (MifareDESFireKey key, uint8_t version);
void mifare_desfire_key_free (MifareDESFireKey key);
const char *desfire_error_lookup (uint8_t error);
uint8_t *tlv_encode (const uint8_t type, const uint8_t *istream, uint16_t isize, size_t *osize);
uint8_t *tlv_decode (const uint8_t *istream, uint8_t *type, uint16_t *size);
@ -161,5 +346,4 @@ uint8_t *tlv_append (uint8_t *a, uint8_t *b);
}
#endif // __cplusplus
#endif /* !__FREEFARE_H__ */

View file

@ -1,4 +1,4 @@
/*
/*-
* Copyright (C) 2010, Romain Tartiere, Romuald Conty.
*
* This program is free software: you can redistribute it and/or modify it
@ -22,6 +22,8 @@
#include "config.h"
#include <openssl/des.h>
/*
* Endienness macros
*
@ -39,10 +41,6 @@
* dealt with).
*/
#if !defined(le32toh) && defined(letoh32)
# define le32toh(x) letoh32(x)
#endif
#if !defined(le32toh) && defined(bswap_32)
# if BYTE_ORDER == LITTLE_ENDIAN
# define be32toh(x) bswap_32(x)
@ -71,6 +69,8 @@
# endif
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
struct mad_sector_0x00;
struct mad_sector_0x10;
@ -78,11 +78,24 @@ struct mad_sector_0x10;
void nxp_crc (uint8_t *crc, const uint8_t value);
MifareTag mifare_classic_tag_new (void);
void mifare_classic_tag_free (MifareTag tag);
MifareTag mifare_desfire_tag_new (void);
void mifare_desfire_tag_free (MifareTag tags);
MifareTag mifare_ultralight_tag_new (void);
void mifare_ultralight_tag_free (MifareTag tag);
uint8_t sector_0x00_crc8 (Mad mad);
uint8_t sector_0x10_crc8 (Mad mad);
typedef enum {
MD_SEND,
MD_RECEIVE
} MifareDirection;
void *mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, int communication_settings);
void *mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int communication_settings);
void mifare_cbc_des (MifareDESFireKey key, uint8_t *data, size_t data_size, MifareDirection direction, int mac);
void rol8(uint8_t *data);
void *assert_crypto_buffer_size (MifareTag tag, size_t nbytes);
#define MIFARE_ULTRALIGHT_PAGE_COUNT 16
struct supported_tag {
@ -123,6 +136,33 @@ struct mifare_classic_tag {
} cached_access_bits;
};
struct mifare_desfire_aid {
uint8_t data[3];
};
struct mifare_desfire_key {
uint8_t data[16];
enum {
T_DES,
T_3DES
} type;
DES_key_schedule ks1;
DES_key_schedule ks2;
};
struct mifare_desfire_tag {
struct mifare_tag __tag;
uint8_t last_picc_error;
char *last_pcd_error;
MifareDESFireKey session_key;
uint8_t authenticated_key_no;
uint8_t *crypto_buffer;
size_t crypto_buffer_size;
};
MifareDESFireKey mifare_desfire_session_key_new (uint8_t rnda[8], uint8_t rndb[8], MifareDESFireKey authentication_key);
struct mifare_ultralight_tag {
struct mifare_tag __tag;
@ -140,8 +180,9 @@ struct mifare_ultralight_tag {
#define ASSERT_ACTIVE(tag) do { if (!tag->active) return errno = ENXIO, -1; } while (0)
#define ASSERT_INACTIVE(tag) do { if (tag->active) return errno = ENXIO, -1; } while (0)
#define ASSERT_MIFARE_ULTRALIGHT(tag) do { if (tag->tag_info->type != ULTRALIGHT) return errno = ENODEV, -1; } while (0)
#define ASSERT_MIFARE_CLASSIC(tag) do { if ((tag->tag_info->type != CLASSIC_1K) && (tag->tag_info->type != CLASSIC_4K)) return errno = ENODEV, -1; } while (0)
#define ASSERT_MIFARE_DESFIRE(tag) do { if (tag->tag_info->type != DESFIRE_4K) return errno = ENODEV, -1; } while (0)
#define ASSERT_MIFARE_ULTRALIGHT(tag) do { if (tag->tag_info->type != ULTRALIGHT) return errno = ENODEV, -1; } while (0)
/*
* MifareTag cast macros
@ -150,6 +191,7 @@ struct mifare_ultralight_tag {
* MifareTag structures to concrete Tags (e.g. MIFARE Classic tag).
*/
#define MIFARE_CLASSIC(tag) ((struct mifare_classic_tag *) tag)
#define MIFARE_DESFIRE(tag) ((struct mifare_desfire_tag *) tag)
#define MIFARE_ULTRALIGHT(tag) ((struct mifare_ultralight_tag *) tag)
/*

96
libfreefare/hexdump.c Normal file
View file

@ -0,0 +1,96 @@
/*-
* Copyright (c) 1986, 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* 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, this list of conditions and the following disclaimer.
* 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.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
*/
//#include <sys/cdefs.h>
//__FBSDID("$FreeBSD: stable/8/lib/libutil/hexdump.c 180161 2008-07-01 22:30:57Z jhb $");
#include <sys/types.h>
#include <libutil.h>
#include <stdio.h>
void
hexdump(const void *ptr, int length, const char *hdr, int flags)
{
int i, j, k;
int cols;
const unsigned char *cp;
char delim;
if ((flags & HD_DELIM_MASK) != 0)
delim = (flags & HD_DELIM_MASK) >> 8;
else
delim = ' ';
if ((flags & HD_COLUMN_MASK) != 0)
cols = flags & HD_COLUMN_MASK;
else
cols = 16;
cp = ptr;
for (i = 0; i < length; i+= cols) {
if (hdr != NULL)
printf("%s", hdr);
if ((flags & HD_OMIT_COUNT) == 0)
printf("%04x ", i);
if ((flags & HD_OMIT_HEX) == 0) {
for (j = 0; j < cols; j++) {
k = i + j;
if (k < length)
printf("%c%02x", delim, cp[k]);
else
printf(" ");
}
}
if ((flags & HD_OMIT_CHARS) == 0) {
printf(" |");
for (j = 0; j < cols; j++) {
k = i + j;
if (k >= length)
printf(" ");
else if (cp[k] >= ' ' && cp[k] <= '~')
printf("%c", cp[k]);
else
printf(".");
}
printf("|");
}
printf("\n");
}
}

View file

@ -32,6 +32,7 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <freefare.h>
@ -46,8 +47,6 @@
#define SECTOR_0X00_AIDS 15
#define SECTOR_0X10_AIDS 23
#define MIN(a, b) ( (a < b) ? a : b )
struct mad_sector_0x00 {
uint8_t crc;
uint8_t info;
@ -83,8 +82,8 @@ mad_new (uint8_t version)
return NULL;
mad->version = version;
memset (&(mad->sector_0x00), '\0', sizeof (mad->sector_0x00));
memset (&(mad->sector_0x10), '\0', sizeof (mad->sector_0x10));
bzero (&(mad->sector_0x00), sizeof (mad->sector_0x00));
bzero (&(mad->sector_0x10), sizeof (mad->sector_0x10));
return mad;
}
@ -331,7 +330,7 @@ mad_set_version (Mad mad, const uint8_t version)
{
if ((version == 2) && (mad->version == 1)) {
/* We use a larger MAD so initialise the new blocks */
memset (&(mad->sector_0x10), '\0', sizeof (mad->sector_0x10));
bzero (&(mad->sector_0x10), sizeof (mad->sector_0x10));
}
mad->version = version;
}

View file

@ -55,6 +55,7 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <nfc/nfc.h>
@ -663,7 +664,7 @@ mifare_classic_format_sector (MifareTag tag, const MifareClassicSectorNumber sec
}
MifareClassicBlock empty_data_block;
memset (empty_data_block, '\x00', sizeof (empty_data_block));
bzero (empty_data_block, sizeof (empty_data_block));
MifareClassicBlock default_trailer_block = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* Key A */

View file

@ -0,0 +1,525 @@
.\" Copyright (C) 2010 Romain Tartiere
.\"
.\" 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/>
.\"
.\" $Id$
.\"
.Dd July 11, 2010
.Dt MIFARE_DESFIRE 3
.Os
.\" _ _
.\" | \ | | __ _ _ __ ___ ___
.\" | \| |/ _` | '_ ` _ \ / _ \
.\" | |\ | (_| | | | | | | __/
.\" |_| \_|\__,_|_| |_| |_|\___|
.\"
.Sh NAME
.Nm mifare_desfire_connect ,
.Nm mifare_desfire_disconnect .
.\"
.Nm mifare_desfire_authenticate ,
.Nm mifare_desfire_change_key_settings ,
.Nm mifare_desfire_get_key_settings ,
.Nm mifare_desfire_change_key ,
.Nm mifare_desfire_get_key_version ,
.\"
.Nm mifare_desfire_create_application ,
.Nm mifare_desfire_delete_application ,
.Nm mifare_desfire_get_application_ids ,
.Nm mifare_desfire_free_application_ids ,
.Nm mifare_desfire_select_application ,
.\"
.Nm mifare_desfire_format_picc ,
.\"
.Nm mifare_desfire_get_version ,
.\"
.Nm mifare_desfire_get_file_ids ,
.Nm mifare_desfire_get_file_settings ,
.Nm mifare_desfire_change_file_settings ,
.Nm mifare_desfire_create_std_data_file ,
.Nm mifare_desfire_create_backup_data_file ,
.Nm mifare_desfire_create_value_file ,
.Nm mifare_desfire_create_linear_record_file ,
.Nm mifare_desfire_create_cyclic_record_file ,
.Nm mifare_desfire_delete_file ,
.\"
.Nm mifare_desfire_read_data ,
.Nm mifare_desfire_read_data_ex ,
.Nm mifare_desfire_write_data ,
.Nm mifare_desfire_write_data_ex ,
.Nm mifare_desfire_get_value ,
.Nm mifare_desfire_get_value_ex ,
.Nm mifare_desfire_credit ,
.Nm mifare_desfire_credit_ex ,
.Nm mifare_desfire_debit ,
.Nm mifare_desfire_debit_ex ,
.Nm mifare_desfire_limited_credit ,
.Nm mifare_desfire_limited_credit_ex ,
.Nm mifare_desfire_write_record ,
.Nm mifare_desfire_write_record_ex ,
.Nm mifare_desfire_read_records ,
.Nm mifare_desfire_read_records_ex ,
.Nm mifare_desfire_clear_record_file ,
.Nm mifare_desfire_commit_transaction ,
.Nm mifare_desfire_abort_transaction ,
.Nd Mifare DESFire Manipulation Functions
.\" _ _ _
.\" | | (_) |__ _ __ __ _ _ __ _ _
.\" | | | | '_ \| '__/ _` | '__| | | |
.\" | |___| | |_) | | | (_| | | | |_| |
.\" |_____|_|_.__/|_| \__,_|_| \__, |
.\" |___/
.Sh LIBRARY
Mifare card manipulation library (libfreefare, \-lfreefare)
.\" ____ _
.\" / ___| _ _ _ __ ___ _ __ ___(_)___
.\" \___ \| | | | '_ \ / _ \| '_ \/ __| / __|
.\" ___) | |_| | | | | (_) | |_) \__ \ \__ \
.\" |____/ \__, |_| |_|\___/| .__/|___/_|___/
.\" |___/ |_|
.Sh SYNOPSIS
.In freefare.h
.Ft int
.Fn mifare_desfire_connect "MifareTag tag"
.Ft int
.Fn mifare_desfire_disconnect "MifareTag tag"
.\".Ft uint8_t
.\".Fn mifare_desfire_get_last_error "MifareTag tag"
.Ft int
.Fn mifare_desfire_authenticate "MifareTag tag" "uint8_t key_no" "MifareDESFireKey key"
.Ft int
.Fn mifare_desfire_change_key_settings "MifareTag tag" "uint8_t settings"
.Ft int
.Fn mifare_desfire_get_key_settings "MifareTag tag" "uint8_t *settings" "uint8_t *max_keys"
.Ft int
.Fn mifare_desfire_change_key "MifareTag tag" "uint8_t key_no" "MifareDESFireKey new_key" "MifareDESFireKey old_key"
.Ft int
.Fn mifare_desfire_get_key_version "MifareTag tag" "uint8_t key_no" "uint8_t *version"
.Ft int
.Fn mifare_desfire_create_application "MifareTag tag" "MifareDESFireAID aid" "uint8_t settings" "uint8_t key_no"
.Ft int
.Fn mifare_desfire_delete_application "MifareTag tag" "MifareDESFireAID aid"
.Ft int
.Fn mifare_desfire_get_application_ids "MifareTag tag" "MifareDESFireAID *aids[]" "size_t *count"
.Ft void
.Fn mifare_desfire_free_application_ids "MifareDESFireAID aids[]"
.Ft int
.Fn mifare_desfire_select_application "MifareTag tag" "MifareDESFireAID aid"
.Ft int
.Fn mifare_desfire_format_picc "MifareTag tag"
.Ft int
.Fn mifare_desfire_get_version "MifareTag tag" "struct mifare_desfire_version_info *version_info"
.Ft int
.Fn mifare_desfire_get_file_ids "MifareTag tag" "uint8_t *files[]" "size_t *count"
.Ft int
.Fn mifare_desfire_get_file_settings "MifareTag tag" "uint8_t file_no" "struct mifare_desfire_file_settings *settings"
.Ft int
.Fn mifare_desfire_change_file_settings "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights"
.Ft int
.Fn mifare_desfire_create_std_data_file "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights" "uint32_t file_size"
.Ft int
.Fn mifare_desfire_create_backup_data_file "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights" "uint32_t file_size"
.Ft int
.Fn mifare_desfire_create_value_file "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights" "int32_t lower_limit" "int32_t upper_limit" "int32_t value" "uint8_t limited_credit_enable"
.Ft int
.Fn mifare_desfire_create_linear_record_file "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights" "uint32_t record_size" "uint32_t max_number_of_records"
.Ft int
.Fn mifare_desfire_create_cyclic_record_file "MifareTag tag" "uint8_t file_no" "uint8_t communication_settings" "uint16_t access_rights" "uint32_t record_size" "uint32_t max_number_of_records"
.Ft int
.Fn mifare_desfire_delete_file "MifareTag tag" "uint8_t file_no"
.Ft ssize_t
.Fn mifare_desfire_read_data "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data"
.Ft ssize_t
.Fn mifare_desfire_read_data_ex "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data" "int cs"
.Ft ssize_t
.Fn mifare_desfire_write_data "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data"
.Ft ssize_t
.Fn mifare_desfire_write_data_ex "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data" "int cs"
.Ft int
.Fn mifare_desfire_get_value "MifareTag tag" "uint8_t file_no" "int32_t *value"
.Ft int
.Fn mifare_desfire_get_value_ex "MifareTag tag" "uint8_t file_no" "int32_t *value" "int cs"
.Ft int
.Fn mifare_desfire_credit "MifareTag tag" "uint8_t file_no" "int32_t amount"
.Ft int
.Fn mifare_desfire_credit_ex "MifareTag tag" "uint8_t file_no" "int32_t amount" "int cs"
.Ft int
.Fn mifare_desfire_debit "MifareTag tag" "uint8_t file_no" "int32_t amount"
.Ft int
.Fn mifare_desfire_debit_ex "MifareTag tag" "uint8_t file_no" "int32_t amount" "int cs"
.Ft int
.Fn mifare_desfire_limited_credit "MifareTag tag" "uint8_t file_no" "int32_t amount"
.Ft int
.Fn mifare_desfire_limited_credit_ex "MifareTag tag" "uint8_t file_no" "int32_t amount" "int cs"
.Ft ssize_t
.Fn mifare_desfire_write_record "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data"
.Ft ssize_t
.Fn mifare_desfire_write_record_ex "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data" "int cs"
.Ft ssize_t
.Fn mifare_desfire_read_records "MifareTag tag" "uint8_t file_no" "off_t offset" "size_t length" "void *data"
.Ft ssize_t
.Fn mifare_desfire_read_records_ex "MifareTag tag" "uint7_t file_no" "off_t offset" "size_t length" "void *data" "int cs"
.Ft int
.Fn mifare_desfire_clear_record_file "MifareTag tag" "uint8_t file_no"
.Ft int
.Fn mifare_desfire_commit_transaction "MifareTag tag"
.Ft int
.Fn mifare_desfire_abort_transaction "MifareTag tag"
.\" ____ _ _ _
.\" | _ \ ___ ___ ___ _ __(_)_ __ | |_(_) ___ _ __
.\" | | | |/ _ \/ __|/ __| '__| | '_ \| __| |/ _ \| '_ \
.\" | |_| | __/\__ \ (__| | | | |_) | |_| | (_) | | | |
.\" |____/ \___||___/\___|_| |_| .__/ \__|_|\___/|_| |_|
.\" |_|
.Sh DESCRIPTION
The
.Fn mifare_desfire_*
functions allows management of Mifare DESFire tags.
.Pp
.Ss Card-level operations
The
.Fn mifare_desfire_connect
and
.Fn mifare_desfire_disconnect
functions activates and deactivates the provided
.Vt tag .
All
.Fn mifare_desfire_*
functions that operates on a
.Vt tag
require it to be on activated.
.Pp
After activation, the selected application is the master application. It is
possible to select another application using the
.Fn mifare_desfire_select_application
function (see bellow).
.Pp
The
.Fn mifare_desfire_get_version
function retrieve various information about the provided
.Vt tag ,
including UID, batch number, production date, and hardware and software
information. Refer to the freefare.h header file for details about the
.Vt settings
field.
.Pp
The
.Fn mifare_desfire_format_picc
function resets
.Vt tag
to factory defaults. For this function to work, a previous authentication with
the card master key is required.
.Pp
.Ss Application-level operations
The
.Fn mifare_desfire_select_application
function makes the application identified by
.Vt aid
the active one. Further file operations will be performed in the context of
this application. After a call to
.Vt mifare_desfire_connect ,
the default application is the card master application. It can be selected
again calling the
.Fn mifare_desfire_select_application
function either with an
.Vt aid
with all its fields set to 0, or by providing the NULL
.Vt aid .
.Pp
The
.Fn mifare_desfire_authenticate
function performs an authentication using the key number
.Vt key_no
on the card and the
.Vt key
(3)DES key on
.Vt tag .
.Pp
The
.Fn mifare_desfire_get_key_settings
function, returns the
.Vt settings
and the number of keys
.Vt max_keys
of the selected application.
.Pp
The
.Fn mifare_desfire_change_key_settings
function changes the selected application settings to
.Vt settings .
The application number of keys cannot be changed after the application has been
created.
.Pp
The
.Fn mifare_desfire_change_key
changes the key
.Vt key_no
from
.Vt old_key
to
.Vt new_key
on
.Vt tag .
Depending on the application settings, a previous authentication with the same
key or another key may be required.
.Pp
The
.Fn mifare_desfire_get_key_version
function retrieves the
.Vt version
of the key with number
.Vt key_no
of the selected application.
.Pp
The
.Fn mifare_desfire_create_application
function, creates an application with AID
.Vt aid ,
the
.Vt settings
key settings and
.Vt key_no
authentication keys. Authentication keys are set to 0 after creation.
.Pp
The
.Fn mifare_desfire_delete_application
deletes the application identified by AID
.Vt aid .
.Pp
The
.Fn mifare_desfire_get_application_ids
function returns a list of all applications of the card. The
.Vt aids
array has to be freed after usage calling
.Fn mifare_desfire_free_application_ids .
.Pp
.Ss File-level operations
The
.Fn mifare_desfire_get_file_ids
function returns the list of
.Vt count
files in the selected application as
.Vt files .
The memory allocated for
.Vt files
has to be reclaimed using
.Xr free 3 .
.Pp
The
.Fn mifare_desfire_get_file_settings
function retrieves the
.Vt settings
of the file
.Vt file_no
of the selected application of
.Vt tag .
.Pp
The
.Fn mifare_desfire_change_file_settings
function change the
.Vt communication_settings
and
.Vt access_rights
of the file
.Vt file_no
of the selected application of
.Vt tag .
.Pp
The
.Fn mifare_desfire_create_*
family of functions create a new file
.Vt file_no
with the provided
.Vt communication_settings
and
.Vt access_rights
on
.Vt tag.
.Bl -tag -width indent
.It Fn mifare_desfire_create_std_data_file
creates a standard data file of size
.Vt file_size .
.It Fn mifare_desfire_create_backup_data_file
creates a backup data file of size
.Vt file_size .
.It Fn mifare_desfire_create_value_file
creates a value file of value
.Vt value
constrained in the range
.Vt lower_limit
.Vt upper_limit ,
and with the
.Vt limited_credit_enable
settings.
.It Fn mifare_desfire_create_linear_record_file
creates a linear record file that can hold
.Vt max_number_of_records
records of size
.Vt record_size .
.It Fn mifare_desfire_create_cyclic_record_file
creates a cyclic record file that can hold
.Vt max_number_of_records
records of size
.Vt record_size .
.El
.Pp
The
.Fn mifare_desfire_delete_file
removes the file
.Vt file_no
from the selected application of
.Vt tag .
.Ss Data-level operations
The
.Fn mifare_desfire_read_data
function reads
.Vt length
bytes of data from offset
.Vt offset
of the file
.Vt file_no
and copies it to
.Vt data .
If
.Vt length
is set to 0, the file is read to end. The function returns the number of bytes
read.
.Pp
The
.Fn mifare_desfire_write_data
function writes
.Vt length
bytes of data from offset
.Vt offset
of the file
.Vt file_no
and copies it to
.Vt data .
The function returns the number of bytes written.
.Pp
The
.Fn mifare_desfire_get_value
reads the
.Vt value
of the file
.Vt file_no
of the selected application.
.Pp
The
.Fn mifare_desfire_credit
function adds
.Vt amount
to the value of the file
.Vt file_no
of the selected application.
.Pp
The
.Fn mifare_desfire_debit
function substracts
.Vt amount
to the value of the file
.Vt file_no
of the selected application.
.Pp
to the value of the file
.Vt file_no
of the selected application.
.Pp
The
.Fn mifare_desfire_limited_credit
function adds
.Vt amount
to the value of the file
.Vt file_no
of the selected application.
.Pp
The
.Fn mifare_desfire_write_record
function writes
.Vt length
records starting at record
.Vt offset
of
.Vt data
in the file
.Vt file_no
and returns the number of bytes written.
.Pp
The
.Fn mifare_desfire_read_records
function reads
.Vt length
records starting at record
.Vt offset
from the file
.Vt file_no
and copy them to
.Vt data ,
returning the number of bytes read.
.Pp
The
.Fn mifare_desfire_clear_record_file
function erase all records from the file
.Vt file_no
of the selected application.
.Pp
The
.Fn mifare_desfire_commit_transaction
validates the set of pending changes on the
.Vt tag ,
while the
.Fn mifare_desfire_abort_transaction
rollbacks the changes.
.Pp
All data-manipulation functions that read data from and write data to files
come with an
.Fn *_ex
variant (e.g.
.Fn mifare_desfire_read_data_ex )
which accepts an extra parameter
.Vt cs
that defines the communication settings to use. If not provided, the library
will try to read-out this value from the file's configuration. Because reading
this information may be denied, the
.Fn *_ex
variant of functions still allows using the library for advanced usage.
.\" ____ _ _
.\" | _ \ ___| |_ _ _ _ __ _ __ __ ____ _| |_ _ ___ ___
.\" | |_) / _ \ __| | | | '__| '_ \ \ \ / / _` | | | | |/ _ \/ __|
.\" | _ < __/ |_| |_| | | | | | | \ V / (_| | | |_| | __/\__ \
.\" |_| \_\___|\__|\__,_|_| |_| |_| \_/ \__,_|_|\__,_|\___||___/
.\"
.Sh RETURN VALUES
Unless stated otherwise, all other functions return a value greater than or
equal to
.Va 0
on success or
.Va -1
on failure.
.\" ____ _
.\" / ___| ___ ___ __ _| |___ ___
.\" \___ \ / _ \/ _ \ / _` | / __|/ _ \
.\" ___) | __/ __/ | (_| | \__ \ (_) |
.\" |____/ \___|\___| \__,_|_|___/\___/
.\"
.Sh SEE ALSO
.Xr freefare 3
.\" _ _ _
.\" / \ _ _| |_| |__ ___ _ __ ___
.\" / _ \| | | | __| '_ \ / _ \| '__/ __|
.\" / ___ \ |_| | |_| | | | (_) | | \__ \
.\" /_/ \_\__,_|\__|_| |_|\___/|_| |___/
.\"
.Sh AUTHORS
.An Romain Tartiere Aq romain@il4p.org

1247
libfreefare/mifare_desfire.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,50 @@
/*-
* Copyright (C) 2010, Romain Tartiere.
*
* 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/>
*
* $Id$
*/
#include <errno.h>
#include <stdlib.h>
#include <freefare.h>
#include "freefare_internal.h"
MifareDESFireAID
mifare_desfire_aid_new (uint8_t application_code, uint8_t function_cluster_code, uint8_t n)
{
MadAid mad_aid = { application_code, function_cluster_code };
return mifare_desfire_aid_new_with_mad_aid (mad_aid, n);
}
MifareDESFireAID
mifare_desfire_aid_new_with_mad_aid (MadAid mad_aid, uint8_t n)
{
MifareDESFireAID res;
if (n & 0xf0)
return errno = EINVAL, NULL;
if ((res = malloc (sizeof (*res)))) {
res->data[0] = 0xf0 | (mad_aid.function_cluster_code >> 4);
res->data[1] = (uint8_t) (((mad_aid.function_cluster_code & 0x0f) << 4) | ((mad_aid.application_code & 0xf0) >> 4));
res->data[2] = ((mad_aid.application_code & 0x0f) << 4) | n;
}
return res;
}

View file

@ -0,0 +1,297 @@
/*-
* Copyright (C) 2010, Romain Tartiere.
*
* 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/>
*
* $Id$
*/
#include "config.h"
#include <openssl/des.h>
#include <string.h>
#include <strings.h>
#include <freefare.h>
#include "freefare_internal.h"
static void xor8 (uint8_t *ivect, uint8_t *data);
static void mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac);
static size_t padded_data_length (size_t nbytes);
static size_t maced_data_length (size_t nbytes);
static size_t enciphered_data_length (size_t nbytes);
static void
xor8 (uint8_t *ivect, uint8_t *data)
{
for (int i = 0; i < 8; i++) {
data[i] ^= ivect[i];
}
}
void
rol8(uint8_t *data)
{
uint8_t first = data[0];
for (int i = 0; i < 7; i++) {
data[i] = data[i+1];
}
data[7] = first;
}
/*
* Size required to store nbytes of data in a buffer of size n*8.
*/
static size_t
padded_data_length (size_t nbytes)
{
if (nbytes % 8)
return ((nbytes / 8) + 1) * 8;
else
return nbytes;
}
/*
* Buffer size required to MAC nbytes of data
*/
static size_t
maced_data_length (size_t nbytes)
{
return nbytes + 4;
}
/*
* Buffer size required to encipher nbytes of data and a two bytes CRC.
*/
static size_t
enciphered_data_length (size_t nbytes)
{
return padded_data_length (nbytes + 2);
}
/*
* Ensure that tag's crypto buffer is large enough to store nbytes of data.
*/
void *
assert_crypto_buffer_size (MifareTag tag, size_t nbytes)
{
void *res = MIFARE_DESFIRE (tag)->crypto_buffer;
if (MIFARE_DESFIRE (tag)->crypto_buffer_size < nbytes) {
if ((res = realloc (MIFARE_DESFIRE (tag)->crypto_buffer, nbytes))) {
MIFARE_DESFIRE (tag)->crypto_buffer = res;
MIFARE_DESFIRE (tag)->crypto_buffer_size = nbytes;
}
}
return res;
}
void *
mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, int communication_settings)
{
void *res;
uint8_t mac[4];
size_t edl, mdl;
switch (communication_settings) {
case 0:
case 2:
res = data;
break;
case 1:
edl = padded_data_length (*nbytes);
if (!(res = assert_crypto_buffer_size (tag, edl)))
abort();
// Fill in the crypto buffer with data ...
memcpy (res, data, *nbytes);
// ... and 0 padding
bzero ((uint8_t *)res + *nbytes, edl - *nbytes);
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, res, edl, MD_SEND, 1);
memcpy (mac, (uint8_t *)res + edl - 8, 4);
mdl = maced_data_length (*nbytes);
if (!(res = assert_crypto_buffer_size (tag, mdl)))
abort();
memcpy (res, data, *nbytes);
memcpy ((uint8_t *)res + *nbytes, mac, 4);
*nbytes += 4;
break;
case 3:
edl = enciphered_data_length (*nbytes);
if (!(res = assert_crypto_buffer_size (tag, edl)))
abort();
// Fill in the crypto buffer with data ...
memcpy (res, data, *nbytes);
// ... CRC ...
append_iso14443a_crc (res, *nbytes);
// ... and 0 padding
bzero ((uint8_t *)(res) + *nbytes + 2, edl - *nbytes - 2);
*nbytes = edl;
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, res, *nbytes, MD_SEND, 0);
break;
default:
res = NULL;
break;
}
return res;
}
void *
mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int communication_settings)
{
void *res = data;
size_t edl;
void *edata;
switch (communication_settings) {
case 0:
case 2:
break;
case 1:
*nbytes -= 4;
edl = enciphered_data_length (*nbytes);
edata = malloc (edl);
memcpy (edata, data, *nbytes);
bzero ((uint8_t *)edata + *nbytes, edl - *nbytes);
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, edata, edl, MD_SEND, 1);
/* ,^^^^^^^
* No! This is not a typo! ---------------------------------'
*/
if (0 != memcmp ((uint8_t *)data + *nbytes, (uint8_t *)edata + edl - 8, 4)) {
printf ("MACing not verified\n");
*nbytes = -1;
res = NULL;
}
free (edata);
break;
case 3:
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, res, *nbytes, MD_RECEIVE, 0);
/*
* Look for the CRC and ensure it is following by NULL padding. We
* can't start by the end because the CRC is supposed to be 0 when
* verified, and accumulating 0's in it should not change it.
*/
bool verified = false;
int end_crc_pos = *nbytes - 7; // The CRC can be over two blocks
do {
uint16_t crc;
iso14443a_crc (res, end_crc_pos, (uint8_t *)&crc);
if (!crc) {
verified = true;
for (int n = end_crc_pos; n < *nbytes; n++) {
uint8_t byte = ((uint8_t *)res)[n];
if (!( (0x00 == byte) || ((0x80 == byte) && (n == end_crc_pos)) ))
verified = false;
}
}
if (verified) {
*nbytes = end_crc_pos - 2;
} else {
end_crc_pos++;
}
} while (!verified && (end_crc_pos < *nbytes));
if (!verified) {
printf ("(3)DES not verified\n");
*nbytes = -1;
res = NULL;
}
break;
default:
printf ("Unknown communication settings\n");
*nbytes = -1;
res = NULL;
break;
}
return res;
}
static void
mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection direction, int mac)
{
uint8_t ovect[8];
if (direction == MD_SEND) {
xor8 (ivect, data);
} else {
memcpy (ovect, data, 8);
}
uint8_t edata[8];
switch (key->type) {
case T_DES:
if (mac) {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
} else {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
}
memcpy (data, edata, 8);
break;
case T_3DES:
if (mac) {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
} else {
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
}
memcpy (data, edata, 8);
break;
}
if (direction == MD_SEND) {
memcpy (ivect, data, 8);
} else {
xor8 (ivect, data);
memcpy (ivect, ovect, 8);
}
}
void
mifare_cbc_des (MifareDESFireKey key, uint8_t *data, size_t data_size, MifareDirection direction, int mac)
{
size_t offset = 0;
uint8_t ivect[8];
bzero (ivect, sizeof (ivect));
while (offset < data_size) {
mifare_des (key, data + offset, ivect, direction, mac);
offset += 8;
}
}

View file

@ -0,0 +1,134 @@
.\" Copyright (C) 2010 Romain Tartiere
.\"
.\" 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/>
.\"
.\" $Id$
.\"
.Dd July 20, 2010
.Dt MIFARE_DESFIRE_KEY 3
.Os
.\" _ _
.\" | \ | | __ _ _ __ ___ ___
.\" | \| |/ _` | '_ ` _ \ / _ \
.\" | |\ | (_| | | | | | | __/
.\" |_| \_|\__,_|_| |_| |_|\___|
.\"
.Sh NAME
.Nm mifare_desfire_des_key_new ,
.Nm mifare_desfire_3des_key_new ,
.Nm mifare_desfire_des_key_new_with_version ,
.Nm mifare_desfire_3des_key_new_with_version ,
.Nm mifare_desfire_key_get_version ,
.Nm mifare_desfire_key_set_version ,
.Nm mifare_desfire_key_free
.Nd Mifare DESFire keys Manipulation Functions
.\" _ _ _
.\" | | (_) |__ _ __ __ _ _ __ _ _
.\" | | | | '_ \| '__/ _` | '__| | | |
.\" | |___| | |_) | | | (_| | | | |_| |
.\" |_____|_|_.__/|_| \__,_|_| \__, |
.\" |___/
.Sh LIBRARY
Mifare card manipulation library (libfreefare, \-lfreefare)
.\" ____ _
.\" / ___| _ _ _ __ ___ _ __ ___(_)___
.\" \___ \| | | | '_ \ / _ \| '_ \/ __| / __|
.\" ___) | |_| | | | | (_) | |_) \__ \ \__ \
.\" |____/ \__, |_| |_|\___/| .__/|___/_|___/
.\" |___/ |_|
.Sh SYNOPSIS
.In freefare.h
.Ft MifareDESFireKey
.Fn mifare_desfire_des_key_new "uint8_t value[8]"
.Ft MifareDESFireKey
.Fn mifare_desfire_3des_key_new "uint8_t value[16]"
.Ft MifareDESFireKey
.Fn mifare_desfire_des_key_new_with_version "uint8_t value[8]"
.Ft MifareDESFireKey
.Fn mifare_desfire_3des_key_new_with_version "uint8_t value[16]"
.Ft uint8_t
.Fn mifare_desfire_key_get_version "MifareDESFireKey key"
.Ft void
.Fn mifare_desfire_key_set_version "MifareDESFireKey key" "uint8_t version"
.Ft void
.Fn mifare_desfire_key_free "MifareDESFireKey key"
.\" ____ _ _ _
.\" | _ \ ___ ___ ___ _ __(_)_ __ | |_(_) ___ _ __
.\" | | | |/ _ \/ __|/ __| '__| | '_ \| __| |/ _ \| '_ \
.\" | |_| | __/\__ \ (__| | | | |_) | |_| | (_) | | | |
.\" |____/ \___||___/\___|_| |_| .__/ \__|_|\___/|_| |_|
.\" |_|
.Sh DESCRIPTION
The
.Fn mifare_desfire_key_*
family of functions allows management of Mifare DESFire keys.
.Pp
The
.Fn mifare_desfire_des_key_new
and
.Fn mifare_desfire_3des_key_new
alocate a new key with the provided data
.Va value .
The key version is set to
.Va 0 .
.Pp
The
.Fn mifare_desfire_des_key_new_with_version
and
.Fn mifare_desfire_3des_key_new_with_version
functions are equivalent to the
.Fn mifare_desfire_des_key_new
and
.Fn mifare_desfire_3des_key_new
functions except that the key version is set to
.Va version .
.Pp
The version of a
.Vt MifareDESFireKey
can be extracted using
.Fn mifare_desfire_key_get_version
and changed using
.Fn mifare_desfire_key_set_version.
.Pp
The
.Fn mifare_desfire_key_free
has to be called for each
.Va MifareDESFireKey
after usage to reclaim memory.
.\" ____ _ _
.\" | _ \ ___| |_ _ _ _ __ _ __ __ ____ _| |_ _ ___ ___
.\" | |_) / _ \ __| | | | '__| '_ \ \ \ / / _` | | | | |/ _ \/ __|
.\" | _ < __/ |_| |_| | | | | | | \ V / (_| | | |_| | __/\__ \
.\" |_| \_\___|\__|\__,_|_| |_| |_| \_/ \__,_|_|\__,_|\___||___/
.\"
.Sh RETURN VALUES
Key allocations functions return the allocaed key of
.Va NULL
on failure.
.\" ____ _
.\" / ___| ___ ___ __ _| |___ ___
.\" \___ \ / _ \/ _ \ / _` | / __|/ _ \
.\" ___) | __/ __/ | (_| | \__ \ (_) |
.\" |____/ \___|\___| \__,_|_|___/\___/
.\"
.Sh SEE ALSO
.Xr mifare_desfire 3
.\" _ _ _
.\" / \ _ _| |_| |__ ___ _ __ ___
.\" / _ \| | | | __| '_ \ / _ \| '__/ __|
.\" / ___ \ |_| | |_| | | | (_) | | \__ \
.\" /_/ \_\__,_|\__|_| |_|\___/|_| |___/
.\"
.Sh AUTHORS
.An Romain Tartiere Aq romain@il4p.org

View file

@ -0,0 +1,142 @@
/*-
* Copyright (C) 2010, Romain Tartiere.
*
* 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/>
*
* $Id$
*/
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>
#include <freefare.h>
#include "freefare_internal.h"
static inline void update_key_schedules (MifareDESFireKey key);
static inline void
update_key_schedules (MifareDESFireKey key)
{
DES_set_key ((DES_cblock *)key->data, &(key->ks1));
DES_set_key ((DES_cblock *)(key->data + 8), &(key->ks2));
}
MifareDESFireKey
mifare_desfire_des_key_new (uint8_t value[8])
{
uint8_t data[8];
memcpy (data, value, 8);
for (int n=0; n < 8; n++)
data[n] &= 0xfe;
return mifare_desfire_des_key_new_with_version (data);
}
MifareDESFireKey
mifare_desfire_des_key_new_with_version (uint8_t value[8])
{
MifareDESFireKey key;
if ((key = malloc (sizeof (struct mifare_desfire_key)))) {
memcpy (key->data, value, 8);
memcpy (key->data+8, value, 8);
update_key_schedules (key);
key->type = T_DES;
}
return key;
}
MifareDESFireKey
mifare_desfire_3des_key_new (uint8_t value[16])
{
uint8_t data[16];
memcpy (data, value, 16);
for (int n=0; n < 8; n++)
data[n] &= 0xfe;
for (int n=8; n < 16; n++)
data[n] |= 0x01;
return mifare_desfire_3des_key_new_with_version (data);
}
MifareDESFireKey
mifare_desfire_3des_key_new_with_version (uint8_t value[16])
{
MifareDESFireKey key;
if ((key = malloc (sizeof (struct mifare_desfire_key)))) {
memcpy (key->data, value, 16);
update_key_schedules (key);
key->type = T_3DES;
}
return key;
}
uint8_t
mifare_desfire_key_get_version (MifareDESFireKey key)
{
uint8_t version = 0;
for (int n = 0; n < 8; n++) {
version |= ((key->data[n] & 1) << (7 - n));
}
return version;
}
void
mifare_desfire_key_set_version (MifareDESFireKey key, uint8_t version)
{
for (int n = 0; n < 8; n++) {
uint8_t version_bit = ((version & (1 << (7-n))) >> (7-n));
key->data[n] &= 0xfe;
key->data[n] |= version_bit;
if (key->type == T_DES) {
key->data[n+8] = key->data[n];
} else {
// Write ~version to avoid turning a 3DES key into a DES key
key->data[n+8] &= 0xfe;
key->data[n+8] |= ~version_bit;
}
}
}
MifareDESFireKey
mifare_desfire_session_key_new (uint8_t rnda[8], uint8_t rndb[8], MifareDESFireKey authentication_key)
{
MifareDESFireKey key;
uint8_t buffer[16];
memcpy (buffer, rnda, 4);
memcpy (buffer+4, rndb, 4);
memcpy (buffer+8, rnda+4, 4);
memcpy (buffer+12, rndb+4, 4);
switch (authentication_key->type) {
case T_DES:
key = mifare_desfire_des_key_new_with_version (buffer);
break;
case T_3DES:
key = mifare_desfire_3des_key_new_with_version (buffer);
break;
}
return key;
}
void
mifare_desfire_key_free (MifareDESFireKey key)
{
free (key);
}

View file

@ -45,6 +45,7 @@ Mifare card manipulation library (libfreefare, \-lfreefare)
.\" |____/ \__, |_| |_|\___/| .__/|___/_|___/
.\" |___/ |_|
.Sh SYNOPSIS
.In freefare.h
.Ft int
.Fn mifare_ultralight_connect "MifareTag tag"
.Ft int