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