2 * Copyright (C) 2010, Romain Tartiere.
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>
21 * This implementation was written based on information provided by the
22 * following documents:
24 * NIST Special Publication 800-38B
25 * Recommendation for Block Cipher Modes of Operation: The CMAC Mode for Authentication
28 #include "desfire_crypto.h"
32 #include "commonutil.h"
37 #include "crc16.h" // crc16 ccitt
41 # define AddCrc14A(data, len) compute_crc(CRC_14443_A, (data), (len), (data)+(len), (data)+(len)+1)
44 static inline void update_key_schedules(desfirekey_t key
);
46 static inline void update_key_schedules(desfirekey_t key
) {
47 // DES_set_key ((DES_cblock *)key->data, &(key->ks1));
48 // DES_set_key ((DES_cblock *)(key->data + 8), &(key->ks2));
49 // if (T_3K3DES == key->type) {
50 // DES_set_key ((DES_cblock *)(key->data + 16), &(key->ks3));
54 /******************************************************************************/
56 void des_encrypt(void *out
, const void *in
, const void *key
) {
57 mbedtls_des_context ctx
;
58 mbedtls_des_setkey_enc(&ctx
, key
);
59 mbedtls_des_crypt_ecb(&ctx
, in
, out
);
62 void des_decrypt(void *out
, const void *in
, const void *key
) {
63 mbedtls_des_context ctx
;
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
, unsigned char iv
[8], int keymode
) {
72 mbedtls_des3_context ctx3
;
74 mbedtls_des3_set2key_dec(&ctx3
, key
);
76 mbedtls_des3_set3key_dec(&ctx3
, key
);
79 unsigned char temp
[8];
80 uint8_t *tin
= (uint8_t *) in
;
81 uint8_t *tout
= (uint8_t *) out
;
86 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
88 for (i
= 0; i
< 8; i
++) {
89 tout
[i
] = (unsigned char)(tout
[i
] ^ iv
[i
]);
100 void tdes_nxp_send(const void *in
, void *out
, size_t length
, const void *key
, unsigned char iv
[8], int keymode
) {
104 mbedtls_des3_context ctx3
;
107 mbedtls_des3_set2key_enc(&ctx3
, key
);
109 mbedtls_des3_set3key_enc(&ctx3
, key
);
112 uint8_t *tin
= (uint8_t *) in
;
113 uint8_t *tout
= (uint8_t *) out
;
116 for (i
= 0; i
< 8; i
++) {
117 tin
[i
] = (unsigned char)(tin
[i
] ^ iv
[i
]);
120 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
131 void Desfire_des_key_new(const uint8_t value
[8], desfirekey_t key
) {
133 memcpy(data
, value
, 8);
134 for (int n
= 0; n
< 8; n
++) {
137 Desfire_des_key_new_with_version(data
, key
);
140 void Desfire_des_key_new_with_version(const uint8_t value
[8], desfirekey_t key
) {
143 memcpy(key
->data
, value
, 8);
144 memcpy(key
->data
+ 8, value
, 8);
145 update_key_schedules(key
);
149 void Desfire_3des_key_new(const uint8_t value
[16], desfirekey_t key
) {
151 memcpy(data
, value
, 16);
152 for (int n
= 0; n
< 8; n
++) {
155 for (int n
= 8; n
< 16; n
++) {
158 Desfire_3des_key_new_with_version(data
, key
);
161 void Desfire_3des_key_new_with_version(const uint8_t value
[16], desfirekey_t key
) {
164 memcpy(key
->data
, value
, 16);
165 update_key_schedules(key
);
169 void Desfire_3k3des_key_new(const uint8_t value
[24], desfirekey_t key
) {
171 memcpy(data
, value
, 24);
172 for (int n
= 0; n
< 8; n
++) {
175 Desfire_3k3des_key_new_with_version(data
, key
);
178 void Desfire_3k3des_key_new_with_version(const uint8_t value
[24], desfirekey_t key
) {
180 key
->type
= T_3K3DES
;
181 memcpy(key
->data
, value
, 24);
182 update_key_schedules(key
);
186 void Desfire_aes_key_new(const uint8_t value
[16], desfirekey_t key
) {
187 Desfire_aes_key_new_with_version(value
, 0, key
);
190 void Desfire_aes_key_new_with_version(const uint8_t value
[16], uint8_t version
, desfirekey_t key
) {
192 memcpy(key
->data
, value
, 16);
194 key
->aes_version
= version
;
198 uint8_t Desfire_key_get_version(desfirekey_t key
) {
201 for (int n
= 0; n
< 8; n
++) {
202 version
|= ((key
->data
[n
] & 1) << (7 - n
));
207 void Desfire_key_set_version(desfirekey_t key
, uint8_t version
) {
208 for (int n
= 0; n
< 8; n
++) {
209 uint8_t version_bit
= ((version
& (1 << (7 - n
))) >> (7 - n
));
211 key
->data
[n
] &= 0xFE;
212 key
->data
[n
] |= version_bit
;
214 if (key
->type
== T_DES
) {
215 key
->data
[n
+ 8] = key
->data
[n
];
217 // Write ~version to avoid turning a 3DES key into a DES key
218 key
->data
[n
+ 8] &= 0xFE;
219 key
->data
[n
+ 8] |= ~version_bit
;
224 void Desfire_session_key_new(const uint8_t rnda
[], const uint8_t rndb
[], desfirekey_t authkey
, desfirekey_t key
) {
228 switch (authkey
->type
) {
230 memcpy(buffer
, rnda
, 4);
231 memcpy(buffer
+ 4, rndb
, 4);
232 Desfire_des_key_new_with_version(buffer
, key
);
235 memcpy(buffer
, rnda
, 4);
236 memcpy(buffer
+ 4, rndb
, 4);
237 memcpy(buffer
+ 8, rnda
+ 4, 4);
238 memcpy(buffer
+ 12, rndb
+ 4, 4);
239 Desfire_3des_key_new_with_version(buffer
, key
);
242 memcpy(buffer
, rnda
, 4);
243 memcpy(buffer
+ 4, rndb
, 4);
244 memcpy(buffer
+ 8, rnda
+ 6, 4);
245 memcpy(buffer
+ 12, rndb
+ 6, 4);
246 memcpy(buffer
+ 16, rnda
+ 12, 4);
247 memcpy(buffer
+ 20, rndb
+ 12, 4);
248 Desfire_3k3des_key_new(buffer
, key
);
251 memcpy(buffer
, rnda
, 4);
252 memcpy(buffer
+ 4, rndb
, 4);
253 memcpy(buffer
+ 8, rnda
+ 12, 4);
254 memcpy(buffer
+ 12, rndb
+ 12, 4);
255 Desfire_aes_key_new(buffer
, key
);
260 static size_t key_macing_length(desfirekey_t key
);
262 // iceman, see memxor inside string.c, dest/src swapped..
263 static void xor(const uint8_t *ivect
, uint8_t *data
, const size_t len
) {
264 for (size_t i
= 0; i
< len
; i
++) {
269 void cmac_generate_subkeys(desfirekey_t key
, MifareCryptoDirection direction
) {
270 int kbs
= key_block_size(key
);
271 const uint8_t R
= (kbs
== 8) ? 0x1B : 0x87;
277 memset(ivect
, 0, kbs
);
279 mifare_cypher_blocks_chained(NULL
, key
, ivect
, l
, kbs
, direction
, MCO_ENCYPHER
);
283 // Used to compute CMAC on complete blocks
284 memcpy(key
->cmac_sk1
, l
, kbs
);
286 lsl(key
->cmac_sk1
, kbs
);
288 key
->cmac_sk1
[kbs
- 1] ^= R
;
291 // Used to compute CMAC on the last block if non-complete
292 memcpy(key
->cmac_sk2
, key
->cmac_sk1
, kbs
);
293 txor
= key
->cmac_sk1
[0] & 0x80;
294 lsl(key
->cmac_sk2
, kbs
);
296 key
->cmac_sk2
[kbs
- 1] ^= R
;
300 void cmac(const desfirekey_t key
, uint8_t *ivect
, const uint8_t *data
, size_t len
, uint8_t *cmac
) {
301 int kbs
= key_block_size(key
);
306 uint8_t *buffer
= calloc(padded_data_length(len
, kbs
), sizeof(uint8_t));
307 if (buffer
== NULL
) {
308 PrintAndLogEx(WARNING
, "failed to allocate memory");
312 memcpy(buffer
, data
, len
);
314 if ((!len
) || (len
% kbs
)) {
315 buffer
[len
++] = 0x80;
317 buffer
[len
++] = 0x00;
319 xor(key
->cmac_sk2
, buffer
+ len
- kbs
, kbs
);
321 xor(key
->cmac_sk1
, buffer
+ len
- kbs
, kbs
);
324 mifare_cypher_blocks_chained(NULL
, key
, ivect
, buffer
, len
, MCD_SEND
, MCO_ENCYPHER
);
326 memcpy(cmac
, ivect
, kbs
);
330 // This function is almot like cmac(...). but with some key differences.
331 void mifare_kdf_an10922(const desfirekey_t key
, const uint8_t *data
, size_t len
) {
332 int kbs
= key_block_size(key
);
334 if (key
== NULL
|| kbs
== 0 || data
== NULL
|| len
< 1 || len
> 31) {
338 cmac_generate_subkeys(key
, MCD_SEND
);
340 // reserv atleast 32bytes.
341 uint8_t *buffer
= calloc(len
, sizeof(uint8_t));
342 if (buffer
== NULL
) {
343 PrintAndLogEx(WARNING
, "failed to allocate memory");
346 uint8_t *ivect
= calloc(kbs
, sizeof(uint8_t));
348 PrintAndLogEx(WARNING
, "failed to allocate memory");
353 memcpy(&buffer
[1], data
, len
++);
356 buffer
[len
++] = 0x80;
358 buffer
[len
++] = 0x00;
360 xor(key
->cmac_sk2
, buffer
+ kbs
, kbs
);
362 xor(key
->cmac_sk1
, buffer
+ kbs
, kbs
);
365 mbedtls_aes_context actx
;
366 mbedtls_aes_init(&actx
);
367 mbedtls_aes_setkey_enc(&actx
, key
->data
, kbs
* 8);
368 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_ENCRYPT
, kbs2
, ivect
, buffer
, buffer
);
369 mbedtls_aes_free(&actx
);
371 memcpy(key
->data
, buffer
+ kbs
, kbs
);
376 size_t key_block_size(const desfirekey_t key
) {
380 size_t block_size
= 8;
395 * Size of MACing produced with the key.
397 static size_t key_macing_length(const desfirekey_t key
) {
398 size_t mac_length
= MAC_LENGTH
;
402 mac_length
= MAC_LENGTH
;
406 mac_length
= CMAC_LENGTH
;
413 * Size required to store nbytes of data in a buffer of size n*block_size.
415 size_t padded_data_length(const size_t nbytes
, const size_t block_size
) {
416 if ((!nbytes
) || (nbytes
% block_size
))
417 return ((nbytes
/ block_size
) + 1) * block_size
;
423 * Buffer size required to MAC nbytes of data
425 size_t maced_data_length(const desfirekey_t key
, const size_t nbytes
) {
426 return nbytes
+ key_macing_length(key
);
429 * Buffer size required to encipher nbytes of data and a two bytes CRC.
431 size_t enciphered_data_length(const desfiretag_t tag
, const size_t nbytes
, int communication_settings
) {
432 size_t crc_length
= 0;
433 if (!(communication_settings
& NO_CRC
)) {
434 switch (DESFIRE(tag
)->authentication_scheme
) {
444 size_t block_size
= DESFIRE(tag
)->session_key
? key_block_size(DESFIRE(tag
)->session_key
) : 1;
446 return padded_data_length(nbytes
+ crc_length
, block_size
);
449 void *mifare_cryto_preprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, size_t offset
, int communication_settings
) {
453 bool append_mac
= true;
454 desfirekey_t key
= DESFIRE(tag
)->session_key
;
459 switch (communication_settings
& MDCM_MASK
) {
461 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
465 * When using new authentication methods, PLAIN data transmission from
466 * the PICC to the PCD are CMACed, so we have to maintain the
467 * cryptographic initialisation vector up-to-date to check data
470 * The only difference with CMACed data transmission is that the CMAC
471 * is not apended to the data send by the PCD to the PICC.
478 communication_settings
|= NO_CRC
;
480 switch (DESFIRE(tag
)->authentication_scheme
) {
482 if (!(communication_settings
& MAC_COMMAND
))
486 edl
= padded_data_length(*nbytes
- offset
, key_block_size(DESFIRE(tag
)->session_key
)) + offset
;
488 // Fill in the crypto buffer with data ...
489 memcpy(res
, data
, *nbytes
);
491 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
493 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, edl
- offset
, MCD_SEND
, MCO_ENCYPHER
);
495 memcpy(mac
, res
+ edl
- 8, 4);
497 // Copy again provided data (was overwritten by mifare_cypher_blocks_chained)
498 memcpy(res
, data
, *nbytes
);
500 if (!(communication_settings
& MAC_COMMAND
))
503 size_t bla
= maced_data_length(DESFIRE(tag
)->session_key
, *nbytes
- offset
) + offset
;
506 memcpy(res
+ *nbytes
, mac
, 4);
511 if (!(communication_settings
& CMAC_COMMAND
))
513 cmac(key
, DESFIRE(tag
)->ivect
, res
, *nbytes
, DESFIRE(tag
)->cmac
);
516 size_t len
= maced_data_length(key
, *nbytes
);
518 memcpy(res
, data
, *nbytes
);
519 memcpy(res
+ *nbytes
, DESFIRE(tag
)->cmac
, CMAC_LENGTH
);
520 *nbytes
+= CMAC_LENGTH
;
527 case MDCM_ENCIPHERED
: {
528 /* |<-------------- data -------------->|
529 * |<--- offset -->| |
530 * +---------------+--------------------+-----+---------+
531 * | CMD + HEADERS | DATA TO BE SECURED | CRC | PADDING |
532 * +---------------+--------------------+-----+---------+ ----------------
533 * | |<~~~~v~~~~~~~~~~~~~>| ^ | | (DES / 3DES)
534 * | | `---- crc16() ----' | |
535 * | | | ^ | | ----- *or* -----
536 * |<~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~~~~>| ^ | | (3K3DES / AES)
537 * | `---- crc32() ----' | |
538 * | | ---- *then* ----
539 * |<---------------------------------->|
540 * encypher()/decypher()
543 if (!(communication_settings
& ENC_COMMAND
))
546 edl
= enciphered_data_length(tag
, *nbytes
- offset
, communication_settings
) + offset
;
548 // Fill in the crypto buffer with data ...
549 memcpy(res
, data
, *nbytes
);
551 if (!(communication_settings
& NO_CRC
)) {
553 switch (DESFIRE(tag
)->authentication_scheme
) {
555 AddCrc14A(res
+ offset
, *nbytes
- offset
);
560 crc32_append(res
, *nbytes
);
567 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
571 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, *nbytes
- offset
, MCD_SEND
, (AS_NEW
== DESFIRE(tag
)->authentication_scheme
) ? MCO_ENCYPHER
: MCO_DECYPHER
);
585 void *mifare_cryto_postprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, int communication_settings
) {
588 tag
->crypto_buffer_size
= *nbytes
* 2;
589 tag
->crypto_buffer
= (uint8_t *)calloc(tag
->crypto_buffer_size
, sizeof(uint8_t));
591 uint8_t first_cmac_byte
= 0x00;
593 desfirekey_t key
= DESFIRE(tag
)->session_key
;
598 // Return directly if we just have a status code.
602 switch (communication_settings
& MDCM_MASK
) {
605 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
610 communication_settings
|= NO_CRC
;
611 switch (DESFIRE(tag
)->authentication_scheme
) {
613 if (communication_settings
& MAC_VERIFY
) {
614 *nbytes
-= key_macing_length(key
);
619 Dbprintf("No room for MAC!");
624 size_t edl
= enciphered_data_length(tag
, *nbytes
, communication_settings
);
625 edata
= calloc(edl
, sizeof(uint8_t));
627 memcpy(edata
, data
, *nbytes
);
628 memset((uint8_t *)edata
+ *nbytes
, 0, edl
- *nbytes
);
630 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, edata
, edl
, MCD_SEND
, MCO_ENCYPHER
);
632 if (0 != memcmp((uint8_t *)data
+ *nbytes
, (uint8_t *)edata
+ edl
- 8, 4)) {
634 PrintAndLogEx(NORMAL
, "Expected MAC %s", sprint_hex(data
+ *nbytes
, key_macing_length(key
)));
635 PrintAndLogEx(NORMAL
, "Actual MAC %s", sprint_hex(edata
+ edl
- 8, key_macing_length(key
)));
638 Dbprintf("MACing not verified");
639 hexdump((uint8_t *)data
+ *nbytes
, key_macing_length(key
), "Expect ", 0);
640 hexdump((uint8_t *)edata
+ edl
- 8, key_macing_length(key
), "Actual ", 0);
642 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
649 if (!(communication_settings
& CMAC_COMMAND
))
651 if (communication_settings
& CMAC_VERIFY
) {
657 first_cmac_byte
= ((uint8_t *)data
)[*nbytes
- 9];
658 ((uint8_t *)data
)[*nbytes
- 9] = ((uint8_t *)data
)[*nbytes
- 1];
661 int n
= (communication_settings
& CMAC_VERIFY
) ? 8 : 0;
662 cmac(key
, DESFIRE(tag
)->ivect
, ((uint8_t *)data
), *nbytes
- n
, DESFIRE(tag
)->cmac
);
664 if (communication_settings
& CMAC_VERIFY
) {
665 ((uint8_t *)data
)[*nbytes
- 9] = first_cmac_byte
;
666 if (0 != memcmp(DESFIRE(tag
)->cmac
, (uint8_t *)data
+ *nbytes
- 9, 8)) {
668 Dbprintf("CMAC NOT verified :-(");
669 hexdump((uint8_t *)data
+ *nbytes
- 9, 8, "Expect ", 0);
670 hexdump(DESFIRE(tag
)->cmac
, 8, "Actual ", 0);
672 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
685 case MDCM_ENCIPHERED
: {
686 bool verified
= false;
688 int end_crc_pos
= 0x00;
693 * ,-----------------+-------------------------------+--------+
694 * \ BLOCK n-1 | BLOCK n | STATUS |
695 * / PAYLOAD | CRC0 | CRC1 | 0x80? | 0x000000000000 | 0x9100 |
696 * `-----------------+-------------------------------+--------+
698 * <------------ DATA ------------>
699 * FRAME = PAYLOAD + CRC(PAYLOAD) + PADDING
702 * ,-------------------------------+-----------------------------------------------+--------+
703 * \ BLOCK n-1 | BLOCK n | STATUS |
704 * / PAYLOAD | CRC0 | CRC1 | CRC2 | CRC3 | 0x80? | 0x0000000000000000000000000000 | 0x9100 |
705 * `-------------------------------+-----------------------------------------------+--------+
706 * <----------------------------------- DATA ------------------------------------->|
708 * <----------------- DATA ---------------->
709 * FRAME = PAYLOAD + CRC(PAYLOAD + STATUS) + PADDING + STATUS
710 * `------------------'
713 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
, *nbytes
, MCD_RECEIVE
, MCO_DECYPHER
);
716 * Look for the CRC and ensure it is followed by NULL padding. We
717 * can't start by the end because the CRC is supposed to be 0 when
718 * verified, and accumulating 0's in it should not change it.
720 switch (DESFIRE(tag
)->authentication_scheme
) {
722 crc_pos
= *nbytes
- 8 - 1; // The CRC can be over two blocks
729 /* Move status between payload and CRC */
730 res
= DESFIRE(tag
)->crypto_buffer
;
732 memcpy(res
, data
, *nbytes
);
734 size_t padding_start_pos
= *nbytes
- 1;
735 while (padding_start_pos
> 0 && ((uint8_t *) res
)[padding_start_pos
] == 0x00) {
738 //TODO: Add support for cases where there is no padding. Uncommon but possible.
739 crc_pos
= padding_start_pos
- 4;
741 memcpy((uint8_t *) res
+ crc_pos
+ 1, (uint8_t *) res
+ crc_pos
, *nbytes
- crc_pos
);
742 ((uint8_t *) res
)[crc_pos
] = 0x00;
750 uint16_t crc_16
= 0x00;
752 switch (DESFIRE(tag
)->authentication_scheme
) {
754 AddCrc14A((uint8_t *)res
, end_crc_pos
);
755 end_crc_pos
= crc_pos
+ 2;
762 end_crc_pos
= crc_pos
+ 4;
763 crc32_ex(res
, end_crc_pos
, (uint8_t *)&crc
);
768 for (int n
= end_crc_pos
; n
< *nbytes
- 1; n
++) {
769 uint8_t byte
= ((uint8_t *)res
)[n
];
770 if (!((0x00 == byte
) || ((0x80 == byte
) && (n
== end_crc_pos
))))
776 switch (DESFIRE(tag
)->authentication_scheme
) {
778 ((uint8_t *)data
)[(*nbytes
)++] = 0x00;
781 *nbytes
= crc_pos
- 1;
782 /* The status byte was already before the CRC */
786 switch (DESFIRE(tag
)->authentication_scheme
) {
790 x
= ((uint8_t *)res
)[crc_pos
- 1];
791 ((uint8_t *)res
)[crc_pos
- 1] = ((uint8_t *)res
)[crc_pos
];
792 ((uint8_t *)res
)[crc_pos
] = x
;
797 } while (!verified
&& (end_crc_pos
< *nbytes
));
801 /* FIXME In some configurations, the file is transmitted PLAIN */
802 Dbprintf("CRC not verified in decyphered stream");
804 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
812 PrintAndLogEx(ERR
, "Unknown communication settings");
818 free(tag
->crypto_buffer
);
819 tag
->crypto_buffer_size
= 0;
820 tag
->crypto_buffer
= NULL
;
825 void mifare_cypher_single_block(desfirekey_t key
, uint8_t *data
, uint8_t *ivect
, MifareCryptoDirection direction
, MifareCryptoOperation operation
, size_t block_size
) {
826 uint8_t ovect
[MAX_CRYPTO_BLOCK_SIZE
];
827 if (direction
== MCD_SEND
) {
828 xor(ivect
, data
, block_size
);
830 memcpy(ovect
, data
, block_size
);
833 uint8_t edata
[MAX_CRYPTO_BLOCK_SIZE
];
839 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
840 des_encrypt(edata
, data
, key
->data
);
843 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
844 des_decrypt(edata
, data
, key
->data
);
851 mbedtls_des3_context ctx3
;
852 mbedtls_des3_set2key_enc(&ctx3
, key
->data
);
853 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
854 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
855 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
856 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
860 mbedtls_des3_context ctx3
;
861 mbedtls_des3_set2key_dec(&ctx3
, key
->data
);
862 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
863 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
864 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
865 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
873 mbedtls_des3_context ctx3
;
874 mbedtls_des3_set3key_enc(&ctx3
, key
->data
);
875 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
876 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
877 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
878 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_ENCRYPT);
882 mbedtls_des3_context ctx3
;
883 mbedtls_des3_set3key_dec(&ctx3
, key
->data
);
884 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
885 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_DECRYPT);
886 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
887 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
895 mbedtls_aes_context actx
;
896 mbedtls_aes_init(&actx
);
897 mbedtls_aes_setkey_enc(&actx
, key
->data
, 128);
898 mbedtls_aes_crypt_ecb(&actx
, MBEDTLS_AES_ENCRYPT
, data
, edata
);
899 mbedtls_aes_free(&actx
);
903 mbedtls_aes_context actx
;
904 mbedtls_aes_init(&actx
);
905 mbedtls_aes_setkey_dec(&actx
, key
->data
, 128);
906 mbedtls_aes_crypt_ecb(&actx
, MBEDTLS_AES_DECRYPT
, data
, edata
);
907 mbedtls_aes_free(&actx
);
914 memcpy(data
, edata
, block_size
);
916 if (direction
== MCD_SEND
) {
917 memcpy(ivect
, data
, block_size
);
919 xor(ivect
, data
, block_size
);
920 memcpy(ivect
, ovect
, block_size
);
925 * This function performs all CBC cyphering / deciphering.
927 * The tag argument may be NULL, in which case both key and ivect shall be set.
928 * When using the tag session_key and ivect for processing data, these
929 * arguments should be set to NULL.
931 * Because the tag may contain additional data, one may need to call this
932 * function with tag, key and ivect defined.
934 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
) {
937 key
= DESFIRE(tag
)->session_key
;
939 ivect
= DESFIRE(tag
)->ivect
;
941 switch (DESFIRE(tag
)->authentication_scheme
) {
943 memset(ivect
, 0, MAX_CRYPTO_BLOCK_SIZE
);
950 size_t block_size
= key_block_size(key
);
952 while (offset
< data_size
) {
953 mifare_cypher_single_block(key
, data
+ offset
, ivect
, direction
, operation
, block_size
);
954 offset
+= block_size
;
958 void desfire_crc32(const uint8_t *data
, const size_t len
, uint8_t *crc
) {
959 crc32_ex(data
, len
, crc
);
962 void desfire_crc32_append(uint8_t *data
, const size_t len
) {
963 crc32_ex(data
, len
, data
+ len
);
966 void iso14443a_crc_append(uint8_t *data
, size_t len
) {
967 return compute_crc(CRC_14443_A
, data
, len
, data
+ len
, data
+ len
+ 1);
970 void iso14443a_crc(uint8_t *data
, size_t len
, uint8_t *pbtCrc
) {
971 return compute_crc(CRC_14443_A
, data
, len
, pbtCrc
, pbtCrc
+ 1);