1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015 Piwi
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 // High frequency Topaz (NFC Type 1) commands
9 //-----------------------------------------------------------------------------
16 #include "cmdparser.h"
17 #include "cmdhftopaz.h"
21 #include "proxmark3.h"
22 #include "iso14443crc.h"
23 #include "protocols.h"
28 static void topaz_switch_on_field(void)
30 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_SELECT
| ISO14A_NO_DISCONNECT
| ISO14A_TOPAZMODE
, 0, 0}};
35 static void topaz_switch_off_field(void)
37 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
42 static int topaz_send_cmd_raw(uint8_t *cmd
, uint8_t len
, uint8_t *response
)
44 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_TOPAZMODE
, len
, 0}};
45 memcpy(c
.d
.asBytes
, cmd
, len
);
49 WaitForResponse(CMD_ACK
, &resp
);
51 if (resp
.arg
[0] > 0) {
52 memcpy(response
, resp
.d
.asBytes
, resp
.arg
[0]);
59 static int topaz_send_cmd(uint8_t *cmd
, uint8_t len
, uint8_t *response
)
62 uint8_t first
, second
;
63 ComputeCrc14443(CRC_14443_B
, cmd
, len
-2, &first
, &second
);
68 return topaz_send_cmd_raw(cmd
, len
, response
);
72 static int topaz_select(uint8_t *atqa
, uint8_t *rid_response
)
74 // ToDo: implement anticollision
76 uint8_t wupa_cmd
[] = {TOPAZ_WUPA
};
77 uint8_t rid_cmd
[] = {TOPAZ_RID
, 0, 0, 0, 0, 0, 0, 0, 0};
79 topaz_switch_on_field();
81 if (!topaz_send_cmd(wupa_cmd
, sizeof(wupa_cmd
), atqa
)) {
82 topaz_switch_off_field();
83 return -1; // WUPA failed
86 if (!topaz_send_cmd(rid_cmd
, sizeof(rid_cmd
), rid_response
)) {
87 topaz_switch_off_field();
88 return -2; // RID failed
95 static int topaz_rall(uint8_t *uid
, uint8_t *rall_response
)
97 uint8_t rall_cmd
[] = {TOPAZ_RALL
, 0, 0, 0, 0, 0, 0, 0, 0};
99 memcpy(&rall_cmd
[3], uid
, 4);
100 if (!topaz_send_cmd(rall_cmd
, sizeof(rall_cmd
), rall_response
)) {
101 topaz_switch_off_field();
102 return -1; // RALL failed
109 static bool topaz_block_is_locked(uint8_t blockno
, uint8_t *lockbits
)
111 if(lockbits
[blockno
/8] >> (blockno
% 8) & 0x01) {
119 static int topaz_print_CC(uint8_t *data
)
121 if(data
[0] != 0xe1) {
122 return -1; // no NDEF message
125 PrintAndLog("Capability Container: %02x %02x %02x %02x", data
[0], data
[1], data
[2], data
[3]);
126 PrintAndLog(" %02x: NDEF Magic Number", data
[0]);
127 PrintAndLog(" %02x: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
128 PrintAndLog(" %02x: Physical Memory Size of this tag: %d bytes", data
[2], (data
[2] + 1) * 8);
129 PrintAndLog(" %02x: %s / %s", data
[3],
130 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
131 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
136 static void get_TLV(uint8_t **TLV_ptr
, uint8_t *tag
, uint16_t *length
, uint8_t **value
)
144 case 0x00: // NULL TLV.
145 case 0xFE: // Terminator TLV.
147 case 0x01: // Lock Control TLV
148 case 0x02: // Reserved Memory TLV
149 case 0x03: // NDEF message TLV
150 case 0xFD: // proprietary TLV
153 if (*length
== 0xff) {
154 *length
= **TLV_ptr
<< 8;
156 *length
|= **TLV_ptr
;
168 static bool topaz_print_lock_control_TLVs(uint8_t *memory
)
170 uint8_t *TLV_ptr
= memory
;
174 bool lock_TLV_present
= false;
176 while(*TLV_ptr
!= 0x03 && *TLV_ptr
!= 0xFD && *TLV_ptr
!= 0xFE) {
177 // all Lock Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
178 get_TLV(&TLV_ptr
, &tag
, &length
, &value
);
179 if (tag
== 0x01) { // the Lock Control TLV
180 uint8_t pages_addr
= value
[0] >> 4;
181 uint8_t byte_offset
= value
[0] & 0x0f;
182 uint8_t size_in_bits
= value
[1] ? value
[1] : 256;
183 uint8_t bytes_per_page
= 1 << (value
[2] & 0x0f);
184 uint8_t bytes_locked_per_bit
= 1 << (value
[2] >> 4);
185 PrintAndLog("Lock Area of %d bits at byte offset 0x%02x. Each Lock Bit locks %d bytes.",
187 pages_addr
* bytes_per_page
+ byte_offset
,
188 bytes_locked_per_bit
);
189 lock_TLV_present
= true;
193 if (!lock_TLV_present
) {
194 PrintAndLog("(No Lock Control TLV present)");
202 static int topaz_print_reserved_memory_control_TLVs(uint8_t *memory
)
204 uint8_t *TLV_ptr
= memory
;
208 bool reserved_memory_control_TLV_present
= false;
210 while(*TLV_ptr
!= 0x03 && *TLV_ptr
!= 0xFD && *TLV_ptr
!= 0xFE) {
211 // all Reserved Memory Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
212 get_TLV(&TLV_ptr
, &tag
, &length
, &value
);
213 if (tag
== 0x02) { // the Reserved Memory Control TLV
214 uint8_t pages_addr
= value
[0] >> 4;
215 uint8_t byte_offset
= value
[0] & 0x0f;
216 uint8_t size_in_bytes
= value
[1] ? value
[1] : 256;
217 uint8_t bytes_per_page
= 1 << (value
[2] & 0x0f);
218 PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.",
220 pages_addr
* bytes_per_page
+ byte_offset
);
221 reserved_memory_control_TLV_present
= true;
225 if (!reserved_memory_control_TLV_present
) {
226 PrintAndLog("(No Reserved Memory Control TLV present)");
234 static void topaz_print_lifecycle_state(uint8_t *data
)
240 static void topaz_print_NDEF(uint8_t *data
)
246 int CmdHFTopazReader(const char *Cmd
)
250 uint8_t rid_response
[8];
251 uint8_t *uid_echo
= &rid_response
[2];
253 uint8_t raw_content
[124];
256 uint8_t data_block
[15][8];
260 uint8_t *static_lock_bytes
= rall_response
.static_memory
.data_block
[0x0e];
262 status
= topaz_select(atqa
, rid_response
);
265 PrintAndLog("Error: couldn't receive ATQA");
269 PrintAndLog("ATQA : %02x %02x", atqa
[1], atqa
[0]);
270 if (atqa
[1] != 0x0c && atqa
[0] != 0x00) {
271 PrintAndLog("Tag doesn't support the Topaz protocol.");
272 topaz_switch_off_field();
277 PrintAndLog("Error: tag didn't answer to RID");
278 topaz_switch_off_field();
283 PrintAndLog("HR0 : %02x (%sa Topaz tag (%scapable of carrying a NDEF message), %s memory map)", rid_response
[0],
284 (rid_response
[0] & 0xF0) == 0x10 ? "" : "not ",
285 (rid_response
[0] & 0xF0) == 0x10 ? "" : "not ",
286 (rid_response
[0] & 0x0F) == 0x10 ? "static" : "dynamic");
287 PrintAndLog("HR1 : %02x", rid_response
[1]);
289 status
= topaz_rall(uid_echo
, rall_response
.raw_content
);
292 PrintAndLog("Error: tag didn't answer to RALL");
293 topaz_switch_off_field();
297 PrintAndLog("UID : %02x %02x %02x %02x %02x %02x %02x",
298 rall_response
.static_memory
.data_block
[0][6],
299 rall_response
.static_memory
.data_block
[0][5],
300 rall_response
.static_memory
.data_block
[0][4],
301 rall_response
.static_memory
.data_block
[0][3],
302 rall_response
.static_memory
.data_block
[0][2],
303 rall_response
.static_memory
.data_block
[0][1],
304 rall_response
.static_memory
.data_block
[0][0]);
305 PrintAndLog(" UID[6] (Manufacturer Byte) = %02x, Manufacturer: %s",
306 rall_response
.static_memory
.data_block
[0][6],
307 getTagInfo(rall_response
.static_memory
.data_block
[0][6]));
310 PrintAndLog("Static Data blocks 00 to 0c:");
311 PrintAndLog("block# | offset | Data | Locked?");
313 for (uint16_t i
= 0; i
<= 0x0c; i
++) {
314 for (uint16_t j
= 0; j
< 8; j
++) {
315 sprintf(&line
[3*j
], "%02x ", rall_response
.static_memory
.data_block
[i
][j
] /*rall_response[2 + 8*i + j]*/);
317 PrintAndLog(" 0x%02x | 0x%02x | %s| %-3s", i
, i
*8, line
, topaz_block_is_locked(i
, static_lock_bytes
) ? "yes" : "no");
321 PrintAndLog("Static Reserved block 0d:");
322 for (uint16_t j
= 0; j
< 8; j
++) {
323 sprintf(&line
[3*j
], "%02x ", rall_response
.static_memory
.data_block
[0x0d][j
]);
325 PrintAndLog(" 0x%02x | 0x%02x | %s| %-3s", 0x0d, 0x0d*8, line
, "n/a");
328 PrintAndLog("Static Lockbits / OTP block 0e:");
329 for (uint16_t j
= 0; j
< 8; j
++) {
330 sprintf(&line
[3*j
], "%02x ", static_lock_bytes
[j
]);
332 PrintAndLog(" 0x%02x | 0x%02x | %s| %-3s", 0x0e, 0x0e*8, line
, "n/a");
336 status
= topaz_print_CC(&rall_response
.static_memory
.data_block
[1][0]);
339 PrintAndLog("No NDEF message present");
340 topaz_switch_off_field();
345 bool lock_TLV_present
= topaz_print_lock_control_TLVs(&rall_response
.static_memory
.data_block
[1][4]);
348 bool reserved_mem_present
= topaz_print_reserved_memory_control_TLVs(&rall_response
.static_memory
.data_block
[1][4]);
350 topaz_print_lifecycle_state(&rall_response
.static_memory
.data_block
[1][0]);
352 topaz_print_NDEF(&rall_response
.static_memory
.data_block
[1][0]);
354 topaz_switch_off_field();
359 int CmdHFTopazSim(const char *Cmd
)
361 PrintAndLog("not yet implemented");
366 int CmdHFTopazCmdRaw(const char *Cmd
)
368 PrintAndLog("not yet implemented");
373 static int CmdHelp(const char *Cmd
);
376 static command_t CommandTable
[] =
378 {"help", CmdHelp
, 1, "This help"},
379 {"reader", CmdHFTopazReader
, 0, "Act like a Topaz reader"},
380 {"sim", CmdHFTopazSim
, 0, "<UID> -- Simulate Topaz tag"},
381 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop a Topaz reader-tag communication"},
382 {"raw", CmdHFTopazCmdRaw
, 0, "Send raw hex data to tag"},
383 {NULL
, NULL
, 0, NULL
}
387 int CmdHFTopaz(const char *Cmd
) {
389 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
392 CmdsParse(CommandTable
, Cmd
);
396 static int CmdHelp(const char *Cmd
)
398 CmdsHelp(CommandTable
);