Skip to content

Commit

Permalink
Saving work on the LibNFC testing code for DESFire builds
Browse files Browse the repository at this point in the history
  • Loading branch information
maxieds committed Jul 15, 2022
1 parent d297a0e commit 4cfea3d
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This notice must be retained at the top of all source files where indicated.
#include "../../Terminal/Terminal.h"
#include "../../Terminal/Commands.h"
#include "../../Settings.h"
#include "../../Memory.h"
#include "DESFireChameleonTerminal.h"
#include "DESFireFirmwareSettings.h"
#include "DESFirePICCControl.h"
Expand Down Expand Up @@ -110,6 +111,7 @@ CommandStatusIdType CommandDESFireSetHeaderProperty(char *OutParam, const char *
return COMMAND_ERR_INVALID_USAGE_ID;
}
SynchronizePICCInfo();
MemoryStore();
return COMMAND_INFO_OK_ID;
}
#endif /* DISABLE_PERMISSIVE_DESFIRE_SETTINGS */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA1(uint8_t *Buffer, uint16_t ByteCount) {
Encrypt2K3DESBuffer(CryptoChallengeResponseBytesSize, DesfireCommandState.RndB,
&Buffer[1], IV, Key);
}
DesfireLogEntry(LOG_INFO_DESFIRE_STATUS_INFO, (void *) IV, CryptoChallengeResponseBytesSize);

/* Scrub the key */
memset(Key, 0, keySize);
Expand Down Expand Up @@ -1817,7 +1818,6 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) {
DesfireState = DESFIRE_IDLE;
/* Validate command length */
if (ByteCount != 2 * CryptoChallengeResponseBytesSize + 1) {
DesfireLogEntry(LOG_INFO_DESFIRE_OUTGOING_DATA, Buffer, ByteCount);
Buffer[0] = STATUS_LENGTH_ERROR;
return DESFIRE_STATUS_RESPONSE_SIZE;
}
Expand Down Expand Up @@ -1845,6 +1845,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) {
Decrypt2K3DESBuffer(2 * CryptoChallengeResponseBytesSize, challengeRndAB,
&Buffer[1], IV, Key);
}
DesfireLogEntry(LOG_INFO_DESFIRE_STATUS_INFO, (void *) IV, CryptoChallengeResponseBytesSize);
RotateArrayLeft(challengeRndAB + CryptoChallengeResponseBytesSize, challengeRndB,
CryptoChallengeResponseBytesSize);
memcpy(challengeRndA, challengeRndAB, CryptoChallengeResponseBytesSize);
Expand All @@ -1866,6 +1867,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) {
Encrypt2K3DESBuffer(CryptoChallengeResponseBytesSize, challengeRndAB,
&Buffer[1], IV, Key);
}
DesfireLogEntry(LOG_INFO_DESFIRE_STATUS_INFO, (void *) IV, CryptoChallengeResponseBytesSize);

/* Create the session key based on the previous exchange */
generateSessionKey(SessionKey, challengeRndA, challengeRndB, cryptoKeyType);
Expand Down
8 changes: 5 additions & 3 deletions Firmware/Chameleon-Mini/Application/MifareDESFire.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This notice must be retained at the top of all source files where indicated.
#ifdef CONFIG_MF_DESFIRE_SUPPORT

#include "../Common.h"
#include "../Memory.h"
#include "Reader14443A.h"

#include "MifareDESFire.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ static void MifareDesfireAppInitLocal(uint8_t StorageSize, uint8_t Version, bool
InitialisePiccBackendEV0(StorageSize, FormatPICC);
break;
}
MemoryStore(); /* Make sure the randomized UID and PICC header data are preserved */
DesfireCommMode = DESFIRE_DEFAULT_COMMS_STANDARD;
}

Expand Down Expand Up @@ -133,6 +135,7 @@ uint16_t MifareDesfireProcessCommand(uint8_t *Buffer, uint16_t ByteCount) {
} else if (Buffer[0] != STATUS_ADDITIONAL_FRAME) {
DesfireState = DESFIRE_IDLE;
}
DesfireLogEntry(LOG_INFO_DESFIRE_INCOMING_DATA, (void *) Buffer, ByteCount);

