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