e10211965fd713e4d10c9a3ab4950113d76a6828
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // iso14443-4 mifare commands
9 //-----------------------------------------------------------------------------
17 #include "polarssl/libpcrypto.h"
19 int CalculateMAC(mf4Session
*session
, uint8_t *data
, int datalen
, uint8_t *mac
, bool verbose
) {
20 if (!session
|| !session
->Authenticated
|| !mac
|| !data
|| !datalen
)
26 PrintAndLog("MAC data[%d]: %s", datalen
, sprint_hex(data
, datalen
));
28 return aes_cmac8(NULL
, session
->Key
, data
, mac
, datalen
);
31 int MifareAuth4(mf4Session
*session
, uint8_t *keyn
, uint8_t *key
, bool activateField
, bool leaveSignalON
, bool verbose
) {
32 uint8_t data
[257] = {0};
35 uint8_t Rnd1
[17] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00};
36 uint8_t Rnd2
[17] = {0};
39 session
->Authenticated
= false;
41 uint8_t cmd1
[] = {0x70, keyn
[1], keyn
[0], 0x00};
42 int res
= ExchangeRAW14a(cmd1
, sizeof(cmd1
), activateField
, true, data
, sizeof(data
), &datalen
);
44 PrintAndLog("ERROR exchande raw error: %d", res
);
50 PrintAndLog("<phase1: %s", sprint_hex(data
, datalen
));
53 PrintAndLog("ERROR: card response length: %d", datalen
);
58 if (data
[0] != 0x90) {
59 PrintAndLog("ERROR: card response error: %02x", data
[2]);
64 if (datalen
!= 19) { // code 1b + 16b + crc 2b
65 PrintAndLog("ERROR: card response must be 19 bytes long instead of: %d", datalen
);
70 aes_decode(NULL
, key
, &data
[1], Rnd2
, 16);
73 PrintAndLog("Rnd2: %s", sprint_hex(Rnd2
, 16));
75 uint8_t cmd2
[33] = {0};
78 uint8_t raw
[32] = {0};
79 memmove(raw
, Rnd1
, 16);
80 memmove(&raw
[16], &Rnd2
[1], 16);
82 aes_encode(NULL
, key
, raw
, &cmd2
[1], 32);
84 PrintAndLog(">phase2: %s", sprint_hex(cmd2
, 33));
86 res
= ExchangeRAW14a(cmd2
, sizeof(cmd2
), false, true, data
, sizeof(data
), &datalen
);
88 PrintAndLog("ERROR exchande raw error: %d", res
);
94 PrintAndLog("<phase2: %s", sprint_hex(data
, datalen
));
96 aes_decode(NULL
, key
, &data
[1], raw
, 32);
99 PrintAndLog("res: %s", sprint_hex(raw
, 32));
100 PrintAndLog("Rnd1`: %s", sprint_hex(&raw
[4], 16));
103 if (memcmp(&raw
[4], &Rnd1
[1], 16)) {
104 PrintAndLog("\nERROR: Authentication FAILED. rnd not equal");
106 PrintAndLog("rnd1 reader: %s", sprint_hex(&Rnd1
[1], 16));
107 PrintAndLog("rnd1 card: %s", sprint_hex(&raw
[4], 16));
120 session
->Authenticated
= true;
121 session
->KeyNum
= keyn
[1] + (keyn
[0] << 8);
122 memmove(session
->Rnd1
, Rnd1
, 16);
123 memmove(session
->Rnd2
, Rnd2
, 16);
124 memmove(session
->Key
, key
, 16);
127 PrintAndLog("Authentication OK");
132 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
133 // plus evtl. 8 sectors with 16 blocks each (4k cards)
134 uint8_t mfNumBlocksPerSector(uint8_t sectorNo
) {
141 uint8_t mfFirstBlockOfSector(uint8_t sectorNo
) {
145 return 32 * 4 + (sectorNo
- 32) * 16;
148 uint8_t mfSectorTrailer(uint8_t blockNo
) {
149 if (blockNo
< 32*4) {
150 return (blockNo
| 0x03);
152 return (blockNo
| 0x0f);
156 bool mfIsSectorTrailer(uint8_t blockNo
) {
157 return (blockNo
== mfSectorTrailer(blockNo
));
160 uint8_t mfSectorNum(uint8_t blockNo
) {
161 if (blockNo
< 32 * 4)
164 return 32 + (blockNo
- 32 * 4) / 16;