Move master key detection and authentication in a convenience library.

The autohell hack sucks and should be fixed, but I was unable to find the magic
trick to prevent automake from building broken libtool rules which produce
warnings about portability :-/.
This commit is contained in:
Romain Tartiere 2010-11-21 00:10:04 +00:00
parent 9011bc01e3
commit 02c9d33ce1
4 changed files with 100 additions and 34 deletions

View file

@ -12,6 +12,7 @@ TESTS = run-test.sh
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)"
noinst_LTLIBRARIES = \
mifare_desfire_auto_authenticate.la \
test_mad.la \
test_mifare_application.la \
test_mifare_classic.la \
@ -26,6 +27,9 @@ noinst_LTLIBRARIES = \
AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined
mifare_desfire_auto_authenticate_la_SOURCES = mifare_desfire_auto_authenticate.c \
mifare_desfire_auto_authenticate.h
test_mad_la_SOURCES = test_mad.c
test_mad_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la
@ -47,7 +51,8 @@ test_mifare_classic_create_trailer_block_la_LIBADD = $(top_builddir)/libfreefare
test_mifare_desfire_la_SOURCES = test_mifare_desfire.c \
mifare_desfire_fixture.c \
mifare_desfire_fixture.h
test_mifare_desfire_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la -lutil
test_mifare_desfire_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la \
mifare_desfire_auto_authenticate.lo
test_mifare_desfire_aid_la_SOURCES = test_mifare_desfire_aid.c
test_mifare_desfire_aid_la_LIBADD = $(top_builddir)/libfreefare/libfreefare.la

View file

@ -0,0 +1,61 @@
/*-
* 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 <cutter.h>
#include <freefare.h>
#include "mifare_desfire_auto_authenticate.h"
uint8_t key_data_null[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t key_data_des[8] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
uint8_t key_data_3des[16] = { 'C', 'a', 'r', 'd', ' ', 'M', 'a', 's', 't', 'e', 'r', ' ', 'K', 'e', 'y', '!' };
void
mifare_desfire_auto_authenticate (MifareTag tag, uint8_t key_no)
{
/* Determine which key is currently the master one */
uint8_t key_version;
int res = mifare_desfire_get_key_version (tag, key_no, &key_version);
cut_assert_equal_int (0, res, cut_message ("mifare_desfire_get_key_version()"));
MifareDESFireKey key;
switch (key_version) {
case 0x00:
key = mifare_desfire_des_key_new_with_version (key_data_null);
break;
case 0xAA:
key = mifare_desfire_des_key_new_with_version (key_data_des);
break;
case 0xC7:
key = mifare_desfire_3des_key_new_with_version (key_data_3des);
break;
default:
cut_fail ("Unknown master key.");
}
cut_assert_not_null (key, cut_message ("Cannot allocate key"));
/* Authenticate with this key */
res = mifare_desfire_authenticate (tag, key_no, key);
cut_assert_equal_int (0, res, cut_message ("mifare_desfire_authenticate()"));
mifare_desfire_key_free (key);
}

View file

@ -0,0 +1,29 @@
/*-
* 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$
*/
#ifndef _MIFARE_DESFIRE_AUTO_AUTHENTICATE_H
#define _MIFARE_DESFIRE_AUTO_AUTHENTICATE_H
extern uint8_t key_data_null[8];
extern uint8_t key_data_des[8];
extern uint8_t key_data_3des[16];
void mifare_desfire_auto_authenticate (MifareTag tag, uint8_t key_no);
#endif

View file

@ -26,10 +26,7 @@
#include "freefare_internal.h"
#include "mifare_desfire_fixture.h"
uint8_t key_data_null[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t key_data_des[8] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
uint8_t key_data_3des[16] = { 'C', 'a', 'r', 'd', ' ', 'M', 'a', 's', 't', 'e', 'r', ' ', 'K', 'e', 'y', '!' };
#include "mifare_desfire_auto_authenticate.h"
#define cut_assert_success(last_command) \
do { \
@ -52,34 +49,7 @@ test_mifare_desfire (void)
res = mifare_desfire_get_version (tag, &version_info);
cut_assert_success ("mifare_desfire_get_version()");
/* Determine which key is currently the master one */
uint8_t key_version;
res = mifare_desfire_get_key_version (tag, 0, &key_version);
cut_assert_success ("mifare_desfire_get_key_version()");
MifareDESFireKey key;
switch (key_version) {
case 0x00:
key = mifare_desfire_des_key_new_with_version (key_data_null);
break;
case 0xAA:
key = mifare_desfire_des_key_new_with_version (key_data_des);
break;
case 0xC7:
key = mifare_desfire_3des_key_new_with_version (key_data_3des);
break;
default:
cut_fail ("Unknown master key.");
}
cut_assert_not_null (key, cut_message ("Cannot allocate key"));
/* Authenticate with this key */
res = mifare_desfire_authenticate (tag, 0, key);
cut_assert_success ("mifare_desfire_authenticate()");
mifare_desfire_key_free (key);
mifare_desfire_auto_authenticate (tag, 0);
/*
* This unit test change key settings to more restrictive ones, so reset
@ -89,10 +59,11 @@ test_mifare_desfire (void)
cut_assert_success ("mifare_desfire_change_key_settings()");
/* Change master key to DES */
key = mifare_desfire_des_key_new_with_version (key_data_des);
MifareDESFireKey key = mifare_desfire_des_key_new_with_version (key_data_des);
mifare_desfire_change_key (tag, 0, key, NULL);
cut_assert_success ("mifare_desfire_change_key()");
uint8_t key_version;
res = mifare_desfire_get_key_version (tag, 0, &key_version);
cut_assert_success ("mifare_desfire_get_key_version()");
cut_assert_equal_int (0xAA, key_version, cut_message ("Wrong key_version value."));