]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
minor adjustment to askmandemod for lf search
[proxmark3-svn] / armsrc / mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011, 2012
3 // Many authors, whom made it possible
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Work with mifare cards.
10 //-----------------------------------------------------------------------------
11
12 #include "proxmark3.h"
13 #include "apps.h"
14 #include "util.h"
15 #include "string.h"
16
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
19 #include "crapto1.h"
20 #include "mifareutil.h"
21
22 int MF_DBGLEVEL = MF_DBG_ALL;
23
24 // memory management
25 uint8_t* get_bigbufptr_recvrespbuf(void) {
26 return (((uint8_t *)BigBuf) + RECV_RESP_OFFSET);
27 }
28 uint8_t* get_bigbufptr_recvcmdbuf(void) {
29 return (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
30 }
31 uint8_t* get_bigbufptr_emlcardmem(void) {
32 return (((uint8_t *)BigBuf) + CARD_MEMORY_OFFSET);
33 }
34
35 // crypto1 helpers
36 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
37 uint8_t bt = 0;
38 int i;
39
40 if (len != 1) {
41 for (i = 0; i < len; i++)
42 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
43 } else {
44 bt = 0;
45 for (i = 0; i < 4; i++)
46 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;
47
48 data[0] = bt;
49 }
50 return;
51 }
52
53 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
54 uint8_t bt = 0;
55 int i;
56 par[0] = 0;
57 for (i = 0; i < len; i++) {
58 bt = data[i];
59 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
60 if((i&0x0007) == 0) par[i>>3] = 0;
61 par[i>>3] |= (((filter(pcs->odd) ^ oddparity(bt)) & 0x01)<<(7-(i&0x0007)));
62 }
63 return;
64 }
65
66 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
67 uint8_t bt = 0;
68 int i;
69
70 for (i = 0; i < 4; i++)
71 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
72
73 return bt;
74 }
75
76 // send commands
77 int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
78 {
79 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing);
80 }
81
82 int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
83 {
84 uint8_t dcmd[8];//, ecmd[4];
85 //uint32_t par=0;
86
87 dcmd[0] = cmd;
88 dcmd[1] = data[0];
89 dcmd[2] = data[1];
90 dcmd[3] = data[2];
91 dcmd[4] = data[3];
92 dcmd[5] = data[4];
93 AppendCrc14443a(dcmd, 6);
94 //Dbprintf("Data command: %02x", dcmd[0]);
95 //Dbprintf("Data R: %02x %02x %02x %02x %02x %02x %02x", dcmd[1],dcmd[2],dcmd[3],dcmd[4],dcmd[5],dcmd[6],dcmd[7]);
96
97 //memcpy(ecmd, dcmd, sizeof(dcmd));
98 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
99 int len = ReaderReceive(answer, answer_parity);
100 if(!len)
101 {
102 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
103 return 2;
104 }
105 return len;
106 }
107
108 int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)
109 {
110 uint8_t dcmd[4], ecmd[4];
111 uint16_t pos, res;
112 uint8_t par[1]; // 1 Byte parity is enough here
113 dcmd[0] = cmd;
114 dcmd[1] = data;
115 AppendCrc14443a(dcmd, 2);
116
117 memcpy(ecmd, dcmd, sizeof(dcmd));
118
119 if (crypted) {
120 par[0] = 0;
121 for (pos = 0; pos < 4; pos++)
122 {
123 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
124 par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));
125 }
126
127 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
128
129 } else {
130 ReaderTransmit(dcmd, sizeof(dcmd), timing);
131 }
132
133 int len = ReaderReceive(answer, par);
134
135 if (answer_parity) *answer_parity = par[0];
136
137 if (crypted == CRYPT_ALL) {
138 if (len == 1) {
139 res = 0;
140 for (pos = 0; pos < 4; pos++)
141 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
142
143 answer[0] = res;
144
145 } else {
146 for (pos = 0; pos < len; pos++)
147 {
148 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
149 }
150 }
151 }
152
153 return len;
154 }
155
156 // mifare commands
157 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested)
158 {
159 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
160 }
161
162 int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested, uint32_t *ntptr, uint32_t *timing)
163 {
164 // variables
165 int len;
166 uint32_t pos;
167 uint8_t tmp4[4];
168 uint8_t par[1] = {0};
169 byte_t nr[4];
170 uint32_t nt, ntpp; // Supplied tag nonce
171
172 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
173 uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
174 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
175
176 // Transmit MIFARE_CLASSIC_AUTH
177 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
178 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
179 if (len != 4) return 1;
180
181 // "random" reader nonce:
182 nr[0] = 0x55;
183 nr[1] = 0x41;
184 nr[2] = 0x49;
185 nr[3] = 0x92;
186
187 // Save the tag nonce (nt)
188 nt = bytes_to_num(receivedAnswer, 4);
189
190 // ----------------------------- crypto1 create
191 if (isNested)
192 crypto1_destroy(pcs);
193
194 // Init cipher with key
195 crypto1_create(pcs, ui64Key);
196
197 if (isNested == AUTH_NESTED) {
198 // decrypt nt with help of new key
199 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
200 } else {
201 // Load (plain) uid^nt into the cipher
202 crypto1_word(pcs, nt ^ uid, 0);
203 }
204
205 // some statistic
206 if (!ntptr && (MF_DBGLEVEL >= 3))
207 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
208
209 // save Nt
210 if (ntptr)
211 *ntptr = nt;
212
213
214 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
215 par[0] = 0;
216 for (pos = 0; pos < 4; pos++)
217 {
218 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
219 par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));
220 }
221
222 // Skip 32 bits in pseudo random generator
223 nt = prng_successor(nt,32);
224
225 // ar+parity
226 for (pos = 4; pos < 8; pos++)
227 {
228 nt = prng_successor(nt,8);
229 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
230 par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));
231 }
232
233 // Transmit reader nonce and reader answer
234 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
235
236 // Receive 4 byte tag answer
237 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
238 if (!len)
239 {
240 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
241 return 2;
242 }
243
244 memcpy(tmp4, receivedAnswer, 4);
245 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
246
247 if (ntpp != bytes_to_num(tmp4, 4)) {
248 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
249 return 3;
250 }
251
252 return 0;
253 }
254
255 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
256 {
257 // variables
258 int len;
259 uint8_t bt[2];
260
261 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
262 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
263
264 // command MIFARE_CLASSIC_READBLOCK
265 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
266 if (len == 1) {
267 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
268 return 1;
269 }
270 if (len != 18) {
271 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
272 return 2;
273 }
274
275 memcpy(bt, receivedAnswer + 16, 2);
276 AppendCrc14443a(receivedAnswer, 16);
277 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
278 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
279 return 3;
280 }
281
282 memcpy(blockData, receivedAnswer, 16);
283 return 0;
284 }
285
286 int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
287 {
288 // variables
289 uint16_t len;
290 uint8_t bt[2];
291
292 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
293 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
294
295 // command MIFARE_CLASSIC_READBLOCK
296 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
297 if (len == 1) {
298 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
299 return 1;
300 }
301 if (len != 18) {
302 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
303 return 2;
304 }
305
306 memcpy(bt, receivedAnswer + 16, 2);
307 AppendCrc14443a(receivedAnswer, 16);
308 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
309 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
310 return 3;
311 }
312
313 memcpy(blockData, receivedAnswer, 14);
314 return 0;
315 }
316
317
318 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
319 {
320 // variables
321 int len, i;
322 uint32_t pos;
323 uint8_t par[3] = {0}; // enough for 18 Bytes to send
324 byte_t res;
325
326 uint8_t d_block[18], d_block_enc[18];
327 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
328 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
329
330 // command MIFARE_CLASSIC_WRITEBLOCK
331 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
332
333 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
334 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
335 return 1;
336 }
337
338 memcpy(d_block, blockData, 16);
339 AppendCrc14443a(d_block, 16);
340
341 // crypto
342 for (pos = 0; pos < 18; pos++)
343 {
344 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
345 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
346 }
347
348 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
349
350 // Receive the response
351 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
352
353 res = 0;
354 for (i = 0; i < 4; i++)
355 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
356
357 if ((len != 1) || (res != 0x0A)) {
358 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
359 return 2;
360 }
361
362 return 0;
363 }
364
365 int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
366 {
367 // variables
368 uint16_t len;
369 uint8_t par[3] = {0}; // enough for 18 parity bits
370
371 uint8_t d_block[18];
372 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
373 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
374
375 // command MIFARE_CLASSIC_WRITEBLOCK
376 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
377
378 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
379 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
380 return 1;
381 }
382
383 memset(d_block,'\0',18);
384 memcpy(d_block, blockData, 16);
385 AppendCrc14443a(d_block, 16);
386
387 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
388
389 // Receive the response
390 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
391
392 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
393 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
394 return 2;
395 }
396
397 return 0;
398 }
399
400 int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
401 {
402 uint16_t len;
403
404 uint8_t d_block[8];
405 uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
406 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
407
408 // command MIFARE_CLASSIC_WRITEBLOCK
409 memset(d_block,'\0',8);
410 d_block[0]= blockNo;
411 memcpy(d_block+1,blockData,4);
412 AppendCrc14443a(d_block, 6);
413
414 //i know the data send here is correct
415 len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
416
417 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
418 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
419 return 1;
420 }
421
422 return 0;
423 }
424
425 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
426 {
427 // variables
428 uint16_t len;
429
430 // Mifare HALT
431 uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
432 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
433
434 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
435 if (len != 0) {
436 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
437 return 1;
438 }
439
440 return 0;
441 }
442
443 int mifare_ultra_halt(uint32_t uid)
444 {
445 uint16_t len;
446
447 // Mifare HALT
448 uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
449 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
450
451 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
452 if (len != 0) {
453 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
454 return 1;
455 }
456
457 return 0;
458 }
459
460
461 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
462 // plus evtl. 8 sectors with 16 blocks each (4k cards)
463 uint8_t NumBlocksPerSector(uint8_t sectorNo)
464 {
465 if (sectorNo < 32)
466 return 4;
467 else
468 return 16;
469 }
470
471 uint8_t FirstBlockOfSector(uint8_t sectorNo)
472 {
473 if (sectorNo < 32)
474 return sectorNo * 4;
475 else
476 return 32*4 + (sectorNo - 32) * 16;
477
478 }
479
480
481 // work with emulator memory
482 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
483 uint8_t* emCARD = get_bigbufptr_emlcardmem();
484
485 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
486 }
487
488 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
489 uint8_t* emCARD = get_bigbufptr_emlcardmem();
490
491 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
492 }
493
494 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
495 uint8_t* emCARD = get_bigbufptr_emlcardmem();
496
497 memcpy(data, emCARD + bytePtr, byteCount);
498 }
499
500 int emlCheckValBl(int blockNum) {
501 uint8_t* emCARD = get_bigbufptr_emlcardmem();
502 uint8_t* data = emCARD + blockNum * 16;
503
504 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
505 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
506 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
507 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
508 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
509 (data[12] != (data[15] ^ 0xff))
510 )
511 return 1;
512 return 0;
513 }
514
515 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
516 uint8_t* emCARD = get_bigbufptr_emlcardmem();
517 uint8_t* data = emCARD + blockNum * 16;
518
519 if (emlCheckValBl(blockNum)) {
520 return 1;
521 }
522
523 memcpy(blReg, data, 4);
524 *blBlock = data[12];
525
526 return 0;
527 }
528
529 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
530 uint8_t* emCARD = get_bigbufptr_emlcardmem();
531 uint8_t* data = emCARD + blockNum * 16;
532
533 memcpy(data + 0, &blReg, 4);
534 memcpy(data + 8, &blReg, 4);
535 blReg = blReg ^ 0xffffffff;
536 memcpy(data + 4, &blReg, 4);
537
538 data[12] = blBlock;
539 data[13] = blBlock ^ 0xff;
540 data[14] = blBlock;
541 data[15] = blBlock ^ 0xff;
542
543 return 0;
544 }
545
546 uint64_t emlGetKey(int sectorNum, int keyType) {
547 uint8_t key[6];
548 uint8_t* emCARD = get_bigbufptr_emlcardmem();
549
550 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
551 return bytes_to_num(key, 6);
552 }
553
554 void emlClearMem(void) {
555 int b;
556
557 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
558 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
559 uint8_t* emCARD = get_bigbufptr_emlcardmem();
560
561 memset(emCARD, 0, CARD_MEMORY_SIZE);
562
563 // fill sectors trailer data
564 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
565 emlSetMem((uint8_t *)trailer, b , 1);
566 }
567
568 // uid
569 emlSetMem((uint8_t *)uid, 0, 1);
570 return;
571 }
Impressum, Datenschutz