Skip to content

Commit

Permalink
Space saving defines to remove currently unused crypto exchange funct…
Browse files Browse the repository at this point in the history
…ionality (will revisit when get to this subproject)
  • Loading branch information
maxieds committed Feb 12, 2022
1 parent 20a6960 commit b6be1b8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Firmware/Chameleon-Mini/Application/CryptoAES128.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ uint8_t CryptoAESDecryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciph
return 0;
}

#ifdef ENABLE_CRYPTO_TESTS
// This routine performs the CBC "send" mode chaining: C = E(P ^ IV); IV = C
void CryptoAES_CBCSend(uint16_t Count, void *Plaintext, void *Ciphertext,
uint8_t *IV, uint8_t *Key,
Expand Down Expand Up @@ -376,3 +377,4 @@ void CryptoAESDecrypt_CBCReceive(uint16_t Count, uint8_t *PlainText, uint8_t *Ci
};
CryptoAES_CBCRecv(Count, PlainText, CipherText, IV, Key, CryptoSpec);
}
#endif
11 changes: 7 additions & 4 deletions Firmware/Chameleon-Mini/Application/CryptoAES128.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,20 @@ typedef enum aes_intlvl {
/* AES interrupt callback function pointer. */
typedef void (*aes_callback_t)(void);

#define CRYPTO_AES128_STRUCT_ATTR __attribute((aligned(1)))

typedef struct {
CryptoAESDec_t ProcessingMode;
uint8_t ProcessingDelay; // [0,15]
CryptoAESAuto_t StartMode;
unsigned char OpMode; // 0 = ECB, 1 = CBC, 2 = OFB, 3 = CFB, 4 = CTR
CryptoAESXor_t XorMode;
} CryptoAESConfig_t;
} CryptoAESConfig_t CRYPTO_AES128_STRUCT_ATTR;

typedef struct {
unsigned char datrdy; // ENABLE/DISABLE; Data ready interrupt
unsigned char urad; // ENABLE/DISABLE; Unspecified Register Access Detection
} CryptoAES_ISRConfig_t;
} CryptoAES_ISRConfig_t CRYPTO_AES128_STRUCT_ATTR;

/* AES encryption complete. */
#define AES_ENCRYPTION_COMPLETE (1UL << 0)
Expand Down Expand Up @@ -149,15 +151,15 @@ typedef uint8_t (*CryptoAESFuncType)(uint8_t *, uint8_t *, uint8_t *);
typedef struct {
CryptoAESFuncType cryptFunc;
uint16_t blockSize;
} CryptoAES_CBCSpec_t;
} CryptoAES_CBCSpec_t CRYPTO_AES128_STRUCT_ATTR;

#ifdef ENABLE_CRYPTO_TESTS
void CryptoAES_CBCSend(uint16_t Count, void *Plaintext, void *Ciphertext,
uint8_t *IV, uint8_t *Key,
CryptoAES_CBCSpec_t CryptoSpec);
void CryptoAES_CBCRecv(uint16_t Count, void *Plaintext, void *Ciphertext,
uint8_t *IV, uint8_t *Key,
CryptoAES_CBCSpec_t CryptoSpec);

void CryptoAESEncrypt_CBCSend(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
uint8_t *Key, uint8_t *IV);
void CryptoAESDecrypt_CBCSend(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
Expand All @@ -166,6 +168,7 @@ void CryptoAESEncrypt_CBCReceive(uint16_t Count, uint8_t *PlainText, uint8_t *Ci
uint8_t *Key, uint8_t *IV);
void CryptoAESDecrypt_CBCReceive(uint16_t Count, uint8_t *PlainText, uint8_t *CipherText,
uint8_t *Key, uint8_t *IV);
#endif

/* Crypto utility functions: */
#define CRYPTO_BYTES_TO_BLOCKS(numBytes, blockSize) \
Expand Down
5 changes: 4 additions & 1 deletion Firmware/Chameleon-Mini/Application/CryptoTDEA.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void CryptoDecrypt3KTDEA(void *Plaintext, void *Ciphertext, const uint8_t *Keys)
void Encrypt3DESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext, const uint8_t *IV, const uint8_t *Keys);
void Decrypt3DESBuffer(uint16_t Count, void *Plaintext, const void *Ciphertext, const uint8_t *IV, const uint8_t *Keys);


#ifdef ENABLE_CRYPTO_TESTS
/** Performs the 2-key Triple DES en/deciphering in the CBC "send" mode (xor-then-crypt)
*
* \param Count Block count, expected to be >= 1
Expand Down Expand Up @@ -100,20 +100,23 @@ void CryptoDecrypt2KTDEA_CBCReceive(uint16_t Count, const void *Input, void *Out
*/
void CryptoEncrypt3KTDEA_CBCSend(uint16_t Count, const void *Plaintext, void *Ciphertext, void *IV, const uint8_t *Keys);
void CryptoDecrypt3KTDEA_CBCReceive(uint16_t Count, const void *Plaintext, void *Ciphertext, void *IV, const uint8_t *Keys);
#endif

/* Spec for more generic send/recv encrypt/decrypt schemes: */
typedef struct {
CryptoTDEAFuncType cryptFunc;
uint16_t blockSize;
} CryptoTDEA_CBCSpec;

#ifdef ENABLE_CRYPTO_TESTS
void CryptoTDEA_CBCSend(uint16_t Count, void *Plaintext, void *Ciphertext,
void *IV, const uint8_t *Keys, CryptoTDEA_CBCSpec CryptoSpec);
void CryptoTDEA_CBCRecv(uint16_t Count, void *Plaintext, void *Ciphertext,
void *IV, const uint8_t *Keys, CryptoTDEA_CBCSpec CryptoSpec);

uint8_t TransferEncryptTDEASend(uint8_t *Buffer, uint8_t Count);
uint8_t TransferEncryptTDEAReceive(uint8_t *Buffer, uint8_t Count);
#endif

/** Applies padding to the data within the buffer
*
Expand Down
4 changes: 4 additions & 0 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ void InitAESCryptoKeyData(void) {
memset(&SessionIV[0], 0x00, CRYPTO_MAX_BLOCK_SIZE);
}

#ifdef ENABLE_CRYPTO_TESTS
uint8_t CryptoAESTransferEncryptSend(uint8_t *Buffer, uint8_t Count, const uint8_t *Key) {
uint8_t AvailablePlaintext = TransferState.ReadData.Encryption.AvailablePlaintext;
uint8_t TempBuffer[(DESFIRE_MAX_PAYLOAD_AES_BLOCKS + 1) * CRYPTO_DES_BLOCK_SIZE];
Expand Down Expand Up @@ -213,6 +214,7 @@ uint8_t CryptoAESTransferEncryptReceive(uint8_t *Buffer, uint8_t Count, const ui
LogEntry(LOG_INFO_DESFIRE_INCOMING_DATA_ENC, Buffer, Count);
return STATUS_OPERATION_OK;
}
#endif

/* Checksum routines */

