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