]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
improved version of "hf 14a mifare" command
[proxmark3-svn] / armsrc / mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011
3 // Many authors, that makes it possible
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // code for work with mifare cards.
10 //-----------------------------------------------------------------------------
11
12 #include "proxmark3.h"
13 #include "apps.h"
14 #include "util.h"
15 #include "string.h"
16
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
19 #include "crapto1.h"
20 #include "mifareutil.h"
21
22 uint8_t* mifare_get_bigbufptr(void) {
23 return (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
24 }
25
26 int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer)
27 {
28 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, NULL);
29 }
30
31 int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t * parptr)
32 {
33 uint8_t dcmd[4], ecmd[4];
34 uint32_t pos, par, res;
35
36 dcmd[0] = cmd;
37 dcmd[1] = data;
38 AppendCrc14443a(dcmd, 2);
39
40 memcpy(ecmd, dcmd, sizeof(dcmd));
41
42 if (crypted) {
43 par = 0;
44 for (pos = 0; pos < 4; pos++)
45 {
46 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
47 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) * 0x08 );
48 }
49
50 ReaderTransmitPar(ecmd, sizeof(ecmd), par);
51
52 } else {
53 ReaderTransmit(dcmd, sizeof(dcmd));
54 }
55
56 int len = ReaderReceivePar(answer, &par);
57
58 if (parptr) *parptr = par;
59
60 if (crypted == CRYPT_ALL) {
61 if (len == 1) {
62 res = 0;
63 for (pos = 0; pos < 4; pos++)
64 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
65
66 answer[0] = res;
67
68 } else {
69 for (pos = 0; pos < len; pos++)
70 {
71 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
72 }
73 }
74 }
75
76 return len;
77 }
78
79 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested)
80 {
81 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL);
82 }
83
84 int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested, uint32_t * ntptr)
85 {
86 // variables
87 int len;
88 uint32_t pos;
89 uint8_t tmp4[4];
90 byte_t par = 0;
91 byte_t ar[4];
92 uint32_t nt, ntpp; // Supplied tag nonce
93
94 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
95 uint8_t* receivedAnswer = mifare_get_bigbufptr();
96
97 // Transmit MIFARE_CLASSIC_AUTH
98 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer);
99 // Dbprintf("rand nonce len: %x", len);
100 if (len != 4) return 1;
101
102 ar[0] = 0x55;
103 ar[1] = 0x41;
104 ar[2] = 0x49;
105 ar[3] = 0x92;
106
107 // Save the tag nonce (nt)
108 nt = bytes_to_num(receivedAnswer, 4);
109
110 // ----------------------------- crypto1 create
111 if (isNested)
112 crypto1_destroy(pcs);
113
114 // Init cipher with key
115 crypto1_create(pcs, ui64Key);
116
117 if (isNested == AUTH_NESTED) {
118 // decrypt nt with help of new key
119 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
120 } else {
121 // Load (plain) uid^nt into the cipher
122 crypto1_word(pcs, nt ^ uid, 0);
123 }
124
125 // some statistic
126 if (!ntptr)
127 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
128
129 // save Nt
130 if (ntptr)
131 *ntptr = nt;
132
133 par = 0;
134 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
135 for (pos = 0; pos < 4; pos++)
136 {
137 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];
138 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );
139 }
140
141 // Skip 32 bits in pseudo random generator
142 nt = prng_successor(nt,32);
143
144 // ar+parity
145 for (pos = 4; pos < 8; pos++)
146 {
147 nt = prng_successor(nt,8);
148 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
149 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );
150 }
151
152 // Transmit reader nonce and reader answer
153 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par);
154
155 // Receive 4 bit answer
156 len = ReaderReceive(receivedAnswer);
157 if (!len)
158 {
159 Dbprintf("Authentication failed. Card timeout.");
160 return 2;
161 }
162
163 memcpy(tmp4, receivedAnswer, 4);
164 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
165
166 if (ntpp != bytes_to_num(tmp4, 4)) {
167 Dbprintf("Authentication failed. Error card response.");
168 return 3;
169 }
170
171 return 0;
172 }
173
174 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
175 {
176 // variables
177 int len;
178 uint8_t bt[2];
179
180 uint8_t* receivedAnswer = mifare_get_bigbufptr();
181
182 // command MIFARE_CLASSIC_READBLOCK
183 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer);
184 if (len == 1) {
185 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
186 return 1;
187 }
188 if (len != 18) {
189 Dbprintf("Cmd Error: card timeout. len: %x", len);
190 return 2;
191 }
192
193 memcpy(bt, receivedAnswer + 16, 2);
194 AppendCrc14443a(receivedAnswer, 16);
195 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
196 Dbprintf("Cmd CRC response error.");
197 return 3;
198 }
199
200 memcpy(blockData, receivedAnswer, 16);
201 return 0;
202 }
203
204 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
205 {
206 // variables
207 int len, i;
208 uint32_t pos;
209 uint32_t par = 0;
210 byte_t res;
211
212 uint8_t d_block[18], d_block_enc[18];
213 uint8_t* receivedAnswer = mifare_get_bigbufptr();
214
215 // command MIFARE_CLASSIC_WRITEBLOCK
216 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer);
217
218 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
219 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
220 return 1;
221 }
222
223 memcpy(d_block, blockData, 16);
224 AppendCrc14443a(d_block, 16);
225
226 // crypto
227 par = 0;
228 for (pos = 0; pos < 18; pos++)
229 {
230 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
231 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );
232 }
233
234 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par);
235
236 // Receive the response
237 len = ReaderReceive(receivedAnswer);
238
239 res = 0;
240 for (i = 0; i < 4; i++)
241 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
242
243 if ((len != 1) || (res != 0x0A)) {
244 Dbprintf("Cmd send data2 Error: %02x", res);
245 return 2;
246 }
247
248 return 0;
249 }
250
251 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
252 {
253 // variables
254 int len;
255
256 // Mifare HALT
257 uint8_t* receivedAnswer = mifare_get_bigbufptr();
258
259 len = mifare_sendcmd_short(pcs, 1, 0x50, 0x00, receivedAnswer);
260 if (len != 0) {
261 Dbprintf("halt error. response len: %x", len);
262 return 1;
263 }
264
265 return 0;
266 }
Impressum, Datenschutz