]>
Commit | Line | Data |
---|---|---|
4f131b53 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) Merlok - 2017 | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Command: hf mf list. It shows data from arm buffer. | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "cmdhflist.h" | |
12 | ||
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <string.h> | |
6612a5a2 | 16 | #include <stdint.h> |
17 | #include <stdbool.h> | |
18 | #include "util.h" | |
19 | #include "data.h" | |
20 | #include "ui.h" | |
21 | #include "iso14443crc.h" | |
22 | #include "parity.h" | |
23 | #include "protocols.h" | |
7b215d14 OM |
24 | #include "crapto1/crapto1.h" |
25 | #include "mifarehost.h" | |
c6a886fb | 26 | #include "mifaredefault.h" |
4f131b53 | 27 | |
28 | ||
6612a5a2 | 29 | enum MifareAuthSeq { |
30 | masNone, | |
31 | masNt, | |
32 | masNrAr, | |
33 | masAt, | |
747885a6 | 34 | masAuthComplete, |
aadc6bf1 | 35 | masFirstData, |
6612a5a2 | 36 | masData, |
6612a5a2 | 37 | masError, |
38 | }; | |
39 | static enum MifareAuthSeq MifareAuthState; | |
aadc6bf1 OM |
40 | static TAuthData AuthData; |
41 | ||
42 | void ClearAuthData() { | |
43 | AuthData.uid = 0; | |
44 | AuthData.nt = 0; | |
7b215d14 | 45 | AuthData.first_auth = true; |
e01bc794 OM |
46 | AuthData.ks2 = 0; |
47 | AuthData.ks3 = 0; | |
aadc6bf1 | 48 | } |
6612a5a2 | 49 | |
50 | /** | |
51 | * @brief iso14443A_CRC_check Checks CRC in command or response | |
52 | * @param isResponse | |
53 | * @param data | |
54 | * @param len | |
55 | * @return 0 : CRC-command, CRC not ok | |
56 | * 1 : CRC-command, CRC ok | |
57 | * 2 : Not crc-command | |
58 | */ | |
59 | uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
60 | { | |
61 | uint8_t b1,b2; | |
62 | ||
63 | if(len <= 2) return 2; | |
64 | ||
65 | if(isResponse & (len < 6)) return 2; | |
66 | ||
67 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); | |
68 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
69 | return 0; | |
70 | } else { | |
71 | return 1; | |
72 | } | |
73 | } | |
74 | ||
75 | uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
76 | { | |
77 | switch(MifareAuthState) { | |
78 | case masNone: | |
6612a5a2 | 79 | case masError: |
80 | return iso14443A_CRC_check(isResponse, data, len); | |
81 | default: | |
82 | return 2; | |
83 | } | |
6612a5a2 | 84 | } |
85 | ||
86 | void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
87 | { | |
88 | switch(cmd[0]) | |
89 | { | |
a31f7f89 OM |
90 | case ISO14443A_CMD_WUPA: |
91 | snprintf(exp,size,"WUPA"); | |
a31f7f89 | 92 | break; |
6612a5a2 | 93 | case ISO14443A_CMD_ANTICOLL_OR_SELECT:{ |
94 | // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor) | |
95 | // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK) | |
96 | if(cmd[1] == 0x70) | |
97 | { | |
98 | snprintf(exp,size,"SELECT_UID"); break; | |
99 | }else | |
100 | { | |
101 | snprintf(exp,size,"ANTICOLL"); break; | |
102 | } | |
103 | } | |
104 | case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{ | |
105 | //95 20 = Anticollision of cascade level2 | |
106 | //95 70 = Select of cascade level2 | |
107 | if(cmd[2] == 0x70) | |
108 | { | |
109 | snprintf(exp,size,"SELECT_UID-2"); break; | |
110 | }else | |
111 | { | |
112 | snprintf(exp,size,"ANTICOLL-2"); break; | |
113 | } | |
114 | } | |
a31f7f89 OM |
115 | case ISO14443A_CMD_REQA: |
116 | snprintf(exp,size,"REQA"); | |
a31f7f89 | 117 | break; |
6612a5a2 | 118 | case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break; |
119 | case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break; | |
120 | case ISO14443A_CMD_HALT: | |
121 | snprintf(exp,size,"HALT"); | |
122 | MifareAuthState = masNone; | |
123 | break; | |
124 | case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break; | |
125 | case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break; | |
126 | case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break; | |
127 | case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break; | |
128 | case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break; | |
129 | case MIFARE_AUTH_KEYA: | |
130 | if ( cmdsize > 3) { | |
131 | snprintf(exp,size,"AUTH-A(%d)",cmd[1]); | |
132 | MifareAuthState = masNt; | |
133 | } else { | |
134 | // case MIFARE_ULEV1_VERSION : both 0x60. | |
135 | snprintf(exp,size,"EV1 VERSION"); | |
136 | } | |
137 | break; | |
138 | case MIFARE_AUTH_KEYB: | |
139 | MifareAuthState = masNt; | |
140 | snprintf(exp,size,"AUTH-B(%d)",cmd[1]); | |
141 | break; | |
142 | case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break; | |
143 | case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break; | |
144 | case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break; | |
145 | case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break; | |
146 | case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break; | |
147 | case MIFARE_ULEV1_AUTH: | |
148 | if ( cmdsize == 7 ) | |
149 | snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] ); | |
150 | else | |
151 | snprintf(exp,size,"PWD-AUTH"); | |
152 | break; | |
153 | case MIFARE_ULEV1_FASTREAD:{ | |
154 | if ( cmdsize >=3 && cmd[2] <= 0xE6) | |
155 | snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]); | |
156 | else | |
157 | snprintf(exp,size,"?"); | |
158 | break; | |
159 | } | |
160 | case MIFARE_ULC_WRITE:{ | |
161 | if ( cmd[1] < 0x21 ) | |
162 | snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); | |
163 | else | |
164 | snprintf(exp,size,"?"); | |
165 | break; | |
166 | } | |
167 | case MIFARE_ULEV1_READ_CNT:{ | |
168 | if ( cmd[1] < 5 ) | |
169 | snprintf(exp,size,"READ CNT(%d)",cmd[1]); | |
170 | else | |
171 | snprintf(exp,size,"?"); | |
172 | break; | |
173 | } | |
174 | case MIFARE_ULEV1_INCR_CNT:{ | |
175 | if ( cmd[1] < 5 ) | |
176 | snprintf(exp,size,"INCR(%d)",cmd[1]); | |
177 | else | |
178 | snprintf(exp,size,"?"); | |
179 | break; | |
180 | } | |
181 | case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break; | |
182 | case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break; | |
183 | case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break; | |
184 | default: snprintf(exp,size,"?"); break; | |
185 | } | |
186 | return; | |
187 | } | |
188 | ||
b957bcd3 | 189 | void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) { |
2d7bdee3 | 190 | if (!isResponse && cmdsize == 1) { |
191 | switch(cmd[0]) { | |
192 | case ISO14443A_CMD_WUPA: | |
193 | case ISO14443A_CMD_REQA: | |
194 | MifareAuthState = masNone; | |
195 | break; | |
196 | default: | |
197 | break; | |
198 | } | |
199 | } | |
200 | ||
aadc6bf1 OM |
201 | // get UID |
202 | if (MifareAuthState == masNone) { | |
7b215d14 | 203 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) { |
aadc6bf1 OM |
204 | ClearAuthData(); |
205 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
206 | } | |
7b215d14 | 207 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) { |
aadc6bf1 OM |
208 | ClearAuthData(); |
209 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
210 | } | |
211 | } | |
212 | ||
6612a5a2 | 213 | switch(MifareAuthState) { |
214 | case masNt: | |
fb30f5a1 | 215 | if (cmdsize == 4 && isResponse) { |
aadc6bf1 | 216 | snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)"); |
6612a5a2 | 217 | MifareAuthState = masNrAr; |
aadc6bf1 | 218 | if (AuthData.first_auth) |
7b215d14 | 219 | AuthData.nt = bytes_to_num(cmd, 4); |
aadc6bf1 | 220 | else |
7b215d14 | 221 | AuthData.nt_enc = bytes_to_num(cmd, 4); |
b957bcd3 | 222 | AuthData.nt_enc_par = parity[0]; |
6612a5a2 | 223 | return; |
224 | } else { | |
225 | MifareAuthState = masError; | |
6612a5a2 | 226 | } |
227 | break; | |
228 | case masNrAr: | |
fb30f5a1 | 229 | if (cmdsize == 8 && !isResponse) { |
6c30a244 | 230 | snprintf(exp,size,"AUTH: nr ar (enc)"); |
6612a5a2 | 231 | MifareAuthState = masAt; |
7b215d14 OM |
232 | AuthData.nr_enc = bytes_to_num(cmd, 4); |
233 | AuthData.ar_enc = bytes_to_num(&cmd[4], 4); | |
b957bcd3 | 234 | AuthData.ar_enc_par = parity[0] << 4; |
6612a5a2 | 235 | return; |
236 | } else { | |
237 | MifareAuthState = masError; | |
238 | } | |
239 | break; | |
240 | case masAt: | |
fb30f5a1 | 241 | if (cmdsize == 4 && isResponse) { |
6c30a244 | 242 | snprintf(exp,size,"AUTH: at (enc)"); |
747885a6 | 243 | MifareAuthState = masAuthComplete; |
7b215d14 | 244 | AuthData.at_enc = bytes_to_num(cmd, 4); |
b957bcd3 | 245 | AuthData.at_enc_par = parity[0]; |
6612a5a2 | 246 | return; |
247 | } else { | |
248 | MifareAuthState = masError; | |
249 | } | |
250 | break; | |
251 | default: | |
252 | break; | |
253 | } | |
254 | ||
747885a6 | 255 | if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError))) |
6612a5a2 | 256 | annotateIso14443a(exp, size, cmd, cmdsize); |
257 | ||
258 | } | |
7b215d14 | 259 | |
2d7bdee3 | 260 | bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen) { |
747885a6 | 261 | static struct Crypto1State *traceCrypto1; |
dca8220f | 262 | static uint64_t mfLastKey; |
747885a6 | 263 | |
7b215d14 OM |
264 | *mfDataLen = 0; |
265 | ||
747885a6 OM |
266 | if (MifareAuthState == masAuthComplete) { |
267 | if (traceCrypto1) { | |
268 | crypto1_destroy(traceCrypto1); | |
0113dcf3 | 269 | traceCrypto1 = NULL; |
747885a6 OM |
270 | } |
271 | ||
272 | MifareAuthState = masFirstData; | |
273 | return false; | |
274 | } | |
275 | ||
7b215d14 OM |
276 | if (cmdsize > 32) |
277 | return false; | |
278 | ||
279 | if (MifareAuthState == masFirstData) { | |
280 | if (AuthData.first_auth) { | |
e01bc794 OM |
281 | AuthData.ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64); |
282 | AuthData.ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96); | |
0113dcf3 | 283 | |
284 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
285 | PrintAndLog(" | * | key | probable key:%010"PRIx64" Prng:%s ks2:%08x ks3:%08x | |", | |
286 | mfLastKey, | |
28ee794f | 287 | validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD", |
e01bc794 OM |
288 | AuthData.ks2, |
289 | AuthData.ks3); | |
7b215d14 OM |
290 | |
291 | AuthData.first_auth = false; | |
747885a6 | 292 | |
e01bc794 | 293 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7b215d14 | 294 | } else { |
0113dcf3 | 295 | if (traceCrypto1) { |
296 | crypto1_destroy(traceCrypto1); | |
297 | traceCrypto1 = NULL; | |
298 | } | |
299 | ||
747885a6 | 300 | // check last used key |
0113dcf3 | 301 | if (false && mfLastKey) { |
2d7bdee3 | 302 | if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize, parity)) { |
0113dcf3 | 303 | PrintAndLog(" | * | key | last used key:%010"PRIx64" ks2:%08x ks3:%08x | |", |
304 | mfLastKey, | |
2d7bdee3 | 305 | AuthData.ks2, |
306 | AuthData.ks3); | |
307 | ||
308 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
c6a886fb | 309 | }; |
747885a6 OM |
310 | } |
311 | ||
312 | // check default keys | |
0113dcf3 | 313 | if (false && !traceCrypto1) { |
7bea1581 | 314 | for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){ |
2d7bdee3 | 315 | if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize, parity)) { |
0113dcf3 | 316 | PrintAndLog(" | * | key | default key:%010"PRIx64" ks2:%08x ks3:%08x | |", |
317 | MifareDefaultKeys[defaultKeyCounter], | |
2d7bdee3 | 318 | AuthData.ks2, |
319 | AuthData.ks3); | |
320 | ||
0113dcf3 | 321 | mfLastKey = MifareDefaultKeys[defaultKeyCounter]; |
856da9a1 | 322 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7bea1581 OM |
323 | break; |
324 | }; | |
325 | } | |
c6a886fb | 326 | } |
747885a6 OM |
327 | |
328 | // nested | |
7bea1581 | 329 | if (!traceCrypto1 && validate_prng_nonce(AuthData.nt)) { |
0113dcf3 | 330 | uint32_t ntx = prng_successor(AuthData.nt, 90); |
7bea1581 OM |
331 | for (int i = 0; i < 16383; i++) { |
332 | ntx = prng_successor(ntx, 1); | |
333 | if (NTParityChk(&AuthData, ntx)){ | |
334 | ||
335 | uint32_t ks2 = AuthData.ar_enc ^ prng_successor(ntx, 64); | |
336 | uint32_t ks3 = AuthData.at_enc ^ prng_successor(ntx, 96); | |
337 | struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3); | |
338 | memcpy(mfData, cmd, cmdsize); | |
339 | mf_crypto1_decrypt(pcs, mfData, cmdsize, 0); | |
340 | ||
341 | crypto1_destroy(pcs); | |
0113dcf3 | 342 | if (CheckCrypto1Parity(cmd, cmdsize, mfData, parity) && CheckCrc14443(CRC_14443_A, mfData, cmdsize)) { |
e01bc794 OM |
343 | AuthData.ks2 = ks2; |
344 | AuthData.ks3 = ks3; | |
0113dcf3 | 345 | |
346 | AuthData.nt = ntx; | |
347 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
348 | PrintAndLog(" | * | key | nested probable key:%010"PRIx64" ks2:%08x ks3:%08x | |", | |
349 | mfLastKey, | |
350 | AuthData.ks2, | |
351 | AuthData.ks3); | |
352 | ||
e01bc794 | 353 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); |
7bea1581 OM |
354 | break; |
355 | } | |
356 | } | |
357 | } | |
747885a6 OM |
358 | } |
359 | ||
360 | //hardnested | |
7bea1581 | 361 | if (!traceCrypto1) { |
2d7bdee3 | 362 | printf("hardnested not implemented. uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc); |
7bea1581 | 363 | } |
7b215d14 OM |
364 | } |
365 | ||
366 | ||
367 | ||
368 | MifareAuthState = masData; | |
7b215d14 OM |
369 | } |
370 | ||
747885a6 OM |
371 | if (MifareAuthState == masData && traceCrypto1) { |
372 | memcpy(mfData, cmd, cmdsize); | |
373 | mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0); | |
374 | *mfDataLen = cmdsize; | |
7b215d14 OM |
375 | } |
376 | ||
377 | return *mfDataLen > 0; | |
378 | } | |
379 | ||
dca8220f OM |
380 | bool NTParityChk(TAuthData *ad, uint32_t ntx) { |
381 | if ( | |
382 | (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) || | |
383 | (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) || | |
384 | (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01)) | |
385 | ) | |
386 | return false; | |
387 | ||
388 | uint32_t ar = prng_successor(ntx, 64); | |
389 | if ( | |
390 | (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) || | |
391 | (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) || | |
392 | (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01)) | |
393 | ) | |
394 | return false; | |
395 | ||
396 | uint32_t at = prng_successor(ntx, 96); | |
397 | if ( | |
398 | (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) || | |
399 | (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) || | |
400 | (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) || | |
401 | (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01)) | |
402 | ) | |
403 | return false; | |
404 | ||
405 | return true; | |
406 | } | |
407 | ||
2d7bdee3 | 408 | bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity) { |
dca8220f OM |
409 | uint8_t buf[32] = {0}; |
410 | struct Crypto1State *pcs; | |
411 | ||
2d7bdee3 | 412 | AuthData.ks2 = 0; |
413 | AuthData.ks3 = 0; | |
414 | ||
dca8220f OM |
415 | pcs = crypto1_create(key); |
416 | uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc; | |
417 | uint32_t ar = prng_successor(nt1, 64); | |
418 | uint32_t at = prng_successor(nt1, 96); | |
2d7bdee3 | 419 | |
420 | crypto1_word(pcs, ad->nr_enc, 1); | |
421 | // uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc; // if needs deciphered nr | |
dca8220f OM |
422 | uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc; |
423 | uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc; | |
dca8220f | 424 | |
2d7bdee3 | 425 | if (!(ar == ar1 && at == at1 && NTParityChk(ad, nt1))) |
dca8220f | 426 | return false; |
dca8220f OM |
427 | |
428 | memcpy(buf, cmd, cmdsize); | |
429 | mf_crypto1_decrypt(pcs, buf, cmdsize, 0); | |
430 | ||
e0158d33 OM |
431 | crypto1_destroy(pcs); |
432 | ||
2d7bdee3 | 433 | if (!CheckCrypto1Parity(cmd, cmdsize, buf, parity)) |
856da9a1 | 434 | return false; |
2d7bdee3 | 435 | |
597fca4f | 436 | if(!CheckCrc14443(CRC_14443_A, buf, cmdsize)) |
437 | return false; | |
438 | ||
0113dcf3 | 439 | AuthData.nt = nt1; |
2d7bdee3 | 440 | AuthData.ks2 = AuthData.ar_enc ^ ar; |
441 | AuthData.ks3 = AuthData.at_enc ^ at; | |
442 | ||
443 | return true; | |
444 | } | |
445 | ||
597fca4f | 446 | bool CheckCrypto1Parity(uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, uint8_t *parity_enc) { |
2d7bdee3 | 447 | for (int i = 0; i < cmdsize - 1; i++) { |
8253ca0e | 448 | if (oddparity8(cmd[i]) ^ (cmd[i + 1] & 0x01) ^ ((parity_enc[i / 8] >> (7 - i % 8)) & 0x01) ^ (cmd_enc[i + 1] & 0x01)) |
449 | return false; | |
856da9a1 | 450 | } |
2d7bdee3 | 451 | |
452 | return true; | |
bf52d74a | 453 | } |
0113dcf3 | 454 | |
455 | uint64_t GetCrypto1ProbableKey(TAuthData *ad) { | |
456 | struct Crypto1State *revstate = lfsr_recovery64(ad->ks2, ad->ks3); | |
457 | lfsr_rollback_word(revstate, 0, 0); | |
458 | lfsr_rollback_word(revstate, 0, 0); | |
459 | lfsr_rollback_word(revstate, ad->nr_enc, 1); | |
460 | lfsr_rollback_word(revstate, ad->uid ^ ad->nt, 0); | |
461 | ||
462 | uint64_t lfsr = 0; | |
463 | crypto1_get_lfsr(revstate, &lfsr); | |
464 | crypto1_destroy(revstate); | |
465 | ||
466 | return lfsr; | |
467 | } |