]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
Added the changes to armsrc for milfare ultralight C and Desfire commands
[proxmark3-svn] / armsrc / mifareutil.c
CommitLineData
16a372ab 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#include <stdio.h>
17#include <stdlib.h>
18
19#include "iso14443crc.h"
20#include "iso14443a.h"
21#include "crapto1.h"
22#include "mifareutil.h"
23
24int MF_DBGLEVEL = MF_DBG_ALL;
25
26// memory management
27uint8_t* mifare_get_bigbufptr(void) {
28 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET); // was 3560 - tied to other size changes
29}
30uint8_t* eml_get_bigbufptr_sendbuf(void) {
31 return (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
32}
33uint8_t* eml_get_bigbufptr_recbuf(void) {
34 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET);
35}
36uint8_t* eml_get_bigbufptr_cardmem(void) {
37 return (((uint8_t *)BigBuf) + CARD_MEMORY);
38}
39
40// crypto1 helpers
41void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
42 uint8_t bt = 0;
43 int i;
44
45 if (len != 1) {
46 for (i = 0; i < len; i++)
47 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
48 } else {
49 bt = 0;
50 for (i = 0; i < 4; i++)
51 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;
52
53 data[0] = bt;
54 }
55 return;
56}
57
58void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, int len, uint32_t *par) {
59 uint8_t bt = 0;
60 int i;
61 uint32_t mltpl = 1 << (len - 1); // for len=18 it=0x20000
62 *par = 0;
63 for (i = 0; i < len; i++) {
64 bt = data[i];
65 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
66 *par = (*par >> 1) | ( ((filter(pcs->odd) ^ oddparity(bt)) & 0x01) * mltpl );
67 }
68 return;
69}
70
71uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
72 uint8_t bt = 0;
73 int i;
74
75 for (i = 0; i < 4; i++)
76 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
77
78 return bt;
79}
80
81// send commands
82int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t *timing)
83{
981bd429 84 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, NULL, timing);
85}
86
87int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *timing)
88{
89 uint8_t dcmd[8];//, ecmd[4];
90 //uint32_t par=0;
91
92 dcmd[0] = cmd;
16a372ab 93 memcpy(dcmd+1,data,5);
981bd429 94 AppendCrc14443a(dcmd, 6);
95 //Dbprintf("Data command: %02x", dcmd[0]);
96 //Dbprintf("Data R: %02x %02x %02x %02x %02x %02x %02x", dcmd[1],dcmd[2],dcmd[3],dcmd[4],dcmd[5],dcmd[6],dcmd[7]);
97
98 //memcpy(ecmd, dcmd, sizeof(dcmd));
99 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
100 int len = ReaderReceive(answer);
101 if(!len)
102 {
103 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
104 return 2;
105 }
16a372ab 106
107 return len;
108}
109
110int mifare_sendcmd_short_mfucauth(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint32_t *timing)
111{
112 uint8_t dcmd[19];//, ecmd[4];
113 int len;
114 //uint32_t par=0;
115
116 dcmd[0] = cmd;
117 memcpy(dcmd+1,data,16);
118 AppendCrc14443a(dcmd, 17);
119 //Dbprintf("Data command: %02x", dcmd[0]);
120 //Dbprintf("Data R: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", dcmd[0], dcmd[1],dcmd[2],dcmd[3],dcmd[4],dcmd[5],dcmd[6],dcmd[7],dcmd[8],dcmd[9],dcmd[10],dcmd[11],dcmd[12],dcmd[13],dcmd[14],dcmd[15],dcmd[16],dcmd[17],dcmd[18]);
121
122 //uint32_t time=0;
123 //uint8_t wupa[] = { 0x52 };
124 //ReaderTransmitBitsPar(wupa,7,0, NULL);
125 // Receive the ATQA
126 //if(!ReaderReceive(answer)) return 0;
127
128 ReaderTransmit(dcmd, sizeof(dcmd), timing);
129 len = ReaderReceive(answer);
130 if(!len)
131 {
132 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
133 len = ReaderReceive(answer);
134 }
135 if(len==1)
136 {
137 if (MF_DBGLEVEL >= 1) Dbprintf("NAK - Authentication failed.");
138 return 1;
139 }
981bd429 140 return len;
141}
142
143int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t * parptr, uint32_t *timing)
144{
145 uint8_t dcmd[4], ecmd[4];
16a372ab 146 uint32_t pos, par, res;
147
148 dcmd[0] = cmd;
149 dcmd[1] = data;
150 AppendCrc14443a(dcmd, 2);
151
152 memcpy(ecmd, dcmd, sizeof(dcmd));
153
154 if (crypted) {
155 par = 0;
156 for (pos = 0; pos < 4; pos++)
157 {
158 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
159 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) * 0x08 );
160 }
161
162 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
163
164 } else {
165 ReaderTransmit(dcmd, sizeof(dcmd), timing);
166 }
167
168 int len = ReaderReceivePar(answer, &par);
169
170 if (parptr) *parptr = par;
171
172 if (crypted == CRYPT_ALL) {
173 if (len == 1) {
174 res = 0;
175 for (pos = 0; pos < 4; pos++)
176 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
177
178 answer[0] = res;
179
180 } else {
181 for (pos = 0; pos < len; pos++)
182 {
183 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
184 }
185 }
186 }
187
188 return len;
189}
190
191// mifare commands
192int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested)
193{
194 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
195}
196
197int 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)
198{
199 // variables
200 int len;
201 uint32_t pos;
202 uint8_t tmp4[4];
203 byte_t par = 0;
204 byte_t ar[4];
205 uint32_t nt, ntpp; // Supplied tag nonce
206
207 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
208 uint8_t* receivedAnswer = mifare_get_bigbufptr();
209
210 // Transmit MIFARE_CLASSIC_AUTH
211 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, timing);
212 if (MF_DBGLEVEL >= 4) Dbprintf("rand nonce len: %x", len);
213 if (len != 4) return 1;
214
215 ar[0] = 0x55;
216 ar[1] = 0x41;
217 ar[2] = 0x49;
218 ar[3] = 0x92;
219
220 // Save the tag nonce (nt)
221 nt = bytes_to_num(receivedAnswer, 4);
222
223 // ----------------------------- crypto1 create
224 if (isNested)
225 crypto1_destroy(pcs);
226
227 // Init cipher with key
228 crypto1_create(pcs, ui64Key);
229
230 if (isNested == AUTH_NESTED) {
231 // decrypt nt with help of new key
232 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
233 } else {
234 // Load (plain) uid^nt into the cipher
235 crypto1_word(pcs, nt ^ uid, 0);
236 }
237
238 // some statistic
239 if (!ntptr && (MF_DBGLEVEL >= 3))
240 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
241
242 // save Nt
243 if (ntptr)
244 *ntptr = nt;
245
246 par = 0;
247 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
248 for (pos = 0; pos < 4; pos++)
249 {
250 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];
251 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );
252 }
253
254 // Skip 32 bits in pseudo random generator
255 nt = prng_successor(nt,32);
256
257 // ar+parity
258 for (pos = 4; pos < 8; pos++)
259 {
260 nt = prng_successor(nt,8);
261 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
262 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );
263 }
264
265 // Transmit reader nonce and reader answer
266 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
267
268 // Receive 4 bit answer
269 len = ReaderReceive(receivedAnswer);
270 if (!len)
271 {
272 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
273 return 2;
274 }
275
276 memcpy(tmp4, receivedAnswer, 4);
277 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
278
279 if (ntpp != bytes_to_num(tmp4, 4)) {
280 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
281 return 3;
282 }
283
284 return 0;
285}
286
287int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
288{
289 // variables
290 int len;
291 uint8_t bt[2];
292
293 uint8_t* receivedAnswer = mifare_get_bigbufptr();
294
295 // command MIFARE_CLASSIC_READBLOCK
296 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, 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, 16);
981bd429 314 return 0;
315}
316
16a372ab 317int mifare_ultra_auth1(uint32_t uid, uint8_t *blockData){
318 // variables
319 int len;
320
321 uint8_t* receivedAnswer = mifare_get_bigbufptr();
322
323 // command MIFARE_CLASSIC_READBLOCK
324 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,NULL);
325 if (len == 1) {
326 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
327 return 1;
328 }
329 if (len == 11) {
330 if (MF_DBGLEVEL >= 1) Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
331 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
332 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
333 receivedAnswer[10]);
334 memcpy(blockData, receivedAnswer, 11);
335 return 0;
336 }
337 //else something went wrong???
338 return 1;
339}
340
341int mifare_ultra_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
342 // variables
343 int len;
344
345 uint8_t* receivedAnswer = mifare_get_bigbufptr();
346
347
348 // command MIFARE_CLASSIC_READBLOCK
349 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer,NULL);
350 if (len == 1) {
351 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
352 return 1;
353 }
354 if (len == 11){
355 if (MF_DBGLEVEL >= 1) Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
356 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
357 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
358 receivedAnswer[10]);
359 memcpy(blockData, receivedAnswer, 11);
360 return 0;
361 }
362 //something went wrong?
363 return 1;
364}
365
981bd429 366int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
367{
368 // variables
369 int len;
370 uint8_t bt[2];
371
372 uint8_t* receivedAnswer = mifare_get_bigbufptr();
373
374 // command MIFARE_CLASSIC_READBLOCK
375 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer,NULL);
376 if (len == 1) {
377 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
378 return 1;
379 }
380 if (len != 18) {
381 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
382 return 2;
383 }
384
385 memcpy(bt, receivedAnswer + 16, 2);
386 AppendCrc14443a(receivedAnswer, 16);
387 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
388 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
389 return 3;
390 }
391
392 memcpy(blockData, receivedAnswer, 14);
393 return 0;
394}
395
396
397int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
398{
399 // variables
16a372ab 400 int len, i;
401 uint32_t pos;
402 uint32_t par = 0;
403 byte_t res;
404
405 uint8_t d_block[18], d_block_enc[18];
406 uint8_t* receivedAnswer = mifare_get_bigbufptr();
407
408 // command MIFARE_CLASSIC_WRITEBLOCK
409 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, NULL);
410
411 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
412 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
413 return 1;
414 }
415
416 memcpy(d_block, blockData, 16);
417 AppendCrc14443a(d_block, 16);
418
419 // crypto
420 par = 0;
421 for (pos = 0; pos < 18; pos++)
422 {
423 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
424 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );
425 }
426
427 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
428
429 // Receive the response
430 len = ReaderReceive(receivedAnswer);
431
432 res = 0;
433 for (i = 0; i < 4; i++)
434 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
435
436 if ((len != 1) || (res != 0x0A)) {
437 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
438 return 2;
439 }
440
981bd429 441 return 0;
442}
443
444int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
445{
446 // variables
447 int len;
448 uint32_t par = 0;
449
450 uint8_t d_block[18];
451 uint8_t* receivedAnswer = mifare_get_bigbufptr();
452
453 // command MIFARE_CLASSIC_WRITEBLOCK
454 len = mifare_sendcmd_short(NULL, 1, 0xA0, blockNo, receivedAnswer,NULL);
455
456 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
457 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
458 return 1;
459 }
460
461 memset(d_block,'\0',18);
462 memcpy(d_block, blockData, 16);
463 AppendCrc14443a(d_block, 16);
464
465 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
466
467 // Receive the response
468 len = ReaderReceive(receivedAnswer);
469
470 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
471 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
472 return 2;
473 }
474
475 return 0;
476}
477
478int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
479{
480 // variables
481 int len;
482 //uint32_t par = 0;
483
484 uint8_t d_block[8];
485 uint8_t* receivedAnswer = mifare_get_bigbufptr();
486
487 // command MIFARE_CLASSIC_WRITEBLOCK
488 memset(d_block,'\0',8);
489 d_block[0]= blockNo;
490 memcpy(d_block+1,blockData,4);
491 AppendCrc14443a(d_block, 6);
492
493 //i know the data send here is correct
494 len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer,NULL);
495
496 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
497 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
498 return 1;
499 }
500 return 0;
501}
502
503int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
504{
505 // variables
16a372ab 506 int len;
507
508 // Mifare HALT
509 uint8_t* receivedAnswer = mifare_get_bigbufptr();
510
511 len = mifare_sendcmd_short(pcs, pcs == NULL ? 0:1, 0x50, 0x00, receivedAnswer, NULL);
512 if (len != 0) {
513 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
514 return 1;
515 }
516
981bd429 517 return 0;
518}
519
520int mifare_ultra_halt(uint32_t uid)
521{
522 // variables
523 int len;
524
525 // Mifare HALT
526 uint8_t* receivedAnswer = mifare_get_bigbufptr();
527
528 len = mifare_sendcmd_short(NULL, 1, 0x50, 0x00, receivedAnswer, NULL);
529 if (len != 0) {
530 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
531 return 1;
532 }
533
534 return 0;
535}
536
537// work with emulator memory
538void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
539 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
16a372ab 540
541 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
542}
543
544void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
545 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
546
547 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
548}
549
550void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
551 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
552
553 memcpy(data, emCARD + bytePtr, byteCount);
554}
555
556int emlCheckValBl(int blockNum) {
557 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
558 uint8_t* data = emCARD + blockNum * 16;
559
560 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
561 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
562 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
563 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
564 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
565 (data[12] != (data[15] ^ 0xff))
566 )
567 return 1;
568 return 0;
569}
570
571int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
572 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
573 uint8_t* data = emCARD + blockNum * 16;
574
575 if (emlCheckValBl(blockNum)) {
576 return 1;
577 }
578
579 memcpy(blReg, data, 4);
580 *blBlock = data[12];
581
582 return 0;
583}
584
585int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
586 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
587 uint8_t* data = emCARD + blockNum * 16;
588
589 memcpy(data + 0, &blReg, 4);
590 memcpy(data + 8, &blReg, 4);
591 blReg = blReg ^ 0xffffffff;
592 memcpy(data + 4, &blReg, 4);
593
594 data[12] = blBlock;
595 data[13] = blBlock ^ 0xff;
596 data[14] = blBlock;
597 data[15] = blBlock ^ 0xff;
598
599 return 0;
600}
601
602uint64_t emlGetKey(int sectorNum, int keyType) {
603 uint8_t key[6];
604 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
605
606 memcpy(key, emCARD + 3 * 16 + sectorNum * 4 * 16 + keyType * 10, 6);
607 return bytes_to_num(key, 6);
608}
609
610void emlClearMem(void) {
611 int b;
612
613 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
614 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
615 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
616
617 memset(emCARD, 0, CARD_MEMORY_LEN);
618
619 // fill sectors trailer data
620 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
621 emlSetMem((uint8_t *)trailer, b , 1);
622 }
623
624 // uid
625 emlSetMem((uint8_t *)uid, 0, 1);
626 return;
627}
628
629//
630//DESFIRE
631//
632
633int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *timing)
634{
635 uint8_t dcmd[5];//, ecmd[4];
636 //dcmd= (uint8_t *) malloc(data_len+3);
637 //uint32_t par=0;
638
639 dcmd[0] = cmd;
640 memcpy(dcmd+1,data,2);
641 AppendCrc14443a(dcmd, 3);
642 //Dbprintf("Data command: %02x", dcmd[0]);
643 //Dbprintf("Data R: %02x %02x", dcmd[1],dcmd[2]);
644
645 //memcpy(ecmd, dcmd, sizeof(dcmd));
646 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
647 int len = ReaderReceive(answer);
648 if(!len)
649 {
650 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
651 return 2;
652 }
653 //free(dcmd);
654 return len;
655}
656
657
658int mifare_sendcmd_special2(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *timing)
659{
660 uint8_t dcmd[20];//, ecmd[4];
661 //dcmd= (uint8_t *) malloc(data_len+3);
662 //uint32_t par=0;
663
664 dcmd[0] = cmd;
665 memcpy(dcmd+1,data,17);
666 AppendCrc14443a(dcmd, 18);
667 //Dbprintf("Data command: %02x", dcmd[0]);
668 //Dbprintf("Data R: %02x %02x %02x %02x", dcmd[1],dcmd[2],dcmd[3],dcmd[4]);
669
670 //memcpy(ecmd, dcmd, sizeof(dcmd));
671 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
672 int len = ReaderReceive(answer);
673 if(!len)
674 {
675 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
676 return 2;
677 }
678 //free(dcmd);
679 return len;
680}
681
682int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
683 // variables
684 int len;
685 // load key, keynumber
686 uint8_t data[2]={0x0a, 0x00};
687 //uint8_t data_len=2;
688 uint8_t* receivedAnswer = mifare_get_bigbufptr();
689
690 // command MIFARE_CLASSIC_READBLOCK
691 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,NULL);
692 if (len == 1) {
693 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
694 return 1;
695 }
696 if (len == 12) {
697 if (MF_DBGLEVEL >= 1) Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
698 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
699 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
700 receivedAnswer[10],receivedAnswer[11]);
701 memcpy(blockData, receivedAnswer, 12);
702 return 0;
703 }
704 //else something went wrong???
705 return 1;
706}
707
708int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
709 // variables
710 int len;
711 uint8_t data[17]={0xaf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
712 memcpy(data+1,key,16);
713 uint8_t* receivedAnswer = mifare_get_bigbufptr();
714
715
716 // command MIFARE_CLASSIC_READBLOCK
717 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer,NULL);
718 if ((receivedAnswer[0] == 0x03)&&(receivedAnswer[1] == 0xae)) {
719 if (MF_DBGLEVEL >= 1) Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
720 return 1;
721 }
722 if (len == 12){
723 if (MF_DBGLEVEL >= 1) Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
724 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
725 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
726 receivedAnswer[10],receivedAnswer[11]);
727 memcpy(blockData, receivedAnswer, 12);
728 return 0;
729 }
730 //something went wrong?
731 return 1;
732}
Impressum, Datenschutz