1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2020 sirloins based on em4x50
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4170 commands
9 //-----------------------------------------------------------------------------
11 #include "fpgaloader.h"
15 #include "commonutil.h"
17 #include "appmain.h" // tear
19 static em4x70_tag_t tag
= { 0 };
21 // EM4170 requires a parity bit on commands, other variants do not.
22 static bool command_parity
= true;
24 // Conversion from Ticks to RF periods
26 // 1RF Period = 8us = 12 Ticks
27 #define TICKS_PER_FC 12
29 // Chip timing from datasheet
30 // Converted into Ticks for timing functions
31 #define EM4X70_T_TAG_QUARTER_PERIOD (8 * TICKS_PER_FC)
32 #define EM4X70_T_TAG_HALF_PERIOD (16 * TICKS_PER_FC)
33 #define EM4X70_T_TAG_THREE_QUARTER_PERIOD (24 * TICKS_PER_FC)
34 #define EM4X70_T_TAG_FULL_PERIOD (32 * TICKS_PER_FC) // 1 Bit Period
35 #define EM4X70_T_TAG_TWA (128 * TICKS_PER_FC) // Write Access Time
36 #define EM4X70_T_TAG_DIV (224 * TICKS_PER_FC) // Divergency Time
37 #define EM4X70_T_TAG_AUTH (4224 * TICKS_PER_FC) // Authentication Time
38 #define EM4X70_T_TAG_WEE (3072 * TICKS_PER_FC) // EEPROM write Time
39 #define EM4X70_T_TAG_TWALB (672 * TICKS_PER_FC) // Write Access Time of Lock Bits
40 #define EM4X70_T_TAG_BITMOD (4 * TICKS_PER_FC) // Initial time to stop modulation when sending 0
41 #define EM4X70_T_TAG_TOLERANCE (8 * TICKS_PER_FC) // Tolerance in RF periods for receive/LIW
43 #define EM4X70_T_TAG_TIMEOUT (4 * EM4X70_T_TAG_FULL_PERIOD) // Timeout if we ever get a pulse longer than this
44 #define EM4X70_T_WAITING_FOR_LIW 50 // Pulses to wait for listen window
45 #define EM4X70_T_READ_HEADER_LEN 16 // Read header length (16 bit periods)
47 #define EM4X70_COMMAND_RETRIES 5 // Attempts to send/read command
48 #define EM4X70_MAX_RECEIVE_LENGTH 96 // Maximum bits to expect from any command
51 * These IDs are from the EM4170 datasheet
52 * Some versions of the chip require a
53 * (even) parity bit, others do not
55 #define EM4X70_COMMAND_ID 0x01
56 #define EM4X70_COMMAND_UM1 0x02
57 #define EM4X70_COMMAND_AUTH 0x03
58 #define EM4X70_COMMAND_PIN 0x04
59 #define EM4X70_COMMAND_WRITE 0x05
60 #define EM4X70_COMMAND_UM2 0x07
62 // Constants used to determing high/low state of signal
63 #define EM4X70_NOISE_THRESHOLD 13 // May depend on noise in environment
64 #define HIGH_SIGNAL_THRESHOLD (127 + EM4X70_NOISE_THRESHOLD)
65 #define LOW_SIGNAL_THRESHOLD (127 - EM4X70_NOISE_THRESHOLD)
67 #define IS_HIGH(sample) (sample > LOW_SIGNAL_THRESHOLD ? true : false)
68 #define IS_LOW(sample) (sample < HIGH_SIGNAL_THRESHOLD ? true : false)
70 // Timing related macros
71 #define IS_TIMEOUT(timeout_ticks) (GetTicks() > timeout_ticks)
72 #define TICKS_ELAPSED(start_ticks) (GetTicks() - start_ticks)
74 static uint8_t bits2byte(const uint8_t *bits
, int length
);
75 static void bits2bytes(const uint8_t *bits
, int length
, uint8_t *out
);
76 static int em4x70_receive(uint8_t *bits
, size_t length
);
77 static bool find_listen_window(bool command
);
79 static void init_tag(void) {
80 memset(tag
.data
, 0x00, ARRAYLEN(tag
.data
));
83 static void em4x70_setup_read(void) {
85 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
86 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
88 // 50ms for the resonant antenna to settle.
91 // Now set up the SSC to get the ADC samples that are now streaming at us.
92 FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER
);
94 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
);
96 // Connect the A/D to the peak-detected low-frequency path.
97 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
99 // Steal this pin from the SSP (SPI communication channel with fpga) and
100 // use it to control the modulation
101 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
102 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
104 // Disable modulation at default, which means enable the field
114 static bool get_signalproperties(void) {
116 // Simple check to ensure we see a signal above the noise threshold
117 uint32_t no_periods
= 32;
119 // wait until signal/noise > 1 (max. 32 periods)
120 for (int i
= 0; i
< EM4X70_T_TAG_FULL_PERIOD
* no_periods
; i
++) {
122 // about 2 samples per bit period
123 WaitTicks(EM4X70_T_TAG_HALF_PERIOD
);
125 if (AT91C_BASE_SSC
->SSC_RHR
> HIGH_SIGNAL_THRESHOLD
) {
133 * get_falling_pulse_length
135 * Returns time between falling edge pulse in ticks
137 static uint32_t get_falling_pulse_length(void) {
139 uint32_t timeout
= GetTicks() + EM4X70_T_TAG_TIMEOUT
;
141 while (IS_HIGH(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
143 if (IS_TIMEOUT(timeout
))
146 uint32_t start_ticks
= GetTicks();
148 while (IS_LOW(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
150 if (IS_TIMEOUT(timeout
))
153 while (IS_HIGH(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
155 if (IS_TIMEOUT(timeout
))
158 return TICKS_ELAPSED(start_ticks
);
162 * get_rising_pulse_length
164 * Returns time between rising edge pulse in ticks
166 static uint32_t get_rising_pulse_length(void) {
168 uint32_t timeout
= GetTicks() + EM4X70_T_TAG_TIMEOUT
;
170 while (IS_LOW(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
172 if (IS_TIMEOUT(timeout
))
175 uint32_t start_ticks
= GetTicks();
177 while (IS_HIGH(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
179 if (IS_TIMEOUT(timeout
))
182 while (IS_LOW(AT91C_BASE_SSC
->SSC_RHR
) && !IS_TIMEOUT(timeout
));
184 if (IS_TIMEOUT(timeout
))
187 return TICKS_ELAPSED(start_ticks
);
191 static uint32_t get_pulse_length(edge_detection_t edge
) {
193 if (edge
== RISING_EDGE
)
194 return get_rising_pulse_length();
195 else if (edge
== FALLING_EDGE
)
196 return get_falling_pulse_length();
201 static bool check_pulse_length(uint32_t pl
, uint32_t length
) {
202 // check if pulse length <pl> corresponds to given length <length>
203 return ((pl
>= (length
- EM4X70_T_TAG_TOLERANCE
)) && (pl
<= (length
+ EM4X70_T_TAG_TOLERANCE
)));
206 static void em4x70_send_bit(bool bit
) {
208 // send single bit according to EM4170 application note and datasheet
209 uint32_t start_ticks
= GetTicks();
213 // disable modulation (drop the field) n cycles of carrier
215 while (TICKS_ELAPSED(start_ticks
) <= EM4X70_T_TAG_BITMOD
);
217 // enable modulation (activates the field) for remaining first
218 // half of bit period
220 while (TICKS_ELAPSED(start_ticks
) <= EM4X70_T_TAG_HALF_PERIOD
);
222 // disable modulation for second half of bit period
224 while (TICKS_ELAPSED(start_ticks
) <= EM4X70_T_TAG_FULL_PERIOD
);
228 // bit = "1" means disable modulation for full bit period
230 while (TICKS_ELAPSED(start_ticks
) <= EM4X70_T_TAG_FULL_PERIOD
);
237 * sends 4 bits of data + 1 bit of parity (with_parity)
240 static void em4x70_send_nibble(uint8_t nibble
, bool with_parity
) {
244 // Non automotive EM4x70 based tags are 3 bits + 1 parity.
245 // So drop the MSB and send a parity bit instead after the command
249 for (int i
= msb_bit
; i
< 4; i
++) {
250 int bit
= (nibble
>> (3 - i
)) & 1;
251 em4x70_send_bit(bit
);
256 em4x70_send_bit(parity
);
259 static void em4x70_send_byte(uint8_t byte
) {
260 // Send byte msb first
261 for (int i
= 0; i
< 8; i
++)
262 em4x70_send_bit((byte
>> (7 - i
)) & 1);
265 static void em4x70_send_word(const uint16_t word
) {
267 // Split into nibbles
270 for (int i
= 0; i
< 2; i
++) {
271 uint8_t byte
= (word
>> (8 * i
)) & 0xff;
272 nibbles
[j
++] = (byte
>> 4) & 0xf;
273 nibbles
[j
++] = byte
& 0xf;
276 // send 16 bit word with parity bits according to EM4x70 datasheet
277 // sent as 4 x nibbles (4 bits + parity)
278 for (int i
= 0; i
< 4; i
++) {
279 em4x70_send_nibble(nibbles
[i
], true);
282 // send column parities (4 bit)
283 em4x70_send_nibble(nibbles
[0] ^ nibbles
[1] ^ nibbles
[2] ^ nibbles
[3], false);
285 // send final stop bit (always "0")
289 static bool check_ack(void) {
290 // returns true if signal structue corresponds to ACK, anything else is
291 // counted as NAK (-> false)
294 if (check_pulse_length(get_pulse_length(FALLING_EDGE
), 2 * EM4X70_T_TAG_FULL_PERIOD
) &&
295 check_pulse_length(get_pulse_length(FALLING_EDGE
), 2 * EM4X70_T_TAG_FULL_PERIOD
)) {
300 // Othewise it was a NACK or Listen Window
304 static int authenticate(const uint8_t *rnd
, const uint8_t *frnd
, uint8_t *response
) {
306 if (find_listen_window(true)) {
308 em4x70_send_nibble(EM4X70_COMMAND_AUTH
, true);
310 // Send 56-bit Random number
311 for (int i
= 0; i
< 7; i
++) {
312 em4x70_send_byte(rnd
[i
]);
315 // Send 7 x 0's (Diversity bits)
316 for (int i
= 0; i
< 7; i
++) {
322 // Send first 24 bits
323 for (int i
= 0; i
< 3; i
++) {
324 em4x70_send_byte(frnd
[i
]);
327 // Send last 4 bits (no parity)
328 em4x70_send_nibble((frnd
[3] >> 4) & 0xf, false);
330 // Receive header, 20-bit g(RN), LIW
331 uint8_t grnd
[EM4X70_MAX_RECEIVE_LENGTH
] = {0};
332 int num
= em4x70_receive(grnd
, 20);
334 Dbprintf("Auth failed");
337 bits2bytes(grnd
, 24, response
);
344 static int send_pin(const uint32_t pin
) {
346 // sends pin code for unlocking
347 if (find_listen_window(true)) {
350 em4x70_send_nibble(EM4X70_COMMAND_PIN
, true);
352 // --> Send TAG ID (bytes 4-7)
353 for (int i
= 0; i
< 4; i
++) {
354 em4x70_send_byte(tag
.data
[7 - i
]);
358 for (int i
= 0; i
< 4 ; i
++) {
359 em4x70_send_byte((pin
>> (i
* 8)) & 0xff);
362 // Wait TWALB (write access lock bits)
363 WaitTicks(EM4X70_T_TAG_TWALB
);
368 // <w> Writes Lock Bits
369 WaitTicks(EM4X70_T_TAG_WEE
);
370 // <-- Receive header + ID
371 uint8_t tag_id
[EM4X70_MAX_RECEIVE_LENGTH
];
372 int num
= em4x70_receive(tag_id
, 32);
374 Dbprintf("Invalid ID Received");
377 bits2bytes(tag_id
, num
, &tag
.data
[4]);
385 static int write(const uint16_t word
, const uint8_t address
) {
387 // writes <word> to specified <address>
388 if (find_listen_window(true)) {
390 // send write command
391 em4x70_send_nibble(EM4X70_COMMAND_WRITE
, true);
393 // send address data with parity bit
394 em4x70_send_nibble(address
, true);
397 em4x70_send_word(word
);
400 WaitTicks(EM4X70_T_TAG_TWA
);
402 // look for ACK sequence
405 // now EM4x70 needs EM4X70_T_TAG_TWEE (EEPROM write time)
406 // for saving data and should return with ACK
407 WaitTicks(EM4X70_T_TAG_WEE
);
418 static bool find_listen_window(bool command
) {
421 while (cnt
< EM4X70_T_WAITING_FOR_LIW
) {
429 if (check_pulse_length(get_pulse_length(RISING_EDGE
), (2 * EM4X70_T_TAG_FULL_PERIOD
) + EM4X70_T_TAG_HALF_PERIOD
) &&
430 check_pulse_length(get_pulse_length(RISING_EDGE
), (2 * EM4X70_T_TAG_FULL_PERIOD
) + EM4X70_T_TAG_HALF_PERIOD
) &&
431 check_pulse_length(get_pulse_length(FALLING_EDGE
), (2 * EM4X70_T_TAG_FULL_PERIOD
) + EM4X70_T_TAG_FULL_PERIOD
) &&
432 check_pulse_length(get_pulse_length(FALLING_EDGE
), EM4X70_T_TAG_FULL_PERIOD
+ (2 * EM4X70_T_TAG_HALF_PERIOD
))) {
435 /* Here we are after the 64 duration edge.
436 * em4170 says we need to wait about 48 RF clock cycles.
437 * depends on the delay between tag and us
439 * I've found between 4-5 quarter periods (32-40) works best
441 WaitTicks(4 * EM4X70_T_TAG_QUARTER_PERIOD
);
454 static void bits2bytes(const uint8_t *bits
, int length
, uint8_t *out
) {
456 if (length
% 8 != 0) {
457 Dbprintf("Should have a multiple of 8 bits, was sent %d", length
);
460 int num_bytes
= length
/ 8; // We should have a multiple of 8 here
462 for (int i
= 1; i
<= num_bytes
; i
++) {
463 out
[num_bytes
- i
] = bits2byte(bits
, 8);
468 static uint8_t bits2byte(const uint8_t *bits
, int length
) {
470 // converts <length> separate bits into a single "byte"
472 for (int i
= 0; i
< length
; i
++) {
483 static bool send_command_and_read(uint8_t command
, uint8_t *bytes
, size_t length
) {
485 int retries
= EM4X70_COMMAND_RETRIES
;
489 if (find_listen_window(true)) {
490 uint8_t bits
[EM4X70_MAX_RECEIVE_LENGTH
] = {0};
491 size_t out_length_bits
= length
* 8;
492 em4x70_send_nibble(command
, command_parity
);
493 int len
= em4x70_receive(bits
, out_length_bits
);
494 if (len
< out_length_bits
) {
495 Dbprintf("Invalid data received length: %d, expected %d", len
, out_length_bits
);
498 bits2bytes(bits
, len
, bytes
);
510 * read pre-programmed ID (4 bytes)
512 static bool em4x70_read_id(void) {
514 return send_command_and_read(EM4X70_COMMAND_ID
, &tag
.data
[4], 4);
521 * read user memory 1 (4 bytes including lock bits)
523 static bool em4x70_read_um1(void) {
525 return send_command_and_read(EM4X70_COMMAND_UM1
, &tag
.data
[0], 4);
533 * read user memory 2 (8 bytes)
535 static bool em4x70_read_um2(void) {
537 return send_command_and_read(EM4X70_COMMAND_UM2
, &tag
.data
[24], 8);
541 static bool find_em4x70_tag(void) {
542 // function is used to check wether a tag on the proxmark is an
543 // EM4170 tag or not -> speed up "lf search" process
544 return find_listen_window(false);
547 static int em4x70_receive(uint8_t *bits
, size_t length
) {
551 edge_detection_t edge
= RISING_EDGE
;
552 bool foundheader
= false;
554 // Read out the header
555 // 12 Manchester 1's (may miss some during settle period)
558 // Skip a few leading 1's as it could be noisy
559 WaitTicks(6 * EM4X70_T_TAG_FULL_PERIOD
);
561 // wait until we get the transition from 1's to 0's which is 1.5 full windows
562 for (int i
= 0; i
< EM4X70_T_READ_HEADER_LEN
; i
++) {
563 pl
= get_pulse_length(edge
);
564 if (check_pulse_length(pl
, 3 * EM4X70_T_TAG_HALF_PERIOD
)) {
571 Dbprintf("Failed to find read header");
575 // Skip next 3 0's, header check consumes the first 0
576 for (int i
= 0; i
< 3; i
++) {
577 // If pulse length is not 1 bit, then abort early
578 if (!check_pulse_length(get_pulse_length(edge
), EM4X70_T_TAG_FULL_PERIOD
)) {
583 // identify remaining bits based on pulse lengths
584 // between listen windows only pulse lengths of 1, 1.5 and 2 are possible
585 while (bit_pos
< length
) {
587 pl
= get_pulse_length(edge
);
589 if (check_pulse_length(pl
, EM4X70_T_TAG_FULL_PERIOD
)) {
591 // pulse length 1 -> assign bit
592 bits
[bit_pos
++] = edge
== FALLING_EDGE
? 1 : 0;
594 } else if (check_pulse_length(pl
, 3 * EM4X70_T_TAG_HALF_PERIOD
)) {
596 // pulse length 1.5 -> 2 bits + flip edge detection
597 if (edge
== FALLING_EDGE
) {
607 } else if (check_pulse_length(pl
, 2 * EM4X70_T_TAG_FULL_PERIOD
)) {
609 // pulse length of 2 -> two bits
610 if (edge
== FALLING_EDGE
) {
619 // Listen Window, or invalid bit
627 void em4x70_info(em4x70_data_t
*etd
) {
631 // Support tags with and without command parity bits
632 command_parity
= etd
->parity
;
638 if (get_signalproperties() && find_em4x70_tag()) {
639 // Read ID, UM1 and UM2
640 status
= em4x70_read_id() && em4x70_read_um1() && em4x70_read_um2();
645 reply_ng(CMD_LF_EM4X70_INFO
, status
, tag
.data
, sizeof(tag
.data
));
648 void em4x70_write(em4x70_data_t
*etd
) {
652 command_parity
= etd
->parity
;
658 if (get_signalproperties() && find_em4x70_tag()) {
661 status
= write(etd
->word
, etd
->address
) == PM3_SUCCESS
;
664 // Read Tag after writing
665 if (em4x70_read_id()) {
675 reply_ng(CMD_LF_EM4X70_WRITE
, status
, tag
.data
, sizeof(tag
.data
));
678 void em4x70_unlock(em4x70_data_t
*etd
) {
682 command_parity
= etd
->parity
;
688 if (get_signalproperties() && find_em4x70_tag()) {
690 // Read ID (required for send_pin command)
691 if (em4x70_read_id()) {
694 status
= send_pin(etd
->pin
) == PM3_SUCCESS
;
696 // If the write succeeded, read the rest of the tag
708 reply_ng(CMD_LF_EM4X70_UNLOCK
, status
, tag
.data
, sizeof(tag
.data
));
711 void em4x70_auth(em4x70_data_t
*etd
) {
714 uint8_t response
[3] = {0};
716 command_parity
= etd
->parity
;
722 if (get_signalproperties() && find_em4x70_tag()) {
724 // Authenticate and get tag response
725 status
= authenticate(etd
->rnd
, etd
->frnd
, response
) == PM3_SUCCESS
;
730 reply_ng(CMD_LF_EM4X70_AUTH
, status
, response
, sizeof(response
));
733 void em4x70_write_pin(em4x70_data_t
*etd
) {
737 command_parity
= etd
->parity
;
743 if (get_signalproperties() && find_em4x70_tag()) {
745 // Read ID (required for send_pin command)
746 if (em4x70_read_id()) {
749 if ((write(etd
->pin
& 0xFFFF, EM4X70_PIN_WORD_UPPER
) == PM3_SUCCESS
) &&
750 (write((etd
->pin
>> 16) & 0xFFFF, EM4X70_PIN_WORD_LOWER
) == PM3_SUCCESS
)) {
752 // Now Try to authenticate using the new PIN
755 status
= send_pin(etd
->pin
) == PM3_SUCCESS
;
757 // If the write succeeded, read the rest of the tag
770 reply_ng(CMD_LF_EM4X70_WRITEPIN
, status
, tag
.data
, sizeof(tag
.data
));
773 void em4x70_write_key(em4x70_data_t
*etd
) {
777 command_parity
= etd
->parity
;
783 if (get_signalproperties() && find_em4x70_tag()) {
785 // Read ID to ensure we can write to card
786 if (em4x70_read_id()) {
789 // Write each crypto block
790 for (int i
= 0; i
< 6; i
++) {
792 uint16_t key_word
= (etd
->crypt_key
[(i
* 2) + 1] << 8) + etd
->crypt_key
[i
* 2];
793 // Write each word, abort if any failure occurs
794 if (write(key_word
, 9 - i
) != PM3_SUCCESS
) {
799 // TODO: Ideally here we would perform a test authentication
800 // to ensure the new key was written correctly. This is
801 // what the datasheet suggests. We can't do that until
802 // we have the crypto algorithm implemented.
808 reply_ng(CMD_LF_EM4X70_WRITEKEY
, status
, tag
.data
, sizeof(tag
.data
));