]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfnedap.c
ADD: started with a NEDAP demod, read, clone and sim functionality.
[proxmark3-svn] / client / cmdlfnedap.c
1 //-----------------------------------------------------------------------------
2 //
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
5 // the license.
6 //-----------------------------------------------------------------------------
7 // Low frequency NEDAP tag commands
8 //-----------------------------------------------------------------------------
9 #include <string.h>
10 #include <inttypes.h>
11 #include "cmdlfnedap.h"
12 static int CmdHelp(const char *Cmd);
13
14 int usage_lf_nedap_clone(void){
15 PrintAndLog("clone a NEDAP tag to a T55x7 tag.");
16 PrintAndLog("");
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");
21 PrintAndLog("");
22 PrintAndLog("Sample : lf nedap clone 112233");
23 return 0;
24 }
25
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.");
29 PrintAndLog("");
30 PrintAndLog("Usage: lf nedap sim <Card-Number>");
31 PrintAndLog("Options :");
32 PrintAndLog(" <Card Number> : 24-bit value card number");
33 PrintAndLog("");
34 PrintAndLog("Sample : lf nedap sim 112233");
35 return 0;
36 }
37
38 int GetNedapBits(uint32_t cn, uint8_t *nedapBits) {
39
40 uint8_t pre[128];
41 memset(pre, 0x00, sizeof(pre));
42
43 // preamble 1111 1111 10 = 0XF8
44 num_to_bytebits(0xF8, 10, pre);
45
46 // fixed tagtype code? 0010 1101 = 0x2D
47 num_to_bytebits(0x2D, 8, pre+10);
48
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);
53
54 //----from this part, the UID in clear text, with a 1bit ZERO as separator between bytes.
55 pre[64] = 0;
56
57 // cardnumber
58 num_to_bytebits(cn, 24, pre+64);
59
60 pre[73] = 0;
61 pre[82] = 0;
62 pre[91] = 0;
63 pre[100] = 0;
64 pre[109] = 0;
65 pre[118] = 0;
66
67 // add paritybits (bitsource, dest, sourcelen, paritylen, parityType (odd, even,)
68 addParity(pre+64, pre+64, 128, 8, 1);
69 //1111111110001011010000010110100011001001000010110101001101011001000110011010010000000000100001110001001000000001000101011100111
70 return 1;
71 }
72
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;
80
81 //get binary from fsk wave
82 int idx = NedapDemod(BitStream, &size);
83 if (idx < 0){
84 if (g_debugMode){
85 if (idx == -5)
86 PrintAndLog("DEBUG: Error - not enough samples");
87 else if (idx == -1)
88 PrintAndLog("DEBUG: Error - only noise found");
89 else if (idx == -2)
90 PrintAndLog("DEBUG: Error - problem during ASK/Biphase demod");
91 else if (idx == -3)
92 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
93 else if (idx == -4)
94 PrintAndLog("DEBUG: Error - NEDAP preamble not found");
95 else
96 PrintAndLog("DEBUG: Error - idx: %d",idx);
97 }
98 return 0;
99 }
100
101 /* Index map
102 0 10 20 30 40 50 64
103 | | | | | | |
104 preamble enc tag type encrypted uid d 33 d 90 p 04 d 71 d 40 d 45 d E7 P
105 1111111110 00101101000001011 0100011001001000010110101001101011001 0 00110011 0 10010000 0 00000100 0 01110001 0 01000000 0 01000101 0 11100111 1
106 uid2 uid1 uid0 I I R R
107 Tag ID is 049033
108 I = Identical on all tags
109 R = Random ?
110 UID2, UID1, UID0 == card number
111 */
112
113 //get raw ID before removing parities
114 uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32);
115 uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32);
116 uint32_t rawHi2 = bytebits_to_byte(BitStream+idx+32,32);
117 uint32_t rawHi3 = bytebits_to_byte(BitStream+idx,32);
118 setDemodBuf(BitStream,128,idx);
119
120 // ok valid card found!
121 uint32_t cardnum = bytebits_to_byte(BitStream+81, 16);
122 PrintAndLog("NEDAP ID Found - Card: %d - Raw: %08x%08x%08x%08x", cardnum, rawHi3, rawHi2, rawHi, rawLo);
123
124 if (g_debugMode){
125 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
126 printDemodBuff();
127 }
128 return 1;
129 }
130
131
132 int CmdLFNedapRead(const char *Cmd) {
133 CmdLFRead("s");
134 getSamples("30000",false);
135 return CmdFSKdemodNedap("");
136 }
137 /*
138 int CmdLFNedapClone(const char *Cmd) {
139
140 char cmdp = param_getchar(Cmd, 0);
141 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_clone();
142
143 uint32_t cardnumber=0, cn = 0;
144 uint32_t blocks[5];
145 uint8_t i;
146 uint8_t bs[128];
147 memset(bs, 0x00, sizeof(bs));
148
149 if (sscanf(Cmd, "%u", &cn ) != 1) return usage_lf_nedap_clone();
150
151 cardnumber = (cn & 0x00FFFFFF);
152
153 if ( !GetNedapBits(cardnumber, bs)) {
154 PrintAndLog("Error with tag bitstream generation.");
155 return 1;
156 }
157
158 ((ASK/biphase data rawdemod ab 0 64 1 0
159 //NEDAP - compat mode, ASK/Biphase, data rate 64, 4 data blocks
160 blocks[0] = T55x7_MODULATION_BIPHASE | T55x7_BITRATE_RF_64 | 4<<T55x7_MAXBLOCK_SHIFT;
161
162 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
163 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT;
164
165 blocks[1] = bytebits_to_byte(bs,32);
166 blocks[2] = bytebits_to_byte(bs+32,32);
167 blocks[3] = bytebits_to_byte(bs+64,32);
168 blocks[4] = bytebits_to_byte(bs+96,32);
169
170 PrintAndLog("Preparing to clone NEDAP to T55x7 with card number: %u", cardnumber);
171 PrintAndLog("Blk | Data ");
172 PrintAndLog("----+------------");
173 for ( i = 0; i<5; ++i )
174 PrintAndLog(" %02d | %08" PRIx32, i, blocks[i]);
175
176 UsbCommand resp;
177 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
178
179 for ( i = 0; i<5; ++i ) {
180 c.arg[0] = blocks[i];
181 c.arg[1] = i;
182 clearCommandBuffer();
183 SendCommand(&c);
184 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
185 PrintAndLog("Error occurred, device did not respond during write operation.");
186 return -1;
187 }
188 }
189 return 0;
190 }
191 */
192
193 int CmdLFNedapSim(const char *Cmd) {
194
195 char cmdp = param_getchar(Cmd, 0);
196 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_sim();
197
198 uint32_t cardnumber = 0, cn = 0;
199
200 uint8_t bs[128];
201 size_t size = sizeof(bs);
202 memset(bs, 0x00, size);
203
204 // NEDAP, Bihase = 2, clock 64, inverted,
205 uint8_t encoding = 2, separator = 0, clk=64, invert=1;
206 uint16_t arg1, arg2;
207 arg1 = clk << 8 | encoding;
208 arg2 = invert << 8 | separator;
209
210 if (sscanf(Cmd, "%u", &cn ) != 2) return usage_lf_nedap_sim();
211 cardnumber = (cn & 0x00FFFFFF);
212
213 if ( !GetNedapBits(cardnumber, bs)) {
214 PrintAndLog("Error with tag bitstream generation.");
215 return 1;
216 }
217
218 PrintAndLog("Simulating Nedap - CardNumber: %u", cardnumber );
219
220 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
221 memcpy(c.d.asBytes, bs, size);
222 clearCommandBuffer();
223 SendCommand(&c);
224 return 0;
225 }
226
227 static command_t CommandTable[] = {
228 {"help", CmdHelp, 1, "This help"},
229 {"read", CmdLFNedapRead, 0, "Attempt to read and extract tag data"},
230 // {"clone", CmdLFNedapClone, 0, "<Card Number> clone nedap tag"},
231 {"sim", CmdLFNedapSim, 0, "<Card Number> simulate nedap tag"},
232 {NULL, NULL, 0, NULL}
233 };
234
235 int CmdLFNedap(const char *Cmd) {
236 clearCommandBuffer();
237 CmdsParse(CommandTable, Cmd);
238 return 0;
239 }
240
241 int CmdHelp(const char *Cmd) {
242 CmdsHelp(CommandTable);
243 return 0;
244 }
Impressum, Datenschutz