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