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"
30 static void xor (const uint8_t *ivect
, uint8_t *data
, const size_t len
);
31 static size_t key_macing_length (desfirekey_t key
);
33 static void xor (const uint8_t *ivect
, uint8_t *data
, const size_t len
) {
34 for (size_t i
= 0; i
< len
; i
++) {
39 void cmac_generate_subkeys ( desfirekey_t key
) {
40 int kbs
= key_block_size (key
);
41 const uint8_t R
= (kbs
== 8) ? 0x1B : 0x87;
47 memset (ivect
, 0, kbs
);
49 mifare_cypher_blocks_chained (NULL
, key
, ivect
, l
, kbs
, MCD_RECEIVE
, MCO_ENCYPHER
);
53 // Used to compute CMAC on complete blocks
54 memcpy (key
->cmac_sk1
, l
, kbs
);
56 lsl (key
->cmac_sk1
, kbs
);
58 key
->cmac_sk1
[kbs
-1] ^= R
;
60 // Used to compute CMAC on the last block if non-complete
61 memcpy (key
->cmac_sk2
, key
->cmac_sk1
, kbs
);
62 xor = key
->cmac_sk1
[0] & 0x80;
63 lsl (key
->cmac_sk2
, kbs
);
65 key
->cmac_sk2
[kbs
-1] ^= R
;
68 void cmac (const desfirekey_t key
, uint8_t *ivect
, const uint8_t *data
, size_t len
, uint8_t *cmac
) {
69 int kbs
= key_block_size (key
);
70 uint8_t *buffer
= malloc (padded_data_length (len
, kbs
));
72 memcpy (buffer
, data
, len
);
74 if ((!len
) || (len
% kbs
)) {
79 xor (key
->cmac_sk2
, buffer
+ len
- kbs
, kbs
);
81 xor (key
->cmac_sk1
, buffer
+ len
- kbs
, kbs
);
84 mifare_cypher_blocks_chained (NULL
, key
, ivect
, buffer
, len
, MCD_SEND
, MCO_ENCYPHER
);
86 memcpy (cmac
, ivect
, kbs
);
89 size_t key_block_size (const desfirekey_t key
) {
90 size_t block_size
= 8;
107 * Size of MACing produced with the key.
109 static size_t key_macing_length (const desfirekey_t key
) {
110 size_t mac_length
= MAC_LENGTH
;
115 mac_length
= MAC_LENGTH
;
119 mac_length
= CMAC_LENGTH
;
127 * Size required to store nbytes of data in a buffer of size n*block_size.
129 size_t padded_data_length (const size_t nbytes
, const size_t block_size
) {
130 if ((!nbytes
) || (nbytes
% block_size
))
131 return ((nbytes
/ block_size
) + 1) * block_size
;
137 * Buffer size required to MAC nbytes of data
139 size_t maced_data_length (const desfirekey_t key
, const size_t nbytes
) {
140 return nbytes
+ key_macing_length (key
);
143 * Buffer size required to encipher nbytes of data and a two bytes CRC.
145 size_t enciphered_data_length (const desfiretag_t tag
, const size_t nbytes
, int communication_settings
) {
146 size_t crc_length
= 0;
147 if (!(communication_settings
& NO_CRC
)) {
148 switch (DESFIRE(tag
)->authentication_scheme
) {
158 size_t block_size
= DESFIRE(tag
)->session_key
? key_block_size (DESFIRE(tag
)->session_key
) : 1;
160 return padded_data_length (nbytes
+ crc_length
, block_size
);
163 void* mifare_cryto_preprocess_data (desfiretag_t tag
, void *data
, size_t *nbytes
, off_t offset
, int communication_settings
) {
167 bool append_mac
= true;
168 desfirekey_t key
= DESFIRE(tag
)->session_key
;
173 switch (communication_settings
& MDCM_MASK
) {
175 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
179 * When using new authentication methods, PLAIN data transmission from
180 * the PICC to the PCD are CMACed, so we have to maintain the
181 * cryptographic initialisation vector up-to-date to check data
184 * The only difference with CMACed data transmission is that the CMAC
185 * is not apended to the data send by the PCD to the PICC.
192 switch (DESFIRE(tag
)->authentication_scheme
) {
194 if (!(communication_settings
& MAC_COMMAND
))
198 edl
= padded_data_length (*nbytes
- offset
, key_block_size (DESFIRE(tag
)->session_key
)) + offset
;
200 // Fill in the crypto buffer with data ...
201 memcpy (res
, data
, *nbytes
);
203 memset (res
+ *nbytes
, 0, edl
- *nbytes
);
205 mifare_cypher_blocks_chained (tag
, NULL
, NULL
, res
+ offset
, edl
- offset
, MCD_SEND
, MCO_ENCYPHER
);
207 memcpy (mac
, res
+ edl
- 8, 4);
209 // Copy again provided data (was overwritten by mifare_cypher_blocks_chained)
210 memcpy (res
, data
, *nbytes
);
212 if (!(communication_settings
& MAC_COMMAND
))
215 size_t bla
= maced_data_length (DESFIRE(tag
)->session_key
, *nbytes
- offset
) + offset
;
218 memcpy (res
+ *nbytes
, mac
, 4);
223 if (!(communication_settings
& CMAC_COMMAND
))
225 cmac (key
, DESFIRE (tag
)->ivect
, res
, *nbytes
, DESFIRE (tag
)->cmac
);
228 maced_data_length (key
, *nbytes
);
230 memcpy (res
, data
, *nbytes
);
231 memcpy (res
+ *nbytes
, DESFIRE (tag
)->cmac
, CMAC_LENGTH
);
232 *nbytes
+= CMAC_LENGTH
;
238 case MDCM_ENCIPHERED
:
239 /* |<-------------- data -------------->|
240 * |<--- offset -->| |
241 * +---------------+--------------------+-----+---------+
242 * | CMD + HEADERS | DATA TO BE SECURED | CRC | PADDING |
243 * +---------------+--------------------+-----+---------+ ----------------
244 * | |<~~~~v~~~~~~~~~~~~~>| ^ | | (DES / 3DES)
245 * | | `---- crc16() ----' | |
246 * | | | ^ | | ----- *or* -----
247 * |<~~~~~~~~~~~~~~~~~~~~v~~~~~~~~~~~~~>| ^ | | (3K3DES / AES)
248 * | `---- crc32() ----' | |
249 * | | ---- *then* ----
250 * |<---------------------------------->|
251 * encypher()/decypher()
254 if (!(communication_settings
& ENC_COMMAND
))
256 edl
= enciphered_data_length (tag
, *nbytes
- offset
, communication_settings
) + offset
;
258 // Fill in the crypto buffer with data ...
259 memcpy (res
, data
, *nbytes
);
260 if (!(communication_settings
& NO_CRC
)) {
262 switch (DESFIRE (tag
)->authentication_scheme
) {
264 AppendCrc14443a(res
+ offset
, *nbytes
- offset
);
268 crc32_append (res
, *nbytes
);
274 memset (res
+ *nbytes
, 0, edl
- *nbytes
);
278 mifare_cypher_blocks_chained (tag
, NULL
, NULL
, res
+ offset
, *nbytes
- offset
, MCD_SEND
, (AS_NEW
== DESFIRE(tag
)->authentication_scheme
) ? MCO_ENCYPHER
: MCO_DECYPHER
);
291 void* mifare_cryto_postprocess_data (desfiretag_t tag
, void *data
, ssize_t
*nbytes
, int communication_settings
)
296 uint8_t first_cmac_byte
= 0x00;
298 desfirekey_t key
= DESFIRE(tag
)->session_key
;
303 // Return directly if we just have a status code.
307 switch (communication_settings
& MDCM_MASK
) {
310 if (AS_LEGACY
== DESFIRE(tag
)->authentication_scheme
)
315 switch (DESFIRE (tag
)->authentication_scheme
) {
317 if (communication_settings
& MAC_VERIFY
) {
318 *nbytes
-= key_macing_length (key
);
323 Dbprintf ("No room for MAC!");
328 edl
= enciphered_data_length (tag
, *nbytes
- 1, communication_settings
);
329 edata
= malloc (edl
);
331 memcpy (edata
, data
, *nbytes
- 1);
332 memset ((uint8_t *)edata
+ *nbytes
- 1, 0, edl
- *nbytes
+ 1);
334 mifare_cypher_blocks_chained (tag
, NULL
, NULL
, edata
, edl
, MCD_SEND
, MCO_ENCYPHER
);
336 if (0 != memcmp ((uint8_t *)data
+ *nbytes
- 1, (uint8_t *)edata
+ edl
- 8, 4)) {
338 Dbprintf ("MACing not verified");
339 hexdump ((uint8_t *)data
+ *nbytes
- 1, key_macing_length (key
), "Expect ", 0);
340 hexdump ((uint8_t *)edata
+ edl
- 8, key_macing_length (key
), "Actual ", 0);
342 DESFIRE (tag
)->last_pcd_error
= CRYPTO_ERROR
;
349 if (!(communication_settings
& CMAC_COMMAND
))
351 if (communication_settings
& CMAC_VERIFY
) {
357 first_cmac_byte
= ((uint8_t *)data
)[*nbytes
- 9];
358 ((uint8_t *)data
)[*nbytes
- 9] = ((uint8_t *)data
)[*nbytes
-1];
361 int n
= (communication_settings
& CMAC_VERIFY
) ? 8 : 0;
362 cmac (key
, DESFIRE (tag
)->ivect
, ((uint8_t *)data
), *nbytes
- n
, DESFIRE (tag
)->cmac
);
364 if (communication_settings
& CMAC_VERIFY
) {
365 ((uint8_t *)data
)[*nbytes
- 9] = first_cmac_byte
;
366 if (0 != memcmp (DESFIRE (tag
)->cmac
, (uint8_t *)data
+ *nbytes
- 9, 8)) {
368 Dbprintf ("CMAC NOT verified :-(");
369 hexdump ((uint8_t *)data
+ *nbytes
- 9, 8, "Expect ", 0);
370 hexdump (DESFIRE (tag
)->cmac
, 8, "Actual ", 0);
372 DESFIRE (tag
)->last_pcd_error
= CRYPTO_ERROR
;
385 case MDCM_ENCIPHERED
:
387 bool verified
= false;
389 int end_crc_pos
= 0x00;
394 * ,-----------------+-------------------------------+--------+
395 * \ BLOCK n-1 | BLOCK n | STATUS |
396 * / PAYLOAD | CRC0 | CRC1 | 0x80? | 0x000000000000 | 0x9100 |
397 * `-----------------+-------------------------------+--------+
399 * <------------ DATA ------------>
400 * FRAME = PAYLOAD + CRC(PAYLOAD) + PADDING
403 * ,-------------------------------+-----------------------------------------------+--------+
404 * \ BLOCK n-1 | BLOCK n | STATUS |
405 * / PAYLOAD | CRC0 | CRC1 | CRC2 | CRC3 | 0x80? | 0x0000000000000000000000000000 | 0x9100 |
406 * `-------------------------------+-----------------------------------------------+--------+
407 * <----------------------------------- DATA ------------------------------------->|
409 * <----------------- DATA ---------------->
410 * FRAME = PAYLOAD + CRC(PAYLOAD + STATUS) + PADDING + STATUS
411 * `------------------'
414 mifare_cypher_blocks_chained (tag
, NULL
, NULL
, res
, *nbytes
, MCD_RECEIVE
, MCO_DECYPHER
);
417 * Look for the CRC and ensure it is followed by NULL padding. We
418 * can't start by the end because the CRC is supposed to be 0 when
419 * verified, and accumulating 0's in it should not change it.
421 switch (DESFIRE (tag
)->authentication_scheme
) {
423 crc_pos
= *nbytes
- 8 - 1; // The CRC can be over two blocks
430 /* Move status between payload and CRC */
431 res
= DESFIRE (tag
)->crypto_buffer
;
432 memcpy (res
, data
, *nbytes
);
434 crc_pos
= (*nbytes
) - 16 - 3;
439 memcpy ((uint8_t *)res
+ crc_pos
+ 1, (uint8_t *)res
+ crc_pos
, *nbytes
- crc_pos
);
440 ((uint8_t *)res
)[crc_pos
] = 0x00;
447 uint16_t crc16
=0x00;
449 switch (DESFIRE (tag
)->authentication_scheme
) {
451 end_crc_pos
= crc_pos
+ 2;
452 AppendCrc14443a (res
, end_crc_pos
);
460 end_crc_pos
= crc_pos
+ 4;
461 crc32 (res
, end_crc_pos
, (uint8_t *)&crc
);
466 for (int n
= end_crc_pos
; n
< *nbytes
- 1; n
++) {
467 uint8_t byte
= ((uint8_t *)res
)[n
];
468 if (!( (0x00 == byte
) || ((0x80 == byte
) && (n
== end_crc_pos
)) ))
474 switch (DESFIRE (tag
)->authentication_scheme
) {
476 ((uint8_t *)data
)[(*nbytes
)++] = 0x00;
479 /* The status byte was already before the CRC */
483 switch (DESFIRE (tag
)->authentication_scheme
) {
487 x
= ((uint8_t *)res
)[crc_pos
- 1];
488 ((uint8_t *)res
)[crc_pos
- 1] = ((uint8_t *)res
)[crc_pos
];
489 ((uint8_t *)res
)[crc_pos
] = x
;
494 } while (!verified
&& (end_crc_pos
< *nbytes
));
498 /* FIXME In some configurations, the file is transmitted PLAIN */
499 Dbprintf("CRC not verified in decyphered stream");
501 DESFIRE (tag
)->last_pcd_error
= CRYPTO_ERROR
;
508 Dbprintf("Unknown communication settings");
518 void mifare_cypher_single_block (desfirekey_t key
, uint8_t *data
, uint8_t *ivect
, MifareCryptoDirection direction
, MifareCryptoOperation operation
, size_t block_size
)
520 uint8_t ovect
[MAX_CRYPTO_BLOCK_SIZE
];
522 if (direction
== MCD_SEND
) {
523 xor (ivect
, data
, block_size
);
525 memcpy (ovect
, data
, block_size
);
528 uint8_t edata
[MAX_CRYPTO_BLOCK_SIZE
];
534 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
535 des_enc(edata
, data
, key
->data
);
538 //DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
539 des_dec(edata
, data
, key
->data
);
546 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
547 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
548 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
549 tdes_enc(edata
,data
, key
->data
);
552 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
553 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
554 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
555 tdes_dec(data
, edata
, key
->data
);
562 tdes_enc(edata
,data
, key
->data
);
563 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT);
564 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT);
565 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_ENCRYPT);
568 tdes_dec(data
, edata
, key
->data
);
569 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks3), DES_DECRYPT);
570 // DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT);
571 // DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT);
581 AesCtxIni(&ctx
, ivect
, key
->data
, KEY128
,CBC
);
582 AesEncrypt(&ctx
, data
, edata
, sizeof(data
) );
588 AesCtxIni(&ctx
, ivect
, key
->data
, KEY128
,CBC
);
589 AesDecrypt(&ctx
, edata
, data
, sizeof(edata
));
596 memcpy (data
, edata
, block_size
);
598 if (direction
== MCD_SEND
) {
599 memcpy (ivect
, data
, block_size
);
601 xor (ivect
, data
, block_size
);
602 memcpy (ivect
, ovect
, block_size
);
607 * This function performs all CBC cyphering / deciphering.
609 * The tag argument may be NULL, in which case both key and ivect shall be set.
610 * When using the tag session_key and ivect for processing data, these
611 * arguments should be set to NULL.
613 * Because the tag may contain additional data, one may need to call this
614 * function with tag, key and ivect defined.
616 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
) {
621 key
= DESFIRE (tag
)->session_key
;
623 ivect
= DESFIRE (tag
)->ivect
;
625 switch (DESFIRE (tag
)->authentication_scheme
) {
627 memset (ivect
, 0, MAX_CRYPTO_BLOCK_SIZE
);
634 block_size
= key_block_size (key
);
637 while (offset
< data_size
) {
638 mifare_cypher_single_block (key
, data
+ offset
, ivect
, direction
, operation
, block_size
);
639 offset
+= block_size
;