]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfnedap.c
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 NEDAP tag commands
8 //-----------------------------------------------------------------------------
11 #include "cmdlfnedap.h"
12 static int CmdHelp(const char *Cmd
);
14 int usage_lf_nedap_clone(void){
15 PrintAndLog("clone a NEDAP tag to a T55x7 tag.");
17 PrintAndLog("Usage: lf nedap clone <Card-Number>");
18 PrintAndLog("Options :");
19 PrintAndLog(" <Card Number> : 24-bit value card number");
20 // PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip");
22 PrintAndLog("Sample : lf nedap clone 112233");
26 int usage_lf_nedap_sim(void) {
27 PrintAndLog("Enables simulation of NEDAP card with specified card number.");
28 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
30 PrintAndLog("Usage: lf nedap sim <Card-Number>");
31 PrintAndLog("Options :");
32 PrintAndLog(" <Card Number> : 24-bit value card number");
34 PrintAndLog("Sample : lf nedap sim 112233");
38 int GetNedapBits(uint32_t cn
, uint8_t *nedapBits
) {
41 memset(pre
, 0x00, sizeof(pre
));
43 // preamble 1111 1111 10 = 0XF8
44 num_to_bytebits(0xF8, 10, pre
);
46 // fixed tagtype code? 0010 1101 = 0x2D
47 num_to_bytebits(0x2D, 8, pre
+10);
49 // 46 encrypted bits - UNKNOWN ALGO
50 // -- 16 bits checksum. Should be 4x4 checksum, based on UID and 2 constant values.
51 // -- 30 bits undocumented?
52 num_to_bytebits(cn
, 46, pre
+18);
54 //----from this part, the UID in clear text, with a 1bit ZERO as separator between bytes.
58 num_to_bytebits(cn
, 24, pre
+64);
67 // add paritybits (bitsource, dest, sourcelen, paritylen, parityType (odd, even,)
68 addParity(pre
+64, pre
+64, 128, 8, 1);
69 //1111111110001011010000010110100011001001000010110101001101011001000110011010010000000000100001110001001000000001000101011100111
73 //NEDAP demod - ASK/Biphase, RF/64 with preamble of 1111111110 (always a 128 bit data stream)
74 //print NEDAP Prox ID, encoding, encrypted ID,
75 int CmdFSKdemodNedap(const char *Cmd
) {
76 //raw ask demod no start bit finding just get binary from wave
77 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
]={0};
78 size_t size
= getFromGraphBuf(BitStream
);
79 if (size
==0) return 0;
81 //get binary from ask wave
82 if (!ASKbiphaseDemod("0 64 1 0", FALSE
)) {
83 if (g_debugMode
) PrintAndLog("Error NEDAP: ASKbiphaseDemod failed");
87 size
= DemodBufferLen
;
89 int idx
= NedapDemod(BitStream
, &size
);
93 PrintAndLog("DEBUG: Error - not enough samples");
95 PrintAndLog("DEBUG: Error - only noise found");
97 PrintAndLog("DEBUG: Error - problem during ASK/Biphase demod");
99 PrintAndLog("DEBUG: Error - Size not correct: %d", size
);
101 PrintAndLog("DEBUG: Error - NEDAP preamble not found");
103 PrintAndLog("DEBUG: Error - idx: %d",idx
);
111 preamble enc tag type encrypted uid d 33 d 90 p 04 d 71 d 40 d 45 d E7 P
112 1111111110 00101101000001011 0100011001001000010110101001101011001 0 00110011 0 10010000 0 00000100 0 01110001 0 01000000 0 01000101 0 11100111 1
113 uid2 uid1 uid0 I I R R
115 I = Identical on all tags
117 UID2, UID1, UID0 == card number
120 //get raw ID before removing parities
121 uint32_t rawLo
= bytebits_to_byte(BitStream
+idx
+96,32);
122 uint32_t rawHi
= bytebits_to_byte(BitStream
+idx
+64,32);
123 uint32_t rawHi2
= bytebits_to_byte(BitStream
+idx
+32,32);
124 uint32_t rawHi3
= bytebits_to_byte(BitStream
+idx
,32);
125 setDemodBuf(BitStream
,128,idx
);
127 // ok valid card found!
128 uint32_t cardnum
= bytebits_to_byte(BitStream
+81, 16);
129 PrintAndLog("NEDAP ID Found - Card: %d - Raw: %08x%08x%08x%08x", cardnum
, rawHi3
, rawHi2
, rawHi
, rawLo
);
132 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx
, 128);
139 int CmdLFNedapRead(const char *Cmd
) {
141 getSamples("30000",false);
142 return CmdFSKdemodNedap("");
145 int CmdLFNedapClone(const char *Cmd) {
147 char cmdp = param_getchar(Cmd, 0);
148 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_clone();
150 uint32_t cardnumber=0, cn = 0;
154 memset(bs, 0x00, sizeof(bs));
156 if (sscanf(Cmd, "%u", &cn ) != 1) return usage_lf_nedap_clone();
158 cardnumber = (cn & 0x00FFFFFF);
160 if ( !GetNedapBits(cardnumber, bs)) {
161 PrintAndLog("Error with tag bitstream generation.");
165 ((ASK/biphase data rawdemod ab 0 64 1 0
166 //NEDAP - compat mode, ASK/Biphase, data rate 64, 4 data blocks
167 blocks[0] = T55x7_MODULATION_BIPHASE | T55x7_BITRATE_RF_64 | 4<<T55x7_MAXBLOCK_SHIFT;
169 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
170 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT;
172 blocks[1] = bytebits_to_byte(bs,32);
173 blocks[2] = bytebits_to_byte(bs+32,32);
174 blocks[3] = bytebits_to_byte(bs+64,32);
175 blocks[4] = bytebits_to_byte(bs+96,32);
177 PrintAndLog("Preparing to clone NEDAP to T55x7 with card number: %u", cardnumber);
178 PrintAndLog("Blk | Data ");
179 PrintAndLog("----+------------");
180 for ( i = 0; i<5; ++i )
181 PrintAndLog(" %02d | %08" PRIx32, i, blocks[i]);
184 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
186 for ( i = 0; i<5; ++i ) {
187 c.arg[0] = blocks[i];
189 clearCommandBuffer();
191 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
192 PrintAndLog("Error occurred, device did not respond during write operation.");
200 int CmdLFNedapSim(const char *Cmd
) {
202 char cmdp
= param_getchar(Cmd
, 0);
203 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_nedap_sim();
205 uint32_t cardnumber
= 0, cn
= 0;
208 size_t size
= sizeof(bs
);
209 memset(bs
, 0x00, size
);
211 // NEDAP, Bihase = 2, clock 64, inverted,
212 uint8_t encoding
= 2, separator
= 0, clk
=64, invert
=1;
214 arg1
= clk
<< 8 | encoding
;
215 arg2
= invert
<< 8 | separator
;
217 if (sscanf(Cmd
, "%u", &cn
) != 2) return usage_lf_nedap_sim();
218 cardnumber
= (cn
& 0x00FFFFFF);
220 if ( !GetNedapBits(cardnumber
, bs
)) {
221 PrintAndLog("Error with tag bitstream generation.");
225 PrintAndLog("Simulating Nedap - CardNumber: %u", cardnumber
);
227 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
228 memcpy(c
.d
.asBytes
, bs
, size
);
229 clearCommandBuffer();
234 static command_t CommandTable
[] = {
235 {"help", CmdHelp
, 1, "This help"},
236 {"read", CmdLFNedapRead
, 0, "Attempt to read and extract tag data"},
237 // {"clone", CmdLFNedapClone, 0, "<Card Number> clone nedap tag"},
238 {"sim", CmdLFNedapSim
, 0, "<Card Number> simulate nedap tag"},
239 {NULL
, NULL
, 0, NULL
}
242 int CmdLFNedap(const char *Cmd
) {
243 clearCommandBuffer();
244 CmdsParse(CommandTable
, Cmd
);
248 int CmdHelp(const char *Cmd
) {
249 CmdsHelp(CommandTable
);