]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdhf14b.c
FIX: Coverity, out-of-bounds write, CID# 121336, s_index should take factor in consi...
[proxmark3-svn] / client / cmdhf14b.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency ISO14443B commands
9//-----------------------------------------------------------------------------
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include "iso14443crc.h"
16#include "proxmark3.h"
17#include "data.h"
18#include "graph.h"
19#include "util.h"
20#include "ui.h"
21#include "cmdparser.h"
22#include "cmdhf14b.h"
23#include "cmdmain.h"
24#include "cmdhf14a.h"
25#include "tea.h"
26#include "cmdhf.h"
27#include "prng.h"
28#include "sha1.h"
29
30static int CmdHelp(const char *Cmd);
31
32int CmdHF14BList(const char *Cmd) {
33 CmdHFList("14b");
34 return 0;
35}
36
37int CmdHF14BSim(const char *Cmd)
38{
39 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443B};
40 clearCommandBuffer();
41 SendCommand(&c);
42 return 0;
43}
44
45int CmdHF14BSnoop(const char *Cmd)
46{
47 UsbCommand c = {CMD_SNOOP_ISO_14443B};
48 clearCommandBuffer();
49 SendCommand(&c);
50 return 0;
51}
52
53/* New command to read the contents of a SRI512 tag
54 * SRI512 tags are ISO14443-B modulated memory tags,
55 * this command just dumps the contents of the memory
56 */
57int CmdSri512Read(const char *Cmd)
58{
59 UsbCommand c = {CMD_READ_SRI512_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
60 clearCommandBuffer();
61 SendCommand(&c);
62 return 0;
63}
64
65/* New command to read the contents of a SRIX4K tag
66 * SRIX4K tags are ISO14443-B modulated memory tags,
67 * this command just dumps the contents of the memory/
68 */
69int CmdSrix4kRead(const char *Cmd)
70{
71 UsbCommand c = {CMD_READ_SRIX4K_TAG, {strtol(Cmd, NULL, 0), 0, 0}};
72 clearCommandBuffer();
73 SendCommand(&c);
74 return 0;
75}
76
77
78int rawClose(void){
79 UsbCommand resp;
80 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}};
81 clearCommandBuffer();
82 SendCommand(&c);
83 if (!WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
84 return 0;
85 }
86 return 0;
87}
88
89int HF14BCmdRaw(bool reply, bool *crc, bool power, uint8_t *data, uint8_t *datalen, bool verbose){
90
91 if(*crc) {
92 ComputeCrc14443(CRC_14443_B, data, *datalen, data+*datalen, data+*datalen+1);
93 *datalen += 2;
94 }
95
96 UsbCommand c = {CMD_ISO_14443B_COMMAND, {0, 0, 0}}; // len,recv,power
97 c.arg[0] = *datalen;
98 c.arg[1] = reply;
99 c.arg[2] = power;
100 memcpy(c.d.asBytes, data, *datalen);
101 clearCommandBuffer();
102 SendCommand(&c);
103
104 if (!reply) return 1;
105
106 UsbCommand resp;
107 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
108 if (verbose) PrintAndLog("timeout while waiting for reply.");
109 return 0;
110 }
111
112 *datalen = resp.arg[0];
113 if (verbose) PrintAndLog("received %u octets", *datalen);
114 if(*datalen<3) return 0;
115
116 memcpy(data, resp.d.asBytes, *datalen);
117
118 uint8_t first = 0, second = 0;
119 ComputeCrc14443(CRC_14443_B, data, *datalen-2, &first, &second);
120 *crc = ( data[*datalen-2] == first && data[*datalen-1] == second);
121
122 if (verbose)
123 PrintAndLog("[LEN %u] %s[%02X %02X] %s",
124 *datalen,
125 sprint_hex(data, *datalen-2),
126 data[*datalen-2],
127 data[*datalen-1],
128 (*crc)?"OK":"FAIL"
129 );
130
131 return 1;
132}
133
134int CmdHF14BCmdRaw (const char *Cmd) {
135 bool reply = true;
136 bool crc = false;
137 bool power = false;
138 bool select = false;
139 bool SRx = false;
140 char buf[5]="";
141 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
142 uint8_t datalen = 0;
143 unsigned int temp;
144 int i = 0;
145 if (strlen(Cmd)<3) {
146 PrintAndLog("Usage: hf 14b raw [-r] [-c] [-p] [-s || -ss] <0A 0B 0C ... hex>");
147 PrintAndLog(" -r do not read response");
148 PrintAndLog(" -c calculate and append CRC");
149 PrintAndLog(" -p leave the field on after receive");
150 PrintAndLog(" -s active signal field ON with select");
151 PrintAndLog(" -ss active signal field ON with select for SRx ST Microelectronics tags");
152 return 0;
153 }
154
155 // strip
156 while (*Cmd==' ' || *Cmd=='\t') Cmd++;
157
158 while (Cmd[i]!='\0') {
159 if (Cmd[i]==' ' || Cmd[i]=='\t') { i++; continue; }
160 if (Cmd[i]=='-') {
161 switch (Cmd[i+1]) {
162 case 'r':
163 case 'R':
164 reply = false;
165 break;
166 case 'c':
167 case 'C':
168 crc = true;
169 break;
170 case 'p':
171 case 'P':
172 power = true;
173 break;
174 case 's':
175 case 'S':
176 select = true;
177 if (Cmd[i+2]=='s' || Cmd[i+2]=='S') {
178 SRx = true;
179 i++;
180 }
181 break;
182 default:
183 PrintAndLog("Invalid option");
184 return 0;
185 }
186 i+=2;
187 continue;
188 }
189 if ((Cmd[i]>='0' && Cmd[i]<='9') ||
190 (Cmd[i]>='a' && Cmd[i]<='f') ||
191 (Cmd[i]>='A' && Cmd[i]<='F') ) {
192 buf[strlen(buf)+1]=0;
193 buf[strlen(buf)]=Cmd[i];
194 i++;
195
196 if (strlen(buf)>=2) {
197 sscanf(buf,"%x",&temp);
198 data[datalen++]=(uint8_t)(temp & 0xff);
199 *buf=0;
200 memset(buf, 0x00, sizeof(buf));
201 }
202 continue;
203 }
204 PrintAndLog("Invalid char on input");
205 return 0;
206 }
207 if (datalen == 0)
208 {
209 PrintAndLog("Missing data input");
210 return 0;
211 }
212
213 if (select){ //auto select 14b tag
214 uint8_t cmd2[16];
215 bool crc2 = true;
216 uint8_t cmdLen;
217
218 if (SRx) {
219 // REQ SRx
220 cmdLen = 2;
221 cmd2[0] = 0x06;
222 cmd2[1] = 0x00;
223 } else {
224 // REQB
225 cmdLen = 3;
226 cmd2[0] = 0x05;
227 cmd2[1] = 0x00;
228 cmd2[2] = 0x08;
229 }
230
231 // REQB
232 if (HF14BCmdRaw(true, &crc2, true, cmd2, &cmdLen, false)==0) return rawClose();
233
234 PrintAndLog("REQB : %s", sprint_hex(cmd2, cmdLen));
235
236 if ( SRx && (cmdLen != 3 || !crc2) ) return rawClose();
237 else if (cmd2[0] != 0x50 || cmdLen != 14 || !crc2) return rawClose();
238
239 uint8_t chipID = 0;
240 if (SRx) {
241 // select
242 chipID = cmd2[0];
243 cmd2[0] = 0x0E;
244 cmd2[1] = chipID;
245 cmdLen = 2;
246 } else {
247 // attrib
248 cmd2[0] = 0x1D;
249 // UID from cmd2[1 - 4]
250 cmd2[5] = 0x00;
251 cmd2[6] = 0x08;
252 cmd2[7] = 0x01;
253 cmd2[8] = 0x00;
254 cmdLen = 9;
255 }
256 // wait
257
258 // attrib
259 if (HF14BCmdRaw(true, &crc2, true, cmd2, &cmdLen, false)==0) return rawClose();
260 PrintAndLog("ATTRIB : %s", sprint_hex(cmd2, cmdLen));
261
262 if (cmdLen != 3 || !crc2) return rawClose();
263 if (SRx && cmd2[0] != chipID) return rawClose();
264
265 }
266 return HF14BCmdRaw(reply, &crc, power, data, &datalen, true);
267}
268
269// print full atqb info
270static void print_atqb_resp(uint8_t *data){
271 //PrintAndLog (" UID: %s", sprint_hex(data+1,4));
272 PrintAndLog (" App Data: %s", sprint_hex(data+5,4));
273 PrintAndLog (" Protocol: %s", sprint_hex(data+9,3));
274 uint8_t BitRate = data[9];
275 if (!BitRate) PrintAndLog (" Bit Rate: 106 kbit/s only PICC <-> PCD");
276 if (BitRate & 0x10) PrintAndLog (" Bit Rate: 212 kbit/s PICC -> PCD supported");
277 if (BitRate & 0x20) PrintAndLog (" Bit Rate: 424 kbit/s PICC -> PCD supported");
278 if (BitRate & 0x40) PrintAndLog (" Bit Rate: 847 kbit/s PICC -> PCD supported");
279 if (BitRate & 0x01) PrintAndLog (" Bit Rate: 212 kbit/s PICC <- PCD supported");
280 if (BitRate & 0x02) PrintAndLog (" Bit Rate: 424 kbit/s PICC <- PCD supported");
281 if (BitRate & 0x04) PrintAndLog (" Bit Rate: 847 kbit/s PICC <- PCD supported");
282 if (BitRate & 0x80) PrintAndLog (" Same bit rate <-> required");
283
284 uint16_t maxFrame = data[10]>>4;
285 if (maxFrame < 5) maxFrame = 8 * maxFrame + 16;
286 else if (maxFrame == 5) maxFrame = 64;
287 else if (maxFrame == 6) maxFrame = 96;
288 else if (maxFrame == 7) maxFrame = 128;
289 else if (maxFrame == 8) maxFrame = 256;
290 else maxFrame = 257;
291
292 PrintAndLog ("Max Frame Size: %u%s",maxFrame, (maxFrame == 257) ? "+ RFU" : "");
293
294 uint8_t protocolT = data[10] & 0xF;
295 PrintAndLog (" Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4",(protocolT) ? "" : "not " );
296 PrintAndLog ("Frame Wait Int: %u", data[11]>>4);
297 PrintAndLog (" App Data Code: Application is %s",(data[11]&4) ? "Standard" : "Proprietary");
298 PrintAndLog (" Frame Options: NAD is %ssupported",(data[11]&2) ? "" : "not ");
299 PrintAndLog (" Frame Options: CID is %ssupported",(data[11]&1) ? "" : "not ");
300 PrintAndLog ("Max Buf Length: %u (MBLI) %s",data[14]>>4, (data[14] & 0xF0) ? "" : "not supported");
301
302 return;
303}
304
305// get SRx chip model (from UID) // from ST Microelectronics
306char *get_ST_Chip_Model(uint8_t data){
307 static char model[20];
308 char *retStr = model;
309 memset(model,0, sizeof(model));
310
311 switch (data) {
312 case 0x0: sprintf(retStr, "SRIX4K (Special)"); break;
313 case 0x2: sprintf(retStr, "SR176"); break;
314 case 0x3: sprintf(retStr, "SRIX4K"); break;
315 case 0x4: sprintf(retStr, "SRIX512"); break;
316 case 0x6: sprintf(retStr, "SRI512"); break;
317 case 0x7: sprintf(retStr, "SRI4K"); break;
318 case 0xC: sprintf(retStr, "SRT512"); break;
319 default : sprintf(retStr, "Unknown"); break;
320 }
321 return retStr;
322}
323
324int print_ST_Lock_info(uint8_t model){
325 //assume connection open and tag selected...
326 uint8_t data[16] = {0x00};
327 uint8_t datalen = 2;
328 bool crc = true;
329 uint8_t resplen;
330 uint8_t blk1;
331 data[0] = 0x08;
332
333 if (model == 0x2) { //SR176 has special command:
334 data[1] = 0xf;
335 resplen = 4;
336 } else {
337 data[1] = 0xff;
338 resplen = 6;
339 }
340
341 //std read cmd
342 if (HF14BCmdRaw(true, &crc, true, data, &datalen, false)==0) return rawClose();
343
344 if (datalen != resplen || !crc) return rawClose();
345
346 PrintAndLog("Chip Write Protection Bits:");
347 // now interpret the data
348 switch (model){
349 case 0x0: //fall through (SRIX4K special)
350 case 0x3: //fall through (SRIx4K)
351 case 0x7: // (SRI4K)
352 //only need data[3]
353 blk1 = 9;
354 PrintAndLog(" raw: %s", sprint_bin(data+3, 1));
355 PrintAndLog(" 07/08:%slocked", (data[3] & 1) ? " not " : " " );
356 for (uint8_t i = 1; i<8; i++){
357 PrintAndLog(" %02u:%slocked", blk1, (data[3] & (1 << i)) ? " not " : " " );
358 blk1++;
359 }
360 break;
361 case 0x4: //fall through (SRIX512)
362 case 0x6: //fall through (SRI512)
363 case 0xC: // (SRT512)
364 //need data[2] and data[3]
365 blk1 = 0;
366 PrintAndLog(" raw: %s", sprint_bin(data+2, 2));
367 for (uint8_t b=2; b<4; b++){
368 for (uint8_t i=0; i<8; i++){
369 PrintAndLog(" %02u:%slocked", blk1, (data[b] & (1 << i)) ? " not " : " " );
370 blk1++;
371 }
372 }
373 break;
374 case 0x2: // (SR176)
375 //need data[2]
376 blk1 = 0;
377 PrintAndLog(" raw: %s", sprint_bin(data+2, 1));
378 for (uint8_t i = 0; i<8; i++){
379 PrintAndLog(" %02u/%02u:%slocked", blk1, blk1+1, (data[2] & (1 << i)) ? " " : " not " );
380 blk1+=2;
381 }
382 break;
383 default:
384 return rawClose();
385 }
386 return 1;
387}
388
389// print UID info from SRx chips (ST Microelectronics)
390static void print_st_general_info(uint8_t *data){
391 //uid = first 8 bytes in data
392 PrintAndLog(" UID: %s", sprint_hex(SwapEndian64(data,8,8),8));
393 PrintAndLog(" MFG: %02X, %s", data[6], getTagInfo(data[6]));
394 PrintAndLog("Chip: %02X, %s", data[5]>>2, get_ST_Chip_Model(data[5]>>2));
395 return;
396}
397
398// 14b get and print UID only (general info)
399int HF14BStdReader(uint8_t *data, uint8_t *datalen){
400 //05 00 00 = find one tag in field
401 //1d xx xx xx xx 00 08 01 00 = attrib xx=UID (resp 10 [f9 e0])
402 //a3 = ? (resp 03 [e2 c2])
403 //02 = ? (resp 02 [6a d3])
404 // 022b (resp 02 67 00 [29 5b])
405 // 0200a40400 (resp 02 67 00 [29 5b])
406 // 0200a4040c07a0000002480300 (resp 02 67 00 [29 5b])
407 // 0200a4040c07a0000002480200 (resp 02 67 00 [29 5b])
408 // 0200a4040006a0000000010100 (resp 02 6a 82 [4b 4c])
409 // 0200a4040c09d27600002545500200 (resp 02 67 00 [29 5b])
410 // 0200a404000cd2760001354b414e4d30310000 (resp 02 6a 82 [4b 4c])
411 // 0200a404000ca000000063504b43532d313500 (resp 02 6a 82 [4b 4c])
412 // 0200a4040010a000000018300301000000000000000000 (resp 02 6a 82 [4b 4c])
413 //03 = ? (resp 03 [e3 c2])
414 //c2 = ? (resp c2 [66 15])
415 //b2 = ? (resp a3 [e9 67])
416 //a2 = ? (resp 02 [6a d3])
417 bool crc = true;
418 *datalen = 3;
419 //std read cmd
420 data[0] = 0x05;
421 data[1] = 0x00;
422 data[2] = 0x08;
423
424 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
425
426 if (data[0] != 0x50 || *datalen != 14 || !crc) return rawClose();
427
428 PrintAndLog ("\n14443-3b tag found:");
429 PrintAndLog (" UID: %s", sprint_hex(data+1,4));
430
431 uint8_t cmd2[16];
432 uint8_t cmdLen = 3;
433 bool crc2 = true;
434
435 cmd2[0] = 0x1D;
436 // UID from data[1 - 4]
437 cmd2[1] = data[1];
438 cmd2[2] = data[2];
439 cmd2[3] = data[3];
440 cmd2[4] = data[4];
441 cmd2[5] = 0x00;
442 cmd2[6] = 0x08;
443 cmd2[7] = 0x01;
444 cmd2[8] = 0x00;
445 cmdLen = 9;
446
447 // attrib
448 if (HF14BCmdRaw(true, &crc2, true, cmd2, &cmdLen, false)==0) return rawClose();
449
450 if (cmdLen != 3 || !crc2) return rawClose();
451 // add attrib responce to data
452 data[14] = cmd2[0];
453 rawClose();
454 return 1;
455}
456
457// 14b get and print Full Info (as much as we know)
458int HF14BStdInfo(uint8_t *data, uint8_t *datalen){
459 if (!HF14BStdReader(data,datalen)) return 0;
460
461 //add more info here
462 print_atqb_resp(data);
463 return 1;
464}
465
466// SRx get and print general info about SRx chip from UID
467int HF14B_ST_Reader(uint8_t *data, uint8_t *datalen, bool closeCon){
468 bool crc = true;
469 *datalen = 2;
470 //wake cmd
471 data[0] = 0x06;
472 data[1] = 0x00;
473
474 //leave power on
475 // verbose on for now for testing - turn off when functional
476 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
477
478 if (*datalen != 3 || !crc) return rawClose();
479
480 uint8_t chipID = data[0];
481 // select
482 data[0] = 0x0E;
483 data[1] = chipID;
484 *datalen = 2;
485
486 //leave power on
487 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
488
489 if (*datalen != 3 || !crc || data[0] != chipID) return rawClose();
490
491 // get uid
492 data[0] = 0x0B;
493 *datalen = 1;
494
495 //leave power on
496 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)==0) return rawClose();
497
498 if (*datalen != 10 || !crc) return rawClose();
499
500 //power off ?
501 if (closeCon) rawClose();
502
503 PrintAndLog("\n14443-3b ST tag found:");
504 print_st_general_info(data);
505 return 1;
506}
507
508// SRx get and print full info (needs more info...)
509int HF14B_ST_Info(uint8_t *data, uint8_t *datalen){
510 if (!HF14B_ST_Reader(data, datalen, false)) return 0;
511
512 //add locking bit information here.
513 if (print_ST_Lock_info(data[5]>>2))
514 rawClose();
515
516 return 1;
517}
518
519// test for other 14b type tags (mimic another reader - don't have tags to identify)
520int HF14B_Other_Reader(uint8_t *data, uint8_t *datalen){
521 bool crc = true;
522 *datalen = 4;
523 //std read cmd
524 data[0] = 0x00;
525 data[1] = 0x0b;
526 data[2] = 0x3f;
527 data[3] = 0x80;
528
529 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
530 if (*datalen > 2 || !crc) {
531 PrintAndLog ("\n14443-3b tag found:");
532 PrintAndLog ("Unknown tag type answered to a 0x000b3f80 command ans:");
533 PrintAndLog ("%s",sprint_hex(data,*datalen));
534 rawClose();
535 return 1;
536 }
537 }
538
539 crc = false;
540 *datalen = 1;
541 data[0] = 0x0a;
542
543 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
544 if (*datalen > 0) {
545 PrintAndLog ("\n14443-3b tag found:");
546 PrintAndLog ("Unknown tag type answered to a 0x0A command ans:");
547 PrintAndLog ("%s",sprint_hex(data,*datalen));
548 rawClose();
549 return 1;
550 }
551 }
552
553 crc = false;
554 *datalen = 1;
555 data[0] = 0x0c;
556
557 if (HF14BCmdRaw(true, &crc, true, data, datalen, false)!=0) {
558 if (*datalen > 0) {
559 PrintAndLog ("\n14443-3b tag found:");
560 PrintAndLog ("Unknown tag type answered to a 0x0C command ans:");
561 PrintAndLog ("%s",sprint_hex(data,*datalen));
562 rawClose();
563 return 1;
564 }
565 }
566 rawClose();
567 return 0;
568}
569
570// get and print all info known about any known 14b tag
571int HF14BInfo(bool verbose){
572 uint8_t data[USB_CMD_DATA_SIZE];
573 uint8_t datalen = 5;
574
575 // try std 14b (atqb)
576 if (HF14BStdInfo(data, &datalen)) return 1;
577
578 // try st 14b
579 if (HF14B_ST_Info(data, &datalen)) return 1;
580
581 // try unknown 14b read commands (to be identified later)
582 // could be read of calypso, CEPAS, moneo, or pico pass.
583 if (HF14B_Other_Reader(data, &datalen)) return 1;
584
585 if (verbose) PrintAndLog("no 14443B tag found");
586 return 0;
587}
588
589// menu command to get and print all info known about any known 14b tag
590int CmdHF14Binfo(const char *Cmd){
591 return HF14BInfo(true);
592}
593
594// get and print general info about all known 14b chips
595int HF14BReader(bool verbose){
596 uint8_t data[USB_CMD_DATA_SIZE];
597 uint8_t datalen = 5;
598
599 // try std 14b (atqb)
600 if (HF14BStdReader(data, &datalen)) return 1;
601
602 // try st 14b
603 if (HF14B_ST_Reader(data, &datalen, true)) return 1;
604
605 // try unknown 14b read commands (to be identified later)
606 // could be read of calypso, CEPAS, moneo, or pico pass.
607 if (HF14B_Other_Reader(data, &datalen)) return 1;
608
609 if (verbose) PrintAndLog("no 14443B tag found");
610 return 0;
611}
612
613// menu command to get and print general info about all known 14b chips
614int CmdHF14BReader(const char *Cmd){
615 return HF14BReader(true);
616}
617
618int CmdSriWrite( const char *Cmd){
619/*
620 * For SRIX4K blocks 00 - 7F
621 * hf 14b raw -c -p 09 $srix4kwblock $srix4kwdata
622 *
623 * For SR512 blocks 00 - 0F
624 * hf 14b raw -c -p 09 $sr512wblock $sr512wdata
625 *
626 * Special block FF = otp_lock_reg block.
627 * Data len 4 bytes-
628 */
629 char cmdp = param_getchar(Cmd, 0);
630 uint8_t blockno = -1;
631 uint8_t data[4] = {0x00};
632 bool isSrix4k = true;
633 char str[20];
634
635 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
636 PrintAndLog("Usage: hf 14b write <1|2> <BLOCK> <DATA>");
637 PrintAndLog(" [1 = SRIX4K]");
638 PrintAndLog(" [2 = SRI512]");
639 PrintAndLog(" [BLOCK number depends on tag, special block == FF]");
640 PrintAndLog(" sample: hf 14b write 1 7F 11223344");
641 PrintAndLog(" : hf 14b write 1 FF 11223344");
642 PrintAndLog(" : hf 14b write 2 15 11223344");
643 PrintAndLog(" : hf 14b write 2 FF 11223344");
644 return 0;
645 }
646
647 if ( cmdp == '2' )
648 isSrix4k = false;
649
650 //blockno = param_get8(Cmd, 1);
651
652 if ( param_gethex(Cmd,1, &blockno, 2) ) {
653 PrintAndLog("Block number must include 2 HEX symbols");
654 return 0;
655 }
656
657 if ( isSrix4k ){
658 if ( blockno > 0x7f && blockno != 0xff ){
659 PrintAndLog("Block number out of range");
660 return 0;
661 }
662 } else {
663 if ( blockno > 0x0f && blockno != 0xff ){
664 PrintAndLog("Block number out of range");
665 return 0;
666 }
667 }
668
669 if (param_gethex(Cmd, 2, data, 8)) {
670 PrintAndLog("Data must include 8 HEX symbols");
671 return 0;
672 }
673
674 if ( blockno == 0xff)
675 PrintAndLog("[%s] Write special block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512" , blockno, sprint_hex(data,4) );
676 else
677 PrintAndLog("[%s] Write block %02X [ %s ]", (isSrix4k)?"SRIX4K":"SRI512", blockno, sprint_hex(data,4) );
678
679 sprintf(str, "-c 09 %02x %02x%02x%02x%02x", blockno, data[0], data[1], data[2], data[3]);
680
681 CmdHF14BCmdRaw(str);
682 return 0;
683}
684
685uint32_t srix4kEncode(uint32_t value) {
686/*
687// vv = value
688// pp = position
689// vv vv vv pp
6904 bytes : 00 1A 20 01
691*/
692
693#define NibbleHigh(b) ( (b & 0xF0) >> 4 )
694#define NibbleLow(b) ( b & 0x0F )
695#define Crumb(b,p) (((b & (0x3 << p) ) >> p ) & 0xF)
696
697 // only the lower crumbs.
698 uint8_t block = (value & 0xFF);
699 uint8_t i = 0;
700 uint8_t valuebytes[] = {0,0,0};
701
702 num_to_bytes(value, 3, valuebytes);
703
704 // Scrambled part
705 // Crumb swapping of value.
706 uint8_t temp[] = {0,0};
707 temp[0] = (Crumb(value, 22) << 4 | Crumb(value, 14 ) << 2 | Crumb(value, 6)) << 4;
708 temp[0] |= Crumb(value, 20) << 4 | Crumb(value, 12 ) << 2 | Crumb(value, 4);
709 temp[1] = (Crumb(value, 18) << 4 | Crumb(value, 10 ) << 2 | Crumb(value, 2)) << 4;
710 temp[1] |= Crumb(value, 16) << 4 | Crumb(value, 8 ) << 2 | Crumb(value, 0);
711
712 // chksum part
713 uint32_t chksum = 0xFF - block;
714
715 // chksum is reduced by each nibbles of value.
716 for (i = 0; i < 3; ++i){
717 chksum -= NibbleHigh(valuebytes[i]);
718 chksum -= NibbleLow(valuebytes[i]);
719 }
720
721 // base4 conversion and left shift twice
722 i = 3;
723 uint8_t base4[] = {0,0,0,0};
724 while( chksum !=0 ){
725 base4[i--] = (chksum % 4 << 2);
726 chksum /= 4;
727 }
728
729 // merge scambled and chksum parts
730 uint32_t encvalue =
731 ( NibbleLow ( base4[0]) << 28 ) |
732 ( NibbleHigh( temp[0]) << 24 ) |
733
734 ( NibbleLow ( base4[1]) << 20 ) |
735 ( NibbleLow ( temp[0]) << 16 ) |
736
737 ( NibbleLow ( base4[2]) << 12 ) |
738 ( NibbleHigh( temp[1]) << 8 ) |
739
740 ( NibbleLow ( base4[3]) << 4 ) |
741 NibbleLow ( temp[1] );
742
743 PrintAndLog("ICE encoded | %08X -> %08X", value, encvalue);
744 return encvalue;
745}
746uint32_t srix4kDecode(uint32_t value) {
747 switch(value) {
748 case 0xC04F42C5: return 0x003139;
749 case 0xC1484807: return 0x002943;
750 case 0xC0C60848: return 0x001A20;
751 }
752 return 0;
753}
754uint32_t srix4kDecodeCounter(uint32_t num) {
755 uint32_t value = ~num;
756 ++value;
757 return value;
758}
759
760uint32_t srix4kGetMagicbytes( uint64_t uid, uint32_t block6, uint32_t block18, uint32_t block19 ){
761#define MASK 0xFFFFFFFF;
762 uint32_t uid32 = uid & MASK;
763 uint32_t counter = srix4kDecodeCounter(block6);
764 uint32_t decodedBlock18 = srix4kDecode(block18);
765 uint32_t decodedBlock19 = srix4kDecode(block19);
766 uint32_t doubleBlock = (decodedBlock18 << 16 | decodedBlock19) + 1;
767
768 uint32_t result = (uid32 * doubleBlock * counter) & MASK;
769 PrintAndLog("Magic bytes | %08X", result);
770 return result;
771}
772int srix4kValid(const char *Cmd){
773
774 uint64_t uid = 0xD00202501A4532F9;
775 uint32_t block6 = 0xFFFFFFFF;
776 uint32_t block18 = 0xC04F42C5;
777 uint32_t block19 = 0xC1484807;
778 uint32_t block21 = 0xD1BCABA4;
779
780 uint32_t test_b18 = 0x00313918;
781 uint32_t test_b18_enc = srix4kEncode(test_b18);
782 //uint32_t test_b18_dec = srix4kDecode(test_b18_enc);
783 PrintAndLog("ENCODE & CHECKSUM | %08X -> %08X (%s)", test_b18, test_b18_enc , "");
784
785 uint32_t magic = srix4kGetMagicbytes(uid, block6, block18, block19);
786 PrintAndLog("BLOCK 21 | %08X -> %08X (no XOR)", block21, magic ^ block21);
787 return 0;
788}
789
790int CmdteaSelfTest(const char *Cmd){
791
792 uint8_t v[8], v_le[8];
793 memset(v, 0x00, sizeof(v));
794 memset(v_le, 0x00, sizeof(v_le));
795 uint8_t* v_ptr = v_le;
796
797 uint8_t cmdlen = strlen(Cmd);
798 cmdlen = ( sizeof(v)<<2 < cmdlen ) ? sizeof(v)<<2 : cmdlen;
799
800 if ( param_gethex(Cmd, 0, v, cmdlen) > 0 ){
801 PrintAndLog("can't read hex chars, uneven? :: %u", cmdlen);
802 return 1;
803 }
804
805 SwapEndian64ex(v , 8, 4, v_ptr);
806
807
808
809 PrintAndLog("Modified Burtle");
810 prng_ctx ctx; // = { 0, 0, 0, 0 };
811 uint32_t num = bytes_to_num(v+1, 4);
812 burtle_init_mod( &ctx, num);
813 PrintAndLog("V : %X", num);
814 PrintAndLog("BURT: %X", burtle_get_mod( &ctx));
815 PrintAndLog("SIMP: %X", GetSimplePrng(num));
816
817 uint8_t calc[16];
818
819 for ( uint8_t i=0; i<8; ++i){
820 if ( i%2 == 0) {
821 calc[0] += v[i];
822 calc[1] += NibbleHigh( v[i]);
823 calc[2] += NibbleLow( v[i]);
824 calc[3] ^= v[i];
825 calc[4] ^= NibbleHigh(v[i]);
826 calc[5] ^= NibbleLow( v[i]);
827 }
828 else {
829 calc[6] += v[i];
830 calc[7] += NibbleHigh( v[i]);
831 calc[8] += NibbleLow( v[i]);
832 calc[9] ^= v[i];
833 calc[10] ^= NibbleHigh(v[i]);
834 calc[11] ^= NibbleLow( v[i]);
835 }
836 }
837 for ( uint8_t i=0; i<4; ++i) calc[12] += v[i];
838 for ( uint8_t i=1; i<5; ++i) calc[13] += v[i];
839 for ( uint8_t i=2; i<6; ++i) calc[14] += v[i];
840 for ( uint8_t i=3; i<7; ++i) calc[15] += v[i];
841
842 PrintAndLog("%s ", sprint_hex(calc, 16) );
843 return 0;
844}
845
846static command_t CommandTable[] =
847{
848 {"help", CmdHelp, 1, "This help"},
849 {"info", CmdHF14Binfo, 0, "Find and print details about a 14443B tag"},
850 {"list", CmdHF14BList, 0, "[Deprecated] List ISO 14443B history"},
851 {"reader", CmdHF14BReader, 0, "Act as a 14443B reader to identify a tag"},
852 {"sim", CmdHF14BSim, 0, "Fake ISO 14443B tag"},
853 {"snoop", CmdHF14BSnoop, 0, "Eavesdrop ISO 14443B"},
854 {"sri512read", CmdSri512Read, 0, "Read contents of a SRI512 tag"},
855 {"srix4kread", CmdSrix4kRead, 0, "Read contents of a SRIX4K tag"},
856 {"sriwrite", CmdSriWrite, 0, "Write data to a SRI512 | SRIX4K tag"},
857 {"raw", CmdHF14BCmdRaw, 0, "Send raw hex data to tag"},
858 //{"valid", srix4kValid, 1, "srix4k checksum test"},
859 {"valid", CmdteaSelfTest, 1, "tea test"},
860 {NULL, NULL, 0, NULL}
861};
862
863int CmdHF14B(const char *Cmd)
864{
865 CmdsParse(CommandTable, Cmd);
866 return 0;
867}
868
869int CmdHelp(const char *Cmd)
870{
871 CmdsHelp(CommandTable);
872 return 0;
873}
Impressum, Datenschutz