From 8bd14245c6f24f8b8991ec667ccafdbe544a6008 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 19 Aug 2021 16:37:55 +0300 Subject: [PATCH] lrp cmac protocol level --- client/src/mifare/desfirecrypto.c | 18 ++++++++++++++++++ client/src/mifare/desfirecrypto.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 47e14eedd..654fabfef 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -668,6 +668,24 @@ void DesfireGenTransSessionKey(uint8_t *key, uint32_t trCntr, uint8_t *uid, bool DesfireCryptoCMACEx(&ctx, DCOMainKey, xiv, 16, 0, sessionkey); } +int DesfireLRPCalcCMAC(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *mac) { + uint8_t mdata[1050] = {0}; + size_t mdatalen = 0; + + mdata[0] = cmd; + Uint2byteToMemLe(&mdata[1], ctx->cmdCntr); + memcpy(&mdata[3], ctx->TI, 4); + if (data != NULL && datalen > 0) + memcpy(&mdata[7], data, datalen); + mdatalen = 1 + 2 + 4 + datalen; + + LRPContext lctx = {0}; + LRPSetKey(&lctx, ctx->sessionKeyMAC, 0, true); + LRPCMAC8(&lctx, mdata, mdatalen, mac); + + return 0; +} + int desfire_get_key_length(DesfireCryptoAlgorythm key_type) { switch (key_type) { case T_DES: diff --git a/client/src/mifare/desfirecrypto.h b/client/src/mifare/desfirecrypto.h index 4a2810bff..7d19a5c28 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -144,6 +144,8 @@ 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); void DesfireGenTransSessionKey(uint8_t *key, uint32_t trCntr, uint8_t *uid, bool forMAC, uint8_t *sessionkey); +int DesfireLRPCalcCMAC(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);