From 1d3c3f5dfdc5252919e6437746497804ae28f269 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Sat, 18 Dec 2010 01:52:48 +0000 Subject: [PATCH] Improve the way data with already a CRC is preprocessed. --- libfreefare/freefare_internal.h | 2 +- libfreefare/mifare_desfire_authenticate.c | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index b1b2f90..6d70522 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -148,7 +148,7 @@ void desfire_crc32_append (uint8_t *data, const size_t len); size_t key_block_size (const MifareDESFireKey key); size_t padded_data_length (const size_t nbytes, const size_t block_size); size_t maced_data_length (const MifareDESFireKey key, const size_t nbytes); -size_t enciphered_data_length (const MifareDESFireKey key, const size_t nbytes); +size_t enciphered_data_length (const MifareDESFireKey key, const size_t nbytes, int communication_settings); void cmac_generate_subkeys (MifareDESFireKey key); void cmac (const MifareDESFireKey key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac); diff --git a/libfreefare/mifare_desfire_authenticate.c b/libfreefare/mifare_desfire_authenticate.c index 271c1e3..499efe6 100644 --- a/libfreefare/mifare_desfire_authenticate.c +++ b/libfreefare/mifare_desfire_authenticate.c @@ -258,9 +258,10 @@ maced_data_length (const MifareDESFireKey key, const size_t nbytes) * Buffer size required to encipher nbytes of data and a two bytes CRC. */ size_t -enciphered_data_length (const MifareDESFireKey key, const size_t nbytes) +enciphered_data_length (const MifareDESFireKey key, const size_t nbytes, int communication_settings) { - size_t crc_length; + size_t crc_length = 0; + if (!(communication_settings & NO_CRC)) { switch (key->type) { case T_DES: case T_3DES: @@ -270,6 +271,7 @@ enciphered_data_length (const MifareDESFireKey key, const size_t nbytes) crc_length = 4; break; } + } size_t block_size = key_block_size (key); @@ -393,7 +395,7 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o case T_3DES: if (!(communication_settings & ENC_COMMAND)) break; - edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset) + offset; + edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset; if (!(res = assert_crypto_buffer_size (tag, edl))) abort(); @@ -414,11 +416,7 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o break; case T_AES: - if (!(communication_settings & NO_CRC)) { - edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes + 4 - offset) + offset; - } else { - edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset) + offset; - } + edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset; if (!(res = assert_crypto_buffer_size (tag, edl))) abort(); @@ -484,7 +482,7 @@ mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int c if (communication_settings & MAC_VERIFY) { *nbytes -= key_macing_length (key); - edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - 1); + edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - 1, communication_settings); edata = malloc (edl); memcpy (edata, data, *nbytes - 1);