2010-11-21 01:10:04 +01:00
|
|
|
#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', '!' };
|
2010-12-15 13:43:31 +01:00
|
|
|
uint8_t key_data_aes[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
2010-12-18 03:28:27 +01:00
|
|
|
uint8_t key_data_3k3des[24] = { 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2017-06-27 13:58:31 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
2010-12-15 13:43:31 +01:00
|
|
|
|
|
|
|
const uint8_t key_data_aes_version = 0x42;
|
2010-11-21 01:10:04 +01:00
|
|
|
|
|
|
|
void
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_desfire_auto_authenticate(FreefareTag tag, uint8_t key_no)
|
2010-11-21 01:10:04 +01:00
|
|
|
{
|
|
|
|
/* Determine which key is currently the master one */
|
|
|
|
uint8_t key_version;
|
2017-06-27 13:58:31 +02:00
|
|
|
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()"));
|
2010-11-21 01:10:04 +01:00
|
|
|
|
|
|
|
MifareDESFireKey key;
|
|
|
|
|
|
|
|
switch (key_version) {
|
|
|
|
case 0x00:
|
2017-06-27 13:58:31 +02:00
|
|
|
key = mifare_desfire_des_key_new_with_version(key_data_null);
|
2010-11-21 01:10:04 +01:00
|
|
|
break;
|
2010-12-15 13:43:31 +01:00
|
|
|
case 0x42:
|
2017-06-27 13:58:31 +02:00
|
|
|
key = mifare_desfire_aes_key_new_with_version(key_data_aes, key_data_aes_version);
|
2010-12-15 13:43:31 +01:00
|
|
|
break;
|
2010-11-21 01:10:04 +01:00
|
|
|
case 0xAA:
|
2017-06-27 13:58:31 +02:00
|
|
|
key = mifare_desfire_des_key_new_with_version(key_data_des);
|
2010-11-21 01:10:04 +01:00
|
|
|
break;
|
|
|
|
case 0xC7:
|
2017-06-27 13:58:31 +02:00
|
|
|
key = mifare_desfire_3des_key_new_with_version(key_data_3des);
|
2010-11-21 01:10:04 +01:00
|
|
|
break;
|
2010-12-18 03:28:27 +01:00
|
|
|
case 0x55:
|
2017-06-27 13:58:31 +02:00
|
|
|
key = mifare_desfire_3k3des_key_new_with_version(key_data_3k3des);
|
2010-12-18 03:28:27 +01:00
|
|
|
break;
|
2010-11-21 01:10:04 +01:00
|
|
|
default:
|
2017-06-27 13:58:31 +02:00
|
|
|
cut_fail("Unknown master key.");
|
2010-11-21 01:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
cut_assert_not_null(key, cut_message("Cannot allocate key"));
|
2010-11-21 01:10:04 +01:00
|
|
|
|
|
|
|
/* Authenticate with this key */
|
2010-12-15 13:43:31 +01:00
|
|
|
switch (key_version) {
|
|
|
|
case 0x00:
|
|
|
|
case 0xAA:
|
|
|
|
case 0xC7:
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate(tag, key_no, key);
|
2010-12-15 13:43:31 +01:00
|
|
|
break;
|
2010-12-18 03:28:27 +01:00
|
|
|
case 0x55:
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate_iso(tag, key_no, key);
|
2010-12-18 03:28:27 +01:00
|
|
|
break;
|
2010-12-15 13:43:31 +01:00
|
|
|
case 0x42:
|
2017-06-27 13:58:31 +02:00
|
|
|
res = mifare_desfire_authenticate_aes(tag, key_no, key);
|
2010-12-18 03:28:27 +01:00
|
|
|
break;
|
2010-12-15 13:43:31 +01:00
|
|
|
}
|
2017-06-27 13:58:31 +02:00
|
|
|
cut_assert_equal_int(0, res, cut_message("mifare_desfire_authenticate()"));
|
2010-11-21 01:10:04 +01:00
|
|
|
|
2017-06-27 13:58:31 +02:00
|
|
|
mifare_desfire_key_free(key);
|
2010-11-21 01:10:04 +01:00
|
|
|
}
|