1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // HitagS emulation (preliminary test version)
8 // (c) 2016 Oguzhan Cicek, Hendrik Schwartke, Ralf Spenneberg
10 //-----------------------------------------------------------------------------
11 // Some code was copied from Hitag2.c
12 //-----------------------------------------------------------------------------
17 #include "proxmark3_arm.h"
20 #include "fpgaloader.h"
25 #include "commonutil.h"
26 #include "hitag2_crypto.h"
29 #define CRC_PRESET 0xFF
30 #define CRC_POLYNOM 0x1D
33 static bool bSuccessful
;
34 static struct hitagS_tag tag
;
35 static uint8_t page_to_be_written
= 0;
36 static int block_data_left
= 0;
38 typedef enum modulation
{
45 static MOD m
= AC2K
; // used modulation
46 static uint32_t temp_uid
;
48 static int sof_bits
; // number of start-of-frame bits
49 static uint8_t pwdh0
, pwdl0
, pwdl1
; // password bytes
50 static uint32_t rnd
= 0x74124485; // randomnumber
51 static bool end
= false;
52 //#define SENDBIT_TEST
54 /* array index 3 2 1 0 // bytes in sim.bin file are 0 1 2 3
55 // UID is 0 1 2 3 // tag.uid is 3210
56 // datasheet HitagS_V11.pdf bytes in tables printed 3 2 1 0
59 #db# conf0: C9 conf1: 00 conf2: 00
61 #db# Page[ 0]: 84 11 C2 5F uid
62 #db# Page[ 1]: AA 00 00 C9 conf, HITAG S 256
63 #db# Page[ 2]: 4E 4F 54 48
64 #db# Page[ 3]: 52 4B 49 4D
65 #db# Page[ 4]: 00 00 00 00
66 #db# Page[ 5]: 00 00 00 00
67 #db# Page[ 6]: 00 00 00 00
68 #db# Page[ 7]: 4B 4F 5F 57 */
70 #define ht2bs_4a(a,b,c,d) (~(((a|b)&c)^(a|d)^b))
71 #define ht2bs_4b(a,b,c,d) (~(((d|c)&(a^b))^(d|a|b)))
72 #define ht2bs_5c(a,b,c,d,e) (~((((((c^e)|d)&a)^b)&(c^b))^(((d^e)|a)&((d^b)|c))))
74 // Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK)
75 // TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz
76 // Hitag units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
77 // T0 = TIMER_CLOCK1 / 125000 = 192
82 #define HITAG_FRAME_LEN 20
83 #define HITAG_T_STOP 36 /* T_EOF should be > 36 */
84 #define HITAG_T_LOW 8 /* T_LOW should be 4..10 */
85 #define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */
86 #define HITAG_T_1_MIN 25 /* T[1] should be 26..30 */
87 //#define HITAG_T_EOF 40 /* T_EOF should be > 36 */
88 #define HITAG_T_EOF 80 /* T_EOF should be > 36 */
89 #define HITAG_T_WAIT_1 200 /* T_wresp should be 199..206 */
90 #define HITAG_T_WAIT_2 90 /* T_wresp should be 199..206 */
91 #define HITAG_T_WAIT_MAX 300 /* bit more than HITAG_T_WAIT_1 + HITAG_T_WAIT_2 */
93 #define HITAG_T_TAG_ONE_HALF_PERIOD 10
94 #define HITAG_T_TAG_TWO_HALF_PERIOD 25
95 #define HITAG_T_TAG_THREE_HALF_PERIOD 41
96 #define HITAG_T_TAG_FOUR_HALF_PERIOD 57
98 #define HITAG_T_TAG_HALF_PERIOD 16
99 #define HITAG_T_TAG_FULL_PERIOD 32
101 #define HITAG_T_TAG_CAPTURE_ONE_HALF 13
102 #define HITAG_T_TAG_CAPTURE_TWO_HALF 25
103 #define HITAG_T_TAG_CAPTURE_THREE_HALF 41
104 #define HITAG_T_TAG_CAPTURE_FOUR_HALF 57
107 * Implementation of the crc8 calculation from Hitag S
108 * from http://www.proxmark.org/files/Documents/125%20kHz%20-%20Hitag/HitagS.V11.pdf
110 static void calc_crc(unsigned char *crc
, unsigned char data
, unsigned char Bitcount
) {
111 *crc
^= data
; // crc = crc (exor) data
113 if (*crc
& 0x80) { // if (MSB-CRC == 1)
114 *crc
<<= 1; // CRC = CRC Bit-shift left
115 *crc
^= CRC_POLYNOM
; // CRC = CRC (exor) CRC_POLYNOM
117 *crc
<<= 1; // CRC = CRC Bit-shift left
119 } while (--Bitcount
);
122 static void hitag_send_bit(int bit
) {
124 // Reset clock for the next bit
125 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
132 while (AT91C_BASE_TC0
->TC_CV
< T0
* 32) {};
135 while (AT91C_BASE_TC0
->TC_CV
< T0
* 64) {};
140 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
143 while (AT91C_BASE_TC0
->TC_CV
< T0
* 32) {};
146 while (AT91C_BASE_TC0
->TC_CV
< T0
* 48) {};
149 while (AT91C_BASE_TC0
->TC_CV
< T0
* 64) {};
158 while (AT91C_BASE_TC0
->TC_CV
< T0
* HITAG_T_TAG_HALF_PERIOD
) {};
161 while (AT91C_BASE_TC0
->TC_CV
< T0
* HITAG_T_TAG_FULL_PERIOD
) {};
166 while (AT91C_BASE_TC0
->TC_CV
< T0
* 8) {};
169 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
172 while (AT91C_BASE_TC0
->TC_CV
< T0
* 24) {};
175 while (AT91C_BASE_TC0
->TC_CV
< T0
* 32) {};
181 // Manchester: Unloaded, then loaded |__--|
183 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
186 while (AT91C_BASE_TC0
->TC_CV
< T0
* 32) {};
189 // Manchester: Loaded, then unloaded |--__|
191 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
194 while (AT91C_BASE_TC0
->TC_CV
< T0
* 32) {};
201 // Manchester: Unloaded, then loaded |__--|
203 while (AT91C_BASE_TC0
->TC_CV
< T0
* 8) {};
206 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
209 // Manchester: Loaded, then unloaded |--__|
211 while (AT91C_BASE_TC0
->TC_CV
< T0
* 8) {};
214 while (AT91C_BASE_TC0
->TC_CV
< T0
* 16) {};
224 static void hitag_send_frame(const uint8_t *frame
, size_t frame_len
) {
225 if (DBGLEVEL
>= DBG_EXTENDED
)
226 Dbprintf("hitag_send_frame: (%i) %02X %02X %02X %02X", frame_len
, frame
[0], frame
[1], frame
[2], frame
[3]);
227 // The beginning of the frame is hidden in some high level; pause until our bits will have an effect
228 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
233 while (AT91C_BASE_TC0
->TC_CV
< T0
* 40) {}; //FADV
237 while (AT91C_BASE_TC0
->TC_CV
< T0
* 20) {}; //STD + ADV
241 // SOF - send start of frame
242 for (size_t i
= 0; i
< sof_bits
; i
++) {
246 // Send the content of the frame
247 for (size_t i
= 0; i
< frame_len
; i
++) {
248 hitag_send_bit((frame
[i
/ 8] >> (7 - (i
% 8))) & 1);
254 static void hitag_reader_send_bit(int bit
) {
257 // Reset clock for the next bit
258 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
260 // Binary puls length modulation (BPLM) is used to encode the data stream
261 // This means that a transmission of a one takes longer than that of a zero
266 // Wait for 4-10 times the carrier period
267 while (AT91C_BASE_TC0
->TC_CV
< T0
* 6) {};
273 while (AT91C_BASE_TC0
->TC_CV
< T0
* 11) {};
276 while (AT91C_BASE_TC0
->TC_CV
< T0
* 14) {};
279 // Wait for 4-10 times the carrier period
280 while (AT91C_BASE_TC0
->TC_CV
< T0
* 6) {};
286 while (AT91C_BASE_TC0
->TC_CV
< T0
* 22) {};
289 while (AT91C_BASE_TC0
->TC_CV
< T0
* 28) {};
296 static void hitag_reader_send_frame(const uint8_t *frame
, size_t frame_len
) {
297 // Send the content of the frame
298 for (size_t i
= 0; i
< frame_len
; i
++) {
299 // if (frame[0] == 0xf8) {
300 //Dbprintf("BIT: %d",(frame[i / 8] >> (7 - (i % 8))) & 1);
302 hitag_reader_send_bit((frame
[i
/ 8] >> (7 - (i
% 8))) & 1);
305 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
309 // Wait for 4-10 times the carrier period
310 while (AT91C_BASE_TC0
->TC_CV
< T0
* 6) {};
316 * to check if the right uid was selected
318 static int check_select(uint8_t *rx
, uint32_t uid
) {
319 unsigned char resp
[48];
321 for (int i
= 0; i
< 48; i
++)
322 resp
[i
] = (rx
[i
/ 8] >> (7 - (i
% 8))) & 0x1;
323 for (int i
= 0; i
< 32; i
++)
324 ans
+= resp
[5 + i
] << (31 - i
);
333 static void hitagS_set_frame_modulation(void) {
343 case HT_FAST_ADVANCED
:
353 * handles all commands from a reader
355 static void hitagS_handle_reader_command(uint8_t *rx
, const size_t rxlen
,
356 uint8_t *tx
, size_t *txlen
) {
357 uint8_t rx_air
[HITAG_FRAME_LEN
];
361 // Copy the (original) received frame how it is send over the air
362 memcpy(rx_air
, rx
, nbytes(rxlen
));
364 // Reset the transmission frame length
367 // Try to find out which command was send by selecting on length (in bits)
370 //UID request with a selected response protocol mode
371 if (DBGLEVEL
>= DBG_EXTENDED
)
372 Dbprintf("UID request: length: %i first byte: %02x", rxlen
, rx
[0]);
373 tag
.pstate
= HT_READY
;
374 tag
.tstate
= HT_NO_OP
;
375 if ((rx
[0] & 0xf0) == 0x30) {
376 if (DBGLEVEL
>= DBG_EXTENDED
)
377 Dbprintf("HT_STANDARD");
378 tag
.mode
= HT_STANDARD
;
382 if ((rx
[0] & 0xf0) == 0xc0) {
383 tag
.mode
= HT_ADVANCED
;
384 if (DBGLEVEL
>= DBG_EXTENDED
)
385 Dbprintf("HT_ADVANCED");
390 if ((rx
[0] & 0xf0) == 0xd0) {
391 if (DBGLEVEL
>= DBG_EXTENDED
)
392 Dbprintf("HT_FAST_ADVANCED");
393 tag
.mode
= HT_FAST_ADVANCED
;
397 //send uid as a response
399 for (int i
= 0; i
< 4; i
++)
400 tx
[i
] = (tag
.uid
>> (24 - (i
* 8))) & 0xff;
404 //select command from reader received
405 if (DBGLEVEL
>= DBG_EXTENDED
)
407 if (check_select(rx
, tag
.uid
) == 1) {
408 if (DBGLEVEL
>= DBG_EXTENDED
)
409 DbpString("SELECT match");
410 //if the right tag was selected
412 hitagS_set_frame_modulation();
415 for (int i
= 0; i
< 4; i
++)
416 tx
[i
] = tag
.pages
[1][i
];
418 if (tag
.mode
!= HT_STANDARD
) {
421 for (int i
= 0; i
< 4; i
++)
422 calc_crc(&crc
, tx
[i
], 8);
429 //challenge message received
430 Dbprintf("Challenge for UID: %X", temp_uid
);
433 state
= _hitag2_init(REV64(tag
.key
),
434 REV32((tag
.pages
[0][3] << 24) + (tag
.pages
[0][2] << 16) + (tag
.pages
[0][1] << 8) + tag
.pages
[0][0]),
435 REV32((rx
[3] << 24) + (rx
[2] << 16) + (rx
[1] << 8) + rx
[0])
437 Dbprintf(",{0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}",
438 rx
[0], rx
[1], rx
[2], rx
[3], rx
[4], rx
[5], rx
[6], rx
[7]);
440 hitagS_set_frame_modulation();
442 for (int i
= 0; i
< 4; i
++)
443 _hitag2_byte(&state
);
445 //send con2, pwdh0, pwdl0, pwdl1 encrypted as a response
446 tx
[0] = _hitag2_byte(&state
) ^ tag
.pages
[1][2];
447 tx
[1] = _hitag2_byte(&state
) ^ tag
.pwdh0
;
448 tx
[2] = _hitag2_byte(&state
) ^ tag
.pwdl0
;
449 tx
[3] = _hitag2_byte(&state
) ^ tag
.pwdl1
;
450 if (tag
.mode
!= HT_STANDARD
) {
454 calc_crc(&crc
, tag
.pages
[1][2], 8);
455 calc_crc(&crc
, tag
.pwdh0
, 8);
456 calc_crc(&crc
, tag
.pwdl0
, 8);
457 calc_crc(&crc
, tag
.pwdl1
, 8);
458 tx
[4] = (crc
^ _hitag2_byte(&state
));
461 * some readers do not allow to authenticate multiple times in a row with the same tag.
462 * use this to change the uid between authentications.
464 if (temp2 % 2 == 0) {
465 tag.uid = 0x11223344;
466 tag.pages[0][0] = 0x11;
467 tag.pages[0][1] = 0x22;
468 tag.pages[0][2] = 0x33;
469 tag.pages[0][3] = 0x44;
471 tag.uid = 0x55667788;
472 tag.pages[0][0] = 0x55;
473 tag.pages[0][1] = 0x66;
474 tag.pages[0][2] = 0x77;
475 tag.pages[0][3] = 0x88;
481 if (DBGLEVEL
>= DBG_EXTENDED
)
483 //data received to be written
484 if (tag
.tstate
== HT_WRITING_PAGE_DATA
) {
485 tag
.tstate
= HT_NO_OP
;
486 tag
.pages
[page_to_be_written
][0] = rx
[0];
487 tag
.pages
[page_to_be_written
][1] = rx
[1];
488 tag
.pages
[page_to_be_written
][2] = rx
[2];
489 tag
.pages
[page_to_be_written
][3] = rx
[3];
493 page_to_be_written
= 0;
494 hitagS_set_frame_modulation();
495 } else if (tag
.tstate
== HT_WRITING_BLOCK_DATA
) {
496 tag
.pages
[page_to_be_written
][0] = rx
[0];
497 tag
.pages
[page_to_be_written
][1] = rx
[1];
498 tag
.pages
[page_to_be_written
][2] = rx
[2];
499 tag
.pages
[page_to_be_written
][3] = rx
[3];
503 hitagS_set_frame_modulation();
504 page_to_be_written
++;
506 if (block_data_left
== 0) {
507 tag
.tstate
= HT_NO_OP
;
508 page_to_be_written
= 0;
513 //write page, write block, read page or read block command received
514 if ((rx
[0] & 0xf0) == 0xc0) { //read page
516 uint8_t page
= ((rx
[0] & 0x0f) * 16) + ((rx
[1] & 0xf0) / 16);
518 tx
[0] = tag
.pages
[page
][0];
519 tx
[1] = tag
.pages
[page
][1];
520 tx
[2] = tag
.pages
[page
][2];
521 tx
[3] = tag
.pages
[page
][3];
522 if (tag
.LKP
&& page
== 1)
525 hitagS_set_frame_modulation();
527 if (tag
.mode
!= HT_STANDARD
) {
531 for (int i
= 0; i
< 4; i
++)
532 calc_crc(&crc
, tx
[i
], 8);
536 if (tag
.LKP
&& (page
== 2 || page
== 3)) {
537 //if reader asks for key or password and the LKP-mark is set do not respond
541 } else if ((rx
[0] & 0xf0) == 0xd0) { //read block
542 uint8_t page
= ((rx
[0] & 0x0f) * 16) + ((rx
[1] & 0xf0) / 16);
544 //send page,...,page+3 data
545 for (int i
= 0; i
< 4; i
++) {
546 tx
[0 + i
* 4] = tag
.pages
[page
+ 0 + i
* 4][0];
547 tx
[1 + i
* 4] = tag
.pages
[page
+ 1 + i
* 4][1];
548 tx
[2 + i
* 4] = tag
.pages
[page
+ 2 + i
* 4][2];
549 tx
[3 + i
* 4] = tag
.pages
[page
+ 3 + i
* 4][3];
552 hitagS_set_frame_modulation();
554 if (tag
.mode
!= HT_STANDARD
) {
558 for (int i
= 0; i
< 16; i
++)
559 calc_crc(&crc
, tx
[i
], 8);
563 if ((page
) % 4 != 0 || (tag
.LKP
&& (page
) == 0)) {
567 } else if ((rx
[0] & 0xf0) == 0x80) { //write page
568 uint8_t page
= ((rx
[0] & 0x0f) * 16) + ((rx
[1] & 0xf0) / 16);
569 if ((tag
.LCON
&& page
== 1)
570 || (tag
.LKP
&& (page
== 2 || page
== 3))) {
577 page_to_be_written
= page
;
578 tag
.tstate
= HT_WRITING_PAGE_DATA
;
581 } else if ((rx
[0] & 0xf0) == 0x90) { //write block
582 uint8_t page
= ((rx
[0] & 0x0f) * 6) + ((rx
[1] & 0xf0) / 16);
583 hitagS_set_frame_modulation();
584 if (page
% 4 != 0 || page
== 0) {
591 page_to_be_written
= page
;
593 tag
.tstate
= HT_WRITING_BLOCK_DATA
;
599 if (DBGLEVEL
>= DBG_EXTENDED
)
600 Dbprintf("unknown rxlen: (%i) %02X %02X %02X %02X ...", rxlen
, rx
[0], rx
[1], rx
[2], rx
[3]);
606 * to autenticate to a tag with the given key or challenge
608 static int hitagS_handle_tag_auth(hitag_function htf
, uint64_t key
, uint64_t NrAr
, uint8_t *rx
, const size_t rxlen
, uint8_t *tx
, size_t *txlen
) {
609 uint8_t rx_air
[HITAG_FRAME_LEN
];
610 int response_bit
[200];
611 unsigned char mask
= 1;
612 unsigned char uid
[32];
616 uint8_t conf_pages
[3];
617 memcpy(rx_air
, rx
, nbytes(rxlen
));
620 if (tag
.pstate
== HT_READY
&& rxlen
>= 67) {
623 Dbprintf("authentication failed!");
627 for (int i
= 0; i
< 10; i
++) {
628 for (int j
= 0; j
< 8; j
++) {
630 if ((rx
[i
] & ((mask
<< 7) >> j
)) != 0)
636 for (int i
= 5; i
< z
; i
+= 2) {
637 uid
[k
] = response_bit
[i
];
642 uint8_t uid1
= (uid
[0] << 7)
651 uint8_t uid2
= (uid
[8] << 7)
660 uint8_t uid3
= (uid
[16] << 7)
669 uint8_t uid4
= (uid
[24] << 7)
678 if (DBGLEVEL
>= DBG_EXTENDED
)
679 Dbprintf("UID: %02X %02X %02X %02X", uid1
, uid2
, uid3
, uid4
);
681 tag
.uid
= (uid4
<< 24 | uid3
<< 16 | uid2
<< 8 | uid1
);
686 calc_crc(&crc
, 0x00, 5);
687 calc_crc(&crc
, uid1
, 8);
688 calc_crc(&crc
, uid2
, 8);
689 calc_crc(&crc
, uid3
, 8);
690 calc_crc(&crc
, uid4
, 8);
692 for (int i
= 0; i
< 100; i
++) {
696 for (int i
= 0; i
< 5; i
++) {
701 for (; i
< 37; i
++) {
702 response_bit
[i
] = uid
[i
- 5];
705 for (int j
= 0; j
< 8; j
++) {
707 if ((crc
& ((mask
<< 7) >> j
)) != 0)
713 for (int i
= 0; i
< 6; i
++) {
714 tx
[i
] = (response_bit
[k
] << 7)
715 | (response_bit
[k
+ 1] << 6)
716 | (response_bit
[k
+ 2] << 5)
717 | (response_bit
[k
+ 3] << 4)
718 | (response_bit
[k
+ 4] << 3)
719 | (response_bit
[k
+ 5] << 2)
720 | (response_bit
[k
+ 6] << 1)
721 | response_bit
[k
+ 7];
726 tag
.pstate
= HT_INIT
;
727 } else if (tag
.pstate
== HT_INIT
&& rxlen
== 44) {
728 // received configuration after select command
730 for (int i
= 0; i
< 6; i
++) {
731 for (int j
= 0; j
< 8; j
++) {
733 if ((rx
[i
] & ((mask
<< 7) >> j
)) != 0)
738 conf_pages
[0] = ((response_bit
[4] << 7) | (response_bit
[5] << 6)
739 | (response_bit
[6] << 5) | (response_bit
[7] << 4)
740 | (response_bit
[8] << 3) | (response_bit
[9] << 2)
741 | (response_bit
[10] << 1) | response_bit
[11]);
742 //check wich memorysize this tag has
743 if (response_bit
[10] == 0 && response_bit
[11] == 0)
744 tag
.max_page
= 32 / 32;
745 if (response_bit
[10] == 0 && response_bit
[11] == 1)
746 tag
.max_page
= 256 / 32;
747 if (response_bit
[10] == 1 && response_bit
[11] == 0)
748 tag
.max_page
= 2048 / 32;
749 conf_pages
[1] = ((response_bit
[12] << 7) | (response_bit
[13] << 6)
750 | (response_bit
[14] << 5) | (response_bit
[15] << 4)
751 | (response_bit
[16] << 3) | (response_bit
[17] << 2)
752 | (response_bit
[18] << 1) | response_bit
[19]);
753 tag
.auth
= response_bit
[12];
754 tag
.TTFC
= response_bit
[13];
755 //tag.TTFDR in response_bit[14] and response_bit[15]
756 //tag.TTFM in response_bit[16] and response_bit[17]
757 tag
.LCON
= response_bit
[18];
758 tag
.LKP
= response_bit
[19];
759 conf_pages
[2] = ((response_bit
[20] << 7) | (response_bit
[21] << 6)
760 | (response_bit
[22] << 5) | (response_bit
[23] << 4)
761 | (response_bit
[24] << 3) | (response_bit
[25] << 2)
762 | (response_bit
[26] << 1) | response_bit
[27]);
763 tag
.LCK7
= response_bit
[20];
764 tag
.LCK6
= response_bit
[21];
765 tag
.LCK5
= response_bit
[22];
766 tag
.LCK4
= response_bit
[23];
767 tag
.LCK3
= response_bit
[24];
768 tag
.LCK2
= response_bit
[25];
769 tag
.LCK1
= response_bit
[26];
770 tag
.LCK0
= response_bit
[27];
772 if (DBGLEVEL
>= DBG_EXTENDED
)
773 Dbprintf("conf0: %02X conf1: %02X conf2: %02X", conf_pages
[0], conf_pages
[1], conf_pages
[2]);
776 //if the tag is in authentication mode try the key or challenge
779 if (htf
== 02 || htf
== 04) { //RHTS_KEY //WHTS_KEY
780 state
= _hitag2_init(REV64(key
), REV32(tag
.uid
), REV32(rnd
));
782 for (int i
= 0; i
< 4; i
++) {
783 auth_ks
[i
] = _hitag2_byte(&state
) ^ 0xff;
787 tx
[1] = (rnd
>> 8) & 0xff;
788 tx
[2] = (rnd
>> 16) & 0xff;
789 tx
[3] = (rnd
>> 24) & 0xff;
795 if (DBGLEVEL
>= DBG_EXTENDED
)
796 Dbprintf("%02X %02X %02X %02X %02X %02X %02X %02X", tx
[0],
797 tx
[1], tx
[2], tx
[3], tx
[4], tx
[5], tx
[6], tx
[7]);
798 } else if (htf
== 01 || htf
== 03) { //RHTS_CHALLENGE //WHTS_CHALLENGE
799 for (int i
= 0; i
< 8; i
++)
800 tx
[i
] = ((NrAr
>> (56 - (i
* 8))) & 0xff);
803 tag
.pstate
= HT_AUTHENTICATE
;
805 Dbprintf("authentication failed!");
808 } else if (tag
.auth
== 0) {
809 tag
.pstate
= HT_SELECTED
;
812 } else if (tag
.pstate
== HT_AUTHENTICATE
&& rxlen
== 44) {
813 //encrypted con2,password received.
815 calc_crc(&crc
, 0x80, 1);
816 calc_crc(&crc
, ((rx
[0] & 0x0f) * 16 + ((rx
[1] & 0xf0) / 16)), 8);
817 calc_crc(&crc
, ((rx
[1] & 0x0f) * 16 + ((rx
[2] & 0xf0) / 16)), 8);
818 calc_crc(&crc
, ((rx
[2] & 0x0f) * 16 + ((rx
[3] & 0xf0) / 16)), 8);
819 calc_crc(&crc
, ((rx
[3] & 0x0f) * 16 + ((rx
[4] & 0xf0) / 16)), 8);
820 if (DBGLEVEL
>= DBG_EXTENDED
) {
821 Dbprintf("UID:::%X", tag
.uid
);
822 Dbprintf("RND:::%X", rnd
);
829 if (htf
== 02 || htf
== 04) { //RHTS_KEY //WHTS_KEY
831 state
= _hitag2_init(REV64(key
), REV32(tag
.uid
), REV32(rnd
));
832 for (int i
= 0; i
< 5; i
++)
833 _hitag2_byte(&state
);
835 pwdh0
= ((rx
[1] & 0x0f) * 16 + ((rx
[2] & 0xf0) / 16)) ^ _hitag2_byte(&state
);
836 pwdl0
= ((rx
[2] & 0x0f) * 16 + ((rx
[3] & 0xf0) / 16)) ^ _hitag2_byte(&state
);
837 pwdl1
= ((rx
[3] & 0x0f) * 16 + ((rx
[4] & 0xf0) / 16)) ^ _hitag2_byte(&state
);
840 if (DBGLEVEL
>= DBG_EXTENDED
)
841 Dbprintf("pwdh0 %02X pwdl0 %02X pwdl1 %02X", pwdh0
, pwdl0
, pwdl1
);
843 //Dbprintf("%X %02X", rnd, ((rx[4] & 0x0f) * 16) + ((rx[5] & 0xf0) / 16));
846 tag
.pstate
= HT_SELECTED
; //tag is now ready for read/write commands
853 * Emulates a Hitag S Tag with the given data from the .hts file
855 void SimulateHitagSTag(bool tag_mem_supplied
, uint8_t *data
) {
859 // int frame_count = 0;
860 int response
= 0, overflow
= 0;
862 uint8_t rx
[HITAG_FRAME_LEN
];
865 uint8_t txbuf
[HITAG_FRAME_LEN
];
869 // Reset the received frame, frame count and timing info
870 memset(rx
, 0x00, sizeof(rx
));
872 // free eventually allocated BigBuf memory
874 BigBuf_Clear_ext(false);
876 // Clean up trace and prepare it for storing frames
880 DbpString("Starting HitagS simulation");
883 tag
.pstate
= HT_READY
;
884 tag
.tstate
= HT_NO_OP
;
886 // read tag data into memory
887 if (tag_mem_supplied
) {
888 for (i
= 0; i
< 16; i
++)
889 for (j
= 0; j
< 4; j
++)
890 tag
.pages
[i
][j
] = 0x0;
892 DbpString("Loading hitagS memory...");
893 memcpy((uint8_t *)tag
.pages
, data
, 4 * 64);
895 // use the last read tag
898 tag
.uid
= ((tag
.pages
[0][3]) << 24) | ((tag
.pages
[0][2]) << 16) | ((tag
.pages
[0][1]) << 8) | tag
.pages
[0][0];
899 tag
.key
= (((uint64_t)tag
.pages
[3][3]) << 40) |
900 (((uint64_t)tag
.pages
[3][2]) << 32) |
901 (((uint64_t)tag
.pages
[3][1]) << 24) |
902 (((uint64_t)tag
.pages
[3][0]) << 16) |
903 (((uint64_t)tag
.pages
[2][3]) << 8) |
904 (((uint64_t)tag
.pages
[2][2]));
905 tag
.pwdl0
= tag
.pages
[2][0];
906 tag
.pwdl1
= tag
.pages
[2][1];
907 tag
.pwdh0
= tag
.pages
[1][3];
910 if ((tag
.pages
[1][0] & 0x2) == 0 && (tag
.pages
[1][0] & 0x1) == 1)
912 if ((tag
.pages
[1][0] & 0x2) == 0 && (tag
.pages
[1][0] & 0x1) == 0)
914 if (DBGLEVEL
>= DBG_EXTENDED
)
915 for (i
= 0; i
< tag
.max_page
; i
++)
916 Dbprintf("Page[%2d]: %02X %02X %02X %02X", i
,
917 (tag
.pages
[i
][3]) & 0xff,
918 (tag
.pages
[i
][2]) & 0xff,
919 (tag
.pages
[i
][1]) & 0xff,
920 tag
.pages
[i
][0] & 0xff);
923 if ((tag
.pages
[1][1] & 0x80) == 0x80)
926 if ((tag
.pages
[1][1] & 0x2) == 0x02)
929 if ((tag
.pages
[1][1] & 0x1) == 0x01)
932 //0=read write 1=read only
934 if ((tag
.pages
[1][2] & 0x80) == 0x80)
937 if ((tag
.pages
[1][2] & 0x40) == 0x040)
940 if ((tag
.pages
[1][2] & 0x20) == 0x20)
943 if ((tag
.pages
[1][2] & 0x10) == 0x10)
946 if ((tag
.pages
[1][2] & 0x8) == 0x08)
949 if ((tag
.pages
[1][2] & 0x4) == 0x04)
952 if ((tag
.pages
[1][2] & 0x2) == 0x02)
955 if ((tag
.pages
[1][2] & 0x1) == 0x01)
959 // Set up simulator mode, frequency divisor which will drive the FPGA
960 // and analog mux selection.
961 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
962 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
963 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
); //125kHz
964 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
966 // Configure output pin that is connected to the FPGA (for modulating)
967 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
968 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
970 // Disable modulation at default, which means release resistance
973 // Enable Peripheral Clock for
974 // TIMER_CLOCK0, used to measure exact timing before answering
975 // TIMER_CLOCK1, used to capture edges of the tag frames
976 AT91C_BASE_PMC
->PMC_PCER
|= (1 << AT91C_ID_TC0
) | (1 << AT91C_ID_TC1
);
978 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
980 // Disable timer during configuration
981 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
982 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
984 // TC0: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), no triggers
985 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
;
987 // TC1: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
988 // external trigger rising edge, load RA on rising edge of TIOA.
989 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
990 | AT91C_TC_ETRGEDG_RISING
| AT91C_TC_ABETRG
| AT91C_TC_LDRA_RISING
;
992 // Enable and reset counter
993 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
994 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
996 // synchronized startup procedure
997 while (AT91C_BASE_TC0
->TC_CV
> 0); // wait until TC0 returned to zero
999 while (!BUTTON_PRESS() && !data_available()) {
1003 // Receive frame, watch for at most T0*EOF periods
1004 while (AT91C_BASE_TC1
->TC_CV
< T0
* HITAG_T_EOF
) {
1005 // Check if rising edge in modulation is detected
1006 if (AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
1007 // Retrieve the new timing values
1008 int ra
= (AT91C_BASE_TC1
->TC_RA
/ T0
) + overflow
;
1011 // Reset timer every frame, we have to capture the last edge for timing
1012 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1016 // Capture reader frame
1017 if (ra
>= HITAG_T_STOP
) {
1019 //DbpString("wierd0?");
1021 // Capture the T0 periods that have passed since last communication or field drop (reset)
1022 response
= (ra
- HITAG_T_LOW
);
1023 } else if (ra
>= HITAG_T_1_MIN
) {
1025 rx
[rxlen
/ 8] |= 1 << (7 - (rxlen
% 8));
1027 } else if (ra
>= HITAG_T_0_MIN
) {
1029 rx
[rxlen
/ 8] |= 0 << (7 - (rxlen
% 8));
1032 // Ignore wierd value, is to small to mean anything
1037 // Check if frame was captured
1040 LogTrace(rx
, nbytes(rxlen
), response
, response
, NULL
, true);
1042 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1043 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1045 // Process the incoming frame (rx) and prepare the outgoing frame (tx)
1046 hitagS_handle_reader_command(rx
, rxlen
, tx
, &txlen
);
1048 // Wait for HITAG_T_WAIT_1 carrier periods after the last reader bit,
1049 // not that since the clock counts since the rising edge, but T_Wait1 is
1050 // with respect to the falling edge, we need to wait actually (T_Wait1 - T_Low)
1051 // periods. The gap time T_Low varies (4..10). All timer values are in
1052 // terms of T0 units
1053 while (AT91C_BASE_TC0
->TC_CV
< T0
* (HITAG_T_WAIT_1
- HITAG_T_LOW
)) {};
1055 // Send and store the tag answer (if there is any)
1057 // Transmit the tag frame
1058 hitag_send_frame(tx
, txlen
);
1059 LogTrace(tx
, nbytes(txlen
), 0, 0, NULL
, false);
1062 // Enable and reset external trigger in timer for capturing future frames
1063 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1065 // Reset the received frame and response timing info
1066 memset(rx
, 0x00, sizeof(rx
));
1071 // Reset the frame length
1073 // Save the timer overflow, will be 0 when frame was received
1074 overflow
+= (AT91C_BASE_TC1
->TC_CV
/ T0
);
1075 // Reset the timer to restart while-loop that receives frames
1076 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_SWTRG
;
1082 // release allocated memory from BigBuff.
1085 DbpString("Sim Stopped");
1088 static void hitagS_receive_frame(uint8_t *rx
, size_t *rxlen
, int *response
) {
1090 // Reset values for receiving frames
1091 memset(rx
, 0x00, HITAG_FRAME_LEN
* sizeof(uint8_t));
1097 uint32_t errorCount
= 0;
1099 // Receive frame, watch for at most T0*EOF periods
1100 while (AT91C_BASE_TC1
->TC_CV
< T0
* HITAG_T_WAIT_MAX
) {
1101 // Check if falling edge in tag modulation is detected
1102 if (AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
1103 // Retrieve the new timing values
1104 int ra
= (AT91C_BASE_TC1
->TC_RA
/ T0
);
1106 // Reset timer every frame, we have to capture the last edge for timing
1107 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
1111 // Capture tag frame (manchester decoding using only falling edges)
1112 if (ra
>= HITAG_T_EOF
) {
1114 //DbpString("wierd1?");
1116 // Capture the T0 periods that have passed since last communication or field drop (reset)
1117 // We always recieve a 'one' first, which has the falling edge after a half period |-_|
1118 *response
= ra
- HITAG_T_TAG_HALF_PERIOD
;
1119 } else if (ra
>= HITAG_T_TAG_CAPTURE_FOUR_HALF
) {
1120 // Manchester coding example |-_|_-|-_| (101)
1121 rx
[(*rxlen
) / 8] |= 0 << (7 - ((*rxlen
) % 8));
1123 rx
[(*rxlen
) / 8] |= 1 << (7 - ((*rxlen
) % 8));
1125 } else if (ra
>= HITAG_T_TAG_CAPTURE_THREE_HALF
) {
1126 // Manchester coding example |_-|...|_-|-_| (0...01)
1127 rx
[(*rxlen
) / 8] |= 0 << (7 - ((*rxlen
) % 8));
1129 // We have to skip this half period at start and add the 'one' the second time
1131 rx
[(*rxlen
) / 8] |= 1 << (7 - ((*rxlen
) % 8));
1136 } else if (ra
>= HITAG_T_TAG_CAPTURE_TWO_HALF
) {
1137 // Manchester coding example |_-|_-| (00) or |-_|-_| (11)
1139 // Ignore bits that are transmitted during SOF
1142 // bit is same as last bit
1143 rx
[(*rxlen
) / 8] |= lastbit
<< (7 - ((*rxlen
) % 8));
1147 // Ignore wierd value, is to small to mean anything
1152 // if we saw over 100 wierd values break it probably isn't hitag...
1153 if (errorCount
> 100) break;
1155 // We can break this loop if we received the last bit from a frame
1156 if (AT91C_BASE_TC1
->TC_CV
> T0
* HITAG_T_EOF
) {
1164 * Authenticates to the Tag with the given key or challenge.
1165 * If the key was given the password will be decrypted.
1166 * Reads every page of a hitag S transpoder.
1168 void ReadHitagS(hitag_function htf
, hitag_data
*htd
) {
1173 // int frame_count = 0;
1175 int response_bit
[200];
1176 uint8_t rx
[HITAG_FRAME_LEN
];
1178 uint8_t txbuf
[HITAG_FRAME_LEN
];
1179 uint8_t *tx
= txbuf
;
1182 int t_wait
= HITAG_T_WAIT_MAX
;
1185 unsigned char mask
= 1;
1187 unsigned char pageData
[32];
1188 page_to_be_written
= 0;
1190 //read given key/challenge
1196 tag
.pstate
= HT_READY
;
1197 tag
.tstate
= HT_NO_OP
;
1200 case RHTSF_CHALLENGE
: {
1201 DbpString("Authenticating using nr,ar pair:");
1202 memcpy(NrAr_
, htd
->auth
.NrAr
, 8);
1203 Dbhexdump(8, NrAr_
, false);
1204 NrAr
= NrAr_
[7] | ((uint64_t)NrAr_
[6]) << 8 | ((uint64_t)NrAr_
[5]) << 16 | ((uint64_t)NrAr_
[4]) << 24 | ((uint64_t)NrAr_
[3]) << 32 |
1205 ((uint64_t)NrAr_
[2]) << 40 | ((uint64_t)NrAr_
[1]) << 48 | ((uint64_t)NrAr_
[0]) << 56;
1209 DbpString("Authenticating using key:");
1210 memcpy(key_
, htd
->crypto
.key
, 6);
1211 Dbhexdump(6, key_
, false);
1212 key
= key_
[5] | ((uint64_t)key_
[4]) << 8 | ((uint64_t)key_
[3]) << 16 | ((uint64_t)key_
[2]) << 24 | ((uint64_t)key_
[1]) << 32 | ((uint64_t)key_
[0]) << 40;
1216 Dbprintf("Error , unknown function: %d", htf
);
1221 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1222 // Reset the return status
1223 bSuccessful
= false;
1225 // Clean up trace and prepare it for storing frames
1233 // Set fpga in edge detect with reader field, we can modulate as reader now
1234 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_READER_FIELD
);
1235 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
); //125kHz
1236 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1238 // Configure output and enable pin that is connected to the FPGA (for modulating)
1239 AT91C_BASE_PIOA
->PIO_OER
|= GPIO_SSC_DOUT
;
1240 AT91C_BASE_PIOA
->PIO_PER
|= GPIO_SSC_DOUT
;
1242 // Disable modulation at default, which means enable the field
1245 // Enable Peripheral Clock for
1246 // TIMER_CLOCK0, used to measure exact timing before answering
1247 // TIMER_CLOCK1, used to capture edges of the tag frames
1248 AT91C_BASE_PMC
->PMC_PCER
|= (1 << AT91C_ID_TC0
) | (1 << AT91C_ID_TC1
);
1250 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1252 // Disable timer during configuration
1253 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1254 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1256 // TC0: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), no triggers
1257 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
;
1259 // TC1: Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1260 // external trigger rising edge, load RA on falling edge of TIOA.
1261 AT91C_BASE_TC1
->TC_CMR
=
1262 AT91C_TC_CLKS_TIMER_DIV1_CLOCK
|
1263 AT91C_TC_ETRGEDG_FALLING
|
1265 AT91C_TC_LDRA_FALLING
;
1267 // Enable and reset counters
1268 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1269 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1271 // synchronized startup procedure
1272 while (AT91C_BASE_TC0
->TC_CV
> 0); // wait until TC0 returned to zero
1273 // Reset the received frame, frame count and timing info
1275 while (!bStop
&& !BUTTON_PRESS() && !data_available()) {
1279 // Check if frame was captured and store it
1282 LogTrace(rx
, nbytes(rxlen
), response
, response
, NULL
, false);
1285 // By default reset the transmission buffer
1290 //start authentication
1292 memcpy(tx
, "\xC0", nbytes(txlen
));
1293 tag
.pstate
= HT_READY
;
1294 tag
.tstate
= HT_NO_OP
;
1295 } else if (tag
.pstate
!= HT_SELECTED
) {
1296 if (hitagS_handle_tag_auth(htf
, key
, NrAr
, rx
, rxlen
, tx
, &txlen
) == -1)
1300 if (tag
.pstate
== HT_SELECTED
&& tag
.tstate
== HT_NO_OP
&& rxlen
> 0) {
1302 tag
.tstate
= HT_READING_PAGE
;
1305 tx
[0] = 0xc0 + (pageNum
/ 16);
1306 calc_crc(&crc
, tx
[0], 8);
1307 calc_crc(&crc
, 0x00 + ((pageNum
% 16) * 16), 4);
1308 tx
[1] = 0x00 + ((pageNum
% 16) * 16) + (crc
/ 16);
1309 tx
[2] = 0x00 + (crc
% 16) * 16;
1310 } else if (tag
.pstate
== HT_SELECTED
1311 && tag
.tstate
== HT_READING_PAGE
1313 //save received data - 40 bits
1315 for (i
= 0; i
< 5; i
++) {
1316 for (j
= 0; j
< 8; j
++) {
1317 response_bit
[z
] = 0;
1318 if ((rx
[i
] & ((mask
<< 7) >> j
)) != 0)
1319 response_bit
[z
] = 1;
1324 for (i
= 4; i
< 36; i
++) { // ignore first 4 bits: SOF (actualy 1 or 6 depending on response protocol)
1325 pageData
[k
] = response_bit
[i
];
1328 for (i
= 0; i
< 4; i
++) // set page bytes to 0
1329 tag
.pages
[pageNum
][i
] = 0x0;
1330 for (i
= 0; i
< 4; i
++) { // set page bytes from recieved bits
1331 tag
.pages
[pageNum
][i
] += ((pageData
[i
* 8] << 7)
1332 | (pageData
[1 + (i
* 8)] << 6)
1333 | (pageData
[2 + (i
* 8)] << 5)
1334 | (pageData
[3 + (i
* 8)] << 4)
1335 | (pageData
[4 + (i
* 8)] << 3)
1336 | (pageData
[5 + (i
* 8)] << 2)
1337 | (pageData
[6 + (i
* 8)] << 1)
1338 | pageData
[7 + (i
* 8)]);
1340 if (tag
.auth
&& tag
.LKP
&& pageNum
== 1) {
1341 Dbprintf("Page[%2d]: %02X %02X %02X %02X", pageNum
, pwdh0
,
1342 (tag
.pages
[pageNum
][2]) & 0xff,
1343 (tag
.pages
[pageNum
][1]) & 0xff,
1344 tag
.pages
[pageNum
][0] & 0xff);
1346 Dbprintf("Page[%2d]: %02X %02X %02X %02X", pageNum
,
1347 (tag
.pages
[pageNum
][3]) & 0xff,
1348 (tag
.pages
[pageNum
][2]) & 0xff,
1349 (tag
.pages
[pageNum
][1]) & 0xff,
1350 tag
.pages
[pageNum
][0] & 0xff);
1354 //display key and password if possible
1355 if (pageNum
== 2 && tag
.auth
== 1 && tag
.LKP
) {
1356 if (htf
== RHTSF_KEY
) {
1357 Dbprintf("Page[ 2]: %02X %02X %02X %02X",
1358 (uint8_t)(key
>> 8) & 0xff,
1359 (uint8_t) key
& 0xff,
1363 Dbprintf("Page[ 3]: %02X %02X %02X %02X",
1364 (uint8_t)(key
>> 40) & 0xff,
1365 (uint8_t)(key
>> 32) & 0xff,
1366 (uint8_t)(key
>> 24) & 0xff,
1367 (uint8_t)(key
>> 16) & 0xff
1370 //if the authentication is done with a challenge the key and password are unknown
1371 Dbprintf("Page[ 2]: __ __ __ __");
1372 Dbprintf("Page[ 3]: __ __ __ __");
1374 // since page 2+3 are not accessible when LKP == 1 and AUT == 1 fastforward to next readable page
1380 tx
[0] = 0xc0 + (pageNum
/ 16);
1381 calc_crc(&crc
, tx
[0], 8);
1382 calc_crc(&crc
, 0x00 + ((pageNum
% 16) * 16), 4);
1383 tx
[1] = 0x00 + ((pageNum
% 16) * 16) + (crc
/ 16);
1384 tx
[2] = 0x00 + (crc
% 16) * 16;
1385 if (pageNum
>= tag
.max_page
) {
1390 // Send and store the reader command
1391 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1392 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1394 // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting,
1395 // Since the clock counts since the last falling edge, a 'one' means that the
1396 // falling edge occurred halfway the period. with respect to this falling edge,
1397 // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
1398 // All timer values are in terms of T0 units
1400 while (AT91C_BASE_TC0
->TC_CV
< T0
* (t_wait
+ (HITAG_T_TAG_HALF_PERIOD
* lastbit
))) {};
1402 // Transmit the reader frame
1403 hitag_reader_send_frame(tx
, txlen
);
1405 // Enable and reset external trigger in timer for capturing future frames
1406 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1408 // Add transmitted frame to total count
1411 LogTrace(tx
, nbytes(txlen
), HITAG_T_WAIT_2
, HITAG_T_WAIT_2
, NULL
, true);
1414 hitagS_receive_frame(rx
, &rxlen
, &response
);
1420 reply_mix(CMD_ACK
, bSuccessful
, 0, 0, 0, 0);
1424 * Authenticates to the Tag with the given Key or Challenge.
1425 * Writes the given 32Bit data into page_
1427 void WritePageHitagS(hitag_function htf
, hitag_data
*htd
, int page
) {
1431 // int frame_count = 0;
1433 uint8_t rx
[HITAG_FRAME_LEN
];
1435 uint8_t txbuf
[HITAG_FRAME_LEN
];
1436 uint8_t *tx
= txbuf
;
1439 int t_wait
= HITAG_T_WAIT_MAX
;
1442 uint8_t data
[4] = {0, 0, 0, 0};
1444 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1446 bSuccessful
= false;
1448 // Clean up trace and prepare it for storing frames
1452 //read given key/challenge, the page and the data
1458 case WHTSF_CHALLENGE
: {
1459 memcpy(data
, htd
->auth
.data
, 4);
1460 DbpString("Authenticating using nr,ar pair:");
1461 memcpy(NrAr_
, htd
->auth
.NrAr
, 8);
1462 Dbhexdump(8, NrAr_
, false);
1463 NrAr
= NrAr_
[7] | ((uint64_t)NrAr_
[6]) << 8 | ((uint64_t)NrAr_
[5]) << 16 | ((uint64_t)NrAr_
[4]) << 24 | ((uint64_t)NrAr_
[3]) << 32 |
1464 ((uint64_t)NrAr_
[2]) << 40 | ((uint64_t)NrAr_
[1]) << 48 | ((uint64_t)NrAr_
[0]) << 56;
1469 memcpy(data
, htd
->crypto
.data
, 4);
1470 DbpString("Authenticating using key:");
1471 memcpy(key_
, htd
->crypto
.key
, 6);
1472 Dbhexdump(6, key_
, false);
1473 key
= key_
[5] | ((uint64_t)key_
[4]) << 8 | ((uint64_t)key_
[3]) << 16 | ((uint64_t)key_
[2]) << 24 | ((uint64_t)key_
[1]) << 32 | ((uint64_t)key_
[0]) << 40;
1477 Dbprintf("Error , unknown function: %d", htf
);
1482 Dbprintf("Page: %d", page
);
1483 Dbprintf("DATA: %02X %02X %02X %02X", data
[0], data
[1], data
[2], data
[3]);
1485 tag
.pstate
= HT_READY
;
1486 tag
.tstate
= HT_NO_OP
;
1490 // Configure output and enable pin that is connected to the FPGA (for modulating)
1491 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
1492 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
1494 // Set fpga in edge detect with reader field, we can modulate as reader now
1495 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_READER_FIELD
);
1496 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
); //125kHz
1497 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1499 // Disable modulation at default, which means enable the field
1502 // Enable Peripheral Clock for
1503 // TIMER_CLOCK0, used to measure exact timing before answering
1504 // TIMER_CLOCK1, used to capture edges of the tag frames
1505 AT91C_BASE_PMC
->PMC_PCER
|= (1 << AT91C_ID_TC0
) | (1 << AT91C_ID_TC1
);
1507 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1509 // Disable timer during configuration
1510 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1511 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1513 // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1514 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
;
1515 // external trigger rising edge, load RA on falling edge of TIOA.
1516 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
1517 | AT91C_TC_ETRGEDG_FALLING
1519 | AT91C_TC_LDRA_FALLING
;
1521 // Enable and reset counters
1522 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1523 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1525 while (AT91C_BASE_TC0
->TC_CV
> 0);
1527 // Reset the received frame, frame count and timing info
1532 while (!bStop
&& !BUTTON_PRESS() && !data_available()) {
1536 // Check if frame was captured and store it
1539 LogTrace(rx
, nbytes(rxlen
), response
, response
, NULL
, false);
1542 //check for valid input
1545 "usage: lf hitag writer [--03 | --04] [--nrar CHALLENGE | -k KEY] [-p page] -d [4 hex bytes]");
1549 // By default reset the transmission buffer
1553 if (rxlen
== 0 && tag
.tstate
== HT_WRITING_PAGE_ACK
) {
1554 //no write access on this page
1555 Dbprintf("no write access on page %d", page
);
1557 } else if (rxlen
== 0 && tag
.tstate
!= HT_WRITING_PAGE_DATA
) {
1558 //start the authetication
1560 memcpy(tx
, "\xc0", nbytes(txlen
));
1561 tag
.pstate
= HT_READY
;
1562 tag
.tstate
= HT_NO_OP
;
1563 } else if (tag
.pstate
!= HT_SELECTED
) {
1564 //try to authenticate with the given key or challenge
1565 if (hitagS_handle_tag_auth(htf
, key
, NrAr
, rx
, rxlen
, tx
, &txlen
) == -1)
1568 if (tag
.pstate
== HT_SELECTED
&& tag
.tstate
== HT_NO_OP
&& rxlen
> 0) {
1569 //check if the given page exists
1570 if (page
> tag
.max_page
) {
1571 Dbprintf("page number too big");
1574 //ask Tag for write permission
1575 tag
.tstate
= HT_WRITING_PAGE_ACK
;
1578 tx
[0] = 0x90 + (page
/ 16);
1579 calc_crc(&crc
, tx
[0], 8);
1580 calc_crc(&crc
, 0x00 + ((page
% 16) * 16), 4);
1581 tx
[1] = 0x00 + ((page
% 16) * 16) + (crc
/ 16);
1582 tx
[2] = 0x00 + (crc
% 16) * 16;
1583 } else if (tag
.pstate
== HT_SELECTED
&& tag
.tstate
== HT_WRITING_PAGE_ACK
1584 && rxlen
== 6 && rx
[0] == 0xf4) {
1585 //ACK recieved to write the page. send data
1586 tag
.tstate
= HT_WRITING_PAGE_DATA
;
1589 calc_crc(&crc
, data
[3], 8);
1590 calc_crc(&crc
, data
[2], 8);
1591 calc_crc(&crc
, data
[1], 8);
1592 calc_crc(&crc
, data
[0], 8);
1598 } else if (tag
.pstate
== HT_SELECTED
&& tag
.tstate
== HT_WRITING_PAGE_DATA
1599 && rxlen
== 6 && rx
[0] == 0xf4) {
1601 Dbprintf("Successful!");
1605 // Send and store the reader command
1606 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1607 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1609 // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting,
1610 // Since the clock counts since the last falling edge, a 'one' means that the
1611 // falling edge occurred halfway the period. with respect to this falling edge,
1612 // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
1613 // All timer values are in terms of T0 units
1615 while (AT91C_BASE_TC0
->TC_CV
< T0
* (t_wait
+ (HITAG_T_TAG_HALF_PERIOD
* lastbit
))) {};
1617 // Transmit the reader frame
1618 hitag_reader_send_frame(tx
, txlen
);
1620 // Enable and reset external trigger in timer for capturing future frames
1621 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1623 // Add transmitted frame to total count
1626 LogTrace(tx
, nbytes(txlen
), HITAG_T_WAIT_2
, HITAG_T_WAIT_2
, NULL
, true);
1629 hitagS_receive_frame(rx
, &rxlen
, &response
);
1637 reply_mix(CMD_ACK
, bSuccessful
, 0, 0, 0, 0);
1641 * Tries to authenticate to a Hitag S Transponder with the given challenges from a .cc file.
1642 * Displays all Challenges that failed.
1643 * When collecting Challenges to break the key it is possible that some data
1644 * is not received correctly due to Antenna problems. This function
1645 * detects these challenges.
1647 void check_challenges(bool file_given
, uint8_t *data
) {
1649 // int frame_count = 0;
1651 uint8_t uid_byte
[4];
1652 uint8_t rx
[HITAG_FRAME_LEN
];
1653 uint8_t unlocker
[60][8];
1656 uint8_t txbuf
[HITAG_FRAME_LEN
];
1657 int t_wait
= HITAG_T_WAIT_MAX
;
1658 int lastbit
, STATE
= 0;
1660 int response_bit
[200];
1661 unsigned char mask
= 1;
1662 unsigned char uid
[32];
1665 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1666 // Reset the return status
1667 bSuccessful
= false;
1669 // Clean up trace and prepare it for storing frames
1677 // Configure output and enable pin that is connected to the FPGA (for modulating)
1678 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
1679 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
1681 // Set fpga in edge detect with reader field, we can modulate as reader now
1682 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_READER_FIELD
);
1683 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
); //125kHz
1684 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1686 // Disable modulation at default, which means enable the field
1689 // Enable Peripheral Clock for
1690 // TIMER_CLOCK0, used to measure exact timing before answering
1691 // TIMER_CLOCK1, used to capture edges of the tag frames
1692 AT91C_BASE_PMC
->PMC_PCER
|= (1 << AT91C_ID_TC0
) | (1 << AT91C_ID_TC1
);
1694 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1696 // Disable timer during configuration
1697 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1698 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1700 // TC0: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), no triggers
1701 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
;
1703 // TC1: Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1704 // external trigger rising edge, load RA on falling edge of TIOA.
1705 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
1706 | AT91C_TC_ETRGEDG_FALLING
1708 | AT91C_TC_LDRA_FALLING
;
1710 // Enable and reset counters
1711 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1712 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1714 while (AT91C_BASE_TC0
->TC_CV
> 0) {};
1716 // Reset the received frame, frame count and timing info
1722 DbpString("Loading challenges...");
1723 memcpy((uint8_t *)unlocker
, data
, 60 * 8);
1726 while (file_given
&& !bStop
&& !BUTTON_PRESS()) {
1730 // Check if frame was captured and store it
1733 LogTrace(rx
, nbytes(rxlen
), response
, response
, NULL
, false);
1736 uint8_t *tx
= txbuf
;
1741 Dbprintf("Challenge failed: %02X %02X %02X %02X %02X %02X %02X %02X",
1742 unlocker
[u1
- 1][0], unlocker
[u1
- 1][1],
1743 unlocker
[u1
- 1][2], unlocker
[u1
- 1][3],
1744 unlocker
[u1
- 1][4], unlocker
[u1
- 1][5],
1745 unlocker
[u1
- 1][6], unlocker
[u1
- 1][7]);
1748 //start new authentication
1749 memcpy(tx
, "\xC0", nbytes(txlen
));
1750 } else if (rxlen
>= 67 && STATE
== 0) {
1753 for (i
= 0; i
< 10; i
++) {
1754 for (j
= 0; j
< 8; j
++) {
1755 response_bit
[z
] = 0;
1756 if ((rx
[i
] & ((mask
<< 7) >> j
)) != 0)
1757 response_bit
[z
] = 1;
1762 for (i
= 5; i
< z
; i
+= 2) {
1763 uid
[k
] = response_bit
[i
];
1768 uid_byte
[0] = (uid
[0] << 7) | (uid
[1] << 6) | (uid
[2] << 5)
1769 | (uid
[3] << 4) | (uid
[4] << 3) | (uid
[5] << 2)
1770 | (uid
[6] << 1) | uid
[7];
1771 uid_byte
[1] = (uid
[8] << 7) | (uid
[9] << 6) | (uid
[10] << 5)
1772 | (uid
[11] << 4) | (uid
[12] << 3) | (uid
[13] << 2)
1773 | (uid
[14] << 1) | uid
[15];
1774 uid_byte
[2] = (uid
[16] << 7) | (uid
[17] << 6) | (uid
[18] << 5)
1775 | (uid
[19] << 4) | (uid
[20] << 3) | (uid
[21] << 2)
1776 | (uid
[22] << 1) | uid
[23];
1777 uid_byte
[3] = (uid
[24] << 7) | (uid
[25] << 6) | (uid
[26] << 5)
1778 | (uid
[27] << 4) | (uid
[28] << 3) | (uid
[29] << 2)
1779 | (uid
[30] << 1) | uid
[31];
1780 //Dbhexdump(10, rx, rxlen);
1784 calc_crc(&crc
, 0x00, 5);
1785 calc_crc(&crc
, uid_byte
[0], 8);
1786 calc_crc(&crc
, uid_byte
[1], 8);
1787 calc_crc(&crc
, uid_byte
[2], 8);
1788 calc_crc(&crc
, uid_byte
[3], 8);
1789 for (i
= 0; i
< 100; i
++) {
1790 response_bit
[i
] = 0;
1792 for (i
= 0; i
< 5; i
++) {
1793 response_bit
[i
] = 0;
1795 for (i
= 5; i
< 37; i
++) {
1796 response_bit
[i
] = uid
[i
- 5];
1798 for (j
= 0; j
< 8; j
++) {
1799 response_bit
[i
] = 0;
1800 if ((crc
& ((mask
<< 7) >> j
)) != 0)
1801 response_bit
[i
] = 1;
1805 for (i
= 0; i
< 6; i
++) {
1806 tx
[i
] = (response_bit
[k
] << 7) | (response_bit
[k
+ 1] << 6)
1807 | (response_bit
[k
+ 2] << 5)
1808 | (response_bit
[k
+ 3] << 4)
1809 | (response_bit
[k
+ 4] << 3)
1810 | (response_bit
[k
+ 5] << 2)
1811 | (response_bit
[k
+ 6] << 1) | response_bit
[k
+ 7];
1815 } else if (STATE
== 1 && rxlen
== 44) {
1816 //received configuration
1819 for (i
= 0; i
< 6; i
++) {
1820 for (j
= 0; j
< 8; j
++) {
1821 response_bit
[z
] = 0;
1822 if ((rx
[i
] & ((mask
<< 7) >> j
)) != 0)
1823 response_bit
[z
] = 1;
1829 if (u1
>= ARRAYLEN(unlocker
))
1831 for (i
= 0; i
< 8; i
++)
1832 tx
[i
] = unlocker
[u1
][i
];
1835 } else if (STATE
== 2 && rxlen
>= 44) {
1836 Dbprintf("Challenge success: %02X%02X%02X%02X %02X%02X%02X%02X",
1837 unlocker
[u1
- 1][0], unlocker
[u1
- 1][1],
1838 unlocker
[u1
- 1][2], unlocker
[u1
- 1][3],
1839 unlocker
[u1
- 1][4], unlocker
[u1
- 1][5],
1840 unlocker
[u1
- 1][6], unlocker
[u1
- 1][7]);
1844 // Send and store the reader command
1845 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1846 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1848 // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting,
1849 // Since the clock counts since the last falling edge, a 'one' means that the
1850 // falling edge occurred halfway the period. with respect to this falling edge,
1851 // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
1852 // All timer values are in terms of T0 units
1854 while (AT91C_BASE_TC0
->TC_CV
< T0
* (t_wait
+ (HITAG_T_TAG_HALF_PERIOD
* lastbit
))) {};
1856 // Transmit the reader frame
1857 hitag_reader_send_frame(tx
, txlen
);
1859 // Enable and reset external trigger in timer for capturing future frames
1860 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1862 // Add transmitted frame to total count
1865 LogTrace(tx
, nbytes(txlen
), HITAG_T_WAIT_2
, HITAG_T_WAIT_2
, NULL
, true);
1868 hitagS_receive_frame(rx
, &rxlen
, &response
);
1873 reply_mix(CMD_ACK
, bSuccessful
, 0, 0, 0, 0);