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