Reindent.
This commit is contained in:
parent
b300ef5fcf
commit
dd5156a997
4 changed files with 124 additions and 124 deletions
|
@ -54,7 +54,7 @@ freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai)
|
||||||
if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) &&
|
if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) &&
|
||||||
(nai.btSak == supported_tags[i].SAK) &&
|
(nai.btSak == supported_tags[i].SAK) &&
|
||||||
(!supported_tags[i].ATS_length || ((nai.szAtsLen == supported_tags[i].ATS_length) &&
|
(!supported_tags[i].ATS_length || ((nai.szAtsLen == supported_tags[i].ATS_length) &&
|
||||||
(0 == memcmp (nai.abtAts, supported_tags[i].ATS, ATS_LENGTH))))) {
|
(0 == memcmp (nai.abtAts, supported_tags[i].ATS, ATS_LENGTH))))) {
|
||||||
|
|
||||||
tag_info = &(supported_tags[i]);
|
tag_info = &(supported_tags[i]);
|
||||||
found = true;
|
found = true;
|
||||||
|
|
|
@ -365,7 +365,7 @@ mad_set_version (Mad mad, const uint8_t version)
|
||||||
{
|
{
|
||||||
if ((version == 2) && (mad->version == 1)) {
|
if ((version == 2) && (mad->version == 1)) {
|
||||||
/* We use a larger MAD so initialise the new blocks */
|
/* We use a larger MAD so initialise the new blocks */
|
||||||
memset (&(mad->sector_0x10), 0, sizeof (mad->sector_0x10));
|
memset (&(mad->sector_0x10), 0, sizeof (mad->sector_0x10));
|
||||||
}
|
}
|
||||||
mad->version = version;
|
mad->version = version;
|
||||||
}
|
}
|
||||||
|
|
|
@ -510,14 +510,14 @@ mifare_desfire_change_key (MifareTag tag, uint8_t key_no, MifareDESFireKey new_k
|
||||||
|
|
||||||
int new_key_length;
|
int new_key_length;
|
||||||
switch (new_key->type) {
|
switch (new_key->type) {
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
case T_AES:
|
case T_AES:
|
||||||
new_key_length = 16;
|
new_key_length = 16;
|
||||||
break;
|
break;
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
new_key_length = 24;
|
new_key_length = 24;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy (cmd + __cmd_n, new_key->data, new_key_length);
|
memcpy (cmd + __cmd_n, new_key->data, new_key_length);
|
||||||
|
@ -921,7 +921,7 @@ mifare_desfire_get_card_uid (MifareTag tag, char **uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf (*uid, "%02x%02x%02x%02x%02x%02x%02x",
|
sprintf (*uid, "%02x%02x%02x%02x%02x%02x%02x",
|
||||||
p[0], p[1], p[2], p[3],
|
p[0], p[1], p[2], p[3],
|
||||||
p[4], p[5], p[6]);
|
p[4], p[5], p[6]);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -977,7 +977,7 @@ mifare_desfire_get_file_settings (MifareTag tag, uint8_t file_no, struct mifare_
|
||||||
|
|
||||||
BUFFER_APPEND (cmd, 0xF5);
|
BUFFER_APPEND (cmd, 0xF5);
|
||||||
BUFFER_APPEND (cmd, file_no);
|
BUFFER_APPEND (cmd, file_no);
|
||||||
|
|
||||||
uint8_t *p = mifare_cryto_preprocess_data (tag, cmd, &__cmd_n, 0, MDCM_PLAIN | CMAC_COMMAND);
|
uint8_t *p = mifare_cryto_preprocess_data (tag, cmd, &__cmd_n, 0, MDCM_PLAIN | CMAC_COMMAND);
|
||||||
|
|
||||||
DESFIRE_TRANSCEIVE2 (tag, p, __cmd_n, res);
|
DESFIRE_TRANSCEIVE2 (tag, p, __cmd_n, res);
|
||||||
|
|
|
@ -264,16 +264,16 @@ enciphered_data_length (const MifareDESFireKey key, const size_t nbytes, int com
|
||||||
{
|
{
|
||||||
size_t crc_length = 0;
|
size_t crc_length = 0;
|
||||||
if (!(communication_settings & NO_CRC)) {
|
if (!(communication_settings & NO_CRC)) {
|
||||||
switch (key->type) {
|
switch (key->type) {
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
crc_length = 2;
|
crc_length = 2;
|
||||||
break;
|
break;
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
case T_AES:
|
case T_AES:
|
||||||
crc_length = 4;
|
crc_length = 4;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t block_size = key_block_size (key);
|
size_t block_size = key_block_size (key);
|
||||||
|
@ -336,39 +336,39 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* pass through */
|
/* pass through */
|
||||||
edl = padded_data_length (*nbytes - offset, key_block_size (MIFARE_DESFIRE (tag)->session_key)) + offset;
|
edl = padded_data_length (*nbytes - offset, key_block_size (MIFARE_DESFIRE (tag)->session_key)) + offset;
|
||||||
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
// Fill in the crypto buffer with data ...
|
// Fill in the crypto buffer with data ...
|
||||||
memcpy (res, data, *nbytes);
|
memcpy (res, data, *nbytes);
|
||||||
// ... and 0 padding
|
// ... and 0 padding
|
||||||
memset ((uint8_t *)res + *nbytes, 0, edl - *nbytes);
|
memset ((uint8_t *)res + *nbytes, 0, edl - *nbytes);
|
||||||
|
|
||||||
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *) res + offset, edl - offset, MCD_SEND, MCO_ENCYPHER);
|
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *) res + offset, edl - offset, MCD_SEND, MCO_ENCYPHER);
|
||||||
|
|
||||||
memcpy (mac, (uint8_t *)res + edl - 8, 4);
|
memcpy (mac, (uint8_t *)res + edl - 8, 4);
|
||||||
|
|
||||||
// Copy again provided data (was overwritten by mifare_cbc_des)
|
// Copy again provided data (was overwritten by mifare_cbc_des)
|
||||||
memcpy (res, data, *nbytes);
|
memcpy (res, data, *nbytes);
|
||||||
|
|
||||||
if (!(communication_settings & MAC_COMMAND))
|
if (!(communication_settings & MAC_COMMAND))
|
||||||
break;
|
break;
|
||||||
// Append MAC
|
// Append MAC
|
||||||
mdl = maced_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset) + offset;
|
mdl = maced_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset) + offset;
|
||||||
if (!(res = assert_crypto_buffer_size (tag, mdl)))
|
if (!(res = assert_crypto_buffer_size (tag, mdl)))
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
memcpy ((uint8_t *)res + *nbytes, mac, 4);
|
memcpy ((uint8_t *)res + *nbytes, mac, 4);
|
||||||
|
|
||||||
*nbytes += 4;
|
*nbytes += 4;
|
||||||
break;
|
break;
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
case T_AES:
|
case T_AES:
|
||||||
if (!(communication_settings & CMAC_COMMAND))
|
if (!(communication_settings & CMAC_COMMAND))
|
||||||
break;
|
break;
|
||||||
cmac (key, MIFARE_DESFIRE (tag)->ivect, res, *nbytes, MIFARE_DESFIRE (tag)->cmac);
|
cmac (key, MIFARE_DESFIRE (tag)->ivect, res, *nbytes, MIFARE_DESFIRE (tag)->cmac);
|
||||||
|
|
||||||
if (append_mac) {
|
if (append_mac) {
|
||||||
mdl = maced_data_length (key, *nbytes);
|
mdl = maced_data_length (key, *nbytes);
|
||||||
if (!(res = assert_crypto_buffer_size (tag, mdl)))
|
if (!(res = assert_crypto_buffer_size (tag, mdl)))
|
||||||
|
@ -404,18 +404,18 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
if (!(communication_settings & ENC_COMMAND))
|
if (!(communication_settings & ENC_COMMAND))
|
||||||
break;
|
break;
|
||||||
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset;
|
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset;
|
||||||
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
// Fill in the crypto buffer with data ...
|
// Fill in the crypto buffer with data ...
|
||||||
memcpy (res, data, *nbytes);
|
memcpy (res, data, *nbytes);
|
||||||
if (!(communication_settings & NO_CRC)) {
|
if (!(communication_settings & NO_CRC)) {
|
||||||
// ... CRC ...
|
// ... CRC ...
|
||||||
switch (key->type) {
|
switch (key->type) {
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
iso14443a_crc_append ((uint8_t *)res + offset, *nbytes - offset);
|
iso14443a_crc_append ((uint8_t *)res + offset, *nbytes - offset);
|
||||||
*nbytes += 2;
|
*nbytes += 2;
|
||||||
break;
|
break;
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
|
@ -426,32 +426,32 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
|
||||||
// Never reached.
|
// Never reached.
|
||||||
abort ();
|
abort ();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
// ... and 0 padding
|
||||||
// ... and 0 padding
|
memset ((uint8_t *)(res) + *nbytes, 0, edl - *nbytes);
|
||||||
memset ((uint8_t *)(res) + *nbytes, 0, edl - *nbytes);
|
|
||||||
|
|
||||||
*nbytes = edl;
|
*nbytes = edl;
|
||||||
|
|
||||||
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *) res + offset, *nbytes - offset, MCD_SEND, (key->type == T_3K3DES) ? MCO_ENCYPHER : MCO_DECYPHER);
|
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *) res + offset, *nbytes - offset, MCD_SEND, (key->type == T_3K3DES) ? MCO_ENCYPHER : MCO_DECYPHER);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case T_AES:
|
case T_AES:
|
||||||
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset;
|
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset;
|
||||||
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
if (!(res = assert_crypto_buffer_size (tag, edl)))
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
// Fill in the crypto buffer with data ...
|
// Fill in the crypto buffer with data ...
|
||||||
memcpy (res, data, *nbytes);
|
memcpy (res, data, *nbytes);
|
||||||
size_t pdl;
|
size_t pdl;
|
||||||
if (!(communication_settings & NO_CRC)) {
|
if (!(communication_settings & NO_CRC)) {
|
||||||
desfire_crc32_append (res, *nbytes);
|
desfire_crc32_append (res, *nbytes);
|
||||||
pdl = padded_data_length (*nbytes - offset + 4, key_block_size (MIFARE_DESFIRE (tag)->session_key));
|
pdl = padded_data_length (*nbytes - offset + 4, key_block_size (MIFARE_DESFIRE (tag)->session_key));
|
||||||
bzero ((uint8_t *)res + *nbytes + 4, (offset + pdl) - (*nbytes + 4));
|
bzero ((uint8_t *)res + *nbytes + 4, (offset + pdl) - (*nbytes + 4));
|
||||||
} else {
|
} else {
|
||||||
pdl = padded_data_length (*nbytes - offset, key_block_size (MIFARE_DESFIRE (tag)->session_key));
|
pdl = padded_data_length (*nbytes - offset, key_block_size (MIFARE_DESFIRE (tag)->session_key));
|
||||||
bzero ((uint8_t *)res + *nbytes, (offset + pdl) - (*nbytes));
|
bzero ((uint8_t *)res + *nbytes, (offset + pdl) - (*nbytes));
|
||||||
}
|
}
|
||||||
mifare_cbc_des (key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *)res + offset, pdl, MCD_SEND, MCO_ENCYPHER);
|
mifare_cbc_des (key, MIFARE_DESFIRE (tag)->ivect, (uint8_t *)res + offset, pdl, MCD_SEND, MCO_ENCYPHER);
|
||||||
*nbytes = offset + pdl;
|
*nbytes = offset + pdl;
|
||||||
|
|
||||||
|
@ -501,26 +501,26 @@ mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int c
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
if (communication_settings & MAC_VERIFY) {
|
if (communication_settings & MAC_VERIFY) {
|
||||||
*nbytes -= key_macing_length (key);
|
*nbytes -= key_macing_length (key);
|
||||||
|
|
||||||
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - 1, communication_settings);
|
edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - 1, communication_settings);
|
||||||
edata = malloc (edl);
|
edata = malloc (edl);
|
||||||
|
|
||||||
memcpy (edata, data, *nbytes - 1);
|
memcpy (edata, data, *nbytes - 1);
|
||||||
memset ((uint8_t *)edata + *nbytes - 1, 0, edl - *nbytes + 1);
|
memset ((uint8_t *)edata + *nbytes - 1, 0, edl - *nbytes + 1);
|
||||||
|
|
||||||
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, edata, edl, MCD_SEND, MCO_ENCYPHER);
|
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, edata, edl, MCD_SEND, MCO_ENCYPHER);
|
||||||
|
|
||||||
if (0 != memcmp ((uint8_t *)data + *nbytes - 1, (uint8_t *)edata + edl - 8, 4)) {
|
if (0 != memcmp ((uint8_t *)data + *nbytes - 1, (uint8_t *)edata + edl - 8, 4)) {
|
||||||
warnx ("MACing not verified");
|
warnx ("MACing not verified");
|
||||||
#if WITH_DEBUG
|
#if WITH_DEBUG
|
||||||
hexdump ((uint8_t *)data + *nbytes - 1, key_macing_length (key), "Expect ", 0);
|
hexdump ((uint8_t *)data + *nbytes - 1, key_macing_length (key), "Expect ", 0);
|
||||||
hexdump ((uint8_t *)edata + edl - 8, key_macing_length (key), "Actual ", 0);
|
hexdump ((uint8_t *)edata + edl - 8, key_macing_length (key), "Actual ", 0);
|
||||||
abort ();
|
abort ();
|
||||||
#endif
|
#endif
|
||||||
*nbytes = -1;
|
*nbytes = -1;
|
||||||
res = NULL;
|
res = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
|
@ -566,44 +566,44 @@ mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int c
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
(*nbytes)--;
|
(*nbytes)--;
|
||||||
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, res, *nbytes, MCD_RECEIVE, MCO_DECYPHER);
|
mifare_cbc_des (MIFARE_DESFIRE (tag)->session_key, MIFARE_DESFIRE (tag)->ivect, res, *nbytes, MCD_RECEIVE, MCO_DECYPHER);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Look for the CRC and ensure it is followed by NULL padding. We
|
* Look for the CRC and ensure it is followed by NULL padding. We
|
||||||
* can't start by the end because the CRC is supposed to be 0 when
|
* 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.
|
* verified, and accumulating 0's in it should not change it.
|
||||||
*/
|
*/
|
||||||
bool verified = false;
|
bool verified = false;
|
||||||
int end_crc_pos = *nbytes - 7; // The CRC can be over two blocks
|
int end_crc_pos = *nbytes - 7; // The CRC can be over two blocks
|
||||||
|
|
||||||
do {
|
do {
|
||||||
uint16_t crc;
|
uint16_t crc;
|
||||||
iso14443a_crc (res, end_crc_pos, (uint8_t *)&crc);
|
iso14443a_crc (res, end_crc_pos, (uint8_t *)&crc);
|
||||||
if (!crc) {
|
if (!crc) {
|
||||||
verified = true;
|
verified = true;
|
||||||
for (int n = end_crc_pos; n < *nbytes - 1; n++) {
|
for (int n = end_crc_pos; n < *nbytes - 1; n++) {
|
||||||
uint8_t byte = ((uint8_t *)res)[n];
|
uint8_t byte = ((uint8_t *)res)[n];
|
||||||
if (!( (0x00 == byte) || ((0x80 == byte) && (n == end_crc_pos)) ))
|
if (!( (0x00 == byte) || ((0x80 == byte) && (n == end_crc_pos)) ))
|
||||||
verified = false;
|
verified = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (verified) {
|
||||||
if (verified) {
|
*nbytes = end_crc_pos - 2;
|
||||||
*nbytes = end_crc_pos - 2;
|
((uint8_t *)data)[(*nbytes)++] = 0x00;
|
||||||
((uint8_t *)data)[(*nbytes)++] = 0x00;
|
} else {
|
||||||
} else {
|
end_crc_pos++;
|
||||||
end_crc_pos++;
|
}
|
||||||
}
|
} while (!verified && (end_crc_pos < *nbytes - 1));
|
||||||
} while (!verified && (end_crc_pos < *nbytes - 1));
|
|
||||||
|
|
||||||
if (!verified) {
|
if (!verified) {
|
||||||
warnx ("(3)DES not verified");
|
warnx ("(3)DES not verified");
|
||||||
#if WITH_DEBUG
|
#if WITH_DEBUG
|
||||||
abort ();
|
abort ();
|
||||||
#endif
|
#endif
|
||||||
*nbytes = -1;
|
*nbytes = -1;
|
||||||
res = NULL;
|
res = NULL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
case T_AES:
|
case T_AES:
|
||||||
|
@ -733,16 +733,16 @@ mifare_cbc_des (MifareDESFireKey key, uint8_t *ivect, uint8_t *data, size_t data
|
||||||
size_t block_size;
|
size_t block_size;
|
||||||
|
|
||||||
switch (key->type) {
|
switch (key->type) {
|
||||||
case T_DES:
|
case T_DES:
|
||||||
case T_3DES:
|
case T_3DES:
|
||||||
memset (ivect, 0, MAX_CRYPTO_BLOCK_SIZE);
|
memset (ivect, 0, MAX_CRYPTO_BLOCK_SIZE);
|
||||||
/* pass-through */
|
/* pass-through */
|
||||||
case T_3K3DES:
|
case T_3K3DES:
|
||||||
block_size = 8;
|
block_size = 8;
|
||||||
break;
|
break;
|
||||||
case T_AES:
|
case T_AES:
|
||||||
block_size = 16;
|
block_size = 16;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue