Fixed (?) issues from PR #129
[legacy-proxmark3.git] / client / cmdlfawid.c
blob06397e7086844f57a05fc0db883ca5bd8d5cbc3f
1 //-----------------------------------------------------------------------------
2 // Authored by Craig Young <cyoung@tripwire.com> based on cmdlfhid.c structure
3 //
4 // cmdlfhid.c is Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 //
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
8 // the license.
9 //-----------------------------------------------------------------------------
10 // Low frequency AWID26 commands
11 //-----------------------------------------------------------------------------
13 #include <stdio.h> // sscanf
14 #include "proxmark3.h" // Definitions, USB controls, etc
15 #include "ui.h" // PrintAndLog
16 #include "cmdparser.h" // CmdsParse, CmdsHelp
17 #include "cmdlfawid.h" // AWID function declarations
18 #include "lfdemod.h" // parityTest
19 #include "cmdmain.h"
20 static int CmdHelp(const char *Cmd);
23 int usage_lf_awid_fskdemod(void) {
24 PrintAndLog("Enables AWID26 compatible reader mode printing details of scanned AWID26 tags.");
25 PrintAndLog("By default, values are printed and logged until the button is pressed or another USB command is issued.");
26 PrintAndLog("If the ['1'] option is provided, reader mode is exited after reading a single AWID26 card.");
27 PrintAndLog("");
28 PrintAndLog("Usage: lf awid fskdemod ['1']");
29 PrintAndLog(" Options : ");
30 PrintAndLog(" 1 : (optional) stop after reading a single card");
31 PrintAndLog("");
32 PrintAndLog(" sample : lf awid fskdemod");
33 PrintAndLog(" : lf awid fskdemod 1");
34 return 0;
37 int usage_lf_awid_sim(void) {
38 PrintAndLog("Enables simulation of AWID26 card with specified facility-code and card number.");
39 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
40 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
41 PrintAndLog("");
42 PrintAndLog("Usage: lf awid sim <Facility-Code> <Card-Number>");
43 PrintAndLog(" Options : ");
44 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
45 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
46 PrintAndLog("");
47 PrintAndLog(" sample : lf awid sim 224 1337");
48 return 0;
51 int usage_lf_awid_clone(void) {
52 PrintAndLog("Enables cloning of AWID26 card with specified facility-code and card number onto T55x7.");
53 PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process.");
54 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
55 PrintAndLog("");
56 PrintAndLog("Usage: lf awid clone <Facility-Code> <Card-Number>");
57 PrintAndLog(" Options : ");
58 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
59 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
60 PrintAndLog("");
61 PrintAndLog(" sample : lf awid clone 224 1337");
62 return 0;
65 int CmdAWIDDemodFSK(const char *Cmd)
67 int findone=0;
68 if(Cmd[0]=='1') findone=1;
69 if (Cmd[0]=='h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
70 UsbCommand c={CMD_AWID_DEMOD_FSK};
71 c.arg[0]=findone;
72 SendCommand(&c);
73 return 0;
76 int getAWIDBits(unsigned int fc, unsigned int cn, uint8_t *AWIDBits)
78 int i;
79 uint32_t fcode=(fc & 0x000000FF), cnum=(cn & 0x0000FFFF), uBits=0;
80 if (fcode != fc)
81 PrintAndLog("NOTE: Facility code truncated for AWID26 format (8-bit facility code)");
82 if (cnum!=cn)
83 PrintAndLog("NOTE: Card number was truncated for AWID26 format (16-bit card number)");
85 AWIDBits[0] = 0x01; // 6-bit Preamble with 2 parity bits
86 AWIDBits[1] = 0x1D; // First byte from card format (26-bit) plus parity bits
87 AWIDBits[2] = 0x80; // Set the next two bits as 0b10 to finish card format
88 uBits = (fcode<<4) + (cnum>>12);
89 if (!parityTest(uBits,12,0))
90 AWIDBits[2] |= (1<<5); // If not already even parity, set bit to make even
91 uBits = AWIDBits[2]>>5;
92 if (!parityTest(uBits, 3, 1))
93 AWIDBits[2] |= (1<<4);
94 uBits = fcode>>5; // first 3 bits of facility-code
95 AWIDBits[2] += (uBits<<1);
96 if (!parityTest(uBits, 3, 1))
97 AWIDBits[2]++; // Set parity bit to make odd parity
98 uBits = (fcode & 0x1C)>>2;
99 AWIDBits[3] = 0;
100 if (!parityTest(uBits,3,1))
101 AWIDBits[3] |= (1<<4);
102 AWIDBits[3] += (uBits<<5);
103 uBits = ((fcode & 0x3)<<1) + ((cnum & 0x8000)>>15); // Grab/shift 2 LSBs from facility code and add shifted MSB from cardnum
104 if (!parityTest(uBits,3,1))
105 AWIDBits[3]++; // Set LSB for parity
106 AWIDBits[3]+= (uBits<<1);
107 uBits = (cnum & 0x7000)>>12;
108 AWIDBits[4] = uBits<<5;
109 if (!parityTest(uBits,3,1))
110 AWIDBits[4] |= (1<<4);
111 uBits = (cnum & 0x0E00)>>9;
112 AWIDBits[4] += (uBits<<1);
113 if (!parityTest(uBits,3,1))
114 AWIDBits[4]++; // Set LSB for parity
115 uBits = (cnum & 0x1C0)>>6; // Next bits from card number
116 AWIDBits[5]=(uBits<<5);
117 if (!parityTest(uBits,3,1))
118 AWIDBits[5] |= (1<<4); // Set odd parity bit as needed
119 uBits = (cnum & 0x38)>>3;
120 AWIDBits[5]+= (uBits<<1);
121 if (!parityTest(uBits,3,1))
122 AWIDBits[5]++; // Set odd parity bit as needed
123 uBits = (cnum & 0x7); // Last three bits from card number!
124 AWIDBits[6] = (uBits<<5);
125 if (!parityTest(uBits,3,1))
126 AWIDBits[6] |= (1<<4);
127 uBits = (cnum & 0x0FFF);
128 if (!parityTest(uBits,12,1))
129 AWIDBits[6] |= (1<<3);
130 else
131 AWIDBits[6]++;
132 for (i = 7; i<12; i++)
133 AWIDBits[i]=0x11;
134 return 1;
137 int CmdAWIDSim(const char *Cmd)
139 uint32_t fcode = 0, cnum = 0, fc=0, cn=0, i=0;
140 uint8_t *BS, BitStream[12];
141 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
142 uint64_t arg2 = 50; // clk RF/50 invert=0
143 BS = BitStream;
144 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) {
145 return usage_lf_awid_sim();
148 fcode=(fc & 0x000000FF);
149 cnum=(cn & 0x0000FFFF);
150 if (fc!=fcode)
151 PrintAndLog("Facility-Code (%u) truncated to 8-bits: %u",fc,fcode);
152 if (cn!=cnum)
153 PrintAndLog("Card number (%u) truncated to 16-bits: %u",cn,cnum);
154 PrintAndLog("Emulating AWID26 -- FC: %u; CN: %u\n",fcode,cnum);
155 PrintAndLog("Press pm3-button to abort simulation or run another command");
156 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
157 if (getAWIDBits(fc, cn, BS)) {
158 PrintAndLog("Running 'lf simfsk c 50 H 10 L 8 d %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'",
159 BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6],
160 BS[7],BS[8],BS[9],BS[10],BS[11]);
161 } else
162 PrintAndLog("Error with tag bitstream generation.");
163 UsbCommand c;
164 c.cmd = CMD_FSK_SIM_TAG;
165 c.arg[0] = arg1; // fcHigh<<8 + fcLow
166 c.arg[1] = arg2; // Inversion and clk setting
167 c.arg[2] = 96; // Bitstream length: 96-bits == 12 bytes
168 for (i=0; i < 96; i++)
169 c.d.asBytes[i] = (BS[i/8] & (1<<(7-(i%8))))?1:0;
170 SendCommand(&c);
171 return 0;
174 int CmdAWIDClone(const char *Cmd)
176 uint32_t fc=0,cn=0,blocks[4] = {0x00107060, 0, 0, 0x11111111}, i=0;
177 uint8_t BitStream[12];
178 uint8_t *BS=BitStream;
179 UsbCommand c, resp;
181 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) {
182 return usage_lf_awid_clone();
185 if ((fc & 0xFF) != fc) {
186 fc &= 0xFF;
187 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
189 if ((cn & 0xFFFF) != cn) {
190 cn &= 0xFFFF;
191 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
193 if (getAWIDBits(fc,cn,BS)) {
194 PrintAndLog("Preparing to clone AWID26 to T55x7 with FC: %u, CN: %u (Raw: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x)",
195 fc,cn, BS[0],BS[1],BS[2],BS[3],BS[4],BS[5],BS[6],BS[7],BS[8],BS[9],BS[10],BS[11]);
196 blocks[1] = (BS[0]<<24) + (BS[1]<<16) + (BS[2]<<8) + (BS[3]);
197 blocks[2] = (BS[4]<<24) + (BS[5]<<16) + (BS[6]<<8) + (BS[7]);
198 PrintAndLog("Block 0: 0x%08x", blocks[0]);
199 PrintAndLog("Block 1: 0x%08x", blocks[1]);
200 PrintAndLog("Block 2: 0x%08x", blocks[2]);
201 PrintAndLog("Block 3: 0x%08x", blocks[3]);
202 for (i=0; i<4; i++) {
203 c.cmd = CMD_T55XX_WRITE_BLOCK;
204 c.arg[0] = blocks[i];
205 c.arg[1] = i;
206 c.arg[2] = 0;
207 SendCommand(&c);
208 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
209 PrintAndLog("Error occurred, device did not respond during write operation.");
210 return -1;
215 return 0;
218 static command_t CommandTable[] =
220 {"help", CmdHelp, 1, "This help"},
221 {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"},
222 {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"},
223 {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> -- Clone AWID to T55x7 (tag must be in range of antenna)"},
224 {NULL, NULL, 0, NULL}
227 int CmdLFAWID(const char *Cmd)
229 CmdsParse(CommandTable, Cmd);
230 return 0;
233 int CmdHelp(const char *Cmd)
235 CmdsHelp(CommandTable);
236 return 0;