diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 86d0a9bac..c43a856cb 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -303,7 +303,7 @@ void DesfireCMACGenerateSubkeys(DesfireContext *ctx, DesfireCryptoOpKeyType key_ } } -void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *data, size_t len, uint8_t *cmac) { +void DesfireCryptoCMACEx(DesfireContext *ctx, uint8_t *data, size_t len, size_t minlen, uint8_t *cmac) { int kbs = desfire_get_key_block_length(ctx->keyType); if (kbs == 0) return; @@ -317,9 +317,9 @@ void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *data, size_t len, uint8_t * memcpy(buffer, data, len); - if ((!len) || (len % kbs)) { + if ((!len) || (len % kbs) || (len < minlen)) { buffer[len++] = 0x80; - while (len % kbs) { + while (len % kbs || len < minlen) { buffer[len++] = 0x00; } bin_xor(buffer + len - kbs, sk2, kbs); @@ -333,6 +333,10 @@ void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *data, size_t len, uint8_t * memcpy(cmac, ctx->IV, kbs); } +void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *data, size_t len, uint8_t *cmac) { + DesfireCryptoCMACEx(ctx, data, len, 0, cmac); +} + // This function is almot like cmac(...). but with some key differences. void MifareKdfAn10922(DesfireContext *ctx, const uint8_t *data, size_t len) { int kbs = desfire_get_key_block_length(ctx->keyType); // 8 or 16 @@ -355,7 +359,11 @@ void MifareKdfAn10922(DesfireContext *ctx, const uint8_t *data, size_t len) { buffer[0] = 0x01; memcpy(&buffer[1], data, len++); - if (len != (kbs2)) { + uint8_t cmac[DESFIRE_MAX_CRYPTO_BLOCK_SIZE] = {0}; + DesfireCryptoCMACEx(ctx, buffer, len, kbs2, cmac); + memcpy(ctx->key, cmac, kbs); + + /*if (len != (kbs2)) { buffer[len++] = 0x80; while (len % kbs2) { buffer[len++] = 0x00; @@ -367,7 +375,7 @@ void MifareKdfAn10922(DesfireContext *ctx, const uint8_t *data, size_t len) { aes_encode(NULL, ctx->key, buffer, buffer, kbs2); - memcpy(ctx->key, buffer + kbs, kbs); + memcpy(ctx->key, buffer + kbs, kbs);*/ } void DesfireDESKeySetVersion(uint8_t *key, DesfireCryptoAlgorythm keytype, uint8_t version) { diff --git a/client/src/mifare/desfirecrypto.h b/client/src/mifare/desfirecrypto.h index e28c14cb6..33c8eba0f 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -113,6 +113,7 @@ void DesfireCryptoEncDec(DesfireContext *ctx, DesfireCryptoOpKeyType key_type, u void DesfireCryptoEncDecEx(DesfireContext *ctx, DesfireCryptoOpKeyType key_type, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, bool dir_to_send, bool encode, uint8_t *iv); void DesfireCMACGenerateSubkeys(DesfireContext *ctx, DesfireCryptoOpKeyType key_type, uint8_t *sk1, uint8_t *sk2); void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *srcdata, size_t srcdatalen, uint8_t *cmac); +void DesfireCryptoCMACEx(DesfireContext *ctx, uint8_t *data, size_t len, size_t minlen, uint8_t *cmac); void MifareKdfAn10922(DesfireContext *ctx, const uint8_t *data, size_t len); void DesfireDESKeySetVersion(uint8_t *key, DesfireCryptoAlgorythm keytype, uint8_t version);