uint16_t ReturnBytes = 0;
switch (DesfireState) {
Expand Down Expand Up @@ -187,7 +190,6 @@ uint16_t MifareDesfireProcess(uint8_t *Buffer, uint16_t BitCount) {
DEBUG_PRINT_P(PSTR("RESEND LAST"));
memcpy(&Buffer[0], &ISO14443ALastIncomingDataFrame[0], ASBYTES(ISO14443ALastIncomingDataFrameBits));
return ISO14443ALastIncomingDataFrameBits;
//return ISO14443A_APP_NO_RESPONSE;
}
DesfireCmdCLA = Buffer[0];
if (ByteCount >= 2 && Buffer[1] == STATUS_ADDITIONAL_FRAME && DesfireCLA(Buffer[0])) {
Expand Down Expand Up @@ -267,8 +269,8 @@ uint16_t MifareDesfireAppProcess(uint8_t *Buffer, uint16_t BitCount) {
memcpy(&Buffer[0], &ISO7816PrologueBytes[0], 2);
ProcessedByteCount = DesfirePostprocessAPDU(ActiveCommMode, Buffer, ProcessedByteCount + 2);
return ISO14443AStoreLastDataFrameAndReturn(Buffer, ASBITS(ProcessedByteCount));
} else if (ByteCount >= 5 && DesfireCLA(Buffer[0]) && Buffer[1] == STATUS_ADDITIONAL_FRAME &&
Buffer[2] == 0x00 && Buffer[3] == 0x00 && Buffer[4] == ByteCount - 9 &&
} else if (ByteCount >= 6 && DesfireCLA(Buffer[0]) && Buffer[1] == STATUS_ADDITIONAL_FRAME &&
Buffer[2] == 0x00 && Buffer[3] == 0x00 && Buffer[4] == ByteCount - 8 &&
DesfireStateExpectingAdditionalFrame(DesfireState)) {
/* [PM3-V2] : Handle the native-wrapped version of the additional frame data: */
uint16_t checkSumPostVerifyBytes = DesfirePreprocessAPDUAndTruncate(ActiveCommMode, Buffer, ByteCount);
Expand Down
3 changes: 1 addition & 2 deletions Firmware/Chameleon-Mini/BuildScripts/custom_build_targets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ desfire-dev: FLASH_DATA_SIZE:=0x$(FLASH_DATA_SIZE_CONST)
desfire-dev: FLASH_DATA_SIZE_UPPER_CONST:=20000
desfire-dev: FLASH_DATA_ADDR:=0x$(shell echo $$(( 0x$(FLASH_DATA_SIZE_UPPER_CONST) - 0x$(FLASH_DATA_SIZE_CONST) )) | xargs -0 printf %X)
desfire-dev: SUPPORTED_TAGS_BUILD:=-DCONFIG_MF_DESFIRE_SUPPORT
desfire-dev: EXTRA_CONFIG_SETTINGS:=-DMEMORY_LIMITED_TESTING \
-DDESFIRE_CRYPTO1_SAVE_SPACE \
desfire-dev: EXTRA_CONFIG_SETTINGS:=-DDESFIRE_CRYPTO1_SAVE_SPACE \
-finline-small-functions \
-DDESFIRE_MIN_OUTGOING_LOGSIZE=0 \
-DDESFIRE_MIN_INCOMING_LOGSIZE=0 \
Expand Down
2 changes: 1 addition & 1 deletion Firmware/Chameleon-Mini/LiveLogTick.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern LogBlockListNode *LogBlockListBegin;
extern LogBlockListNode *LogBlockListEnd;
extern uint8_t LogBlockListElementCount;

#define LIVE_LOGGER_POST_TICKS (7)
#define LIVE_LOGGER_POST_TICKS (4)
extern uint8_t LiveLogModePostTickCount;

INLINE bool AtomicAppendLogBlock(LogEntryEnum logCode, uint16_t sysTickTime, const uint8_t *logData, uint8_t logDataSize);
Expand Down
2 changes: 1 addition & 1 deletion Firmware/Chameleon-Mini/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ clean: local-clean

git-add-dev: LOCALFW_SOURCE_FILES:=Makefile ./*.{c,h} ./*/*.{c,h} ./*/*/*.{c,h}
git-add-dev: LOCALFW_BUILD_SCRIPT_FILES:=.gitignore custom_build_targets.mk lufa_build_extra.mk
git-add-dev: TESTING_SOURCE_FILES:=Makefile LocalInclude/*.h Source/*.c SampleOutputDumps/*.dump
git-add-dev: TESTING_SOURCE_FILES:=Makefile LocalInclude/*.h Source/*.c Libs/ArduinoCryptoLib/* Libs/ArduinoCryptoLib/*/*.{c,h} SampleOutputDumps/*.dump
git-add-dev:
@make style && git add $(LOCALFW_SOURCE_FILES)
@cd $(BUILD_SCR) && git add -f $(LOCALFW_BUILD_SCRIPT_FILES)
Expand Down
2 changes: 1 addition & 1 deletion Software/DESFireLibNFCTesting/LocalInclude/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef __LOCAL_CONFIG_H__
#define __LOCAL_CONFIG_H__

#define MAX_FRAME_LENGTH (264)
#define MAX_FRAME_LENGTH (255) // (64)
#define APPLICATION_AID_LENGTH (3)

static const uint8_t MASTER_APPLICATION_AID[] = {
Expand Down
24 changes: 18 additions & 6 deletions Software/DESFireLibNFCTesting/LocalInclude/CryptoUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ static uint8_t __CryptoAESOpMode = CRYPTO_AES_ECB_MODE;

static inline size_t EncryptAES128(const uint8_t *plainSrcBuf, size_t bufSize,
uint8_t *encDestBuf, CryptoData_t cdata) {
bool copyIVBuf = true;
DesfireAESCryptoContext *cryptoCtx = &(cdata.cryptoCtx);
DesfireAESCryptoInit(cdata.keyData, cdata.keySize, cryptoCtx);
size_t bufBlocks = bufSize / AES128_BLOCK_SIZE;
bool padLastBlock = (bufSize % AES128_BLOCK_SIZE) != 0;
uint8_t *IV = SessionIV;
uint8_t inputBlock[AES128_BLOCK_SIZE];
memset(IV, 0x00, AES128_BLOCK_SIZE);
if ((bufSize % AES128_BLOCK_SIZE) != 0) {
return 0xBE;
}
for (int blk = 0; blk < bufBlocks; blk++) {
if (__CryptoAESOpMode == CRYPTO_AES_CBC_MODE) {
if (blk == 0) {
Expand All @@ -120,6 +124,9 @@ static inline size_t EncryptAES128(const uint8_t *plainSrcBuf, size_t bufSize,
CryptoMemoryXOR(&plainSrcBuf[blk * AES128_BLOCK_SIZE], inputBlock, AES128_BLOCK_SIZE);
}
aes128EncryptBlock(cryptoCtx, encDestBuf + blk * AES128_BLOCK_SIZE, inputBlock);
if (blk + 1 == bufBlocks && copyIVBuf) {
memcpy(IV, inputBlock, AES128_BLOCK_SIZE);
}
} else { /* ECB mode */
memcpy(inputBlock, &plainSrcBuf[blk * AES128_BLOCK_SIZE], AES128_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, AES128_BLOCK_SIZE);
Expand All @@ -132,13 +139,17 @@ static inline size_t EncryptAES128(const uint8_t *plainSrcBuf, size_t bufSize,

static inline size_t DecryptAES128(const uint8_t *encSrcBuf, size_t bufSize,
uint8_t *plainDestBuf, CryptoData_t cdata) {
bool copyIVBuf = true;
DesfireAESCryptoContext *cryptoCtx = &(cdata.cryptoCtx);
DesfireAESCryptoInit(cdata.keyData, cdata.keySize, cryptoCtx);
size_t bufBlocks = (bufSize + AES128_BLOCK_SIZE - 1) / AES128_BLOCK_SIZE;
bool padLastBlock = (bufSize % AES128_BLOCK_SIZE) != 0;
uint8_t *IV = SessionIV;
uint8_t inputBlock[AES128_BLOCK_SIZE];
memset(IV, 0x00, AES128_BLOCK_SIZE);
if ((bufSize % AES128_BLOCK_SIZE) != 0) {
return 0xBE;
}
for (int blk = 0; blk < bufBlocks; blk++) {
if (__CryptoAESOpMode == CRYPTO_AES_CBC_MODE) {
aes128DecryptBlock(cryptoCtx, inputBlock, encSrcBuf + blk * AES128_BLOCK_SIZE);
Expand All @@ -150,6 +161,9 @@ static inline size_t DecryptAES128(const uint8_t *encSrcBuf, size_t bufSize,
CryptoMemoryXOR(&encSrcBuf[(blk - 1) * AES128_BLOCK_SIZE],
plainDestBuf + blk * AES128_BLOCK_SIZE, AES128_BLOCK_SIZE);
}
if (blk + 1 == bufBlocks && copyIVBuf) {
memcpy(IV, inputBlock, AES128_BLOCK_SIZE);
}
} else { /* ECB mode */
aes128DecryptBlock(cryptoCtx, plainDestBuf + blk * AES128_BLOCK_SIZE, encSrcBuf + blk * AES128_BLOCK_SIZE);
CryptoMemoryXOR(IV, plainDestBuf + blk * AES128_BLOCK_SIZE, AES128_BLOCK_SIZE);
Expand Down Expand Up @@ -182,8 +196,8 @@ static inline size_t Encrypt2K3DES(const uint8_t *plainSrcBuf, size_t bufSize,
for (int blk = 0; blk < numBlocks; blk++) {
memcpy(inputBlock, &plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_DES_BLOCK_SIZE);
DES_ecb2_encrypt(&encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE],
&plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched1, &keySched2, DES_ENCRYPT);
DES_ecb2_encrypt(&plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE],
&encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched1, &keySched2, DES_ENCRYPT);
memcpy(IV, &encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
}
}
Expand Down Expand Up @@ -238,8 +252,7 @@ static inline size_t Encrypt3DES(const uint8_t *plainSrcBuf, size_t bufSize,
for (int blk = 0; blk < numBlocks; blk++) {
memcpy(inputBlock, &plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_DES_BLOCK_SIZE);
DES_ecb3_encrypt(&encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE],
&plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched1, &keySched2, &keySched3, DES_ENCRYPT);
DES_ecb3_encrypt(inputBlock, &encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched1, &keySched2, &keySched3, DES_ENCRYPT);
memcpy(IV, &encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
}
}
Expand Down Expand Up @@ -294,8 +307,7 @@ static inline size_t EncryptDES(const uint8_t *plainSrcBuf, size_t bufSize,
for (int blk = 0; blk < numBlocks; blk++) {
memcpy(inputBlock, &plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_DES_BLOCK_SIZE);
DES_ecb_encrypt(&encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE],
&plainSrcBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched, DES_ENCRYPT);
DES_ecb_encrypt(inputBlock, &encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], &keySched, DES_ENCRYPT);
memcpy(IV, &encDestBuf[blk * CRYPTO_DES_BLOCK_SIZE], CRYPTO_DES_BLOCK_SIZE);
}
}
Expand Down
50 changes: 35 additions & 15 deletions Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static inline int AuthenticateAES128(nfc_device *nfcConnDev, uint8_t keyIndex, c
}
RxData_t *rxDataStorage = InitRxDataStruct(MAX_FRAME_LENGTH);
bool rxDataStatus = false;
rxDataStatus = libnfcTransmitBytes(nfcConnDev, AUTHENTICATE_AES_CMD, sizeof(AUTHENTICATE_AES_CMD), rxDataStorage);
rxDataStatus = libnfcTransmitBits(nfcConnDev, AUTHENTICATE_AES_CMD, sizeof(AUTHENTICATE_AES_CMD), rxDataStorage);
if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " <- ");
print_hex(rxDataStorage->rxDataBuf, rxDataStorage->recvSzRx);
Expand Down Expand Up @@ -65,8 +65,9 @@ static inline int AuthenticateAES128(nfc_device *nfcConnDev, uint8_t keyIndex, c
ConcatByteArrays(rndA, CRYPTO_CHALLENGE_RESPONSE_SIZE, rotatedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE, challengeResponse);
EncryptAES128(challengeResponse, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE, challengeResponseCipherText, aesCryptoData);

uint8_t sendBytesBuf[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6];
memset(sendBytesBuf, 0x00, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6);
uint16_t nonDataPaddingSize = 7;
uint8_t sendBytesBuf[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + nonDataPaddingSize];
memset(sendBytesBuf, 0x00, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + nonDataPaddingSize);
sendBytesBuf[0] = 0x90;
sendBytesBuf[1] = 0xaf;
sendBytesBuf[4] = 0x20;
Expand All @@ -82,7 +83,7 @@ static inline int AuthenticateAES128(nfc_device *nfcConnDev, uint8_t keyIndex, c
fprintf(stdout, " -> ");
print_hex(sendBytesBuf, sizeof(sendBytesBuf));
}
rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6, rxDataStorage);
rxDataStatus = libnfcTransmitBits(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + nonDataPaddingSize, rxDataStorage);
if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " <- ");
print_hex(rxDataStorage->rxDataBuf, rxDataStorage->recvSzRx);
Expand Down Expand Up @@ -157,24 +158,36 @@ static inline int AuthenticateISO(nfc_device *nfcConnDev, uint8_t keyIndex, cons
uint8_t rndA[CRYPTO_CHALLENGE_RESPONSE_SIZE], challengeResponse[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE], challengeResponseCipherText[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE];
int8_t IVBuf[CRYPTO_CHALLENGE_RESPONSE_SIZE];
memcpy(encryptedRndB, rxDataStorage->rxDataBuf, CRYPTO_CHALLENGE_RESPONSE_SIZE);
//memset(IVBuf, 0x00, CRYPTO_CHALLENGE_RESPONSE_SIZE);
memcpy(IVBuf, &encryptedRndB[CRYPTO_3KTDEA_BLOCK_SIZE], CRYPTO_3KTDEA_BLOCK_SIZE);
CryptoData_t desCryptoData = { 0 };
desCryptoData.keySize = 3 * 8;
desCryptoData.keyData = keyData;
desCryptoData.ivSize = CRYPTO_CHALLENGE_RESPONSE_SIZE;
Decrypt3DES(encryptedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE, plainTextRndB, NULL, desCryptoData);
Decrypt3DES(encryptedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE, plainTextRndB, IVBuf, desCryptoData);
if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " -- IV = ");
print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE);
}
RotateArrayRight(plainTextRndB, rotatedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE);
memset(IVBuf, 0x00, CRYPTO_CHALLENGE_RESPONSE_SIZE);
//memset(IVBuf, 0x00, CRYPTO_CHALLENGE_RESPONSE_SIZE);
//memcpy(IVBuf, &encryptedRndB[CRYPTO_3KTDEA_BLOCK_SIZE], CRYPTO_3KTDEA_BLOCK_SIZE);
desCryptoData.ivData = IVBuf;
GenerateRandomBytes(rndA, CRYPTO_CHALLENGE_RESPONSE_SIZE);
ConcatByteArrays(rndA, CRYPTO_CHALLENGE_RESPONSE_SIZE, rotatedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE, challengeResponse);
Encrypt3DES(challengeResponse, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE, challengeResponseCipherText, NULL, desCryptoData);
Encrypt3DES(challengeResponse, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE, challengeResponseCipherText, IVBuf, desCryptoData);
if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " -- IV = ");
print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE);
}

