diff --git a/Firmware/Chameleon-Mini/Application/CryptoTDEA-HWAccelerated.S b/Firmware/Chameleon-Mini/Application/CryptoTDEA-HWAccelerated.S index 5d617819..5b0d91be 100644 --- a/Firmware/Chameleon-Mini/Application/CryptoTDEA-HWAccelerated.S +++ b/Firmware/Chameleon-Mini/Application/CryptoTDEA-HWAccelerated.S @@ -215,8 +215,8 @@ _CommonEpilogue: ; ; Returns: ; Nothing. -.global CryptoEncryptDEA -CryptoEncryptDEA: +.global CryptoEncryptDES +CryptoEncryptDES: ; Preserve the clobbered regs push r2 push r3 @@ -246,10 +246,10 @@ CryptoEncryptDEA: ld r1, Z+ ld r0, Z+ ; Encrypt - movw r16, r20 + movw r16, r20 rcall _EncryptDEA ; Store the ciphertext - movw r30, r22 + movw r30, r22 st Z+, r7 st Z+, r6 st Z+, r5 @@ -260,11 +260,12 @@ CryptoEncryptDEA: st Z+, r0 ; Restore clobbered regs - pop r17 - pop r16 + pop r17 + pop r16 ; Reuse epilogue code rjmp _CommonEpilogue + ; Input: ; R25:R24 - Pointer to plaintext input buffer ; R23:R22 - Pointer to ciphertext output buffer @@ -272,8 +273,8 @@ CryptoEncryptDEA: ; ; Returns: ; Nothing. -.global CryptoDecryptDEA -CryptoDecryptDEA: +.global CryptoDecryptDES +CryptoDecryptDES: ; Preserve the clobbered regs push r2 push r3 @@ -292,8 +293,8 @@ CryptoDecryptDEA: push r16 push r17 - ; Load the plaintext pointer to Z and fetch data - movw r30, r24 + ; Load the ciphertext pointer to Z and fetch data + movw r30, r22 ld r7, Z+ ld r6, Z+ ld r5, Z+ @@ -302,11 +303,11 @@ CryptoDecryptDEA: ld r2, Z+ ld r1, Z+ ld r0, Z+ - ; Encrypt + ; Decrypt movw r16, r20 rcall _DecryptDEA - ; Store the ciphertext - movw r30, r22 + ; Store the plaintext + movw r30, r24 st Z+, r7 st Z+, r6 st Z+, r5 @@ -401,7 +402,7 @@ CryptoDecrypt3KTDEA: push r16 push r17 - ; Load the plaintext pointer to Z and fetch data + ; Load the ciphertext pointer to Z and fetch data movw r30, r22 ld r7, Z+ ld r6, Z+ @@ -411,10 +412,10 @@ CryptoDecrypt3KTDEA: ld r2, Z+ ld r1, Z+ ld r0, Z+ - ; Encrypt + ; Decrypt movw r16, r20 rcall _Decrypt3KTDEA - ; Store the ciphertext + ; Store the plaintext movw r30, r24 st Z+, r7 st Z+, r6 @@ -460,7 +461,7 @@ CryptoEncrypt2KTDEA: push r16 push r17 - ; Load the plaintext pointer to Z and fetch data + ; Load the ciphertext pointer to Z and fetch data movw r30, r24 ld r7, Z+ ld r6, Z+ diff --git a/Firmware/Chameleon-Mini/Application/CryptoTDEA.S b/Firmware/Chameleon-Mini/Application/CryptoTDEA.S deleted file mode 100644 index 438844bb..00000000 --- a/Firmware/Chameleon-Mini/Application/CryptoTDEA.S +++ /dev/null @@ -1,659 +0,0 @@ -; -; DEA related code is kept in this file. -; All data is handled big-endian style (MSByte first) in memory. -; - -.section .text - -; This routine loads the key and performs 16 rounds of DEA. -; -; Input: -; R31:R30 - A pointer to the 8-byte key (with parity bits), MSB first. -; R7:R0 - 8-byte input data block, LSB in R0. -; SREG:H - Set to decipher, clear to encipher. -; -; Returns: -; R7:R0 - Result of en/deciphering, LSB in R0 -_LoadKeyAndRunDEA: - ld r15, Z+ - ld r14, Z+ - ld r13, Z+ - ld r12, Z+ - ld r11, Z+ - ld r10, Z+ - ld r9, Z+ - ld r8, Z+ - des 0 - des 1 - des 2 - des 3 - des 4 - des 5 - des 6 - des 7 - des 8 - des 9 - des 10 - des 11 - des 12 - des 13 - des 14 - des 15 - ret - -; -; Triple DEA subroutines -; - -; This routine performs Triple DEA encryption (E-D-E) using keying option 1: K1, K2, K3. -; -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_Encrypt3KTDEA: - ; Reload Z with the key block pointer - movw r30, r16 - ; Encipher - clh - rcall _LoadKeyAndRunDEA - - ; Z now points to K2 - ; Decipher - seh - rcall _LoadKeyAndRunDEA - - ; Z now points to K3 - ; Encipher - clh - rjmp _LoadKeyAndRunDEA - -; This routine performs Triple DEA encryption (E-D-E) using keying option 2: K1, K2, K1. -; -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_Encrypt2KTDEA: - ; Reload Z with the key block pointer - movw r30, r16 - ; Encipher - clh - rcall _LoadKeyAndRunDEA - - ; Z now points to K2 - ; Decipher - seh - rcall _LoadKeyAndRunDEA - - ; Reload Z with the key block pointer - movw r30, r16 - ; Encipher - clh - rjmp _LoadKeyAndRunDEA - - - -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_EncryptDEA: - ; Reload Z with the key block pointer - movw r30, r16 - ; Encipher - clh - rjmp _LoadKeyAndRunDEA - - -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_DecryptDEA: - ; Reload Z with the key block pointer - movw r30, r16 - ; Encipher - seh - rjmp _LoadKeyAndRunDEA - - - -; This routine performs Triple DEA decryption (D-E-D) using keying option 1: K1, K2, K3. -; -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_Decrypt3KTDEA: - ; Reload Z with the key block pointer and adjust to point to K3 - movw r30, r16 - adiw r30, 16 - ; Decipher - seh - rcall _LoadKeyAndRunDEA - - ; Reload Z with the key block pointer and adjust to point to K2 - movw r30, r16 - adiw r30, 8 - ; Encipher - clh - rcall _LoadKeyAndRunDEA - - ; Reload Z with the key block pointer - movw r30, r16 - ; Decipher - seh - rjmp _LoadKeyAndRunDEA - -; This routine performs Triple DEA decryption (D-E-D) using keying option 2: K1, K2, K1. -; -; Input: -; R17:R16 - Key block pointer. -; R7:R0 - Input data, LSB in R0 -; -; Returns: -; R7:R0 - Result of enciphering, LSB in R0 -_Decrypt2KTDEA: - ; Reload Z with the key block pointer - movw r30, r16 - ; Decipher - seh - rcall _LoadKeyAndRunDEA - - ; Z now points to K2 - ; Encipher - clh - rcall _LoadKeyAndRunDEA - - ; Reload Z with the key block pointer - movw r30, r16 - ; Decipher - seh - rjmp _LoadKeyAndRunDEA - -; -; Common prologue and epilogue code -; - -_CommonEpilogue: - pop r15 - pop r14 - pop r13 - pop r12 - pop r11 - pop r10 - pop r9 - pop r8 - pop r7 - pop r6 - pop r5 - pop r4 - pop r3 - pop r2 - eor r1, r1 - ret - -; -; Triple DEA ECB Routines -; - -; This routine performs Triple DEA encryption using keying option 2: K1, K2, K1. -; -; Input: -; R25:R24 - Pointer to plaintext output buffer -; R23:R22 - Pointer to ciphertext input buffer -; R21:R20 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoDecrypt2KTDEA -CryptoDecrypt2KTDEA: - ; Preserve the clobbered regs - push r2 - push r3 - push r4 - push r5 - push r6 - push r7 - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - push r16 - push r17 - - ; Load the plaintext pointer to Z and fetch data - movw r30, r22 - ld r7, Z+ - ld r6, Z+ - ld r5, Z+ - ld r4, Z+ - ld r3, Z+ - ld r2, Z+ - ld r1, Z+ - ld r0, Z+ - ; Encrypt - movw r16, r20 - rcall _Decrypt2KTDEA - ; Store the ciphertext - movw r30, r24 - st Z+, r7 - st Z+, r6 - st Z+, r5 - st Z+, r4 - st Z+, r3 - st Z+, r2 - st Z+, r1 - st Z+, r0 - - ; Restore clobbered regs - pop r17 - pop r16 - ; Reuse epilogue code - rjmp _CommonEpilogue - - -; This routine performs Triple DEA encryption using keying option 2: K1, K2, K1. -; -; Input: -; R25:R24 - Pointer to plaintext input buffer -; R23:R22 - Pointer to ciphertext output buffer -; R21:R20 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoEncrypt2KTDEA -CryptoEncrypt2KTDEA: - ; Preserve the clobbered regs - push r2 - push r3 - push r4 - push r5 - push r6 - push r7 - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - push r16 - push r17 - - ; Load the plaintext pointer to Z and fetch data - movw r30, r24 - ld r7, Z+ - ld r6, Z+ - ld r5, Z+ - ld r4, Z+ - ld r3, Z+ - ld r2, Z+ - ld r1, Z+ - ld r0, Z+ - ; Encrypt - movw r16, r20 - rcall _Encrypt2KTDEA - ; Store the ciphertext - movw r30, r22 - st Z+, r7 - st Z+, r6 - st Z+, r5 - st Z+, r4 - st Z+, r3 - st Z+, r2 - st Z+, r1 - st Z+, r0 - - ; Restore clobbered regs - pop r17 - pop r16 - ; Reuse epilogue code - rjmp _CommonEpilogue - -; -; Triple DEA CBC Routines -; - -; This routine performs the CBC "send" mode chaining: C = E(P ^ IV); IV = C -; -; Input: -; R31:R30 - Cryptographic primitive pointer -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -; -_DEACBCSend: - ; Preserve the clobbered regs - push r2 - push r3 - push r4 - push r5 - push r6 - push r7 - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - push r28 - push r29 - - ; Load the plaintext pointer to Y - movw r28, r22 - ; Load the ciphertext pointer to X - movw r26, r20 - ; Store the crypto primitive pointer in r23:r22 - movw r22, r30 - - ; Load the IV pointer to Z - movw r30, r18 - ; Load the IV - ld r7, Z+ - ld r6, Z+ - ld r5, Z+ - ld r4, Z+ - ld r3, Z+ - ld r2, Z+ - ld r1, Z+ - ld r0, Z+ - -1: - ; Load the plaintext block - ld r15, Y+ - ld r14, Y+ - ld r13, Y+ - ld r12, Y+ - ld r11, Y+ - ld r10, Y+ - ld r9, Y+ - ld r8, Y+ - ; XOR the plaintext with the IV - eor r7, r15 - eor r6, r14 - eor r5, r13 - eor r4, r12 - eor r3, r11 - eor r2, r10 - eor r1, r9 - eor r0, r8 - - ; Call the primitive - movw r30, r22 - icall - - ; Store the ciphertext - ; It will be reused as the IV for the next block, if any - st X+, r7 - st X+, r6 - st X+, r5 - st X+, r4 - st X+, r3 - st X+, r2 - st X+, r1 - st X+, r0 - - ; Decrement the counter, repeat if more blocks. - sbiw r24, 1 - brne 1b - - ; Load the IV pointer to X - movw r26, r18 - ; Store the updated IV - st X+, r7 - st X+, r6 - st X+, r5 - st X+, r4 - st X+, r3 - st X+, r2 - st X+, r1 - st X+, r0 - - ; Restore clobbered regs - pop r29 - pop r28 - rjmp _CommonEpilogue - -; This routine performs the CBC "receive" mode chaining: C = E(P) ^ IV; IV = P -; -; Input: -; R31:R30 - Cryptographic primitive pointer -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -; -_DEACBCReceive: - ; Preserve the clobbered regs - push r2 - push r3 - push r4 - push r5 - push r6 - push r7 - push r8 - push r9 - push r10 - push r11 - push r12 - push r13 - push r14 - push r15 - push r28 - push r29 - - ; Load the plaintext pointer to Y - movw r28, r22 - ; Load the ciphertext pointer to X - movw r26, r20 - ; Store the crypto primitive pointer in r23:r22 - movw r22, r30 - -1: - ; Load the plaintext block - ldd r7, Y+0 - ldd r6, Y+1 - ldd r5, Y+2 - ldd r4, Y+3 - ldd r3, Y+4 - ldd r2, Y+5 - ldd r1, Y+6 - ldd r0, Y+7 - - ; Call the primitive - movw r30, r22 - icall - - ; Load the IV block - movw r30, r18 - ld r15, Z+ - ld r14, Z+ - ld r13, Z+ - ld r12, Z+ - ld r11, Z+ - ld r10, Z+ - ld r9, Z+ - ld r8, Z+ - ; XOR the ciphertext with the IV - eor r7, r15 - eor r6, r14 - eor r5, r13 - eor r4, r12 - eor r3, r11 - eor r2, r10 - eor r1, r9 - eor r0, r8 - ; Reload the plaintext block - ld r15, Y+ - ld r14, Y+ - ld r13, Y+ - ld r12, Y+ - ld r11, Y+ - ld r10, Y+ - ld r9, Y+ - ld r8, Y+ - ; Store the new IV before it gets potentially overwritten - movw r30, r18 - st Z+, r15 - st Z+, r14 - st Z+, r13 - st Z+, r12 - st Z+, r11 - st Z+, r10 - st Z+, r9 - st Z+, r8 - ; Store the ciphertext - st X+, r7 - st X+, r6 - st X+, r5 - st X+, r4 - st X+, r3 - st X+, r2 - st X+, r1 - st X+, r0 - - ; Decrement the counter, repeat if more blocks. - sbiw r24, 1 - brne 1b - - ; Restore clobbered regs - pop r29 - pop r28 - rjmp _CommonEpilogue - - -; This routine performs Triple DEA encryption in CBC mode using keying option 2: K1, K2, K1. -; The CBC is operated in the "send" mode: C = E(P ^ IV); IV = C -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoEncrypt2KTDEA_CBCSend -CryptoEncrypt2KTDEA_CBCSend: - ldi r31, pm_hi8(_Encrypt2KTDEA) - ldi r30, pm_lo8(_Encrypt2KTDEA) - rjmp _DEACBCSend - -; This routine performs Triple DEA encryption in CBC mode using keying option 2: K1, K2, K1. -; The CBC is operated in the "receive" mode: C = E(P) ^ IV; IV = P -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoEncrypt2KTDEA_CBCReceive -CryptoEncrypt2KTDEA_CBCReceive: - ldi r31, pm_hi8(_Encrypt2KTDEA) - ldi r30, pm_lo8(_Encrypt2KTDEA) - rjmp _DEACBCReceive - -; This routine performs Triple DEA decryption in CBC mode using keying option 2: K1, K2, K1. -; The CBC is operated in the "send" mode: C = E(P ^ IV); IV = C -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoDecrypt2KTDEA_CBCSend -CryptoDecrypt2KTDEA_CBCSend: - ldi r31, pm_hi8(_Decrypt2KTDEA) - ldi r30, pm_lo8(_Decrypt2KTDEA) - rjmp _DEACBCSend - -; This routine performs Triple DEA decryption in CBC mode using keying option 2: K1, K2, K1. -; The CBC is operated in the "receive" mode: C = E(P) ^ IV; IV = P -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoDecrypt2KTDEA_CBCReceive -CryptoDecrypt2KTDEA_CBCReceive: - ldi r31, pm_hi8(_Decrypt2KTDEA) - ldi r30, pm_lo8(_Decrypt2KTDEA) - rjmp _DEACBCReceive - -; This routine performs Triple DEA encryption in CBC mode using keying option 1: K1, K2, K3. -; The CBC is operated in the "send" mode: C = E(P ^ IV); IV = C -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoEncrypt3KTDEA_CBCSend -CryptoEncrypt3KTDEA_CBCSend: - ldi r31, pm_hi8(_Encrypt3KTDEA) - ldi r30, pm_lo8(_Encrypt3KTDEA) - rjmp _DEACBCSend - -; This routine performs Triple DEA decryption in CBC mode using keying option 1: K1, K2, K3. -; The CBC is operated in the "receive" mode: C = E(P) ^ IV; IV = P -; -; Input: -; R25:R24 - Count of blocks. -; R23:R22 - Pointer to plaintext input buffer -; R21:R20 - Pointer to ciphertext output buffer -; R19:R18 - IV block pointer. -; R17:R16 - Key block pointer. -; -; Returns: -; Nothing. -.global CryptoEncrypt3KTDEA_CBCReceive -CryptoEncrypt3KTDEA_CBCReceive: - ldi r31, pm_hi8(_Decrypt3KTDEA) - ldi r30, pm_lo8(_Decrypt3KTDEA) - rjmp _DEACBCReceive diff --git a/Firmware/Chameleon-Mini/Application/CryptoTDEA.c b/Firmware/Chameleon-Mini/Application/CryptoTDEA.c index 99a9135b..e31dc688 100644 --- a/Firmware/Chameleon-Mini/Application/CryptoTDEA.c +++ b/Firmware/Chameleon-Mini/Application/CryptoTDEA.c @@ -118,7 +118,7 @@ static int CryptoDecryptCBCBuffer(CryptoTDEA_CBCSpec *CryptoSpec, uint16_t Count int EncryptDESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext, const uint8_t *IVIn, const uint8_t *Keys) { CryptoTDEA_CBCSpec CryptoSpec = { - .cryptFunc = &CryptoEncryptDEA, + .cryptFunc = &CryptoEncryptDES, .blockSize = CRYPTO_DES_BLOCK_SIZE }; return CryptoEncryptCBCBuffer(&CryptoSpec, Count, Plaintext, Ciphertext, IVIn, Keys); @@ -126,7 +126,7 @@ int EncryptDESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext, co int DecryptDESBuffer(uint16_t Count, void *Plaintext, const void *Ciphertext, const uint8_t *IVIn, const uint8_t *Keys) { CryptoTDEA_CBCSpec CryptoSpec = { - .cryptFunc = &CryptoDecryptDEA, + .cryptFunc = &CryptoDecryptDES, .blockSize = CRYPTO_DES_BLOCK_SIZE }; return CryptoDecryptCBCBuffer(&CryptoSpec, Count, Plaintext, Ciphertext, IVIn, Keys); diff --git a/Firmware/Chameleon-Mini/Application/CryptoTDEA.h b/Firmware/Chameleon-Mini/Application/CryptoTDEA.h index 8d87442b..60ecfd2c 100644 --- a/Firmware/Chameleon-Mini/Application/CryptoTDEA.h +++ b/Firmware/Chameleon-Mini/Application/CryptoTDEA.h @@ -53,8 +53,8 @@ typedef uint8_t Crypto3KTDEAKeyType[CRYPTO_3KTDEA_KEY_SIZE]; typedef void (*CryptoTDEACBCFuncType)(uint16_t Count, const void *Plaintext, void *Ciphertext, void *IV, const uint8_t *Keys); typedef void (*CryptoTDEAFuncType)(const void *PlainText, void *Ciphertext, const uint8_t *Keys); -void CryptoEncryptDEA(void *Plaintext, void *Ciphertext, const uint8_t *Keys); -void CryptoDecryptDEA(void *Plaintext, void *Ciphertext, const uint8_t *Keys); +void CryptoEncryptDES(void *Plaintext, void *Ciphertext, const uint8_t *Keys); +void CryptoDecryptDES(void *Plaintext, void *Ciphertext, const uint8_t *Keys); int EncryptDESBuffer(uint16_t Count, const void *Plaintext, void *Ciphertext, const uint8_t *IV, const uint8_t *Keys); int DecryptDESBuffer(uint16_t Count, void *Plaintext, const void *Ciphertext, const uint8_t *IV, const uint8_t *Keys); diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c b/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c index 27a57502..4f3ce1ec 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.c @@ -153,151 +153,4 @@ 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]; - uint16_t tempBufSize = (DESFIRE_MAX_PAYLOAD_AES_BLOCKS + 1) * CRYPTO_DES_BLOCK_SIZE; - uint16_t bufFillSize = MIN(tempBufSize, AvailablePlaintext), bufFillSize2; - uint8_t *tempBufOffset; - if (AvailablePlaintext) { - /* Fill the partial block */ - memcpy(&TempBuffer[0], &TransferState.BlockBuffer[0], bufFillSize); - } - /* Copy fresh plaintext to the temp buffer */ - if (Count > bufFillSize && tempBufSize - bufFillSize > 0) { - tempBufOffset = &TempBuffer[bufFillSize]; - bufFillSize2 = bufFillSize; - bufFillSize = MIN(Count, tempBufSize - bufFillSize); - memcpy(tempBufOffset, Buffer, bufFillSize); - Count += bufFillSize2 + Count - bufFillSize; - } - uint8_t BlockCount = Count / CRYPTO_AES_BLOCK_SIZE; - /* Stash extra plaintext for later */ - AvailablePlaintext = Count - BlockCount * CRYPTO_AES_BLOCK_SIZE; - if (AvailablePlaintext) { - memcpy(&TransferState.BlockBuffer[0], - &Buffer[BlockCount * CRYPTO_AES_BLOCK_SIZE], AvailablePlaintext); - } - TransferState.ReadData.Encryption.AvailablePlaintext = AvailablePlaintext; - /* Encrypt complete blocks in the buffer */ - uint8_t zeroIV[CRYPTO_AES_BLOCK_SIZE]; - memset(zeroIV, 0x00, CRYPTO_AES_BLOCK_SIZE); - CryptoAESEncrypt_CBCSend(BlockCount, &TempBuffer[0], &Buffer[0], - *Key, zeroIV); - /* Return byte count to transfer */ - return BlockCount * CRYPTO_AES_BLOCK_SIZE; -} - -uint8_t CryptoAESTransferEncryptReceive(uint8_t *Buffer, uint8_t Count, const uint8_t *Key) { - DesfireLogEntry(LOG_INFO_DESFIRE_INCOMING_DATA_ENC, Buffer, Count); - return STATUS_OPERATION_OK; -} -#endif - -/* Checksum routines */ - -void TransferChecksumUpdateCRCA(const uint8_t *Buffer, uint8_t Count) { - TransferState.Checksums.MACData.CRCA = - ISO14443AUpdateCRCA(Buffer, Count, TransferState.Checksums.MACData.CRCA); -} - -uint8_t TransferChecksumFinalCRCA(uint8_t *Buffer) { - /* Copy the checksum to destination */ - memcpy(Buffer, &TransferState.Checksums.MACData.CRCA, 2); - /* Return the checksum size */ - return 2; -} - -void TransferChecksumUpdateMACTDEA(const uint8_t *Buffer, uint8_t Count) { - uint8_t AvailablePlaintext = TransferState.Checksums.AvailablePlaintext; - uint8_t TempBuffer[CRYPTO_DES_BLOCK_SIZE]; - - if (AvailablePlaintext) { - uint8_t TempBytes; - /* Fill the partial block */ - TempBytes = CRYPTO_DES_BLOCK_SIZE - AvailablePlaintext; - if (TempBytes > Count) - TempBytes = Count; - memcpy(&TransferState.BlockBuffer[AvailablePlaintext], &Buffer[0], TempBytes); - Count -= TempBytes; - Buffer += TempBytes; - /* MAC the partial block */ - TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc(1, &TransferState.BlockBuffer[0], - &TempBuffer[0], SessionIV, SessionKey); - } - /* MAC complete blocks in the buffer */ - while (Count >= CRYPTO_DES_BLOCK_SIZE) { - /* NOTE: This is block-by-block, hence slow. - * See if it's better to just allocate a temp buffer large enough (64 bytes). */ - TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc(1, &Buffer[0], &TempBuffer[0], - SessionIV, SessionKey); - Count -= CRYPTO_DES_BLOCK_SIZE; - Buffer += CRYPTO_DES_BLOCK_SIZE; - } - /* Copy the new partial block */ - if (Count) { - memcpy(&TransferState.BlockBuffer[0], &Buffer[0], Count); - } - TransferState.Checksums.AvailablePlaintext = Count; -} - -uint8_t TransferChecksumFinalMACTDEA(uint8_t *Buffer) { - uint8_t AvailablePlaintext = TransferState.Checksums.AvailablePlaintext; - uint8_t TempBuffer[CRYPTO_DES_BLOCK_SIZE]; - - if (AvailablePlaintext) { - /* Apply padding */ - CryptoPaddingTDEA(&TransferState.BlockBuffer[0], AvailablePlaintext, false); - /* MAC the partial block */ - TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc(1, &TransferState.BlockBuffer[0], - &TempBuffer[0], SessionIV, SessionKey); - TransferState.Checksums.AvailablePlaintext = 0; - } - /* Copy the checksum to destination */ - memcpy(Buffer, SessionIV, 4); - /* Return the checksum size */ - return 4; -} - -void TransferChecksumUpdateCMAC(const uint8_t *Buffer, uint8_t Count) {} // TODO - -uint8_t TransferChecksumFinalCMAC(uint8_t *Buffer) { - return 0x00; // TODO -} - -/* Encryption routines */ - -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]; - uint8_t BlockCount; - - if (AvailablePlaintext) { - /* Fill the partial block */ - memcpy(&TempBuffer[0], &TransferState.BlockBuffer[0], AvailablePlaintext); - } - /* Copy fresh plaintext to the temp buffer */ - memcpy(&TempBuffer[AvailablePlaintext], Buffer, Count); - Count += AvailablePlaintext; - BlockCount = Count / CRYPTO_DES_BLOCK_SIZE; - /* Stash extra plaintext for later */ - AvailablePlaintext = Count - BlockCount * CRYPTO_DES_BLOCK_SIZE; - if (AvailablePlaintext) { - memcpy(&TransferState.BlockBuffer[0], - &Buffer[BlockCount * CRYPTO_DES_BLOCK_SIZE], AvailablePlaintext); - } - TransferState.ReadData.Encryption.AvailablePlaintext = AvailablePlaintext; - /* Encrypt complete blocks in the buffer */ - CryptoEncrypt2KTDEA_CBCSend(BlockCount, &TempBuffer[0], &Buffer[0], - SessionIV, SessionKey); - /* Return byte count to transfer */ - return BlockCount * CRYPTO_DES_BLOCK_SIZE; -} - -uint8_t TransferEncryptTDEAReceive(uint8_t *Buffer, uint8_t Count) { - DesfireLogEntry(LOG_INFO_DESFIRE_INCOMING_DATA_ENC, Buffer, Count); - return 0; -} - #endif /* CONFIG_MF_DESFIRE_SUPPORT */ diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h b/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h index 09f3f459..3cd5d524 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h @@ -128,11 +128,6 @@ 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) /********************************************************* @@ -140,20 +135,10 @@ uint8_t CryptoAESTransferEncryptReceive(uint8_t *Buffer, uint8_t Count, const ui *********************************************************/ #include "../CryptoTDEA.h" +#include "../CryptoCMAC.h" #define DESFIRE_2KTDEA_NONCE_SIZE (CRYPTO_DES_BLOCK_SIZE) #define DESFIRE_DES_IV_SIZE (CRYPTO_DES_BLOCK_SIZE) #define DESFIRE_MAX_PAYLOAD_TDEA_BLOCKS (DESFIRE_MAX_PAYLOAD_SIZE / CRYPTO_DES_BLOCK_SIZE) -/* Checksum routines: */ -void TransferChecksumUpdateCRCA(const uint8_t *Buffer, uint8_t Count); -uint8_t TransferChecksumFinalCRCA(uint8_t *Buffer); -void TransferChecksumUpdateMACTDEA(const uint8_t *Buffer, uint8_t Count); -uint8_t TransferChecksumFinalMACTDEA(uint8_t *Buffer); - -#include "../CryptoCMAC.h" - -void TransferChecksumUpdateCMAC(const uint8_t *Buffer, uint8_t Count); -uint8_t TransferChecksumFinalCMAC(uint8_t *Buffer); - #endif diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFireFile.c b/Firmware/Chameleon-Mini/Application/DESFire/DESFireFile.c index b0478fa7..3a4d37e6 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFireFile.c +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFireFile.c @@ -267,10 +267,8 @@ uint8_t ReadDataFileSetup(uint8_t FileIndex, uint8_t CommSettings, uint16_t Offs /* Setup data source */ TransferState.ReadData.Source.Func = &ReadDataEEPROMSource; if (Length == 0) { - TransferState.ReadData.Encryption.FirstPaddingBitSet = true; TransferState.ReadData.BytesLeft = fileSize - Offset; } else { - TransferState.ReadData.Encryption.FirstPaddingBitSet = false; TransferState.ReadData.BytesLeft = Length; } /* Clean data is always located in the beginning of data area */ diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c b/Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c index 42823450..2fd4a6a5 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFireInstructions.c @@ -482,7 +482,6 @@ uint16_t EV0CmdAuthenticateLegacy1(uint8_t *Buffer, uint16_t ByteCount) { keySize = GetDefaultCryptoMethodKeySize(CRYPTO_TYPE_DES); DesfireCommandState.KeyId = KeyId; DesfireCommandState.CryptoMethodType = CRYPTO_TYPE_DES; - DesfireCommandState.ActiveCommMode = GetCryptoMethodCommSettings(CRYPTO_TYPE_DES); CryptoChallengeResponseSize = CRYPTO_DES_BLOCK_SIZE; /* Fetch the key */ @@ -1740,21 +1739,15 @@ uint16_t DesfireCmdAuthenticate3KTDEA1(uint8_t *Buffer, uint16_t ByteCount) { if (cryptoKeyType == CRYPTO_TYPE_ANY || cryptoKeyType == CRYPTO_TYPE_3K3DES) { keySize = GetDefaultCryptoMethodKeySize(CRYPTO_TYPE_3K3DES); DesfireCommandState.CryptoMethodType = CRYPTO_TYPE_3K3DES; - DesfireCommandState.ActiveCommMode = GetCryptoMethodCommSettings(CRYPTO_TYPE_3K3DES); - CryptoChallengeResponseSize = CRYPTO_CHALLENGE_RESPONSE_BYTES; } else if (cryptoKeyType == CRYPTO_TYPE_AES128) { return DesfireCmdAuthenticateAES1(Buffer, ByteCount); } else if (cryptoKeyType == CRYPTO_TYPE_DES) { - keySize = GetDefaultCryptoMethodKeySize(CRYPTO_TYPE_DES); - DesfireCommandState.CryptoMethodType = CRYPTO_TYPE_DES; - DesfireCommandState.ActiveCommMode = GetCryptoMethodCommSettings(CRYPTO_TYPE_DES); - CryptoChallengeResponseSize = CRYPTO_DES_BLOCK_SIZE; + return EV0CmdAuthenticateLegacy1(Buffer, ByteCount); } else { keySize = GetDefaultCryptoMethodKeySize(CRYPTO_TYPE_2KTDEA); DesfireCommandState.CryptoMethodType = CRYPTO_TYPE_2KTDEA; - DesfireCommandState.ActiveCommMode = GetCryptoMethodCommSettings(CRYPTO_TYPE_2KTDEA); - CryptoChallengeResponseSize = CRYPTO_DES_BLOCK_SIZE; } + CryptoChallengeResponseSize = CRYPTO_CHALLENGE_RESPONSE_BYTES; /* Fetch the key */ ReadAppKey(SelectedApp.Slot, KeyId, Key, keySize); @@ -1813,13 +1806,7 @@ uint16_t DesfireCmdAuthenticate3KTDEA2(uint8_t *Buffer, uint16_t ByteCount) { BYTE *Key, *IV; cryptoKeyType = DesfireCommandState.CryptoMethodType; - if (cryptoKeyType == CRYPTO_TYPE_ANY || cryptoKeyType == CRYPTO_TYPE_3K3DES) { - CryptoChallengeResponseSize = CRYPTO_CHALLENGE_RESPONSE_BYTES; - } else if (cryptoKeyType == CRYPTO_TYPE_DES) { - CryptoChallengeResponseSize = CRYPTO_DES_BLOCK_SIZE; - } else { - CryptoChallengeResponseSize = CRYPTO_DES_BLOCK_SIZE; - } + CryptoChallengeResponseSize = CRYPTO_CHALLENGE_RESPONSE_BYTES; /* Set status for the next incoming command on error */ DesfireState = DESFIRE_IDLE; @@ -1942,7 +1929,6 @@ uint16_t DesfireCmdAuthenticateAES1(uint8_t *Buffer, uint16_t ByteCount) { /* Indicate that we are in AES key authentication land */ DesfireCommandState.KeyId = KeyId; DesfireCommandState.CryptoMethodType = CRYPTO_TYPE_AES128; - DesfireCommandState.ActiveCommMode = GetCryptoMethodCommSettings(CRYPTO_TYPE_AES128); /* Fetch the key */ ReadAppKey(SelectedApp.Slot, KeyId, Key, keySize); diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.c b/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.c index 5dda280e..868d3b11 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.c +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.c @@ -82,7 +82,6 @@ void SynchronizePICCInfo(void) { WriteBlockBytes(&Picc, DESFIRE_PICC_INFO_BLOCK_ID, sizeof(DESFirePICCInfoType)); } -/* TODO: Currently, everything is transfered in plaintext, without checksums */ TransferStatus PiccToPcdTransfer(uint8_t *Buffer) { TransferStatus Status; uint8_t XferBytes; @@ -95,22 +94,10 @@ TransferStatus PiccToPcdTransfer(uint8_t *Buffer) { /* Read input bytes */ TransferState.ReadData.Source.Func(Buffer, XferBytes); TransferState.ReadData.BytesLeft -= XferBytes; - /* Update checksum/MAC */ - //if (TransferState.Checksums.UpdateFunc) - // TransferState.Checksums.UpdateFunc(Buffer, XferBytes); - //if (TransferState.ReadData.BytesLeft == 0) { - // /* Finalise TransferChecksum and append the checksum */ - // if (TransferState.Checksums.FinalFunc) - // XferBytes += TransferState.Checksums.FinalFunc(&Buffer[XferBytes]); - //} - /* Encrypt */ - //Status.BytesProcessed = TransferState.ReadData.Encryption.Func(Buffer, XferBytes); - Status.IsComplete = TransferState.ReadData.Encryption.AvailablePlaintext == 0; Status.BytesProcessed = XferBytes; Status.IsComplete = TransferState.ReadData.BytesLeft == 0; } else { /* Final encryption block */ - //Status.BytesProcessed = TransferState.ReadData.Encryption.Func(Buffer, 0); Status.IsComplete = true; Status.BytesProcessed = 0; Status.IsComplete = true; @@ -118,39 +105,24 @@ TransferStatus PiccToPcdTransfer(uint8_t *Buffer) { return Status; } -/* TODO: Currently, everything is transfered in plaintext, without checksums */ uint8_t PcdToPiccTransfer(uint8_t *Buffer, uint8_t Count) { TransferState.WriteData.Sink.Func(Buffer, Count); return STATUS_OPERATION_OK; } -/* Setup routines */ - uint8_t ReadDataFilterSetup(uint8_t CommSettings) { switch (CommSettings) { case DESFIRE_COMMS_PLAINTEXT: break; case DESFIRE_COMMS_PLAINTEXT_MAC: - TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateMACTDEA; - TransferState.Checksums.FinalFunc = &TransferChecksumFinalMACTDEA; - 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; memset(SessionIV, PICC_EMPTY_BYTE, sizeof(SessionIV)); SessionIVByteSize = CRYPTO_3KTDEA_KEY_SIZE; break; case DESFIRE_COMMS_CIPHERTEXT_AES128: - /* A.k.a., CommMode=FULL from NXP application note AN12343: */ - TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateCMAC; - TransferState.Checksums.FinalFunc = &TransferChecksumFinalCMAC; - TransferState.Checksums.MACData.CRCA = ISO14443A_CRCA_INIT; // TODO ??? - TransferState.WriteData.Encryption.Func = &CryptoAESEncrypt_CBCSend; memset(SessionIV, 0, sizeof(SessionIVByteSize)); SessionIVByteSize = CRYPTO_AES_KEY_SIZE; default: @@ -162,33 +134,18 @@ uint8_t ReadDataFilterSetup(uint8_t CommSettings) { uint8_t WriteDataFilterSetup(uint8_t CommSettings) { switch (CommSettings) { case DESFIRE_COMMS_PLAINTEXT: - TransferState.Checksums.UpdateFunc = NULL; - TransferState.Checksums.FinalFunc = NULL; - TransferState.Checksums.MACData.CryptoChecksumFunc.TDEAFunc = NULL; memset(SessionIV, 0, sizeof(SessionIVByteSize)); SessionIVByteSize = 0; break; case DESFIRE_COMMS_PLAINTEXT_MAC: - TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateMACTDEA; - TransferState.Checksums.FinalFunc = &TransferChecksumFinalMACTDEA; - 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; memset(SessionIV, 0, sizeof(SessionIVByteSize)); SessionIVByteSize = CRYPTO_AES_KEY_SIZE; break; case DESFIRE_COMMS_CIPHERTEXT_AES128: - // A.k.a., CommMode=FULL from NXP application note AN12343: - TransferState.Checksums.UpdateFunc = &TransferChecksumUpdateCMAC; - TransferState.Checksums.FinalFunc = &TransferChecksumFinalCMAC; - TransferState.Checksums.MACData.CRCA = ISO14443A_CRCA_INIT; // TODO ??? - TransferState.WriteData.Encryption.Func = &CryptoAESEncrypt_CBCReceive; memset(SessionIV, 0, sizeof(SessionIVByteSize)); SessionIVByteSize = CRYPTO_AES_KEY_SIZE; break; diff --git a/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.h b/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.h index 919e4d5d..45fc6f71 100644 --- a/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.h +++ b/Firmware/Chameleon-Mini/Application/DESFire/DESFirePICCControl.h @@ -50,8 +50,6 @@ extern SelectedFileCacheType SelectedFile; typedef void (*TransferSourceFuncType)(BYTE *Buffer, BYTE Count); typedef void (*TransferSinkFuncType)(BYTE *Buffer, BYTE Count); -typedef void (*TransferChecksumUpdateFuncType)(const BYTE *Buffer, BYTE Count); -typedef BYTE(*TransferChecksumFinalFuncType)(BYTE *Buffer); typedef BYTE(*TransferEncryptFuncType)(BYTE *Buffer, BYTE Count); typedef TransferStatus(*PiccToPcdTransferFilterFuncType)(BYTE *Buffer); typedef BYTE(*PcdToPiccTransferFilterFuncType)(BYTE *Buffer, BYTE Count); @@ -62,33 +60,12 @@ typedef union DESFIRE_FIRMWARE_PACKING { BYTE NextIndex; } GetApplicationIds; BYTE BlockBuffer[CRYPTO_MAX_BLOCK_SIZE]; - struct DESFIRE_FIRMWARE_ALIGNAT { - TransferChecksumUpdateFuncType UpdateFunc; - TransferChecksumFinalFuncType FinalFunc; - BYTE AvailablePlaintext; - struct DESFIRE_FIRMWARE_ALIGNAT { - union DESFIRE_FIRMWARE_ALIGNAT { - CryptoAESCBCFuncType AESFunc; - CryptoTDEACBCFuncType TDEAFunc; - } CryptoChecksumFunc; - union { - SIZET CRCA; - UINT CRC32; - BYTE CMAC[DESFIRE_CMAC_LENGTH]; - }; - } MACData; - } Checksums; struct DESFIRE_FIRMWARE_ALIGNAT { SIZET BytesLeft; struct DESFIRE_FIRMWARE_ALIGNAT { TransferSourceFuncType Func; SIZET Pointer; /* in FRAM */ } Source; - struct DESFIRE_FIRMWARE_ALIGNAT { - BOOL FirstPaddingBitSet; - TransferEncryptFuncType Func; - BYTE AvailablePlaintext; - } Encryption; } ReadData; struct DESFIRE_FIRMWARE_ALIGNAT { SIZET BytesLeft; @@ -96,10 +73,6 @@ typedef union DESFIRE_FIRMWARE_PACKING { TransferSinkFuncType Func; SIZET Pointer; /* in FRAM */ } Sink; - struct DESFIRE_FIRMWARE_ALIGNAT { - TransferEncryptFuncType Func; - BYTE AvailablePlaintext; - } Encryption; } WriteData; } TransferStateType; extern TransferStateType TransferState; diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index ce5eff7a..484dc654 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -397,7 +397,7 @@ local-clean: @mkdir -p $(OBJDIR) clean: local-clean -git-add-dev: LOCALFW_SOURCE_FILES:=Makefile ./*.{c,h} ./*/*.{c,h} ./*/*/*.{c,h} +git-add-dev: LOCALFW_SOURCE_FILES:=Makefile ./*.{c,h,S} ./*/*.{c,h} ./Application/*.S ./*/*/*.{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: diff --git a/Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h b/Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h index bd89a4d0..1c32dbc7 100644 --- a/Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h +++ b/Software/DESFireLibNFCTesting/LocalInclude/DesfireUtils.h @@ -172,7 +172,7 @@ static inline int AuthenticateISO(nfc_device *nfcConnDev, uint8_t keyIndex, cons fprintf(stdout, " -> "); print_hex(AUTHENTICATE_ISO_CMD, sizeof(AUTHENTICATE_ISO_CMD)); fprintf(stdout, " -- IV = "); - print_hex(IVBuf, AES128_BLOCK_SIZE); + print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE); } RxData_t *rxDataStorage = InitRxDataStruct(MAX_FRAME_LENGTH); bool rxDataStatus = false; @@ -226,7 +226,7 @@ static inline int AuthenticateISO(nfc_device *nfcConnDev, uint8_t keyIndex, cons fprintf(stdout, " -> "); print_hex(sendBytesBuf, sizeof(sendBytesBuf)); fprintf(stdout, " -- IV = "); - print_hex(desCryptoData.ivData, AES128_BLOCK_SIZE); + print_hex(desCryptoData.ivData, CRYPTO_3KTDEA_BLOCK_SIZE); } rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, sizeof(sendBytesBuf), rxDataStorage); if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) { @@ -300,7 +300,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c fprintf(stdout, " -> "); print_hex(AUTHENTICATE_LEGACY_CMD, sizeof(AUTHENTICATE_LEGACY_CMD)); fprintf(stdout, " -- IV = "); - print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE); + print_hex(IVBuf, CRYPTO_DES_BLOCK_SIZE); } RxData_t *rxDataStorage = InitRxDataStruct(MAX_FRAME_LENGTH); bool rxDataStatus = false; @@ -326,7 +326,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c DecryptDES(encryptedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY, plainTextRndB, IVBuf, desCryptoData); if (PRINT_STATUS_EXCHANGE_MESSAGES) { fprintf(stdout, " -- IV = "); - print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE); + print_hex(IVBuf, CRYPTO_DES_BLOCK_SIZE); } RotateArrayRight(plainTextRndB, rotatedRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); desCryptoData.ivData = IVBuf; @@ -344,17 +344,17 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c if (PRINT_STATUS_EXCHANGE_MESSAGES) { fprintf(stdout, " -- RNDA = "); - print_hex(rndA, CRYPTO_CHALLENGE_RESPONSE_SIZE); + print_hex(rndA, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); fprintf(stdout, " -- RNDB = "); - print_hex(plainTextRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE); + print_hex(plainTextRndB, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); fprintf(stdout, " -- CHAL = "); - print_hex(challengeResponse, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE); + print_hex(challengeResponse, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); fprintf(stdout, " -- ENC-CHAL = "); - print_hex(challengeResponseCipherText, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE); + print_hex(challengeResponseCipherText, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); fprintf(stdout, " -> "); print_hex(sendBytesBuf, sizeof(sendBytesBuf)); fprintf(stdout, " -- IV = "); - print_hex(desCryptoData.ivData, AES128_BLOCK_SIZE); + print_hex(desCryptoData.ivData, CRYPTO_DES_BLOCK_SIZE); } rxDataStatus = libnfcTransmitBytes(nfcConnDev, sendBytesBuf, 2 * CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY + nonDataPaddingSize, rxDataStorage); if (rxDataStatus && PRINT_STATUS_EXCHANGE_MESSAGES) { @@ -376,7 +376,7 @@ static inline int AuthenticateLegacy(nfc_device *nfcConnDev, uint8_t keyIndex, c DecryptDES(rxDataStorage->rxDataBuf, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY, decryptedRndAFromPICCRotated, IVBuf, desCryptoData); if (PRINT_STATUS_EXCHANGE_MESSAGES) { fprintf(stdout, " -- IV = "); - print_hex(IVBuf, CRYPTO_3KTDEA_BLOCK_SIZE); + print_hex(IVBuf, CRYPTO_DES_BLOCK_SIZE); } RotateArrayLeft(decryptedRndAFromPICCRotated, decryptedRndA, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY); if (!memcmp(rndA, decryptedRndA, CRYPTO_CHALLENGE_RESPONSE_SIZE_LEGACY)) { diff --git a/Software/DESFireLibNFCTesting/Makefile b/Software/DESFireLibNFCTesting/Makefile index 742cf828..e46b3f94 100644 --- a/Software/DESFireLibNFCTesting/Makefile +++ b/Software/DESFireLibNFCTesting/Makefile @@ -42,6 +42,14 @@ LIBNFC_CUSTOM_CONFIG= -DLIBNFC_LOGLEVEL=NFC_LOG_PRIORITY_DEBUG -ULOG_GROUP -DLOG_GROUP=NFC_LOG_GROUP_GENERAL CFLAGS+= $(LIBNFC_SUPPORTED_DRIVERS) $(LIBNFC_CUSTOM_CONFIG) +UTILS_SOURCE=LocalInclude/Config.h \ + LocalInclude/CryptoUtils.h \ + LocalInclude/DesfireUtils.h \ + LocalInclude/ErrorHandling.h \ + LocalInclude/GeneralUtils.h \ + LocalInclude/LibNFCUtils.h \ + LocalInclude/LibNFCWrapper.h + FILE_BASENAMES=NFCAntiCollisionMod \ TestAuthenticateLegacy \ TestAuthenticateAES128 \ @@ -65,7 +73,7 @@ all: default default: prelims $(OBJFILES) $(BINOUTS) -$(OBJDIR)/%.$(OBJEXT): Source/%.c +$(OBJDIR)/%.$(OBJEXT): Source/%.c $(UTILS_SOURCE) $(CC) $(CFLAGS) $< -c -o $@ @/bin/bash ./Scripts/LibNFCObjectDefsRename.sh $@