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