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