diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 4135a1f72..b4bec3d19 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -629,7 +629,7 @@ void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool en DesfireContext_t ctx = {0}; ctx.keyType = T_AES; memcpy(ctx.key, key, 16); // aes-128 - DesfireCryptoCMAC(&ctx, data, 32, cmac); + DesfireCryptoCMACEx(&ctx, DCOMainKey, data, 32, 0, cmac); memcpy(sessionkey, cmac, CRYPTO_AES_BLOCK_SIZE); } diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c index 95b054e7f..e74881d44 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -320,6 +320,21 @@ static bool TestEV2SessionKeys(void) { DesfireGenSessionKeyEV2(key, rnda, rndb, false, sessionkey); res = res && (memcmp(sessionkey, sessionkeymac, sizeof(sessionkeymac)) == 0); + // Non-zero key vectors, cross-checked with an independent AES-CMAC implementation + uint8_t key2[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; + uint8_t rnda2[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; + uint8_t rndb2[] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}; + uint8_t sessionkeyauth2[] = {0x19, 0x54, 0x28, 0x11, 0x9C, 0x85, 0x28, 0xD5, 0x95, 0x80, 0xA7, 0x3C, 0x7C, 0x96, 0xC6, 0xA3}; + uint8_t sessionkeymac2[] = {0x0A, 0xF0, 0x0F, 0x6E, 0x39, 0xF9, 0x04, 0xC4, 0xA6, 0x38, 0x8C, 0x81, 0x2D, 0x53, 0xDC, 0x94}; + + memset(sessionkey, 0, sizeof(sessionkey)); + DesfireGenSessionKeyEV2(key2, rnda2, rndb2, true, sessionkey); + res = res && (memcmp(sessionkey, sessionkeyauth2, sizeof(sessionkeyauth2)) == 0); + + memset(sessionkey, 0, sizeof(sessionkey)); + DesfireGenSessionKeyEV2(key2, rnda2, rndb2, false, sessionkey); + res = res && (memcmp(sessionkey, sessionkeymac2, sizeof(sessionkeymac2)) == 0); + PrintAndLogEx(INFO, "EV2 session keys.. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail")); return res; }