]> git.zerfleddert.de Git - proxmark3-svn/blob - client/mifare/mifarehost.c
Cleanup armsrc/string.c and string.h (#964)
[proxmark3-svn] / client / mifare / mifarehost.c
1 // Merlok, 2011, 2012
2 // people from mifare@nethemba.com, 2010
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // mifare commands
9 //-----------------------------------------------------------------------------
10
11 #include "mifarehost.h"
12
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <pthread.h>
18
19 #include "crapto1/crapto1.h"
20 #include "comms.h"
21 #include "usb_cmd.h"
22 #include "cmdmain.h"
23 #include "ui.h"
24 #include "parity.h"
25 #include "util.h"
26 #include "iso14443crc.h"
27 #include "util_posix.h"
28
29 #include "mifare.h"
30 #include "mifare4.h"
31
32 // mifare tracer flags used in mfTraceDecode()
33 #define TRACE_IDLE 0x00
34 #define TRACE_AUTH1 0x01
35 #define TRACE_AUTH2 0x02
36 #define TRACE_AUTH_OK 0x03
37 #define TRACE_READ_DATA 0x04
38 #define TRACE_WRITE_OK 0x05
39 #define TRACE_WRITE_DATA 0x06
40 #define TRACE_ERROR 0xFF
41
42
43 static int compare_uint64(const void *a, const void *b) {
44 // didn't work: (the result is truncated to 32 bits)
45 //return (*(int64_t*)b - *(int64_t*)a);
46
47 // better:
48 if (*(uint64_t*)b == *(uint64_t*)a) return 0;
49 else if (*(uint64_t*)b < *(uint64_t*)a) return 1;
50 else return -1;
51 }
52
53
54 // create the intersection (common members) of two sorted lists. Lists are terminated by -1. Result will be in list1. Number of elements is returned.
55 static uint32_t intersection(uint64_t *list1, uint64_t *list2)
56 {
57 if (list1 == NULL || list2 == NULL) {
58 return 0;
59 }
60 uint64_t *p1, *p2, *p3;
61 p1 = p3 = list1;
62 p2 = list2;
63
64 while ( *p1 != -1 && *p2 != -1 ) {
65 if (compare_uint64(p1, p2) == 0) {
66 *p3++ = *p1++;
67 p2++;
68 }
69 else {
70 while (compare_uint64(p1, p2) < 0) ++p1;
71 while (compare_uint64(p1, p2) > 0) ++p2;
72 }
73 }
74 *p3 = -1;
75 return p3 - list1;
76 }
77
78
79 // Darkside attack (hf mf mifare)
80 static uint32_t nonce2key(uint32_t uid, uint32_t nt, uint32_t nr, uint32_t ar, uint64_t par_info, uint64_t ks_info, uint64_t **keys) {
81 struct Crypto1State *states;
82 uint32_t i, pos;
83 uint8_t bt, ks3x[8], par[8][8];
84 uint64_t key_recovered;
85 uint64_t *keylist;
86
87 // Reset the last three significant bits of the reader nonce
88 nr &= 0xffffff1f;
89
90 for (pos=0; pos<8; pos++) {
91 ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
92 bt = (par_info >> (pos*8)) & 0xff;
93 for (i=0; i<8; i++) {
94 par[7-pos][i] = (bt >> i) & 0x01;
95 }
96 }
97
98 states = lfsr_common_prefix(nr, ar, ks3x, par, (par_info == 0));
99
100 if (states == NULL) {
101 *keys = NULL;
102 return 0;
103 }
104
105 keylist = (uint64_t*)states;
106
107 for (i = 0; keylist[i]; i++) {
108 lfsr_rollback_word(states+i, uid^nt, 0);
109 crypto1_get_lfsr(states+i, &key_recovered);
110 keylist[i] = key_recovered;
111 }
112 keylist[i] = -1;
113
114 *keys = keylist;
115 return i;
116 }
117
118
119 int mfDarkside(uint64_t *key) {
120 uint32_t uid = 0;
121 uint32_t nt = 0, nr = 0, ar = 0;
122 uint64_t par_list = 0, ks_list = 0;
123 uint64_t *keylist = NULL, *last_keylist = NULL;
124 uint32_t keycount = 0;
125 int16_t isOK = 0;
126
127 UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};
128
129 // message
130 printf("-------------------------------------------------------------------------\n");
131 printf("Executing command. Expected execution time: 25sec on average\n");
132 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
133 printf("-------------------------------------------------------------------------\n");
134
135
136 while (true) {
137 clearCommandBuffer();
138 SendCommand(&c);
139
140 //flush queue
141 while (ukbhit()) {
142 int c = getchar(); (void) c;
143 }
144
145 // wait cycle
146 while (true) {
147 printf(".");
148 fflush(stdout);
149 if (ukbhit()) {
150 return -5;
151 break;
152 }
153
154 UsbCommand resp;
155 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
156 isOK = resp.arg[0];
157 if (isOK < 0) {
158 return isOK;
159 }
160 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);
161 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);
162 par_list = bytes_to_num(resp.d.asBytes + 8, 8);
163 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);
164 nr = (uint32_t)bytes_to_num(resp.d.asBytes + 24, 4);
165 ar = (uint32_t)bytes_to_num(resp.d.asBytes + 28, 4);
166 break;
167 }
168 }
169
170 if (par_list == 0 && c.arg[0] == true) {
171 PrintAndLog("Parity is all zero. Most likely this card sends NACK on every failed authentication.");
172 }
173 c.arg[0] = false;
174
175 keycount = nonce2key(uid, nt, nr, ar, par_list, ks_list, &keylist);
176
177 if (keycount == 0) {
178 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);
179 PrintAndLog("This is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
180 continue;
181 }
182
183 if (par_list == 0) {
184 qsort(keylist, keycount, sizeof(*keylist), compare_uint64);
185 keycount = intersection(last_keylist, keylist);
186 if (keycount == 0) {
187 free(last_keylist);
188 last_keylist = keylist;
189 continue;
190 }
191 }
192
193 if (keycount > 1) {
194 PrintAndLog("Found %u possible keys. Trying to authenticate with each of them ...\n", keycount);
195 } else {
196 PrintAndLog("Found a possible key. Trying to authenticate...\n");
197 }
198
199 uint8_t *keys_to_chk = malloc(keycount * 6);
200 for (int i = 0; i < keycount; i++) {
201 num_to_bytes(keylist[i], 6, keys_to_chk+i);
202 }
203
204 *key = -1;
205 mfCheckKeys(0, 0, 0, false, keycount, keys_to_chk, key);
206
207 free(keys_to_chk);
208
209 if (*key != -1) {
210 free(last_keylist);
211 free(keylist);
212 break;
213 } else {
214 PrintAndLog("Authentication failed. Trying again...");
215 free(last_keylist);
216 last_keylist = keylist;
217 }
218 }
219
220 return 0;
221 }
222
223
224 static int mfCheckKeysEx(uint8_t blockNo, uint8_t keyType, uint16_t timeout14a, bool clear_trace, uint32_t keycnt, uint8_t *keys, uint64_t *found_key, bool fixed_nonce) {
225
226 bool display_progress = false;
227 uint64_t start_time = msclock();
228 uint64_t next_print_time = start_time + 5 * 1000;
229
230 if (keycnt > 1000) {
231 PrintAndLog("We have %d keys to check. This can take some time!", keycnt);
232 PrintAndLog("Press button to abort.");
233 display_progress = true;
234 }
235
236 uint8_t bytes_per_key = fixed_nonce ? 5 : 6;
237 uint32_t max_keys = keycnt > USB_CMD_DATA_SIZE/bytes_per_key ? USB_CMD_DATA_SIZE/bytes_per_key : keycnt;
238 *found_key = -1;
239 bool multisectorCheck = false;
240
241 for (int i = 0, ii = 0; i < keycnt; i += max_keys) {
242
243 if ((i + max_keys) >= keycnt) {
244 max_keys = keycnt - i;
245 }
246
247 bool init = (i == 0);
248 bool drop_field = (max_keys == keycnt);
249 uint8_t flags = clear_trace | multisectorCheck << 1 | init << 2 | drop_field << 3 | fixed_nonce << 4;
250
251 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), flags | timeout14a << 16, max_keys}};
252 memcpy(c.d.asBytes, keys + i * bytes_per_key, max_keys * bytes_per_key);
253 SendCommand(&c);
254
255 UsbCommand resp;
256 if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000))
257 return 1;
258
259 if ((resp.arg[0] & 0xff) != 0x01) {
260 if ((int)resp.arg[1] < 0) { // error or user aborted
261 return (int)resp.arg[1];
262 } else { // nothing found yet
263 if (display_progress && msclock() >= next_print_time) {
264 float brute_force_per_second = (float)(i - ii) / (float)(msclock() - start_time) * 1000.0;
265 ii = i;
266 start_time = msclock();
267 next_print_time = start_time + 10 * 1000;
268 PrintAndLog(" %8d keys left | %5.1f keys/sec | worst case %6.1f seconds remaining", keycnt - i, brute_force_per_second, (keycnt-i)/brute_force_per_second);
269 }
270 }
271 } else { // success
272 if (fixed_nonce) {
273 *found_key = i + resp.arg[1] - 1;
274 } else {
275 *found_key = bytes_to_num(resp.d.asBytes, 6);
276 }
277 return 0;
278 }
279 }
280
281 return 2; // nothing found
282 }
283
284
285 int mfCheckKeys(uint8_t blockNo, uint8_t keyType, uint16_t timeout14a, bool clear_trace, uint32_t keycnt, uint8_t *keys, uint64_t *found_key) {
286 return mfCheckKeysEx(blockNo, keyType, timeout14a, clear_trace, keycnt, keys, found_key, false);
287 }
288
289
290 static int mfCheckKeysFixedNonce(uint8_t blockNo, uint8_t keyType, uint16_t timeout14a, bool clear_trace, uint32_t keycnt, uint8_t *keys, uint32_t *key_index) {
291 return mfCheckKeysEx(blockNo, keyType, timeout14a, clear_trace, keycnt, keys, (uint64_t*)key_index, true);
292 }
293
294
295 int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint16_t timeout14a, bool clear_trace, bool init, bool drop_field, uint8_t keycnt, uint8_t *keyBlock, sector_t *e_sector) {
296
297 uint8_t keyPtr = 0;
298
299 if (e_sector == NULL)
300 return -1;
301
302 bool multisectorCheck = true;
303 uint8_t flags = clear_trace | multisectorCheck << 1 | init << 2 | drop_field << 3;
304
305 UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), flags | timeout14a << 16, keycnt}};
306 memcpy(c.d.asBytes, keyBlock, 6 * keycnt);
307 SendCommand(&c);
308
309 UsbCommand resp;
310 if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth
311 if ((resp.arg[0] & 0xff) != 0x01) return 2;
312
313 bool foundAKey = false;
314 for(int sec = 0; sec < sectorCnt; sec++){
315 for(int keyAB = 0; keyAB < 2; keyAB++){
316 keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);
317 if (keyPtr){
318 e_sector[sec].foundKey[keyAB] = true;
319 e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);
320 foundAKey = true;
321 }
322 }
323 }
324 return foundAKey ? 0 : 3;
325 }
326
327 // Compare 16 Bits out of cryptostate
328 int Compare16Bits(const void * a, const void * b) {
329 if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;
330 else if ((*(uint64_t*)b & 0x00ff000000ff0000) > (*(uint64_t*)a & 0x00ff000000ff0000)) return 1;
331 else return -1;
332 }
333
334 typedef
335 struct {
336 union {
337 struct Crypto1State *slhead;
338 uint64_t *keyhead;
339 } head;
340 union {
341 struct Crypto1State *sltail;
342 uint64_t *keytail;
343 } tail;
344 uint32_t len;
345 uint32_t uid;
346 uint32_t blockNo;
347 uint32_t keyType;
348 uint32_t nt;
349 uint32_t ks1;
350 } StateList_t;
351
352
353 // wrapper function for multi-threaded lfsr_recovery32
354 void
355 #ifdef __has_attribute
356 #if __has_attribute(force_align_arg_pointer)
357 __attribute__((force_align_arg_pointer))
358 #endif
359 #endif
360 *nested_worker_thread(void *arg) {
361 struct Crypto1State *p1;
362 StateList_t *statelist = arg;
363
364 statelist->head.slhead = lfsr_recovery32(statelist->ks1, statelist->nt ^ statelist->uid);
365 for (p1 = statelist->head.slhead; *(uint64_t *)p1 != 0; p1++);
366 statelist->len = p1 - statelist->head.slhead;
367 statelist->tail.sltail = --p1;
368 qsort(statelist->head.slhead, statelist->len, sizeof(uint64_t), Compare16Bits);
369
370 return statelist->head.slhead;
371 }
372
373
374 static int nested_fixed_nonce(StateList_t statelist, uint32_t fixed_nt, uint32_t authentication_timeout, uint8_t *resultKey) {
375 // We have a tag with a fixed nonce (nt) and therefore only one (usually long) list of possible crypto states.
376 // Instead of testing all those keys on the device with a complete authentication cycle, we do all of the crypto operations here.
377 uint8_t nr_enc[4] = NESTED_FIXED_NR_ENC; // we use a fixed {nr}
378 uint8_t ar[4];
379 num_to_bytes(prng_successor(fixed_nt, 64), 4, ar); // ... and ar is fixed too
380
381 // create an array of possible {ar} and parity bits
382 uint32_t num_ar_par = statelist.len;
383 uint8_t *ar_par = calloc(num_ar_par, 5);
384 if (ar_par == NULL) {
385 free(statelist.head.slhead);
386 return -4;
387 }
388
389 for (int i = 0; i < num_ar_par; i++) {
390 // roll back to initial state using the nt observed with the nested authentication
391 lfsr_rollback_word(statelist.head.slhead + i, statelist.nt ^ statelist.uid, 0);
392 // instead feed in the fixed_nt for the first authentication
393 struct Crypto1State cs = *(statelist.head.slhead + i);
394 crypto1_word(&cs, fixed_nt ^ statelist.uid, 0);
395 // determine nr such that the resulting {nr} is constant and feed it into the cypher. Calculate the encrypted parity bits
396 uint8_t par_enc = 0;
397 for (int j = 0; j < 4; j++) {
398 uint8_t nr_byte = crypto1_byte(&cs, nr_enc[j], 1) ^ nr_enc[j];
399 par_enc |= (((filter(cs.odd) ^ oddparity8(nr_byte)) & 0x01) << (7-j));
400 }
401 // calculate the encrypted reader response {ar} and its parity bits
402 for (int j = 0; j < 4; j++) {
403 ar_par[5*i + j] = crypto1_byte(&cs, 0, 0) ^ ar[j];
404 par_enc |= ((filter(cs.odd) ^ oddparity8(ar[j])) & 0x01) << (3-j);
405 }
406 ar_par[5*i + 4] = par_enc;
407 }
408
409 // test each {ar} response
410 uint32_t key_index;
411
412 int isOK = mfCheckKeysFixedNonce(statelist.blockNo, statelist.keyType, authentication_timeout, true, num_ar_par, ar_par, &key_index);
413
414 if (isOK == 0) { // success, key found
415 // key_index contains the index into the cypher state list
416 struct Crypto1State *p1 = statelist.head.slhead + key_index;
417 uint64_t key64;
418 crypto1_get_lfsr(p1, &key64);
419 num_to_bytes(key64, 6, resultKey);
420 }
421 if (isOK == 1) { // timeout
422 isOK = -1;
423 }
424 free(statelist.head.slhead);
425 free(ar_par);
426 return isOK;
427 }
428
429
430 static int nested_standard(StateList_t statelists[2], uint32_t authentication_timeout, uint8_t *resultKey) {
431
432 // the first 16 Bits of the crypto states already contain part of our key.
433 // Create the intersection of the two lists based on these 16 Bits and
434 // roll back the crypto state for the remaining states
435 struct Crypto1State *p1, *p2, *p3, *p4;
436 p1 = p3 = statelists[0].head.slhead;
437 p2 = p4 = statelists[1].head.slhead;
438 while (p1 <= statelists[0].tail.sltail && p2 <= statelists[1].tail.sltail) {
439 if (Compare16Bits(p1, p2) == 0) {
440 struct Crypto1State savestate, *savep = &savestate;
441 savestate = *p1;
442 while (Compare16Bits(p1, savep) == 0 && p1 <= statelists[0].tail.sltail) {
443 *p3 = *p1;
444 lfsr_rollback_word(p3, statelists[0].nt ^ statelists[0].uid, 0);
445 p3++;
446 p1++;
447 }
448 savestate = *p2;
449 while (Compare16Bits(p2, savep) == 0 && p2 <= statelists[1].tail.sltail) {
450 *p4 = *p2;
451 lfsr_rollback_word(p4, statelists[1].nt ^ statelists[1].uid, 0);
452 p4++;
453 p2++;
454 }
455 }
456 else {
457 while (Compare16Bits(p1, p2) == -1) p1++;
458 while (Compare16Bits(p1, p2) == 1) p2++;
459 }
460 }
461 *(uint64_t*)p3 = -1;
462 *(uint64_t*)p4 = -1;
463 statelists[0].len = p3 - statelists[0].head.slhead;
464 statelists[1].len = p4 - statelists[1].head.slhead;
465 statelists[0].tail.sltail=--p3;
466 statelists[1].tail.sltail=--p4;
467
468 // the statelists now contain possible crypto states initialized with the key. The key we are searching for
469 // must be in the intersection of both lists. Sort the lists and create the intersection:
470 qsort(statelists[0].head.keyhead, statelists[0].len, sizeof(uint64_t), compare_uint64);
471 qsort(statelists[1].head.keyhead, statelists[1].len, sizeof(uint64_t), compare_uint64);
472 statelists[0].len = intersection(statelists[0].head.keyhead, statelists[1].head.keyhead);
473
474 // create an array of the possible keys
475 uint32_t num_keys = statelists[0].len;
476 uint8_t *keys = calloc(num_keys, 6);
477 if (keys == NULL) {
478 free(statelists[0].head.slhead);
479 free(statelists[1].head.slhead);
480 return -4;
481 }
482
483 uint64_t key64 = 0;
484 for (int i = 0; i < num_keys; i++) {
485 crypto1_get_lfsr(statelists[0].head.slhead + i, &key64);
486 num_to_bytes(key64, 6, keys + i*6);
487 }
488
489 // and test each key with mfCheckKeys
490 int isOK = mfCheckKeys(statelists[0].blockNo, statelists[0].keyType, authentication_timeout, true, num_keys, keys, &key64);
491
492 if (isOK == 0) { // success, key found
493 num_to_bytes(key64, 6, resultKey);
494 }
495 if (isOK == 1) { // timeout
496 isOK = -1;
497 }
498 free(statelists[0].head.slhead);
499 free(statelists[1].head.slhead);
500 free(keys);
501 return isOK;
502 }
503
504
505 int mfnested(uint8_t blockNo, uint8_t keyType, uint16_t timeout14a, uint8_t *key, uint8_t trgBlockNo, uint8_t trgKeyType, uint8_t *resultKey, bool calibrate) {
506
507 // flush queue
508 clearCommandBuffer();
509
510 UsbCommand c = {CMD_MIFARE_NESTED, {blockNo + keyType * 0x100, trgBlockNo + trgKeyType * 0x100, calibrate}};
511 memcpy(c.d.asBytes, key, 6);
512 SendCommand(&c);
513
514 UsbCommand resp;
515 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
516 return -1;
517 }
518
519 if ((int)resp.arg[0]) {
520 return (int)resp.arg[0]; // error during nested
521 }
522
523 uint32_t uid;
524 memcpy(&uid, resp.d.asBytes, 4);
525 PrintAndLog("uid:%08x trgbl=%d trgkey=%x", uid, (uint16_t)resp.arg[2] & 0xff, (uint16_t)resp.arg[2] >> 8);
526
527 StateList_t statelists[2];
528 for (int i = 0; i < 2; i++) {
529 statelists[i].blockNo = resp.arg[2] & 0xff;
530 statelists[i].keyType = (resp.arg[2] >> 8) & 0xff;
531 statelists[i].uid = uid;
532 memcpy(&statelists[i].nt, (void *)(resp.d.asBytes + 4 + i * 8 + 0), 4);
533 memcpy(&statelists[i].ks1, (void *)(resp.d.asBytes + 4 + i * 8 + 4), 4);
534 }
535
536 uint32_t authentication_timeout;
537 memcpy(&authentication_timeout, resp.d.asBytes + 20, 4);
538 PrintAndLog("Setting authentication timeout to %" PRIu32 "us", authentication_timeout * 1000 / 106);
539
540 uint8_t num_unique_nonces;
541 uint32_t fixed_nt = 0;
542 if (statelists[0].nt == statelists[1].nt && statelists[0].ks1 == statelists[1].ks1) {
543 num_unique_nonces = 1;
544 memcpy(&fixed_nt, resp.d.asBytes + 24, 4);
545 PrintAndLog("Fixed nt detected: %08" PRIx32 " on first authentication, %08" PRIx32 " on nested authentication", fixed_nt, statelists[0].nt);
546 } else {
547 num_unique_nonces = 2;
548 }
549
550 // create and run worker threads to calculate possible crypto states
551 pthread_t thread_id[2];
552 for (int i = 0; i < num_unique_nonces; i++) {
553 pthread_create(thread_id + i, NULL, nested_worker_thread, &statelists[i]);
554 }
555 // wait for threads to terminate:
556 for (int i = 0; i < num_unique_nonces; i++) {
557 pthread_join(thread_id[i], (void*)&statelists[i].head.slhead);
558 }
559
560 if (num_unique_nonces == 2) {
561 return nested_standard(statelists, authentication_timeout, resultKey);
562 } else {
563 return nested_fixed_nonce(statelists[0], fixed_nt, authentication_timeout, resultKey);
564 }
565 }
566
567
568 // MIFARE
569 int mfReadSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, uint8_t *data) {
570
571 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
572 memcpy(c.d.asBytes, key, 6);
573 clearCommandBuffer();
574 SendCommand(&c);
575
576 UsbCommand resp;
577 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
578 uint8_t isOK = resp.arg[0] & 0xff;
579
580 if (isOK) {
581 memcpy(data, resp.d.asBytes, mfNumBlocksPerSector(sectorNo) * 16);
582 return 0;
583 } else {
584 return 1;
585 }
586 } else {
587 PrintAndLogEx(ERR, "Command execute timeout");
588 return 2;
589 }
590
591 return 0;
592 }
593
594 // EMULATOR
595
596 int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {
597 UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};
598 SendCommand(&c);
599
600 UsbCommand resp;
601 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;
602 memcpy(data, resp.d.asBytes, blocksCount * 16);
603 return 0;
604 }
605
606 int mfEmlSetMem(uint8_t *data, int blockNum, int blocksCount) {
607 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNum, blocksCount, 0}};
608 memcpy(c.d.asBytes, data, blocksCount * 16);
609 SendCommand(&c);
610 return 0;
611 }
612
613 // "MAGIC" CARD
614
615 int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {
616 uint8_t isOK = 0;
617
618 UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};
619 SendCommand(&c);
620
621 UsbCommand resp;
622 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
623 isOK = resp.arg[0] & 0xff;
624 memcpy(data, resp.d.asBytes, 16);
625 if (!isOK) return 2;
626 } else {
627 PrintAndLog("Command execute timeout");
628 return 1;
629 }
630 return 0;
631 }
632
633 int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uint8_t params) {
634
635 uint8_t isOK = 0;
636 UsbCommand c = {CMD_MIFARE_CSETBLOCK, {wantWipe, params & (0xFE | (uid == NULL ? 0:1)), blockNo}};
637 memcpy(c.d.asBytes, data, 16);
638 SendCommand(&c);
639
640 UsbCommand resp;
641 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
642 isOK = resp.arg[0] & 0xff;
643 if (uid != NULL)
644 memcpy(uid, resp.d.asBytes, 4);
645 if (!isOK)
646 return 2;
647 } else {
648 PrintAndLog("Command execute timeout");
649 return 1;
650 }
651
652 return 0;
653 }
654
655 int mfCWipe(uint32_t numSectors, bool gen1b, bool wantWipe, bool wantFill) {
656 uint8_t isOK = 0;
657 uint8_t cmdParams = wantWipe + wantFill * 0x02 + gen1b * 0x04;
658 UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, cmdParams, 0}};
659 SendCommand(&c);
660
661 UsbCommand resp;
662 WaitForResponse(CMD_ACK,&resp);
663 isOK = resp.arg[0] & 0xff;
664
665 return isOK;
666 }
667
668 int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {
669 uint8_t oldblock0[16] = {0x00};
670 uint8_t block0[16] = {0x00};
671 int gen = 0, res;
672
673 gen = mfCIdentify();
674
675 /* generation 1a magic card by default */
676 uint8_t cmdParams = CSETBLOCK_SINGLE_OPER;
677 if (gen == 2) {
678 /* generation 1b magic card */
679 cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B;
680 }
681
682 res = mfCGetBlock(0, oldblock0, cmdParams);
683
684 if (res == 0) {
685 memcpy(block0, oldblock0, 16);
686 PrintAndLog("old block 0: %s", sprint_hex(block0,16));
687 } else {
688 PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");
689 }
690
691 // fill in the new values
692 // UID
693 memcpy(block0, uid, 4);
694 // Mifare UID BCC
695 block0[4] = block0[0] ^ block0[1] ^ block0[2] ^ block0[3];
696 // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)
697 if (sak != NULL)
698 block0[5] = sak[0];
699 if (atqa != NULL) {
700 block0[6] = atqa[1];
701 block0[7] = atqa[0];
702 }
703 PrintAndLog("new block 0: %s", sprint_hex(block0, 16));
704
705 res = mfCSetBlock(0, block0, oldUID, false, cmdParams);
706 if (res) {
707 PrintAndLog("Can't set block 0. Error: %d", res);
708 return res;
709 }
710
711 return 0;
712 }
713
714 int mfCIdentify() {
715 UsbCommand c = {CMD_MIFARE_CIDENT, {0, 0, 0}};
716 SendCommand(&c);
717 UsbCommand resp;
718 WaitForResponse(CMD_ACK,&resp);
719
720 uint8_t isGeneration = resp.arg[0] & 0xff;
721 switch( isGeneration ){
722 case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;
723 case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break;
724 default: PrintAndLog("No chinese magic backdoor command detected"); break;
725 }
726
727 return (int) isGeneration;
728 }
729
730
731 // SNIFFER
732
733 // constants
734 static uint8_t trailerAccessBytes[4] = {0x08, 0x77, 0x8F, 0x00};
735
736 // variables
737 char logHexFileName[FILE_PATH_SIZE] = {0x00};
738 static uint8_t traceCard[4096] = {0x00};
739 static char traceFileName[FILE_PATH_SIZE] = {0x00};
740 static int traceState = TRACE_IDLE;
741 static uint8_t traceCurBlock = 0;
742 static uint8_t traceCurKey = 0;
743
744 struct Crypto1State *traceCrypto1 = NULL;
745
746 struct Crypto1State *revstate;
747 uint64_t lfsr;
748 uint64_t ui64Key;
749 uint32_t ks2;
750 uint32_t ks3;
751
752 uint32_t uid; // serial number
753 uint32_t nt; // tag challenge
754 uint32_t nt_enc; // encrypted tag challenge
755 uint8_t nt_enc_par; // encrypted tag challenge parity
756 uint32_t nr_enc; // encrypted reader challenge
757 uint32_t ar_enc; // encrypted reader response
758 uint8_t ar_enc_par; // encrypted reader response parity
759 uint32_t at_enc; // encrypted tag response
760 uint8_t at_enc_par; // encrypted tag response parity
761
762 int isTraceCardEmpty(void) {
763 return ((traceCard[0] == 0) && (traceCard[1] == 0) && (traceCard[2] == 0) && (traceCard[3] == 0));
764 }
765
766 int isBlockEmpty(int blockN) {
767 for (int i = 0; i < 16; i++)
768 if (traceCard[blockN * 16 + i] != 0) return 0;
769
770 return 1;
771 }
772
773 int isBlockTrailer(int blockN) {
774 return ((blockN & 0x03) == 0x03);
775 }
776
777 int saveTraceCard(void) {
778 FILE * f;
779
780 if ((!strlen(traceFileName)) || (isTraceCardEmpty())) return 0;
781
782 f = fopen(traceFileName, "w+");
783 if ( !f ) return 1;
784
785 for (int i = 0; i < 64; i++) { // blocks
786 for (int j = 0; j < 16; j++) // bytes
787 fprintf(f, "%02x", *(traceCard + i * 16 + j));
788 if (i < 63)
789 fprintf(f,"\n");
790 }
791 fclose(f);
792 return 0;
793 }
794
795 int loadTraceCard(uint8_t *tuid) {
796 FILE * f;
797 char buf[64] = {0x00};
798 uint8_t buf8[64] = {0x00};
799 int i, blockNum;
800
801 if (!isTraceCardEmpty())
802 saveTraceCard();
803
804 memset(traceCard, 0x00, 4096);
805 memcpy(traceCard, tuid + 3, 4);
806
807 FillFileNameByUID(traceFileName, tuid, ".eml", 7);
808
809 f = fopen(traceFileName, "r");
810 if (!f) return 1;
811
812 blockNum = 0;
813
814 while(!feof(f)){
815
816 memset(buf, 0, sizeof(buf));
817 if (fgets(buf, sizeof(buf), f) == NULL) {
818 PrintAndLog("File reading error.");
819 fclose(f);
820 return 2;
821 }
822
823 if (strlen(buf) < 32){
824 if (feof(f)) break;
825 PrintAndLog("File content error. Block data must include 32 HEX symbols");
826 fclose(f);
827 return 2;
828 }
829 for (i = 0; i < 32; i += 2)
830 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
831
832 memcpy(traceCard + blockNum * 16, buf8, 16);
833
834 blockNum++;
835 }
836 fclose(f);
837
838 return 0;
839 }
840
841 int mfTraceInit(uint8_t *tuid, uint8_t *atqa, uint8_t sak, bool wantSaveToEmlFile) {
842
843 if (traceCrypto1)
844 crypto1_destroy(traceCrypto1);
845
846 traceCrypto1 = NULL;
847
848 if (wantSaveToEmlFile)
849 loadTraceCard(tuid);
850
851 traceCard[4] = traceCard[0] ^ traceCard[1] ^ traceCard[2] ^ traceCard[3];
852 traceCard[5] = sak;
853 memcpy(&traceCard[6], atqa, 2);
854 traceCurBlock = 0;
855 uid = bytes_to_num(tuid + 3, 4);
856
857 traceState = TRACE_IDLE;
858
859 return 0;
860 }
861
862 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len, bool isEncrypted){
863 uint8_t bt = 0;
864 int i;
865
866 if (len != 1) {
867 for (i = 0; i < len; i++)
868 data[i] = crypto1_byte(pcs, 0x00, isEncrypted) ^ data[i];
869 } else {
870 bt = 0;
871 for (i = 0; i < 4; i++)
872 bt |= (crypto1_bit(pcs, 0, isEncrypted) ^ BIT(data[0], i)) << i;
873
874 data[0] = bt;
875 }
876 return;
877 }
878
879 bool NTParityCheck(uint32_t ntx) {
880 if (
881 (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((nt_enc_par >> 5) & 0x01) ^ (nt_enc & 0x01)) ||
882 (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((nt_enc_par >> 6) & 0x01) ^ (nt_enc >> 8 & 0x01)) ||
883 (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((nt_enc_par >> 7) & 0x01) ^ (nt_enc >> 16 & 0x01))
884 )
885 return false;
886
887 uint32_t ar = prng_successor(ntx, 64);
888 if (
889 (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ar_enc_par >> 5) & 0x01) ^ (ar_enc & 0x01)) ||
890 (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ar_enc_par >> 6) & 0x01) ^ (ar_enc >> 8 & 0x01)) ||
891 (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ar_enc_par >> 7) & 0x01) ^ (ar_enc >> 16 & 0x01))
892 )
893 return false;
894
895 uint32_t at = prng_successor(ntx, 96);
896 if (
897 (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ar_enc_par >> 4) & 0x01) ^ (at_enc >> 24 & 0x01)) ||
898 (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((at_enc_par >> 5) & 0x01) ^ (at_enc & 0x01)) ||
899 (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((at_enc_par >> 6) & 0x01) ^ (at_enc >> 8 & 0x01)) ||
900 (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((at_enc_par >> 7) & 0x01) ^ (at_enc >> 16 & 0x01))
901 )
902 return false;
903
904 return true;
905 }
906
907
908 int mfTraceDecode(uint8_t *data_src, int len, uint8_t parity, bool wantSaveToEmlFile) {
909 uint8_t data[64];
910
911 if (traceState == TRACE_ERROR) return 1;
912 if (len > 64) {
913 traceState = TRACE_ERROR;
914 return 1;
915 }
916
917 memcpy(data, data_src, len);
918 if ((traceCrypto1) && ((traceState == TRACE_IDLE) || (traceState > TRACE_AUTH_OK))) {
919 mf_crypto1_decrypt(traceCrypto1, data, len, 0);
920 uint8_t parity[16];
921 oddparitybuf(data, len, parity);
922 PrintAndLog("dec> %s [%s]", sprint_hex(data, len), printBitsPar(parity, len));
923 AddLogHex(logHexFileName, "dec> ", data, len);
924 }
925
926 switch (traceState) {
927 case TRACE_IDLE:
928 // check packet crc16!
929 if ((len >= 4) && (!CheckCrc14443(CRC_14443_A, data, len))) {
930 PrintAndLog("dec> CRC ERROR!!!");
931 AddLogLine(logHexFileName, "dec> ", "CRC ERROR!!!");
932 traceState = TRACE_ERROR; // do not decrypt the next commands
933 return 1;
934 }
935
936 // AUTHENTICATION
937 if ((len ==4) && ((data[0] == 0x60) || (data[0] == 0x61))) {
938 traceState = TRACE_AUTH1;
939 traceCurBlock = data[1];
940 traceCurKey = data[0] == 60 ? 1:0;
941 return 0;
942 }
943
944 // READ
945 if ((len ==4) && ((data[0] == 0x30))) {
946 traceState = TRACE_READ_DATA;
947 traceCurBlock = data[1];
948 return 0;
949 }
950
951 // WRITE
952 if ((len ==4) && ((data[0] == 0xA0))) {
953 traceState = TRACE_WRITE_OK;
954 traceCurBlock = data[1];
955 return 0;
956 }
957
958 // HALT
959 if ((len ==4) && ((data[0] == 0x50) && (data[1] == 0x00))) {
960 traceState = TRACE_ERROR; // do not decrypt the next commands
961 return 0;
962 }
963
964 return 0;
965 break;
966
967 case TRACE_READ_DATA:
968 if (len == 18) {
969 traceState = TRACE_IDLE;
970
971 if (isBlockTrailer(traceCurBlock)) {
972 memcpy(traceCard + traceCurBlock * 16 + 6, data + 6, 4);
973 } else {
974 memcpy(traceCard + traceCurBlock * 16, data, 16);
975 }
976 if (wantSaveToEmlFile) saveTraceCard();
977 return 0;
978 } else {
979 traceState = TRACE_ERROR;
980 return 1;
981 }
982 break;
983
984 case TRACE_WRITE_OK:
985 if ((len == 1) && (data[0] == 0x0a)) {
986 traceState = TRACE_WRITE_DATA;
987
988 return 0;
989 } else {
990 traceState = TRACE_ERROR;
991 return 1;
992 }
993 break;
994
995 case TRACE_WRITE_DATA:
996 if (len == 18) {
997 traceState = TRACE_IDLE;
998
999 memcpy(traceCard + traceCurBlock * 16, data, 16);
1000 if (wantSaveToEmlFile) saveTraceCard();
1001 return 0;
1002 } else {
1003 traceState = TRACE_ERROR;
1004 return 1;
1005 }
1006 break;
1007
1008 case TRACE_AUTH1:
1009 if (len == 4) {
1010 traceState = TRACE_AUTH2;
1011 if (!traceCrypto1) {
1012 nt = bytes_to_num(data, 4);
1013 } else {
1014 nt_enc = bytes_to_num(data, 4);
1015 nt_enc_par = parity;
1016 }
1017 return 0;
1018 } else {
1019 traceState = TRACE_ERROR;
1020 return 1;
1021 }
1022 break;
1023
1024 case TRACE_AUTH2:
1025 if (len == 8) {
1026 traceState = TRACE_AUTH_OK;
1027
1028 nr_enc = bytes_to_num(data, 4);
1029 ar_enc = bytes_to_num(data + 4, 4);
1030 ar_enc_par = parity << 4;
1031 return 0;
1032 } else {
1033 traceState = TRACE_ERROR;
1034 return 1;
1035 }
1036 break;
1037
1038 case TRACE_AUTH_OK:
1039 if (len ==4) {
1040 traceState = TRACE_IDLE;
1041
1042 at_enc = bytes_to_num(data, 4);
1043 at_enc_par = parity;
1044 if (!traceCrypto1) {
1045
1046 // decode key here)
1047 ks2 = ar_enc ^ prng_successor(nt, 64);
1048 ks3 = at_enc ^ prng_successor(nt, 96);
1049 revstate = lfsr_recovery64(ks2, ks3);
1050 lfsr_rollback_word(revstate, 0, 0);
1051 lfsr_rollback_word(revstate, 0, 0);
1052 lfsr_rollback_word(revstate, nr_enc, 1);
1053 lfsr_rollback_word(revstate, uid ^ nt, 0);
1054
1055 crypto1_get_lfsr(revstate, &lfsr);
1056 crypto1_destroy(revstate);
1057 ui64Key = lfsr;
1058 printf("key> probable key:%x%x Prng:%s ks2:%08x ks3:%08x\n",
1059 (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),
1060 validate_prng_nonce(nt) ? "WEAK": "HARDEND",
1061 ks2,
1062 ks3);
1063 AddLogUint64(logHexFileName, "key> ", lfsr);
1064 } else {
1065 if (validate_prng_nonce(nt)) {
1066 struct Crypto1State *pcs;
1067 pcs = crypto1_create(ui64Key);
1068 uint32_t nt1 = crypto1_word(pcs, nt_enc ^ uid, 1) ^ nt_enc;
1069 uint32_t ar = prng_successor(nt1, 64);
1070 uint32_t at = prng_successor(nt1, 96);
1071 printf("key> nested auth uid: %08x nt: %08x nt_parity: %s ar: %08x at: %08x\n", uid, nt1, printBitsPar(&nt_enc_par, 4), ar, at);
1072 uint32_t nr1 = crypto1_word(pcs, nr_enc, 1) ^ nr_enc;
1073 uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ar_enc;
1074 uint32_t at1 = crypto1_word(pcs, 0, 0) ^ at_enc;
1075 crypto1_destroy(pcs);
1076 printf("key> the same key test. nr1: %08x ar1: %08x at1: %08x \n", nr1, ar1, at1);
1077
1078 if (NTParityCheck(nt1))
1079 printf("key> the same key test OK. key=%x%x\n", (unsigned int)((ui64Key & 0xFFFFFFFF00000000) >> 32), (unsigned int)(ui64Key & 0xFFFFFFFF));
1080 else
1081 printf("key> the same key test. check nt parity error.\n");
1082
1083 uint32_t ntc = prng_successor(nt, 90);
1084 uint32_t ntx = 0;
1085 int ntcnt = 0;
1086 for (int i = 0; i < 16383; i++) {
1087 ntc = prng_successor(ntc, 1);
1088 if (NTParityCheck(ntc)){
1089 if (!ntcnt)
1090 ntx = ntc;
1091 ntcnt++;
1092 }
1093 }
1094 if (ntcnt)
1095 printf("key> nt candidate=%08x nonce distance=%d candidates count=%d\n", ntx, nonce_distance(nt, ntx), ntcnt);
1096 else
1097 printf("key> don't have any nt candidate( \n");
1098
1099 nt = ntx;
1100 ks2 = ar_enc ^ prng_successor(ntx, 64);
1101 ks3 = at_enc ^ prng_successor(ntx, 96);
1102
1103 // decode key
1104 revstate = lfsr_recovery64(ks2, ks3);
1105 lfsr_rollback_word(revstate, 0, 0);
1106 lfsr_rollback_word(revstate, 0, 0);
1107 lfsr_rollback_word(revstate, nr_enc, 1);
1108 lfsr_rollback_word(revstate, uid ^ nt, 0);
1109
1110 crypto1_get_lfsr(revstate, &lfsr);
1111 crypto1_destroy(revstate);
1112 ui64Key = lfsr;
1113 printf("key> probable key:%x%x ks2:%08x ks3:%08x\n",
1114 (unsigned int)((lfsr & 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr & 0xFFFFFFFF),
1115 ks2,
1116 ks3);
1117 AddLogUint64(logHexFileName, "key> ", lfsr);
1118 } else {
1119 printf("key> hardnested not implemented!\n");
1120
1121 crypto1_destroy(traceCrypto1);
1122
1123 // not implemented
1124 traceState = TRACE_ERROR;
1125 }
1126 }
1127
1128 int blockShift = ((traceCurBlock & 0xFC) + 3) * 16;
1129 if (isBlockEmpty((traceCurBlock & 0xFC) + 3)) memcpy(traceCard + blockShift + 6, trailerAccessBytes, 4);
1130
1131 if (traceCurKey) {
1132 num_to_bytes(lfsr, 6, traceCard + blockShift + 10);
1133 } else {
1134 num_to_bytes(lfsr, 6, traceCard + blockShift);
1135 }
1136 if (wantSaveToEmlFile) saveTraceCard();
1137
1138 if (traceCrypto1) {
1139 crypto1_destroy(traceCrypto1);
1140 }
1141
1142 // set cryptosystem state
1143 traceCrypto1 = lfsr_recovery64(ks2, ks3);
1144 return 0;
1145 } else {
1146 traceState = TRACE_ERROR;
1147 return 1;
1148 }
1149 break;
1150
1151 default:
1152 traceState = TRACE_ERROR;
1153 return 1;
1154 }
1155
1156 return 0;
1157 }
1158
1159 // DECODING
1160
1161 int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){
1162 /*
1163 uint32_t nt; // tag challenge
1164 uint32_t ar_enc; // encrypted reader response
1165 uint32_t at_enc; // encrypted tag response
1166 */
1167 if (traceCrypto1) {
1168 crypto1_destroy(traceCrypto1);
1169 }
1170 ks2 = ar_enc ^ prng_successor(nt, 64);
1171 ks3 = at_enc ^ prng_successor(nt, 96);
1172 traceCrypto1 = lfsr_recovery64(ks2, ks3);
1173
1174 mf_crypto1_decrypt(traceCrypto1, data, len, 0);
1175
1176 PrintAndLog("Decrypted data: [%s]", sprint_hex(data,len) );
1177 crypto1_destroy(traceCrypto1);
1178 return 0;
1179 }
1180
1181 /** validate_prng_nonce
1182 * Determine if nonce is deterministic. ie: Suspectable to Darkside attack.
1183 * returns
1184 * true = weak prng
1185 * false = hardend prng
1186 */
1187 bool validate_prng_nonce(uint32_t nonce) {
1188 uint16_t *dist = 0;
1189 uint16_t x, i;
1190
1191 dist = malloc(2 << 16);
1192 if(!dist)
1193 return -1;
1194
1195 // init prng table:
1196 for (x = i = 1; i; ++i) {
1197 dist[(x & 0xff) << 8 | x >> 8] = i;
1198 x = x >> 1 | (x ^ x >> 2 ^ x >> 3 ^ x >> 5) << 15;
1199 }
1200
1201 uint32_t res = (65535 - dist[nonce >> 16] + dist[nonce & 0xffff]) % 65535;
1202
1203 free(dist);
1204 return (res == 16);
1205 }
1206
1207 /* Detect Tag Prng,
1208 * function performs a partial AUTH, where it tries to authenticate against block0, key A, but only collects tag nonce.
1209 * the tag nonce is check to see if it has a predictable PRNG.
1210 * @returns
1211 * TRUE if tag uses WEAK prng (ie Now the NACK bug also needs to be present for Darkside attack)
1212 * FALSE is tag uses HARDEND prng (ie hardnested attack possible, with known key)
1213 */
1214 int DetectClassicPrng(void){
1215
1216 UsbCommand resp, respA;
1217 uint8_t cmd[] = {0x60, 0x00}; // MIFARE_AUTH_KEYA
1218 uint32_t flags = ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_RATS;
1219
1220 UsbCommand c = {CMD_READER_ISO_14443a, {flags, sizeof(cmd), 0}};
1221 memcpy(c.d.asBytes, cmd, sizeof(cmd));
1222
1223 clearCommandBuffer();
1224 SendCommand(&c);
1225 if (!WaitForResponseTimeout(CMD_NACK, &resp, 2000)) {
1226 PrintAndLog("PRNG UID: Reply timeout.");
1227 return -1;
1228 }
1229
1230 // if select tag failed.
1231 if (resp.arg[0] == 0) {
1232 PrintAndLog("PRNG error: selecting tag failed, can't detect prng.");
1233 return -1;
1234 }
1235
1236 if (!WaitForResponseTimeout(CMD_ACK, &respA, 5000)) {
1237 PrintAndLog("PRNG data: Reply timeout.");
1238 return -1;
1239 }
1240
1241 // check respA
1242 if (respA.arg[0] != 4) {
1243 PrintAndLog("PRNG data error: Wrong length: %d", respA.arg[0]);
1244 return -1;
1245 }
1246
1247 uint32_t nonce = bytes_to_num(respA.d.asBytes, respA.arg[0]);
1248 return validate_prng_nonce(nonce);
1249 }
Impressum, Datenschutz