]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iclass.c
Implement replay command.
[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"
1e262141 44// Needed for CRC in emulation mode;
45// same construction as in ISO 14443;
46// different initial value (CRC_ICLASS)
47#include "iso14443crc.h"
c3963755 48#include "iso15693tools.h"
cee5a30d 49
1e262141 50static int timeout = 4096;
cee5a30d 51
52// CARD TO READER
53// Sequence D: 11110000 modulation with subcarrier during first half
54// Sequence E: 00001111 modulation with subcarrier during second half
55// Sequence F: 00000000 no modulation with subcarrier
56// READER TO CARD
57// Sequence X: 00001100 drop after half a period
58// Sequence Y: 00000000 no drop
59// Sequence Z: 11000000 drop at start
cee5a30d 60#define SEC_X 0x0c
61#define SEC_Y 0x00
62#define SEC_Z 0xc0
63
1e262141 64static int SendIClassAnswer(uint8_t *resp, int respLen, int delay);
cee5a30d 65
66//-----------------------------------------------------------------------------
67// The software UART that receives commands from the reader, and its state
68// variables.
69//-----------------------------------------------------------------------------
70static struct {
71 enum {
72 STATE_UNSYNCD,
73 STATE_START_OF_COMMUNICATION,
74 STATE_RECEIVING
75 } state;
76 uint16_t shiftReg;
77 int bitCnt;
78 int byteCnt;
79 int byteCntMax;
80 int posCnt;
81 int nOutOfCnt;
82 int OutOfCnt;
83 int syncBit;
1e262141 84 int parityBits;
85 int samples;
cee5a30d 86 int highCnt;
87 int swapper;
88 int counter;
89 int bitBuffer;
90 int dropPosition;
91 uint8_t *output;
92} Uart;
93
1e262141 94static RAMFUNC int OutOfNDecoding(int bit)
cee5a30d 95{
9f693930 96 //int error = 0;
cee5a30d 97 int bitright;
98
99 if(!Uart.bitBuffer) {
100 Uart.bitBuffer = bit ^ 0xFF0;
101 return FALSE;
102 }
103 else {
104 Uart.bitBuffer <<= 4;
105 Uart.bitBuffer ^= bit;
106 }
107
108 /*if(Uart.swapper) {
109 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
110 Uart.byteCnt++;
111 Uart.swapper = 0;
112 if(Uart.byteCnt > 15) { return TRUE; }
113 }
114 else {
115 Uart.swapper = 1;
116 }*/
117
118 if(Uart.state != STATE_UNSYNCD) {
119 Uart.posCnt++;
120
121 if((Uart.bitBuffer & Uart.syncBit) ^ Uart.syncBit) {
122 bit = 0x00;
123 }
124 else {
125 bit = 0x01;
126 }
127 if(((Uart.bitBuffer << 1) & Uart.syncBit) ^ Uart.syncBit) {
128 bitright = 0x00;
129 }
130 else {
131 bitright = 0x01;
132 }
133 if(bit != bitright) { bit = bitright; }
134
135
136 // So, now we only have to deal with *bit*, lets see...
137 if(Uart.posCnt == 1) {
138 // measurement first half bitperiod
139 if(!bit) {
140 // Drop in first half means that we are either seeing
141 // an SOF or an EOF.
142
143 if(Uart.nOutOfCnt == 1) {
144 // End of Communication
145 Uart.state = STATE_UNSYNCD;
146 Uart.highCnt = 0;
147 if(Uart.byteCnt == 0) {
148 // Its not straightforward to show single EOFs
149 // So just leave it and do not return TRUE
150 Uart.output[Uart.byteCnt] = 0xf0;
151 Uart.byteCnt++;
152
153 // Calculate the parity bit for the client...
154 Uart.parityBits = 1;
155 }
156 else {
157 return TRUE;
158 }
159 }
160 else if(Uart.state != STATE_START_OF_COMMUNICATION) {
161 // When not part of SOF or EOF, it is an error
162 Uart.state = STATE_UNSYNCD;
163 Uart.highCnt = 0;
9f693930 164 //error = 4;
cee5a30d 165 }
166 }
167 }
168 else {
169 // measurement second half bitperiod
170 // Count the bitslot we are in... (ISO 15693)
171 Uart.nOutOfCnt++;
172
173 if(!bit) {
174 if(Uart.dropPosition) {
175 if(Uart.state == STATE_START_OF_COMMUNICATION) {
9f693930 176 //error = 1;
cee5a30d 177 }
178 else {
9f693930 179 //error = 7;
cee5a30d 180 }
181 // It is an error if we already have seen a drop in current frame
182 Uart.state = STATE_UNSYNCD;
183 Uart.highCnt = 0;
184 }
185 else {
186 Uart.dropPosition = Uart.nOutOfCnt;
187 }
188 }
189
190 Uart.posCnt = 0;
191
192
193 if(Uart.nOutOfCnt == Uart.OutOfCnt && Uart.OutOfCnt == 4) {
194 Uart.nOutOfCnt = 0;
195
196 if(Uart.state == STATE_START_OF_COMMUNICATION) {
197 if(Uart.dropPosition == 4) {
198 Uart.state = STATE_RECEIVING;
199 Uart.OutOfCnt = 256;
200 }
201 else if(Uart.dropPosition == 3) {
202 Uart.state = STATE_RECEIVING;
203 Uart.OutOfCnt = 4;
204 //Uart.output[Uart.byteCnt] = 0xdd;
205 //Uart.byteCnt++;
206 }
207 else {
208 Uart.state = STATE_UNSYNCD;
209 Uart.highCnt = 0;
210 }
211 Uart.dropPosition = 0;
212 }
213 else {
214 // RECEIVING DATA
215 // 1 out of 4
216 if(!Uart.dropPosition) {
217 Uart.state = STATE_UNSYNCD;
218 Uart.highCnt = 0;
9f693930 219 //error = 9;
cee5a30d 220 }
221 else {
222 Uart.shiftReg >>= 2;
223
224 // Swap bit order
225 Uart.dropPosition--;
226 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
227 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
228
229 Uart.shiftReg ^= ((Uart.dropPosition & 0x03) << 6);
230 Uart.bitCnt += 2;
231 Uart.dropPosition = 0;
232
233 if(Uart.bitCnt == 8) {
234 Uart.output[Uart.byteCnt] = (Uart.shiftReg & 0xff);
235 Uart.byteCnt++;
236
237 // Calculate the parity bit for the client...
238 Uart.parityBits <<= 1;
239 Uart.parityBits ^= OddByteParity[(Uart.shiftReg & 0xff)];
240
241 Uart.bitCnt = 0;
242 Uart.shiftReg = 0;
243 }
244 }
245 }
246 }
247 else if(Uart.nOutOfCnt == Uart.OutOfCnt) {
248 // RECEIVING DATA
249 // 1 out of 256
250 if(!Uart.dropPosition) {
251 Uart.state = STATE_UNSYNCD;
252 Uart.highCnt = 0;
9f693930 253 //error = 3;
cee5a30d 254 }
255 else {
256 Uart.dropPosition--;
257 Uart.output[Uart.byteCnt] = (Uart.dropPosition & 0xff);
258 Uart.byteCnt++;
259
260 // Calculate the parity bit for the client...
261 Uart.parityBits <<= 1;
262 Uart.parityBits ^= OddByteParity[(Uart.dropPosition & 0xff)];
263
264 Uart.bitCnt = 0;
265 Uart.shiftReg = 0;
266 Uart.nOutOfCnt = 0;
267 Uart.dropPosition = 0;
268 }
269 }
270
271 /*if(error) {
272 Uart.output[Uart.byteCnt] = 0xAA;
273 Uart.byteCnt++;
274 Uart.output[Uart.byteCnt] = error & 0xFF;
275 Uart.byteCnt++;
276 Uart.output[Uart.byteCnt] = 0xAA;
277 Uart.byteCnt++;
278 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
279 Uart.byteCnt++;
280 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
281 Uart.byteCnt++;
282 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
283 Uart.byteCnt++;
284 Uart.output[Uart.byteCnt] = 0xAA;
285 Uart.byteCnt++;
286 return TRUE;
287 }*/
288 }
289
290 }
291 else {
292 bit = Uart.bitBuffer & 0xf0;
293 bit >>= 4;
294 bit ^= 0x0F; // drops become 1s ;-)
295 if(bit) {
296 // should have been high or at least (4 * 128) / fc
297 // according to ISO this should be at least (9 * 128 + 20) / fc
298 if(Uart.highCnt == 8) {
299 // we went low, so this could be start of communication
300 // it turns out to be safer to choose a less significant
301 // syncbit... so we check whether the neighbour also represents the drop
302 Uart.posCnt = 1; // apparently we are busy with our first half bit period
303 Uart.syncBit = bit & 8;
304 Uart.samples = 3;
305 if(!Uart.syncBit) { Uart.syncBit = bit & 4; Uart.samples = 2; }
306 else if(bit & 4) { Uart.syncBit = bit & 4; Uart.samples = 2; bit <<= 2; }
307 if(!Uart.syncBit) { Uart.syncBit = bit & 2; Uart.samples = 1; }
308 else if(bit & 2) { Uart.syncBit = bit & 2; Uart.samples = 1; bit <<= 1; }
309 if(!Uart.syncBit) { Uart.syncBit = bit & 1; Uart.samples = 0;
310 if(Uart.syncBit && (Uart.bitBuffer & 8)) {
311 Uart.syncBit = 8;
312
313 // the first half bit period is expected in next sample
314 Uart.posCnt = 0;
315 Uart.samples = 3;
316 }
317 }
318 else if(bit & 1) { Uart.syncBit = bit & 1; Uart.samples = 0; }
319
320 Uart.syncBit <<= 4;
321 Uart.state = STATE_START_OF_COMMUNICATION;
322 Uart.bitCnt = 0;
323 Uart.byteCnt = 0;
324 Uart.parityBits = 0;
325 Uart.nOutOfCnt = 0;
326 Uart.OutOfCnt = 4; // Start at 1/4, could switch to 1/256
327 Uart.dropPosition = 0;
328 Uart.shiftReg = 0;
9f693930 329 //error = 0;
cee5a30d 330 }
331 else {
332 Uart.highCnt = 0;
333 }
334 }
335 else {
336 if(Uart.highCnt < 8) {
337 Uart.highCnt++;
338 }
339 }
340 }
341
342 return FALSE;
343}
344
345//=============================================================================
1e262141 346// Manchester
cee5a30d 347//=============================================================================
348
349static struct {
350 enum {
351 DEMOD_UNSYNCD,
352 DEMOD_START_OF_COMMUNICATION,
353 DEMOD_START_OF_COMMUNICATION2,
354 DEMOD_START_OF_COMMUNICATION3,
355 DEMOD_SOF_COMPLETE,
356 DEMOD_MANCHESTER_D,
357 DEMOD_MANCHESTER_E,
358 DEMOD_END_OF_COMMUNICATION,
359 DEMOD_END_OF_COMMUNICATION2,
360 DEMOD_MANCHESTER_F,
361 DEMOD_ERROR_WAIT
362 } state;
363 int bitCount;
364 int posCount;
365 int syncBit;
366 int parityBits;
367 uint16_t shiftReg;
368 int buffer;
369 int buffer2;
370 int buffer3;
371 int buff;
372 int samples;
373 int len;
374 enum {
375 SUB_NONE,
376 SUB_FIRST_HALF,
377 SUB_SECOND_HALF,
378 SUB_BOTH
379 } sub;
380 uint8_t *output;
381} Demod;
382
383static RAMFUNC int ManchesterDecoding(int v)
384{
385 int bit;
386 int modulation;
387 int error = 0;
388
389 bit = Demod.buffer;
390 Demod.buffer = Demod.buffer2;
391 Demod.buffer2 = Demod.buffer3;
392 Demod.buffer3 = v;
393
394 if(Demod.buff < 3) {
395 Demod.buff++;
396 return FALSE;
397 }
398
399 if(Demod.state==DEMOD_UNSYNCD) {
400 Demod.output[Demod.len] = 0xfa;
401 Demod.syncBit = 0;
402 //Demod.samples = 0;
403 Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part
cee5a30d 404
405 if(bit & 0x08) {
406 Demod.syncBit = 0x08;
407 }
408
409 if(bit & 0x04) {
410 if(Demod.syncBit) {
411 bit <<= 4;
412 }
413 Demod.syncBit = 0x04;
414 }
415
416 if(bit & 0x02) {
417 if(Demod.syncBit) {
418 bit <<= 2;
419 }
420 Demod.syncBit = 0x02;
421 }
422
423 if(bit & 0x01 && Demod.syncBit) {
424 Demod.syncBit = 0x01;
425 }
426
427 if(Demod.syncBit) {
428 Demod.len = 0;
429 Demod.state = DEMOD_START_OF_COMMUNICATION;
430 Demod.sub = SUB_FIRST_HALF;
431 Demod.bitCount = 0;
432 Demod.shiftReg = 0;
433 Demod.parityBits = 0;
434 Demod.samples = 0;
435 if(Demod.posCount) {
436 //if(trigger) LED_A_OFF(); // Not useful in this case...
437 switch(Demod.syncBit) {
438 case 0x08: Demod.samples = 3; break;
439 case 0x04: Demod.samples = 2; break;
440 case 0x02: Demod.samples = 1; break;
441 case 0x01: Demod.samples = 0; break;
442 }
443 // SOF must be long burst... otherwise stay unsynced!!!
444 if(!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) {
445 Demod.state = DEMOD_UNSYNCD;
446 }
447 }
448 else {
449 // SOF must be long burst... otherwise stay unsynced!!!
450 if(!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) {
451 Demod.state = DEMOD_UNSYNCD;
452 error = 0x88;
453 }
454
455 }
456 error = 0;
457
458 }
459 }
460 else {
461 modulation = bit & Demod.syncBit;
462 modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
463 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
464
465 Demod.samples += 4;
466
467 if(Demod.posCount==0) {
468 Demod.posCount = 1;
469 if(modulation) {
470 Demod.sub = SUB_FIRST_HALF;
471 }
472 else {
473 Demod.sub = SUB_NONE;
474 }
475 }
476 else {
477 Demod.posCount = 0;
478 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
479 if(Demod.state!=DEMOD_ERROR_WAIT) {
480 Demod.state = DEMOD_ERROR_WAIT;
481 Demod.output[Demod.len] = 0xaa;
482 error = 0x01;
483 }
484 }*/
485 //else if(modulation) {
486 if(modulation) {
487 if(Demod.sub == SUB_FIRST_HALF) {
488 Demod.sub = SUB_BOTH;
489 }
490 else {
491 Demod.sub = SUB_SECOND_HALF;
492 }
493 }
494 else if(Demod.sub == SUB_NONE) {
495 if(Demod.state == DEMOD_SOF_COMPLETE) {
496 Demod.output[Demod.len] = 0x0f;
497 Demod.len++;
498 Demod.parityBits <<= 1;
499 Demod.parityBits ^= OddByteParity[0x0f];
500 Demod.state = DEMOD_UNSYNCD;
501// error = 0x0f;
502 return TRUE;
503 }
504 else {
505 Demod.state = DEMOD_ERROR_WAIT;
506 error = 0x33;
507 }
508 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
509 Demod.state = DEMOD_ERROR_WAIT;
510 Demod.output[Demod.len] = 0xaa;
511 error = 0x01;
512 }*/
513 }
514
515 switch(Demod.state) {
516 case DEMOD_START_OF_COMMUNICATION:
517 if(Demod.sub == SUB_BOTH) {
518 //Demod.state = DEMOD_MANCHESTER_D;
519 Demod.state = DEMOD_START_OF_COMMUNICATION2;
520 Demod.posCount = 1;
521 Demod.sub = SUB_NONE;
522 }
523 else {
524 Demod.output[Demod.len] = 0xab;
525 Demod.state = DEMOD_ERROR_WAIT;
526 error = 0xd2;
527 }
528 break;
529 case DEMOD_START_OF_COMMUNICATION2:
530 if(Demod.sub == SUB_SECOND_HALF) {
531 Demod.state = DEMOD_START_OF_COMMUNICATION3;
532 }
533 else {
534 Demod.output[Demod.len] = 0xab;
535 Demod.state = DEMOD_ERROR_WAIT;
536 error = 0xd3;
537 }
538 break;
539 case DEMOD_START_OF_COMMUNICATION3:
540 if(Demod.sub == SUB_SECOND_HALF) {
541// Demod.state = DEMOD_MANCHESTER_D;
542 Demod.state = DEMOD_SOF_COMPLETE;
543 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
544 //Demod.len++;
545 }
546 else {
547 Demod.output[Demod.len] = 0xab;
548 Demod.state = DEMOD_ERROR_WAIT;
549 error = 0xd4;
550 }
551 break;
552 case DEMOD_SOF_COMPLETE:
553 case DEMOD_MANCHESTER_D:
554 case DEMOD_MANCHESTER_E:
555 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
556 // 00001111 = 1 (0 in 14443)
557 if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF
558 Demod.bitCount++;
559 Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100;
560 Demod.state = DEMOD_MANCHESTER_D;
561 }
562 else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF
563 Demod.bitCount++;
564 Demod.shiftReg >>= 1;
565 Demod.state = DEMOD_MANCHESTER_E;
566 }
567 else if(Demod.sub == SUB_BOTH) {
568 Demod.state = DEMOD_MANCHESTER_F;
569 }
570 else {
571 Demod.state = DEMOD_ERROR_WAIT;
572 error = 0x55;
573 }
574 break;
575
576 case DEMOD_MANCHESTER_F:
577 // Tag response does not need to be a complete byte!
578 if(Demod.len > 0 || Demod.bitCount > 0) {
579 if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF
580 Demod.shiftReg >>= (9 - Demod.bitCount);
581 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
582 Demod.len++;
583 // No parity bit, so just shift a 0
584 Demod.parityBits <<= 1;
585 }
586
587 Demod.state = DEMOD_UNSYNCD;
588 return TRUE;
589 }
590 else {
591 Demod.output[Demod.len] = 0xad;
592 Demod.state = DEMOD_ERROR_WAIT;
593 error = 0x03;
594 }
595 break;
596
597 case DEMOD_ERROR_WAIT:
598 Demod.state = DEMOD_UNSYNCD;
599 break;
600
601 default:
602 Demod.output[Demod.len] = 0xdd;
603 Demod.state = DEMOD_UNSYNCD;
604 break;
605 }
606
607 /*if(Demod.bitCount>=9) {
608 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
609 Demod.len++;
610
611 Demod.parityBits <<= 1;
612 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
613
614 Demod.bitCount = 0;
615 Demod.shiftReg = 0;
616 }*/
617 if(Demod.bitCount>=8) {
618 Demod.shiftReg >>= 1;
619 Demod.output[Demod.len] = (Demod.shiftReg & 0xff);
620 Demod.len++;
621
622 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
623 Demod.parityBits <<= 1;
624 Demod.parityBits ^= OddByteParity[(Demod.shiftReg & 0xff)];
625
626 Demod.bitCount = 0;
627 Demod.shiftReg = 0;
628 }
629
630 if(error) {
631 Demod.output[Demod.len] = 0xBB;
632 Demod.len++;
633 Demod.output[Demod.len] = error & 0xFF;
634 Demod.len++;
635 Demod.output[Demod.len] = 0xBB;
636 Demod.len++;
637 Demod.output[Demod.len] = bit & 0xFF;
638 Demod.len++;
639 Demod.output[Demod.len] = Demod.buffer & 0xFF;
640 Demod.len++;
641 // Look harder ;-)
642 Demod.output[Demod.len] = Demod.buffer2 & 0xFF;
643 Demod.len++;
644 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
645 Demod.len++;
646 Demod.output[Demod.len] = 0xBB;
647 Demod.len++;
648 return TRUE;
649 }
650
651 }
652
653 } // end (state != UNSYNCED)
654
655 return FALSE;
656}
657
658//=============================================================================
1e262141 659// Finally, a `sniffer' for iClass communication
cee5a30d 660// Both sides of communication!
661//=============================================================================
662
663//-----------------------------------------------------------------------------
664// Record the sequence of commands sent by the reader to the tag, with
665// triggering so that we start recording at the point that the tag is moved
666// near the reader.
667//-----------------------------------------------------------------------------
668void RAMFUNC SnoopIClass(void)
669{
1e262141 670// DEFINED ABOVE
671// #define RECV_CMD_OFFSET 3032
672// #define RECV_RES_OFFSET 3096
673// #define DMA_BUFFER_OFFSET 3160
674// #define DMA_BUFFER_SIZE 4096
81cd0474 675// #define TRACE_SIZE 3000
cee5a30d 676
677 // We won't start recording the frames that we acquire until we trigger;
678 // a good trigger condition to get started is probably when we see a
679 // response from the tag.
9f693930 680 //int triggered = FALSE; // FALSE to wait first for card
cee5a30d 681
682 // The command (reader -> tag) that we're receiving.
683 // The length of a received command will in most cases be no more than 18 bytes.
684 // So 32 should be enough!
685 uint8_t *receivedCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
686 // The response (tag -> reader) that we're receiving.
687 uint8_t *receivedResponse = (((uint8_t *)BigBuf) + RECV_RES_OFFSET);
688
689 // As we receive stuff, we copy it from receivedCmd or receivedResponse
690 // into trace, along with its length and other annotations.
691 //uint8_t *trace = (uint8_t *)BigBuf;
692
1e262141 693 // reset traceLen to 0
694 iso14a_set_tracing(TRUE);
d19929cb 695 iso14a_clear_trace();
1e262141 696 iso14a_set_trigger(FALSE);
cee5a30d 697
698 // The DMA buffer, used to stream samples from the FPGA
699 int8_t *dmaBuf = ((int8_t *)BigBuf) + DMA_BUFFER_OFFSET;
700 int lastRxCounter;
701 int8_t *upTo;
702 int smpl;
703 int maxBehindBy = 0;
704
705 // Count of samples received so far, so that we can include timing
706 // information in the trace buffer.
707 int samples = 0;
708 rsamples = 0;
709
710 memset(trace, 0x44, RECV_CMD_OFFSET);
711
712 // Set up the demodulator for tag -> reader responses.
713 Demod.output = receivedResponse;
714 Demod.len = 0;
715 Demod.state = DEMOD_UNSYNCD;
716
717 // Setup for the DMA.
718 FpgaSetupSsc();
719 upTo = dmaBuf;
720 lastRxCounter = DMA_BUFFER_SIZE;
721 FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE);
722
723 // And the reader -> tag commands
724 memset(&Uart, 0, sizeof(Uart));
725 Uart.output = receivedCmd;
726 Uart.byteCntMax = 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
727 Uart.state = STATE_UNSYNCD;
728
729 // And put the FPGA in the appropriate mode
730 // Signal field is off with the appropriate LED
731 LED_D_OFF();
732 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER);
733 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
734
735 int div = 0;
736 //int div2 = 0;
737 int decbyte = 0;
738 int decbyter = 0;
739
740 // And now we loop, receiving samples.
741 for(;;) {
742 LED_A_ON();
743 WDT_HIT();
744 int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &
745 (DMA_BUFFER_SIZE-1);
746 if(behindBy > maxBehindBy) {
747 maxBehindBy = behindBy;
748 if(behindBy > 400) {
749 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
750 goto done;
751 }
752 }
753 if(behindBy < 1) continue;
754
755 LED_A_OFF();
756 smpl = upTo[0];
757 upTo++;
758 lastRxCounter -= 1;
759 if(upTo - dmaBuf > DMA_BUFFER_SIZE) {
760 upTo -= DMA_BUFFER_SIZE;
761 lastRxCounter += DMA_BUFFER_SIZE;
762 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
763 AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;
764 }
765
766 //samples += 4;
767 samples += 1;
768 //div2++;
769
770 //if(div2 > 3) {
771 //div2 = 0;
772 //decbyte ^= ((smpl & 0x01) << (3 - div));
773 //decbyte ^= (((smpl & 0x01) | ((smpl & 0x02) >> 1)) << (3 - div)); // better already...
774 //decbyte ^= (((smpl & 0x01) | ((smpl & 0x02) >> 1) | ((smpl & 0x04) >> 2)) << (3 - div)); // even better...
775 if(smpl & 0xF) {
776 decbyte ^= (1 << (3 - div));
777 }
778 //decbyte ^= (MajorityNibble[(smpl & 0x0F)] << (3 - div));
779
780 // FOR READER SIDE COMMUMICATION...
781 //decbyte ^= ((smpl & 0x10) << (3 - div));
782 decbyter <<= 2;
783 decbyter ^= (smpl & 0x30);
784
785 div++;
786
787 if((div + 1) % 2 == 0) {
788 smpl = decbyter;
1e262141 789 if(OutOfNDecoding((smpl & 0xF0) >> 4)) {
cee5a30d 790 rsamples = samples - Uart.samples;
791 LED_C_ON();
792 //if(triggered) {
793 trace[traceLen++] = ((rsamples >> 0) & 0xff);
794 trace[traceLen++] = ((rsamples >> 8) & 0xff);
795 trace[traceLen++] = ((rsamples >> 16) & 0xff);
796 trace[traceLen++] = ((rsamples >> 24) & 0xff);
797 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff);
798 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff);
799 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff);
800 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff);
801 trace[traceLen++] = Uart.byteCnt;
802 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt);
803 traceLen += Uart.byteCnt;
81cd0474 804 if(traceLen > TRACE_SIZE) break;
cee5a30d 805 //}
806 /* And ready to receive another command. */
807 Uart.state = STATE_UNSYNCD;
808 /* And also reset the demod code, which might have been */
809 /* false-triggered by the commands from the reader. */
810 Demod.state = DEMOD_UNSYNCD;
811 LED_B_OFF();
812 Uart.byteCnt = 0;
813 }
814 decbyter = 0;
815 }
816
817 if(div > 3) {
818 smpl = decbyte;
819 if(ManchesterDecoding(smpl & 0x0F)) {
820 rsamples = samples - Demod.samples;
821 LED_B_ON();
822
823 // timestamp, as a count of samples
824 trace[traceLen++] = ((rsamples >> 0) & 0xff);
825 trace[traceLen++] = ((rsamples >> 8) & 0xff);
826 trace[traceLen++] = ((rsamples >> 16) & 0xff);
827 trace[traceLen++] = 0x80 | ((rsamples >> 24) & 0xff);
828 trace[traceLen++] = ((Demod.parityBits >> 0) & 0xff);
829 trace[traceLen++] = ((Demod.parityBits >> 8) & 0xff);
830 trace[traceLen++] = ((Demod.parityBits >> 16) & 0xff);
831 trace[traceLen++] = ((Demod.parityBits >> 24) & 0xff);
832 // length
833 trace[traceLen++] = Demod.len;
834 memcpy(trace+traceLen, receivedResponse, Demod.len);
835 traceLen += Demod.len;
81cd0474 836 if(traceLen > TRACE_SIZE) break;
cee5a30d 837
9f693930 838 //triggered = TRUE;
cee5a30d 839
840 // And ready to receive another response.
841 memset(&Demod, 0, sizeof(Demod));
842 Demod.output = receivedResponse;
843 Demod.state = DEMOD_UNSYNCD;
844 LED_C_OFF();
845 }
846
847 div = 0;
848 decbyte = 0x00;
849 }
850 //}
851
852 if(BUTTON_PRESS()) {
853 DbpString("cancelled_a");
854 goto done;
855 }
856 }
857
858 DbpString("COMMAND FINISHED");
859
860 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
861 Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]);
862
863done:
864 AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
865 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
866 Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]);
867 LED_A_OFF();
868 LED_B_OFF();
1e262141 869 LED_C_OFF();
870 LED_D_OFF();
871}
872
912a3e94 873void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
874 int i;
875 for(i = 0; i < 8; i++) {
876 rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5);
1e262141 877 }
878}
879
880//-----------------------------------------------------------------------------
881// Wait for commands from reader
882// Stop when button is pressed
883// Or return TRUE when command is captured
884//-----------------------------------------------------------------------------
885static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen)
886{
912a3e94 887 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
1e262141 888 // only, since we are receiving, not transmitting).
889 // Signal field is off with the appropriate LED
890 LED_D_OFF();
891 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
892
893 // Now run a `software UART' on the stream of incoming samples.
894 Uart.output = received;
895 Uart.byteCntMax = maxLen;
896 Uart.state = STATE_UNSYNCD;
897
898 for(;;) {
899 WDT_HIT();
900
901 if(BUTTON_PRESS()) return FALSE;
902
903 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
904 AT91C_BASE_SSC->SSC_THR = 0x00;
905 }
906 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
907 uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
908 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
909 *len = Uart.byteCnt;
910 return TRUE;
911 }*/
912 if(OutOfNDecoding(b & 0x0f)) {
913 *len = Uart.byteCnt;
914 return TRUE;
915 }
916 }
917 }
918}
919
920
921//-----------------------------------------------------------------------------
922// Prepare tag messages
923//-----------------------------------------------------------------------------
924static void CodeIClassTagAnswer(const uint8_t *cmd, int len)
925{
926 int i;
927
928 ToSendReset();
929
930 // Send SOF
931 ToSend[++ToSendMax] = 0x00;
932 ToSend[++ToSendMax] = 0x00;
933 ToSend[++ToSendMax] = 0x00;
934 ToSend[++ToSendMax] = 0xff;
935 ToSend[++ToSendMax] = 0xff;
936 ToSend[++ToSendMax] = 0xff;
937 ToSend[++ToSendMax] = 0x00;
938 ToSend[++ToSendMax] = 0xff;
939
940 for(i = 0; i < len; i++) {
941 int j;
942 uint8_t b = cmd[i];
943
944 // Data bits
945 for(j = 0; j < 8; j++) {
946 if(b & 1) {
947 ToSend[++ToSendMax] = 0x00;
948 ToSend[++ToSendMax] = 0xff;
949 } else {
950 ToSend[++ToSendMax] = 0xff;
951 ToSend[++ToSendMax] = 0x00;
952 }
953 b >>= 1;
954 }
955 }
956
957 // Send EOF
958 ToSend[++ToSendMax] = 0xff;
959 ToSend[++ToSendMax] = 0x00;
960 ToSend[++ToSendMax] = 0xff;
961 ToSend[++ToSendMax] = 0xff;
962 ToSend[++ToSendMax] = 0xff;
963 ToSend[++ToSendMax] = 0x00;
964 ToSend[++ToSendMax] = 0x00;
965 ToSend[++ToSendMax] = 0x00;
966
967 // Convert from last byte pos to length
968 ToSendMax++;
969}
970
971// Only SOF
972static void CodeIClassTagSOF()
973{
974 ToSendReset();
975
976 // Send SOF
977 ToSend[++ToSendMax] = 0x00;
978 ToSend[++ToSendMax] = 0x00;
979 ToSend[++ToSendMax] = 0x00;
980 ToSend[++ToSendMax] = 0xff;
981 ToSend[++ToSendMax] = 0xff;
982 ToSend[++ToSendMax] = 0xff;
983 ToSend[++ToSendMax] = 0x00;
984 ToSend[++ToSendMax] = 0xff;
985
986 // Convert from last byte pos to length
987 ToSendMax++;
988}
989
990//-----------------------------------------------------------------------------
991// Simulate iClass Card
992// Only CSN (Card Serial Number)
993//
994//-----------------------------------------------------------------------------
995void SimulateIClass(uint8_t arg0, uint8_t *datain)
996{
1e262141 997 uint8_t simType = arg0;
1e262141 998
81cd0474 999 // Enable and clear the trace
1000 tracing = TRUE;
1001 traceLen = 0;
1002 memset(trace, 0x44, TRACE_SIZE);
1003
1e262141 1004 // CSN followed by two CRC bytes
1e262141 1005 uint8_t response2[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
912a3e94 1006 uint8_t response3[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
1e262141 1007
1008 // e-Purse
1009 uint8_t response4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1e262141 1010
1011 if(simType == 0) {
1012 // Use the CSN from commandline
1013 memcpy(response3, datain, 8);
1014 }
1015
1016 // Construct anticollision-CSN
912a3e94 1017 rotateCSN(response3,response2);
1e262141 1018
1019 // Compute CRC on both CSNs
1020 ComputeCrc14443(CRC_ICLASS, response2, 8, &response2[8], &response2[9]);
1021 ComputeCrc14443(CRC_ICLASS, response3, 8, &response3[8], &response3[9]);
1022
1023 // Reader 0a
1024 // Tag 0f
1025 // Reader 0c
1026 // Tag anticoll. CSN
1027 // Reader 81 anticoll. CSN
1028 // Tag CSN
1029
81cd0474 1030 uint8_t *resp;
1031 int respLen;
1032 uint8_t* respdata = NULL;
1033 int respsize = 0;
1034 uint8_t sof = 0x0f;
1e262141 1035
1036 // Respond SOF -- takes 8 bytes
81cd0474 1037 uint8_t *resp1 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET);
1e262141 1038 int resp1Len;
1039
1040 // Anticollision CSN (rotated CSN)
1041 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
81cd0474 1042 uint8_t *resp2 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 10);
1e262141 1043 int resp2Len;
1044
1045 // CSN
1046 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
81cd0474 1047 uint8_t *resp3 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 190);
912a3e94 1048 int resp3Len;
1e262141 1049
1050 // e-Purse
1051 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
81cd0474 1052 uint8_t *resp4 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 370);
1e262141 1053 int resp4Len;
1054
1055 // + 1720..
81cd0474 1056 uint8_t *receivedCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
1057 memset(receivedCmd, 0x44, RECV_CMD_SIZE);
1e262141 1058 int len;
1059
1e262141 1060 // Prepare card messages
1061 ToSendMax = 0;
1062
1063 // First card answer: SOF
1064 CodeIClassTagSOF();
1065 memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax;
1066
1067 // Anticollision CSN
1068 CodeIClassTagAnswer(response2, sizeof(response2));
1069 memcpy(resp2, ToSend, ToSendMax); resp2Len = ToSendMax;
1070
1071 // CSN
1072 CodeIClassTagAnswer(response3, sizeof(response3));
912a3e94 1073 memcpy(resp3, ToSend, ToSendMax); resp3Len = ToSendMax;
1e262141 1074
1075 // e-Purse
1076 CodeIClassTagAnswer(response4, sizeof(response4));
1077 memcpy(resp4, ToSend, ToSendMax); resp4Len = ToSendMax;
1078
1079 // We need to listen to the high-frequency, peak-detected path.
1080 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1081 FpgaSetupSsc();
1082
1083 // To control where we are in the protocol
1e262141 1084 int cmdsRecvd = 0;
912a3e94 1085
1e262141 1086 LED_A_ON();
1087 for(;;) {
1088 LED_B_OFF();
1089 if(!GetIClassCommandFromReader(receivedCmd, &len, 100)) {
81cd0474 1090 DbpString("button press");
1e262141 1091 break;
81cd0474 1092 }
1e262141 1093
81cd0474 1094 // Okay, look at the command now.
1095 if(receivedCmd[0] == 0x0a) {
1e262141 1096 // Reader in anticollission phase
1097 resp = resp1; respLen = resp1Len; //order = 1;
81cd0474 1098 respdata = &sof;
1099 respsize = sizeof(sof);
1e262141 1100 //resp = resp2; respLen = resp2Len; order = 2;
1101 //DbpString("Hello request from reader:");
1102 } else if(receivedCmd[0] == 0x0c) {
1103 // Reader asks for anticollission CSN
1104 resp = resp2; respLen = resp2Len; //order = 2;
81cd0474 1105 respdata = response2;
1106 respsize = sizeof(response2);
1e262141 1107 //DbpString("Reader requests anticollission CSN:");
1108 } else if(receivedCmd[0] == 0x81) {
1109 // Reader selects anticollission CSN.
1110 // Tag sends the corresponding real CSN
912a3e94 1111 resp = resp3; respLen = resp3Len; //order = 3;
81cd0474 1112 respdata = response3;
1113 respsize = sizeof(response3);
1e262141 1114 //DbpString("Reader selects anticollission CSN:");
1115 } else if(receivedCmd[0] == 0x88) {
1116 // Read e-purse (88 02)
1117 resp = resp4; respLen = resp4Len; //order = 4;
81cd0474 1118 respdata = response4;
1119 respsize = sizeof(response4);
1e262141 1120 LED_B_ON();
1121 } else if(receivedCmd[0] == 0x05) {
1122 // Reader random and reader MAC!!!
1123 // Lets store this ;-)
81cd0474 1124/*
912a3e94 1125 Dbprintf(" CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1e262141 1126 response3[0], response3[1], response3[2],
1127 response3[3], response3[4], response3[5],
1128 response3[6], response3[7]);
81cd0474 1129*/
1e262141 1130 Dbprintf("READER AUTH (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1131 len,
1132 receivedCmd[0], receivedCmd[1], receivedCmd[2],
1133 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1134 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
1135
1136 // Do not respond
1137 // We do not know what to answer, so lets keep quit
1138 resp = resp1; respLen = 0; //order = 5;
81cd0474 1139 respdata = NULL;
1140 respsize = 0;
1e262141 1141 } else if(receivedCmd[0] == 0x00 && len == 1) {
1142 // Reader ends the session
1143 resp = resp1; respLen = 0; //order = 0;
81cd0474 1144 respdata = NULL;
1145 respsize = 0;
1146 } else {
1e262141 1147 // Never seen this command before
1148 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1149 len,
1150 receivedCmd[0], receivedCmd[1], receivedCmd[2],
1151 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1152 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
1153 // Do not respond
1154 resp = resp1; respLen = 0; //order = 0;
81cd0474 1155 respdata = NULL;
1156 respsize = 0;
1e262141 1157 }
1158
1159 if(cmdsRecvd > 999) {
1160 DbpString("1000 commands later...");
1161 break;
1162 }
1163 else {
1164 cmdsRecvd++;
1165 }
1166
81cd0474 1167 if(respLen > 0) {
1168 SendIClassAnswer(resp, respLen, 21);
1169 }
1170
1171 if (tracing) {
7bc95e2e 1172 LogTrace(receivedCmd,len, rsamples, Uart.parityBits, TRUE);
81cd0474 1173 if (respdata != NULL) {
7bc95e2e 1174 LogTrace(respdata,respsize, rsamples, SwapBits(GetParity(respdata,respsize),respsize), FALSE);
81cd0474 1175 }
4ab4336a 1176 if(traceLen > TRACE_SIZE) {
1177 DbpString("Trace full");
1178 break;
1179 }
81cd0474 1180 }
1e262141 1181
81cd0474 1182 memset(receivedCmd, 0x44, RECV_CMD_SIZE);
1183 }
1e262141 1184
912a3e94 1185 Dbprintf("%x", cmdsRecvd);
1e262141 1186 LED_A_OFF();
1187 LED_B_OFF();
1188}
1189
1190static int SendIClassAnswer(uint8_t *resp, int respLen, int delay)
1191{
1192 int i = 0, u = 0, d = 0;
1193 uint8_t b = 0;
912a3e94 1194 // return 0;
1195 // Modulate Manchester
1196 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_MOD424);
1e262141 1197 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_MOD);
1198 AT91C_BASE_SSC->SSC_THR = 0x00;
1199 FpgaSetupSsc();
1200
1201 // send cycle
1202 for(;;) {
1203 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1204 volatile uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1205 (void)b;
1206 }
1207 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1208 if(d < delay) {
1209 b = 0x00;
1210 d++;
1211 }
1212 else if(i >= respLen) {
1213 b = 0x00;
1214 u++;
1215 } else {
1216 b = resp[i];
1217 u++;
1218 if(u > 1) { i++; u = 0; }
1219 }
1220 AT91C_BASE_SSC->SSC_THR = b;
1221
1222 if(u > 4) break;
1223 }
1224 if(BUTTON_PRESS()) {
1225 break;
1226 }
1227 }
1228
1229 return 0;
1230}
1231
1232/// THE READER CODE
1233
1234//-----------------------------------------------------------------------------
1235// Transmit the command (to the tag) that was placed in ToSend[].
1236//-----------------------------------------------------------------------------
1237static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait)
1238{
1239 int c;
1240
1241 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1242 AT91C_BASE_SSC->SSC_THR = 0x00;
1243 FpgaSetupSsc();
1244
1245 if (wait)
1246 if(*wait < 10)
1247 *wait = 10;
1248
1249 for(c = 0; c < *wait;) {
1250 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1251 AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
1252 c++;
1253 }
1254 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1255 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1256 (void)r;
1257 }
1258 WDT_HIT();
1259 }
1260
1261 uint8_t sendbyte;
1262 bool firstpart = TRUE;
1263 c = 0;
1264 for(;;) {
1265 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1266
1267 // DOUBLE THE SAMPLES!
1268 if(firstpart) {
1269 sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4);
1270 }
1271 else {
1272 sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4);
1273 c++;
1274 }
1275 if(sendbyte == 0xff) {
1276 sendbyte = 0xfe;
1277 }
1278 AT91C_BASE_SSC->SSC_THR = sendbyte;
1279 firstpart = !firstpart;
1280
1281 if(c >= len) {
1282 break;
1283 }
1284 }
1285 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1286 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1287 (void)r;
1288 }
1289 WDT_HIT();
1290 }
1291 if (samples) *samples = (c + *wait) << 3;
1292}
1293
1294
1295//-----------------------------------------------------------------------------
1296// Prepare iClass reader command to send to FPGA
1297//-----------------------------------------------------------------------------
1298void CodeIClassCommand(const uint8_t * cmd, int len)
1299{
1300 int i, j, k;
1301 uint8_t b;
1302
1303 ToSendReset();
1304
1305 // Start of Communication: 1 out of 4
1306 ToSend[++ToSendMax] = 0xf0;
1307 ToSend[++ToSendMax] = 0x00;
1308 ToSend[++ToSendMax] = 0x0f;
1309 ToSend[++ToSendMax] = 0x00;
1310
1311 // Modulate the bytes
1312 for (i = 0; i < len; i++) {
1313 b = cmd[i];
1314 for(j = 0; j < 4; j++) {
1315 for(k = 0; k < 4; k++) {
1316 if(k == (b & 3)) {
1317 ToSend[++ToSendMax] = 0x0f;
1318 }
1319 else {
1320 ToSend[++ToSendMax] = 0x00;
1321 }
1322 }
1323 b >>= 2;
1324 }
1325 }
1326
1327 // End of Communication
1328 ToSend[++ToSendMax] = 0x00;
1329 ToSend[++ToSendMax] = 0x00;
1330 ToSend[++ToSendMax] = 0xf0;
1331 ToSend[++ToSendMax] = 0x00;
1332
1333 // Convert from last character reference to length
1334 ToSendMax++;
1335}
1336
1337void ReaderTransmitIClass(uint8_t* frame, int len)
1338{
1339 int wait = 0;
1340 int samples = 0;
1341 int par = 0;
1342
1343 // This is tied to other size changes
1344 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1345 CodeIClassCommand(frame,len);
1346
1347 // Select the card
1348 TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait);
1349 if(trigger)
1350 LED_A_ON();
1351
1352 // Store reader command in buffer
7bc95e2e 1353 if (tracing) LogTrace(frame,len,rsamples,par,TRUE);
1e262141 1354}
1355
1356//-----------------------------------------------------------------------------
1357// Wait a certain time for tag response
1358// If a response is captured return TRUE
1359// If it takes too long return FALSE
1360//-----------------------------------------------------------------------------
1361static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) //uint8_t *buffer
1362{
1363 // buffer needs to be 512 bytes
1364 int c;
1365
1366 // Set FPGA mode to "reader listen mode", no modulation (listen
1367 // only, since we are receiving, not transmitting).
1368 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN);
1369
1370 // Now get the answer from the card
1371 Demod.output = receivedResponse;
1372 Demod.len = 0;
1373 Demod.state = DEMOD_UNSYNCD;
1374
1375 uint8_t b;
1376 if (elapsed) *elapsed = 0;
1377
1378 bool skip = FALSE;
1379
1380 c = 0;
1381 for(;;) {
1382 WDT_HIT();
1383
1384 if(BUTTON_PRESS()) return FALSE;
1385
1386 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1387 AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!!
1388 if (elapsed) (*elapsed)++;
1389 }
1390 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1391 if(c < timeout) { c++; } else { return FALSE; }
1392 b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1393 skip = !skip;
1394 if(skip) continue;
1395 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1396 *samples = ((c - 1) << 3) + 4;
1397 return TRUE;
1398 }*/
1399 if(ManchesterDecoding(b & 0x0f)) {
1400 *samples = c << 3;
1401 return TRUE;
1402 }
1403 }
1404 }
1405}
1406
1407int ReaderReceiveIClass(uint8_t* receivedAnswer)
1408{
1409 int samples = 0;
1410 if (!GetIClassAnswer(receivedAnswer,160,&samples,0)) return FALSE;
7bc95e2e 1411 rsamples += samples;
1412 if (tracing) LogTrace(receivedAnswer,Demod.len,rsamples,Demod.parityBits,FALSE);
1e262141 1413 if(samples == 0) return FALSE;
1414 return Demod.len;
1415}
1416
1417// Reader iClass Anticollission
1418void ReaderIClass(uint8_t arg0) {
1e262141 1419 uint8_t act_all[] = { 0x0a };
1420 uint8_t identify[] = { 0x0c };
4ab4336a 1421 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1e262141 1422
1423 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1424
1425 // Reset trace buffer
1426 memset(trace, 0x44, RECV_CMD_OFFSET);
1427 traceLen = 0;
1428
1429 // Setup SSC
1430 FpgaSetupSsc();
1431 // Start from off (no field generated)
1432 // Signal field is off with the appropriate LED
cee5a30d 1433 LED_D_OFF();
1e262141 1434 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1435 SpinDelay(200);
1436
1437 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1438
1439 // Now give it time to spin up.
1440 // Signal field is on with the appropriate LED
1441 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1442 SpinDelay(200);
1443
1444 LED_A_ON();
1445
1446 for(;;) {
4ab4336a 1447
1448 if(traceLen > TRACE_SIZE) {
1449 DbpString("Trace full");
1450 break;
1451 }
1452
1453 if (BUTTON_PRESS()) break;
1e262141 1454
1455 // Send act_all
1456 ReaderTransmitIClass(act_all, 1);
1457 // Card present?
1458 if(ReaderReceiveIClass(resp)) {
1459 ReaderTransmitIClass(identify, 1);
4ab4336a 1460 if(ReaderReceiveIClass(resp) == 10) {
1461 // Select card
1462 memcpy(&select[1],resp,8);
1463 ReaderTransmitIClass(select, sizeof(select));
1464
1465 if(ReaderReceiveIClass(resp) == 10) {
1466 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1467 resp[0], resp[1], resp[2],
1468 resp[3], resp[4], resp[5],
1469 resp[6], resp[7]);
1470 }
1471 // Card selected, whats next... ;-)
1e262141 1472 }
1473 }
1474 WDT_HIT();
1475 }
1476
1477 LED_A_OFF();
cee5a30d 1478}
1479
c3963755 1480void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
1481 uint8_t act_all[] = { 0x0a };
1482 uint8_t identify[] = { 0x0c };
1483 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1484 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1485 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1486 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1487
1488 uint16_t crc = 0;
1489 uint8_t cardsize=0;
1490 bool read_success=false;
1491 uint8_t mem=0;
1492
1493 static struct memory_t{
1494 int k16;
1495 int book;
1496 int k2;
1497 int lockauth;
1498 int keyaccess;
1499 } memory;
1500
1501 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1502
1503 // Reset trace buffer
1504 memset(trace, 0x44, RECV_CMD_OFFSET);
1505 traceLen = 0;
1506
1507 // Setup SSC
1508 FpgaSetupSsc();
1509 // Start from off (no field generated)
1510 // Signal field is off with the appropriate LED
1511 LED_D_OFF();
1512 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1513 SpinDelay(200);
1514
1515 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1516
1517 // Now give it time to spin up.
1518 // Signal field is on with the appropriate LED
1519 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1520 SpinDelay(200);
1521
1522 LED_A_ON();
1523
1524 for(int i=0;i<1;i++) {
1525
1526 if(traceLen > TRACE_SIZE) {
1527 DbpString("Trace full");
1528 break;
1529 }
1530
1531 if (BUTTON_PRESS()) break;
1532
1533 // Send act_all
1534 ReaderTransmitIClass(act_all, 1);
1535 // Card present?
1536 if(ReaderReceiveIClass(resp)) {
1537 ReaderTransmitIClass(identify, 1);
1538 if(ReaderReceiveIClass(resp) == 10) {
1539 // Select card
1540 memcpy(&select[1],resp,8);
1541 ReaderTransmitIClass(select, sizeof(select));
1542
1543 if(ReaderReceiveIClass(resp) == 10) {
1544 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1545 resp[0], resp[1], resp[2],
1546 resp[3], resp[4], resp[5],
1547 resp[6], resp[7]);
1548 }
1549 // Card selected
1550 Dbprintf("Readcheck on Sector 2");
1551 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1552 if(ReaderReceiveIClass(resp) == 8) {
1553 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1554 resp[0], resp[1], resp[2],
1555 resp[3], resp[4], resp[5],
1556 resp[6], resp[7]);
1557 }else return;
1558 Dbprintf("Authenticate");
1559 //for now replay captured auth (as cc not updated)
1560 memcpy(check+5,MAC,4);
1561 Dbprintf(" AA: %02x %02x %02x %02x",
1562 check[5], check[6], check[7],check[8]);
1563 ReaderTransmitIClass(check, sizeof(check));
1564 if(ReaderReceiveIClass(resp) == 4) {
1565 Dbprintf(" AR: %02x %02x %02x %02x",
1566 resp[0], resp[1], resp[2],resp[3]);
1567 }else {
1568 Dbprintf("Error: Authentication Fail!");
1569 return;
1570 }
1571 Dbprintf("Dump Contents");
1572 //first get configuration block
1573 read_success=false;
1574 read[1]=1;
1575 uint8_t *blockno=&read[1];
1576 crc = iclass_crc16((char *)blockno,1);
1577 read[2] = crc >> 8;
1578 read[3] = crc & 0xff;
1579 while(!read_success){
1580 ReaderTransmitIClass(read, sizeof(read));
1581 if(ReaderReceiveIClass(resp) == 10) {
1582 read_success=true;
1583 mem=resp[5];
1584 memory.k16= (mem & 0x80);
1585 memory.book= (mem & 0x20);
1586 memory.k2= (mem & 0x8);
1587 memory.lockauth= (mem & 0x2);
1588 memory.keyaccess= (mem & 0x1);
1589
1590 }
1591 }
1592 if (memory.k16){
1593 cardsize=255;
1594 }else cardsize=32;
1595 //then loop around remaining blocks
1596 for(uint8_t j=0; j<cardsize; j++){
1597 read_success=false;
1598 uint8_t *blockno=&j;
1599 //crc_data[0]=j;
1600 read[1]=j;
1601 crc = iclass_crc16((char *)blockno,1);
1602 read[2] = crc >> 8;
1603 read[3] = crc & 0xff;
1604 while(!read_success){
1605 ReaderTransmitIClass(read, sizeof(read));
1606 if(ReaderReceiveIClass(resp) == 10) {
1607 read_success=true;
1608 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1609 j, resp[0], resp[1], resp[2],
1610 resp[3], resp[4], resp[5],
1611 resp[6], resp[7]);
1612 }
1613 }
1614 }
1615 }
1616 }
1617 WDT_HIT();
1618 }
1619
1620 LED_A_OFF();
1621}
1622
912a3e94 1623
Impressum, Datenschutz