libfreefare/test/test_mad.c

290 lines
7.6 KiB
C
Raw Normal View History

2010-02-19 11:43:06 +01:00
/*-
* 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
* 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$
*/
2010-01-11 21:14:30 +01:00
#include <cutter.h>
2010-01-11 21:14:30 +01:00
#include <freefare.h>
#include "freefare_internal.h"
2010-01-08 12:24:30 +01:00
2010-01-11 21:14:30 +01:00
void
test_mad (void)
{
int res;
2010-01-11 21:14:30 +01:00
Mad mad = mad_new (1);
cut_assert_not_null (mad, cut_message ("Can create a new MAD"));
cut_assert_equal_int (1, mad_get_version (mad), cut_message ("Wrong default MAD version"));
2010-01-11 21:14:30 +01:00
mad_set_version (mad, 2);
cut_assert_equal_int (2, mad_get_version (mad), cut_message ("Can't change MAD version"));
cut_assert_equal_int (0, mad_get_card_publisher_sector (mad), cut_message ("Wrong default MAD publisher"));
2010-01-11 21:14:30 +01:00
res = mad_set_card_publisher_sector (mad, 13);
cut_assert_equal_int (0, res, cut_message ("mad_set_card_publisher_sector() returned an error."));
cut_assert_equal_int (13, mad_get_card_publisher_sector (mad), cut_message ("Wrong publisher sector"));
2010-01-11 21:14:30 +01:00
res = mad_set_card_publisher_sector (mad, 0xff);
cut_assert_equal_int (-1, res, cut_message ("Invalid sector"));
cut_assert_equal_int (13, mad_get_card_publisher_sector (mad), cut_message ("Previous publisher sector value"));
2010-01-11 21:14:30 +01:00
MadAid aid = {
.function_cluster_code = 0,
.application_code = 0
};
2010-01-11 21:14:30 +01:00
res = mad_get_aid (mad, 3, &aid);
cut_assert_equal_int (0, res, cut_message ("mad_get_aid() failed"));
cut_assert_equal_int (0, aid.function_cluster_code, cut_message ("Invalid default value"));
cut_assert_equal_int (0, aid.application_code, cut_message ("Invalid default value"));
2010-01-11 21:14:30 +01:00
aid.function_cluster_code = 0xc0;
aid.application_code = 0x42;
res = mad_set_aid (mad, 3, aid);
cut_assert_equal_int (0, res, cut_message ("mad_set_aid() failed"));
2010-01-11 21:14:30 +01:00
res = mad_get_aid (mad, 3, &aid);
cut_assert_equal_int (0, res, cut_message ("mad_get_aid() failed"));
cut_assert_equal_int (0xC0, aid.function_cluster_code, cut_message ("Invalid value"));
cut_assert_equal_int (0x42, aid.application_code, cut_message ("Invalid value"));
2010-01-11 21:14:30 +01:00
mad_free (mad);
}
#define CRC_PRESET 0xc7
2010-01-08 12:24:30 +01:00
2010-01-11 21:14:30 +01:00
void
test_mad_crc8_basic (void)
2010-01-08 12:24:30 +01:00
{
2010-01-11 21:14:30 +01:00
uint8_t crc;
const uint8_t crc_value = 0x42;
/* Check integrity */
crc = CRC_PRESET;
nxp_crc(&crc, crc_value);
2010-01-11 21:14:30 +01:00
uint8_t save = crc;
crc = CRC_PRESET;
nxp_crc(&crc, crc_value);
nxp_crc(&crc, save);
cut_assert_equal_int (0x00, crc, cut_message ("CRC should verify crc(message + crc(message)) = 0"));
2010-01-08 12:24:30 +01:00
}
/*
* The following MAD values where extracted from documentation.
*/
2010-01-11 21:14:30 +01:00
void
test_mad_crc8_doc_example (void)
2010-01-08 12:24:30 +01:00
{
2010-01-11 21:14:30 +01:00
/* Preset */
uint8_t crc = CRC_PRESET;
/* Block 1 -- 0x01 - 0x07 */
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x08);
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x08);
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x08);
2010-01-11 21:14:30 +01:00
/* Block 2 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x04);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x00 - 0x07 */
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0x10);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0x10);
nxp_crc(&crc, 0x02);
nxp_crc(&crc, 0x10);
nxp_crc(&crc, 0x02);
nxp_crc(&crc, 0x10);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x11);
nxp_crc(&crc, 0x30);
2010-01-11 21:14:30 +01:00
/* Append zeros of augmented message */
cut_assert_equal_int (0x89, crc, cut_message ("Sample CRC should match"));
2010-01-08 12:24:30 +01:00
}
/*
* The following MAD values where extracted from a MIFARE dump.
*/
2010-01-11 21:14:30 +01:00
void
test_mad_crc8_real_example_1 (void)
2010-01-08 12:24:30 +01:00
{
2010-01-11 21:14:30 +01:00
/* Preset */
uint8_t crc = CRC_PRESET;
/* Block 1 -- 0x01 - 0x07 */
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
2010-01-11 21:14:30 +01:00
/* Block 2 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x00 - 0x07 */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Append zeros of augmented message */
cut_assert_equal_int (0xc4, crc, cut_message ("Read example 1 CRC should match"));
2010-01-08 12:24:30 +01:00
}
/*
* The following MAD values where extracted from a MIFARE dump.
*/
2010-01-11 21:14:30 +01:00
void
test_mad_crc8_real_example_2 (void)
2010-01-08 12:24:30 +01:00
{
2010-01-11 21:14:30 +01:00
/* Preset */
uint8_t crc = CRC_PRESET;
/* Block 1 -- 0x01 - 0x07 */
nxp_crc(&crc, 0x01);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
2010-01-11 21:14:30 +01:00
/* Block 2 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x03);
nxp_crc(&crc, 0xe1);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x00 - 0x07 */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Block 3 -- 0x08 - 0x0f */
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
nxp_crc(&crc, 0x00);
2010-01-11 21:14:30 +01:00
/* Append zeros of augmented message */
cut_assert_equal_int (0xab, crc, cut_message ("Read example 1 CRC should match"));
2010-01-08 12:24:30 +01:00
}
2010-01-11 21:14:30 +01:00
void
test_mad_sector_0x00_crc8 (void)
2010-01-08 12:24:30 +01:00
{
int res;
2010-01-11 21:14:30 +01:00
Mad mad = mad_new (1);
cut_assert_not_null (mad, cut_message ("mad_new() failed"));
2010-01-11 21:14:30 +01:00
res = mad_set_card_publisher_sector (mad, 0x01);
/* Block 1 */
MadAid aid1 = { 0x01, 0x08 };
mad_set_aid (mad, 1, aid1);
mad_set_aid (mad, 2, aid1);
mad_set_aid (mad, 3, aid1);
/* Block 2 */
MadAid empty_aid = { 0x00, 0x00 };
mad_set_aid (mad, 4, empty_aid);
mad_set_aid (mad, 5, empty_aid);
mad_set_aid (mad, 6, empty_aid);
MadAid aid2 = { 0x04, 0x00 };
mad_set_aid (mad, 7, aid2);
/* Block 3 */
MadAid aid3 = { 0x03, 0x10 };
mad_set_aid (mad, 8, aid3);
mad_set_aid (mad, 9, aid3);
MadAid aid4 = { 0x02, 0x10 };
mad_set_aid (mad, 10, aid4);
mad_set_aid (mad, 11, aid4);
mad_set_aid (mad, 12, empty_aid);
mad_set_aid (mad, 13, empty_aid);
mad_set_aid (mad, 14, empty_aid);
MadAid aid5 = { 0x11, 0x30 };
mad_set_aid (mad, 15, aid5);
res = sector_0x00_crc8 (mad);
cut_assert_equal_int(0x89, res, cut_message ("Sample CRC should match"));
2010-01-11 21:14:30 +01:00
mad_free (mad);
}