From a66dbf90fe7850308047954c82a45b12df5692fc Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 30 Jul 2021 13:14:54 +0300 Subject: [PATCH] ev2 session keys generation --- client/src/mifare/desfirecrypto.c | 31 ++++++++++++++++++++ client/src/mifare/desfirecrypto.h | 2 ++ client/src/mifare/desfiretest.c | 48 ++++++++++++++++++++++++------- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 391da348a..cf26fe22d 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -386,6 +386,37 @@ uint8_t DesfireCommModeToFileCommMode(DesfireCommunicationMode comm_mode) { return fmode; } +// https://www.nxp.com/docs/en/application-note/AN12343.pdf +// page 35 +void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool enckey, uint8_t *sessionkey) { + uint8_t data[64] = {0}; + memset(sessionkey, 0, CRYPTO_AES_BLOCK_SIZE); + + if (enckey) { + data[0] = 0xa5; + data[1] = 0x5a; + } else { + data[0] = 0x5a; + data[1] = 0xa5; + } + data[3] = 0x01; + data[5] = 0x80; + + // data+6 - start of rnd part + memcpy(data + 6, rndA, 8); + bin_xor(data + 8, rndB, 6); // xor rndb 6b + memcpy(data + 14, rndB + 6, 10); + memcpy(data + 24, rndA + 8, 8); + + uint8_t cmac[CRYPTO_AES_BLOCK_SIZE] = {0}; + DesfireContext ctx = {0}; + ctx.keyType = T_AES; + memcpy(ctx.key, key, 16); // aes-128 + DesfireCryptoCMAC(&ctx, data, 32, cmac); + + memcpy(sessionkey, cmac, CRYPTO_AES_BLOCK_SIZE); +} + 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 b992a7f83..b8ba977fd 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -109,6 +109,8 @@ uint8_t DesfireDESKeyGetVersion(uint8_t *key); DesfireCommunicationMode DesfireFileCommModeToCommMode(uint8_t file_comm_mode); uint8_t DesfireCommModeToFileCommMode(DesfireCommunicationMode comm_mode); +void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool enckey, uint8_t *sessionkey); + 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/desfiretest.c b/client/src/mifare/desfiretest.c index b89963352..480f44462 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -44,9 +44,9 @@ static bool TestCRC16(void) { res = res && (len == 0); if (res) - PrintAndLogEx(INFO, "crc16............ " _GREEN_("passed")); + PrintAndLogEx(INFO, "crc16............. " _GREEN_("passed")); else - PrintAndLogEx(ERR, "crc16............ " _RED_("fail")); + PrintAndLogEx(ERR, "crc16............. " _RED_("fail")); return res; } @@ -71,9 +71,9 @@ static bool TestCRC32(void) { res = res && (len == 0); if (res) - PrintAndLogEx(INFO, "crc32............ " _GREEN_("passed")); + PrintAndLogEx(INFO, "crc32............. " _GREEN_("passed")); else - PrintAndLogEx(ERR, "crc32............ " _RED_("fail")); + PrintAndLogEx(ERR, "crc32............. " _RED_("fail")); return res; } @@ -115,9 +115,9 @@ static bool TestCMAC3TDEA(void) { res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); if (res) - PrintAndLogEx(INFO, "CMAC 3TDEA....... " _GREEN_("passed")); + PrintAndLogEx(INFO, "CMAC 3TDEA........ " _GREEN_("passed")); else - PrintAndLogEx(ERR, "CMAC 3TDEA....... " _RED_("fail")); + PrintAndLogEx(ERR, "CMAC 3TDEA........ " _RED_("fail")); return res; } @@ -159,9 +159,9 @@ static bool TestCMAC2TDEA(void) { res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); if (res) - PrintAndLogEx(INFO, "CMAC 2TDEA....... " _GREEN_("passed")); + PrintAndLogEx(INFO, "CMAC 2TDEA........ " _GREEN_("passed")); else - PrintAndLogEx(ERR, "CMAC 2TDEA....... " _RED_("fail")); + PrintAndLogEx(ERR, "CMAC 2TDEA........ " _RED_("fail")); return res; } @@ -199,9 +199,36 @@ static bool TestCMACDES(void) { res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); if (res) - PrintAndLogEx(INFO, "CMAC DES......... " _GREEN_("passed")); + PrintAndLogEx(INFO, "CMAC DES.......... " _GREEN_("passed")); else - PrintAndLogEx(ERR, "CMAC DES......... " _RED_("fail")); + PrintAndLogEx(ERR, "CMAC DES.......... " _RED_("fail")); + + return res; +} + +// https://www.nxp.com/docs/en/application-note/AN12343.pdf +// page 33-34 +static bool TestEV2SessionKeys(void) { + bool res = true; + + uint8_t key[16] = {0}; + uint8_t rnda[] = {0xB0, 0x4D, 0x07, 0x87, 0xC9, 0x3E, 0xE0, 0xCC, 0x8C, 0xAC, 0xC8, 0xE8, 0x6F, 0x16, 0xC6, 0xFE}; + uint8_t rndb[] = {0xFA, 0x65, 0x9A, 0xD0, 0xDC, 0xA7, 0x38, 0xDD, 0x65, 0xDC, 0x7D, 0xC3, 0x86, 0x12, 0xAD, 0x81}; + uint8_t sessionkeyauth[] = {0x63, 0xDC, 0x07, 0x28, 0x62, 0x89, 0xA7, 0xA6, 0xC0, 0x33, 0x4C, 0xA3, 0x1C, 0x31, 0x4A, 0x04}; + uint8_t sessionkeymac[] = {0x77, 0x4F, 0x26, 0x74, 0x3E, 0xCE, 0x6A, 0xF5, 0x03, 0x3B, 0x6A, 0xE8, 0x52, 0x29, 0x46, 0xF6}; + + uint8_t sessionkey[16] = {0}; + DesfireGenSessionKeyEV2(key, rnda, rndb, true, sessionkey); + res = res && (memcmp(sessionkey, sessionkeyauth, sizeof(sessionkeyauth)) == 0); + + memset(sessionkey, 0, sizeof(sessionkey)); + DesfireGenSessionKeyEV2(key, rnda, rndb, false, sessionkey); + res = res && (memcmp(sessionkey, sessionkeymac, sizeof(sessionkeymac)) == 0); + + if (res) + PrintAndLogEx(INFO, "EV2 session keys.. " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "EV2 session keys.. " _RED_("fail")); return res; } @@ -216,6 +243,7 @@ bool DesfireTest(bool verbose) { res = res && TestCMAC3TDEA(); res = res && TestCMAC2TDEA(); res = res && TestCMACDES(); + res = res && TestEV2SessionKeys(); PrintAndLogEx(INFO, "---------------------------"); if (res)