diff --git a/client/src/mifare/desfire_crypto.c b/client/src/mifare/desfire_crypto.c index fd3cb427d..b0bbc62b7 100644 --- a/client/src/mifare/desfire_crypto.c +++ b/client/src/mifare/desfire_crypto.c @@ -53,20 +53,6 @@ static inline void update_key_schedules(desfirekey_t key) { // } } -int desfire_get_key_length(enum DESFIRE_CRYPTOALGO key_type) { - switch (key_type) { - case T_DES: - return 8; - case T_3DES: - return 16; - case T_3K3DES: - return 24; - case T_AES: - return 16; - } - return 0; -} - /******************************************************************************/ void tdes_nxp_receive(const void *in, void *out, size_t length, const void *key, unsigned char iv[8], int keymode) { @@ -382,21 +368,6 @@ void mifare_kdf_an10922(const desfirekey_t key, const uint8_t *data, size_t len) free(buffer); } -size_t desfire_get_key_block_length(enum DESFIRE_CRYPTOALGO key_type) { - size_t block_size = 8; - switch (key_type) { - case T_DES: - case T_3DES: - case T_3K3DES: - block_size = 8; - break; - case T_AES: - block_size = 16; - break; - } - return block_size; -} - size_t key_block_size(const desfirekey_t key) { if (key == NULL) { return 0; @@ -422,16 +393,6 @@ static size_t key_macing_length(const desfirekey_t key) { return mac_length; } -/* - * Size required to store nbytes of data in a buffer of size n*block_size. - */ -size_t padded_data_length(const size_t nbytes, const size_t block_size) { - if ((!nbytes) || (nbytes % block_size)) - return ((nbytes / block_size) + 1) * block_size; - else - return nbytes; -} - /* * Buffer size required to MAC nbytes of data */ diff --git a/client/src/mifare/desfire_crypto.h b/client/src/mifare/desfire_crypto.h index c4c879ef3..160af81bd 100644 --- a/client/src/mifare/desfire_crypto.h +++ b/client/src/mifare/desfire_crypto.h @@ -74,16 +74,11 @@ typedef enum { /* Error code managed by the library */ #define CRYPTO_ERROR 0x01 -int desfire_get_key_length(enum DESFIRE_CRYPTOALGO key_type); -size_t desfire_get_key_block_length(enum DESFIRE_CRYPTOALGO key_type); - enum DESFIRE_AUTH_SCHEME { AS_LEGACY, AS_NEW }; - - #define DESFIRE_KEY(key) ((struct desfire_key *) key) struct desfire_key { enum DESFIRE_CRYPTOALGO type; @@ -139,7 +134,6 @@ void *mifare_cryto_postprocess_data(desfiretag_t tag, void *data, size_t *nbytes void mifare_cypher_single_block(desfirekey_t key, uint8_t *data, uint8_t *ivect, MifareCryptoDirection direction, MifareCryptoOperation operation, size_t block_size); void mifare_cypher_blocks_chained(desfiretag_t tag, desfirekey_t key, uint8_t *ivect, uint8_t *data, size_t data_size, MifareCryptoDirection direction, MifareCryptoOperation operation); size_t key_block_size(const desfirekey_t key); -size_t padded_data_length(const size_t nbytes, const size_t block_size); size_t maced_data_length(const desfirekey_t key, const size_t nbytes); size_t enciphered_data_length(const desfiretag_t tag, const size_t nbytes, int communication_settings); void cmac_generate_subkeys(desfirekey_t key, MifareCryptoDirection direction); diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 366995a06..af94d479d 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -31,7 +31,7 @@ #include "crc16.h" // crc16 ccitt #include "crc32.h" #include "commonutil.h" -#include "mifare/desfire_crypto.h" +#include "crypto/libpcrypto.h" void DesfireClearContext(DesfireContext *ctx) { ctx->keyNum = 0; @@ -96,7 +96,7 @@ bool DesfireIsAuthenticated(DesfireContext *dctx) { } size_t DesfireGetMACLength(DesfireContext *ctx) { - size_t mac_length = MAC_LENGTH; + size_t mac_length = DESFIRE_MAC_LENGTH; switch (ctx->secureChannel) { case DACNone: mac_length = 0; @@ -536,6 +536,45 @@ int DesfireEV2CalcCMAC(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t d return aes_cmac8(NULL, ctx->sessionKeyMAC, mdata, mac, mdatalen); } +int desfire_get_key_length(DesfireCryptoAlgorythm key_type) { + switch (key_type) { + case T_DES: + return 8; + case T_3DES: + return 16; + case T_3K3DES: + return 24; + case T_AES: + return 16; + } + return 0; +} + +size_t desfire_get_key_block_length(DesfireCryptoAlgorythm key_type) { + size_t block_size = 8; + switch (key_type) { + case T_DES: + case T_3DES: + case T_3K3DES: + block_size = 8; + break; + case T_AES: + block_size = 16; + break; + } + return block_size; +} + +/* + * Size required to store nbytes of data in a buffer of size n*block_size. + */ +size_t padded_data_length(const size_t nbytes, const size_t block_size) { + if ((!nbytes) || (nbytes % block_size)) + return ((nbytes / block_size) + 1) * block_size; + else + return nbytes; +} + void desfire_crc32(const uint8_t *data, const size_t len, uint8_t *crc) { crc32_ex(data, len, crc); } diff --git a/client/src/mifare/desfirecrypto.h b/client/src/mifare/desfirecrypto.h index 9fa379049..37d56bf82 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -22,12 +22,13 @@ #define __DESFIRECRYPTO_H #include "common.h" -#include "mifare/mifare4.h" #define CRYPTO_AES_BLOCK_SIZE 16 #define MAX_CRYPTO_BLOCK_SIZE 16 #define DESFIRE_MAX_CRYPTO_BLOCK_SIZE 16 #define DESFIRE_MAX_KEY_SIZE 24 +#define DESFIRE_MAC_LENGTH 4 +#define DESFIRE_CMAC_LENGTH 8 #define DESFIRE_GET_ISO_STATUS(x) ( ((uint16_t)(0x91<<8)) + (uint16_t)x ) @@ -125,6 +126,10 @@ void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool en void DesfireEV2FillIV(DesfireContext *ctx, bool ivforcommand, uint8_t *iv); int DesfireEV2CalcCMAC(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *mac); +int desfire_get_key_length(DesfireCryptoAlgorythm key_type); +size_t desfire_get_key_block_length(DesfireCryptoAlgorythm key_type); +size_t padded_data_length(const size_t nbytes, const size_t block_size); + void desfire_crc32(const uint8_t *data, const size_t len, uint8_t *crc); void desfire_crc32_append(uint8_t *data, const size_t len); bool desfire_crc32_check(uint8_t *data, const size_t len, uint8_t *crc); diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c index ced1fd073..03106f24e 100644 --- a/client/src/mifare/desfiresecurechan.c +++ b/client/src/mifare/desfiresecurechan.c @@ -21,7 +21,6 @@ #include "crc32.h" #include "commonutil.h" #include "protocols.h" -#include "mifare/desfire_crypto.h" static const uint8_t CommandsCanUseAnyChannel[] = { MFDES_S_ADDITIONAL_FRAME,