X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/f38a152863a5eb289acb169c5a38b4b77e87956e..15d4bf3e7822a2b3863f15a2f0d7c5b9f5f76911:/armsrc/desfire_crypto.c?ds=sidebyside diff --git a/armsrc/desfire_crypto.c b/armsrc/desfire_crypto.c index b77ad8ef..e7eceb3c 100644 --- a/armsrc/desfire_crypto.c +++ b/armsrc/desfire_crypto.c @@ -27,9 +27,8 @@ */ #include "desfire_crypto.h" -static void xor (const uint8_t *ivect, uint8_t *data, const size_t len); - -static size_t key_macing_length (desfirekey_t key); +static void xor (const uint8_t *ivect, uint8_t *data, const size_t len); +static size_t key_macing_length (desfirekey_t key); static void xor (const uint8_t *ivect, uint8_t *data, const size_t len) { for (size_t i = 0; i < len; i++) { @@ -85,6 +84,7 @@ void cmac (const desfirekey_t key, uint8_t *ivect, const uint8_t *data, size_t l mifare_cypher_blocks_chained (NULL, key, ivect, buffer, len, MCD_SEND, MCO_ENCYPHER); memcpy (cmac, ivect, kbs); + free(buffer); } size_t key_block_size (const desfirekey_t key) { @@ -161,7 +161,7 @@ size_t enciphered_data_length (const desfiretag_t tag, const size_t nbytes, int return padded_data_length (nbytes + crc_length, block_size); } -void* mifare_cryto_preprocess_data (desfiretag_t tag, void *data, size_t *nbytes, off_t offset, int communication_settings) { +void* mifare_cryto_preprocess_data (desfiretag_t tag, void *data, size_t *nbytes, size_t offset, int communication_settings) { uint8_t *res = data; uint8_t mac[4]; size_t edl; @@ -226,8 +226,8 @@ void* mifare_cryto_preprocess_data (desfiretag_t tag, void *data, size_t *nbytes cmac (key, DESFIRE (tag)->ivect, res, *nbytes, DESFIRE (tag)->cmac); if (append_mac) { - maced_data_length (key, *nbytes); - + size_t len = maced_data_length (key, *nbytes); + ++len; memcpy (res, data, *nbytes); memcpy (res + *nbytes, DESFIRE (tag)->cmac, CMAC_LENGTH); *nbytes += CMAC_LENGTH; @@ -289,7 +289,7 @@ void* mifare_cryto_preprocess_data (desfiretag_t tag, void *data, size_t *nbytes } -void* mifare_cryto_postprocess_data (desfiretag_t tag, void *data, ssize_t *nbytes, int communication_settings) +void* mifare_cryto_postprocess_data (desfiretag_t tag, void *data, size_t *nbytes, int communication_settings) { void *res = data; size_t edl; @@ -321,7 +321,7 @@ void* mifare_cryto_postprocess_data (desfiretag_t tag, void *data, ssize_t *nbyt *nbytes = -1; res = NULL; #ifdef WITH_DEBUG - printf ("No room for MAC!"); + Dbprintf ("No room for MAC!"); #endif break; } @@ -336,7 +336,7 @@ void* mifare_cryto_postprocess_data (desfiretag_t tag, void *data, ssize_t *nbyt if (0 != memcmp ((uint8_t *)data + *nbytes - 1, (uint8_t *)edata + edl - 8, 4)) { #ifdef WITH_DEBUG - printf ("MACing not verified"); + Dbprintf ("MACing not verified"); hexdump ((uint8_t *)data + *nbytes - 1, key_macing_length (key), "Expect ", 0); hexdump ((uint8_t *)edata + edl - 8, key_macing_length (key), "Actual ", 0); #endif @@ -366,7 +366,7 @@ void* mifare_cryto_postprocess_data (desfiretag_t tag, void *data, ssize_t *nbyt ((uint8_t *)data)[*nbytes - 9] = first_cmac_byte; if (0 != memcmp (DESFIRE (tag)->cmac, (uint8_t *)data + *nbytes - 9, 8)) { #ifdef WITH_DEBUG - printf ("CMAC NOT verified :-("); + Dbprintf ("CMAC NOT verified :-("); hexdump ((uint8_t *)data + *nbytes - 9, 8, "Expect ", 0); hexdump (DESFIRE (tag)->cmac, 8, "Actual ", 0); #endif @@ -580,7 +580,7 @@ void mifare_cypher_single_block (desfirekey_t key, uint8_t *data, uint8_t *ivect { AesCtx ctx; AesCtxIni(&ctx, ivect, key->data, KEY128,CBC); - AesEncrypt(&ctx, data, edata, sizeof(data) ); + AesEncrypt(&ctx, data, edata, sizeof(edata) ); break; } case MCO_DECYPHER: