27 #ifndef __DESFIRE_CRYPTO_H__ 28 #define __DESFIRE_CRYPTO_H__ 30 #include "../../Common.h" 32 #include "DESFireFirmwareSettings.h" 42 #define DESFIRE_COMMS_PLAINTEXT (0x00) 43 #define DESFIRE_COMMS_PLAINTEXT_MAC (0x01) 44 #define DESFIRE_COMMS_CIPHERTEXT_DES (0x03) 45 #define DESFIRE_COMMS_CIPHERTEXT_AES128 (0x04) 46 #define DESFIRE_DEFAULT_COMMS_STANDARD (DESFIRE_COMMS_PLAINTEXT) 48 #define CRYPTO_TYPE_ANY (0x00) 49 #define CRYPTO_TYPE_DES (0x01) 50 #define CRYPTO_TYPE_2KTDEA (0x0A) 51 #define CRYPTO_TYPE_3K3DES (0x1A) 52 #define CRYPTO_TYPE_AES128 (0x4A) 54 #define CryptoTypeDES(ct) \ 55 ((ct == CRYPTO_TYPE_DES) || (ct == CRYPTO_TYPE_ANY)) 56 #define CryptoType2KTDEA(ct) \ 57 ((ct == CRYPTO_TYPE_2KTDEA) || (ct == CRYPTO_TYPE_ANY)) 58 #define CryptoType3KTDEA(ct) \ 59 ((ct == CRYPTO_TYPE_3K3DES) || (ct == CRYPTO_TYPE_ANY)) 60 #define CryptoTypeAES(ct) \ 61 ((ct == CRYPTO_TYPE_AES128) || (ct == CRYPTO_TYPE_ANY)) 64 #define CRYPTO_AES_KEY_SIZE (16) 65 #define CRYPTO_MAX_KEY_SIZE (24) 66 #define CRYPTO_MAX_BLOCK_SIZE (16) 67 #define DESFIRE_AES_IV_SIZE (CRYPTO_AES_BLOCK_SIZE) 68 #define DESFIRE_SESSION_KEY_SIZE (CRYPTO_3KTDEA_KEY_SIZE) 69 #define CRYPTO_CHALLENGE_RESPONSE_BYTES (8) 71 typedef BYTE CryptoKeyBufferType[CRYPTO_MAX_KEY_SIZE];
72 typedef BYTE CryptoIVBufferType[CRYPTO_MAX_BLOCK_SIZE];
74 extern CryptoKeyBufferType SessionKey;
75 extern CryptoIVBufferType SessionIV;
76 extern BYTE SessionIVByteSize;
78 extern uint8_t Authenticated;
79 extern uint8_t AuthenticatedWithKey;
80 extern uint8_t AuthenticatedWithPICCMasterKey;
81 extern uint8_t CryptoAuthMethod;
82 extern uint8_t ActiveCommMode;
89 void InvalidateAuthState(BYTE keepPICCAuthData);
90 bool IsAuthenticated(
void);
92 BYTE GetDefaultCryptoMethodKeySize(uint8_t cryptoType);
93 BYTE GetCryptoMethodCommSettings(uint8_t cryptoType);
94 const char *GetCryptoMethodDesc(uint8_t cryptoType);
95 const char *GetCommSettingsDesc(uint8_t cryptoType);
97 #define DESFIRE_MAC_LENGTH 4 98 #define DESFIRE_CMAC_LENGTH 8 // in bytes 101 #define DESFIRE_NOT_AUTHENTICATED 0xFF 103 typedef enum DESFIRE_FIRMWARE_ENUM_PACKING {
105 DESFIRE_AUTH_ISO_2KTDEA,
106 DESFIRE_AUTH_ISO_3KTDEA,
110 BYTE GetCryptoKeyTypeFromAuthenticateMethod(BYTE authCmdMethod);
112 #define CryptoBitsToBytes(cryptoBits) \ 113 (cryptoBits / BITS_PER_BYTE) 119 #include "../CryptoAES128.h" 121 typedef uint8_t DesfireAESCryptoKey[CRYPTO_AES_KEY_SIZE];
123 extern CryptoAESConfig_t AESCryptoContext;
124 extern DesfireAESCryptoKey AESCryptoSessionKey;
125 extern DesfireAESCryptoKey AESCryptoIVBuffer;
127 void InitAESCryptoKeyData(DesfireAESCryptoKey *cryptoKeyData);
129 typedef void (*CryptoAESCBCFuncType)(uint16_t,
void *,
void *, uint8_t *, uint8_t *);
131 typedef uint8_t (*CryptoTransferSendFunc)(uint8_t *, uint8_t);
132 typedef uint8_t (*CryptoTransferReceiveFunc)(uint8_t *, uint8_t);
133 uint8_t CryptoAESTransferEncryptSend(uint8_t *Buffer, uint8_t Count,
const uint8_t *Key);
134 uint8_t CryptoAESTransferEncryptReceive(uint8_t *Buffer, uint8_t Count,
const uint8_t *Key);
136 #define DESFIRE_MAX_PAYLOAD_AES_BLOCKS (DESFIRE_MAX_PAYLOAD_SIZE / CRYPTO_AES_BLOCK_SIZE) 142 #include "../CryptoTDEA.h" 144 #define DESFIRE_2KTDEA_NONCE_SIZE (CRYPTO_DES_BLOCK_SIZE) 145 #define DESFIRE_DES_IV_SIZE (CRYPTO_DES_BLOCK_SIZE) 146 #define DESFIRE_MAX_PAYLOAD_TDEA_BLOCKS (DESFIRE_MAX_PAYLOAD_SIZE / CRYPTO_DES_BLOCK_SIZE) 149 void TransferChecksumUpdateCRCA(
const uint8_t *Buffer, uint8_t Count);
150 uint8_t TransferChecksumFinalCRCA(uint8_t *Buffer);
151 void TransferChecksumUpdateMACTDEA(
const uint8_t *Buffer, uint8_t Count);
152 uint8_t TransferChecksumFinalMACTDEA(uint8_t *Buffer);