From fff1c8fae16811c89db44675f3bac2059362c60b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 9 Jul 2021 15:15:16 +0300 Subject: [PATCH 1/5] add test command and crc tests --- client/CMakeLists.txt | 1 + client/Makefile | 1 + client/src/cmdhfmfdes.c | 9 ++- client/src/mifare/desfiretest.c | 102 ++++++++++++++++++++++++++++++++ client/src/mifare/desfiretest.h | 19 ++++++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 client/src/mifare/desfiretest.c create mode 100644 client/src/mifare/desfiretest.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 536464d18..c7892ba7c 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -229,6 +229,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/mifare/desfirecrypto.c ${PM3_ROOT}/client/src/mifare/desfiresecurechan.c ${PM3_ROOT}/client/src/mifare/desfirecore.c + ${PM3_ROOT}/client/src/mifare/desfiretest.c ${PM3_ROOT}/client/src/uart/uart_posix.c ${PM3_ROOT}/client/src/uart/uart_win32.c ${PM3_ROOT}/client/src/ui/overlays.ui diff --git a/client/Makefile b/client/Makefile index 7d09586a0..51f7cb2df 100644 --- a/client/Makefile +++ b/client/Makefile @@ -592,6 +592,7 @@ SRCS = aiddesfire.c \ mifare/desfirecrypto.c \ mifare/desfirecore.c \ mifare/desfiresecurechan.c \ + mifare/desfiretest.c \ mifare/mad.c \ mifare/mfkey.c \ mifare/mifare4.c \ diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 7526207e3..73c4eb14c 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -30,9 +30,10 @@ #include "util_posix.h" // msleep #include "mifare/desfire_crypto.h" #include "mifare/desfirecore.h" +#include "mifare/desfiretest.h" +#include "mifare/mifaredefault.h" // default keys #include "crapto1/crapto1.h" #include "fileutils.h" -#include "mifare/mifaredefault.h" // default keys #include "nfc/ndef.h" // NDEF #include "mifare/mad.h" #include "generator.h" @@ -5312,6 +5313,10 @@ static int CmdHF14ADesGetAppNames(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesTest(const char *Cmd) { + DesfireTest(true); + return PM3_SUCCESS; +} static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, @@ -5346,7 +5351,7 @@ static command_t CommandTable[] = { {"read", CmdHF14ADesReadData, IfPm3Iso14443a, "Read data from standard/backup/record file"}, {"write", CmdHF14ADesWriteData, IfPm3Iso14443a, "Write data to standard/backup/record file"}, {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("System") " -----------------------"}, -// {"test", CmdHF14ADesTest, IfPm3Iso14443a, "Test crypto"}, + {"test", CmdHF14ADesTest, IfPm3Iso14443a, "Test crypto"}, {NULL, NULL, NULL, NULL} }; diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c new file mode 100644 index 000000000..f60623daa --- /dev/null +++ b/client/src/mifare/desfiretest.c @@ -0,0 +1,102 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2021 Merlok +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// tests for desfire +//----------------------------------------------------------------------------- + +#include "desfiretest.h" + +#include +#include // memcpy memset +#include "fileutils.h" + +#include "crypto/libpcrypto.h" +#include "mifare/desfirecrypto.h" + +static bool TestCRC16(void) { + uint8_t data[] = {0x04, 0x44, 0x0F, 0x32, 0x76, 0x31, 0x80, 0x27, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + bool res = true; + size_t len = DesfireSearchCRCPos(data, 16, 0x00, 2); + res = res && (len == 7); + + len = DesfireSearchCRCPos(data, 7 + 2, 0x00, 2); + res = res && (len == 7); + + len = DesfireSearchCRCPos(data, 7, 0x00, 2); + res = res && (len == 0); + + len = DesfireSearchCRCPos(data, 3, 0x00, 2); + res = res && (len == 0); + + len = DesfireSearchCRCPos(data, 1, 0x00, 2); + res = res && (len == 0); + + if (res) + PrintAndLogEx(INFO, "crc16............ " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "crc16............ " _RED_("fail")); + + return res; +} + +static bool TestCRC32(void) { + uint8_t data[] = {0x04, 0x44, 0x0F, 0x32, 0x76, 0x31, 0x80, 0x99, 0xCE, 0x1A, 0xD4, 0x00, 0x00, 0x00, 0x00, 0x00}; + + bool res = true; + size_t len = DesfireSearchCRCPos(data, 16, 0x00, 4); + res = res && (len == 7); + + len = DesfireSearchCRCPos(data, 7 + 4, 0x00, 4); + res = res && (len == 7); + + len = DesfireSearchCRCPos(data, 5, 0x00, 4); + res = res && (len == 0); + + len = DesfireSearchCRCPos(data, 4, 0x00, 4); + res = res && (len == 0); + + len = DesfireSearchCRCPos(data, 2, 0x00, 4); + res = res && (len == 0); + + if (res) + PrintAndLogEx(INFO, "crc32............ " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "crc32............ " _RED_("fail")); + + return res; +} +static bool TestCMAC(void) { + bool res = true; + + + if (res) + PrintAndLogEx(INFO, "CMAC............. " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "CMAC............. " _RED_("fail")); + + return res; +} + +bool DesfireTest(bool verbose) { + bool res = true; + + PrintAndLogEx(INFO, "------ " _CYAN_("Desfire Tests") " ------"); + + res = res && TestCRC16(); + res = res && TestCRC32(); + res = res && TestCMAC(); + + PrintAndLogEx(INFO, "---------------------------"); + if (res) + PrintAndLogEx(SUCCESS, " Tests [ %s ]", _GREEN_("ok")); + else + PrintAndLogEx(FAILED, " Tests [ %s ]", _RED_("fail")); + + PrintAndLogEx(NORMAL, ""); + return res; +} diff --git a/client/src/mifare/desfiretest.h b/client/src/mifare/desfiretest.h new file mode 100644 index 000000000..e89330899 --- /dev/null +++ b/client/src/mifare/desfiretest.h @@ -0,0 +1,19 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2021 Merlok +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// tests for desfire +//----------------------------------------------------------------------------- + +#ifndef __CIPURSETEST_H__ +#define __CIPURSETEST_H__ + +#include +#include "common.h" + +bool DesfireTest(bool verbose); + +#endif /* __CIPURSETEST_H__ */ From 8403624bb1fc447ee078ddec24b188b13590b517 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 9 Jul 2021 15:50:40 +0300 Subject: [PATCH 2/5] cmac tests --- client/src/mifare/desfiretest.c | 131 +++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 4 deletions(-) diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c index f60623daa..8bad6683f 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -17,6 +17,12 @@ #include "crypto/libpcrypto.h" #include "mifare/desfirecrypto.h" +static uint8_t CMACData[] = {0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, + 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, + 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51}; + + static bool TestCRC16(void) { uint8_t data[] = {0x04, 0x44, 0x0F, 0x32, 0x76, 0x31, 0x80, 0x27, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; @@ -70,14 +76,129 @@ static bool TestCRC32(void) { return res; } -static bool TestCMAC(void) { + +// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TDES_CMAC.pdf +static bool TestCMAC3TDEA(void) { bool res = true; + + uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, + 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23}; + DesfireContext dctx; + DesfireSetKey(&dctx, 0, T_3K3DES, key); + memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); + uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; + uint8_t cmac1[] = {0x7D, 0xB0, 0xD3, 0x7D, 0xF9, 0x36, 0xC5, 0x50}; + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); + res = res && (memcmp(cmac, cmac1, sizeof(cmac1)) == 0); + + uint8_t cmac2[] = {0x30, 0x23, 0x9C, 0xF1, 0xF5, 0x2E, 0x66, 0x09}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); + res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); + + uint8_t cmac3[] = {0x6C, 0x9F, 0x3E, 0xE4, 0x92, 0x3F, 0x6B, 0xE2}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 20, cmac); + res = res && (memcmp(cmac, cmac3, sizeof(cmac1)) == 0); + + uint8_t cmac4[] = {0x99, 0x42, 0x9B, 0xD0, 0xBF, 0x79, 0x04, 0xE5}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); + res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); if (res) - PrintAndLogEx(INFO, "CMAC............. " _GREEN_("passed")); + PrintAndLogEx(INFO, "CMAC 3TDEA....... " _GREEN_("passed")); else - PrintAndLogEx(ERR, "CMAC............. " _RED_("fail")); + PrintAndLogEx(ERR, "CMAC 3TDEA....... " _RED_("fail")); + + return res; +} + +// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TDES_CMAC.pdf +static bool TestCMAC2TDEA(void) { + bool res = true; + + uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01}; + DesfireContext dctx; + DesfireSetKey(&dctx, 0, T_3DES, key); + memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); + uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; + + uint8_t cmac1[] = {0x79, 0xCE, 0x52, 0xA7, 0xF7, 0x86, 0xA9, 0x60}; + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); +// PrintAndLogEx(INFO, "cmac: %s", sprint_hex(cmac, 16)); + res = res && (memcmp(cmac, cmac1, sizeof(cmac1)) == 0); + + uint8_t cmac2[] = {0xCC, 0x18, 0xA0, 0xB7, 0x9A, 0xF2, 0x41, 0x3B}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); + res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); + + uint8_t cmac3[] = {0xC0, 0x6D, 0x37, 0x7E, 0xCD, 0x10, 0x19, 0x69}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 20, cmac); + res = res && (memcmp(cmac, cmac3, sizeof(cmac1)) == 0); + + uint8_t cmac4[] = {0x9C, 0xD3, 0x35, 0x80, 0xF9, 0xB6, 0x4D, 0xFB}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); + res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); + + if (res) + PrintAndLogEx(INFO, "CMAC 2TDEA....... " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "CMAC 2TDEA....... " _RED_("fail")); + + return res; +} + +static bool TestCMACDES(void) { + bool res = true; + + uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; + DesfireContext dctx; + DesfireSetKey(&dctx, 0, T_DES, key); + memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); + uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; + + uint8_t cmac1[] = {0x86, 0xF7, 0x9C, 0x13, 0xFD, 0x30, 0x6E, 0x67}; + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); + res = res && (memcmp(cmac, cmac1, sizeof(cmac1)) == 0); + + uint8_t cmac2[] = {0xBE, 0xA4, 0x21, 0x22, 0x92, 0x46, 0x2A, 0x85}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); + res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); + + uint8_t cmac3[] = {0x3E, 0x2F, 0x83, 0x10, 0xC5, 0x69, 0x27, 0x5E}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 20, cmac); + res = res && (memcmp(cmac, cmac3, sizeof(cmac1)) == 0); + + uint8_t cmac4[] = {0x9D, 0x1F, 0xC4, 0xD4, 0xC0, 0x25, 0x91, 0x32}; + memset(cmac, 0, sizeof(cmac)); + memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); + DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); + res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); + + if (res) + PrintAndLogEx(INFO, "CMAC DES......... " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "CMAC DES......... " _RED_("fail")); return res; } @@ -89,7 +210,9 @@ bool DesfireTest(bool verbose) { res = res && TestCRC16(); res = res && TestCRC32(); - res = res && TestCMAC(); + res = res && TestCMAC3TDEA(); + res = res && TestCMAC2TDEA(); + res = res && TestCMACDES(); PrintAndLogEx(INFO, "---------------------------"); if (res) From 48a96366795bc7fd4586dbd1cc31a80dc52a32c4 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 9 Jul 2021 15:56:18 +0300 Subject: [PATCH 3/5] style --- client/src/mifare/desfiretest.c | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c index 8bad6683f..b89963352 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -17,15 +17,16 @@ #include "crypto/libpcrypto.h" #include "mifare/desfirecrypto.h" -static uint8_t CMACData[] = {0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, - 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, - 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, - 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51}; +static uint8_t CMACData[] = {0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, + 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, + 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51 + }; static bool TestCRC16(void) { uint8_t data[] = {0x04, 0x44, 0x0F, 0x32, 0x76, 0x31, 0x80, 0x27, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - + bool res = true; size_t len = DesfireSearchCRCPos(data, 16, 0x00, 2); res = res && (len == 7); @@ -52,7 +53,7 @@ static bool TestCRC16(void) { static bool TestCRC32(void) { uint8_t data[] = {0x04, 0x44, 0x0F, 0x32, 0x76, 0x31, 0x80, 0x99, 0xCE, 0x1A, 0xD4, 0x00, 0x00, 0x00, 0x00, 0x00}; - + bool res = true; size_t len = DesfireSearchCRCPos(data, 16, 0x00, 4); res = res && (len == 7); @@ -81,14 +82,15 @@ static bool TestCRC32(void) { static bool TestCMAC3TDEA(void) { bool res = true; - uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, - 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, - 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23}; + uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, + 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 + }; DesfireContext dctx; DesfireSetKey(&dctx, 0, T_3K3DES, key); memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; - + uint8_t cmac1[] = {0x7D, 0xB0, 0xD3, 0x7D, 0xF9, 0x36, 0xC5, 0x50}; memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); @@ -99,7 +101,7 @@ static bool TestCMAC3TDEA(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); - + uint8_t cmac3[] = {0x6C, 0x9F, 0x3E, 0xE4, 0x92, 0x3F, 0x6B, 0xE2}; memset(cmac, 0, sizeof(cmac)); memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); @@ -111,7 +113,7 @@ static bool TestCMAC3TDEA(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); - + if (res) PrintAndLogEx(INFO, "CMAC 3TDEA....... " _GREEN_("passed")); else @@ -124,17 +126,18 @@ static bool TestCMAC3TDEA(void) { static bool TestCMAC2TDEA(void) { bool res = true; - uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, - 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01}; + uint8_t key[DESFIRE_MAX_KEY_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01 + }; DesfireContext dctx; DesfireSetKey(&dctx, 0, T_3DES, key); memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; - + uint8_t cmac1[] = {0x79, 0xCE, 0x52, 0xA7, 0xF7, 0x86, 0xA9, 0x60}; memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); -// PrintAndLogEx(INFO, "cmac: %s", sprint_hex(cmac, 16)); +// PrintAndLogEx(INFO, "cmac: %s", sprint_hex(cmac, 16)); res = res && (memcmp(cmac, cmac1, sizeof(cmac1)) == 0); uint8_t cmac2[] = {0xCC, 0x18, 0xA0, 0xB7, 0x9A, 0xF2, 0x41, 0x3B}; @@ -142,7 +145,7 @@ static bool TestCMAC2TDEA(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); - + uint8_t cmac3[] = {0xC0, 0x6D, 0x37, 0x7E, 0xCD, 0x10, 0x19, 0x69}; memset(cmac, 0, sizeof(cmac)); memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); @@ -154,7 +157,7 @@ static bool TestCMAC2TDEA(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); - + if (res) PrintAndLogEx(INFO, "CMAC 2TDEA....... " _GREEN_("passed")); else @@ -171,7 +174,7 @@ static bool TestCMACDES(void) { DesfireSetKey(&dctx, 0, T_DES, key); memcpy(dctx.sessionKeyMAC, key, DESFIRE_MAX_KEY_SIZE); uint8_t cmac[DESFIRE_MAX_KEY_SIZE] = {0}; - + uint8_t cmac1[] = {0x86, 0xF7, 0x9C, 0x13, 0xFD, 0x30, 0x6E, 0x67}; memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 0, cmac); @@ -182,7 +185,7 @@ static bool TestCMACDES(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 16, cmac); res = res && (memcmp(cmac, cmac2, sizeof(cmac1)) == 0); - + uint8_t cmac3[] = {0x3E, 0x2F, 0x83, 0x10, 0xC5, 0x69, 0x27, 0x5E}; memset(cmac, 0, sizeof(cmac)); memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); @@ -194,7 +197,7 @@ static bool TestCMACDES(void) { memset(dctx.IV, 0, DESFIRE_MAX_KEY_SIZE); DesfireCryptoCMAC(&dctx, CMACData, 32, cmac); res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0); - + if (res) PrintAndLogEx(INFO, "CMAC DES......... " _GREEN_("passed")); else From 65b92e03526965bb64b233a79ccb20dbff7452d9 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 9 Jul 2021 15:59:45 +0300 Subject: [PATCH 4/5] add tests to CI --- tools/pm3_tests.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 41882f2e5..aba88f922 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -492,6 +492,7 @@ while true; do if ! CheckExecute "hf iclass loclass test" "$CLIENTBIN -c 'hf iclass loclass --test'" "key diversification (ok)"; then break; fi if ! CheckExecute "emv test" "$CLIENTBIN -c 'emv test'" "Test(s) \[ ok"; then break; fi if ! CheckExecute "hf cipurse test" "$CLIENTBIN -c 'hf cipurse test'" "Tests \[ ok"; then break; fi + if ! CheckExecute "hf mfdes test" "$CLIENTBIN -c 'hf mfdes test'" "Tests \[ ok"; then break; fi fi fi echo -e "\n------------------------------------------------------------" From aa1d98ba55aae1a0e156cdd9270935a71afcc1ff Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 9 Jul 2021 16:06:18 +0300 Subject: [PATCH 5/5] fix test AlwaysAvailable --- client/src/cmdhfmfdes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 73c4eb14c..b4c84e832 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5351,7 +5351,7 @@ static command_t CommandTable[] = { {"read", CmdHF14ADesReadData, IfPm3Iso14443a, "Read data from standard/backup/record file"}, {"write", CmdHF14ADesWriteData, IfPm3Iso14443a, "Write data to standard/backup/record file"}, {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("System") " -----------------------"}, - {"test", CmdHF14ADesTest, IfPm3Iso14443a, "Test crypto"}, + {"test", CmdHF14ADesTest, AlwaysAvailable, "Test crypto"}, {NULL, NULL, NULL, NULL} };