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"
35 #include "crc16.h" // crc16 ccitt
37 #include "iso14443a.h"
42 # define AddCrc14A(data, len) compute_crc(CRC_14443_A, (data), (len), (data)+(len), (data)+(len)+1)
45 static mbedtls_des_context ctx
;
46 static mbedtls_des3_context ctx3
;
47 static mbedtls_aes_context actx
;
49 static void update_key_schedules(desfirekey_t key
);
51 static void update_key_schedules(desfirekey_t key
) {
52 // DES_set_key ((DES_cblock *)key->data, &(key->ks1));
53 // DES_set_key ((DES_cblock *)(key->data + 8), &(key->ks2));
54 // if (T_3K3DES == key->type) {
55 // DES_set_key ((DES_cblock *)(key->data + 16), &(key->ks3));
59 /******************************************************************************/
60 void des_encrypt(void *out
, const void *in
, const void *key
) {
61 mbedtls_des_setkey_enc(&ctx
, key
);
62 mbedtls_des_crypt_ecb(&ctx
, in
, out
);
65 void des_decrypt(void *out
, const void *in
, const void *key
) {
66 mbedtls_des_setkey_dec(&ctx
, key
);
67 mbedtls_des_crypt_ecb(&ctx
, in
, out
);
70 void tdes_nxp_receive(const void *in
, void *out
, size_t length
, const void *key
, unsigned char iv
[8], int keymode
) {
71 if (length
% 8) return;
72 if (keymode
== 2) mbedtls_des3_set2key_dec(&ctx3
, key
);
73 else mbedtls_des3_set3key_dec(&ctx3
, key
);
76 unsigned char temp
[8];
77 uint8_t *tin
= (uint8_t *) in
;
78 uint8_t *tout
= (uint8_t *) out
;
83 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
85 for (i
= 0; i
< 8; i
++)
86 tout
[i
] = (unsigned char)(tout
[i
] ^ iv
[i
]);
96 void tdes_nxp_send(const void *in
, void *out
, size_t length
, const void *key
, unsigned char iv
[8], int keymode
) {
97 if (length
% 8) return;
98 if (keymode
== 2) mbedtls_des3_set2key_enc(&ctx3
, key
);
99 else mbedtls_des3_set3key_enc(&ctx3
, key
);
102 uint8_t *tin
= (uint8_t *) in
;
103 uint8_t *tout
= (uint8_t *) out
;
106 for (i
= 0; i
< 8; i
++)
107 tin
[i
] = (unsigned char)(tin
[i
] ^ iv
[i
]);
109 mbedtls_des3_crypt_ecb(&ctx3
, tin
, tout
);
121 void Desfire_des_key_new(const uint8_t value
[8], desfirekey_t key
) {
123 memcpy(data
, value
, 8);
124 for (int n
= 0; n
< 8; n
++)
126 Desfire_des_key_new_with_version(data
, key
);
129 void Desfire_des_key_new_with_version(const uint8_t value
[8], desfirekey_t key
) {
132 memcpy(key
->data
, value
, 8);
133 memcpy(key
->data
+ 8, value
, 8);
134 update_key_schedules(key
);
138 void Desfire_3des_key_new(const uint8_t value
[16], desfirekey_t key
) {
140 memcpy(data
, value
, 16);
141 for (int n
= 0; n
< 8; n
++)
143 for (int n
= 8; n
< 16; n
++)
145 Desfire_3des_key_new_with_version(data
, key
);
148 void Desfire_3des_key_new_with_version(const uint8_t value
[16], desfirekey_t key
) {
151 memcpy(key
->data
, value
, 16);
152 update_key_schedules(key
);
156 void Desfire_3k3des_key_new(const uint8_t value
[24], desfirekey_t key
) {
158 memcpy(data
, value
, 24);
159 for (int n
= 0; n
< 8; n
++)
161 Desfire_3k3des_key_new_with_version(data
, key
);
164 void Desfire_3k3des_key_new_with_version(const uint8_t value
[24], desfirekey_t key
) {
166 key
->type
= T_3K3DES
;
167 memcpy(key
->data
, value
, 24);
168 update_key_schedules(key
);
172 void Desfire_aes_key_new(const uint8_t value
[16], desfirekey_t key
) {
173 Desfire_aes_key_new_with_version(value
, 0, key
);
176 void Desfire_aes_key_new_with_version(const uint8_t value
[16], uint8_t version
, desfirekey_t key
) {
179 memcpy(key
->data
, value
, 16);
181 key
->aes_version
= version
;
185 uint8_t Desfire_key_get_version(desfirekey_t key
) {
188 for (int n
= 0; n
< 8; n
++) {
189 version
|= ((key
->data
[n
] & 1) << (7 - n
));
194 void Desfire_key_set_version(desfirekey_t key
, uint8_t version
) {
195 for (int n
= 0; n
< 8; n
++) {
196 uint8_t version_bit
= ((version
& (1 << (7 - n
))) >> (7 - n
));
197 key
->data
[n
] &= 0xfe;
198 key
->data
[n
] |= version_bit
;
199 if (key
->type
== T_DES
) {
200 key
->data
[n
+ 8] = key
->data
[n
];
202 // Write ~version to avoid turning a 3DES key into a DES key
203 key
->data
[n
+ 8] &= 0xfe;
204 key
->data
[n
+ 8] |= ~version_bit
;
209 void Desfire_session_key_new(const uint8_t rnda
[], const uint8_t rndb
[], desfirekey_t authkey
, desfirekey_t key
) {
213 switch (authkey
->type
) {
215 memcpy(buffer
, rnda
, 4);
216 memcpy(buffer
+ 4, rndb
, 4);
217 Desfire_des_key_new_with_version(buffer
, key
);
220 memcpy(buffer
, rnda
, 4);
221 memcpy(buffer
+ 4, rndb
, 4);
222 memcpy(buffer
+ 8, rnda
+ 4, 4);
223 memcpy(buffer
+ 12, rndb
+ 4, 4);
224 Desfire_3des_key_new_with_version(buffer
, key
);
227 memcpy(buffer
, rnda
, 4);
228 memcpy(buffer
+ 4, rndb
, 4);
229 memcpy(buffer
+ 8, rnda
+ 6, 4);
230 memcpy(buffer
+ 12, rndb
+ 6, 4);
231 memcpy(buffer
+ 16, rnda
+ 12, 4);
232 memcpy(buffer
+ 20, rndb
+ 12, 4);
233 Desfire_3k3des_key_new(buffer
, key
);
236 memcpy(buffer
, rnda
, 4);
237 memcpy(buffer
+ 4, rndb
, 4);
238 memcpy(buffer
+ 8, rnda
+ 12, 4);
239 memcpy(buffer
+ 12, rndb
+ 12, 4);
240 Desfire_aes_key_new(buffer
, key
);
245 static size_t key_macing_length(desfirekey_t key
);
247 // iceman, see memxor inside string.c, dest/src swapped..
248 static void xor(const uint8_t *ivect
, uint8_t *data
, const size_t len
) {
249 for (size_t i
= 0; i
< len
; i
++) {
254 void cmac_generate_subkeys(desfirekey_t key
) {
255 int kbs
= key_block_size(key
);
256 const uint8_t R
= (kbs
== 8) ? 0x1B : 0x87;
262 memset(ivect
, 0, kbs
);
264 mifare_cypher_blocks_chained(NULL
, key
, ivect
, l
, kbs
, MCD_RECEIVE
, MCO_ENCYPHER
);
268 // Used to compute CMAC on complete blocks
269 memcpy(key
->cmac_sk1
, l
, kbs
);
271 lsl(key
->cmac_sk1
, kbs
);
273 key
->cmac_sk1
[kbs
- 1] ^= R
;
275 // Used to compute CMAC on the last block if non-complete
276 memcpy(key
->cmac_sk2
, key
->cmac_sk1
, kbs
);
277 txor
= key
->cmac_sk1
[0] & 0x80;
278 lsl(key
->cmac_sk2
, kbs
);
280 key
->cmac_sk2
[kbs
- 1] ^= R
;
283 void cmac(const desfirekey_t key
, uint8_t *ivect
, const uint8_t *data
, size_t len
, uint8_t *cmac
) {
284 int kbs
= key_block_size(key
);
288 uint8_t *buffer
= BigBuf_malloc(padded_data_length(len
, kbs
));
290 memcpy(buffer
, data
, len
);
292 if ((!len
) || (len
% kbs
)) {
293 buffer
[len
++] = 0x80;
295 buffer
[len
++] = 0x00;
297 xor(key
->cmac_sk2
, buffer
+ len
- kbs
, kbs
);
299 xor(key
->cmac_sk1
, buffer
+ len
- kbs
, kbs
);
302 mifare_cypher_blocks_chained(NULL
, key
, ivect
, buffer
, len
, MCD_SEND
, MCO_ENCYPHER
);
304 memcpy(cmac
, ivect
, kbs
);
308 size_t key_block_size(const desfirekey_t key
) {
311 size_t block_size
= 8;
326 * Size of MACing produced with the key.
328 static size_t key_macing_length(const desfirekey_t key
) {
329 size_t mac_length
= MAC_LENGTH
;
333 mac_length
= MAC_LENGTH
;
337 mac_length
= CMAC_LENGTH
;
344 * Size required to store nbytes of data in a buffer of size n*block_size.
346 size_t padded_data_length(const size_t nbytes
, const size_t block_size
) {
347 if ((!nbytes
) || (nbytes
% block_size
))
348 return ((nbytes
/ block_size
) + 1) * block_size
;
354 * Buffer size required to MAC nbytes of data
356 size_t maced_data_length(const desfirekey_t key
, const size_t nbytes
) {
357 return nbytes
+ key_macing_length(key
);
360 * Buffer size required to encipher nbytes of data and a two bytes CRC.
362 size_t enciphered_data_length(const desfiretag_t tag
, const size_t nbytes
, int communication_settings
) {
363 size_t crc_length
= 0;
364 if (!(communication_settings
& NO_CRC
)) {
365 switch (DESFIRE(tag
)->authentication_scheme
) {
375 size_t block_size
= DESFIRE(tag
)->session_key
? key_block_size(DESFIRE(tag
)->session_key
) : 1;
377 return padded_data_length(nbytes
+ crc_length
, block_size
);
380 void *mifare_cryto_preprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, size_t offset
, int communication_settings
) {
384 bool append_mac
= true;
385 desfirekey_t key
= DESFIRE(tag
)->session_key
;
390 switch (communication_settings
& MDCM_MASK
) {
392 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
396 * When using new authentication methods, PLAIN data transmission from
397 * the PICC to the PCD are CMACed, so we have to maintain the
398 * cryptographic initialisation vector up-to-date to check data
401 * The only difference with CMACed data transmission is that the CMAC
402 * is not apended to the data send by the PCD to the PICC.
409 switch (DESFIRE(tag
)->authentication_scheme
) {
411 if (!(communication_settings
& MAC_COMMAND
))
415 edl
= padded_data_length(*nbytes
- offset
, key_block_size(DESFIRE(tag
)->session_key
)) + offset
;
417 // Fill in the crypto buffer with data ...
418 memcpy(res
, data
, *nbytes
);
420 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
422 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, edl
- offset
, MCD_SEND
, MCO_ENCYPHER
);
424 memcpy(mac
, res
+ edl
- 8, 4);
426 // Copy again provided data (was overwritten by mifare_cypher_blocks_chained)
427 memcpy(res
, data
, *nbytes
);
429 if (!(communication_settings
& MAC_COMMAND
))
432 size_t bla
= maced_data_length(DESFIRE(tag
)->session_key
, *nbytes
- offset
) + offset
;
435 memcpy(res
+ *nbytes
, mac
, 4);
440 if (!(communication_settings
& CMAC_COMMAND
))
442 cmac(key
, DESFIRE(tag
)->ivect
, res
, *nbytes
, DESFIRE(tag
)->cmac
);
445 size_t len
= maced_data_length(key
, *nbytes
);
447 memcpy(res
, data
, *nbytes
);
448 memcpy(res
+ *nbytes
, DESFIRE(tag
)->cmac
, CMAC_LENGTH
);
449 *nbytes
+= CMAC_LENGTH
;
455 case MDCM_ENCIPHERED
:
456 /* |<-------------- data -------------->|
457 * |<--- offset -->| |
458 * +---------------+--------------------+-----+---------+
459 * | CMD + HEADERS | DATA TO BE SECURED | CRC | PADDING |
460 * +---------------+--------------------+-----+---------+ ----------------
461 * | |<~~~~v~~~~~~~~~~~~~>| ^ | | (DES / 3DES)
462 * | | `---- crc16() ----' | |
463 * | | | ^ | | ----- *or* -----
464 * |<~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~~~~>| ^ | | (3K3DES / AES)
465 * | `---- crc32() ----' | |
466 * | | ---- *then* ----
467 * |<---------------------------------->|
468 * encypher()/decypher()
471 if (!(communication_settings
& ENC_COMMAND
))
473 edl
= enciphered_data_length(tag
, *nbytes
- offset
, communication_settings
) + offset
;
475 // Fill in the crypto buffer with data ...
476 memcpy(res
, data
, *nbytes
);
477 if (!(communication_settings
& NO_CRC
)) {
479 switch (DESFIRE(tag
)->authentication_scheme
) {
481 AddCrc14A(res
+ offset
, *nbytes
- offset
);
485 crc32_append(res
, *nbytes
);
491 memset(res
+ *nbytes
, 0, edl
- *nbytes
);
495 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
+ offset
, *nbytes
- offset
, MCD_SEND
, (AS_NEW
== DESFIRE(tag
)->authentication_scheme
) ? MCO_ENCYPHER
: MCO_DECYPHER
);
508 void *mifare_cryto_postprocess_data(desfiretag_t tag
, void *data
, size_t *nbytes
, int communication_settings
) {
511 uint8_t first_cmac_byte
= 0x00;
513 desfirekey_t key
= DESFIRE(tag
)->session_key
;
518 // Return directly if we just have a status code.
522 switch (communication_settings
& MDCM_MASK
) {
525 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
530 switch (DESFIRE(tag
)->authentication_scheme
) {
532 if (communication_settings
& MAC_VERIFY
) {
533 *nbytes
-= key_macing_length(key
);
538 Dbprintf("No room for MAC!");
543 size_t edl
= enciphered_data_length(tag
, *nbytes
- 1, communication_settings
);
544 edata
= BigBuf_malloc(edl
);
546 memcpy(edata
, data
, *nbytes
- 1);
547 memset((uint8_t *)edata
+ *nbytes
- 1, 0, edl
- *nbytes
+ 1);
549 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, edata
, edl
, MCD_SEND
, MCO_ENCYPHER
);
551 if (0 != memcmp((uint8_t *)data
+ *nbytes
- 1, (uint8_t *)edata
+ edl
- 8, 4)) {
553 Dbprintf("MACing not verified");
554 hexdump((uint8_t *)data
+ *nbytes
- 1, key_macing_length(key
), "Expect ", 0);
555 hexdump((uint8_t *)edata
+ edl
- 8, key_macing_length(key
), "Actual ", 0);
557 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
564 if (!(communication_settings
& CMAC_COMMAND
))
566 if (communication_settings
& CMAC_VERIFY
) {
572 first_cmac_byte
= ((uint8_t *)data
)[*nbytes
- 9];
573 ((uint8_t *)data
)[*nbytes
- 9] = ((uint8_t *)data
)[*nbytes
- 1];
576 int n
= (communication_settings
& CMAC_VERIFY
) ? 8 : 0;
577 cmac(key
, DESFIRE(tag
)->ivect
, ((uint8_t *)data
), *nbytes
- n
, DESFIRE(tag
)->cmac
);
579 if (communication_settings
& CMAC_VERIFY
) {
580 ((uint8_t *)data
)[*nbytes
- 9] = first_cmac_byte
;
581 if (0 != memcmp(DESFIRE(tag
)->cmac
, (uint8_t *)data
+ *nbytes
- 9, 8)) {
583 Dbprintf("CMAC NOT verified :-(");
584 hexdump((uint8_t *)data
+ *nbytes
- 9, 8, "Expect ", 0);
585 hexdump(DESFIRE(tag
)->cmac
, 8, "Actual ", 0);
587 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
600 case MDCM_ENCIPHERED
:
602 bool verified
= false;
604 int end_crc_pos
= 0x00;
609 * ,-----------------+-------------------------------+--------+
610 * \ BLOCK n-1 | BLOCK n | STATUS |
611 * / PAYLOAD | CRC0 | CRC1 | 0x80? | 0x000000000000 | 0x9100 |
612 * `-----------------+-------------------------------+--------+
614 * <------------ DATA ------------>
615 * FRAME = PAYLOAD + CRC(PAYLOAD) + PADDING
618 * ,-------------------------------+-----------------------------------------------+--------+
619 * \ BLOCK n-1 | BLOCK n | STATUS |
620 * / PAYLOAD | CRC0 | CRC1 | CRC2 | CRC3 | 0x80? | 0x0000000000000000000000000000 | 0x9100 |
621 * `-------------------------------+-----------------------------------------------+--------+
622 * <----------------------------------- DATA ------------------------------------->|
624 * <----------------- DATA ---------------->
625 * FRAME = PAYLOAD + CRC(PAYLOAD + STATUS) + PADDING + STATUS
626 * `------------------'
629 mifare_cypher_blocks_chained(tag
, NULL
, NULL
, res
, *nbytes
, MCD_RECEIVE
, MCO_DECYPHER
);
632 * Look for the CRC and ensure it is followed by NULL padding. We
633 * can't start by the end because the CRC is supposed to be 0 when
634 * verified, and accumulating 0's in it should not change it.
636 switch (DESFIRE(tag
)->authentication_scheme
) {
638 crc_pos
= *nbytes
- 8 - 1; // The CRC can be over two blocks
645 /* Move status between payload and CRC */
646 res
= DESFIRE(tag
)->crypto_buffer
;
647 memcpy(res
, data
, *nbytes
);
649 crc_pos
= (*nbytes
) - 16 - 3;
654 memcpy((uint8_t *)res
+ crc_pos
+ 1, (uint8_t *)res
+ crc_pos
, *nbytes
- crc_pos
);
655 ((uint8_t *)res
)[crc_pos
] = 0x00;
662 uint16_t crc_16
= 0x00;
664 switch (DESFIRE(tag
)->authentication_scheme
) {
666 AddCrc14A((uint8_t *)res
, end_crc_pos
);
667 end_crc_pos
= crc_pos
+ 2;
674 end_crc_pos
= crc_pos
+ 4;
675 crc32_ex(res
, end_crc_pos
, (uint8_t *)&crc
);
680 for (int n
= end_crc_pos
; n
< *nbytes
- 1; n
++) {
681 uint8_t byte
= ((uint8_t *)res
)[n
];
682 if (!((0x00 == byte
) || ((0x80 == byte
) && (n
== end_crc_pos
))))
688 switch (DESFIRE(tag
)->authentication_scheme
) {
690 ((uint8_t *)data
)[(*nbytes
)++] = 0x00;
693 /* The status byte was already before the CRC */
697 switch (DESFIRE(tag
)->authentication_scheme
) {
701 x
= ((uint8_t *)res
)[crc_pos
- 1];
702 ((uint8_t *)res
)[crc_pos
- 1] = ((uint8_t *)res
)[crc_pos
];
703 ((uint8_t *)res
)[crc_pos
] = x
;
708 } while (!verified
&& (end_crc_pos
< *nbytes
));
712 /* FIXME In some configurations, the file is transmitted PLAIN */
713 Dbprintf("CRC not verified in decyphered stream");
715 DESFIRE(tag
)->last_pcd_error
= CRYPTO_ERROR
;
722 Dbprintf("Unknown communication settings");
732 void mifare_cypher_single_block(desfirekey_t key
, uint8_t *data
, uint8_t *ivect
, MifareCryptoDirection direction
, MifareCryptoOperation operation
, size_t block_size
) {
733 uint8_t ovect
[MAX_CRYPTO_BLOCK_SIZE
];
734 if (direction
== MCD_SEND
) {
735 xor(ivect
, data
, block_size
);
737 memcpy(ovect
, data
, block_size
);
740 uint8_t edata
[MAX_CRYPTO_BLOCK_SIZE
];
746 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
747 des_encrypt(edata
, data
, key
->data
);
750 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
751 des_decrypt(edata
, data
, key
->data
);
758 mbedtls_des3_set2key_enc(&ctx3
, key
->data
);
759 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
760 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
761 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
762 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
765 mbedtls_des3_set2key_dec(&ctx3
, key
->data
);
766 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
767 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
768 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
769 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
776 mbedtls_des3_set3key_enc(&ctx3
, key
->data
);
777 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
778 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
779 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
780 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_ENCRYPT);
783 mbedtls_des3_set3key_dec(&ctx3
, key
->data
);
784 mbedtls_des3_crypt_ecb(&ctx3
, data
, edata
);
785 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_DECRYPT);
786 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
787 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
794 mbedtls_aes_init(&actx
);
795 mbedtls_aes_setkey_enc(&actx
, key
->data
, 128);
796 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_ENCRYPT
, sizeof(edata
), ivect
, data
, edata
);
800 mbedtls_aes_init(&actx
);
801 mbedtls_aes_setkey_dec(&actx
, key
->data
, 128);
802 mbedtls_aes_crypt_cbc(&actx
, MBEDTLS_AES_DECRYPT
, sizeof(edata
), ivect
, edata
, data
);
809 memcpy(data
, edata
, block_size
);
811 if (direction
== MCD_SEND
) {
812 memcpy(ivect
, data
, block_size
);
814 xor(ivect
, data
, block_size
);
815 memcpy(ivect
, ovect
, block_size
);
820 * This function performs all CBC cyphering / deciphering.
822 * The tag argument may be NULL, in which case both key and ivect shall be set.
823 * When using the tag session_key and ivect for processing data, these
824 * arguments should be set to NULL.
826 * Because the tag may contain additional data, one may need to call this
827 * function with tag, key and ivect defined.
829 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
) {
834 key
= DESFIRE(tag
)->session_key
;
836 ivect
= DESFIRE(tag
)->ivect
;
838 switch (DESFIRE(tag
)->authentication_scheme
) {
840 memset(ivect
, 0, MAX_CRYPTO_BLOCK_SIZE
);
847 block_size
= key_block_size(key
);
850 while (offset
< data_size
) {
851 mifare_cypher_single_block(key
, data
+ offset
, ivect
, direction
, operation
, block_size
);
852 offset
+= block_size
;