]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
1. fixed hf 14a mifare. added functionality to ignore one Nt
[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
f397b5cc
M
22int MF_DBGLEVEL = MF_DBG_ALL;\r
23\r
20f9a2a1
M
24uint8_t* mifare_get_bigbufptr(void) {\r
25 return (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes\r
26}\r
27\r
28int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer)\r
f89c7050
M
29{\r
30 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, NULL);\r
31}\r
32\r
33int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t* answer, uint32_t * parptr)\r
20f9a2a1
M
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
f89c7050
M
58 int len = ReaderReceivePar(answer, &par);\r
59 \r
60 if (parptr) *parptr = par;\r
20f9a2a1 61\r
4abe4f58 62 if (crypted == CRYPT_ALL) {\r
20f9a2a1
M
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
81int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint64_t isNested) \r
f89c7050
M
82{\r
83 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL);\r
84}\r
85\r
86int 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
87{\r
88 // variables\r
4abe4f58 89 int len; \r
20f9a2a1
M
90 uint32_t pos;\r
91 uint8_t tmp4[4];\r
4abe4f58
M
92 byte_t par = 0;\r
93 byte_t ar[4];\r
20f9a2a1
M
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
4abe4f58 97 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 98\r
4abe4f58 99 // Transmit MIFARE_CLASSIC_AUTH\r
20f9a2a1 100 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer);\r
f397b5cc 101 if (MF_DBGLEVEL >= 4) Dbprintf("rand nonce len: %x", len); \r
4abe4f58 102 if (len != 4) return 1;\r
20f9a2a1
M
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
20f9a2a1
M
111\r
112 // ----------------------------- crypto1 create\r
4abe4f58
M
113 if (isNested)\r
114 crypto1_destroy(pcs);\r
115\r
116 // Init cipher with key\r
20f9a2a1
M
117 crypto1_create(pcs, ui64Key);\r
118\r
4abe4f58
M
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
f397b5cc 128 if (!ntptr && (MF_DBGLEVEL >= 3))\r
f89c7050
M
129 Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r
130 \r
131 // save Nt\r
132 if (ntptr)\r
133 *ntptr = nt;\r
20f9a2a1
M
134\r
135 par = 0;\r
4abe4f58
M
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
20f9a2a1 140 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(ar[pos])) & 0x01) * 0x80 );\r
4abe4f58 141 } \r
20f9a2a1 142 \r
4abe4f58
M
143 // Skip 32 bits in pseudo random generator\r
144 nt = prng_successor(nt,32);\r
20f9a2a1
M
145\r
146 // ar+parity\r
4abe4f58
M
147 for (pos = 4; pos < 8; pos++)\r
148 {\r
20f9a2a1 149 nt = prng_successor(nt,8);\r
4abe4f58 150 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
20f9a2a1 151 par = (par >> 1)| ( ((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) * 0x80 );\r
4abe4f58 152 } \r
20f9a2a1 153 \r
4abe4f58
M
154 // Transmit reader nonce and reader answer\r
155 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par);\r
20f9a2a1 156\r
4abe4f58 157 // Receive 4 bit answer\r
20f9a2a1 158 len = ReaderReceive(receivedAnswer);\r
4abe4f58
M
159 if (!len)\r
160 {\r
f397b5cc 161 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r
20f9a2a1 162 return 2;\r
4abe4f58 163 }\r
20f9a2a1 164 \r
4abe4f58 165 memcpy(tmp4, receivedAnswer, 4);\r
20f9a2a1
M
166 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r
167 \r
168 if (ntpp != bytes_to_num(tmp4, 4)) {\r
f397b5cc 169 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r
20f9a2a1
M
170 return 3;\r
171 }\r
172\r
173 return 0;\r
174}\r
175\r
176int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
177{\r
178 // variables\r
4abe4f58 179 int len; \r
20f9a2a1
M
180 uint8_t bt[2];\r
181 \r
4abe4f58 182 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 183 \r
4abe4f58 184 // command MIFARE_CLASSIC_READBLOCK\r
20f9a2a1
M
185 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer);\r
186 if (len == 1) {\r
f397b5cc 187 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
188 return 1;\r
189 }\r
190 if (len != 18) {\r
f397b5cc 191 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r
20f9a2a1
M
192 return 2;\r
193 }\r
194\r
195 memcpy(bt, receivedAnswer + 16, 2);\r
4abe4f58 196 AppendCrc14443a(receivedAnswer, 16);\r
20f9a2a1 197 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
f397b5cc 198 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r
20f9a2a1
M
199 return 3;\r
200 }\r
201 \r
202 memcpy(blockData, receivedAnswer, 16);\r
203 return 0;\r
204}\r
205\r
206int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
207{\r
208 // variables\r
4abe4f58 209 int len, i; \r
20f9a2a1 210 uint32_t pos;\r
4abe4f58
M
211 uint32_t par = 0;\r
212 byte_t res;\r
20f9a2a1
M
213 \r
214 uint8_t d_block[18], d_block_enc[18];\r
4abe4f58 215 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1 216 \r
4abe4f58 217 // command MIFARE_CLASSIC_WRITEBLOCK\r
20f9a2a1
M
218 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer);\r
219\r
220 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
f397b5cc 221 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
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
4abe4f58
M
230 for (pos = 0; pos < 18; pos++)\r
231 {\r
232 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
20f9a2a1 233 par = (par >> 1) | ( ((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) * 0x20000 );\r
4abe4f58 234 } \r
20f9a2a1 235\r
4abe4f58 236 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par);\r
20f9a2a1 237\r
4abe4f58 238 // Receive the response\r
20f9a2a1
M
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
f397b5cc 246 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
20f9a2a1
M
247 return 2;\r
248 }\r
249 \r
250 return 0;\r
251}\r
252\r
253int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
254{\r
255 // variables\r
4abe4f58 256 int len; \r
20f9a2a1
M
257 \r
258 // Mifare HALT\r
4abe4f58 259 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
20f9a2a1
M
260\r
261 len = mifare_sendcmd_short(pcs, 1, 0x50, 0x00, receivedAnswer);\r
4abe4f58 262 if (len != 0) {\r
f397b5cc 263 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len); \r
20f9a2a1
M
264 return 1;\r
265 }\r
266\r
267 return 0;\r
268}\r
Impressum, Datenschutz