1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Low frequency visa 2000 tag commands
18 // ASK/Manchester, RF/64, STT, 96 bits (complete)
19 //-----------------------------------------------------------------------------
21 #include "cmdlfvisa2000.h"
27 #include "commonutil.h" // ARRAYLEN
29 #include "cmdparser.h" // command_t
35 #include "protocols.h" // for T55xx config register definitions
36 #include "lfdemod.h" // parityTest
37 #include "cmdlft55xx.h" // write verify
38 #include "cmdlfem4x05.h" //
39 #include "cliparser.h"
42 #define VISA2k_BL0CK1 0x56495332
45 static int CmdHelp(const char *Cmd
);
47 static uint8_t visa_chksum(uint32_t id
) {
49 for (uint8_t i
= 0; i
< 32; i
+= 4)
50 sum
^= (id
>> i
) & 0xF;
54 static uint8_t visa_parity(uint32_t id
) {
56 const uint8_t par_lut
[] = {
57 0, 1, 1, 0, 1, 0, 0, 1,
58 1, 0, 0, 1, 0, 1, 1, 0
62 par
|= par_lut
[(id
>> 28) & 0xF ] << 7;
63 par
|= par_lut
[(id
>> 24) & 0xF ] << 6;
64 par
|= par_lut
[(id
>> 20) & 0xF ] << 5;
65 par
|= par_lut
[(id
>> 16) & 0xF ] << 4;
66 par
|= par_lut
[(id
>> 12) & 0xF ] << 3;
67 par
|= par_lut
[(id
>> 8) & 0xF ] << 2;
68 par
|= par_lut
[(id
>> 4) & 0xF ] << 1;
69 par
|= par_lut
[(id
& 0xF) ];
75 * 56495332 00096ebd 00000077 —> tag id 618173
76 * aaaaaaaa iiiiiiii -----ppc
78 * a = fixed value ascii 'VIS2'
80 * p = even parity bit for each nibble in card id.
81 * c = checksum (xor of card id)
84 //see ASKDemod for what args are accepted
85 int demodVisa2k(bool verbose
) {
86 (void) verbose
; // unused so far
87 buffer_savestate_t saveState
= save_bufferS32(g_GraphBuffer
, g_GraphTraceLen
);
88 saveState
.offset
= g_GridOffset
;
90 //CmdAskEdgeDetect("");
94 if (ASKDemod_ext(64, 0, 0, 0, false, false, false, 1, &st
) != PM3_SUCCESS
) {
95 PrintAndLogEx(DEBUG
, "DEBUG: Error - Visa2k: ASK/Manchester Demod failed");
96 restore_bufferS32(saveState
, g_GraphBuffer
);
97 g_GridOffset
= saveState
.offset
;
100 size_t size
= g_DemodBufferLen
;
101 int ans
= detectVisa2k(g_DemodBuffer
, &size
);
104 PrintAndLogEx(DEBUG
, "DEBUG: Error - Visa2k: too few bits found");
106 PrintAndLogEx(DEBUG
, "DEBUG: Error - Visa2k: preamble not found");
108 PrintAndLogEx(DEBUG
, "DEBUG: Error - Visa2k: Size not correct: %zu", size
);
110 PrintAndLogEx(DEBUG
, "DEBUG: Error - Visa2k: ans: %d", ans
);
112 restore_bufferS32(saveState
, g_GraphBuffer
);
113 g_GridOffset
= saveState
.offset
;
116 setDemodBuff(g_DemodBuffer
, 96, ans
);
117 setClockGrid(g_DemodClock
, g_DemodStartIdx
+ (ans
* g_DemodClock
));
120 uint32_t raw1
= bytebits_to_byte(g_DemodBuffer
, 32);
121 uint32_t raw2
= bytebits_to_byte(g_DemodBuffer
+ 32, 32);
122 uint32_t raw3
= bytebits_to_byte(g_DemodBuffer
+ 64, 32);
125 uint8_t calc
= visa_chksum(raw2
);
126 uint8_t chk
= raw3
& 0xF;
130 PrintAndLogEx(DEBUG
, "DEBUG: error: Visa2000 checksum (%s) %x - %x\n", _RED_("fail"), chk
, calc
);
131 restore_bufferS32(saveState
, g_GraphBuffer
);
132 g_GridOffset
= saveState
.offset
;
136 uint8_t calc_par
= visa_parity(raw2
);
137 uint8_t chk_par
= (raw3
& 0xFF0) >> 4;
138 if (calc_par
!= chk_par
) {
139 PrintAndLogEx(DEBUG
, "DEBUG: error: Visa2000 parity (%s) %x - %x\n", _RED_("fail"), chk_par
, calc_par
);
140 restore_bufferS32(saveState
, g_GraphBuffer
);
141 g_GridOffset
= saveState
.offset
;
144 PrintAndLogEx(SUCCESS
, "Visa2000 - Card " _GREEN_("%u") ", Raw: %08X%08X%08X", raw2
, raw1
, raw2
, raw3
);
148 static int CmdVisa2kDemod(const char *Cmd
) {
149 CLIParserContext
*ctx
;
150 CLIParserInit(&ctx
, "lf visa2000 demod",
151 "Try to find visa2000 preamble, if found decode / descramble data",
159 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
161 return demodVisa2k(true);
164 // 64*96*2=12288 samples just in case we just missed the first preamble we can still catch 2 of them
165 static int CmdVisa2kReader(const char *Cmd
) {
166 CLIParserContext
*ctx
;
167 CLIParserInit(&ctx
, "lf visa2000 reader",
168 "read a visa2000 tag",
169 "lf visa2000 reader -@ -> continuous reader mode"
174 arg_lit0("@", NULL
, "optional - continuous reader mode"),
177 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
178 bool cm
= arg_get_lit(ctx
, 1);
182 PrintAndLogEx(INFO
, "Press " _GREEN_("<Enter>") " to exit");
186 lf_read(false, 20000);
188 } while (cm
&& !kbd_enter_pressed());
192 static int CmdVisa2kClone(const char *Cmd
) {
194 CLIParserContext
*ctx
;
195 CLIParserInit(&ctx
, "lf visa2000 clone",
196 "clone a Visa2000 tag to a T55x7, Q5/T5555 or EM4305/4469 tag.",
197 "lf visa2000 clone --cn 112233 -> encode for T55x7 tag\n"
198 "lf visa2000 clone --cn 112233 --q5 -> encode for Q5/T5555 tag\n"
199 "lf visa2000 clone --cn 112233 --em -> encode for EM4305/4469"
204 arg_u64_1(NULL
, "cn", "<dec>", "Visa2k card ID"),
205 arg_lit0(NULL
, "q5", "optional - specify writing to Q5/T5555 tag"),
206 arg_lit0(NULL
, "em", "optional - specify writing to EM4305/4469 tag"),
209 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
211 uint32_t id
= arg_get_u32_def(ctx
, 1, 0);
212 bool q5
= arg_get_lit(ctx
, 2);
213 bool em
= arg_get_lit(ctx
, 3);
217 PrintAndLogEx(FAILED
, "Can't specify both Q5 and EM4305 at the same time");
221 uint32_t blocks
[4] = {T55x7_MODULATION_MANCHESTER
| T55x7_BITRATE_RF_64
| T55x7_ST_TERMINATOR
| 3 << T55x7_MAXBLOCK_SHIFT
, VISA2k_BL0CK1
, 0};
222 char cardtype
[16] = {"T55x7"};
225 blocks
[0] = T5555_FIXED
| T5555_MODULATION_MANCHESTER
| T5555_SET_BITRATE(64) | T5555_ST_TERMINATOR
| 3 << T5555_MAXBLOCK_SHIFT
;
226 snprintf(cardtype
, sizeof(cardtype
), "Q5/T5555");
231 blocks
[0] = EM4305_VISA2000_CONFIG_BLOCK
;
232 snprintf(cardtype
, sizeof(cardtype
), "EM4305/4469");
236 blocks
[3] = (visa_parity(id
) << 4) | visa_chksum(id
);
238 PrintAndLogEx(INFO
, "Preparing to clone Visa2000 to " _YELLOW_("%s") " with CardId: " _GREEN_("%"PRIu32
), cardtype
, id
);
239 print_blocks(blocks
, ARRAYLEN(blocks
));
243 res
= em4x05_clone_tag(blocks
, ARRAYLEN(blocks
), 0, false);
245 res
= clone_t55xx_tag(blocks
, ARRAYLEN(blocks
));
247 PrintAndLogEx(SUCCESS
, "Done!");
248 PrintAndLogEx(HINT
, "Hint: try " _YELLOW_("`lf visa2000 reader`") " to verify");
252 static int CmdVisa2kSim(const char *Cmd
) {
254 CLIParserContext
*ctx
;
255 CLIParserInit(&ctx
, "lf visa2000 sim",
256 "Enables simulation of visa2k card with specified card number.\n"
257 "Simulation runs until the button is pressed or another USB command is issued.\n",
258 "lf visa2000 sim --cn 1337"
263 arg_u64_1(NULL
, "cn", "<dec>", "Visa2k card ID"),
266 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
267 uint32_t id
= arg_get_u32_def(ctx
, 1, 0);
270 PrintAndLogEx(SUCCESS
, "Simulating Visa2000 - CardId:" _YELLOW_("%u"), id
);
272 uint32_t blocks
[3] = { VISA2k_BL0CK1
, id
, (visa_parity(id
) << 4) | visa_chksum(id
) };
275 for (int i
= 0; i
< 3; ++i
)
276 num_to_bytebits(blocks
[i
], 32, bs
+ i
* 32);
278 lf_asksim_t
*payload
= calloc(1, sizeof(lf_asksim_t
) + sizeof(bs
));
279 payload
->encoding
= 1;
281 payload
->separator
= 1;
283 memcpy(payload
->data
, bs
, sizeof(bs
));
285 clearCommandBuffer();
286 SendCommandNG(CMD_LF_ASK_SIMULATE
, (uint8_t *)payload
, sizeof(lf_asksim_t
) + sizeof(bs
));
289 PacketResponseNG resp
;
290 WaitForResponse(CMD_LF_ASK_SIMULATE
, &resp
);
292 PrintAndLogEx(INFO
, "Done!");
293 if (resp
.status
!= PM3_EOPABORTED
) {
299 static command_t CommandTable
[] = {
300 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
301 {"demod", CmdVisa2kDemod
, AlwaysAvailable
, "demodulate an VISA2000 tag from the GraphBuffer"},
302 {"reader", CmdVisa2kReader
, IfPm3Lf
, "attempt to read and extract tag data"},
303 {"clone", CmdVisa2kClone
, IfPm3Lf
, "clone Visa2000 tag to T55x7, Q5/T5555 or EM4305/4469"},
304 {"sim", CmdVisa2kSim
, IfPm3Lf
, "simulate Visa2000 tag"},
305 {NULL
, NULL
, NULL
, NULL
}
308 static int CmdHelp(const char *Cmd
) {
309 (void)Cmd
; // Cmd is not used so far
310 CmdsHelp(CommandTable
);
314 int CmdLFVisa2k(const char *Cmd
) {
315 clearCommandBuffer();
316 return CmdsParse(CommandTable
, Cmd
);
320 // find Visa2000 preamble in already demoded data
321 int detectVisa2k(uint8_t *dest
, size_t *size
) {
322 if (*size
< 96) return -1; //make sure buffer has data
324 uint8_t preamble
[] = {0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0};
325 if (!preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
))
326 return -2; //preamble not found
327 if (*size
!= 96) return -3; //wrong demoded size
328 //return start position
329 return (int)startIdx
;