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"
25 #define TOPAZ_STATIC_MEMORY (0x0f * 8)
27 typedef struct dynamic_lock_area
{
28 struct dynamic_lock_area
*next
;
30 uint16_t size_in_bits
;
31 uint16_t first_locked_byte
;
32 uint16_t bytes_locked_per_bit
;
33 } dynamic_lock_area_t
;
40 uint8_t data_blocks
[TOPAZ_STATIC_MEMORY
/8][8];
41 dynamic_lock_area_t
*dynamic_lock_areas
;
42 uint8_t *dynamic_reserved_areas
;
43 uint8_t *dynamic_memory
;
47 static void topaz_switch_on_field(void)
49 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_SELECT
| ISO14A_NO_DISCONNECT
| ISO14A_TOPAZMODE
, 0, 0}};
54 static void topaz_switch_off_field(void)
56 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
61 static int topaz_send_cmd_raw(uint8_t *cmd
, uint8_t len
, uint8_t *response
)
63 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_TOPAZMODE
, len
, 0}};
64 memcpy(c
.d
.asBytes
, cmd
, len
);
68 WaitForResponse(CMD_ACK
, &resp
);
70 if (resp
.arg
[0] > 0) {
71 memcpy(response
, resp
.d
.asBytes
, resp
.arg
[0]);
78 static int topaz_send_cmd(uint8_t *cmd
, uint8_t len
, uint8_t *response
)
81 uint8_t first
, second
;
82 ComputeCrc14443(CRC_14443_B
, cmd
, len
-2, &first
, &second
);
87 return topaz_send_cmd_raw(cmd
, len
, response
);
91 static int topaz_select(uint8_t *atqa
, uint8_t *rid_response
)
93 // ToDo: implement anticollision
95 uint8_t wupa_cmd
[] = {TOPAZ_WUPA
};
96 uint8_t rid_cmd
[] = {TOPAZ_RID
, 0, 0, 0, 0, 0, 0, 0, 0};
98 topaz_switch_on_field();
100 if (!topaz_send_cmd(wupa_cmd
, sizeof(wupa_cmd
), atqa
)) {
101 topaz_switch_off_field();
102 return -1; // WUPA failed
105 if (!topaz_send_cmd(rid_cmd
, sizeof(rid_cmd
), rid_response
)) {
106 topaz_switch_off_field();
107 return -2; // RID failed
114 static int topaz_rall(uint8_t *uid
, uint8_t *response
)
116 uint8_t rall_cmd
[] = {TOPAZ_RALL
, 0, 0, 0, 0, 0, 0, 0, 0};
118 memcpy(&rall_cmd
[3], uid
, 4);
119 if (!topaz_send_cmd(rall_cmd
, sizeof(rall_cmd
), response
)) {
120 topaz_switch_off_field();
121 return -1; // RALL failed
128 static dynamic_lock_area_t
*get_dynamic_lock_area(uint16_t byteno
)
130 dynamic_lock_area_t
*lock_area
;
132 lock_area
= topaz_tag
.dynamic_lock_areas
;
134 while (lock_area
!= NULL
) {
135 if (byteno
< lock_area
->first_locked_byte
) {
136 lock_area
= lock_area
->next
;
146 // check if a memory block (8 Bytes) is locked.
147 // TODO: support other sizes of locked_bytes_per_bit (current assumption: each lock bit locks 8 Bytes)
148 static bool topaz_byte_is_locked(uint16_t byteno
)
151 uint16_t locked_bytes_per_bit
;
152 dynamic_lock_area_t
*lock_area
;
154 if (byteno
< TOPAZ_STATIC_MEMORY
) {
155 lockbits
= &topaz_tag
.data_blocks
[0x0e][0];
156 locked_bytes_per_bit
= 8;
158 lock_area
= get_dynamic_lock_area(byteno
);
159 if (lock_area
== NULL
) {
162 locked_bytes_per_bit
= lock_area
->bytes_locked_per_bit
;
163 byteno
= byteno
- lock_area
->first_locked_byte
;
164 lockbits
= &topaz_tag
.dynamic_memory
[lock_area
->byte_offset
- TOPAZ_STATIC_MEMORY
];
167 uint16_t blockno
= byteno
/ locked_bytes_per_bit
;
168 if(lockbits
[blockno
/8] >> (blockno
% 8) & 0x01) {
176 static int topaz_print_CC(uint8_t *data
)
178 if(data
[0] != 0xe1) {
179 return -1; // no NDEF message
182 PrintAndLog("Capability Container: %02x %02x %02x %02x", data
[0], data
[1], data
[2], data
[3]);
183 PrintAndLog(" %02x: NDEF Magic Number", data
[0]);
184 PrintAndLog(" %02x: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
185 uint16_t memsize
= (data
[2] + 1) * 8;
186 topaz_tag
.dynamic_memory
= malloc(memsize
- TOPAZ_STATIC_MEMORY
);
187 PrintAndLog(" %02x: Physical Memory Size of this tag: %d bytes", data
[2], memsize
);
188 PrintAndLog(" %02x: %s / %s", data
[3],
189 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
190 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
195 static void get_TLV(uint8_t **TLV_ptr
, uint8_t *tag
, uint16_t *length
, uint8_t **value
)
203 case 0x00: // NULL TLV.
204 case 0xFE: // Terminator TLV.
206 case 0x01: // Lock Control TLV
207 case 0x02: // Reserved Memory TLV
208 case 0x03: // NDEF message TLV
209 case 0xFD: // proprietary TLV
212 if (*length
== 0xff) {
213 *length
= **TLV_ptr
<< 8;
215 *length
|= **TLV_ptr
;
227 static bool topaz_print_lock_control_TLVs(uint8_t *memory
)
229 uint8_t *TLV_ptr
= memory
;
233 bool lock_TLV_present
= false;
234 uint16_t first_locked_byte
= 0x0f * 8;
236 while(*TLV_ptr
!= 0x03 && *TLV_ptr
!= 0xFD && *TLV_ptr
!= 0xFE) {
237 // all Lock Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
238 get_TLV(&TLV_ptr
, &tag
, &length
, &value
);
239 if (tag
== 0x01) { // the Lock Control TLV
240 uint8_t pages_addr
= value
[0] >> 4;
241 uint8_t byte_offset
= value
[0] & 0x0f;
242 uint16_t size_in_bits
= value
[1] ? value
[1] : 256;
243 uint16_t bytes_per_page
= 1 << (value
[2] & 0x0f);
244 uint16_t bytes_locked_per_bit
= 1 << (value
[2] >> 4);
245 PrintAndLog("Lock Area of %d bits at byte offset 0x%04x. Each Lock Bit locks %d bytes.",
247 pages_addr
* bytes_per_page
+ byte_offset
,
248 bytes_locked_per_bit
);
249 lock_TLV_present
= true;
250 dynamic_lock_area_t
*old
= topaz_tag
.dynamic_lock_areas
;
251 dynamic_lock_area_t
*new = topaz_tag
.dynamic_lock_areas
;
253 new = topaz_tag
.dynamic_lock_areas
= (dynamic_lock_area_t
*)malloc(sizeof(dynamic_lock_area_t
));
255 while(old
->next
!= NULL
) {
258 new = old
->next
= (dynamic_lock_area_t
*)malloc(sizeof(dynamic_lock_area_t
));
261 new->first_locked_byte
= first_locked_byte
;
262 new->byte_offset
= pages_addr
* bytes_per_page
+ byte_offset
;
263 new->size_in_bits
= size_in_bits
;
264 new->bytes_locked_per_bit
= bytes_locked_per_bit
;
265 first_locked_byte
= first_locked_byte
+ size_in_bits
*bytes_locked_per_bit
;
269 if (!lock_TLV_present
) {
270 PrintAndLog("(No Lock Control TLV present)");
278 static int topaz_print_reserved_memory_control_TLVs(uint8_t *memory
)
280 uint8_t *TLV_ptr
= memory
;
284 bool reserved_memory_control_TLV_present
= false;
286 while(*TLV_ptr
!= 0x03 && *TLV_ptr
!= 0xFD && *TLV_ptr
!= 0xFE) {
287 // all Reserved Memory Control TLVs shall be present before the NDEF message TLV, the proprietary TLV (and the Terminator TLV)
288 get_TLV(&TLV_ptr
, &tag
, &length
, &value
);
289 if (tag
== 0x02) { // the Reserved Memory Control TLV
290 uint8_t pages_addr
= value
[0] >> 4;
291 uint8_t byte_offset
= value
[0] & 0x0f;
292 uint8_t size_in_bytes
= value
[1] ? value
[1] : 256;
293 uint8_t bytes_per_page
= 1 << (value
[2] & 0x0f);
294 PrintAndLog("Reserved Memory of %d bytes at byte offset 0x%02x.",
296 pages_addr
* bytes_per_page
+ byte_offset
);
297 reserved_memory_control_TLV_present
= true;
301 if (!reserved_memory_control_TLV_present
) {
302 PrintAndLog("(No Reserved Memory Control TLV present)");
310 static void topaz_print_lifecycle_state(uint8_t *data
)
316 static void topaz_print_NDEF(uint8_t *data
)
322 int CmdHFTopazReader(const char *Cmd
)
326 uint8_t rid_response
[8];
327 uint8_t *uid_echo
= &rid_response
[2];
328 uint8_t rall_response
[124];
330 status
= topaz_select(atqa
, rid_response
);
333 PrintAndLog("Error: couldn't receive ATQA");
337 PrintAndLog("ATQA : %02x %02x", atqa
[1], atqa
[0]);
338 if (atqa
[1] != 0x0c && atqa
[0] != 0x00) {
339 PrintAndLog("Tag doesn't support the Topaz protocol.");
340 topaz_switch_off_field();
345 PrintAndLog("Error: tag didn't answer to RID");
346 topaz_switch_off_field();
350 topaz_tag
.HR01
[0] = rid_response
[0];
351 topaz_tag
.HR01
[1] = rid_response
[1];
354 PrintAndLog("HR0 : %02x (%sa Topaz tag (%scapable of carrying a NDEF message), %s memory map)", rid_response
[0],
355 (rid_response
[0] & 0xF0) == 0x10 ? "" : "not ",
356 (rid_response
[0] & 0xF0) == 0x10 ? "" : "not ",
357 (rid_response
[0] & 0x0F) == 0x10 ? "static" : "dynamic");
358 PrintAndLog("HR1 : %02x", rid_response
[1]);
360 status
= topaz_rall(uid_echo
, rall_response
);
363 PrintAndLog("Error: tag didn't answer to RALL");
364 topaz_switch_off_field();
368 memcpy(topaz_tag
.uid
, rall_response
+2, 7);
369 PrintAndLog("UID : %02x %02x %02x %02x %02x %02x %02x",
377 PrintAndLog(" UID[6] (Manufacturer Byte) = %02x, Manufacturer: %s",
379 getTagInfo(topaz_tag
.uid
[6]));
381 memcpy(topaz_tag
.data_blocks
, rall_response
+2, 0x10*8);
383 PrintAndLog("Static Data blocks 00 to 0c:");
384 PrintAndLog("block# | offset | Data | Locked(y/n)");
386 for (uint16_t i
= 0; i
<= 0x0c; i
++) {
387 for (uint16_t j
= 0; j
< 8; j
++) {
388 sprintf(&line
[3*j
], "%02x ", topaz_tag
.data_blocks
[i
][j
] /*rall_response[2 + 8*i + j]*/);
390 PrintAndLog(" 0x%02x | 0x%04x | %s| %-3s", i
, i
*8, line
, topaz_byte_is_locked(i
*8) ? "yyyyyyyy" : "nnnnnnnn");
394 PrintAndLog("Static Reserved block 0d:");
395 for (uint16_t j
= 0; j
< 8; j
++) {
396 sprintf(&line
[3*j
], "%02x ", topaz_tag
.data_blocks
[0x0d][j
]);
398 PrintAndLog(" 0x%02x | 0x%04x | %s| %-3s", 0x0d, 0x0d*8, line
, "n/a");
401 PrintAndLog("Static Lockbits and OTP Bytes:");
402 for (uint16_t j
= 0; j
< 8; j
++) {
403 sprintf(&line
[3*j
], "%02x ", topaz_tag
.data_blocks
[0x0e][j
]);
405 PrintAndLog(" 0x%02x | 0x%04x | %s| %-3s", 0x0e, 0x0e*8, line
, "n/a");
409 status
= topaz_print_CC(&topaz_tag
.data_blocks
[1][0]);
412 PrintAndLog("No NDEF message present");
413 topaz_switch_off_field();
418 bool lock_TLV_present
= topaz_print_lock_control_TLVs(&topaz_tag
.data_blocks
[1][4]);
421 bool reserved_mem_present
= topaz_print_reserved_memory_control_TLVs(&topaz_tag
.data_blocks
[1][4]);
423 topaz_print_lifecycle_state(&topaz_tag
.data_blocks
[1][0]);
425 topaz_print_NDEF(&topaz_tag
.data_blocks
[1][0]);
427 topaz_switch_off_field();
432 int CmdHFTopazSim(const char *Cmd
)
434 PrintAndLog("not yet implemented");
439 int CmdHFTopazCmdRaw(const char *Cmd
)
441 PrintAndLog("not yet implemented");
446 static int CmdHelp(const char *Cmd
);
449 static command_t CommandTable
[] =
451 {"help", CmdHelp
, 1, "This help"},
452 {"reader", CmdHFTopazReader
, 0, "Act like a Topaz reader"},
453 {"sim", CmdHFTopazSim
, 0, "<UID> -- Simulate Topaz tag"},
454 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop a Topaz reader-tag communication"},
455 {"raw", CmdHFTopazCmdRaw
, 0, "Send raw hex data to tag"},
456 {NULL
, NULL
, 0, NULL
}
460 int CmdHFTopaz(const char *Cmd
) {
462 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
465 CmdsParse(CommandTable
, Cmd
);
469 static int CmdHelp(const char *Cmd
)
471 CmdsHelp(CommandTable
);