]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/pcf7931.c
Bugfixes for LF FDX
[proxmark3-svn] / armsrc / pcf7931.c
1 #include "proxmark3.h"
2 #include "apps.h"
3 #include "lfsampling.h"
4 #include "pcf7931.h"
5 #include "util.h"
6 #include "string.h"
7 #include "fpgaloader.h"
8
9 #define T0_PCF 8 //period for the pcf7931 in us
10 #define ALLOC 16
11
12 size_t DemodPCF7931(uint8_t **outBlocks) {
13 uint8_t bits[256] = {0x00};
14 uint8_t blocks[8][16];
15 uint8_t *dest = BigBuf_get_addr();
16
17 int GraphTraceLen = BigBuf_max_traceLen();
18 if (GraphTraceLen > 18000)
19 GraphTraceLen = 18000;
20
21 int i, j, lastval, bitidx, half_switch;
22 int clock = 64;
23 int tolerance = clock / 8;
24 int pmc, block_done;
25 int lc, warnings = 0;
26 size_t num_blocks = 0;
27 int lmin=128, lmax=128;
28 uint8_t dir;
29 //clear read buffer
30 BigBuf_Clear_keep_EM();
31
32 LFSetupFPGAForADC(95, true);
33 DoAcquisition_default(0, true);
34
35 lmin = 64;
36 lmax = 192;
37
38 i = 2;
39
40 /* Find first local max/min */
41 if(dest[1] > dest[0]) {
42 while(i < GraphTraceLen) {
43 if( !(dest[i] > dest[i-1]) && dest[i] > lmax)
44 break;
45 i++;
46 }
47 dir = 0;
48 } else {
49 while(i < GraphTraceLen) {
50 if( !(dest[i] < dest[i-1]) && dest[i] < lmin)
51 break;
52 i++;
53 }
54 dir = 1;
55 }
56
57 lastval = i++;
58 half_switch = 0;
59 pmc = 0;
60 block_done = 0;
61
62 for (bitidx = 0; i < GraphTraceLen; i++) {
63 if ((dest[i-1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i-1] < dest[i] && dir == 0 && dest[i] < lmin)) {
64 lc = i - lastval;
65 lastval = i;
66
67 // Switch depending on lc length:
68 // Tolerance is 1/8 of clock rate (arbitrary)
69 if (ABS(lc-clock/4) < tolerance) {
70 // 16T0
71 if((i - pmc) == lc) { /* 16T0 was previous one */
72 /* It's a PMC ! */
73 i += (128+127+16+32+33+16)-1;
74 lastval = i;
75 pmc = 0;
76 block_done = 1;
77 } else {
78 pmc = i;
79 }
80 } else if (ABS(lc-clock/2) < tolerance) {
81 // 32TO
82 if((i - pmc) == lc) { /* 16T0 was previous one */
83 /* It's a PMC ! */
84 i += (128+127+16+32+33)-1;
85 lastval = i;
86 pmc = 0;
87 block_done = 1;
88 } else if(half_switch == 1) {
89 bits[bitidx++] = 0;
90 half_switch = 0;
91 }
92 else
93 half_switch++;
94 } else if (ABS(lc-clock) < tolerance) {
95 // 64TO
96 bits[bitidx++] = 1;
97 } else {
98 // Error
99 if (++warnings > 10) {
100 Dbprintf("Error: too many detection errors, aborting.");
101 return 0;
102 }
103 }
104
105 if(block_done == 1) {
106 if(bitidx == 128) {
107 for(j = 0; j < 16; ++j) {
108 blocks[num_blocks][j] =
109 128 * bits[j*8 + 7]+
110 64 * bits[j*8 + 6] +
111 32 * bits[j*8 + 5] +
112 16 * bits[j*8 + 4] +
113 8 * bits[j*8 + 3] +
114 4 * bits[j*8 + 2] +
115 2 * bits[j*8 + 1] +
116 bits[j*8]
117 ;
118 }
119 num_blocks++;
120 }
121 bitidx = 0;
122 block_done = 0;
123 half_switch = 0;
124 }
125 if(i < GraphTraceLen)
126 dir = (dest[i-1] > dest[i]) ? 0 : 1;
127 }
128 if(bitidx==255)
129 bitidx=0;
130 warnings = 0;
131 if(num_blocks == 4) break;
132 }
133 memcpy(outBlocks, blocks, 16 * num_blocks);
134 return num_blocks;
135 }
136
137 bool IsBlock0PCF7931(uint8_t *block) {
138 // assuming all RFU bits are set to 0
139 // if PAC is enabled password is set to 0
140 if (block[7] == 0x01)
141 {
142 if (!memcmp(block, "\x00\x00\x00\x00\x00\x00\x00", 7) && !memcmp(block+9, "\x00\x00\x00\x00\x00\x00\x00", 7))
143 return true;
144 }
145 else if (block[7] == 0x00)
146 {
147 if (!memcmp(block+9, "\x00\x00\x00\x00\x00\x00\x00", 7))
148 return true;
149 }
150 return false;
151 }
152
153 bool IsBlock1PCF7931(uint8_t *block) {
154 // assuming all RFU bits are set to 0
155 if (block[10] == 0 && block[11] == 0 && block[12] == 0 && block[13] == 0)
156 if((block[14] & 0x7f) <= 9 && block[15] <= 9)
157 return true;
158
159 return false;
160 }
161
162 void ReadPCF7931() {
163 int found_blocks = 0; // successfully read blocks
164 int max_blocks = 8; // readable blocks
165 uint8_t memory_blocks[8][17]; // PCF content
166
167 uint8_t single_blocks[8][17]; // PFC blocks with unknown position
168 int single_blocks_cnt = 0;
169
170 size_t n = 0; // transmitted blocks
171 uint8_t tmp_blocks[4][16]; // temporary read buffer
172
173 uint8_t found_0_1 = 0; // flag: blocks 0 and 1 were found
174 int errors = 0; // error counter
175 int tries = 0; // tries counter
176
177 memset(memory_blocks, 0, 8*17*sizeof(uint8_t));
178 memset(single_blocks, 0, 8*17*sizeof(uint8_t));
179
180 int i = 0, j = 0;
181
182 do {
183 i = 0;
184
185 memset(tmp_blocks, 0, 4*16*sizeof(uint8_t));
186 n = DemodPCF7931((uint8_t**)tmp_blocks);
187 if(!n)
188 ++errors;
189
190 // exit if no block is received
191 if (errors >= 10 && found_blocks == 0 && single_blocks_cnt == 0) {
192 Dbprintf("Error, no tag or bad tag");
193 return;
194 }
195 // exit if too many errors during reading
196 if (tries > 50 && (2*errors > tries)) {
197 Dbprintf("Error reading the tag");
198 Dbprintf("Here is the partial content");
199 goto end;
200 }
201
202 // our logic breaks if we don't get at least two blocks
203 if (n < 2) {
204 if (n == 0 || !memcmp(tmp_blocks[0], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16))
205 continue;
206
207 if (single_blocks_cnt < max_blocks) {
208 for (i = 0; i < single_blocks_cnt; ++i) {
209 if (!memcmp(single_blocks[i], tmp_blocks[0], 16)) {
210 j = 1;
211 break;
212 }
213 }
214 if (j != 1) {
215 memcpy(single_blocks[single_blocks_cnt], tmp_blocks[0], 16);
216 single_blocks_cnt++;
217 }
218 j = 0;
219 }
220 ++tries;
221 continue;
222 }
223
224 Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (max_blocks == 0 ? found_blocks : max_blocks), tries, errors);
225
226 i = 0;
227 if(!found_0_1) {
228 while (i < n - 1) {
229 if (IsBlock0PCF7931(tmp_blocks[i]) && IsBlock1PCF7931(tmp_blocks[i+1])) {
230 found_0_1 = 1;
231 memcpy(memory_blocks[0], tmp_blocks[i], 16);
232 memcpy(memory_blocks[1], tmp_blocks[i+1], 16);
233 memory_blocks[0][ALLOC] = memory_blocks[1][ALLOC] = 1;
234 // block 1 tells how many blocks are going to be sent
235 max_blocks = MAX((memory_blocks[1][14] & 0x7f), memory_blocks[1][15]) + 1;
236 found_blocks = 2;
237
238 Dbprintf("Found blocks 0 and 1. PCF is transmitting %d blocks.", max_blocks);
239
240 // handle the following blocks
241 for (j = i + 2; j < n; ++j) {
242 memcpy(memory_blocks[found_blocks], tmp_blocks[j], 16);
243 memory_blocks[found_blocks][ALLOC] = 1;
244 ++found_blocks;
245 }
246 break;
247 }
248 ++i;
249 }
250 } else {
251 // Trying to re-order blocks
252 // Look for identical block in memory blocks
253 while (i < n-1) {
254 // skip all zeroes blocks
255 if (memcmp(tmp_blocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
256 for (j = 1; j < max_blocks - 1; ++j) {
257 if (!memcmp(tmp_blocks[i], memory_blocks[j], 16) && !memory_blocks[j+1][ALLOC]) {
258 memcpy(memory_blocks[j+1], tmp_blocks[i+1], 16);
259 memory_blocks[j+1][ALLOC] = 1;
260 if (++found_blocks >= max_blocks) goto end;
261 }
262 }
263 }
264 if (memcmp(tmp_blocks[i+1], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
265 for (j = 0; j < max_blocks; ++j) {
266 if (!memcmp(tmp_blocks[i+1], memory_blocks[j], 16) && !memory_blocks[(j == 0 ? max_blocks : j) -1][ALLOC]) {
267 if (j == 0) {
268 memcpy(memory_blocks[max_blocks - 1], tmp_blocks[i], 16);
269 memory_blocks[max_blocks - 1][ALLOC] = 1;
270 } else {
271 memcpy(memory_blocks[j-1], tmp_blocks[i], 16);
272 memory_blocks[j-1][ALLOC] = 1;
273 }
274 if (++found_blocks >= max_blocks) goto end;
275 }
276 }
277 }
278 ++i;
279 }
280 }
281 ++tries;
282 if (BUTTON_PRESS()) {
283 Dbprintf("Button pressed, stopping.");
284 goto end;
285 }
286 }
287 while (found_blocks != max_blocks);
288
289 end:
290 Dbprintf("-----------------------------------------");
291 Dbprintf("Memory content:");
292 Dbprintf("-----------------------------------------");
293 for (i = 0; i < max_blocks; ++i) {
294 if (memory_blocks[i][ALLOC])
295 print_result("Block", memory_blocks[i], 16);
296 else
297 Dbprintf("<missing block %d>", i);
298 }
299 Dbprintf("-----------------------------------------");
300
301 if (found_blocks < max_blocks) {
302 Dbprintf("-----------------------------------------");
303 Dbprintf("Blocks with unknown position:");
304 Dbprintf("-----------------------------------------");
305 for (i = 0; i < single_blocks_cnt; ++i)
306 print_result("Block", single_blocks[i], 16);
307
308 Dbprintf("-----------------------------------------");
309 }
310 cmd_send(CMD_ACK,0,0,0,0,0);
311 }
312
313 static void RealWritePCF7931(uint8_t *pass, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data) {
314 uint32_t tab[1024]={0}; // data times frame
315 uint32_t u = 0;
316 uint8_t parity = 0;
317 bool comp = 0;
318
319 //BUILD OF THE DATA FRAME
320 //alimentation of the tag (time for initializing)
321 AddPatternPCF7931(init_delay, 0, 8192/2*T0_PCF, tab);
322 AddPatternPCF7931(8192/2*T0_PCF + 319*T0_PCF+70, 3*T0_PCF, 29*T0_PCF, tab);
323 //password indication bit
324 AddBitPCF7931(1, tab, l, p);
325 // password (on 56 bits)
326 AddBytePCF7931(pass[0], tab, l, p);
327 AddBytePCF7931(pass[1], tab, l, p);
328 AddBytePCF7931(pass[2], tab, l, p);
329 AddBytePCF7931(pass[3], tab, l, p);
330 AddBytePCF7931(pass[4], tab, l, p);
331 AddBytePCF7931(pass[5], tab, l, p);
332 AddBytePCF7931(pass[6], tab, l, p);
333 //programming mode (0 or 1)
334 AddBitPCF7931(0, tab, l, p);
335
336 //block adress on 6 bits
337 for (u = 0; u < 6; ++u) {
338 if (address & (1 << u)) { // bit 1
339 ++parity;
340 AddBitPCF7931(1, tab, l, p);
341 } else { // bit 0
342 AddBitPCF7931(0, tab, l, p);
343 }
344 }
345
346 //byte address on 4 bits
347 for (u = 0; u < 4; ++u)
348 {
349 if (byte & (1 << u)) { // bit 1
350 parity++;
351 AddBitPCF7931(1, tab, l, p);
352 }
353 else // bit 0
354 AddBitPCF7931(0, tab, l, p);
355 }
356
357 //data on 8 bits
358 for (u=0; u<8; u++)
359 {
360 if (data&(1<<u)) { // bit 1
361 parity++;
362 AddBitPCF7931(1, tab, l, p);
363 }
364 else //bit 0
365 AddBitPCF7931(0, tab, l, p);
366 }
367
368 //parity bit
369 if ((parity % 2) == 0)
370 AddBitPCF7931(0, tab, l, p); //even parity
371 else
372 AddBitPCF7931(1, tab, l, p);//odd parity
373
374 //time access memory
375 AddPatternPCF7931(5120+2680, 0, 0, tab);
376
377 //conversion of the scale time
378 for (u = 0; u < 500; ++u)
379 tab[u] = (tab[u] * 3) / 2;
380
381 //compennsation of the counter reload
382 while (!comp) {
383 comp = 1;
384 for (u = 0; tab[u] != 0; ++u)
385 if(tab[u] > 0xFFFF) {
386 tab[u] -= 0xFFFF;
387 comp = 0;
388 }
389 }
390
391 SendCmdPCF7931(tab);
392 }
393
394 void BruteForcePCF7931(uint64_t password, uint8_t tries, uint16_t init_delay, int32_t l, int32_t p) {
395 uint8_t i = 0;
396 uint8_t pass_array[7];
397
398 while (password < 0x00FFFFFFFFFFFFFF) {
399 if (BUTTON_PRESS()) {
400 Dbprintf("Button pressed, stopping bruteforce ...");
401 return;
402 }
403
404 pass_array[0] = password & 0xFF;
405 pass_array[1] = (password >> 8) & 0xFF;
406 pass_array[2] = (password >> 16) & 0xFF;
407 pass_array[3] = (password >> 24) & 0xFF;
408 pass_array[4] = (password >> 32) & 0xFF;
409 pass_array[5] = (password >> 40) & 0xFF;
410 pass_array[6] = (password >> 48) & 0xFF;
411
412 Dbprintf("Trying: %02x %02x %02x %02x %02x %02x %02x ...",
413 pass_array[0],
414 pass_array[1],
415 pass_array[2],
416 pass_array[3],
417 pass_array[4],
418 pass_array[5],
419 pass_array[6]);
420
421 for (i = 0; i < tries; ++i)
422 RealWritePCF7931
423 (
424 pass_array,
425 init_delay,
426 l,
427 p,
428 0,
429 7,
430 0x01
431 );
432
433 ++password;
434 }
435 }
436
437 /* Write on a byte of a PCF7931 tag
438 * @param address : address of the block to write
439 @param byte : address of the byte to write
440 @param data : data to write
441 */
442 void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, uint8_t pass5, uint8_t pass6, uint8_t pass7, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data) {
443 Dbprintf("Initialization delay : %d us", init_delay);
444 Dbprintf("Offsets : %d us on the low pulses width, %d us on the low pulses positions", l, p);
445 Dbprintf("Password (LSB first on each byte): %02x %02x %02x %02x %02x %02x %02x", pass1, pass2, pass3, pass4, pass5, pass6, pass7);
446 Dbprintf("Block address : %02x", address);
447 Dbprintf("Byte address : %02x", byte);
448 Dbprintf("Data : %02x", data);
449
450 uint8_t password[7] = {pass1, pass2, pass3, pass4, pass5, pass6, pass7};
451
452 RealWritePCF7931 (password, init_delay, l, p, address, byte, data);
453 }
454
455
456
457 /* Send a trame to a PCF7931 tags
458 * @param tab : array of the data frame
459 */
460
461 void SendCmdPCF7931(uint32_t * tab) {
462 uint16_t u=0;
463 uint16_t tempo=0;
464
465 Dbprintf("Sending data frame ...");
466
467 FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
468 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
469 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU );
470
471 LED_A_ON();
472
473 // steal this pin from the SSP and use it to control the modulation
474 AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
475 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
476
477 //initialization of the timer
478 AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14);
479 AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
480 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
481 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK; //clock at 48/32 MHz
482 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN;
483 AT91C_BASE_TCB->TCB_BCR = 1;
484
485 tempo = AT91C_BASE_TC0->TC_CV;
486 for (u = 0; tab[u] != 0; u += 3) {
487 // modulate antenna
488 HIGH(GPIO_SSC_DOUT);
489 while(tempo != tab[u])
490 tempo = AT91C_BASE_TC0->TC_CV;
491
492 // stop modulating antenna
493 LOW(GPIO_SSC_DOUT);
494 while(tempo != tab[u+1])
495 tempo = AT91C_BASE_TC0->TC_CV;
496
497 // modulate antenna
498 HIGH(GPIO_SSC_DOUT);
499 while(tempo != tab[u+2])
500 tempo = AT91C_BASE_TC0->TC_CV;
501 }
502
503 LED_A_OFF();
504 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
505 SpinDelay(200);
506
507 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
508 DbpString("Data frame sent (multiple sends may be needed)");
509 LED(0xFFFF, 1000);
510 }
511
512
513 /* Add a byte for building the data frame of PCF7931 tags
514 * @param b : byte to add
515 * @param tab : array of the data frame
516 * @param l : offset on low pulse width
517 * @param p : offset on low pulse positioning
518 */
519 bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p) {
520 uint32_t u;
521 for (u = 0; u < 8; ++u) {
522 if (byte & (1 << u)) { //bit is 1
523 if(AddBitPCF7931(1, tab, l, p)==1)return 1;
524 } else { //bit is 0
525 if(AddBitPCF7931(0, tab, l, p)==1)return 1;
526 }
527 }
528
529 return 0;
530 }
531
532 /* Add a bits for building the data frame of PCF7931 tags
533 * @param b : bit to add
534 * @param tab : array of the data frame
535 * @param l : offset on low pulse width
536 * @param p : offset on low pulse positioning
537 */
538 bool AddBitPCF7931(bool b, uint32_t * tab, int32_t l, int32_t p) {
539 uint8_t u = 0;
540
541 for (u = 0; tab[u] != 0; u += 3){} //we put the cursor at the last value of the array
542
543 if (b == 1) { //add a bit 1
544 if (u == 0) tab[u] = 34 * T0_PCF + p;
545 else tab[u] = 34 * T0_PCF + tab[u-1] + p;
546
547 tab[u+1] = 6 * T0_PCF+tab[u] + l;
548 tab[u+2] = 88 * T0_PCF+tab[u + 1] - l - p;
549 return 0;
550 } else { //add a bit 0
551
552 if (u == 0) tab[u] = 98 * T0_PCF + p;
553 else tab[u] = 98 * T0_PCF + tab[u-1] + p;
554
555 tab[u + 1] = 6 * T0_PCF + tab[u] + l;
556 tab[u + 2] = 24 * T0_PCF + tab[u + 1] - l - p;
557 return 0;
558 }
559
560 return 1;
561 }
562
563 /* Add a custom pattern in the data frame
564 * @param a : delay of the first high pulse
565 * @param b : delay of the low pulse
566 * @param c : delay of the last high pulse
567 * @param tab : array of the data frame
568 */
569 bool AddPatternPCF7931(uint32_t a, uint32_t b, uint32_t c, uint32_t * tab) {
570 uint32_t u = 0;
571 for(u = 0; tab[u] != 0; u += 3){} //we put the cursor at the last value of the array
572
573 if (u == 0) tab[u] = a;
574 else tab[u] = a + tab[u - 1];
575
576 tab[u + 1] = b + tab[u];
577 tab[u + 2] = c + tab[u + 1];
578
579 return 0;
580 }
Impressum, Datenschutz