Simplify redundant code.

This commit is contained in:
Romain Tartiere 2010-12-24 22:08:15 +00:00
parent 9d8c991f50
commit 58f64b881e

View file

@ -299,7 +299,7 @@ assert_crypto_buffer_size (MifareTag tag, size_t nbytes)
void * void *
mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t offset, int communication_settings) mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t offset, int communication_settings)
{ {
void *res = data; uint8_t *res = data;
uint8_t mac[4]; uint8_t mac[4];
size_t edl, mdl; size_t edl, mdl;
bool append_mac = true; bool append_mac = true;
@ -340,11 +340,11 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
// 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 (res + *nbytes, 0, edl - *nbytes);
mifare_cypher_blocks_chained (tag, NULL, NULL, (uint8_t *) res + offset, edl - offset, MCD_SEND, MCO_ENCYPHER); mifare_cypher_blocks_chained (tag, NULL, NULL, res + offset, edl - offset, MCD_SEND, MCO_ENCYPHER);
memcpy (mac, (uint8_t *)res + edl - 8, 4); memcpy (mac, res + edl - 8, 4);
// Copy again provided data (was overwritten by mifare_cypher_blocks_chained) // Copy again provided data (was overwritten by mifare_cypher_blocks_chained)
memcpy (res, data, *nbytes); memcpy (res, data, *nbytes);
@ -356,7 +356,7 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
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 (res + *nbytes, mac, 4);
*nbytes += 4; *nbytes += 4;
break; break;
@ -371,7 +371,7 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
abort(); abort();
memcpy (res, data, *nbytes); memcpy (res, data, *nbytes);
memcpy ((uint8_t *) res + *nbytes, MIFARE_DESFIRE (tag)->cmac, CMAC_LENGTH); memcpy (res + *nbytes, MIFARE_DESFIRE (tag)->cmac, CMAC_LENGTH);
*nbytes += CMAC_LENGTH; *nbytes += CMAC_LENGTH;
} }
break; break;
@ -394,10 +394,6 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
* encypher()/decypher() * encypher()/decypher()
*/ */
switch (key->type) {
case T_DES:
case T_3DES:
case T_3K3DES:
if (!(communication_settings & ENC_COMMAND)) if (!(communication_settings & ENC_COMMAND))
break; break;
edl = enciphered_data_length (tag, *nbytes - offset, communication_settings) + offset; edl = enciphered_data_length (tag, *nbytes - offset, communication_settings) + offset;
@ -410,45 +406,21 @@ mifare_cryto_preprocess_data (MifareTag tag, void *data, size_t *nbytes, off_t o
// ... CRC ... // ... CRC ...
switch (MIFARE_DESFIRE (tag)->authentication_scheme) { switch (MIFARE_DESFIRE (tag)->authentication_scheme) {
case AS_LEGACY: case AS_LEGACY:
iso14443a_crc_append ((uint8_t *)res + offset, *nbytes - offset); iso14443a_crc_append (res + offset, *nbytes - offset);
*nbytes += 2; *nbytes += 2;
break; break;
case AS_NEW: case AS_NEW:
desfire_crc32_append ((uint8_t *)res, *nbytes); desfire_crc32_append (res, *nbytes);
*nbytes += 4; *nbytes += 4;
break; break;
} }
} }
// ... and 0 padding // ... and padding
memset ((uint8_t *)(res) + *nbytes, 0, edl - *nbytes); memset (res + *nbytes, 0, edl - *nbytes);
*nbytes = edl; *nbytes = edl;
mifare_cypher_blocks_chained (tag, NULL, NULL, (uint8_t *) res + offset, *nbytes - offset, MCD_SEND, (AS_NEW == MIFARE_DESFIRE (tag)->authentication_scheme) ? MCO_ENCYPHER : MCO_DECYPHER); mifare_cypher_blocks_chained (tag, NULL, NULL, res + offset, *nbytes - offset, MCD_SEND, (AS_NEW == MIFARE_DESFIRE (tag)->authentication_scheme) ? MCO_ENCYPHER : MCO_DECYPHER);
break;
case T_AES:
edl = enciphered_data_length (tag, *nbytes - offset, communication_settings) + offset;
if (!(res = assert_crypto_buffer_size (tag, edl)))
abort();
// Fill in the crypto buffer with data ...
memcpy (res, data, *nbytes);
size_t pdl;
if (!(communication_settings & NO_CRC)) {
desfire_crc32_append (res, *nbytes);
pdl = padded_data_length (*nbytes - offset + 4, key_block_size (MIFARE_DESFIRE (tag)->session_key));
memset ((uint8_t *)res + *nbytes + 4, 0, (offset + pdl) - (*nbytes + 4));
} else {
pdl = padded_data_length (*nbytes - offset, key_block_size (MIFARE_DESFIRE (tag)->session_key));
memset ((uint8_t *)res + *nbytes, 0, (offset + pdl) - (*nbytes));
}
mifare_cypher_blocks_chained (tag, NULL, NULL, (uint8_t *)res + offset, pdl, MCD_SEND, MCO_ENCYPHER);
*nbytes = offset + pdl;
break;
}
break; break;
default: default:
warnx ("Unknown communication settings"); warnx ("Unknown communication settings");