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 // Hitag2 emulation (preliminary test version)
8 // (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
9 //-----------------------------------------------------------------------------
10 // Hitag2 complete rewrite of the code
11 // - Fixed modulation/encoding issues
12 // - Rewrote code for transponder emulation
13 // - Added snooping of transponder communication
14 // - Added reader functionality
16 // (c) 2012 Roel Verdult
17 //-----------------------------------------------------------------------------
19 #include "proxmark3.h"
25 #include "fpgaloader.h"
30 static bool bAuthenticating
;
32 static bool bSuccessful
;
39 TAG_STATE_RESET
= 0x01, // Just powered up, awaiting GetSnr
40 TAG_STATE_ACTIVATING
= 0x02 , // In activation phase (password mode), sent UID, awaiting reader password
41 TAG_STATE_ACTIVATED
= 0x03, // Activation complete, awaiting read/write commands
42 TAG_STATE_WRITING
= 0x04, // In write command, awaiting sector contents to be written
44 unsigned int active_sector
;
47 byte_t sectors
[12][4];
50 static struct hitag2_tag tag
= {
51 .state
= TAG_STATE_RESET
,
52 .sectors
= { // Password mode: | Crypto mode:
53 [0] = { 0x02, 0x4e, 0x02, 0x20}, // UID | UID
54 [1] = { 0x4d, 0x49, 0x4b, 0x52}, // Password RWD | 32 bit LSB key
55 [2] = { 0x20, 0xf0, 0x4f, 0x4e}, // Reserved | 16 bit MSB key, 16 bit reserved
56 [3] = { 0x0e, 0xaa, 0x48, 0x54}, // Configuration, password TAG | Configuration, password TAG
57 [4] = { 0x46, 0x5f, 0x4f, 0x4b}, // Data: F_OK
58 [5] = { 0x55, 0x55, 0x55, 0x55}, // Data: UUUU
59 [6] = { 0xaa, 0xaa, 0xaa, 0xaa}, // Data: ....
60 [7] = { 0x55, 0x55, 0x55, 0x55}, // Data: UUUU
61 [8] = { 0x00, 0x00, 0x00, 0x00}, // RSK Low
62 [9] = { 0x00, 0x00, 0x00, 0x00}, // RSK High
63 [10] = { 0x00, 0x00, 0x00, 0x00}, // RCF
64 [11] = { 0x00, 0x00, 0x00, 0x00}, // SYNC
69 WRITE_STATE_START
= 0x0,
70 WRITE_STATE_PAGENUM_WRITTEN
,
75 // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces.
76 // Historically it used to be FREE_BUFFER_SIZE, which was 2744.
77 #define AUTH_TABLE_LENGTH 2744
78 static byte_t
* auth_table
;
79 static size_t auth_table_pos
= 0;
80 static size_t auth_table_len
= AUTH_TABLE_LENGTH
;
82 static byte_t password
[4];
83 static byte_t NrAr
[8];
85 static byte_t writedata
[4];
86 static uint64_t cipher_state
;
88 /* Following is a modified version of cryptolib.com/ciphers/hitag2/ */
89 // Software optimized 48-bit Philips/NXP Mifare Hitag2 PCF7936/46/47/52 stream cipher algorithm by I.C. Wiener 2006-2007.
90 // For educational purposes only.
91 // No warranties or guarantees of any kind.
92 // This code is released into the public domain by its author.
99 #define rev8(x) ((((x)>>7)&1)+((((x)>>6)&1)<<1)+((((x)>>5)&1)<<2)+((((x)>>4)&1)<<3)+((((x)>>3)&1)<<4)+((((x)>>2)&1)<<5)+((((x)>>1)&1)<<6)+(((x)&1)<<7))
100 #define rev16(x) (rev8 (x)+(rev8 (x>> 8)<< 8))
101 #define rev32(x) (rev16(x)+(rev16(x>>16)<<16))
102 #define rev64(x) (rev32(x)+(rev32(x>>32)<<32))
103 #define bit(x,n) (((x)>>(n))&1)
104 #define bit32(x,n) ((((x)[(n)>>5])>>((n)))&1)
105 #define inv32(x,i,n) ((x)[(i)>>5]^=((u32)(n))<<((i)&31))
106 #define rotl64(x, n) ((((u64)(x))<<((n)&63))+(((u64)(x))>>((0-(n))&63)))
108 // Single bit Hitag2 functions:
110 #define i4(x,a,b,c,d) ((u32)((((x)>>(a))&1)+(((x)>>(b))&1)*2+(((x)>>(c))&1)*4+(((x)>>(d))&1)*8))
112 static const u32 ht2_f4a
= 0x2C79; // 0010 1100 0111 1001
113 static const u32 ht2_f4b
= 0x6671; // 0110 0110 0111 0001
114 static const u32 ht2_f5c
= 0x7907287B; // 0111 1001 0000 0111 0010 1000 0111 1011
116 static u32
_f20 (const u64 x
)
120 i5
= ((ht2_f4a
>> i4 (x
, 1, 2, 4, 5)) & 1)* 1
121 + ((ht2_f4b
>> i4 (x
, 7,11,13,14)) & 1)* 2
122 + ((ht2_f4b
>> i4 (x
,16,20,22,25)) & 1)* 4
123 + ((ht2_f4b
>> i4 (x
,27,28,30,32)) & 1)* 8
124 + ((ht2_f4a
>> i4 (x
,33,42,43,45)) & 1)*16;
126 return (ht2_f5c
>> i5
) & 1;
129 static u64
_hitag2_init (const u64 key
, const u32 serial
, const u32 IV
)
132 u64 x
= ((key
& 0xFFFF) << 32) + serial
;
134 for (i
= 0; i
< 32; i
++)
137 x
+= (u64
) (_f20 (x
) ^ (((IV
>> i
) ^ (key
>> (i
+16))) & 1)) << 47;
142 static u64
_hitag2_round (u64
*state
)
147 ((((x
>> 0) ^ (x
>> 2) ^ (x
>> 3) ^ (x
>> 6)
148 ^ (x
>> 7) ^ (x
>> 8) ^ (x
>> 16) ^ (x
>> 22)
149 ^ (x
>> 23) ^ (x
>> 26) ^ (x
>> 30) ^ (x
>> 41)
150 ^ (x
>> 42) ^ (x
>> 43) ^ (x
>> 46) ^ (x
>> 47)) & 1) << 47);
156 static u32
_hitag2_byte (u64
* x
)
160 for (i
= 0, c
= 0; i
< 8; i
++) c
+= (u32
) _hitag2_round (x
) << (i
^7);
164 static int hitag2_reset(void)
166 tag
.state
= TAG_STATE_RESET
;
167 tag
.crypto_active
= 0;
171 static int hitag2_init(void)
173 // memcpy(&tag, &resetdata, sizeof(tag));
178 static void hitag2_cipher_reset(struct hitag2_tag
*tag
, const byte_t
*iv
)
180 uint64_t key
= ((uint64_t)tag
->sectors
[2][2]) |
181 ((uint64_t)tag
->sectors
[2][3] << 8) |
182 ((uint64_t)tag
->sectors
[1][0] << 16) |
183 ((uint64_t)tag
->sectors
[1][1] << 24) |
184 ((uint64_t)tag
->sectors
[1][2] << 32) |
185 ((uint64_t)tag
->sectors
[1][3] << 40);
186 uint32_t uid
= ((uint32_t)tag
->sectors
[0][0]) |
187 ((uint32_t)tag
->sectors
[0][1] << 8) |
188 ((uint32_t)tag
->sectors
[0][2] << 16) |
189 ((uint32_t)tag
->sectors
[0][3] << 24);
190 uint32_t iv_
= (((uint32_t)(iv
[0]))) |
191 (((uint32_t)(iv
[1])) << 8) |
192 (((uint32_t)(iv
[2])) << 16) |
193 (((uint32_t)(iv
[3])) << 24);
194 tag
->cs
= _hitag2_init(rev64(key
), rev32(uid
), rev32(iv_
));
197 static int hitag2_cipher_authenticate(uint64_t* cs
, const byte_t
*authenticator_is
)
199 byte_t authenticator_should
[4];
200 authenticator_should
[0] = ~_hitag2_byte(cs
);
201 authenticator_should
[1] = ~_hitag2_byte(cs
);
202 authenticator_should
[2] = ~_hitag2_byte(cs
);
203 authenticator_should
[3] = ~_hitag2_byte(cs
);
204 return (memcmp(authenticator_should
, authenticator_is
, 4) == 0);
207 static int hitag2_cipher_transcrypt(uint64_t* cs
, byte_t
*data
, unsigned int bytes
, unsigned int bits
)
210 for(i
=0; i
<bytes
; i
++) data
[i
] ^= _hitag2_byte(cs
);
211 for(i
=0; i
<bits
; i
++) data
[bytes
] ^= _hitag2_round(cs
) << (7-i
);
215 // Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK)
216 // TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz
217 // Hitag units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
218 // T0 = TIMER_CLOCK1 / 125000 = 192
221 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
222 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
224 #define HITAG_FRAME_LEN 20
225 #define HITAG_T_STOP 36 /* T_EOF should be > 36 */
226 #define HITAG_T_LOW 8 /* T_LOW should be 4..10 */
227 #define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */
228 #define HITAG_T_1_MIN 25 /* T[1] should be 26..30 */
229 //#define HITAG_T_EOF 40 /* T_EOF should be > 36 */
230 #define HITAG_T_EOF 80 /* T_EOF should be > 36 */
231 #define HITAG_T_WAIT_1 200 /* T_wresp should be 199..206 */
232 #define HITAG_T_WAIT_2 90 /* T_wresp should be 199..206 */
233 #define HITAG_T_WAIT_MAX 300 /* bit more than HITAG_T_WAIT_1 + HITAG_T_WAIT_2 */
234 #define HITAG_T_PROG 614
236 #define HITAG_T_TAG_ONE_HALF_PERIOD 10
237 #define HITAG_T_TAG_TWO_HALF_PERIOD 25
238 #define HITAG_T_TAG_THREE_HALF_PERIOD 41
239 #define HITAG_T_TAG_FOUR_HALF_PERIOD 57
241 #define HITAG_T_TAG_HALF_PERIOD 16
242 #define HITAG_T_TAG_FULL_PERIOD 32
244 #define HITAG_T_TAG_CAPTURE_ONE_HALF 13
245 #define HITAG_T_TAG_CAPTURE_TWO_HALF 25
246 #define HITAG_T_TAG_CAPTURE_THREE_HALF 41
247 #define HITAG_T_TAG_CAPTURE_FOUR_HALF 57
250 static void hitag_send_bit(int bit
) {
252 // Reset clock for the next bit
253 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
255 // Fixed modulation, earlier proxmark version used inverted signal
257 // Manchester: Unloaded, then loaded |__--|
259 while(AT91C_BASE_TC0
->TC_CV
< T0
*HITAG_T_TAG_HALF_PERIOD
);
261 while(AT91C_BASE_TC0
->TC_CV
< T0
*HITAG_T_TAG_FULL_PERIOD
);
263 // Manchester: Loaded, then unloaded |--__|
265 while(AT91C_BASE_TC0
->TC_CV
< T0
*HITAG_T_TAG_HALF_PERIOD
);
267 while(AT91C_BASE_TC0
->TC_CV
< T0
*HITAG_T_TAG_FULL_PERIOD
);
272 static void hitag_send_frame(const byte_t
* frame
, size_t frame_len
)
274 // Send start of frame
275 for(size_t i
=0; i
<5; i
++) {
279 // Send the content of the frame
280 for(size_t i
=0; i
<frame_len
; i
++) {
281 hitag_send_bit((frame
[i
/8] >> (7-(i
%8)))&1);
284 // Drop the modulation
289 static void hitag2_handle_reader_command(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
)
291 byte_t rx_air
[HITAG_FRAME_LEN
];
293 // Copy the (original) received frame how it is send over the air
294 memcpy(rx_air
,rx
,nbytes(rxlen
));
296 if(tag
.crypto_active
) {
297 hitag2_cipher_transcrypt(&(tag
.cs
),rx
,rxlen
/8,rxlen
%8);
300 // Reset the transmission frame length
303 // Try to find out which command was send by selecting on length (in bits)
305 // Received 11000 from the reader, request for UID, send UID
307 // Always send over the air in the clear plaintext mode
308 if(rx_air
[0] != 0xC0) {
313 memcpy(tx
,tag
.sectors
[0],4);
314 tag
.crypto_active
= 0;
318 // Read/Write command: ..xx x..y yy with yyy == ~xxx, xxx is sector number
320 unsigned int sector
= (~( ((rx
[0]<<2)&0x04) | ((rx
[1]>>6)&0x03) ) & 0x07);
321 // Verify complement of sector index
322 if(sector
!= ((rx
[0]>>3)&0x07)) {
323 //DbpString("Transmission error (read/write)");
327 switch (rx
[0] & 0xC6) {
328 // Read command: 11xx x00y
330 memcpy(tx
,tag
.sectors
[sector
],4);
334 // Inverted Read command: 01xx x10y
336 for (size_t i
=0; i
<4; i
++) {
337 tx
[i
] = tag
.sectors
[sector
][i
] ^ 0xff;
342 // Write command: 10xx x01y
344 // Prepare write, acknowledge by repeating command
345 memcpy(tx
,rx
,nbytes(rxlen
));
347 tag
.active_sector
= sector
;
348 tag
.state
=TAG_STATE_WRITING
;
353 Dbprintf("Unknown command: %02x %02x",rx
[0],rx
[1]);
360 // Writing data or Reader password
362 if(tag
.state
== TAG_STATE_WRITING
) {
363 // These are the sector contents to be written. We don't have to do anything else.
364 memcpy(tag
.sectors
[tag
.active_sector
],rx
,nbytes(rxlen
));
365 tag
.state
=TAG_STATE_RESET
;
368 // Received RWD password, respond with configuration and our password
369 if(memcmp(rx
,tag
.sectors
[1],4) != 0) {
370 DbpString("Reader password is wrong");
374 memcpy(tx
,tag
.sectors
[3],4);
379 // Received RWD authentication challenge and respnse
381 // Store the authentication attempt
382 if (auth_table_len
< (AUTH_TABLE_LENGTH
-8)) {
383 memcpy(auth_table
+auth_table_len
,rx
,8);
387 // Reset the cipher state
388 hitag2_cipher_reset(&tag
,rx
);
389 // Check if the authentication was correct
390 if(!hitag2_cipher_authenticate(&(tag
.cs
),rx
+4)) {
391 // The reader failed to authenticate, do nothing
392 Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x Failed!",rx
[0],rx
[1],rx
[2],rx
[3],rx
[4],rx
[5],rx
[6],rx
[7]);
395 // Succesful, but commented out reporting back to the Host, this may delay to much.
396 // Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x OK!",rx[0],rx[1],rx[2],rx[3],rx[4],rx[5],rx[6],rx[7]);
398 // Activate encryption algorithm for all further communication
399 tag
.crypto_active
= 1;
401 // Use the tag password as response
402 memcpy(tx
,tag
.sectors
[3],4);
408 // LogTraceHitag(rx,rxlen,0,0,false);
409 // LogTraceHitag(tx,*txlen,0,0,true);
411 if(tag
.crypto_active
) {
412 hitag2_cipher_transcrypt(&(tag
.cs
), tx
, *txlen
/8, *txlen
%8);
416 static void hitag_reader_send_bit(int bit
) {
418 // Reset clock for the next bit
419 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
421 // Binary puls length modulation (BPLM) is used to encode the data stream
422 // This means that a transmission of a one takes longer than that of a zero
424 // Enable modulation, which means, drop the field
427 // Wait for 4-10 times the carrier period
428 while(AT91C_BASE_TC0
->TC_CV
< T0
*6);
431 // Disable modulation, just activates the field again
436 while(AT91C_BASE_TC0
->TC_CV
< T0
*22);
437 // SpinDelayUs(16*8);
440 while(AT91C_BASE_TC0
->TC_CV
< T0
*28);
441 // SpinDelayUs(22*8);
447 static void hitag_reader_send_frame(const byte_t
* frame
, size_t frame_len
)
449 // Send the content of the frame
450 for(size_t i
=0; i
<frame_len
; i
++) {
451 hitag_reader_send_bit((frame
[i
/8] >> (7-(i
%8)))&1);
454 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
455 // Enable modulation, which means, drop the field
457 // Wait for 4-10 times the carrier period
458 while(AT91C_BASE_TC0
->TC_CV
< T0
*6);
459 // Disable modulation, just activates the field again
465 static bool hitag2_password(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
) {
466 // Reset the transmission frame length
469 // Try to find out which command was send by selecting on length (in bits)
471 // No answer, try to resurrect
473 // Stop if there is no answer (after sending password)
475 DbpString("Password failed!");
479 memcpy(tx
,"\xc0",nbytes(*txlen
));
482 // Received UID, tag password
486 memcpy(tx
,password
,4);
488 memcpy(tag
.sectors
[blocknr
],rx
,4);
493 //store password in block1, the TAG answers with Block3, but we need the password in memory
494 memcpy(tag
.sectors
[blocknr
],tx
,4);
496 memcpy(tag
.sectors
[blocknr
],rx
,4);
501 DbpString("Read succesful!");
506 tx
[0] = 0xc0 | (blocknr
<< 3) | ((blocknr
^7) >> 2);
507 tx
[1] = ((blocknr
^7) << 6);
511 // Unexpected response
513 Dbprintf("Uknown frame length: %d",rxlen
);
520 static bool hitag2_write_page(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
)
522 switch (writestate
) {
523 case WRITE_STATE_START
:
525 tx
[0] = 0x82 | (blocknr
<< 3) | ((blocknr
^7) >> 2);
526 tx
[1] = ((blocknr
^7) << 6);
527 writestate
= WRITE_STATE_PAGENUM_WRITTEN
;
529 case WRITE_STATE_PAGENUM_WRITTEN
:
530 // Check if page number was received correctly
532 (rx
[0] == (0x82 | (blocknr
<< 3) | ((blocknr
^7) >> 2))) &&
533 (rx
[1] == (((blocknr
& 0x3) ^ 0x3) << 6))) {
535 memset(tx
, 0, HITAG_FRAME_LEN
);
536 memcpy(tx
, writedata
, 4);
537 writestate
= WRITE_STATE_PROG
;
539 Dbprintf("hitag2_write_page: Page number was not received correctly: rxlen=%d rx=%02x%02x%02x%02x",
540 rxlen
, rx
[0], rx
[1], rx
[2], rx
[3]);
545 case WRITE_STATE_PROG
:
550 Dbprintf("hitag2_write_page: unexpected rx data (%d) after page write", rxlen
);
554 DbpString("hitag2_write_page: Unknown state %d");
562 static bool hitag2_crypto(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
, bool write
) {
563 // Reset the transmission frame length
567 hitag2_cipher_transcrypt(&cipher_state
,rx
,rxlen
/8,rxlen
%8);
571 if (bCrypto
&& !bAuthenticating
&& write
) {
572 if (!hitag2_write_page(rx
, rxlen
, tx
, txlen
)) {
579 // Try to find out which command was send by selecting on length (in bits)
581 // No answer, try to resurrect
584 // Stop if there is no answer while we are in crypto mode (after sending NrAr)
586 // Failed during authentication
587 if (bAuthenticating
) {
588 DbpString("Authentication failed!");
591 // Failed reading a block, could be (read/write) locked, skip block and re-authenticate
593 // Write the low part of the key in memory
594 memcpy(tag
.sectors
[1],key
+2,4);
595 } else if (blocknr
== 2) {
596 // Write the high part of the key in memory
597 tag
.sectors
[2][0] = 0x00;
598 tag
.sectors
[2][1] = 0x00;
599 tag
.sectors
[2][2] = key
[0];
600 tag
.sectors
[2][3] = key
[1];
602 // Just put zero's in the memory (of the unreadable block)
603 memset(tag
.sectors
[blocknr
],0x00,4);
610 memcpy(tx
,"\xc0",nbytes(*txlen
));
614 // Received UID, crypto tag answer
617 uint64_t ui64key
= key
[0] | ((uint64_t)key
[1]) << 8 | ((uint64_t)key
[2]) << 16 | ((uint64_t)key
[3]) << 24 | ((uint64_t)key
[4]) << 32 | ((uint64_t)key
[5]) << 40;
618 uint32_t ui32uid
= rx
[0] | ((uint32_t)rx
[1]) << 8 | ((uint32_t)rx
[2]) << 16 | ((uint32_t)rx
[3]) << 24;
619 Dbprintf("hitag2_crypto: key=0x%x%x uid=0x%x", (uint32_t) ((rev64(ui64key
)) >> 32), (uint32_t) ((rev64(ui64key
)) & 0xffffffff), rev32(ui32uid
));
620 cipher_state
= _hitag2_init(rev64(ui64key
), rev32(ui32uid
), 0);
623 hitag2_cipher_transcrypt(&cipher_state
, tx
+4, 4, 0);
626 bAuthenticating
= true;
628 // Check if we received answer tag (at)
629 if (bAuthenticating
) {
630 bAuthenticating
= false;
632 if (!hitag2_write_page(rx
, rxlen
, tx
, txlen
)) {
638 // Store the received block
639 memcpy(tag
.sectors
[blocknr
],rx
,4);
644 DbpString("Read succesful!");
649 tx
[0] = 0xc0 | (blocknr
<< 3) | ((blocknr
^7) >> 2);
650 tx
[1] = ((blocknr
^7) << 6);
655 // Unexpected response
657 Dbprintf("Uknown frame length: %d",rxlen
);
664 // We have to return now to avoid double encryption
665 if (!bAuthenticating
) {
666 hitag2_cipher_transcrypt(&cipher_state
,tx
,*txlen
/8,*txlen
%8);
674 static bool hitag2_authenticate(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
) {
675 // Reset the transmission frame length
678 // Try to find out which command was send by selecting on length (in bits)
680 // No answer, try to resurrect
682 // Stop if there is no answer while we are in crypto mode (after sending NrAr)
684 DbpString("Authentication failed!");
688 memcpy(tx
,"\xc0",nbytes(*txlen
));
691 // Received UID, crypto tag answer
698 DbpString("Authentication succesful!");
699 // We are done... for now
704 // Unexpected response
706 Dbprintf("Uknown frame length: %d",rxlen
);
715 static bool hitag2_test_auth_attempts(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
) {
717 // Reset the transmission frame length
720 // Try to find out which command was send by selecting on length (in bits)
722 // No answer, try to resurrect
724 // Stop if there is no answer while we are in crypto mode (after sending NrAr)
726 Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x Failed, removed entry!",NrAr
[0],NrAr
[1],NrAr
[2],NrAr
[3],NrAr
[4],NrAr
[5],NrAr
[6],NrAr
[7]);
728 // Removing failed entry from authentiations table
729 memcpy(auth_table
+auth_table_pos
,auth_table
+auth_table_pos
+8,8);
732 // Return if we reached the end of the authentications table
734 if (auth_table_pos
== auth_table_len
) {
738 // Copy the next authentication attempt in row (at the same position, b/c we removed last failed entry)
739 memcpy(NrAr
,auth_table
+auth_table_pos
,8);
742 memcpy(tx
,"\xc0",nbytes(*txlen
));
745 // Received UID, crypto tag answer, or read block response
752 Dbprintf("auth: %02x%02x%02x%02x%02x%02x%02x%02x OK",NrAr
[0],NrAr
[1],NrAr
[2],NrAr
[3],NrAr
[4],NrAr
[5],NrAr
[6],NrAr
[7]);
754 if ((auth_table_pos
+8) == auth_table_len
) {
758 memcpy(NrAr
,auth_table
+auth_table_pos
,8);
763 Dbprintf("Uknown frame length: %d",rxlen
);
771 static bool hitag2_read_uid(byte_t
* rx
, const size_t rxlen
, byte_t
* tx
, size_t* txlen
) {
772 // Reset the transmission frame length
775 // Try to find out which command was send by selecting on length (in bits)
777 // No answer, try to resurrect
779 // Just starting or if there is no answer
781 memcpy(tx
,"\xc0",nbytes(*txlen
));
785 // Check if we received answer tag (at)
786 if (bAuthenticating
) {
787 bAuthenticating
= false;
789 // Store the received block
790 memcpy(tag
.sectors
[blocknr
],rx
,4);
794 //DbpString("Read successful!");
799 // Unexpected response
801 Dbprintf("Uknown frame length: %d",rxlen
);
808 void SnoopHitag(uint32_t type
) {
817 byte_t rx
[HITAG_FRAME_LEN
] = {0};
820 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
822 // Clean up trace and prepare it for storing frames
830 auth_table
= (byte_t
*)BigBuf_malloc(AUTH_TABLE_LENGTH
);
831 memset(auth_table
, 0x00, AUTH_TABLE_LENGTH
);
833 DbpString("Starting Hitag2 snoop");
836 // Set up eavesdropping mode, frequency divisor which will drive the FPGA
837 // and analog mux selection.
838 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_TOGGLE_MODE
);
839 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
840 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
843 // Configure output pin that is connected to the FPGA (for modulating)
844 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
845 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
847 // Disable modulation, we are going to eavesdrop, not modulate ;)
850 // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the reader frames
851 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
852 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
854 // Disable timer during configuration
855 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
857 // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
858 // external trigger rising edge, load RA on rising edge of TIOA.
859 uint32_t t1_channel_mode
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
| AT91C_TC_ETRGEDG_BOTH
| AT91C_TC_ABETRG
| AT91C_TC_LDRA_BOTH
;
860 AT91C_BASE_TC1
->TC_CMR
= t1_channel_mode
;
862 // Enable and reset counter
863 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
865 // Reset the received frame, frame count and timing info
869 reader_frame
= false;
874 while(!BUTTON_PRESS()) {
878 // Receive frame, watch for at most T0*EOF periods
879 while (AT91C_BASE_TC1
->TC_CV
< T0
*HITAG_T_EOF
) {
880 // Check if rising edge in modulation is detected
881 if(AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
882 // Retrieve the new timing values
883 int ra
= (AT91C_BASE_TC1
->TC_RA
/T0
);
885 // Find out if we are dealing with a rising or falling edge
886 rising_edge
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_FRAME
) > 0;
888 // Shorter periods will only happen with reader frames
889 if (!reader_frame
&& rising_edge
&& ra
< HITAG_T_TAG_CAPTURE_ONE_HALF
) {
890 // Switch from tag to reader capture
893 memset(rx
,0x00,sizeof(rx
));
897 // Only handle if reader frame and rising edge, or tag frame and falling edge
898 if (reader_frame
!= rising_edge
) {
903 // Add the buffered timing values of earlier captured edges which were skipped
909 // Capture reader frame
910 if(ra
>= HITAG_T_STOP
) {
912 //DbpString("wierd0?");
914 // Capture the T0 periods that have passed since last communication or field drop (reset)
915 response
= (ra
- HITAG_T_LOW
);
916 } else if(ra
>= HITAG_T_1_MIN
) {
918 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
920 } else if(ra
>= HITAG_T_0_MIN
) {
922 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
925 // Ignore wierd value, is to small to mean anything
929 // Capture tag frame (manchester decoding using only falling edges)
930 if(ra
>= HITAG_T_EOF
) {
932 //DbpString("wierd1?");
934 // Capture the T0 periods that have passed since last communication or field drop (reset)
935 // We always recieve a 'one' first, which has the falling edge after a half period |-_|
936 response
= ra
-HITAG_T_TAG_HALF_PERIOD
;
937 } else if(ra
>= HITAG_T_TAG_CAPTURE_FOUR_HALF
) {
938 // Manchester coding example |-_|_-|-_| (101)
939 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
941 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
943 } else if(ra
>= HITAG_T_TAG_CAPTURE_THREE_HALF
) {
944 // Manchester coding example |_-|...|_-|-_| (0...01)
945 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
947 // We have to skip this half period at start and add the 'one' the second time
949 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
954 } else if(ra
>= HITAG_T_TAG_CAPTURE_TWO_HALF
) {
955 // Manchester coding example |_-|_-| (00) or |-_|-_| (11)
957 // Ignore bits that are transmitted during SOF
960 // bit is same as last bit
961 rx
[rxlen
/ 8] |= lastbit
<< (7-(rxlen
%8));
965 // Ignore wierd value, is to small to mean anything
971 // Check if frame was captured
974 if (!LogTraceHitag(rx
,rxlen
,response
,0,reader_frame
)) {
975 DbpString("Trace full");
979 // Check if we recognize a valid authentication attempt
980 if (nbytes(rxlen
) == 8) {
981 // Store the authentication attempt
982 if (auth_table_len
< (AUTH_TABLE_LENGTH
-8)) {
983 memcpy(auth_table
+auth_table_len
,rx
,8);
988 // Reset the received frame and response timing info
989 memset(rx
,0x00,sizeof(rx
));
991 reader_frame
= false;
1000 // Save the timer overflow, will be 0 when frame was received
1001 overflow
+= (AT91C_BASE_TC1
->TC_CV
/T0
);
1003 // Reset the frame length
1005 // Reset the timer to restart while-loop that receives frames
1006 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_SWTRG
;
1012 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1013 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1014 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1017 // Dbprintf("frame received: %d",frame_count);
1018 // Dbprintf("Authentication Attempts: %d",(auth_table_len/8));
1019 // DbpString("All done");
1022 void SimulateHitagTag(bool tag_mem_supplied
, byte_t
* data
) {
1026 byte_t rx
[HITAG_FRAME_LEN
];
1028 byte_t tx
[HITAG_FRAME_LEN
];
1030 bool bQuitTraceFull
= false;
1033 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1035 // Clean up trace and prepare it for storing frames
1043 auth_table
= (byte_t
*)BigBuf_malloc(AUTH_TABLE_LENGTH
);
1044 memset(auth_table
, 0x00, AUTH_TABLE_LENGTH
);
1046 DbpString("Starting Hitag2 simulation");
1050 if (tag_mem_supplied
) {
1051 DbpString("Loading hitag2 memory...");
1052 memcpy((byte_t
*)tag
.sectors
,data
,48);
1056 for (size_t i
=0; i
<12; i
++) {
1057 for (size_t j
=0; j
<4; j
++) {
1059 block
|= tag
.sectors
[i
][j
];
1061 Dbprintf("| %d | %08x |",i
,block
);
1064 // Set up simulator mode, frequency divisor which will drive the FPGA
1065 // and analog mux selection.
1066 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
1067 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1068 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1071 // Configure output pin that is connected to the FPGA (for modulating)
1072 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
1073 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
1075 // Disable modulation at default, which means release resistance
1078 // Enable Peripheral Clock for TIMER_CLOCK0, used to measure exact timing before answering
1079 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC0
);
1081 // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the reader frames
1082 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
1083 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1085 // Disable timer during configuration
1086 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1088 // Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1089 // external trigger rising edge, load RA on rising edge of TIOA.
1090 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
| AT91C_TC_ETRGEDG_RISING
| AT91C_TC_ABETRG
| AT91C_TC_LDRA_RISING
;
1092 // Reset the received frame, frame count and timing info
1093 memset(rx
,0x00,sizeof(rx
));
1098 // Enable and reset counter
1099 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1101 while(!BUTTON_PRESS()) {
1105 // Receive frame, watch for at most T0*EOF periods
1106 while (AT91C_BASE_TC1
->TC_CV
< T0
*HITAG_T_EOF
) {
1107 // Check if rising edge in modulation is detected
1108 if(AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
1109 // Retrieve the new timing values
1110 int ra
= (AT91C_BASE_TC1
->TC_RA
/T0
) + overflow
;
1113 // Reset timer every frame, we have to capture the last edge for timing
1114 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1118 // Capture reader frame
1119 if(ra
>= HITAG_T_STOP
) {
1121 //DbpString("wierd0?");
1123 // Capture the T0 periods that have passed since last communication or field drop (reset)
1124 response
= (ra
- HITAG_T_LOW
);
1125 } else if(ra
>= HITAG_T_1_MIN
) {
1127 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
1129 } else if(ra
>= HITAG_T_0_MIN
) {
1131 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
1134 // Ignore wierd value, is to small to mean anything
1139 // Check if frame was captured
1143 if (!LogTraceHitag(rx
,rxlen
,response
,0,true)) {
1144 DbpString("Trace full");
1145 if (bQuitTraceFull
) {
1153 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1154 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1156 // Process the incoming frame (rx) and prepare the outgoing frame (tx)
1157 hitag2_handle_reader_command(rx
,rxlen
,tx
,&txlen
);
1159 // Wait for HITAG_T_WAIT_1 carrier periods after the last reader bit,
1160 // not that since the clock counts since the rising edge, but T_Wait1 is
1161 // with respect to the falling edge, we need to wait actually (T_Wait1 - T_Low)
1162 // periods. The gap time T_Low varies (4..10). All timer values are in
1163 // terms of T0 units
1164 while(AT91C_BASE_TC0
->TC_CV
< T0
*(HITAG_T_WAIT_1
-HITAG_T_LOW
));
1166 // Send and store the tag answer (if there is any)
1168 // Transmit the tag frame
1169 hitag_send_frame(tx
,txlen
);
1170 // Store the frame in the trace
1172 if (!LogTraceHitag(tx
,txlen
,0,0,false)) {
1173 DbpString("Trace full");
1174 if (bQuitTraceFull
) {
1183 // Reset the received frame and response timing info
1184 memset(rx
,0x00,sizeof(rx
));
1187 // Enable and reset external trigger in timer for capturing future frames
1188 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1191 // Reset the frame length
1193 // Save the timer overflow, will be 0 when frame was received
1194 overflow
+= (AT91C_BASE_TC1
->TC_CV
/T0
);
1195 // Reset the timer to restart while-loop that receives frames
1196 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_SWTRG
;
1200 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1201 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1202 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1204 DbpString("Sim Stopped");
1208 void ReaderHitag(hitag_function htf
, hitag_data
* htd
) {
1211 byte_t rx
[HITAG_FRAME_LEN
];
1213 byte_t txbuf
[HITAG_FRAME_LEN
];
1220 int t_wait
= HITAG_T_WAIT_MAX
;
1222 bool bQuitTraceFull
= false;
1224 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1225 // Reset the return status
1226 bSuccessful
= false;
1228 // Clean up trace and prepare it for storing frames
1232 //DbpString("Starting Hitag reader family");
1234 // Check configuration
1236 case RHT2F_PASSWORD
: {
1237 Dbprintf("List identifier in password mode");
1238 memcpy(password
,htd
->pwd
.password
,4);
1240 bQuitTraceFull
= false;
1244 case RHT2F_AUTHENTICATE
: {
1245 DbpString("Authenticating using nr,ar pair:");
1246 memcpy(NrAr
,htd
->auth
.NrAr
,8);
1247 Dbhexdump(8,NrAr
,false);
1250 bAuthenticating
= false;
1251 bQuitTraceFull
= true;
1255 DbpString("Authenticating using key:");
1256 memcpy(key
,htd
->crypto
.key
,6); //HACK; 4 or 6?? I read both in the code.
1257 Dbhexdump(6,key
,false);
1261 bAuthenticating
= false;
1262 bQuitTraceFull
= true;
1264 case RHT2F_TEST_AUTH_ATTEMPTS
: {
1265 Dbprintf("Testing %d authentication attempts",(auth_table_len
/8));
1267 memcpy(NrAr
, auth_table
, 8);
1268 bQuitTraceFull
= false;
1272 case RHT2F_UID_ONLY
: {
1276 bAuthenticating
= false;
1277 bQuitTraceFull
= true;
1280 Dbprintf("Error, unknown function: %d",htf
);
1288 // Configure output and enable pin that is connected to the FPGA (for modulating)
1289 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
1290 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
1292 // Set fpga in edge detect with reader field, we can modulate as reader now
1293 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_READER_FIELD
);
1295 // Set Frequency divisor which will drive the FPGA and analog mux selection
1296 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1297 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1300 // Disable modulation at default, which means enable the field
1303 // Give it a bit of time for the resonant antenna to settle.
1306 // Enable Peripheral Clock for TIMER_CLOCK0, used to measure exact timing before answering
1307 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC0
);
1309 // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the tag frames
1310 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
1311 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1313 // Disable timer during configuration
1314 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1316 // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1317 // external trigger rising edge, load RA on falling edge of TIOA.
1318 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
| AT91C_TC_ETRGEDG_FALLING
| AT91C_TC_ABETRG
| AT91C_TC_LDRA_FALLING
;
1320 // Enable and reset counters
1321 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1322 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1324 // Reset the received frame, frame count and timing info
1329 // Tag specific configuration settings (sof, timings, etc.)
1334 //DbpString("Configured for hitagS reader");
1335 } else if (htf
< 20) {
1339 //DbpString("Configured for hitag1 reader");
1340 } else if (htf
< 30) {
1343 t_wait
= HITAG_T_WAIT_2
;
1344 //DbpString("Configured for hitag2 reader");
1346 Dbprintf("Error, unknown hitag reader type: %d",htf
);
1349 uint8_t attempt_count
=0;
1350 while(!bStop
&& !BUTTON_PRESS()) {
1354 // Check if frame was captured and store it
1358 if (!LogTraceHitag(rx
,rxlen
,response
,0,false)) {
1359 DbpString("Trace full");
1360 if (bQuitTraceFull
) {
1369 // By default reset the transmission buffer
1372 case RHT2F_PASSWORD
: {
1373 bStop
= !hitag2_password(rx
,rxlen
,tx
,&txlen
);
1375 case RHT2F_AUTHENTICATE
: {
1376 bStop
= !hitag2_authenticate(rx
,rxlen
,tx
,&txlen
);
1378 case RHT2F_CRYPTO
: {
1379 bStop
= !hitag2_crypto(rx
,rxlen
,tx
,&txlen
, false);
1381 case RHT2F_TEST_AUTH_ATTEMPTS
: {
1382 bStop
= !hitag2_test_auth_attempts(rx
,rxlen
,tx
,&txlen
);
1384 case RHT2F_UID_ONLY
: {
1385 bStop
= !hitag2_read_uid(rx
, rxlen
, tx
, &txlen
);
1386 attempt_count
++; //attempt 3 times to get uid then quit
1387 if (!bStop
&& attempt_count
== 3) bStop
= true;
1390 Dbprintf("Error, unknown function: %d",htf
);
1395 // Send and store the reader command
1396 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1397 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1399 // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting,
1400 // Since the clock counts since the last falling edge, a 'one' means that the
1401 // falling edge occured halfway the period. with respect to this falling edge,
1402 // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
1403 // All timer values are in terms of T0 units
1404 while(AT91C_BASE_TC0
->TC_CV
< T0
*(t_wait
+(HITAG_T_TAG_HALF_PERIOD
*lastbit
)));
1406 //Dbprintf("DEBUG: Sending reader frame");
1408 // Transmit the reader frame
1409 hitag_reader_send_frame(tx
,txlen
);
1411 // Enable and reset external trigger in timer for capturing future frames
1412 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1414 // Add transmitted frame to total count
1418 // Store the frame in the trace
1419 if (!LogTraceHitag(tx
,txlen
,HITAG_T_WAIT_2
,0,true)) {
1420 if (bQuitTraceFull
) {
1429 // Reset values for receiving frames
1430 memset(rx
,0x00,sizeof(rx
));
1434 tag_sof
= reset_sof
;
1436 //Dbprintf("DEBUG: Waiting to receive frame");
1437 uint32_t errorCount
= 0;
1439 // Receive frame, watch for at most T0*EOF periods
1440 while (AT91C_BASE_TC1
->TC_CV
< T0
*HITAG_T_WAIT_MAX
) {
1441 // Check if falling edge in tag modulation is detected
1442 if(AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
1443 // Retrieve the new timing values
1444 int ra
= (AT91C_BASE_TC1
->TC_RA
/T0
);
1446 // Reset timer every frame, we have to capture the last edge for timing
1447 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
1451 // Capture tag frame (manchester decoding using only falling edges)
1452 if(ra
>= HITAG_T_EOF
) {
1454 //Dbprintf("DEBUG: Wierd1");
1456 // Capture the T0 periods that have passed since last communication or field drop (reset)
1457 // We always recieve a 'one' first, which has the falling edge after a half period |-_|
1458 response
= ra
-HITAG_T_TAG_HALF_PERIOD
;
1459 } else if(ra
>= HITAG_T_TAG_CAPTURE_FOUR_HALF
) {
1460 // Manchester coding example |-_|_-|-_| (101)
1462 //need to test to verify we don't exceed memory...
1463 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1466 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
1468 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
1470 } else if(ra
>= HITAG_T_TAG_CAPTURE_THREE_HALF
) {
1471 // Manchester coding example |_-|...|_-|-_| (0...01)
1473 //need to test to verify we don't exceed memory...
1474 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1477 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
1479 // We have to skip this half period at start and add the 'one' the second time
1481 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
1486 } else if(ra
>= HITAG_T_TAG_CAPTURE_TWO_HALF
) {
1487 // Manchester coding example |_-|_-| (00) or |-_|-_| (11)
1489 //need to test to verify we don't exceed memory...
1490 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1494 // Ignore bits that are transmitted during SOF
1497 // bit is same as last bit
1498 rx
[rxlen
/ 8] |= lastbit
<< (7-(rxlen
%8));
1502 //Dbprintf("DEBUG: Wierd2");
1504 // Ignore wierd value, is to small to mean anything
1507 //if we saw over 100 wierd values break it probably isn't hitag...
1508 if (errorCount
>100) break;
1509 // We can break this loop if we received the last bit from a frame
1510 if (AT91C_BASE_TC1
->TC_CV
> T0
*HITAG_T_EOF
) {
1515 //Dbprintf("DEBUG: Done waiting for frame");
1519 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1520 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1521 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1522 //Dbprintf("frame received: %d",frame_count);
1523 //DbpString("All done");
1525 cmd_send(CMD_ACK
,bSuccessful
,0,0,(byte_t
*)tag
.sectors
,48);
1527 cmd_send(CMD_ACK
,bSuccessful
,0,0,0,0);
1531 void WriterHitag(hitag_function htf
, hitag_data
* htd
, int page
) {
1534 byte_t rx
[HITAG_FRAME_LEN
];
1536 byte_t txbuf
[HITAG_FRAME_LEN
];
1543 int t_wait
= HITAG_T_WAIT_MAX
;
1545 bool bQuitTraceFull
= false;
1547 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1548 // Reset the return status
1549 bSuccessful
= false;
1551 // Clean up trace and prepare it for storing frames
1555 //DbpString("Starting Hitag reader family");
1557 // Check configuration
1561 DbpString("Authenticating using key:");
1562 memcpy(key
,htd
->crypto
.key
,6); //HACK; 4 or 6?? I read both in the code.
1563 memcpy(writedata
, htd
->crypto
.data
, 4);
1564 Dbhexdump(6,key
,false);
1568 bAuthenticating
= false;
1569 bQuitTraceFull
= true;
1570 writestate
= WRITE_STATE_START
;
1573 Dbprintf("Error, unknown function: %d",htf
);
1581 // Configure output and enable pin that is connected to the FPGA (for modulating)
1582 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
1583 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
1585 // Set fpga in edge detect with reader field, we can modulate as reader now
1586 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
| FPGA_LF_EDGE_DETECT_READER_FIELD
);
1588 // Set Frequency divisor which will drive the FPGA and analog mux selection
1589 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1590 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1593 // Disable modulation at default, which means enable the field
1596 // Give it a bit of time for the resonant antenna to settle.
1599 // Enable Peripheral Clock for TIMER_CLOCK0, used to measure exact timing before answering
1600 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC0
);
1602 // Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the tag frames
1603 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
1604 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_SSC_FRAME
;
1606 // Disable timer during configuration
1607 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1609 // Capture mode, defaul timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
1610 // external trigger rising edge, load RA on falling edge of TIOA.
1611 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV1_CLOCK
| AT91C_TC_ETRGEDG_FALLING
| AT91C_TC_ABETRG
| AT91C_TC_LDRA_FALLING
;
1613 // Enable and reset counters
1614 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1615 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1617 // Reset the received frame, frame count and timing info
1623 // Tag specific configuration settings (sof, timings, etc.)
1628 //DbpString("Configured for hitagS reader");
1629 } else if (htf
< 20) {
1633 //DbpString("Configured for hitag1 reader");
1634 } else if (htf
< 30) {
1637 t_wait
= HITAG_T_WAIT_2
;
1638 //DbpString("Configured for hitag2 reader");
1640 Dbprintf("Error, unknown hitag reader type: %d",htf
);
1643 while(!bStop
&& !BUTTON_PRESS()) {
1647 // Check if frame was captured and store it
1651 if (!LogTraceHitag(rx
,rxlen
,response
,0,false)) {
1652 DbpString("Trace full");
1653 if (bQuitTraceFull
) {
1662 // By default reset the transmission buffer
1665 case WHT2F_CRYPTO
: {
1666 bStop
= !hitag2_crypto(rx
,rxlen
,tx
,&txlen
, true);
1669 Dbprintf("Error, unknown function: %d",htf
);
1674 // Send and store the reader command
1675 // Disable timer 1 with external trigger to avoid triggers during our own modulation
1676 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1678 // Wait for HITAG_T_WAIT_2 carrier periods after the last tag bit before transmitting,
1679 // Since the clock counts since the last falling edge, a 'one' means that the
1680 // falling edge occured halfway the period. with respect to this falling edge,
1681 // we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
1682 // All timer values are in terms of T0 units
1683 while(AT91C_BASE_TC0
->TC_CV
< T0
*(t_wait
+(HITAG_T_TAG_HALF_PERIOD
*lastbit
)));
1685 //Dbprintf("DEBUG: Sending reader frame");
1687 // Transmit the reader frame
1688 hitag_reader_send_frame(tx
,txlen
);
1690 // Enable and reset external trigger in timer for capturing future frames
1691 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
1693 // Add transmitted frame to total count
1697 // Store the frame in the trace
1698 if (!LogTraceHitag(tx
,txlen
,HITAG_T_WAIT_2
,0,true)) {
1699 if (bQuitTraceFull
) {
1708 // Reset values for receiving frames
1709 memset(rx
,0x00,sizeof(rx
));
1713 tag_sof
= reset_sof
;
1715 //Dbprintf("DEBUG: Waiting to receive frame");
1716 uint32_t errorCount
= 0;
1718 // Receive frame, watch for at most T0*EOF periods
1719 while (AT91C_BASE_TC1
->TC_CV
< T0
*HITAG_T_WAIT_MAX
) {
1720 // Check if falling edge in tag modulation is detected
1721 if(AT91C_BASE_TC1
->TC_SR
& AT91C_TC_LDRAS
) {
1722 // Retrieve the new timing values
1723 int ra
= (AT91C_BASE_TC1
->TC_RA
/T0
);
1725 // Reset timer every frame, we have to capture the last edge for timing
1726 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
1730 // Capture tag frame (manchester decoding using only falling edges)
1731 if(ra
>= HITAG_T_EOF
) {
1733 //Dbprintf("DEBUG: Wierd1");
1735 // Capture the T0 periods that have passed since last communication or field drop (reset)
1736 // We always recieve a 'one' first, which has the falling edge after a half period |-_|
1737 response
= ra
-HITAG_T_TAG_HALF_PERIOD
;
1738 } else if(ra
>= HITAG_T_TAG_CAPTURE_FOUR_HALF
) {
1739 // Manchester coding example |-_|_-|-_| (101)
1741 //need to test to verify we don't exceed memory...
1742 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1745 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
1747 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
1749 } else if(ra
>= HITAG_T_TAG_CAPTURE_THREE_HALF
) {
1750 // Manchester coding example |_-|...|_-|-_| (0...01)
1752 //need to test to verify we don't exceed memory...
1753 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1756 rx
[rxlen
/ 8] |= 0 << (7-(rxlen
%8));
1758 // We have to skip this half period at start and add the 'one' the second time
1760 rx
[rxlen
/ 8] |= 1 << (7-(rxlen
%8));
1765 } else if(ra
>= HITAG_T_TAG_CAPTURE_TWO_HALF
) {
1766 // Manchester coding example |_-|_-| (00) or |-_|-_| (11)
1768 //need to test to verify we don't exceed memory...
1769 //if ( ((rxlen+2) / 8) > HITAG_FRAME_LEN) {
1773 // Ignore bits that are transmitted during SOF
1776 // bit is same as last bit
1777 rx
[rxlen
/ 8] |= lastbit
<< (7-(rxlen
%8));
1781 //Dbprintf("DEBUG: Wierd2");
1783 // Ignore wierd value, is to small to mean anything
1786 //if we saw over 100 wierd values break it probably isn't hitag...
1787 if (errorCount
>100) break;
1788 // We can break this loop if we received the last bit from a frame
1789 if (AT91C_BASE_TC1
->TC_CV
> T0
*HITAG_T_EOF
) {
1794 // Wait some extra time for flash to be programmed
1795 if ((rxlen
== 0) && (writestate
== WRITE_STATE_PROG
))
1797 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_SWTRG
;
1798 while(AT91C_BASE_TC0
->TC_CV
< T0
*(HITAG_T_PROG
- HITAG_T_WAIT_MAX
));
1801 //Dbprintf("DEBUG: Done waiting for frame");
1805 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
1806 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
;
1807 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1808 //Dbprintf("frame received: %d",frame_count);
1809 //DbpString("All done");
1810 cmd_send(CMD_ACK
,bSuccessful
,0,0,(byte_t
*)tag
.sectors
,48);