]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
ede1cbc9de0bce383e8d3aeb0de34f5a0d05faa4
[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 uint8_t dcmd[4], ecmd[4];
29 uint32_t pos, par, res;
30
31 dcmd[0] = cmd;
32 dcmd[1] = data;
33 AppendCrc14443a(dcmd, 2);
34
35 memcpy(ecmd, dcmd, sizeof(dcmd));
36
37 if (crypted) {
38 par = 0;
39 for (pos = 0; pos < 4; pos++)
40 {
41 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
42 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) * 0x08 );
43 }
44
45 ReaderTransmitPar(ecmd, sizeof(ecmd), par);
46
47 } else {
48 ReaderTransmit(dcmd, sizeof(dcmd));
49 }
50
51 int len = ReaderReceive(answer);
52
53 if (crypted) {
54 if (len == 1) {
55 res = 0;
56 for (pos = 0; pos < 4; pos++)
57 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
58
59 answer[0] = res;
60
61 } else {
62 for (pos = 0; pos < len; pos++)
63 {
64 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
65 }
66 }
67 }
68
69 return len;
70 }
71
72 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested)
73 {
74 // variables
75 int len;
76 uint32_t pos;
77 uint8_t tmp4[4];
78 byte_t par = 0;
79 byte_t ar[4];
80 uint32_t nt, ntpp; // Supplied tag nonce
81
82 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
83 uint8_t* receivedAnswer = mifare_get_bigbufptr();
84
85 // Transmit MIFARE_CLASSIC_AUTH
86 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer);
87 // Dbprintf("rand nonce len: %x", len);
88 if (len != 4) return 1;
89
90 ar[0] = 0x55;
91 ar[1] = 0x41;
92 ar[2] = 0x49;
93 ar[3] = 0x92;
94
95 // Save the tag nonce (nt)
96 nt = bytes_to_num(receivedAnswer, 4);
97 Dbprintf("uid: %x nt: %x", uid, nt);
98
99 // ----------------------------- crypto1 create
100 // Init cipher with key
101 crypto1_create(pcs, ui64Key);
102
103 // Load (plain) uid^nt into the cipher
104 crypto1_word(pcs, nt ^ uid, 0);
105
106 par = 0;
107 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
108 for (pos = 0; pos < 4; pos++)
109 {
110 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];
111 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );
112 }
113
114 // Skip 32 bits in pseudo random generator
115 nt = prng_successor(nt,32);
116
117 // ar+parity
118 for (pos = 4; pos < 8; pos++)
119 {
120 nt = prng_successor(nt,8);
121 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
122 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );
123 }
124
125 // Transmit reader nonce and reader answer
126 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par);
127
128 // Receive 4 bit answer
129 len = ReaderReceive(receivedAnswer);
130 if (!len)
131 {
132 Dbprintf("Authentication failed. Card timeout.");
133 return 2;
134 }
135
136 memcpy(tmp4, receivedAnswer, 4);
137 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
138
139 if (ntpp != bytes_to_num(tmp4, 4)) {
140 Dbprintf("Authentication failed. Error card response.");
141 return 3;
142 }
143
144 return 0;
145 }
146
147 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
148 {
149 // variables
150 int len;
151 uint8_t bt[2];
152
153 uint8_t* receivedAnswer = mifare_get_bigbufptr();
154
155 // command MIFARE_CLASSIC_READBLOCK
156 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer);
157 if (len == 1) {
158 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
159 return 1;
160 }
161 if (len != 18) {
162 Dbprintf("Cmd Error: card timeout. len: %x", len);
163 return 2;
164 }
165
166 memcpy(bt, receivedAnswer + 16, 2);
167 AppendCrc14443a(receivedAnswer, 16);
168 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
169 Dbprintf("Cmd CRC response error.");
170 return 3;
171 }
172
173 memcpy(blockData, receivedAnswer, 16);
174 return 0;
175 }
176
177 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
178 {
179 // variables
180 int len, i;
181 uint32_t pos;
182 uint32_t par = 0;
183 byte_t res;
184
185 uint8_t d_block[18], d_block_enc[18];
186 uint8_t* receivedAnswer = mifare_get_bigbufptr();
187
188 // command MIFARE_CLASSIC_WRITEBLOCK
189 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer);
190
191 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
192 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
193 return 1;
194 }
195
196 memcpy(d_block, blockData, 16);
197 AppendCrc14443a(d_block, 16);
198
199 // crypto
200 par = 0;
201 for (pos = 0; pos < 18; pos++)
202 {
203 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
204 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );
205 }
206
207 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par);
208
209 // Receive the response
210 len = ReaderReceive(receivedAnswer);
211
212 res = 0;
213 for (i = 0; i < 4; i++)
214 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
215
216 if ((len != 1) || (res != 0x0A)) {
217 Dbprintf("Cmd send data2 Error: %02x", res);
218 return 2;
219 }
220
221 return 0;
222 }
223
224 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
225 {
226 // variables
227 int len;
228
229 // Mifare HALT
230 uint8_t* receivedAnswer = mifare_get_bigbufptr();
231
232 len = mifare_sendcmd_short(pcs, 1, 0x50, 0x00, receivedAnswer);
233 if (len != 0) {
234 Dbprintf("halt error. response len: %x", len);
235 return 1;
236 }
237
238 return 0;
239 }
Impressum, Datenschutz