]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfnedap.c
ADD: added some identification on between old Desfire, Desfire EV1 and Desfire EV2
[proxmark3-svn] / client / cmdlfnedap.c
CommitLineData
56bbb25a 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"
12static int CmdHelp(const char *Cmd);
13
14int 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
26int 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
38int 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);
c71f4da9 69
56bbb25a 70//1111111110001011010000010110100011001001000010110101001101011001000110011010010000000000100001110001001000000001000101011100111
71 return 1;
72}
73
c71f4da9 74//GetParity( uint8_t *bits, uint8_t type, int length)
75
56bbb25a 76//NEDAP demod - ASK/Biphase, RF/64 with preamble of 1111111110 (always a 128 bit data stream)
77//print NEDAP Prox ID, encoding, encrypted ID,
c71f4da9 78
79int CmdLFNedapDemod(const char *Cmd) {
56bbb25a 80 //raw ask demod no start bit finding just get binary from wave
81 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
82 size_t size = getFromGraphBuf(BitStream);
83 if (size==0) return 0;
84
96faed21 85 //get binary from ask wave
c71f4da9 86 if (!ASKbiphaseDemod("0 64 0 0", FALSE)) {
96faed21 87 if (g_debugMode) PrintAndLog("Error NEDAP: ASKbiphaseDemod failed");
88 return 0;
89 }
d34a0b0c 90 size = DemodBufferLen;
c71f4da9 91 int idx = NedapDemod(DemodBuffer, &size);
56bbb25a 92 if (idx < 0){
93 if (g_debugMode){
94 if (idx == -5)
95 PrintAndLog("DEBUG: Error - not enough samples");
96 else if (idx == -1)
97 PrintAndLog("DEBUG: Error - only noise found");
98 else if (idx == -2)
99 PrintAndLog("DEBUG: Error - problem during ASK/Biphase demod");
100 else if (idx == -3)
101 PrintAndLog("DEBUG: Error - Size not correct: %d", size);
102 else if (idx == -4)
103 PrintAndLog("DEBUG: Error - NEDAP preamble not found");
104 else
105 PrintAndLog("DEBUG: Error - idx: %d",idx);
106 }
107 return 0;
108 }
109
532bcd71 110/* Index map O
111 preamble enc tag type encrypted uid P d 33 d 90 d 04 d 71 d 40 d 45 d E7 P
812513bf 1121111111110 0010110100000101101000110010010000101101010011010110 0 1 0 00110011 0 10010000 0 00000100 0 01110001 0 01000000 0 01000101 0 11100111 1
532bcd71 113 uid2 uid1 uid0 I I R R
56bbb25a 114 Tag ID is 049033
115 I = Identical on all tags
116 R = Random ?
117 UID2, UID1, UID0 == card number
532bcd71 118
56bbb25a 119*/
56bbb25a 120 //get raw ID before removing parities
c71f4da9 121 uint32_t rawLo = bytebits_to_byte(DemodBuffer+idx+96,32);
122 uint32_t rawHi = bytebits_to_byte(DemodBuffer+idx+64,32);
123 uint32_t rawHi2 = bytebits_to_byte(DemodBuffer+idx+32,32);
124 uint32_t rawHi3 = bytebits_to_byte(DemodBuffer+idx,32);
125 setDemodBuf(DemodBuffer,128,idx);
56bbb25a 126
532bcd71 127 // here 62 is length.
128 uint8_t first63bit_parity = GetParity( DemodBuffer, ODD, 62);
129 // Parity bits: 0-62 == 63
130 // here 62 is pos in a zero based array
131 if ( first63bit_parity != DemodBuffer[62] ) {
132 PrintAndLog("1st half parity check failed: %d | %d ", DemodBuffer[62], first63bit_parity);
133 return 0;
134 }
135
136 // uint8_t second63bit_parity = GetParity( DemodBuffer+64, ODD, 63);
137 // if ( second63bit_parity != DemodBuffer[127] ) {
138 // PrintAndLog("2st half parity check failed: %d | %d ", DemodBuffer[127], second63bit_parity);
139 // return 0;
140 // }
141
56bbb25a 142 // ok valid card found!
c71f4da9 143 uint32_t cardnum = bytebits_to_byte(DemodBuffer+81, 16);
56bbb25a 144 PrintAndLog("NEDAP ID Found - Card: %d - Raw: %08x%08x%08x%08x", cardnum, rawHi3, rawHi2, rawHi, rawLo);
145
146 if (g_debugMode){
147 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
148 printDemodBuff();
149 }
532bcd71 150 PrintAndLog("BIN: %s", sprint_bin_break( DemodBuffer, 64, 64) );
151
c71f4da9 152
56bbb25a 153 return 1;
154}
c71f4da9 155/*
156lf t55 wr b 1 d FF8B4168
157lf t55 wr b 2 d C90B5359
158lf t55 wr b 3 d 19A40087
159lf t55 wr b 4 d 120115CF
56bbb25a 160
c71f4da9 1610)
162lf t55 wr b 1 d FF8B6B20
163lf t55 wr b 2 d F19B84A3
164lf t55 wr b 3 d 18058007
165lf t55 wr b 4 d 1200857C
166
167*/
56bbb25a 168
169int CmdLFNedapRead(const char *Cmd) {
170 CmdLFRead("s");
171 getSamples("30000",false);
c71f4da9 172 return CmdLFNedapDemod("");
56bbb25a 173}
174/*
175int CmdLFNedapClone(const char *Cmd) {
176
177 char cmdp = param_getchar(Cmd, 0);
178 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_clone();
179
180 uint32_t cardnumber=0, cn = 0;
181 uint32_t blocks[5];
182 uint8_t i;
183 uint8_t bs[128];
184 memset(bs, 0x00, sizeof(bs));
185
186 if (sscanf(Cmd, "%u", &cn ) != 1) return usage_lf_nedap_clone();
187
188 cardnumber = (cn & 0x00FFFFFF);
189
190 if ( !GetNedapBits(cardnumber, bs)) {
191 PrintAndLog("Error with tag bitstream generation.");
192 return 1;
193 }
194
195 ((ASK/biphase data rawdemod ab 0 64 1 0
196 //NEDAP - compat mode, ASK/Biphase, data rate 64, 4 data blocks
197 blocks[0] = T55x7_MODULATION_BIPHASE | T55x7_BITRATE_RF_64 | 4<<T55x7_MAXBLOCK_SHIFT;
198
199 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
890ae3dd 200 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
56bbb25a 201 blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT;
202
203 blocks[1] = bytebits_to_byte(bs,32);
204 blocks[2] = bytebits_to_byte(bs+32,32);
205 blocks[3] = bytebits_to_byte(bs+64,32);
206 blocks[4] = bytebits_to_byte(bs+96,32);
207
208 PrintAndLog("Preparing to clone NEDAP to T55x7 with card number: %u", cardnumber);
209 PrintAndLog("Blk | Data ");
210 PrintAndLog("----+------------");
211 for ( i = 0; i<5; ++i )
212 PrintAndLog(" %02d | %08" PRIx32, i, blocks[i]);
213
214 UsbCommand resp;
215 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
216
217 for ( i = 0; i<5; ++i ) {
218 c.arg[0] = blocks[i];
219 c.arg[1] = i;
220 clearCommandBuffer();
221 SendCommand(&c);
222 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
223 PrintAndLog("Error occurred, device did not respond during write operation.");
224 return -1;
225 }
226 }
227 return 0;
228}
229*/
230
231int CmdLFNedapSim(const char *Cmd) {
232
233 char cmdp = param_getchar(Cmd, 0);
234 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_nedap_sim();
235
236 uint32_t cardnumber = 0, cn = 0;
237
238 uint8_t bs[128];
239 size_t size = sizeof(bs);
240 memset(bs, 0x00, size);
241
242 // NEDAP, Bihase = 2, clock 64, inverted,
243 uint8_t encoding = 2, separator = 0, clk=64, invert=1;
244 uint16_t arg1, arg2;
245 arg1 = clk << 8 | encoding;
246 arg2 = invert << 8 | separator;
247
248 if (sscanf(Cmd, "%u", &cn ) != 2) return usage_lf_nedap_sim();
249 cardnumber = (cn & 0x00FFFFFF);
250
251 if ( !GetNedapBits(cardnumber, bs)) {
252 PrintAndLog("Error with tag bitstream generation.");
253 return 1;
254 }
255
256 PrintAndLog("Simulating Nedap - CardNumber: %u", cardnumber );
257
258 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
259 memcpy(c.d.asBytes, bs, size);
260 clearCommandBuffer();
261 SendCommand(&c);
262 return 0;
263}
264
812513bf 265
266
c71f4da9 267int CmdLFNedapChk(const char *Cmd){
268
532bcd71 269 uint8_t data[256] = { 0x30, 0x16, 0x00, 0x71, 0x40, 0x21, 0xBE};
c71f4da9 270 int len = 0;
271 param_gethex_ex(Cmd, 0, data, &len);
272
273 len = ( len == 0 ) ? 5 : len>>1;
274
275 PrintAndLog("Input: [%d] %s", len, sprint_hex(data, len));
276
812513bf 277 //uint8_t last = GetParity(data, EVEN, 62);
278 //PrintAndLog("TEST PARITY:: %d | %d ", DemodBuffer[62], last);
532bcd71 279
c71f4da9 280 uint8_t cl = 0x1D, ch = 0x1D, carry = 0;
281 uint8_t al, bl, temp;
282
283 for (int i = 0; i < len; ++i){
284 al = data[i];
285 for (int j = 8; j > 0; --j) {
286
287 bl = al ^ ch;
288 //printf("BL %02x | CH %02x \n", al, ch);
289
290 carry = (cl & 0x80) ? 1 : 0;
291 cl <<= 1;
292
293 temp = (ch & 0x80) ? 1 : 0;
294 ch = (ch << 1) | carry;
295 carry = temp;
296
297 carry = (al & 0x80) ? 1 : 0;
298 al <<= 1;
299
300 carry = (bl & 0x80) ? 1 : 0;
301 bl <<= 1;
302
303 if (carry) {
304 cl ^= 0x21;
305 ch ^= 0x10;
306 }
307 }
308 }
309
310 PrintAndLog("Nedap checksum: [ 0x21, 0xBE ] %x", ((ch << 8) | cl) );
311 return 0;
312}
313
314
56bbb25a 315static command_t CommandTable[] = {
316 {"help", CmdHelp, 1, "This help"},
c71f4da9 317 {"read", CmdLFNedapRead, 0, "Attempt to read and extract tag data"},
318// {"clone", CmdLFNedapClone,0, "<Card Number> clone nedap tag"},
319 {"sim", CmdLFNedapSim, 0, "<Card Number> simulate nedap tag"},
320 {"chk", CmdLFNedapChk, 1, "Calculate Nedap Checksum <uid bytes>"},
56bbb25a 321 {NULL, NULL, 0, NULL}
322};
323
324int CmdLFNedap(const char *Cmd) {
325 clearCommandBuffer();
326 CmdsParse(CommandTable, Cmd);
327 return 0;
328}
329
330int CmdHelp(const char *Cmd) {
331 CmdsHelp(CommandTable);
332 return 0;
333}
Impressum, Datenschutz