Improve the way data with already a CRC is preprocessed.

This commit is contained in:
Romain Tartiere 2010-12-18 01:52:48 +00:00
parent 79f6cb20e5
commit 1d3c3f5dfd
2 changed files with 8 additions and 10 deletions

View file

@ -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 key_block_size (const MifareDESFireKey key);
size_t padded_data_length (const size_t nbytes, const size_t block_size); 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 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_generate_subkeys (MifareDESFireKey key);
void cmac (const MifareDESFireKey key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac); void cmac (const MifareDESFireKey key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac);

View file

@ -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. * Buffer size required to encipher nbytes of data and a two bytes CRC.
*/ */
size_t 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) { switch (key->type) {
case T_DES: case T_DES:
case T_3DES: case T_3DES:
@ -270,6 +271,7 @@ enciphered_data_length (const MifareDESFireKey key, const size_t nbytes)
crc_length = 4; crc_length = 4;
break; break;
} }
}
size_t block_size = key_block_size (key); 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: case T_3DES:
if (!(communication_settings & ENC_COMMAND)) if (!(communication_settings & ENC_COMMAND))
break; 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))) if (!(res = assert_crypto_buffer_size (tag, edl)))
abort(); abort();
@ -414,11 +416,7 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
break; break;
case T_AES: case T_AES:
if (!(communication_settings & NO_CRC)) { edl = enciphered_data_length (MIFARE_DESFIRE (tag)->session_key, *nbytes - offset, communication_settings) + offset;
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;
}
if (!(res = assert_crypto_buffer_size (tag, edl))) if (!(res = assert_crypto_buffer_size (tag, edl)))
abort(); abort();
@ -484,7 +482,7 @@ mifare_cryto_postprocess_data (MifareTag tag, void *data, ssize_t *nbytes, int c
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); 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);