]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
nested authentication works ok (tested)
[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 == CRYPT_ALL) {
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
98 // ----------------------------- crypto1 create
99 if (isNested)
100 crypto1_destroy(pcs);
101
102 // Init cipher with key
103 crypto1_create(pcs, ui64Key);
104
105 if (isNested == AUTH_NESTED) {
106 // decrypt nt with help of new key
107 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
108 } else {
109 // Load (plain) uid^nt into the cipher
110 crypto1_word(pcs, nt ^ uid, 0);
111 }
112
113 // some statistic
114 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
115
116 par = 0;
117 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
118 for (pos = 0; pos < 4; pos++)
119 {
120 mf_nr_ar[pos] = crypto1_byte(pcs, ar[pos], 0) ^ ar[pos];
121 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );
122 }
123
124 // Skip 32 bits in pseudo random generator
125 nt = prng_successor(nt,32);
126
127 // ar+parity
128 for (pos = 4; pos < 8; pos++)
129 {
130 nt = prng_successor(nt,8);
131 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
132 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );
133 }
134
135 // Transmit reader nonce and reader answer
136 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par);
137
138 // Receive 4 bit answer
139 len = ReaderReceive(receivedAnswer);
140 if (!len)
141 {
142 Dbprintf("Authentication failed. Card timeout.");
143 return 2;
144 }
145
146 memcpy(tmp4, receivedAnswer, 4);
147 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
148
149 if (ntpp != bytes_to_num(tmp4, 4)) {
150 Dbprintf("Authentication failed. Error card response.");
151 return 3;
152 }
153
154 return 0;
155 }
156
157 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
158 {
159 // variables
160 int len;
161 uint8_t bt[2];
162
163 uint8_t* receivedAnswer = mifare_get_bigbufptr();
164
165 // command MIFARE_CLASSIC_READBLOCK
166 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer);
167 if (len == 1) {
168 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
169 return 1;
170 }
171 if (len != 18) {
172 Dbprintf("Cmd Error: card timeout. len: %x", len);
173 return 2;
174 }
175
176 memcpy(bt, receivedAnswer + 16, 2);
177 AppendCrc14443a(receivedAnswer, 16);
178 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
179 Dbprintf("Cmd CRC response error.");
180 return 3;
181 }
182
183 memcpy(blockData, receivedAnswer, 16);
184 return 0;
185 }
186
187 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
188 {
189 // variables
190 int len, i;
191 uint32_t pos;
192 uint32_t par = 0;
193 byte_t res;
194
195 uint8_t d_block[18], d_block_enc[18];
196 uint8_t* receivedAnswer = mifare_get_bigbufptr();
197
198 // command MIFARE_CLASSIC_WRITEBLOCK
199 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer);
200
201 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
202 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
203 return 1;
204 }
205
206 memcpy(d_block, blockData, 16);
207 AppendCrc14443a(d_block, 16);
208
209 // crypto
210 par = 0;
211 for (pos = 0; pos < 18; pos++)
212 {
213 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
214 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );
215 }
216
217 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par);
218
219 // Receive the response
220 len = ReaderReceive(receivedAnswer);
221
222 res = 0;
223 for (i = 0; i < 4; i++)
224 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
225
226 if ((len != 1) || (res != 0x0A)) {
227 Dbprintf("Cmd send data2 Error: %02x", res);
228 return 2;
229 }
230
231 return 0;
232 }
233
234 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
235 {
236 // variables
237 int len;
238
239 // Mifare HALT
240 uint8_t* receivedAnswer = mifare_get_bigbufptr();
241
242 len = mifare_sendcmd_short(pcs, 1, 0x50, 0x00, receivedAnswer);
243 if (len != 0) {
244 Dbprintf("halt error. response len: %x", len);
245 return 1;
246 }
247
248 return 0;
249 }
Impressum, Datenschutz