Skip to content

Commit 74f216f

Browse files
committed
Preliminary (partial) support for more CommModes -- This is going to need substantial testing -- II
1 parent e9437a6 commit 74f216f

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Firmware/Chameleon-Mini/Application/CryptoCMAC.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ This notice must be retained at the top of all source files where indicated.
3737
#define CRYPTO_CMAC_RB128 ((uint8_t) 0x87)
3838

3939
bool appendBufferCMAC(uint8_t cryptoType, const uint8_t *keyData, uint8_t *bufferData, uint16_t bufferSize, uint8_t *IV);
40+
bool checkBufferMAC(uint8_t *bufferData, uint16_t bufferSize, uint16_t checksumSize);
4041

4142
uint16_t appendBufferMAC(const uint8_t *keyData, uint8_t *bufferData, uint16_t bufferSize);
43+
bool checkBufferCMAC(uint8_t *bufferData, uint16_t bufferSize, uint16_t checksumSize);
4244

4345
#endif

Firmware/Chameleon-Mini/Application/DESFire/DESFireUtils.c

+26-8
Original file line numberDiff line numberDiff line change
@@ -152,30 +152,48 @@ uint16_t DesfirePreprocessAPDU(uint8_t CommMode, uint8_t *Buffer, uint16_t Buffe
152152
// Remove the CRCA bytes at the end of the buffer:
153153
return MAX(0, BufferSize - 2);
154154
case DESFIRE_COMMS_PLAINTEXT_MAC: {
155-
// TODO: We are not checking the MAC/CMAC bytes for consistency yet ...
156155
uint16_t ChecksumBytes = 0;
157156
if (DesfireCommandState.CryptoMethodType == CRYPTO_TYPE_DES || DesfireCommandState.CryptoMethodType == CRYPTO_TYPE_2KTDEA) {
158157
ChecksumBytes = 4;
158+
if (!checkBufferMAC(Buffer, BufferSize, ChecksumBytes)) {
159+
return 0;
160+
}
159161
}
160162
else if (DesfireCommandState.CryptoMethodType == CRYPTO_TYPE_3K3DES) {
161163
ChecksumBytes = CRYPTO_3KTDEA_BLOCK_SIZE;
164+
if (!checkBufferCMAC(Buffer, BufferSize, ChecksumBytes)) {
165+
return 0;
166+
}
162167
} else {
163-
ChecksumBytes = CRYPTO_AES128_BLOCK_SIZE;
168+
ChecksumBytes = CRYPTO_AES_BLOCK_SIZE;
169+
if (!checkBufferCMAC(Buffer, BufferSize, ChecksumBytes)) {
170+
return 0;
171+
}
164172
}
165173
return MAX(0, BufferSize - ChecksumBytes);
166174
}
167175
case DESFIRE_COMMS_CIPHERTEXT_DES: {
168-
// TODO ...
169-
break;
176+
Decrypt3DESBuffer(BufferSize, Buffer, &Buffer[BufferSize], SessionIV, SessionKey);
177+
memmove(&Buffer[0], &Buffer[BufferSize], BufferSize);
178+
uint16_t ChecksumBytes = CRYPTO_3KTDEA_BLOCK_SIZE;
179+
if (!checkBufferCMAC(Buffer, BufferSize, ChecksumBytes)) {
180+
return 0;
181+
}
182+
return MAX(0, BufferSize - ChecksumBytes);
170183
}
171184
case DESFIRE_COMMS_CIPHERTEXT_AES128: {
172-
// TODO ...
173-
break;
185+
CryptoAESDecryptBuffer(BufferSize, Buffer, &Buffer[BufferSize], SessionIV, SessionKey);
186+
memmove(&Buffer[0], &Buffer[BufferSize], BufferSize);
187+
uint16_t ChecksumBytes = CRYPTO_AES_BLOCK_SIZE;
188+
if (!checkBufferCMAC(Buffer, BufferSize, ChecksumBytes)) {
189+
return 0;
190+
}
191+
return MAX(0, BufferSize - ChecksumBytes);
174192
}
175193
default:
176194
break;
177195
}
178-
return BufferSize;
196+
return 0;
179197
}
180198

181199
uint16_t DesfirePostprocessAPDU(uint8_t CommMode, uint8_t *Buffer, uint16_t BufferSize) {
@@ -224,7 +242,7 @@ uint16_t DesfirePostprocessAPDU(uint8_t CommMode, uint8_t *Buffer, uint16_t Buff
224242
default:
225243
break;
226244
}
227-
return BufferSize;
245+
return 0;
228246
}
229247

230248
#endif /* CONFIG_MF_DESFIRE_SUPPORT */

Firmware/Chameleon-Mini/Application/MifareDESFire.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void MifareDesfireAppTask(void) {
126126

127127
uint16_t MifareDesfireProcessCommand(uint8_t *Buffer, uint16_t ByteCount) {
128128
if (ByteCount == 0) {
129-
return ISO14443A_APP_NO_RESPONSE;//
129+
return ISO14443A_APP_NO_RESPONSE;
130130
} else if ((DesfireCmdCLA != DESFIRE_NATIVE_CLA) &&
131131
(DesfireCmdCLA != DESFIRE_ISO7816_CLA)) {
132132
return ISO14443A_APP_NO_RESPONSE;
@@ -174,7 +174,9 @@ uint16_t MifareDesfireProcessCommand(uint8_t *Buffer, uint16_t ByteCount) {
174174
uint16_t MifareDesfireProcess(uint8_t *Buffer, uint16_t BitCount) {
175175
size_t ByteCount = (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
176176
DesfireCmdCLA = Buffer[0];
177-
if ((ByteCount >= 8 && DesfireCLA(Buffer[0]) && Buffer[2] == 0x00 &&
177+
if (BitCount == 0) {
178+
return ISO14443A_APP_NO_RESPONSE;
179+
} else if ((ByteCount >= 8 && DesfireCLA(Buffer[0]) && Buffer[2] == 0x00 &&
178180
Buffer[3] == 0x00 && (Buffer[4] == ByteCount - 6 || Buffer[4] == ByteCount - 8)) || Iso7816CLA(DesfireCmdCLA)) {
179181
// Wrapped native command structure:
180182
/* Unwrap the PDU from ISO 7816-4 */

0 commit comments

Comments
 (0)