uint8_t sendBytesBuf[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6];
memset(sendBytesBuf, 0x00, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6);
uint16_t nonDataPaddingSize = 6;
uint8_t sendBytesBuf[2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + nonDataPaddingSize];
memset(sendBytesBuf, 0x00, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + nonDataPaddingSize);
sendBytesBuf[0] = 0x90;
sendBytesBuf[1] = 0xaf;
sendBytesBuf[4] = 0x20;
memcpy(sendBytesBuf + 5, challengeResponseCipherText, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE);
memcpy(&sendBytesBuf[5], challengeResponseCipherText, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE);

if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " -- RNDA = ");
Expand All @@ -186,7 +199,7 @@ static inline int AuthenticateISO(nfc_device *nfcConnDev, uint8_t keyIndex, cons
fprintf(stdout, " -> ");
print_hex(sendBytesBuf, sizeof(sendBytesBuf));
}
rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6, rxDataStorage);
rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, sizeof(sendBytesBuf), rxDataStorage);
if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " <- ");
print_hex(rxDataStorage->rxDataBuf, rxDataStorage->recvSzRx);
Expand All @@ -202,8 +215,15 @@ static inline int AuthenticateISO(nfc_device *nfcConnDev, uint8_t keyIndex, cons
// decrypt rndA sent by PICC, compare it to our original randomized rndA computed above,
// and report back whether they match:
uint8_t decryptedRndAFromPICCRotated[CRYPTO_CHALLENGE_RESPONSE_SIZE], decryptedRndA[CRYPTO_CHALLENGE_RESPONSE_SIZE];
Decrypt3DES(rxDataStorage->rxDataBuf, CRYPTO_CHALLENGE_RESPONSE_SIZE, decryptedRndAFromPICCRotated, NULL, desCryptoData);
RotateArrayLeft(decryptedRndAFromPICCRotated, decryptedRndA, CRYPTO_CHALLENGE_RESPONSE_SIZE);
//memcpy(IVBuf, &rxDataStorage->rxDataBuf[rxDataStorage->recvSzRx - CRYPTO_3KTDEA_BLOCK_SIZE - 1], CRYPTO_3KTDEA_BLOCK_SIZE);
//memset(IVBuf, 0x00, CRYPTO_CHALLENGE_RESPONSE_SIZE);
Decrypt3DES(rxDataStorage->rxDataBuf, CRYPTO_CHALLENGE_RESPONSE_SIZE, decryptedRndAFromPICCRotated, IVBuf, desCryptoData);
if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " -- IV = ");
print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE);
}
//RotateArrayLeft(decryptedRndAFromPICCRotated, decryptedRndA, CRYPTO_CHALLENGE_RESPONSE_SIZE);
memcpy(decryptedRndA, decryptedRndAFromPICCRotated, CRYPTO_CHALLENGE_RESPONSE_SIZE);
if (!memcmp(rndA, decryptedRndA, CRYPTO_CHALLENGE_RESPONSE_SIZE)) {
if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " ... AUTH OK! :)\n\n");
Expand Down Expand Up @@ -242,7 +262,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c
}
RxData_t *rxDataStorage = InitRxDataStruct(MAX_FRAME_LENGTH);
bool rxDataStatus = false;
rxDataStatus = libnfcTransmitBytes(nfcConnDev, AUTHENTICATE_LEGACY_CMD, sizeof(AUTHENTICATE_LEGACY_CMD), rxDataStorage);
rxDataStatus = libnfcTransmitBits(nfcConnDev, AUTHENTICATE_LEGACY_CMD, sizeof(AUTHENTICATE_LEGACY_CMD), rxDataStorage);
if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " <- ");
print_hex(rxDataStorage->rxDataBuf, rxDataStorage->recvSzRx);
Expand Down Expand Up @@ -290,7 +310,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c
fprintf(stdout, " -> ");
print_hex(sendBytesBuf, sizeof(sendBytesBuf));
}
rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6, rxDataStorage);
rxDataStatus = libnfcTransmitBits(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE + 6, rxDataStorage);
if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, " <- ");
print_hex(rxDataStorage->rxDataBuf, rxDataStorage->recvSzRx);
Expand Down
Loading

0 comments on commit 4cfea3d

Please sign in to comment.