]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iclass.c
Iclass decrypt error, shouldn't have decrypted block 6 according to Heart of darkness...
[proxmark3-svn] / armsrc / iclass.c
CommitLineData
cee5a30d 1//-----------------------------------------------------------------------------
2// Gerhard de Koning Gans - May 2008
3// Hagen Fritsch - June 2010
4// Gerhard de Koning Gans - May 2011
1e262141 5// Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
cee5a30d 6//
7// This code is licensed to you under the terms of the GNU GPL, version 2 or,
8// at your option, any later version. See the LICENSE.txt file for the text of
9// the license.
10//-----------------------------------------------------------------------------
11// Routines to support iClass.
12//-----------------------------------------------------------------------------
13// Based on ISO14443a implementation. Still in experimental phase.
14// Contribution made during a security research at Radboud University Nijmegen
15//
16// Please feel free to contribute and extend iClass support!!
17//-----------------------------------------------------------------------------
18//
cee5a30d 19// FIX:
20// ====
21// We still have sometimes a demodulation error when snooping iClass communication.
22// The resulting trace of a read-block-03 command may look something like this:
23//
24// + 22279: : 0c 03 e8 01
25//
26// ...with an incorrect answer...
27//
28// + 85: 0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb 33 bb 00 01! 0e! 04! bb !crc
29//
30// We still left the error signalling bytes in the traces like 0xbb
31//
32// A correct trace should look like this:
33//
34// + 21112: : 0c 03 e8 01
35// + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5
36//
37//-----------------------------------------------------------------------------
38
39#include "proxmark3.h"
40#include "apps.h"
41#include "util.h"
42#include "string.h"
7e67e42f 43#include "common.h"
fecd8202 44#include "cmd.h"
1e262141 45// Needed for CRC in emulation mode;
46// same construction as in ISO 14443;
47// different initial value (CRC_ICLASS)
48#include "iso14443crc.h"
c3963755 49#include "iso15693tools.h"
cee5a30d 50
1e262141 51static int timeout = 4096;
cee5a30d 52
cee5a30d 53
1e262141 54static int SendIClassAnswer(uint8_t *resp, int respLen, int delay);
cee5a30d 55
56//-----------------------------------------------------------------------------
57// The software UART that receives commands from the reader, and its state
58// variables.
59//-----------------------------------------------------------------------------
60static struct {
61 enum {
62 STATE_UNSYNCD,
63 STATE_START_OF_COMMUNICATION,
64 STATE_RECEIVING
65 } state;
66 uint16_t shiftReg;
67 int bitCnt;
68 int byteCnt;
69 int byteCntMax;
70 int posCnt;
71 int nOutOfCnt;
72 int OutOfCnt;
73 int syncBit;
1e262141 74 int samples;
cee5a30d 75 int highCnt;
76 int swapper;
77 int counter;
78 int bitBuffer;
79 int dropPosition;
6a1f2d82 80 uint8_t *output;
cee5a30d 81} Uart;
82
1e262141 83static RAMFUNC int OutOfNDecoding(int bit)
cee5a30d 84{
9f693930 85 //int error = 0;
cee5a30d 86 int bitright;
87
88 if(!Uart.bitBuffer) {
89 Uart.bitBuffer = bit ^ 0xFF0;
90 return FALSE;
91 }
92 else {
93 Uart.bitBuffer <<= 4;
94 Uart.bitBuffer ^= bit;
95 }
96
97 /*if(Uart.swapper) {
98 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
99 Uart.byteCnt++;
100 Uart.swapper = 0;
101 if(Uart.byteCnt > 15) { return TRUE; }
102 }
103 else {
104 Uart.swapper = 1;
105 }*/
106
107 if(Uart.state != STATE_UNSYNCD) {
108 Uart.posCnt++;
109
110 if((Uart.bitBuffer & Uart.syncBit) ^ Uart.syncBit) {
111 bit = 0x00;
112 }
113 else {
114 bit = 0x01;
115 }
116 if(((Uart.bitBuffer << 1) & Uart.syncBit) ^ Uart.syncBit) {
117 bitright = 0x00;
118 }
119 else {
120 bitright = 0x01;
121 }
122 if(bit != bitright) { bit = bitright; }
123
124
125 // So, now we only have to deal with *bit*, lets see...
126 if(Uart.posCnt == 1) {
127 // measurement first half bitperiod
128 if(!bit) {
129 // Drop in first half means that we are either seeing
130 // an SOF or an EOF.
131
132 if(Uart.nOutOfCnt == 1) {
133 // End of Communication
134 Uart.state = STATE_UNSYNCD;
135 Uart.highCnt = 0;
136 if(Uart.byteCnt == 0) {
137 // Its not straightforward to show single EOFs
138 // So just leave it and do not return TRUE
6a1f2d82 139 Uart.output[0] = 0xf0;
cee5a30d 140 Uart.byteCnt++;
cee5a30d 141 }
142 else {
143 return TRUE;
144 }
145 }
146 else if(Uart.state != STATE_START_OF_COMMUNICATION) {
147 // When not part of SOF or EOF, it is an error
148 Uart.state = STATE_UNSYNCD;
149 Uart.highCnt = 0;
9f693930 150 //error = 4;
cee5a30d 151 }
152 }
153 }
154 else {
155 // measurement second half bitperiod
156 // Count the bitslot we are in... (ISO 15693)
157 Uart.nOutOfCnt++;
158
159 if(!bit) {
160 if(Uart.dropPosition) {
161 if(Uart.state == STATE_START_OF_COMMUNICATION) {
9f693930 162 //error = 1;
cee5a30d 163 }
164 else {
9f693930 165 //error = 7;
cee5a30d 166 }
167 // It is an error if we already have seen a drop in current frame
168 Uart.state = STATE_UNSYNCD;
169 Uart.highCnt = 0;
170 }
171 else {
172 Uart.dropPosition = Uart.nOutOfCnt;
173 }
174 }
175
176 Uart.posCnt = 0;
177
178
179 if(Uart.nOutOfCnt == Uart.OutOfCnt && Uart.OutOfCnt == 4) {
180 Uart.nOutOfCnt = 0;
181
182 if(Uart.state == STATE_START_OF_COMMUNICATION) {
183 if(Uart.dropPosition == 4) {
184 Uart.state = STATE_RECEIVING;
185 Uart.OutOfCnt = 256;
186 }
187 else if(Uart.dropPosition == 3) {
188 Uart.state = STATE_RECEIVING;
189 Uart.OutOfCnt = 4;
190 //Uart.output[Uart.byteCnt] = 0xdd;
191 //Uart.byteCnt++;
192 }
193 else {
194 Uart.state = STATE_UNSYNCD;
195 Uart.highCnt = 0;
196 }
197 Uart.dropPosition = 0;
198 }
199 else {
200 // RECEIVING DATA
201 // 1 out of 4
202 if(!Uart.dropPosition) {
203 Uart.state = STATE_UNSYNCD;
204 Uart.highCnt = 0;
9f693930 205 //error = 9;
cee5a30d 206 }
207 else {
208 Uart.shiftReg >>= 2;
209
210 // Swap bit order
211 Uart.dropPosition--;
212 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
213 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
214
215 Uart.shiftReg ^= ((Uart.dropPosition & 0x03) << 6);
216 Uart.bitCnt += 2;
217 Uart.dropPosition = 0;
218
219 if(Uart.bitCnt == 8) {
220 Uart.output[Uart.byteCnt] = (Uart.shiftReg & 0xff);
221 Uart.byteCnt++;
cee5a30d 222 Uart.bitCnt = 0;
223 Uart.shiftReg = 0;
224 }
225 }
226 }
227 }
228 else if(Uart.nOutOfCnt == Uart.OutOfCnt) {
229 // RECEIVING DATA
230 // 1 out of 256
231 if(!Uart.dropPosition) {
232 Uart.state = STATE_UNSYNCD;
233 Uart.highCnt = 0;
9f693930 234 //error = 3;
cee5a30d 235 }
236 else {
237 Uart.dropPosition--;
238 Uart.output[Uart.byteCnt] = (Uart.dropPosition & 0xff);
239 Uart.byteCnt++;
cee5a30d 240 Uart.bitCnt = 0;
241 Uart.shiftReg = 0;
242 Uart.nOutOfCnt = 0;
243 Uart.dropPosition = 0;
244 }
245 }
246
247 /*if(error) {
248 Uart.output[Uart.byteCnt] = 0xAA;
249 Uart.byteCnt++;
250 Uart.output[Uart.byteCnt] = error & 0xFF;
251 Uart.byteCnt++;
252 Uart.output[Uart.byteCnt] = 0xAA;
253 Uart.byteCnt++;
254 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
255 Uart.byteCnt++;
256 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
257 Uart.byteCnt++;
258 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
259 Uart.byteCnt++;
260 Uart.output[Uart.byteCnt] = 0xAA;
261 Uart.byteCnt++;
262 return TRUE;
263 }*/
264 }
265
266 }
267 else {
268 bit = Uart.bitBuffer & 0xf0;
269 bit >>= 4;
270 bit ^= 0x0F; // drops become 1s ;-)
271 if(bit) {
272 // should have been high or at least (4 * 128) / fc
273 // according to ISO this should be at least (9 * 128 + 20) / fc
274 if(Uart.highCnt == 8) {
275 // we went low, so this could be start of communication
276 // it turns out to be safer to choose a less significant
277 // syncbit... so we check whether the neighbour also represents the drop
278 Uart.posCnt = 1; // apparently we are busy with our first half bit period
279 Uart.syncBit = bit & 8;
280 Uart.samples = 3;
281 if(!Uart.syncBit) { Uart.syncBit = bit & 4; Uart.samples = 2; }
282 else if(bit & 4) { Uart.syncBit = bit & 4; Uart.samples = 2; bit <<= 2; }
283 if(!Uart.syncBit) { Uart.syncBit = bit & 2; Uart.samples = 1; }
284 else if(bit & 2) { Uart.syncBit = bit & 2; Uart.samples = 1; bit <<= 1; }
285 if(!Uart.syncBit) { Uart.syncBit = bit & 1; Uart.samples = 0;
286 if(Uart.syncBit && (Uart.bitBuffer & 8)) {
287 Uart.syncBit = 8;
288
289 // the first half bit period is expected in next sample
290 Uart.posCnt = 0;
291 Uart.samples = 3;
292 }
293 }
294 else if(bit & 1) { Uart.syncBit = bit & 1; Uart.samples = 0; }
295
296 Uart.syncBit <<= 4;
297 Uart.state = STATE_START_OF_COMMUNICATION;
298 Uart.bitCnt = 0;
299 Uart.byteCnt = 0;
cee5a30d 300 Uart.nOutOfCnt = 0;
301 Uart.OutOfCnt = 4; // Start at 1/4, could switch to 1/256
302 Uart.dropPosition = 0;
303 Uart.shiftReg = 0;
9f693930 304 //error = 0;
cee5a30d 305 }
306 else {
307 Uart.highCnt = 0;
308 }
309 }
310 else {
311 if(Uart.highCnt < 8) {
312 Uart.highCnt++;
313 }
314 }
315 }
316
317 return FALSE;
318}
319
320//=============================================================================
1e262141 321// Manchester
cee5a30d 322//=============================================================================
323
324static struct {
325 enum {
326 DEMOD_UNSYNCD,
327 DEMOD_START_OF_COMMUNICATION,
328 DEMOD_START_OF_COMMUNICATION2,
329 DEMOD_START_OF_COMMUNICATION3,
330 DEMOD_SOF_COMPLETE,
331 DEMOD_MANCHESTER_D,
332 DEMOD_MANCHESTER_E,
333 DEMOD_END_OF_COMMUNICATION,
334 DEMOD_END_OF_COMMUNICATION2,
335 DEMOD_MANCHESTER_F,
336 DEMOD_ERROR_WAIT
337 } state;
338 int bitCount;
339 int posCount;
340 int syncBit;
cee5a30d 341 uint16_t shiftReg;
342 int buffer;
343 int buffer2;
344 int buffer3;
345 int buff;
346 int samples;
347 int len;
348 enum {
349 SUB_NONE,
350 SUB_FIRST_HALF,
351 SUB_SECOND_HALF,
352 SUB_BOTH
353 } sub;
6a1f2d82 354 uint8_t *output;
cee5a30d 355} Demod;
356
357static RAMFUNC int ManchesterDecoding(int v)
358{
359 int bit;
360 int modulation;
361 int error = 0;
362
363 bit = Demod.buffer;
364 Demod.buffer = Demod.buffer2;
365 Demod.buffer2 = Demod.buffer3;
366 Demod.buffer3 = v;
367
368 if(Demod.buff < 3) {
369 Demod.buff++;
370 return FALSE;
371 }
372
373 if(Demod.state==DEMOD_UNSYNCD) {
374 Demod.output[Demod.len] = 0xfa;
375 Demod.syncBit = 0;
376 //Demod.samples = 0;
377 Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part
cee5a30d 378
379 if(bit & 0x08) {
380 Demod.syncBit = 0x08;
381 }
382
383 if(bit & 0x04) {
384 if(Demod.syncBit) {
385 bit <<= 4;
386 }
387 Demod.syncBit = 0x04;
388 }
389
390 if(bit & 0x02) {
391 if(Demod.syncBit) {
392 bit <<= 2;
393 }
394 Demod.syncBit = 0x02;
395 }
396
397 if(bit & 0x01 && Demod.syncBit) {
398 Demod.syncBit = 0x01;
399 }
400
401 if(Demod.syncBit) {
402 Demod.len = 0;
403 Demod.state = DEMOD_START_OF_COMMUNICATION;
404 Demod.sub = SUB_FIRST_HALF;
405 Demod.bitCount = 0;
406 Demod.shiftReg = 0;
cee5a30d 407 Demod.samples = 0;
408 if(Demod.posCount) {
409 //if(trigger) LED_A_OFF(); // Not useful in this case...
410 switch(Demod.syncBit) {
411 case 0x08: Demod.samples = 3; break;
412 case 0x04: Demod.samples = 2; break;
413 case 0x02: Demod.samples = 1; break;
414 case 0x01: Demod.samples = 0; break;
415 }
416 // SOF must be long burst... otherwise stay unsynced!!!
417 if(!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) {
418 Demod.state = DEMOD_UNSYNCD;
419 }
420 }
421 else {
422 // SOF must be long burst... otherwise stay unsynced!!!
423 if(!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) {
424 Demod.state = DEMOD_UNSYNCD;
425 error = 0x88;
426 }
427
428 }
429 error = 0;
430
431 }
432 }
433 else {
434 modulation = bit & Demod.syncBit;
435 modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
cee5a30d 436
437 Demod.samples += 4;
438
439 if(Demod.posCount==0) {
440 Demod.posCount = 1;
441 if(modulation) {
442 Demod.sub = SUB_FIRST_HALF;
443 }
444 else {
445 Demod.sub = SUB_NONE;
446 }
447 }
448 else {
449 Demod.posCount = 0;
450 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
451 if(Demod.state!=DEMOD_ERROR_WAIT) {
452 Demod.state = DEMOD_ERROR_WAIT;
453 Demod.output[Demod.len] = 0xaa;
454 error = 0x01;
455 }
456 }*/
457 //else if(modulation) {
458 if(modulation) {
459 if(Demod.sub == SUB_FIRST_HALF) {
460 Demod.sub = SUB_BOTH;
461 }
462 else {
463 Demod.sub = SUB_SECOND_HALF;
464 }
465 }
466 else if(Demod.sub == SUB_NONE) {
467 if(Demod.state == DEMOD_SOF_COMPLETE) {
468 Demod.output[Demod.len] = 0x0f;
469 Demod.len++;
cee5a30d 470 Demod.state = DEMOD_UNSYNCD;
471// error = 0x0f;
472 return TRUE;
473 }
474 else {
475 Demod.state = DEMOD_ERROR_WAIT;
476 error = 0x33;
477 }
478 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
479 Demod.state = DEMOD_ERROR_WAIT;
480 Demod.output[Demod.len] = 0xaa;
481 error = 0x01;
482 }*/
483 }
484
485 switch(Demod.state) {
486 case DEMOD_START_OF_COMMUNICATION:
487 if(Demod.sub == SUB_BOTH) {
488 //Demod.state = DEMOD_MANCHESTER_D;
489 Demod.state = DEMOD_START_OF_COMMUNICATION2;
490 Demod.posCount = 1;
491 Demod.sub = SUB_NONE;
492 }
493 else {
494 Demod.output[Demod.len] = 0xab;
495 Demod.state = DEMOD_ERROR_WAIT;
496 error = 0xd2;
497 }
498 break;
499 case DEMOD_START_OF_COMMUNICATION2:
500 if(Demod.sub == SUB_SECOND_HALF) {
501 Demod.state = DEMOD_START_OF_COMMUNICATION3;
502 }
503 else {
504 Demod.output[Demod.len] = 0xab;
505 Demod.state = DEMOD_ERROR_WAIT;
506 error = 0xd3;
507 }
508 break;
509 case DEMOD_START_OF_COMMUNICATION3:
510 if(Demod.sub == SUB_SECOND_HALF) {
511// Demod.state = DEMOD_MANCHESTER_D;
512 Demod.state = DEMOD_SOF_COMPLETE;
513 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
514 //Demod.len++;
515 }
516 else {
517 Demod.output[Demod.len] = 0xab;
518 Demod.state = DEMOD_ERROR_WAIT;
519 error = 0xd4;
520 }
521 break;
522 case DEMOD_SOF_COMPLETE:
523 case DEMOD_MANCHESTER_D:
524 case DEMOD_MANCHESTER_E:
525 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
526 // 00001111 = 1 (0 in 14443)
527 if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF
528 Demod.bitCount++;
529 Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100;
530 Demod.state = DEMOD_MANCHESTER_D;
531 }
532 else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF
533 Demod.bitCount++;
534 Demod.shiftReg >>= 1;
535 Demod.state = DEMOD_MANCHESTER_E;
536 }
537 else if(Demod.sub == SUB_BOTH) {
538 Demod.state = DEMOD_MANCHESTER_F;
539 }
540 else {
541 Demod.state = DEMOD_ERROR_WAIT;
542 error = 0x55;
543 }
544 break;
545
546 case DEMOD_MANCHESTER_F:
547 // Tag response does not need to be a complete byte!
548 if(Demod.len > 0 || Demod.bitCount > 0) {
549 if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF
6a1f2d82 550 Demod.shiftReg >>= (9 - Demod.bitCount); // right align data
cee5a30d 551 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
552 Demod.len++;
cee5a30d 553 }
554
555 Demod.state = DEMOD_UNSYNCD;
556 return TRUE;
557 }
558 else {
559 Demod.output[Demod.len] = 0xad;
560 Demod.state = DEMOD_ERROR_WAIT;
561 error = 0x03;
562 }
563 break;
564
565 case DEMOD_ERROR_WAIT:
566 Demod.state = DEMOD_UNSYNCD;
567 break;
568
569 default:
570 Demod.output[Demod.len] = 0xdd;
571 Demod.state = DEMOD_UNSYNCD;
572 break;
573 }
574
575 /*if(Demod.bitCount>=9) {
576 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
577 Demod.len++;
578
579 Demod.parityBits <<= 1;
580 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
581
582 Demod.bitCount = 0;
583 Demod.shiftReg = 0;
584 }*/
585 if(Demod.bitCount>=8) {
586 Demod.shiftReg >>= 1;
587 Demod.output[Demod.len] = (Demod.shiftReg & 0xff);
588 Demod.len++;
cee5a30d 589 Demod.bitCount = 0;
590 Demod.shiftReg = 0;
591 }
592
593 if(error) {
594 Demod.output[Demod.len] = 0xBB;
595 Demod.len++;
596 Demod.output[Demod.len] = error & 0xFF;
597 Demod.len++;
598 Demod.output[Demod.len] = 0xBB;
599 Demod.len++;
600 Demod.output[Demod.len] = bit & 0xFF;
601 Demod.len++;
602 Demod.output[Demod.len] = Demod.buffer & 0xFF;
603 Demod.len++;
604 // Look harder ;-)
605 Demod.output[Demod.len] = Demod.buffer2 & 0xFF;
606 Demod.len++;
607 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
608 Demod.len++;
609 Demod.output[Demod.len] = 0xBB;
610 Demod.len++;
611 return TRUE;
612 }
613
614 }
615
616 } // end (state != UNSYNCED)
617
618 return FALSE;
619}
620
621//=============================================================================
1e262141 622// Finally, a `sniffer' for iClass communication
cee5a30d 623// Both sides of communication!
624//=============================================================================
625
626//-----------------------------------------------------------------------------
627// Record the sequence of commands sent by the reader to the tag, with
628// triggering so that we start recording at the point that the tag is moved
629// near the reader.
630//-----------------------------------------------------------------------------
631void RAMFUNC SnoopIClass(void)
632{
17cba269 633
cee5a30d 634
635 // We won't start recording the frames that we acquire until we trigger;
636 // a good trigger condition to get started is probably when we see a
637 // response from the tag.
9f693930 638 //int triggered = FALSE; // FALSE to wait first for card
cee5a30d 639
640 // The command (reader -> tag) that we're receiving.
641 // The length of a received command will in most cases be no more than 18 bytes.
642 // So 32 should be enough!
f71f4deb 643 #define ICLASS_BUFFER_SIZE 32
644 uint8_t readerToTagCmd[ICLASS_BUFFER_SIZE];
cee5a30d 645 // The response (tag -> reader) that we're receiving.
f71f4deb 646 uint8_t tagToReaderResponse[ICLASS_BUFFER_SIZE];
6a1f2d82 647
7cc204bf 648 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
649
f71f4deb 650 // free all BigBuf memory
651 BigBuf_free();
652 // The DMA buffer, used to stream samples from the FPGA
653 uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
654
3000dc4e
MHS
655 set_tracing(TRUE);
656 clear_trace();
1e262141 657 iso14a_set_trigger(FALSE);
cee5a30d 658
f71f4deb 659 int lastRxCounter;
117d9ec2 660 uint8_t *upTo;
cee5a30d 661 int smpl;
662 int maxBehindBy = 0;
663
664 // Count of samples received so far, so that we can include timing
665 // information in the trace buffer.
666 int samples = 0;
667 rsamples = 0;
668
cee5a30d 669 // Set up the demodulator for tag -> reader responses.
17cba269 670 Demod.output = tagToReaderResponse;
cee5a30d 671 Demod.len = 0;
672 Demod.state = DEMOD_UNSYNCD;
673
674 // Setup for the DMA.
675 FpgaSetupSsc();
676 upTo = dmaBuf;
677 lastRxCounter = DMA_BUFFER_SIZE;
678 FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE);
679
680 // And the reader -> tag commands
681 memset(&Uart, 0, sizeof(Uart));
17cba269 682 Uart.output = readerToTagCmd;
cee5a30d 683 Uart.byteCntMax = 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
684 Uart.state = STATE_UNSYNCD;
685
686 // And put the FPGA in the appropriate mode
687 // Signal field is off with the appropriate LED
688 LED_D_OFF();
689 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER);
690 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
691
81012e67 692 uint32_t time_0 = GetCountSspClk();
55eaed8f
MHS
693 uint32_t time_start = 0;
694 uint32_t time_stop = 0;
81012e67 695
cee5a30d 696 int div = 0;
697 //int div2 = 0;
698 int decbyte = 0;
699 int decbyter = 0;
700
701 // And now we loop, receiving samples.
702 for(;;) {
703 LED_A_ON();
704 WDT_HIT();
705 int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &
706 (DMA_BUFFER_SIZE-1);
707 if(behindBy > maxBehindBy) {
708 maxBehindBy = behindBy;
f71f4deb 709 if(behindBy > (9 * DMA_BUFFER_SIZE / 10)) {
cee5a30d 710 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
711 goto done;
712 }
713 }
714 if(behindBy < 1) continue;
715
716 LED_A_OFF();
717 smpl = upTo[0];
718 upTo++;
719 lastRxCounter -= 1;
720 if(upTo - dmaBuf > DMA_BUFFER_SIZE) {
721 upTo -= DMA_BUFFER_SIZE;
722 lastRxCounter += DMA_BUFFER_SIZE;
723 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
724 AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;
725 }
726
727 //samples += 4;
728 samples += 1;
cee5a30d 729
cee5a30d 730 if(smpl & 0xF) {
731 decbyte ^= (1 << (3 - div));
732 }
cee5a30d 733
734 // FOR READER SIDE COMMUMICATION...
17cba269 735
cee5a30d 736 decbyter <<= 2;
737 decbyter ^= (smpl & 0x30);
738
739 div++;
740
741 if((div + 1) % 2 == 0) {
742 smpl = decbyter;
1e262141 743 if(OutOfNDecoding((smpl & 0xF0) >> 4)) {
cee5a30d 744 rsamples = samples - Uart.samples;
55eaed8f 745 time_stop = (GetCountSspClk()-time_0) << 4;
cee5a30d 746 LED_C_ON();
17cba269 747
81012e67 748 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
17cba269 749 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
6a1f2d82 750 if(tracing) {
751 uint8_t parity[MAX_PARITY_SIZE];
752 GetParity(Uart.output, Uart.byteCnt, parity);
55eaed8f 753 LogTrace(Uart.output,Uart.byteCnt, time_start, time_stop, parity, TRUE);
81012e67
MHS
754 }
755
17cba269
MHS
756
757 /* And ready to receive another command. */
cee5a30d 758 Uart.state = STATE_UNSYNCD;
759 /* And also reset the demod code, which might have been */
760 /* false-triggered by the commands from the reader. */
761 Demod.state = DEMOD_UNSYNCD;
762 LED_B_OFF();
763 Uart.byteCnt = 0;
55eaed8f
MHS
764 }else{
765 time_start = (GetCountSspClk()-time_0) << 4;
cee5a30d 766 }
767 decbyter = 0;
768 }
769
770 if(div > 3) {
771 smpl = decbyte;
772 if(ManchesterDecoding(smpl & 0x0F)) {
55eaed8f
MHS
773 time_stop = (GetCountSspClk()-time_0) << 4;
774
775 rsamples = samples - Demod.samples;
cee5a30d 776 LED_B_ON();
777
6a1f2d82 778 if(tracing) {
779 uint8_t parity[MAX_PARITY_SIZE];
780 GetParity(Demod.output, Demod.len, parity);
55eaed8f 781 LogTrace(Demod.output, Demod.len, time_start, time_stop, parity, FALSE);
81012e67 782 }
17cba269 783
cee5a30d 784 // And ready to receive another response.
785 memset(&Demod, 0, sizeof(Demod));
17cba269 786 Demod.output = tagToReaderResponse;
cee5a30d 787 Demod.state = DEMOD_UNSYNCD;
788 LED_C_OFF();
55eaed8f
MHS
789 }else{
790 time_start = (GetCountSspClk()-time_0) << 4;
cee5a30d 791 }
792
793 div = 0;
794 decbyte = 0x00;
795 }
796 //}
797
798 if(BUTTON_PRESS()) {
799 DbpString("cancelled_a");
800 goto done;
801 }
802 }
803
804 DbpString("COMMAND FINISHED");
805
806 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
3000dc4e 807 Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]);
cee5a30d 808
809done:
810 AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
811 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
3000dc4e 812 Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]);
cee5a30d 813 LED_A_OFF();
814 LED_B_OFF();
1e262141 815 LED_C_OFF();
816 LED_D_OFF();
817}
818
912a3e94 819void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
820 int i;
821 for(i = 0; i < 8; i++) {
822 rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5);
1e262141 823 }
824}
825
826//-----------------------------------------------------------------------------
827// Wait for commands from reader
828// Stop when button is pressed
829// Or return TRUE when command is captured
830//-----------------------------------------------------------------------------
831static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen)
832{
912a3e94 833 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
1e262141 834 // only, since we are receiving, not transmitting).
835 // Signal field is off with the appropriate LED
836 LED_D_OFF();
837 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
838
839 // Now run a `software UART' on the stream of incoming samples.
840 Uart.output = received;
841 Uart.byteCntMax = maxLen;
842 Uart.state = STATE_UNSYNCD;
843
844 for(;;) {
845 WDT_HIT();
846
847 if(BUTTON_PRESS()) return FALSE;
848
849 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
850 AT91C_BASE_SSC->SSC_THR = 0x00;
851 }
852 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
853 uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
3fe4ff4f 854
1e262141 855 if(OutOfNDecoding(b & 0x0f)) {
856 *len = Uart.byteCnt;
857 return TRUE;
858 }
859 }
860 }
861}
862
645c960f
MHS
863static uint8_t encode4Bits(const uint8_t b)
864{
865 uint8_t c = b & 0xF;
866 // OTA, the least significant bits first
867 // The columns are
868 // 1 - Bit value to send
869 // 2 - Reversed (big-endian)
870 // 3 - Encoded
871 // 4 - Hex values
872
873 switch(c){
874 // 1 2 3 4
875 case 15: return 0x55; // 1111 -> 1111 -> 01010101 -> 0x55
876 case 14: return 0x95; // 1110 -> 0111 -> 10010101 -> 0x95
877 case 13: return 0x65; // 1101 -> 1011 -> 01100101 -> 0x65
878 case 12: return 0xa5; // 1100 -> 0011 -> 10100101 -> 0xa5
879 case 11: return 0x59; // 1011 -> 1101 -> 01011001 -> 0x59
880 case 10: return 0x99; // 1010 -> 0101 -> 10011001 -> 0x99
881 case 9: return 0x69; // 1001 -> 1001 -> 01101001 -> 0x69
882 case 8: return 0xa9; // 1000 -> 0001 -> 10101001 -> 0xa9
883 case 7: return 0x56; // 0111 -> 1110 -> 01010110 -> 0x56
884 case 6: return 0x96; // 0110 -> 0110 -> 10010110 -> 0x96
885 case 5: return 0x66; // 0101 -> 1010 -> 01100110 -> 0x66
886 case 4: return 0xa6; // 0100 -> 0010 -> 10100110 -> 0xa6
887 case 3: return 0x5a; // 0011 -> 1100 -> 01011010 -> 0x5a
888 case 2: return 0x9a; // 0010 -> 0100 -> 10011010 -> 0x9a
889 case 1: return 0x6a; // 0001 -> 1000 -> 01101010 -> 0x6a
890 default: return 0xaa; // 0000 -> 0000 -> 10101010 -> 0xaa
891
892 }
893}
1e262141 894
895//-----------------------------------------------------------------------------
896// Prepare tag messages
897//-----------------------------------------------------------------------------
898static void CodeIClassTagAnswer(const uint8_t *cmd, int len)
899{
645c960f
MHS
900
901 /*
902 * SOF comprises 3 parts;
903 * * An unmodulated time of 56.64 us
904 * * 24 pulses of 423.75 KHz (fc/32)
905 * * A logic 1, which starts with an unmodulated time of 18.88us
906 * followed by 8 pulses of 423.75kHz (fc/32)
907 *
908 *
909 * EOF comprises 3 parts:
910 * - A logic 0 (which starts with 8 pulses of fc/32 followed by an unmodulated
911 * time of 18.88us.
912 * - 24 pulses of fc/32
913 * - An unmodulated time of 56.64 us
914 *
915 *
916 * A logic 0 starts with 8 pulses of fc/32
917 * followed by an unmodulated time of 256/fc (~18,88us).
918 *
919 * A logic 0 starts with unmodulated time of 256/fc (~18,88us) followed by
920 * 8 pulses of fc/32 (also 18.88us)
921 *
922 * The mode FPGA_HF_SIMULATOR_MODULATE_424K_8BIT which we use to simulate tag,
923 * works like this.
924 * - A 1-bit input to the FPGA becomes 8 pulses on 423.5kHz (fc/32) (18.88us).
925 * - A 0-bit inptu to the FPGA becomes an unmodulated time of 18.88us
926 *
6b038d19 927 * In this mode the SOF can be written as 00011101 = 0x1D
645c960f
MHS
928 * The EOF can be written as 10111000 = 0xb8
929 * A logic 1 is 01
930 * A logic 0 is 10
931 *
932 * */
933
1e262141 934 int i;
935
936 ToSendReset();
937
938 // Send SOF
645c960f 939 ToSend[++ToSendMax] = 0x1D;
1e262141 940
941 for(i = 0; i < len; i++) {
1e262141 942 uint8_t b = cmd[i];
645c960f
MHS
943 ToSend[++ToSendMax] = encode4Bits(b & 0xF); //Least significant half
944 ToSend[++ToSendMax] = encode4Bits((b >>4) & 0xF);//Most significant half
1e262141 945 }
946
947 // Send EOF
645c960f 948 ToSend[++ToSendMax] = 0xB8;
81012e67 949 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
1e262141 950 // Convert from last byte pos to length
951 ToSendMax++;
952}
953
954// Only SOF
955static void CodeIClassTagSOF()
956{
81012e67
MHS
957 //So far a dummy implementation, not used
958 //int lastProxToAirDuration =0;
1e262141 959
81012e67 960 ToSendReset();
1e262141 961 // Send SOF
645c960f 962 ToSend[++ToSendMax] = 0x1D;
81012e67
MHS
963// lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
964
1e262141 965 // Convert from last byte pos to length
966 ToSendMax++;
967}
55eaed8f 968
9f6e9d15 969int doIClassSimulation(uint8_t csn[], int breakAfterMacReceived, uint8_t *reader_mac_buf);
ff7bb4ef
MHS
970/**
971 * @brief SimulateIClass simulates an iClass card.
972 * @param arg0 type of simulation
973 * - 0 uses the first 8 bytes in usb data as CSN
974 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
975 * in the usb data. This mode collects MAC from the reader, in order to do an offline
976 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
977 * - Other : Uses the default CSN (031fec8af7ff12e0)
978 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
979 * @param arg2
980 * @param datain
981 */
982void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain)
1e262141 983{
ff7bb4ef
MHS
984 uint32_t simType = arg0;
985 uint32_t numberOfCSNS = arg1;
7cc204bf 986 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1e262141 987
ff7bb4ef 988 // Enable and clear the trace
3000dc4e
MHS
989 set_tracing(TRUE);
990 clear_trace();
81cd0474 991
ff7bb4ef 992 uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
ff7bb4ef
MHS
993 if(simType == 0) {
994 // Use the CSN from commandline
995 memcpy(csn_crc, datain, 8);
9f6e9d15 996 doIClassSimulation(csn_crc,0,NULL);
ff7bb4ef
MHS
997 }else if(simType == 1)
998 {
9f6e9d15 999 doIClassSimulation(csn_crc,0,NULL);
ff7bb4ef
MHS
1000 }
1001 else if(simType == 2)
1002 {
9f6e9d15 1003
7b941c8d 1004 uint8_t mac_responses[USB_CMD_DATA_SIZE] = { 0 };
eabba3df 1005 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS);
ff7bb4ef
MHS
1006 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
1007 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
1008 // in order to obtain the keys, as in the "dismantling iclass"-paper.
9f6e9d15
MHS
1009 int i = 0;
1010 for( ; i < numberOfCSNS && i*8+8 < USB_CMD_DATA_SIZE; i++)
ff7bb4ef
MHS
1011 {
1012 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
1013
1014 memcpy(csn_crc, datain+(i*8), 8);
6116c796 1015 if(doIClassSimulation(csn_crc,1,mac_responses+i*8))
f83cc126 1016 {
645c960f 1017 cmd_send(CMD_ACK,CMD_SIMULATE_TAG_ICLASS,i,0,mac_responses,i*8);
f83cc126
MHS
1018 return; // Button pressed
1019 }
ff7bb4ef 1020 }
9f6e9d15
MHS
1021 cmd_send(CMD_ACK,CMD_SIMULATE_TAG_ICLASS,i,0,mac_responses,i*8);
1022
81012e67
MHS
1023 }
1024 else{
ff7bb4ef
MHS
1025 // We may want a mode here where we hardcode the csns to use (from proxclone).
1026 // That will speed things up a little, but not required just yet.
1027 Dbprintf("The mode is not implemented, reserved for future use");
1028 }
9f6e9d15 1029 Dbprintf("Done...");
ff7bb4ef
MHS
1030
1031}
1032/**
1033 * @brief Does the actual simulation
1034 * @param csn - csn to use
1035 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1036 */
9f6e9d15 1037int doIClassSimulation(uint8_t csn[], int breakAfterMacReceived, uint8_t *reader_mac_buf)
ff7bb4ef 1038{
55eaed8f 1039
1e262141 1040 // CSN followed by two CRC bytes
55eaed8f 1041 uint8_t response1[] = { 0x0F} ;
1e262141 1042 uint8_t response2[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ff7bb4ef
MHS
1043 uint8_t response3[] = { 0,0,0,0,0,0,0,0,0,0};
1044 memcpy(response3,csn,sizeof(response3));
f83cc126 1045 Dbprintf("Simulating CSN %02x%02x%02x%02x%02x%02x%02x%02x",csn[0],csn[1],csn[2],csn[3],csn[4],csn[5],csn[6],csn[7]);
1e262141 1046 // e-Purse
1047 uint8_t response4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1e262141 1048
1e262141 1049 // Construct anticollision-CSN
912a3e94 1050 rotateCSN(response3,response2);
1e262141 1051
1052 // Compute CRC on both CSNs
1053 ComputeCrc14443(CRC_ICLASS, response2, 8, &response2[8], &response2[9]);
1054 ComputeCrc14443(CRC_ICLASS, response3, 8, &response3[8], &response3[9]);
1055
ff7bb4ef 1056 int exitLoop = 0;
1e262141 1057 // Reader 0a
1058 // Tag 0f
1059 // Reader 0c
1060 // Tag anticoll. CSN
1061 // Reader 81 anticoll. CSN
1062 // Tag CSN
1063
55eaed8f
MHS
1064 uint8_t *modulated_response;
1065 int modulated_response_size;
1066 uint8_t* trace_data = NULL;
1067 int trace_data_size = 0;
1068 //uint8_t sof = 0x0f;
1e262141 1069
f71f4deb 1070 // free eventually allocated BigBuf memory
1071 BigBuf_free();
645c960f 1072 // Respond SOF -- takes 1 bytes
f71f4deb 1073 uint8_t *resp1 = BigBuf_malloc(2);
1e262141 1074 int resp1Len;
1075
1076 // Anticollision CSN (rotated CSN)
645c960f 1077 // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
f71f4deb 1078 uint8_t *resp2 = BigBuf_malloc(28);
1e262141 1079 int resp2Len;
1080
1081 // CSN
645c960f 1082 // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
f71f4deb 1083 uint8_t *resp3 = BigBuf_malloc(30);
912a3e94 1084 int resp3Len;
1e262141 1085
1086 // e-Purse
b3cc5f29
MHS
1087 // 18: Takes 2 bytes for SOF/EOF and 8 * 2 = 16 bytes (2 bytes/bit)
1088 uint8_t *resp4 = BigBuf_malloc(20);
1e262141 1089 int resp4Len;
1090
f71f4deb 1091 uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE);
6a1f2d82 1092 memset(receivedCmd, 0x44, MAX_FRAME_SIZE);
1e262141 1093 int len;
1094
1e262141 1095 // Prepare card messages
1096 ToSendMax = 0;
1097
1098 // First card answer: SOF
1099 CodeIClassTagSOF();
1100 memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax;
1101
1102 // Anticollision CSN
1103 CodeIClassTagAnswer(response2, sizeof(response2));
1104 memcpy(resp2, ToSend, ToSendMax); resp2Len = ToSendMax;
1105
1106 // CSN
1107 CodeIClassTagAnswer(response3, sizeof(response3));
912a3e94 1108 memcpy(resp3, ToSend, ToSendMax); resp3Len = ToSendMax;
1e262141 1109
1110 // e-Purse
1111 CodeIClassTagAnswer(response4, sizeof(response4));
1112 memcpy(resp4, ToSend, ToSendMax); resp4Len = ToSendMax;
1113
e3dc1e4c
MHS
1114
1115 // Start from off (no field generated)
fa541aca
MHS
1116 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1117 //SpinDelay(200);
1118 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
1119 SpinDelay(100);
1120 StartCountSspClk();
1e262141 1121 // We need to listen to the high-frequency, peak-detected path.
1122 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1123 FpgaSetupSsc();
1124
1125 // To control where we are in the protocol
1e262141 1126 int cmdsRecvd = 0;
81012e67
MHS
1127 uint32_t time_0 = GetCountSspClk();
1128 uint32_t t2r_time =0;
1129 uint32_t r2t_time =0;
912a3e94 1130
1e262141 1131 LED_A_ON();
f83cc126 1132 bool buttonPressed = false;
9f6e9d15 1133
ff7bb4ef 1134 while(!exitLoop) {
81012e67 1135
1e262141 1136 LED_B_OFF();
e3dc1e4c
MHS
1137 //Signal tracer
1138 // Can be used to get a trigger for an oscilloscope..
1139 LED_C_OFF();
3fe4ff4f 1140
1e262141 1141 if(!GetIClassCommandFromReader(receivedCmd, &len, 100)) {
f83cc126 1142 buttonPressed = true;
1e262141 1143 break;
81cd0474 1144 }
81012e67 1145 r2t_time = GetCountSspClk();
e3dc1e4c
MHS
1146 //Signal tracer
1147 LED_C_ON();
1e262141 1148
81cd0474 1149 // Okay, look at the command now.
f83cc126 1150 if(receivedCmd[0] == 0x0a ) {
1e262141 1151 // Reader in anticollission phase
55eaed8f
MHS
1152 modulated_response = resp1; modulated_response_size = resp1Len; //order = 1;
1153 trace_data = response1;
1154 trace_data_size = sizeof(response1);
1e262141 1155 } else if(receivedCmd[0] == 0x0c) {
1156 // Reader asks for anticollission CSN
55eaed8f
MHS
1157 modulated_response = resp2; modulated_response_size = resp2Len; //order = 2;
1158 trace_data = response2;
1159 trace_data_size = sizeof(response2);
1e262141 1160 //DbpString("Reader requests anticollission CSN:");
1161 } else if(receivedCmd[0] == 0x81) {
1162 // Reader selects anticollission CSN.
1163 // Tag sends the corresponding real CSN
55eaed8f
MHS
1164 modulated_response = resp3; modulated_response_size = resp3Len; //order = 3;
1165 trace_data = response3;
1166 trace_data_size = sizeof(response3);
1e262141 1167 //DbpString("Reader selects anticollission CSN:");
1168 } else if(receivedCmd[0] == 0x88) {
1169 // Read e-purse (88 02)
55eaed8f
MHS
1170 modulated_response = resp4; modulated_response_size = resp4Len; //order = 4;
1171 trace_data = response4;
1172 trace_data_size = sizeof(response4);
1e262141 1173 LED_B_ON();
1174 } else if(receivedCmd[0] == 0x05) {
1175 // Reader random and reader MAC!!!
1e262141 1176 // Do not respond
26c0d833 1177 // We do not know what to answer, so lets keep quiet
55eaed8f
MHS
1178 modulated_response = resp1; modulated_response_size = 0; //order = 5;
1179 trace_data = NULL;
1180 trace_data_size = 0;
ff7bb4ef 1181 if (breakAfterMacReceived){
ff7bb4ef 1182 // dbprintf:ing ...
6116c796
MHS
1183 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x"
1184 ,csn[0],csn[1],csn[2],csn[3],csn[4],csn[5],csn[6],csn[7]);
ff7bb4ef 1185 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len,
6116c796 1186 receivedCmd[0], receivedCmd[1], receivedCmd[2],
ff7bb4ef
MHS
1187 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1188 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
9f6e9d15
MHS
1189 if (reader_mac_buf != NULL)
1190 {
1191 memcpy(reader_mac_buf,receivedCmd+1,8);
1192 }
ff7bb4ef
MHS
1193 exitLoop = true;
1194 }
1e262141 1195 } else if(receivedCmd[0] == 0x00 && len == 1) {
1196 // Reader ends the session
55eaed8f
MHS
1197 modulated_response = resp1; modulated_response_size = 0; //order = 0;
1198 trace_data = NULL;
1199 trace_data_size = 0;
81cd0474 1200 } else {
17cba269 1201 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1e262141 1202 // Never seen this command before
1203 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1204 len,
1205 receivedCmd[0], receivedCmd[1], receivedCmd[2],
1206 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1207 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
1208 // Do not respond
55eaed8f
MHS
1209 modulated_response = resp1; modulated_response_size = 0; //order = 0;
1210 trace_data = NULL;
1211 trace_data_size = 0;
1e262141 1212 }
1213
81012e67
MHS
1214 if(cmdsRecvd > 100) {
1215 //DbpString("100 commands later...");
9f6e9d15 1216 //break;
1e262141 1217 }
1218 else {
1219 cmdsRecvd++;
1220 }
55eaed8f 1221 /**
6b038d19 1222 A legit tag has about 380us delay between reader EOT and tag SOF.
55eaed8f
MHS
1223 **/
1224 if(modulated_response_size > 0) {
645c960f 1225 SendIClassAnswer(modulated_response, modulated_response_size, 1);
81012e67 1226 t2r_time = GetCountSspClk();
81cd0474 1227 }
f83cc126 1228
81cd0474 1229 if (tracing) {
6a1f2d82 1230 uint8_t parity[MAX_PARITY_SIZE];
1231 GetParity(receivedCmd, len, parity);
1232 LogTrace(receivedCmd,len, (r2t_time-time_0)<< 4, (r2t_time-time_0) << 4, parity, TRUE);
17cba269 1233
55eaed8f
MHS
1234 if (trace_data != NULL) {
1235 GetParity(trace_data, trace_data_size, parity);
1236 LogTrace(trace_data, trace_data_size, (t2r_time-time_0) << 4, (t2r_time-time_0) << 4, parity, FALSE);
17cba269 1237 }
81012e67
MHS
1238 if(!tracing) {
1239 DbpString("Trace full");
1240 //break;
1241 }
1242
81cd0474 1243 }
6a1f2d82 1244 memset(receivedCmd, 0x44, MAX_FRAME_SIZE);
81cd0474 1245 }
1e262141 1246
9f6e9d15 1247 //Dbprintf("%x", cmdsRecvd);
1e262141 1248 LED_A_OFF();
1249 LED_B_OFF();
7b941c8d
MHS
1250 LED_C_OFF();
1251
f83cc126
MHS
1252 if(buttonPressed)
1253 {
1254 DbpString("Button pressed");
1255 }
f83cc126 1256 return buttonPressed;
1e262141 1257}
1258
1259static int SendIClassAnswer(uint8_t *resp, int respLen, int delay)
1260{
e3dc1e4c 1261 int i = 0, d=0;//, u = 0, d = 0;
1e262141 1262 uint8_t b = 0;
e3dc1e4c 1263
645c960f
MHS
1264 //FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR|FPGA_HF_SIMULATOR_MODULATE_424K);
1265 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR|FPGA_HF_SIMULATOR_MODULATE_424K_8BIT);
e3dc1e4c 1266
1e262141 1267 AT91C_BASE_SSC->SSC_THR = 0x00;
1268 FpgaSetupSsc();
e3dc1e4c
MHS
1269 while(!BUTTON_PRESS()) {
1270 if((AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)){
1271 b = AT91C_BASE_SSC->SSC_RHR; (void) b;
1e262141 1272 }
e3dc1e4c
MHS
1273 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)){
1274 b = 0x00;
1e262141 1275 if(d < delay) {
1e262141 1276 d++;
1277 }
e3dc1e4c
MHS
1278 else {
1279 if( i < respLen){
1280 b = resp[i];
1281 //Hack
1282 //b = 0xAC;
1283 }
1284 i++;
1e262141 1285 }
1286 AT91C_BASE_SSC->SSC_THR = b;
1e262141 1287 }
e3dc1e4c 1288
645c960f
MHS
1289// if (i > respLen +4) break;
1290 if (i > respLen +1) break;
1e262141 1291 }
1292
1293 return 0;
1294}
1295
1296/// THE READER CODE
1297
1298//-----------------------------------------------------------------------------
1299// Transmit the command (to the tag) that was placed in ToSend[].
1300//-----------------------------------------------------------------------------
1301static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait)
1302{
1303 int c;
1e262141 1304 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1305 AT91C_BASE_SSC->SSC_THR = 0x00;
1306 FpgaSetupSsc();
1307
1308 if (wait)
2ed270a8
MHS
1309 {
1310 if(*wait < 10) *wait = 10;
1311
1312 for(c = 0; c < *wait;) {
1313 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1314 AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
1315 c++;
1316 }
1317 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1318 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1319 (void)r;
1320 }
1321 WDT_HIT();
1322 }
1323
1324 }
1e262141 1325
1e262141 1326
1327 uint8_t sendbyte;
1328 bool firstpart = TRUE;
1329 c = 0;
1330 for(;;) {
1331 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1332
1333 // DOUBLE THE SAMPLES!
1334 if(firstpart) {
1335 sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4);
1336 }
1337 else {
1338 sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4);
1339 c++;
1340 }
1341 if(sendbyte == 0xff) {
1342 sendbyte = 0xfe;
1343 }
1344 AT91C_BASE_SSC->SSC_THR = sendbyte;
1345 firstpart = !firstpart;
1346
1347 if(c >= len) {
1348 break;
1349 }
1350 }
1351 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1352 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1353 (void)r;
1354 }
1355 WDT_HIT();
1356 }
1357 if (samples) *samples = (c + *wait) << 3;
1358}
1359
1360
1361//-----------------------------------------------------------------------------
1362// Prepare iClass reader command to send to FPGA
1363//-----------------------------------------------------------------------------
1364void CodeIClassCommand(const uint8_t * cmd, int len)
1365{
1366 int i, j, k;
1367 uint8_t b;
1368
1369 ToSendReset();
1370
1371 // Start of Communication: 1 out of 4
1372 ToSend[++ToSendMax] = 0xf0;
1373 ToSend[++ToSendMax] = 0x00;
1374 ToSend[++ToSendMax] = 0x0f;
1375 ToSend[++ToSendMax] = 0x00;
1376
1377 // Modulate the bytes
1378 for (i = 0; i < len; i++) {
1379 b = cmd[i];
1380 for(j = 0; j < 4; j++) {
1381 for(k = 0; k < 4; k++) {
e3dc1e4c
MHS
1382 if(k == (b & 3)) {
1383 ToSend[++ToSendMax] = 0x0f;
1384 }
1385 else {
1386 ToSend[++ToSendMax] = 0x00;
1387 }
1e262141 1388 }
1389 b >>= 2;
1390 }
1391 }
1392
1393 // End of Communication
1394 ToSend[++ToSendMax] = 0x00;
1395 ToSend[++ToSendMax] = 0x00;
1396 ToSend[++ToSendMax] = 0xf0;
1397 ToSend[++ToSendMax] = 0x00;
1398
1399 // Convert from last character reference to length
1400 ToSendMax++;
1401}
1402
1403void ReaderTransmitIClass(uint8_t* frame, int len)
1404{
6a1f2d82 1405 int wait = 0;
1406 int samples = 0;
1407
1408 // This is tied to other size changes
6a1f2d82 1409 CodeIClassCommand(frame,len);
1410
1411 // Select the card
1412 TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait);
1413 if(trigger)
1414 LED_A_ON();
1415
1416 // Store reader command in buffer
1417 if (tracing) {
1418 uint8_t par[MAX_PARITY_SIZE];
1419 GetParity(frame, len, par);
1420 LogTrace(frame, len, rsamples, rsamples, par, TRUE);
1421 }
1e262141 1422}
1423
1424//-----------------------------------------------------------------------------
1425// Wait a certain time for tag response
1426// If a response is captured return TRUE
1427// If it takes too long return FALSE
1428//-----------------------------------------------------------------------------
1429static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) //uint8_t *buffer
1430{
1431 // buffer needs to be 512 bytes
1432 int c;
1433
1434 // Set FPGA mode to "reader listen mode", no modulation (listen
1435 // only, since we are receiving, not transmitting).
1436 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN);
1437
1438 // Now get the answer from the card
1439 Demod.output = receivedResponse;
1440 Demod.len = 0;
1441 Demod.state = DEMOD_UNSYNCD;
1442
1443 uint8_t b;
1444 if (elapsed) *elapsed = 0;
1445
1446 bool skip = FALSE;
1447
1448 c = 0;
1449 for(;;) {
1450 WDT_HIT();
1451
1452 if(BUTTON_PRESS()) return FALSE;
1453
1454 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1455 AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!!
1456 if (elapsed) (*elapsed)++;
1457 }
1458 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1459 if(c < timeout) { c++; } else { return FALSE; }
1460 b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1461 skip = !skip;
1462 if(skip) continue;
3fe4ff4f 1463
1e262141 1464 if(ManchesterDecoding(b & 0x0f)) {
1465 *samples = c << 3;
1466 return TRUE;
1467 }
1468 }
1469 }
1470}
1471
1472int ReaderReceiveIClass(uint8_t* receivedAnswer)
1473{
1474 int samples = 0;
1475 if (!GetIClassAnswer(receivedAnswer,160,&samples,0)) return FALSE;
7bc95e2e 1476 rsamples += samples;
6a1f2d82 1477 if (tracing) {
1478 uint8_t parity[MAX_PARITY_SIZE];
1479 GetParity(receivedAnswer, Demod.len, parity);
1480 LogTrace(receivedAnswer,Demod.len,rsamples,rsamples,parity,FALSE);
1481 }
1e262141 1482 if(samples == 0) return FALSE;
1483 return Demod.len;
1484}
1485
aa41c605
MHS
1486void setupIclassReader()
1487{
1488 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
1489 // Reset trace buffer
3000dc4e
MHS
1490 set_tracing(TRUE);
1491 clear_trace();
aa41c605
MHS
1492
1493 // Setup SSC
1494 FpgaSetupSsc();
1495 // Start from off (no field generated)
1496 // Signal field is off with the appropriate LED
1497 LED_D_OFF();
1498 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1499 SpinDelay(200);
1500
1501 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1502
1503 // Now give it time to spin up.
1504 // Signal field is on with the appropriate LED
1505 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1506 SpinDelay(200);
1507 LED_A_ON();
1508
1509}
1510
c8dd9b09
MHS
1511size_t sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, uint8_t expected_size, uint8_t retries)
1512{
1513 while(retries-- > 0)
1514 {
1515 ReaderTransmitIClass(command, cmdsize);
1516 if(expected_size == ReaderReceiveIClass(resp)){
1517 return 0;
1518 }
1519 }
1520 return 1;//Error
1521}
1522
1523/**
1524 * @brief Talks to an iclass tag, sends the commands to get CSN and CC.
1525 * @param card_data where the CSN and CC are stored for return
1526 * @return 0 = fail
1527 * 1 = Got CSN
1528 * 2 = Got CSN and CC
1529 */
1530uint8_t handshakeIclassTag(uint8_t *card_data)
1531{
1532 static uint8_t act_all[] = { 0x0a };
1533 static uint8_t identify[] = { 0x0c };
1534 static uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1535 static uint8_t readcheck_cc[]= { 0x88, 0x02 };
f71f4deb 1536 uint8_t resp[ICLASS_BUFFER_SIZE];
c8dd9b09
MHS
1537
1538 uint8_t read_status = 0;
1539
1540 // Send act_all
1541 ReaderTransmitIClass(act_all, 1);
1542 // Card present?
1543 if(!ReaderReceiveIClass(resp)) return read_status;//Fail
1544 //Send Identify
1545 ReaderTransmitIClass(identify, 1);
1546 //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC
1547 uint8_t len = ReaderReceiveIClass(resp);
1548 if(len != 10) return read_status;//Fail
1549
1550 //Copy the Anti-collision CSN to our select-packet
1551 memcpy(&select[1],resp,8);
1552 //Select the card
1553 ReaderTransmitIClass(select, sizeof(select));
1554 //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC
1555 len = ReaderReceiveIClass(resp);
1556 if(len != 10) return read_status;//Fail
1557
1558 //Success - level 1, we got CSN
1559 //Save CSN in response data
1560 memcpy(card_data,resp,8);
1561
1562 //Flag that we got to at least stage 1, read CSN
1563 read_status = 1;
1564
1565 // Card selected, now read e-purse (cc)
1566 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1567 if(ReaderReceiveIClass(resp) == 8) {
1568 //Save CC (e-purse) in response data
1569 memcpy(card_data+8,resp,8);
1570
1571 //Got both
1572 read_status = 2;
1573 }
1574
1575 return read_status;
1576}
1577
1e262141 1578// Reader iClass Anticollission
1579void ReaderIClass(uint8_t arg0) {
1e262141 1580
aa41c605
MHS
1581 uint8_t card_data[24]={0};
1582 uint8_t last_csn[8]={0};
6a1f2d82 1583
aa41c605
MHS
1584 int read_status= 0;
1585 bool abort_after_read = arg0 & FLAG_ICLASS_READER_ONLY_ONCE;
c8dd9b09 1586 bool get_cc = arg0 & FLAG_ICLASS_READER_GET_CC;
3000dc4e 1587 set_tracing(TRUE);
aa41c605 1588 setupIclassReader();
1e262141 1589
aa41c605
MHS
1590 size_t datasize = 0;
1591 while(!BUTTON_PRESS())
1592 {
1e262141 1593
3000dc4e 1594 if(!tracing) {
c8dd9b09
MHS
1595 DbpString("Trace full");
1596 break;
1597 }
1598 WDT_HIT();
4ab4336a 1599
c8dd9b09 1600 read_status = handshakeIclassTag(card_data);
2e9d4b3f 1601
c8dd9b09
MHS
1602 if(read_status == 0) continue;
1603 if(read_status == 1) datasize = 8;
1604 if(read_status == 2) datasize = 16;
1605
1606 LED_B_ON();
1607 //Send back to client, but don't bother if we already sent this
1608 if(memcmp(last_csn, card_data, 8) != 0)
1609 {
2e9d4b3f 1610
c8dd9b09
MHS
1611 if(!get_cc || (get_cc && read_status == 2))
1612 {
1613 cmd_send(CMD_ACK,read_status,0,0,card_data,datasize);
1614 if(abort_after_read) {
1615 LED_A_OFF();
1616 return;
1617 }
1618 //Save that we already sent this....
1619 memcpy(last_csn, card_data, 8);
1620 }
1621 //If 'get_cc' was specified and we didn't get a CC, we'll just keep trying...
1622 }
1623 LED_B_OFF();
1624 }
1625 cmd_send(CMD_ACK,0,0,0,card_data, 0);
aa41c605 1626 LED_A_OFF();
cee5a30d 1627}
1628
c3963755 1629void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
c8dd9b09 1630
cb29e00a 1631 uint8_t card_data[USB_CMD_DATA_SIZE]={0};
39d3ce5d
MHS
1632 uint16_t block_crc_LUT[255] = {0};
1633
1634 {//Generate a lookup table for block crc
1635 for(int block = 0; block < 255; block++){
1636 char bl = block;
1637 block_crc_LUT[block] = iclass_crc16(&bl ,1);
1638 }
1639 }
1640 //Dbprintf("Lookup table: %02x %02x %02x" ,block_crc_LUT[0],block_crc_LUT[1],block_crc_LUT[2]);
c8dd9b09 1641
c3963755 1642 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1643 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1644
fecd8202 1645 uint16_t crc = 0;
c3963755 1646 uint8_t cardsize=0;
c3963755 1647 uint8_t mem=0;
1648
1649 static struct memory_t{
1650 int k16;
1651 int book;
1652 int k2;
1653 int lockauth;
1654 int keyaccess;
1655 } memory;
1656
f71f4deb 1657 uint8_t resp[ICLASS_BUFFER_SIZE];
6a1f2d82 1658
9b82de75 1659 setupIclassReader();
3000dc4e 1660 set_tracing(TRUE);
c3963755 1661
c8dd9b09 1662 while(!BUTTON_PRESS()) {
c3963755 1663
39d3ce5d
MHS
1664 WDT_HIT();
1665
3000dc4e 1666 if(!tracing) {
c3963755 1667 DbpString("Trace full");
1668 break;
1669 }
1670
c8dd9b09
MHS
1671 uint8_t read_status = handshakeIclassTag(card_data);
1672 if(read_status < 2) continue;
1673
1674 //for now replay captured auth (as cc not updated)
1675 memcpy(check+5,MAC,4);
1676
1677 if(sendCmdGetResponseWithRetries(check, sizeof(check),resp, 4, 5))
1678 {
1679 Dbprintf("Error: Authentication Fail!");
1680 continue;
1681 }
1682
39d3ce5d
MHS
1683 //first get configuration block (block 1)
1684 crc = block_crc_LUT[1];
c8dd9b09 1685 read[1]=1;
c8dd9b09
MHS
1686 read[2] = crc >> 8;
1687 read[3] = crc & 0xff;
1688
1689 if(sendCmdGetResponseWithRetries(read, sizeof(read),resp, 10, 10))
1690 {
39d3ce5d 1691 Dbprintf("Dump config (block 1) failed");
c8dd9b09
MHS
1692 continue;
1693 }
1694
1695 mem=resp[5];
1696 memory.k16= (mem & 0x80);
1697 memory.book= (mem & 0x20);
1698 memory.k2= (mem & 0x8);
1699 memory.lockauth= (mem & 0x2);
1700 memory.keyaccess= (mem & 0x1);
1701
1702 cardsize = memory.k16 ? 255 : 32;
1703 WDT_HIT();
cb29e00a
MHS
1704 //Set card_data to all zeroes, we'll fill it with data
1705 memset(card_data,0x0,USB_CMD_DATA_SIZE);
1706 uint8_t failedRead =0;
428d6221 1707 uint32_t stored_data_length =0;
c8dd9b09 1708 //then loop around remaining blocks
39d3ce5d 1709 for(int block=0; block < cardsize; block++){
c8dd9b09
MHS
1710
1711 read[1]= block;
39d3ce5d 1712 crc = block_crc_LUT[block];
c8dd9b09
MHS
1713 read[2] = crc >> 8;
1714 read[3] = crc & 0xff;
1715
1716 if(!sendCmdGetResponseWithRetries(read, sizeof(read), resp, 10, 10))
1717 {
1718 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1719 block, resp[0], resp[1], resp[2],
1720 resp[3], resp[4], resp[5],
1721 resp[6], resp[7]);
1722
cb29e00a
MHS
1723 //Fill up the buffer
1724 memcpy(card_data+stored_data_length,resp,8);
1725 stored_data_length += 8;
cb29e00a
MHS
1726 if(stored_data_length +8 > USB_CMD_DATA_SIZE)
1727 {//Time to send this off and start afresh
1728 cmd_send(CMD_ACK,
1729 stored_data_length,//data length
1730 failedRead,//Failed blocks?
1731 0,//Not used ATM
1732 card_data, stored_data_length);
1733 //reset
1734 stored_data_length = 0;
1735 failedRead = 0;
1736 }
1737
c8dd9b09 1738 }else{
cb29e00a
MHS
1739 failedRead = 1;
1740 stored_data_length +=8;//Otherwise, data becomes misaligned
c8dd9b09 1741 Dbprintf("Failed to dump block %d", block);
c3963755 1742 }
1743 }
428d6221 1744
cb29e00a
MHS
1745 //Send off any remaining data
1746 if(stored_data_length > 0)
1747 {
1748 cmd_send(CMD_ACK,
1749 stored_data_length,//data length
1750 failedRead,//Failed blocks?
1751 0,//Not used ATM
1752 card_data, stored_data_length);
1753 }
c8dd9b09
MHS
1754 //If we got here, let's break
1755 break;
c3963755 1756 }
cb29e00a
MHS
1757 //Signal end of transmission
1758 cmd_send(CMD_ACK,
1759 0,//data length
1760 0,//Failed blocks?
1761 0,//Not used ATM
1762 card_data, 0);
1763
c3963755 1764 LED_A_OFF();
1765}
1766
fecd8202 1767//2. Create Read method (cut-down from above) based off responses from 1.
1768// Since we have the MAC could continue to use replay function.
1769//3. Create Write method
1770/*
1771void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1772 uint8_t act_all[] = { 0x0a };
1773 uint8_t identify[] = { 0x0c };
1774 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1775 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1776 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1777 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1778 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1779
1780 uint16_t crc = 0;
1781
6a1f2d82 1782 uint8_t* resp = (((uint8_t *)BigBuf) + 3560);
912a3e94 1783
fecd8202 1784 // Reset trace buffer
1785 memset(trace, 0x44, RECV_CMD_OFFSET);
1786 traceLen = 0;
1787
1788 // Setup SSC
1789 FpgaSetupSsc();
1790 // Start from off (no field generated)
1791 // Signal field is off with the appropriate LED
1792 LED_D_OFF();
1793 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1794 SpinDelay(200);
1795
1796 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1797
1798 // Now give it time to spin up.
1799 // Signal field is on with the appropriate LED
1800 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1801 SpinDelay(200);
1802
1803 LED_A_ON();
1804
1805 for(int i=0;i<1;i++) {
1806
1807 if(traceLen > TRACE_SIZE) {
1808 DbpString("Trace full");
1809 break;
1810 }
1811
1812 if (BUTTON_PRESS()) break;
1813
1814 // Send act_all
1815 ReaderTransmitIClass(act_all, 1);
1816 // Card present?
1817 if(ReaderReceiveIClass(resp)) {
1818 ReaderTransmitIClass(identify, 1);
1819 if(ReaderReceiveIClass(resp) == 10) {
1820 // Select card
1821 memcpy(&select[1],resp,8);
1822 ReaderTransmitIClass(select, sizeof(select));
1823
1824 if(ReaderReceiveIClass(resp) == 10) {
1825 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1826 resp[0], resp[1], resp[2],
1827 resp[3], resp[4], resp[5],
1828 resp[6], resp[7]);
1829 }
1830 // Card selected
1831 Dbprintf("Readcheck on Sector 2");
1832 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1833 if(ReaderReceiveIClass(resp) == 8) {
1834 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1835 resp[0], resp[1], resp[2],
1836 resp[3], resp[4], resp[5],
1837 resp[6], resp[7]);
1838 }else return;
1839 Dbprintf("Authenticate");
1840 //for now replay captured auth (as cc not updated)
1841 memcpy(check+5,MAC,4);
1842 Dbprintf(" AA: %02x %02x %02x %02x",
1843 check[5], check[6], check[7],check[8]);
1844 ReaderTransmitIClass(check, sizeof(check));
1845 if(ReaderReceiveIClass(resp) == 4) {
1846 Dbprintf(" AR: %02x %02x %02x %02x",
1847 resp[0], resp[1], resp[2],resp[3]);
1848 }else {
1849 Dbprintf("Error: Authentication Fail!");
1850 return;
1851 }
1852 Dbprintf("Write Block");
1853
1854 //read configuration for max block number
1855 read_success=false;
1856 read[1]=1;
1857 uint8_t *blockno=&read[1];
1858 crc = iclass_crc16((char *)blockno,1);
1859 read[2] = crc >> 8;
1860 read[3] = crc & 0xff;
1861 while(!read_success){
1862 ReaderTransmitIClass(read, sizeof(read));
1863 if(ReaderReceiveIClass(resp) == 10) {
1864 read_success=true;
1865 mem=resp[5];
1866 memory.k16= (mem & 0x80);
1867 memory.book= (mem & 0x20);
1868 memory.k2= (mem & 0x8);
1869 memory.lockauth= (mem & 0x2);
1870 memory.keyaccess= (mem & 0x1);
1871
1872 }
1873 }
1874 if (memory.k16){
1875 cardsize=255;
1876 }else cardsize=32;
1877 //check card_size
1878
1879 memcpy(write+1,blockNo,1);
1880 memcpy(write+2,data,8);
1881 memcpy(write+10,mac,4);
1882 while(!send_success){
1883 ReaderTransmitIClass(write, sizeof(write));
1884 if(ReaderReceiveIClass(resp) == 10) {
1885 write_success=true;
1886 }
1887 }//
1888 }
1889 WDT_HIT();
1890 }
1891
1892 LED_A_OFF();
1893}*/
Impressum, Datenschutz