Expand Down Expand Up @@ -281,6 +283,7 @@ uint8_t TransferChecksumFinalMACTDEA(uint8_t *Buffer) {

/* Encryption routines */

#ifdef ENABLE_CRYPTO_TESTS
uint8_t TransferEncryptTDEASend(uint8_t *Buffer, uint8_t Count) {
uint8_t AvailablePlaintext = TransferState.ReadData.Encryption.AvailablePlaintext;
uint8_t TempBuffer[(DESFIRE_MAX_PAYLOAD_TDEA_BLOCKS + 1) * CRYPTO_DES_BLOCK_SIZE];
Expand Down Expand Up @@ -312,5 +315,6 @@ uint8_t TransferEncryptTDEAReceive(uint8_t *Buffer, uint8_t Count) {
LogEntry(LOG_INFO_DESFIRE_INCOMING_DATA_ENC, Buffer, Count);
return 0;
}
#endif

#endif /* CONFIG_MF_DESFIRE_SUPPORT */
3 changes: 3 additions & 0 deletions Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ typedef void (*CryptoAESCBCFuncType)(uint16_t, void *, void *, uint8_t *, uint8_

typedef uint8_t (*CryptoTransferSendFunc)(uint8_t *, uint8_t);
typedef uint8_t (*CryptoTransferReceiveFunc)(uint8_t *, uint8_t);

#ifdef ENABLE_CRYPTO_TESTS
uint8_t CryptoAESTransferEncryptSend(uint8_t *Buffer, uint8_t Count, const uint8_t *Key);
uint8_t CryptoAESTransferEncryptReceive(uint8_t *Buffer, uint8_t Count, const uint8_t *Key);
#endif

#define DESFIRE_MAX_PAYLOAD_AES_BLOCKS (DESFIRE_MAX_PAYLOAD_SIZE / CRYPTO_AES_BLOCK_SIZE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This notice must be retained at the top of all source files where indicated.
#define DESFIRE_PICC_ARRAY_ALIGNAT __attribute__((aligned(1)))
#define DESFIRE_FIRMWARE_ARRAY_ALIGNAT __attribute__((aligned(1)))
#define DESFIRE_FIRMWARE_ENUM_PACKING __attribute__((aligned(1)))
#define DESFIRE_FIRMWARE_NOINIT __attribute__ ((section (".noinit")))
#define DESFIRE_FIRMWARE_NOINIT __attribute__((section(".noinit")))

/* Some standard boolean interpreted and other values for types and return values: */
typedef int BOOL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ uint8_t ReadDataFilterSetup(uint8_t CommSettings) {
case DESFIRE_COMMS_PLAINTEXT_MAC:
TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateMACTDEA;
TransferState.Checksums.FinalFunc = &TransferChecksumFinalMACTDEA;
TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc = &CryptoEncrypt2KTDEA_CBCSend;
//TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc = &CryptoEncrypt2KTDEA_CBCSend;
memset(SessionIV, PICC_EMPTY_BYTE, sizeof(SessionIV));
SessionIVByteSize = CRYPTO_2KTDEA_KEY_SIZE;
break;
case DESFIRE_COMMS_CIPHERTEXT_DES:
TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateCRCA;
TransferState.Checksums.FinalFunc = &TransferChecksumFinalCRCA;
TransferState.Checksums.MACData.CRCA = ISO14443A_CRCA_INIT;
TransferState.ReadData.Encryption.Func = &TransferEncryptTDEASend;
//TransferState.ReadData.Encryption.Func = &TransferEncryptTDEASend;
memset(SessionIV, PICC_EMPTY_BYTE, sizeof(SessionIV));
SessionIVByteSize = CRYPTO_3KTDEA_KEY_SIZE;
break;
Expand All @@ -157,15 +157,15 @@ uint8_t WriteDataFilterSetup(uint8_t CommSettings) {
case DESFIRE_COMMS_PLAINTEXT_MAC:
TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateMACTDEA;
TransferState.Checksums.FinalFunc = &TransferChecksumFinalMACTDEA;
TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc = &CryptoEncrypt2KTDEA_CBCReceive;
//TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc = &CryptoEncrypt2KTDEA_CBCReceive;
memset(SessionIV, 0, sizeof(SessionIVByteSize));
SessionIVByteSize = CRYPTO_2KTDEA_KEY_SIZE;
break;
case DESFIRE_COMMS_CIPHERTEXT_DES:
TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateCRCA;
TransferState.Checksums.FinalFunc = &TransferChecksumFinalCRCA;
TransferState.Checksums.MACData.CRCA = ISO14443A_CRCA_INIT;
TransferState.WriteData.Encryption.Func = &TransferEncryptTDEAReceive;
//TransferState.WriteData.Encryption.Func = &TransferEncryptTDEAReceive;
memset(SessionIV, 0, sizeof(SessionIVByteSize));
SessionIVByteSize = CRYPTO_3KTDEA_KEY_SIZE;
break;
Expand Down
2 changes: 1 addition & 1 deletion Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c
};
AUTHENTICATE_LEGACY_CMD[5] = keyIndex;
if (PRINT_STATUS_EXCHANGE_MESSAGES) {
fprintf(stdout, ">>> Start Legacy 2K3DES (EEE/DDD) Authenticate:\n");
fprintf(stdout, ">>> Start Legacy 2K3DES Authenticate:\n");
fprintf(stdout, " -> ");
print_hex(AUTHENTICATE_LEGACY_CMD, sizeof(AUTHENTICATE_LEGACY_CMD));
}
Expand Down

0 comments on commit b6be1b8

Please sign in to comment.