2010-07-01 16:10:09 +02:00
|
|
|
#include <cutter.h>
|
|
|
|
|
|
|
|
#include <freefare.h>
|
|
|
|
|
2016-01-20 20:31:17 +01:00
|
|
|
#include "fixture.h"
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
void
|
2017-06-27 13:58:31 +02:00
|
|
|
test_mifare_classic_mad(void)
|
2010-07-01 16:10:09 +02:00
|
|
|
{
|
|
|
|
MifareClassicKey key_a_transport = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
|
|
|
MifareClassicKey key_b_sector_00 = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
|
2017-06-27 13:58:31 +02:00
|
|
|
MifareClassicKey key_b_sector_10 = { 0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a };
|
2010-07-01 16:10:09 +02:00
|
|
|
MifareClassicBlock tb;
|
|
|
|
Mad mad;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* __ __ _ ___ _
|
|
|
|
* | \/ | /_\ | \__ _/ |
|
|
|
|
* | |\/| |/ _ \| |) \ V / |
|
|
|
|
* |_| |_/_/ \_\___/ \_/|_|
|
|
|
|
*/
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mad = mad_new(1);
|
|
|
|
cut_assert_not_null(mad, cut_message("mad_new() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Prepare sector 0x00 for writing a MAD.
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x00, key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_classic_trailer_block(&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00);
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_write(tag, 0x03, tb);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_write() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Write the empty MAD
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mad_write(tag, mad, key_b_sector_00, NULL);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_write() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Check the empty MAD
|
2010-09-03 20:01:02 +02:00
|
|
|
MifareClassicBlock ref_01 = {
|
|
|
|
0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
|
|
|
MifareClassicBlock ref_02 = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
2010-07-01 16:10:09 +02:00
|
|
|
MifareClassicBlock data;
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x01, mad_public_key_a, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x01, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_01, sizeof(ref_01), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x02, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_02, sizeof(ref_02), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
Mad mad2 = mad_read(tag);
|
|
|
|
cut_assert_not_null(mad2, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(mad, sizeof(mad), mad2, sizeof(mad2), cut_message("Wrong MAD"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
const char application_data[] = "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
|
2017-06-27 13:58:31 +02:00
|
|
|
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
|
|
|
|
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
|
|
|
|
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
|
|
|
|
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \
|
|
|
|
"APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> ";
|
2010-07-03 12:04:06 +02:00
|
|
|
|
|
|
|
MadAid aid = {
|
|
|
|
.function_cluster_code = 0x01,
|
|
|
|
.application_code = 0x12
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write some data in the application
|
2017-06-27 13:58:31 +02:00
|
|
|
MifareClassicSectorNumber *sectors = mifare_application_alloc(mad, aid, sizeof(application_data));
|
|
|
|
cut_assert_not_null(sectors, cut_message("mifare_application_alloc() failed"));
|
|
|
|
free(sectors);
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mad_write(tag, mad, key_b_sector_00, NULL);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_write() failed"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
ssize_t s = mifare_application_write(tag, mad, aid, &application_data, sizeof(application_data), key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(sizeof(application_data), s, cut_message("mifare_application_write() failed"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
|
|
|
char read_buf[500];
|
|
|
|
|
|
|
|
// Read it again
|
2017-06-27 13:58:31 +02:00
|
|
|
s = mifare_application_read(tag, mad, aid, read_buf, sizeof(application_data), key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(sizeof(application_data), s, cut_message("mifare_application_read() failed"));
|
|
|
|
cut_assert_equal_memory(application_data, sizeof(application_data), read_buf, s, cut_message("Wrong application data"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mad_free(mad);
|
|
|
|
mad_free(mad2);
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Revert to the transport configuration
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x00, key_b_sector_00, MFC_KEY_B);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
|
|
|
res = mifare_classic_format_sector(tag, 0x00);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_format_sector() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
/* __ __ _ ___ ___
|
|
|
|
* | \/ | /_\ | \__ _|_ )
|
|
|
|
* | |\/| |/ _ \| |) \ V // /
|
|
|
|
* |_| |_/_/ \_\___/ \_//___|
|
|
|
|
*/
|
2017-06-27 13:58:31 +02:00
|
|
|
if (freefare_get_tag_type(tag) != MIFARE_CLASSIC_4K) {
|
|
|
|
cut_omit("MADv2 requires a MIFARE Classic 4K to be tested");
|
2010-07-01 16:10:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mad = mad_new(2);
|
|
|
|
cut_assert_not_null(mad, cut_message("mad_new() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Prepare sector 0x00 for writing a MAD.
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x00, key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_classic_trailer_block(&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00);
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_write(tag, 0x03, tb);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_write() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Prepare sector 0x10 for writing a MAD.
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x40, key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_classic_trailer_block(&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_10);
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_write(tag, 0x43, tb);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_write() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Write the empty MAD
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mad_write(tag, mad, key_b_sector_00, key_b_sector_10);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_write() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Check the empty MAD
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x01, mad_public_key_a, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x01, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_01, sizeof(ref_01), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x02, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_02, sizeof(ref_02), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2010-09-03 20:01:02 +02:00
|
|
|
MifareClassicBlock ref_40 = {
|
|
|
|
0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
|
|
|
MifareClassicBlock ref_41 = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
|
|
|
MifareClassicBlock ref_42 = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x40, mad_public_key_a, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x40, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_40, sizeof(ref_01), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x41, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_41, sizeof(ref_02), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_read(tag, 0x42, &data);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(ref_42, sizeof(ref_02), data, sizeof(data), cut_message("Wrong data"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mad2 = mad_read(tag);
|
|
|
|
cut_assert_not_null(mad2, cut_message("mad_read() failed"));
|
|
|
|
cut_assert_equal_memory(mad, sizeof(mad), mad2, sizeof(mad2), cut_message("Wrong MAD"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
2010-07-03 12:04:06 +02:00
|
|
|
// Write some data in the application
|
2017-06-27 13:58:31 +02:00
|
|
|
sectors = mifare_application_alloc(mad, aid, sizeof(application_data));
|
|
|
|
cut_assert_not_null(sectors, cut_message("mifare_application_alloc() failed"));
|
|
|
|
free(sectors);
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mad_write(tag, mad, key_b_sector_00, key_b_sector_10);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mad_write() failed"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
s = mifare_application_write(tag, mad, aid, &application_data, sizeof(application_data), key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(sizeof(application_data), s, cut_message("mifare_application_write() failed"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
|
|
|
// Read it again
|
2017-06-27 13:58:31 +02:00
|
|
|
s = mifare_application_read(tag, mad, aid, read_buf, sizeof(application_data), key_a_transport, MFC_KEY_A);
|
|
|
|
cut_assert_equal_int(sizeof(application_data), s, cut_message("mifare_application_read() failed"));
|
|
|
|
cut_assert_equal_memory(application_data, sizeof(application_data), read_buf, s, cut_message("Wrong application data"));
|
2010-07-03 12:04:06 +02:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mad_free(mad);
|
|
|
|
mad_free(mad2);
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
// Revert to the transport configuration
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_classic_authenticate(tag, 0x00, key_b_sector_00, MFC_KEY_B);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
|
|
|
res = mifare_classic_format_sector(tag, 0x00);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_format_sector() failed"));
|
|
|
|
|
|
|
|
res = mifare_classic_authenticate(tag, 0x40, key_b_sector_10, MFC_KEY_B);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_authenticate() failed"));
|
|
|
|
res = mifare_classic_format_sector(tag, 0x10);
|
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_classic_format_sector() failed"));
|
2010-07-01 16:10:09 +02:00
|
|
|
|
|
|
|
}
|