1 //-----------------------------------------------------------------------------
3 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
4 // at your option, any later version. See the LICENSE.txt file for the text of
6 //-----------------------------------------------------------------------------
7 // Low frequency Noralsy tag commands
8 // ASK/Manchester, STT, RF/32, 96 bits long (some bits unknown)
9 //-----------------------------------------------------------------------------
10 #include "cmdlfnoralsy.h"
14 #include "commonutil.h" // ARRAYLEN
15 #include "cmdparser.h" // command_t
20 #include "protocols.h" // for T55xx config register definitions
21 #include "lfdemod.h" // parityTest
22 #include "cmdlft55xx.h" // verifywrite
23 #include "cmdlfem4x05.h" //
24 #include "cliparser.h"
26 static int CmdHelp(const char *Cmd
);
28 static uint8_t noralsy_chksum(uint8_t *bits
, uint8_t len
) {
30 for (uint8_t i
= 0; i
< len
; i
+= 4)
31 sum
^= bytebits_to_byte(bits
+ i
, 4);
35 //see ASKDemod for what args are accepted
36 int demodNoralsy(bool verbose
) {
37 (void) verbose
; // unused so far
40 if (ASKDemod_ext(32, 0, 0, 0, false, false, false, 1, &st
) != PM3_SUCCESS
) {
41 if (g_debugMode
) PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: ASK/Manchester Demod failed");
45 if (g_debugMode
) PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: sequence terminator not found");
49 size_t size
= DemodBufferLen
;
50 int ans
= detectNoralsy(DemodBuffer
, &size
);
54 PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: too few bits found");
56 PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: preamble not found");
58 PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: Size not correct: %zu", size
);
60 PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: ans: %d", ans
);
64 setDemodBuff(DemodBuffer
, 96, ans
);
65 setClockGrid(g_DemodClock
, g_DemodStartIdx
+ (ans
* g_DemodClock
));
68 uint32_t raw1
= bytebits_to_byte(DemodBuffer
, 32);
69 uint32_t raw2
= bytebits_to_byte(DemodBuffer
+ 32, 32);
70 uint32_t raw3
= bytebits_to_byte(DemodBuffer
+ 64, 32);
72 uint32_t cardid
= ((raw2
& 0xFFF00000) >> 20) << 16;
73 cardid
|= (raw2
& 0xFF) << 8;
74 cardid
|= ((raw3
& 0xFF000000) >> 24);
75 cardid
= BCD2DEC(cardid
);
77 uint16_t year
= (raw2
& 0x000ff000) >> 12;
79 year
+= (year
> 60) ? 1900 : 2000;
82 uint8_t calc1
= noralsy_chksum(DemodBuffer
+ 32, 40);
83 uint8_t calc2
= noralsy_chksum(DemodBuffer
, 76);
84 uint8_t chk1
= 0, chk2
= 0;
85 chk1
= bytebits_to_byte(DemodBuffer
+ 72, 4);
86 chk2
= bytebits_to_byte(DemodBuffer
+ 76, 4);
89 if (g_debugMode
) PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: checksum 1 failed %x - %x\n", chk1
, calc1
);
93 if (g_debugMode
) PrintAndLogEx(DEBUG
, "DEBUG: Error - Noralsy: checksum 2 failed %x - %x\n", chk2
, calc2
);
97 PrintAndLogEx(SUCCESS
, "Noralsy - Card: " _GREEN_("%u")", Year: " _GREEN_("%u") ", Raw: %08X%08X%08X", cardid
, year
, raw1
, raw2
, raw3
);
98 if (raw1
!= 0xBB0214FF) {
99 PrintAndLogEx(WARNING
, "Unknown bits set in first block! Expected 0xBB0214FF, Found: 0x%08X", raw1
);
100 PrintAndLogEx(WARNING
, "Please post this output in forum to further research on this format");
105 static int CmdNoralsyDemod(const char *Cmd
) {
106 CLIParserContext
*ctx
;
107 CLIParserInit(&ctx
, "lf noralsy demod",
108 "Try to find Noralsy preamble, if found decode / descramble data",
116 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
118 return demodNoralsy(true);
121 static int CmdNoralsyReader(const char *Cmd
) {
122 CLIParserContext
*ctx
;
123 CLIParserInit(&ctx
, "lf noralsy reader",
124 "read a Noralsy tag",
125 "lf noralsy reader -@ -> continuous reader mode"
130 arg_lit0("@", NULL
, "optional - continuous reader mode"),
133 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
134 bool cm
= arg_get_lit(ctx
, 1);
138 PrintAndLogEx(INFO
, "Press " _GREEN_("<Enter>") " to exit");
142 lf_read(false, 8000);
144 } while (cm
&& !kbd_enter_pressed());
148 static int CmdNoralsyClone(const char *Cmd
) {
149 CLIParserContext
*ctx
;
150 CLIParserInit(&ctx
, "lf noralsy clone",
151 "clone a Noralsy tag to a T55x7, Q5/T5555 or EM4305/4469 tag.",
152 "lf noralsy clone --cn 112233\n"
153 "lf noralsy clone --cn 112233 --q5 -> encode for Q5/T5555 tag\n"
154 "lf noralsy clone --cn 112233 --em -> encode for EM4305/4469"
159 arg_u64_1(NULL
, "cn", "<dec>", "Noralsy card ID"),
160 arg_u64_0("y", "year", "<dec>", "tag allocation year"),
161 arg_lit0(NULL
, "q5", "optional - specify writing to Q5/T5555 tag"),
162 arg_lit0(NULL
, "em", "optional - specify writing to EM4305/4469 tag"),
165 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
167 uint32_t id
= arg_get_u32_def(ctx
, 1, 0);
168 uint16_t year
= arg_get_u32_def(ctx
, 2, 2000);
169 bool q5
= arg_get_lit(ctx
, 3);
170 bool em
= arg_get_lit(ctx
, 4);
174 PrintAndLogEx(FAILED
, "Can't specify both Q5 and EM4305 at the same time");
178 uint32_t blocks
[4] = {T55x7_MODULATION_MANCHESTER
| T55x7_BITRATE_RF_32
| T55x7_ST_TERMINATOR
| 3 << T55x7_MAXBLOCK_SHIFT
, 0, 0};
179 char cardtype
[16] = {"T55x7"};
182 blocks
[0] = T5555_FIXED
| T5555_MODULATION_MANCHESTER
| T5555_SET_BITRATE(32) | T5555_ST_TERMINATOR
| 3 << T5555_MAXBLOCK_SHIFT
;
183 snprintf(cardtype
, sizeof(cardtype
), "Q5/T5555");
188 blocks
[0] = EM4305_NORALSY_CONFIG_BLOCK
;
189 snprintf(cardtype
, sizeof(cardtype
), "EM4305/4469");
192 uint8_t *bits
= calloc(96, sizeof(uint8_t));
193 if (getnoralsyBits(id
, year
, bits
) != PM3_SUCCESS
) {
194 PrintAndLogEx(ERR
, "Error with tag bitstream generation.");
199 blocks
[1] = bytebits_to_byte(bits
, 32);
200 blocks
[2] = bytebits_to_byte(bits
+ 32, 32);
201 blocks
[3] = bytebits_to_byte(bits
+ 64, 32);
205 PrintAndLogEx(INFO
, "Preparing to clone Noralsy to " _YELLOW_("%s") " with Card id: " _GREEN_("%u"), cardtype
, id
);
206 print_blocks(blocks
, ARRAYLEN(blocks
));
210 res
= em4x05_clone_tag(blocks
, ARRAYLEN(blocks
), 0, false);
212 res
= clone_t55xx_tag(blocks
, ARRAYLEN(blocks
));
214 PrintAndLogEx(SUCCESS
, "Done");
215 PrintAndLogEx(HINT
, "Hint: try " _YELLOW_("`lf noralsy reader`") " to verify");
219 static int CmdNoralsySim(const char *Cmd
) {
221 CLIParserContext
*ctx
;
222 CLIParserInit(&ctx
, "lf noralsy sim",
223 "Enables simulation of Noralsy card with specified card number.\n"
224 "Simulation runs until the button is pressed or another USB command is issued.\n",
225 "lf noralsy sim --cn 1337\n"
226 "lf noralsy sim --cn 1337 --year 2010"
231 arg_u64_1(NULL
, "cn", "<dec>", "Noralsy card ID"),
232 arg_u64_0("y", "year", "<dec>", "tag allocation year"),
235 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
236 uint32_t id
= arg_get_u32_def(ctx
, 1, 0);
237 uint16_t year
= arg_get_u32_def(ctx
, 2, 2000);
241 memset(bs
, 0, sizeof(bs
));
243 if (getnoralsyBits(id
, year
, bs
) != PM3_SUCCESS
) {
244 PrintAndLogEx(ERR
, "Error with tag bitstream generation.");
248 PrintAndLogEx(SUCCESS
, "Simulating Noralsy - CardId: " _YELLOW_("%u") " year " _YELLOW_("%u"), id
, year
);
250 lf_asksim_t
*payload
= calloc(1, sizeof(lf_asksim_t
) + sizeof(bs
));
251 payload
->encoding
= 1;
253 payload
->separator
= 1;
255 memcpy(payload
->data
, bs
, sizeof(bs
));
257 clearCommandBuffer();
258 SendCommandNG(CMD_LF_ASK_SIMULATE
, (uint8_t *)payload
, sizeof(lf_asksim_t
) + sizeof(bs
));
261 PacketResponseNG resp
;
262 WaitForResponse(CMD_LF_ASK_SIMULATE
, &resp
);
264 PrintAndLogEx(INFO
, "Done");
265 if (resp
.status
!= PM3_EOPABORTED
)
271 static command_t CommandTable
[] = {
272 {"help", CmdHelp
, AlwaysAvailable
, "This help"},
273 {"demod", CmdNoralsyDemod
, AlwaysAvailable
, "demodulate an Noralsy tag from the GraphBuffer"},
274 {"reader", CmdNoralsyReader
, IfPm3Lf
, "attempt to read and extract tag data"},
275 {"clone", CmdNoralsyClone
, IfPm3Lf
, "clone Noralsy tag to T55x7 or Q5/T5555"},
276 {"sim", CmdNoralsySim
, IfPm3Lf
, "simulate Noralsy tag"},
277 {NULL
, NULL
, NULL
, NULL
}
280 static int CmdHelp(const char *Cmd
) {
281 (void)Cmd
; // Cmd is not used so far
282 CmdsHelp(CommandTable
);
286 int CmdLFNoralsy(const char *Cmd
) {
287 clearCommandBuffer();
288 return CmdsParse(CommandTable
, Cmd
);
291 int getnoralsyBits(uint32_t id
, uint16_t year
, uint8_t *bits
) {
293 num_to_bytebits(0xBB0214FF, 32, bits
); // --> Have seen 0xBB0214FF / 0xBB0314FF UNKNOWN
295 //convert ID into BCD-format
297 year
= DEC2BCD(year
);
300 uint16_t sub1
= (id
& 0xFFF0000) >> 16;
301 uint8_t sub2
= (id
& 0x000FF00) >> 8;
302 uint8_t sub3
= (id
& 0x00000FF);
304 num_to_bytebits(sub1
, 12, bits
+ 32);
305 num_to_bytebits(year
, 8, bits
+ 44);
306 num_to_bytebits(0, 4, bits
+ 52); // --> UNKNOWN. Flag?
308 num_to_bytebits(sub2
, 8, bits
+ 56);
309 num_to_bytebits(sub3
, 8, bits
+ 64);
312 uint8_t chksum
= noralsy_chksum(bits
+ 32, 40);
313 num_to_bytebits(chksum
, 4, bits
+ 72);
314 chksum
= noralsy_chksum(bits
, 76);
315 num_to_bytebits(chksum
, 4, bits
+ 76);
319 // find Noralsy preamble in already demoded data
320 int detectNoralsy(uint8_t *dest
, size_t *size
) {
321 if (*size
< 96) return -1; //make sure buffer has data
323 uint8_t preamble
[] = {1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0};
324 if (!preambleSearch(dest
, preamble
, sizeof(preamble
), size
, &startIdx
))
325 return -2; //preamble not found
326 if (*size
!= 96) return -3; //wrong demoded size
327 //return start position
328 return (int)startIdx
;
332 * 2520116 | BB0214FF2529900116360000 | 10111011 00000011 00010100 11111111 00100101 00101001 10010000 00000001 00010110 00110110 00000000 00000000
333 * aaa*aaaaiiiYY*iiiicc---- **** iiiiiiii iiiiYYYY YYYY**** iiiiiiii iiiiiiii cccccccc
335 * a = fixed value BB0*14FF
336 * i = printed id, BCD-format