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