1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://github.com/nfc-tools/libfreefare
3 // Copyright (C) 2010, Romain Tartiere.
4 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // See LICENSE.txt for the text of the license.
17 //-----------------------------------------------------------------------------
19 * This implementation was written based on information provided by the
20 * following documents:
22 * NIST Special Publication 800-38B
23 * Recommendation for Block Cipher Modes of Operation: The CMAC Mode for Authentication
26 #include "desfire_crypto.h"
30 #include "commonutil.h"
33 #include "crc16.h" // crc16 ccitt
35 #include "iso14443a.h"
40 # define AddCrc14A(data, len) compute_crc(CRC_14443_A, (data), (len), (data)+(len), (data)+(len)+1)
43 static mbedtls_des_context ctx
;
44 static mbedtls_des3_context ctx3
;
45 static mbedtls_aes_context actx
;
47 static void update_key_schedules(desfirekey_t key
);
49 static void update_key_schedules(desfirekey_t key
) {
50 // DES_set_key ((DES_cblock *)key->data, &(key->ks1));
51 // DES_set_key ((DES_cblock *)(key->data + 8), &(key->ks2));
52 // if (T_3K3DES == key->type) {
53 // DES_set_key ((DES_cblock *)(key->data + 16), &(key->ks3));
57 /******************************************************************************/
58 void des_encrypt(void *out
, const void *in
, const void *key
) {
59 mbedtls_des_setkey_enc(&ctx
, key
);
60 mbedtls_des_crypt_ecb(&ctx
, in
, out
);
63 void des_decrypt(void *out
, const void *in
, const void *key
) {
64 mbedtls_des_setkey_dec(&ctx
, key
);
65 mbedtls_des_crypt_ecb(&ctx
, in
, out
);
68 void tdes_nxp_receive(const void *in
, void *out
, size_t length
, const void *key
, uint8_t *iv
, int keymode
) {
70 // must be even blocks of 8 bytes.
76 mbedtls_des3_set2key_dec(&ctx3
, key
);
78 mbedtls_des3_set3key_dec(&ctx3
, key
);
81 unsigned char temp
[8];
82 uint8_t *tin
= (uint8_t *) in
;
83 uint8_t *tout
= (uint8_t *) out
;
89 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
91 for (uint8_t i
= 0; i
< 8; i
++) {
103 void tdes_nxp_send(const void *in
, void *out
, size_t length
, const void *key
, uint8_t *iv
, int keymode
) {
105 // must be even blocks of 8 bytes.
111 mbedtls_des3_set2key_enc(&ctx3
, key
);
113 mbedtls_des3_set3key_enc(&ctx3
, key
);
116 uint8_t *tin
= (uint8_t *) in
;
117 uint8_t *tout
= (uint8_t *) out
;
121 for (uint8_t i
= 0; i
< 8; i
++) {
125 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
135 void aes128_nxp_receive(const void *in
, void *out
, size_t length
, const void *key
, unsigned char iv
[16]) {
136 if (length
% 8) return;
138 uint8_t *tin
= (uint8_t *) in
;
139 uint8_t *tout
= (uint8_t *) out
;
141 mbedtls_aes_setkey_dec(&actx
, key
, 128);
142 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_DECRYPT
, length
, iv
, tin
, tout
);
145 void aes128_nxp_send(const void *in
, void *out
, size_t length
, const void *key
, unsigned char iv
[16]) {
146 if (length
% 8) return;
148 uint8_t *tin
= (uint8_t *) in
;
149 uint8_t *tout
= (uint8_t *) out
;
151 mbedtls_aes_setkey_enc(&actx
, key
, 128);
152 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_ENCRYPT
, length
, iv
, tin
, tout
);
155 void Desfire_des_key_new(const uint8_t value
[8], desfirekey_t key
) {
157 memcpy(data
, value
, 8);
158 for (int n
= 0; n
< 8; n
++) {
161 Desfire_des_key_new_with_version(data
, key
);
164 void Desfire_des_key_new_with_version(const uint8_t value
[8], desfirekey_t key
) {
167 memcpy(key
->data
, value
, 8);
168 memcpy(key
->data
+ 8, value
, 8);
169 update_key_schedules(key
);
173 void Desfire_3des_key_new(const uint8_t value
[16], desfirekey_t key
) {
175 memcpy(data
, value
, 16);
176 for (int n
= 0; n
< 8; n
++) {
179 for (int n
= 8; n
< 16; n
++) {
182 Desfire_3des_key_new_with_version(data
, key
);
185 void Desfire_3des_key_new_with_version(const uint8_t value
[16], desfirekey_t key
) {
188 memcpy(key
->data
, value
, 16);
189 update_key_schedules(key
);
193 void Desfire_3k3des_key_new(const uint8_t value
[24], desfirekey_t key
) {
195 memcpy(data
, value
, 24);
196 for (int n
= 0; n
< 8; n
++) {
199 Desfire_3k3des_key_new_with_version(data
, key
);
202 void Desfire_3k3des_key_new_with_version(const uint8_t value
[24], desfirekey_t key
) {
204 key
->type
= T_3K3DES
;
205 memcpy(key
->data
, value
, 24);
206 update_key_schedules(key
);
210 void Desfire_aes_key_new(const uint8_t value
[16], desfirekey_t key
) {
211 Desfire_aes_key_new_with_version(value
, 0, key
);
214 void Desfire_aes_key_new_with_version(const uint8_t value
[16], uint8_t version
, desfirekey_t key
) {
217 memcpy(key
->data
, value
, 16);
219 key
->aes_version
= version
;
223 uint8_t Desfire_key_get_version(desfirekey_t key
) {
226 for (int n
= 0; n
< 8; n
++) {
227 version
|= ((key
->data
[n
] & 1) << (7 - n
));
232 void Desfire_key_set_version(desfirekey_t key
, uint8_t version
) {
233 for (int n
= 0; n
< 8; n
++) {
234 uint8_t version_bit
= ((version
& (1 << (7 - n
))) >> (7 - n
));
235 key
->data
[n
] &= 0xFE;
236 key
->data
[n
] |= version_bit
;
237 if (key
->type
== T_DES
) {
238 key
->data
[n
+ 8] = key
->data
[n
];
240 // Write ~version to avoid turning a 3DES key into a DES key
241 key
->data
[n
+ 8] &= 0xFE;
242 key
->data
[n
+ 8] |= ~version_bit
;
247 void Desfire_session_key_new(const uint8_t rnda
[], const uint8_t rndb
[], desfirekey_t authkey
, desfirekey_t key
) {
251 switch (authkey
->type
) {
253 memcpy(buffer
, rnda
, 4);
254 memcpy(buffer
+ 4, rndb
, 4);
255 Desfire_des_key_new_with_version(buffer
, key
);
258 memcpy(buffer
, rnda
, 4);
259 memcpy(buffer
+ 4, rndb
, 4);
260 memcpy(buffer
+ 8, rnda
+ 4, 4);
261 memcpy(buffer
+ 12, rndb
+ 4, 4);
262 Desfire_3des_key_new_with_version(buffer
, key
);
265 memcpy(buffer
, rnda
, 4);
266 memcpy(buffer
+ 4, rndb
, 4);
267 memcpy(buffer
+ 8, rnda
+ 6, 4);
268 memcpy(buffer
+ 12, rndb
+ 6, 4);
269 memcpy(buffer
+ 16, rnda
+ 12, 4);
270 memcpy(buffer
+ 20, rndb
+ 12, 4);
271 Desfire_3k3des_key_new(buffer
, key
);
274 memcpy(buffer
, rnda
, 4);
275 memcpy(buffer
+ 4, rndb
, 4);
276 memcpy(buffer
+ 8, rnda
+ 12, 4);
277 memcpy(buffer
+ 12, rndb
+ 12, 4);
278 Desfire_aes_key_new(buffer
, key
);
283 static size_t key_macing_length(desfirekey_t key
);
285 // iceman, see memxor inside string.c, dest/src swapped..
286 static void xor(const uint8_t *ivect
, uint8_t *data
, const size_t len
) {
287 for (size_t i
= 0; i
< len
; i
++) {
292 void cmac_generate_subkeys(desfirekey_t key
) {
293 int kbs
= key_block_size(key
);
294 const uint8_t R
= (kbs
== 8) ? 0x1B : 0x87;
300 memset(ivect
, 0, kbs
);
302 mifare_cypher_blocks_chained(NULL
, key
, ivect
, l
, kbs
, MCD_RECEIVE
, MCO_ENCYPHER
);
306 // Used to compute CMAC on complete blocks
307 memcpy(key
->cmac_sk1
, l
, kbs
);
311 lsl(key
->cmac_sk1
, kbs
);
314 key
->cmac_sk1
[kbs
- 1] ^= R
;
317 // Used to compute CMAC on the last block if non-complete
318 memcpy(key
->cmac_sk2
, key
->cmac_sk1
, kbs
);
320 txor
= key
->cmac_sk1
[0] & 0x80;
322 lsl(key
->cmac_sk2
, kbs
);
325 key
->cmac_sk2
[kbs
- 1] ^= R
;
329 void cmac(const desfirekey_t key
, uint8_t *ivect
, const uint8_t *data
, size_t len
, uint8_t *cmac
) {
330 int kbs
= key_block_size(key
);
335 uint8_t *buffer
= BigBuf_malloc(padded_data_length(len
, kbs
));
337 memcpy(buffer
, data
, len
);
339 if ((!len
) || (len
% kbs
)) {
340 buffer
[len
++] = 0x80;
342 buffer
[len
++] = 0x00;
344 xor(key
->cmac_sk2
, buffer
+ len
- kbs
, kbs
);
346 xor(key
->cmac_sk1
, buffer
+ len
- kbs
, kbs
);
349 mifare_cypher_blocks_chained(NULL
, key
, ivect
, buffer
, len
, MCD_SEND
, MCO_ENCYPHER
);
351 memcpy(cmac
, ivect
, kbs
);
355 size_t key_block_size(const desfirekey_t key
) {
360 size_t block_size
= 8;
375 * Size of MACing produced with the key.
377 static size_t key_macing_length(const desfirekey_t key
) {
378 size_t mac_length
= DESFIRE_MAC_LENGTH
;
382 mac_length
= DESFIRE_MAC_LENGTH
;
386 mac_length
= DESFIRE_CMAC_LENGTH
;
393 * Size required to store nbytes of data in a buffer of size n*block_size.
395 size_t padded_data_length(const size_t nbytes
, const size_t block_size
) {
396 if ((!nbytes
) || (nbytes
% block_size
))
397 return ((nbytes
/ block_size
) + 1) * block_size
;
403 * Buffer size required to MAC nbytes of data
405 size_t maced_data_length(const desfirekey_t key
, const size_t nbytes
) {
406 return nbytes
+ key_macing_length(key
);
409 * Buffer size required to encipher nbytes of data and a two bytes CRC.
411 size_t enciphered_data_length(const desfiretag_t tag
, const size_t nbytes
, int communication_settings
) {
412 size_t crc_length
= 0;
413 if (!(communication_settings
& NO_CRC
)) {
414 switch (DESFIRE(tag
)->authentication_scheme
) {
424 size_t block_size
= DESFIRE(tag
)->session_key
? key_block_size(DESFIRE(tag
)->session_key
) : 1;
426 return padded_data_length(nbytes
+ crc_length
, block_size
);
429 void *mifare_cryto_preprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, size_t offset
, int communication_settings
) {
433 bool append_mac
= true;
434 desfirekey_t key
= DESFIRE(tag
)->session_key
;
439 switch (communication_settings
& MDCM_MASK
) {
441 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
445 * When using new authentication methods, PLAIN data transmission from
446 * the PICC to the PCD are CMACed, so we have to maintain the
447 * cryptographic initialisation vector up-to-date to check data
450 * The only difference with CMACed data transmission is that the CMAC
451 * is not appended to the data send by the PCD to the PICC.
458 switch (DESFIRE(tag
)->authentication_scheme
) {
460 if (!(communication_settings
& MAC_COMMAND
))
464 edl
= padded_data_length(*nbytes
- offset
, key_block_size(DESFIRE(tag
)->session_key
)) + offset
;
466 // Fill in the crypto buffer with data ...
467 memcpy(res
, data
, *nbytes
);
469 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
471 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, edl
- offset
, MCD_SEND
, MCO_ENCYPHER
);
473 memcpy(mac
, res
+ edl
- 8, 4);
475 // Copy again provided data (was overwritten by mifare_cypher_blocks_chained)
476 memcpy(res
, data
, *nbytes
);
478 if (!(communication_settings
& MAC_COMMAND
))
481 size_t bla
= maced_data_length(DESFIRE(tag
)->session_key
, *nbytes
- offset
) + offset
;
484 memcpy(res
+ *nbytes
, mac
, 4);
489 if (!(communication_settings
& CMAC_COMMAND
))
491 cmac(key
, DESFIRE(tag
)->ivect
, res
, *nbytes
, DESFIRE(tag
)->cmac
);
494 size_t len
= maced_data_length(key
, *nbytes
);
496 memcpy(res
, data
, *nbytes
);
497 memcpy(res
+ *nbytes
, DESFIRE(tag
)->cmac
, DESFIRE_CMAC_LENGTH
);
498 *nbytes
+= DESFIRE_CMAC_LENGTH
;
504 case MDCM_ENCIPHERED
:
505 /* |<-------------- data -------------->|
506 * |<--- offset -->| |
507 * +---------------+--------------------+-----+---------+
508 * | CMD + HEADERS | DATA TO BE SECURED | CRC | PADDING |
509 * +---------------+--------------------+-----+---------+ ----------------
510 * | |<~~~~v~~~~~~~~~~~~~>| ^ | | (DES / 3DES)
511 * | | `---- crc16() ----' | |
512 * | | | ^ | | ----- *or* -----
513 * |<~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~~~~>| ^ | | (3K3DES / AES)
514 * | `---- crc32() ----' | |
515 * | | ---- *then* ----
516 * |<---------------------------------->|
517 * encypher()/decypher()
520 if (!(communication_settings
& ENC_COMMAND
))
522 edl
= enciphered_data_length(tag
, *nbytes
- offset
, communication_settings
) + offset
;
524 // Fill in the crypto buffer with data ...
525 memcpy(res
, data
, *nbytes
);
526 if (!(communication_settings
& NO_CRC
)) {
528 switch (DESFIRE(tag
)->authentication_scheme
) {
530 AddCrc14A(res
+ offset
, *nbytes
- offset
);
534 crc32_append(res
, *nbytes
);
540 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
544 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, *nbytes
- offset
, MCD_SEND
, (AS_NEW
== DESFIRE(tag
)->authentication_scheme
) ? MCO_ENCYPHER
: MCO_DECYPHER
);
557 void *mifare_cryto_postprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, int communication_settings
) {
559 uint8_t first_cmac_byte
= 0x00;
561 desfirekey_t key
= DESFIRE(tag
)->session_key
;
567 // Return directly if we just have a status code.
572 switch (communication_settings
& MDCM_MASK
) {
575 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
) {
581 switch (DESFIRE(tag
)->authentication_scheme
) {
584 if ((communication_settings
& MAC_VERIFY
) == MAC_VERIFY
) {
586 *nbytes
-= key_macing_length(key
);
592 Dbprintf("No room for MAC!");
597 size_t edl
= enciphered_data_length(tag
, *nbytes
- 1, communication_settings
);
599 memset(edata
, 0, sizeof(edata
));
600 memcpy(edata
, data
, *nbytes
- 1);
602 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, edata
, edl
, MCD_SEND
, MCO_ENCYPHER
);
604 if (0 != memcmp((uint8_t *)data
+ *nbytes
- 1, edata
+ edl
- 8, 4)) {
606 Dbprintf("MACing not verified");
607 hexdump((uint8_t *)data
+ *nbytes
- 1, key_macing_length(key
), "Expect ", 0);
608 hexdump(edata
+ edl
- 8, key_macing_length(key
), "Actual ", 0);
610 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
619 if ((communication_settings
& CMAC_COMMAND
) != CMAC_COMMAND
) {
625 if ((communication_settings
& CMAC_VERIFY
) == CMAC_VERIFY
) {
631 first_cmac_byte
= ((uint8_t *)data
)[*nbytes
- 9];
632 ((uint8_t *)data
)[*nbytes
- 9] = ((uint8_t *)data
)[*nbytes
- 1];
637 cmac(key
, DESFIRE(tag
)->ivect
, ((uint8_t *)data
), *nbytes
- n
, DESFIRE(tag
)->cmac
);
639 if ((communication_settings
& CMAC_VERIFY
) == CMAC_VERIFY
) {
641 ((uint8_t *)data
)[*nbytes
- 9] = first_cmac_byte
;
643 if (0 != memcmp(DESFIRE(tag
)->cmac
, (uint8_t *)data
+ *nbytes
- 9, 8)) {
645 Dbprintf("CMAC NOT verified :-(");
646 hexdump((uint8_t *)data
+ *nbytes
- 9, 8, "Expect ", 0);
647 hexdump(DESFIRE(tag
)->cmac
, 8, "Actual ", 0);
649 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
661 case MDCM_ENCIPHERED
: {
663 bool verified
= false;
665 int end_crc_pos
= 0x00;
670 * ,-----------------+-------------------------------+--------+
671 * \ BLOCK n-1 | BLOCK n | STATUS |
672 * / PAYLOAD | CRC0 | CRC1 | 0x80? | 0x000000000000 | 0x9100 |
673 * `-----------------+-------------------------------+--------+
675 * <------------ DATA ------------>
676 * FRAME = PAYLOAD + CRC(PAYLOAD) + PADDING
679 * ,-------------------------------+-----------------------------------------------+--------+
680 * \ BLOCK n-1 | BLOCK n | STATUS |
681 * / PAYLOAD | CRC0 | CRC1 | CRC2 | CRC3 | 0x80? | 0x0000000000000000000000000000 | 0x9100 |
682 * `-------------------------------+-----------------------------------------------+--------+
683 * <----------------------------------- DATA ------------------------------------->|
685 * <----------------- DATA ---------------->
686 * FRAME = PAYLOAD + CRC(PAYLOAD + STATUS) + PADDING + STATUS
687 * `------------------'
690 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
, *nbytes
, MCD_RECEIVE
, MCO_DECYPHER
);
693 * Look for the CRC and ensure it is followed by NULL padding. We
694 * can't start by the end because the CRC is supposed to be 0 when
695 * verified, and accumulating 0's in it should not change it.
697 switch (DESFIRE(tag
)->authentication_scheme
) {
699 crc_pos
= *nbytes
- 8 - 1; // The CRC can be over two blocks
701 crc_pos
= 0; // Single block
706 /* Move status between payload and CRC */
707 res
= DESFIRE(tag
)->crypto_buffer
;
708 memcpy(res
, data
, *nbytes
);
710 crc_pos
= (*nbytes
) - 16 - 3;
712 crc_pos
= 0; // Single block
715 memcpy((uint8_t *)res
+ crc_pos
+ 1, (uint8_t *)res
+ crc_pos
, *nbytes
- crc_pos
);
716 ((uint8_t *)res
)[crc_pos
] = 0x00;
724 uint16_t crc_16
= 0x00;
727 switch (DESFIRE(tag
)->authentication_scheme
) {
729 AddCrc14A((uint8_t *)res
, end_crc_pos
);
730 end_crc_pos
= crc_pos
+ 2;
735 end_crc_pos
= crc_pos
+ 4;
736 crc32_ex(res
, end_crc_pos
, (uint8_t *)&crc
);
743 for (int n
= end_crc_pos
; n
< *nbytes
- 1; n
++) {
744 uint8_t byte
= ((uint8_t *)res
)[n
];
745 if (!((0x00 == byte
) || ((0x80 == byte
) && (n
== end_crc_pos
))))
754 switch (DESFIRE(tag
)->authentication_scheme
) {
756 ((uint8_t *)data
)[(*nbytes
)++] = 0x00;
760 /* The status byte was already before the CRC */
766 switch (DESFIRE(tag
)->authentication_scheme
) {
771 x
= ((uint8_t *)res
)[crc_pos
- 1];
772 ((uint8_t *)res
)[crc_pos
- 1] = ((uint8_t *)res
)[crc_pos
];
773 ((uint8_t *)res
)[crc_pos
] = x
;
780 } while (verified
== false && (end_crc_pos
< *nbytes
));
782 if (verified
== false) {
784 /* FIXME In some configurations, the file is transmitted PLAIN */
785 Dbprintf("CRC not verified in decyphered stream");
787 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
794 Dbprintf("Unknown communication settings");
804 void mifare_cypher_single_block(desfirekey_t key
, uint8_t *data
, uint8_t *ivect
, MifareCryptoDirection direction
, MifareCryptoOperation operation
, size_t block_size
) {
805 uint8_t ovect
[DESFIRE_MAX_CRYPTO_BLOCK_SIZE
];
806 if (direction
== MCD_SEND
) {
807 xor(ivect
, data
, block_size
);
809 memcpy(ovect
, data
, block_size
);
812 uint8_t edata
[DESFIRE_MAX_CRYPTO_BLOCK_SIZE
] = {0};
818 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
819 des_encrypt(edata
, data
, key
->data
);
822 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
823 des_decrypt(edata
, data
, key
->data
);
830 mbedtls_des3_set2key_enc(&ctx3
, key
->data
);
831 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
832 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
833 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
834 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
837 mbedtls_des3_set2key_dec(&ctx3
, key
->data
);
838 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
839 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
840 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
841 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
848 mbedtls_des3_set3key_enc(&ctx3
, key
->data
);
849 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
850 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
851 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
852 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_ENCRYPT);
855 mbedtls_des3_set3key_dec(&ctx3
, key
->data
);
856 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
857 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_DECRYPT);
858 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
859 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
866 mbedtls_aes_init(&actx
);
867 mbedtls_aes_setkey_enc(&actx
, key
->data
, 128);
868 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_ENCRYPT
, sizeof(edata
), ivect
, data
, edata
);
872 mbedtls_aes_init(&actx
);
873 mbedtls_aes_setkey_dec(&actx
, key
->data
, 128);
874 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_DECRYPT
, sizeof(edata
), ivect
, edata
, data
);
881 memcpy(data
, edata
, block_size
);
883 if (direction
== MCD_SEND
) {
884 memcpy(ivect
, data
, block_size
);
886 xor(ivect
, data
, block_size
);
887 memcpy(ivect
, ovect
, block_size
);
892 * This function performs all CBC cyphering / deciphering.
894 * The tag argument may be NULL, in which case both key and ivect shall be set.
895 * When using the tag session_key and ivect for processing data, these
896 * arguments should be set to NULL.
898 * Because the tag may contain additional data, one may need to call this
899 * function with tag, key and ivect defined.
901 void mifare_cypher_blocks_chained(desfiretag_t tag
, desfirekey_t key
, uint8_t *ivect
, uint8_t *data
, size_t data_size
, MifareCryptoDirection direction
, MifareCryptoOperation operation
) {
906 key
= DESFIRE(tag
)->session_key
;
909 ivect
= DESFIRE(tag
)->ivect
;
912 switch (DESFIRE(tag
)->authentication_scheme
) {
914 memset(ivect
, 0, DESFIRE_MAX_CRYPTO_BLOCK_SIZE
);
921 block_size
= key_block_size(key
);
924 while (offset
< data_size
) {
925 mifare_cypher_single_block(key
, data
+ offset
, ivect
, direction
, operation
, block_size
);
926 offset
+= block_size
;