diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 7746ff4d3..f22d0b148 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -619,6 +619,24 @@ int DesfireEV2CalcCMAC(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t d return aes_cmac8(NULL, ctx->sessionKeyMAC, mdata, mac, mdatalen); } +void DesfireGenTransSessionKey(uint8_t *key, uint32_t trCntr, uint8_t *uid, bool forMAC, uint8_t *sessionkey) { + uint8_t xiv[CRYPTO_AES_BLOCK_SIZE] = {0}; + + if (forMAC) { + xiv[0] = 0x5a; + } else { + xiv[0] = 0xa5; + } + xiv[2] = 0x01; + xiv[4] = 0x80; + Uint4byteToMemLe(&xiv[5], trCntr + 1); + memcpy(&xiv[9], uid, 7); + + DesfireContext ctx = {0}; + DesfireSetKey(&ctx, 0, T_AES, key); + DesfireCryptoCMACEx(&ctx, DCOMainKey, xiv, 16, 0, sessionkey); +} + 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 9fe486a3a..df5d22d71 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -137,6 +137,7 @@ void DesfireGenSessionKeyEV1(const uint8_t rnda[], const uint8_t rndb[], Desfire void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool enckey, uint8_t *sessionkey); 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 desfire_get_key_length(DesfireCryptoAlgorythm key_type); size_t desfire_get_key_block_length(DesfireCryptoAlgorythm key_type);