]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
c5843fd7474decf2db8adbf9c9be7345f2827fbb
[proxmark3-svn] / armsrc / mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011
3 // Many authors, whom made 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 // 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 int MF_DBGLEVEL = MF_DBG_ALL;
23
24 // memory management
25 uint8_t* mifare_get_bigbufptr(void) {
26 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET); // was 3560 - tied to other size changes
27 }
28 uint8_t* eml_get_bigbufptr_sendbuf(void) {
29 return (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
30 }
31 uint8_t* eml_get_bigbufptr_recbuf(void) {
32 return (((uint8_t *)BigBuf) + MIFARE_BUFF_OFFSET);
33 }
34 uint8_t* eml_get_bigbufptr_cardmem(void) {
35 return (((uint8_t *)BigBuf) + CARD_MEMORY);
36 }
37
38 // crypto1 helpers
39 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
40 uint8_t bt = 0;
41 int i;
42
43 if (len != 1) {
44 for (i = 0; i < len; i++)
45 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
46 } else {
47 bt = 0;
48 for (i = 0; i < 4; i++)
49 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;
50
51 data[0] = bt;
52 }
53 return;
54 }
55
56 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, int len, uint32_t *par) {
57 uint8_t bt = 0;
58 int i;
59 uint32_t mltpl = 1 << (len - 1); // for len=18 it=0x20000
60 *par = 0;
61 for (i = 0; i < len; i++) {
62 bt = data[i];
63 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
64 *par = (*par >> 1) | ( ((filter(pcs->odd) ^ oddparity(bt)) & 0x01) * mltpl );
65 }
66 return;
67 }
68
69 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
70 uint8_t bt = 0;
71 int i;
72
73 for (i = 0; i < 4; i++)
74 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
75
76 return bt;
77 }
78
79 // send commands
80 int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer)
81 {
82 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, NULL);
83 }
84
85 int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t * parptr)
86 {
87 uint8_t dcmd[4], ecmd[4];
88 uint32_t pos, par, res;
89
90 dcmd[0] = cmd;
91 dcmd[1] = data;
92 AppendCrc14443a(dcmd, 2);
93
94 memcpy(ecmd, dcmd, sizeof(dcmd));
95
96 if (crypted) {
97 par = 0;
98 for (pos = 0; pos < 4; pos++)
99 {
100 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
101 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) * 0x08 );
102 }
103
104 ReaderTransmitPar(ecmd, sizeof(ecmd), par);
105
106 } else {
107 ReaderTransmit(dcmd, sizeof(dcmd));
108 }
109
110 int len = ReaderReceivePar(answer, &par);
111
112 if (parptr) *parptr = par;
113
114 if (crypted == CRYPT_ALL) {
115 if (len == 1) {
116 res = 0;
117 for (pos = 0; pos < 4; pos++)
118 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
119
120 answer[0] = res;
121
122 } else {
123 for (pos = 0; pos < len; pos++)
124 {
125 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
126 }
127 }
128 }
129
130 return len;
131 }
132
133 // mifare commands
134 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested)
135 {
136 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL);
137 }
138
139 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)
140 {
141 // variables
142 int len;
143 uint32_t pos;
144 uint8_t tmp4[4];
145 byte_t par = 0;
146 byte_t ar[4];
147 uint32_t nt, ntpp; // Supplied tag nonce
148
149 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
150 uint8_t* receivedAnswer = mifare_get_bigbufptr();
151
152 // Transmit MIFARE_CLASSIC_AUTH
153 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer);
154 if (MF_DBGLEVEL >= 4) Dbprintf("rand nonce len: %x", len);
155 if (len != 4) return 1;
156
157 ar[0] = 0x55;
158 ar[1] = 0x41;
159 ar[2] = 0x49;
160 ar[3] = 0x92;
161
162 // Save the tag nonce (nt)
163 nt = bytes_to_num(receivedAnswer, 4);
164
165 // ----------------------------- crypto1 create
166 if (isNested)
167 crypto1_destroy(pcs);
168
169 // Init cipher with key
170 crypto1_create(pcs, ui64Key);
171
172 if (isNested == AUTH_NESTED) {
173 // decrypt nt with help of new key
174 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
175 } else {
176 // Load (plain) uid^nt into the cipher
177 crypto1_word(pcs, nt ^ uid, 0);
178 }
179
180 // some statistic
181 if (!ntptr && (MF_DBGLEVEL >= 3))
182 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
183
184 // save Nt
185 if (ntptr)
186 *ntptr = nt;
187
188 par = 0;
189 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
190 for (pos = 0; pos < 4; pos++)
191 {
192 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];
193 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );
194 }
195
196 // Skip 32 bits in pseudo random generator
197 nt = prng_successor(nt,32);
198
199 // ar+parity
200 for (pos = 4; pos < 8; pos++)
201 {
202 nt = prng_successor(nt,8);
203 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
204 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );
205 }
206
207 // Transmit reader nonce and reader answer
208 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par);
209
210 // Receive 4 bit answer
211 len = ReaderReceive(receivedAnswer);
212 if (!len)
213 {
214 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
215 return 2;
216 }
217
218 memcpy(tmp4, receivedAnswer, 4);
219 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
220
221 if (ntpp != bytes_to_num(tmp4, 4)) {
222 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
223 return 3;
224 }
225
226 return 0;
227 }
228
229 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
230 {
231 // variables
232 int len;
233 uint8_t bt[2];
234
235 uint8_t* receivedAnswer = mifare_get_bigbufptr();
236
237 // command MIFARE_CLASSIC_READBLOCK
238 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer);
239 if (len == 1) {
240 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
241 return 1;
242 }
243 if (len != 18) {
244 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
245 return 2;
246 }
247
248 memcpy(bt, receivedAnswer + 16, 2);
249 AppendCrc14443a(receivedAnswer, 16);
250 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
251 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
252 return 3;
253 }
254
255 memcpy(blockData, receivedAnswer, 16);
256 return 0;
257 }
258
259 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
260 {
261 // variables
262 int len, i;
263 uint32_t pos;
264 uint32_t par = 0;
265 byte_t res;
266
267 uint8_t d_block[18], d_block_enc[18];
268 uint8_t* receivedAnswer = mifare_get_bigbufptr();
269
270 // command MIFARE_CLASSIC_WRITEBLOCK
271 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer);
272
273 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
274 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
275 return 1;
276 }
277
278 memcpy(d_block, blockData, 16);
279 AppendCrc14443a(d_block, 16);
280
281 // crypto
282 par = 0;
283 for (pos = 0; pos < 18; pos++)
284 {
285 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
286 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );
287 }
288
289 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par);
290
291 // Receive the response
292 len = ReaderReceive(receivedAnswer);
293
294 res = 0;
295 for (i = 0; i < 4; i++)
296 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
297
298 if ((len != 1) || (res != 0x0A)) {
299 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
300 return 2;
301 }
302
303 return 0;
304 }
305
306 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
307 {
308 // variables
309 int len;
310
311 // Mifare HALT
312 uint8_t* receivedAnswer = mifare_get_bigbufptr();
313
314 len = mifare_sendcmd_short(pcs, 1, 0x50, 0x00, receivedAnswer);
315 if (len != 0) {
316 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
317 return 1;
318 }
319
320 return 0;
321 }
322
323 // work with emulator memory
324 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
325 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
326
327 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
328 }
329
330 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
331 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
332
333 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
334 }
335
336 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
337 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
338
339 memcpy(data, emCARD + bytePtr, byteCount);
340 }
341
342 int emlCheckValBl(int blockNum) {
343 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
344 uint8_t* data = emCARD + blockNum * 16;
345
346 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
347 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
348 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
349 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
350 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
351 (data[12] != (data[15] ^ 0xff))
352 )
353 return 1;
354 return 0;
355 }
356
357 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
358 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
359 uint8_t* data = emCARD + blockNum * 16;
360
361 if (emlCheckValBl(blockNum)) {
362 return 1;
363 }
364
365 memcpy(blReg, data, 4);
366 *blBlock = data[12];
367
368 return 0;
369 }
370
371 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
372 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
373 uint8_t* data = emCARD + blockNum * 16;
374
375 memcpy(data + 0, &blReg, 4);
376 memcpy(data + 8, &blReg, 4);
377 blReg = blReg ^ 0xffffffff;
378 memcpy(data + 4, &blReg, 4);
379
380 data[12] = blBlock;
381 data[13] = blBlock ^ 0xff;
382 data[14] = blBlock;
383 data[15] = blBlock ^ 0xff;
384
385 return 0;
386 }
387
388 uint64_t emlGetKey(int sectorNum, int keyType) {
389 uint8_t key[6];
390 uint8_t* emCARD = eml_get_bigbufptr_cardmem();
391
392 memcpy(key, emCARD + 3 * 16 + sectorNum * 4 * 16 + keyType * 10, 6);
393 return bytes_to_num(key, 6);
394 }
395
396 void emlClearMem(void) {
397 int i;
398
399 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
400 const uint8_t empty[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
401 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
402 // fill sectors data
403 for(i = 0; i < 16; i++) {
404 emlSetMem((uint8_t *)empty, i * 4 + 0, 1);
405 emlSetMem((uint8_t *)empty, i * 4 + 1, 1);
406 emlSetMem((uint8_t *)empty, i * 4 + 2, 1);
407 emlSetMem((uint8_t *)trailer, i * 4 + 3, 1);
408 }
409
410 // uid
411 emlSetMem((uint8_t *)uid, 0, 1);
412 return;
413 }
Impressum, Datenschutz