]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfawid.c
use memcpy instead of re-assign pointer
[proxmark3-svn] / client / cmdlfawid.c
CommitLineData
dbf6e824
CY
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//-----------------------------------------------------------------------------
12
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
709665b5 19#include "util.h" // weigandparity
20#include "protocols.h" // for T55xx config register definitions
976627d5 21#include "cmdmain.h"
dbf6e824 22
709665b5 23static int CmdHelp(const char *Cmd);
bcffcca2
CY
24
25int usage_lf_awid_fskdemod(void) {
37824afe
MHS
26 PrintAndLog("Enables AWID26 compatible reader mode printing details of scanned AWID26 tags.");
27 PrintAndLog("By default, values are printed and logged until the button is pressed or another USB command is issued.");
28 PrintAndLog("If the ['1'] option is provided, reader mode is exited after reading a single AWID26 card.");
29 PrintAndLog("");
30 PrintAndLog("Usage: lf awid fskdemod ['1']");
709665b5 31 PrintAndLog("Options : ");
37824afe
MHS
32 PrintAndLog(" 1 : (optional) stop after reading a single card");
33 PrintAndLog("");
709665b5 34 PrintAndLog("Samples : lf awid fskdemod");
35 PrintAndLog(" : lf awid fskdemod 1");
37824afe 36 return 0;
bcffcca2
CY
37}
38
39int usage_lf_awid_sim(void) {
37824afe
MHS
40 PrintAndLog("Enables simulation of AWID26 card with specified facility-code and card number.");
41 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
42 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
43 PrintAndLog("");
44 PrintAndLog("Usage: lf awid sim <Facility-Code> <Card-Number>");
709665b5 45 PrintAndLog("Options : ");
37824afe
MHS
46 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
47 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
48 PrintAndLog("");
709665b5 49 PrintAndLog("Sample : lf awid sim 224 1337");
37824afe 50 return 0;
bcffcca2
CY
51}
52
53int usage_lf_awid_clone(void) {
37824afe
MHS
54 PrintAndLog("Enables cloning of AWID26 card with specified facility-code and card number onto T55x7.");
55 PrintAndLog("The T55x7 must be on the antenna when issuing this command. T55x7 blocks are calculated and printed in the process.");
56 PrintAndLog("Per AWID26 format, the facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
57 PrintAndLog("");
58 PrintAndLog("Usage: lf awid clone <Facility-Code> <Card-Number>");
709665b5 59 PrintAndLog("Options : ");
37824afe
MHS
60 PrintAndLog(" <Facility-Code> : 8-bit value representing the AWID facility code");
61 PrintAndLog(" <Card Number> : 16-bit value representing the AWID card number");
709665b5 62 PrintAndLog(" Q5 : optional - clone to Q5 (T5555) instead of T55x7 chip");
37824afe 63 PrintAndLog("");
709665b5 64 PrintAndLog("Sample : lf awid clone 224 1337");
37824afe 65 return 0;
bcffcca2
CY
66}
67
709665b5 68int CmdAWIDDemodFSK(const char *Cmd) {
37824afe 69 int findone=0;
709665b5 70 if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
71 if (Cmd[0] == '1') findone = 1;
72
73 UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}};
74 clearCommandBuffer();
37824afe
MHS
75 SendCommand(&c);
76 return 0;
dbf6e824
CY
77}
78
709665b5 79//refactored by marshmellow
80int getAWIDBits(uint32_t fc, uint32_t cn, uint8_t *AWIDBits) {
81 uint8_t pre[66];
82 memset(pre, 0, sizeof(pre));
83 AWIDBits[7]=1;
84 num_to_bytebits(26, 8, pre);
85
86 uint8_t wiegand[24];
87 num_to_bytebits(fc, 8, wiegand);
88 num_to_bytebits(cn, 16, wiegand+8);
89
90 wiegand_add_parity(pre+8, wiegand, 24);
91
92 size_t bitLen = addParity(pre, AWIDBits+8, 66, 4, 1);
93 if (bitLen != 88) return 0;
94 //for (uint8_t i = 0; i<3; i++){
95 // PrintAndLog("DEBUG: %08X", bytebits_to_byte(AWIDBits+(32*i),32));
96 //}
37824afe 97 return 1;
dbf6e824
CY
98}
99
709665b5 100int CmdAWIDSim(const char *Cmd) {
101 uint32_t fcode = 0, cnum = 0, fc=0, cn=0;
102 uint8_t BitStream[96];
103 uint8_t *bs = BitStream;
104 size_t size = sizeof(BitStream);
105 memset(bs, 0, size);
106
37824afe
MHS
107 uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
108 uint64_t arg2 = 50; // clk RF/50 invert=0
709665b5 109
110 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_awid_sim();
111
112 fcode = (fc & 0x000000FF);
113 cnum = (cn & 0x0000FFFF);
114
115 if (fc != fcode) PrintAndLog("Facility-Code (%u) truncated to 8-bits: %u",fc,fcode);
116 if (cn != cnum) PrintAndLog("Card number (%u) truncated to 16-bits: %u",cn,cnum);
37824afe 117
37824afe
MHS
118 PrintAndLog("Emulating AWID26 -- FC: %u; CN: %u\n",fcode,cnum);
119 PrintAndLog("Press pm3-button to abort simulation or run another command");
709665b5 120
121 if (!getAWIDBits(fc, cn, bs)) {
122 PrintAndLog("Error with tag bitstream generation.");
123 return 1;
124 }
37824afe 125 // AWID uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0
709665b5 126 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
127 memcpy(c.d.asBytes, bs, size);
128 clearCommandBuffer();
37824afe
MHS
129 SendCommand(&c);
130 return 0;
dbf6e824
CY
131}
132
709665b5 133int CmdAWIDClone(const char *Cmd) {
134 uint32_t blocks[4] = {T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 3<<T55x7_MAXBLOCK_SHIFT, 0, 0, 0};
135 uint32_t fc=0,cn=0;
136 uint8_t BitStream[96];
137 uint8_t *bs=BitStream;
138 memset(bs,0,sizeof(BitStream));
37824afe 139
709665b5 140 if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_awid_clone();
141
142 if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
143 blocks[0] = T5555_MODULATION_FSK2 | T5555_INVERT_OUTPUT | 50<<T5555_BITRATE_SHIFT | 3<<T5555_MAXBLOCK_SHIFT;
37824afe
MHS
144
145 if ((fc & 0xFF) != fc) {
146 fc &= 0xFF;
147 PrintAndLog("Facility-Code Truncated to 8-bits (AWID26): %u", fc);
148 }
149 if ((cn & 0xFFFF) != cn) {
150 cn &= 0xFFFF;
151 PrintAndLog("Card Number Truncated to 16-bits (AWID26): %u", cn);
152 }
976627d5 153
709665b5 154 if ( !getAWIDBits(fc, cn, bs)) {
155 PrintAndLog("Error with tag bitstream generation.");
156 return 1;
157 }
158
159 blocks[1] = bytebits_to_byte(bs,32);
160 blocks[2] = bytebits_to_byte(bs+32,32);
161 blocks[3] = bytebits_to_byte(bs+64,32);
162
163 PrintAndLog("Preparing to clone AWID26 to T55x7 with FC: %u, CN: %u",
164 fc, cn);
165 PrintAndLog("Blk | Data ");
166 PrintAndLog("----+------------");
167 PrintAndLog(" 00 | 0x%08x", blocks[0]);
168 PrintAndLog(" 01 | 0x%08x", blocks[1]);
169 PrintAndLog(" 02 | 0x%08x", blocks[2]);
170 PrintAndLog(" 03 | 0x%08x", blocks[3]);
171
172 UsbCommand resp;
173 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
174
175 for (uint8_t i=0; i<4; i++) {
176 c.cmd = CMD_T55XX_WRITE_BLOCK;
177 c.arg[0] = blocks[i];
178 c.arg[1] = i;
179 c.arg[2] = 0;
180 clearCommandBuffer();
181 SendCommand(&c);
182 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
183 PrintAndLog("Error occurred, device did not respond during write operation.");
184 return -1;
37824afe 185 }
709665b5 186
37824afe
MHS
187 }
188 return 0;
dbf6e824
CY
189}
190
709665b5 191static command_t CommandTable[] = {
192 {"help", CmdHelp, 1, "This help"},
37824afe
MHS
193 {"fskdemod", CmdAWIDDemodFSK, 0, "['1'] Realtime AWID FSK demodulator (option '1' for one tag only)"},
194 {"sim", CmdAWIDSim, 0, "<Facility-Code> <Card Number> -- AWID tag simulator"},
709665b5 195 {"clone", CmdAWIDClone, 0, "<Facility-Code> <Card Number> <Q5> -- Clone AWID to T55x7 (tag must be in range of antenna)"},
37824afe 196 {NULL, NULL, 0, NULL}
dbf6e824
CY
197};
198
709665b5 199int CmdLFAWID(const char *Cmd) {
37824afe
MHS
200 CmdsParse(CommandTable, Cmd);
201 return 0;
dbf6e824
CY
202}
203
709665b5 204int CmdHelp(const char *Cmd) {
37824afe
MHS
205 CmdsHelp(CommandTable);
206 return 0;
dbf6e824 207}
Impressum, Datenschutz