]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
Removed double print-out
[proxmark3-svn] / armsrc / mifareutil.c
CommitLineData
20f9a2a1 1//-----------------------------------------------------------------------------\r
b62a5a84 2// Merlok, May 2011, 2012\r
8f51ddb0 3// Many authors, whom made it possible\r
20f9a2a1
M
4//\r
5// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
6// at your option, any later version. See the LICENSE.txt file for the text of\r
7// the license.\r
8//-----------------------------------------------------------------------------\r
8f51ddb0 9// Work with mifare cards.\r
20f9a2a1
M
10//-----------------------------------------------------------------------------\r
11\r
12#include "proxmark3.h"\r
13#include "apps.h"\r
14#include "util.h"\r
15#include "string.h"\r
16\r
17#include "iso14443crc.h"\r
18#include "iso14443a.h"\r
19#include "crapto1.h"\r
20#include "mifareutil.h"\r
21\r
f397b5cc
M
22int MF_DBGLEVEL = MF_DBG_ALL;\r
23\r
8f51ddb0 24// memory management\r
20f9a2a1 25uint8_t* mifare_get_bigbufptr(void) {\r
8f51ddb0
M
26 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET); // was 3560 - tied to other size changes\r
27}\r
28uint8_t* eml_get_bigbufptr_sendbuf(void) {\r
29 return (((uint8_t *)BigBuf) + RECV_CMD_OFFSET); \r
30}\r
31uint8_t* eml_get_bigbufptr_recbuf(void) {\r
32 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET);\r
33}\r
34uint8_t* eml_get_bigbufptr_cardmem(void) {\r
35 return (((uint8_t *)BigBuf) + CARD_MEMORY);\r
36}\r
37\r
38// crypto1 helpers\r
39void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){\r
40 uint8_t bt = 0;\r
41 int i;\r
42 \r
43 if (len != 1) {\r
44 for (i = 0; i < len; i++)\r
45 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r
46 } else {\r
47 bt = 0;\r
48 for (i = 0; i < 4; i++)\r
49 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;\r
50 \r
51 data[0] = bt;\r
52 }\r
53 return;\r
54}\r
55\r
56void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, int len, uint32_t *par) {\r
57 uint8_t bt = 0;\r
58 int i;\r
59 uint32_t mltpl = 1 << (len - 1); // for len=18 it=0x20000\r
60 *par = 0;\r
61 for (i = 0; i < len; i++) {\r
62 bt = data[i];\r
63 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r
64 *par = (*par >> 1) | ( ((filter(pcs->odd) ^ oddparity(bt)) & 0x01) * mltpl );\r
65 } \r
66 return;\r
67}\r
68\r
69uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r
70 uint8_t bt = 0;\r
71 int i;\r
72\r
73 for (i = 0; i < 4; i++)\r
74 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;\r
75 \r
76 return bt;\r
20f9a2a1
M
77}\r
78\r
8f51ddb0 79// send commands\r
9492e0b0 80int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t *timing)\r
f89c7050 81{\r
9492e0b0 82 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, NULL, timing);\r
f89c7050
M
83}\r
84\r
9492e0b0 85int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t * parptr, uint32_t *timing)\r
20f9a2a1
M
86{\r
87 uint8_t dcmd[4], ecmd[4];\r
88 uint32_t pos, par, res;\r
89\r
90 dcmd[0] = cmd;\r
91 dcmd[1] = data;\r
92 AppendCrc14443a(dcmd, 2);\r
93 \r
94 memcpy(ecmd, dcmd, sizeof(dcmd));\r
95 \r
96 if (crypted) {\r
97 par = 0;\r
98 for (pos = 0; pos < 4; pos++)\r
99 {\r
100 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r
101 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) * 0x08 );\r
102 } \r
103\r
9492e0b0 104 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r
20f9a2a1
M
105\r
106 } else {\r
9492e0b0 107 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
20f9a2a1
M
108 }\r
109\r
f89c7050
M
110 int len = ReaderReceivePar(answer, &par);\r
111 \r
112 if (parptr) *parptr = par;\r
20f9a2a1 113\r
4abe4f58 114 if (crypted == CRYPT_ALL) {\r
20f9a2a1
M
115 if (len == 1) {\r
116 res = 0;\r
117 for (pos = 0; pos < 4; pos++)\r
118 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;\r
119 \r
120 answer[0] = res;\r
121 \r
122 } else {\r
123 for (pos = 0; pos < len; pos++)\r
124 {\r
125 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r
126 }\r
127 }\r
128 }\r
129 \r
130 return len;\r
131}\r
132\r
8f51ddb0 133// mifare commands\r
20f9a2a1 134int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested) \r
f89c7050 135{\r
9492e0b0 136 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r
f89c7050
M
137}\r
138\r
9492e0b0 139int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested, uint32_t * ntptr, uint32_t *timing) \r
20f9a2a1
M
140{\r
141 // variables\r
4abe4f58 142 int len; \r
20f9a2a1
M
143 uint32_t pos;\r
144 uint8_t tmp4[4];\r
4abe4f58
M
145 byte_t par = 0;\r
146 byte_t ar[4];\r
20f9a2a1
M
147 uint32_t nt, ntpp; // Supplied tag nonce\r
148 \r
149 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r
4abe4f58 150 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 151\r
4abe4f58 152 // Transmit MIFARE_CLASSIC_AUTH\r
9492e0b0 153 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, timing);\r
154 if (MF_DBGLEVEL >= 4) Dbprintf("rand nonce len: %x", len); \r
4abe4f58 155 if (len != 4) return 1;\r
20f9a2a1
M
156 \r
157 ar[0] = 0x55;\r
158 ar[1] = 0x41;\r
159 ar[2] = 0x49;\r
160 ar[3] = 0x92; \r
161 \r
162 // Save the tag nonce (nt)\r
163 nt = bytes_to_num(receivedAnswer, 4);\r
20f9a2a1
M
164\r
165 // ----------------------------- crypto1 create\r
4abe4f58
M
166 if (isNested)\r
167 crypto1_destroy(pcs);\r
168\r
169 // Init cipher with key\r
20f9a2a1
M
170 crypto1_create(pcs, ui64Key);\r
171\r
4abe4f58
M
172 if (isNested == AUTH_NESTED) {\r
173 // decrypt nt with help of new key \r
174 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;\r
175 } else {\r
176 // Load (plain) uid^nt into the cipher\r
177 crypto1_word(pcs, nt ^ uid, 0);\r
178 }\r
179\r
180 // some statistic\r
f397b5cc 181 if (!ntptr && (MF_DBGLEVEL >= 3))\r
f89c7050
M
182 Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r
183 \r
184 // save Nt\r
185 if (ntptr)\r
186 *ntptr = nt;\r
20f9a2a1
M
187\r
188 par = 0;\r
4abe4f58
M
189 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r
190 for (pos = 0; pos < 4; pos++)\r
191 {\r
192 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];\r
20f9a2a1 193 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );\r
4abe4f58 194 } \r
20f9a2a1 195 \r
4abe4f58
M
196 // Skip 32 bits in pseudo random generator\r
197 nt = prng_successor(nt,32);\r
20f9a2a1
M
198\r
199 // ar+parity\r
4abe4f58
M
200 for (pos = 4; pos < 8; pos++)\r
201 {\r
20f9a2a1 202 nt = prng_successor(nt,8);\r
4abe4f58 203 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
20f9a2a1 204 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );\r
4abe4f58 205 } \r
20f9a2a1 206 \r
4abe4f58 207 // Transmit reader nonce and reader answer\r
9492e0b0 208 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r
20f9a2a1 209\r
4abe4f58 210 // Receive 4 bit answer\r
20f9a2a1 211 len = ReaderReceive(receivedAnswer);\r
4abe4f58
M
212 if (!len)\r
213 {\r
f397b5cc 214 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r
20f9a2a1 215 return 2;\r
4abe4f58 216 }\r
20f9a2a1 217 \r
4abe4f58 218 memcpy(tmp4, receivedAnswer, 4);\r
20f9a2a1
M
219 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r
220 \r
221 if (ntpp != bytes_to_num(tmp4, 4)) {\r
f397b5cc 222 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r
20f9a2a1
M
223 return 3;\r
224 }\r
225\r
226 return 0;\r
227}\r
228\r
229int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
230{\r
231 // variables\r
4abe4f58 232 int len; \r
20f9a2a1
M
233 uint8_t bt[2];\r
234 \r
4abe4f58 235 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 236 \r
4abe4f58 237 // command MIFARE_CLASSIC_READBLOCK\r
9492e0b0 238 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, NULL);\r
20f9a2a1 239 if (len == 1) {\r
f397b5cc 240 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
241 return 1;\r
242 }\r
243 if (len != 18) {\r
f397b5cc 244 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r
20f9a2a1
M
245 return 2;\r
246 }\r
247\r
248 memcpy(bt, receivedAnswer + 16, 2);\r
4abe4f58 249 AppendCrc14443a(receivedAnswer, 16);\r
20f9a2a1 250 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
f397b5cc 251 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r
20f9a2a1
M
252 return 3;\r
253 }\r
254 \r
255 memcpy(blockData, receivedAnswer, 16);\r
256 return 0;\r
257}\r
258\r
259int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
260{\r
261 // variables\r
4abe4f58 262 int len, i; \r
20f9a2a1 263 uint32_t pos;\r
4abe4f58
M
264 uint32_t par = 0;\r
265 byte_t res;\r
20f9a2a1
M
266 \r
267 uint8_t d_block[18], d_block_enc[18];\r
4abe4f58 268 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 269 \r
4abe4f58 270 // command MIFARE_CLASSIC_WRITEBLOCK\r
9492e0b0 271 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, NULL);\r
20f9a2a1
M
272\r
273 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
f397b5cc 274 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
275 return 1;\r
276 }\r
277 \r
278 memcpy(d_block, blockData, 16);\r
279 AppendCrc14443a(d_block, 16);\r
280 \r
281 // crypto\r
282 par = 0;\r
4abe4f58
M
283 for (pos = 0; pos < 18; pos++)\r
284 {\r
285 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
20f9a2a1 286 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );\r
4abe4f58 287 } \r
20f9a2a1 288\r
9492e0b0 289 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
20f9a2a1 290\r
4abe4f58 291 // Receive the response\r
20f9a2a1
M
292 len = ReaderReceive(receivedAnswer); \r
293\r
294 res = 0;\r
295 for (i = 0; i < 4; i++)\r
296 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r
297\r
298 if ((len != 1) || (res != 0x0A)) {\r
f397b5cc 299 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
20f9a2a1
M
300 return 2;\r
301 }\r
302 \r
303 return 0;\r
304}\r
305\r
306int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
307{\r
308 // variables\r
4abe4f58 309 int len; \r
20f9a2a1
M
310 \r
311 // Mifare HALT\r
4abe4f58 312 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 313\r
9492e0b0 314 len = mifare_sendcmd_short(pcs, pcs == NULL ? 0:1, 0x50, 0x00, receivedAnswer, NULL);\r
4abe4f58 315 if (len != 0) {\r
f397b5cc 316 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len); \r
20f9a2a1
M
317 return 1;\r
318 }\r
319\r
320 return 0;\r
321}\r
9ca155ba 322\r
8f51ddb0
M
323// work with emulator memory\r
324void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
325 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
326 \r
327 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
328}\r
329\r
330void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
331 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
332 \r
333 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
334}\r
335\r
336void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
337 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
338 \r
339 memcpy(data, emCARD + bytePtr, byteCount);\r
340}\r
341\r
0014cb46
M
342int emlCheckValBl(int blockNum) {\r
343 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
344 uint8_t* data = emCARD + blockNum * 16;\r
345\r
346 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r
347 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r
348 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r
349 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r
350 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r
351 (data[12] != (data[15] ^ 0xff))\r
352 ) \r
353 return 1;\r
354 return 0;\r
355}\r
356\r
357int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
358 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
359 uint8_t* data = emCARD + blockNum * 16;\r
360 \r
361 if (emlCheckValBl(blockNum)) {\r
362 return 1;\r
363 }\r
364 \r
365 memcpy(blReg, data, 4);\r
366 *blBlock = data[12];\r
367 \r
368 return 0;\r
369}\r
370\r
371int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
372 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
373 uint8_t* data = emCARD + blockNum * 16;\r
374 \r
375 memcpy(data + 0, &blReg, 4);\r
376 memcpy(data + 8, &blReg, 4);\r
377 blReg = blReg ^ 0xffffffff;\r
378 memcpy(data + 4, &blReg, 4);\r
379 \r
380 data[12] = blBlock;\r
381 data[13] = blBlock ^ 0xff;\r
382 data[14] = blBlock;\r
383 data[15] = blBlock ^ 0xff;\r
384 \r
385 return 0;\r
386}\r
387\r
8556b852
M
388uint64_t emlGetKey(int sectorNum, int keyType) {\r
389 uint8_t key[6];\r
390 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
391 \r
392 memcpy(key, emCARD + 3 * 16 + sectorNum * 4 * 16 + keyType * 10, 6);\r
393 return bytes_to_num(key, 6);\r
394}\r
395\r
8f51ddb0 396void emlClearMem(void) {\r
aea4d766 397 int b;\r
8f51ddb0
M
398 \r
399 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
8f51ddb0 400 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
aea4d766 401 uint8_t* emCARD = eml_get_bigbufptr_cardmem();\r
402 \r
403 memset(emCARD, 0, CARD_MEMORY_LEN);\r
404 \r
405 // fill sectors trailer data\r
406 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r
407 emlSetMem((uint8_t *)trailer, b , 1);\r
408 } \r
8f51ddb0
M
409\r
410 // uid\r
411 emlSetMem((uint8_t *)uid, 0, 1);\r
412 return;\r
413}\r
Impressum, Datenschutz