add test command and crc tests

This commit is contained in:
merlokk 2021-07-09 15:15:16 +03:00
commit fff1c8fae1
5 changed files with 130 additions and 2 deletions

View file

@ -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

View file

@ -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 \

View file

@ -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}
};

View file

@ -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 <unistd.h>
#include <string.h> // 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;
}

View file

@ -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 <stdbool.h>
#include "common.h"
bool DesfireTest(bool verbose);
#endif /* __CIPURSETEST_H__ */