]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/nonce2key/nonce2key.c
1 //-----------------------------------------------------------------------------
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // MIFARE Darkside hack
11 //-----------------------------------------------------------------------------
12 #include "nonce2key.h"
13 #include "mifarehost.h"
15 #include "proxmark3.h"
17 int nonce2key(uint32_t uid
, uint32_t nt
, uint32_t nr
, uint64_t par_info
, uint64_t ks_info
, uint64_t * key
) {
18 struct Crypto1State
*state
;
19 uint32_t i
, pos
, rr
= 0, nr_diff
;
20 byte_t bt
, ks3x
[8], par
[8][8];
22 // Reset the last three significant bits of the reader nonce
25 PrintAndLog("uid(%08x) nt(%08x) par(%016"llx
") ks(%016"llx
") nr(%08"llx
")\n", uid
, nt
, par_info
, ks_info
, nr
);
27 for ( pos
= 0; pos
< 8; pos
++ ) {
28 ks3x
[7-pos
] = (ks_info
>> (pos
*8)) & 0x0f;
29 bt
= (par_info
>> (pos
*8)) & 0xff;
31 for ( i
= 0; i
< 8; i
++) {
32 par
[7-pos
][i
] = (bt
>> i
) & 0x01;
36 printf("+----+--------+---+-----+---------------+\n");
37 printf("|diff|{nr} |ks3|ks3^5|parity |\n");
38 printf("+----+--------+---+-----+---------------+\n");
39 for ( i
= 0; i
< 8; i
++) {
40 nr_diff
= nr
| i
<< 5;
41 printf("| %02x |%08x| %01x | %01x |", i
<< 5, nr_diff
, ks3x
[i
], ks3x
[i
]^5);
43 for (pos
= 0; pos
< 7; pos
++) printf("%01x,", par
[i
][pos
]);
44 printf("%01x|\n", par
[i
][7]);
46 printf("+----+--------+---+-----+---------------+\n");
50 state
= lfsr_common_prefix(nr
, rr
, ks3x
, par
);
51 lfsr_rollback_word(state
, uid
^nt
, 0);
52 crypto1_get_lfsr(state
, key
);
53 crypto1_destroy(state
);
56 if ( t1
> 0 ) PrintAndLog("Time in nonce2key: %.0f ticks \n", (float)t1
);
60 // call when PAR == 0, special attack?
61 int nonce2key_ex(uint32_t uid
, uint32_t nt
, uint32_t nr
, uint64_t ks_info
, uint64_t * key
) {
62 struct Crypto1State
*state
;
63 uint32_t i
, pos
, key_count
;
66 uint64_t key_recovered
;
68 static uint32_t last_uid
;
69 static int64_t *last_keylist
;
71 if (last_uid
!= uid
&& last_keylist
!= NULL
) {
77 // Reset the last three significant bits of the reader nonce
80 PrintAndLog("uid(%08x) nt(%08x) ks(%016"llx
") nr(%08"llx
")\n", uid
, nt
, ks_info
, nr
);
82 for (pos
=0; pos
<8; pos
++) {
83 ks3x
[7-pos
] = (ks_info
>> (pos
*8)) & 0x0f;
86 PrintAndLog("parity is all zero,try special attack! just wait for few more seconds");
90 state
= lfsr_common_prefix_ex(nr
, ks3x
);
91 state_s
= (int64_t*)state
;
94 //sprintf(filename, "nt_%08x_%d.txt", nt, nr);
95 //printf("name %s\n", filename);
96 //FILE* fp = fopen(filename,"w");
97 for (i
= 0; (state
) && ((state
+ i
)->odd
!= -1); i
++) {
98 lfsr_rollback_word(state
+i
, uid
^nt
, 0);
99 crypto1_get_lfsr(state
+ i
, &key_recovered
);
100 *(state_s
+ i
) = key_recovered
;
101 //fprintf(fp, "%012llx\n",key_recovered);
108 qsort(state_s
, i
, sizeof(*state_s
), compar_int
);
111 //Create the intersection:
112 if ( last_keylist
!= NULL
) {
113 int64_t *p1
, *p2
, *p3
;
114 p1
= p3
= last_keylist
;
116 while ( *p1
!= -1 && *p2
!= -1 ) {
117 if (compar_int(p1
, p2
) == 0) {
118 printf("p1:%"llx
" p2:%"llx
" p3:%"llx
" key:%012"llx
"\n",(uint64_t)(p1
-last_keylist
),(uint64_t)(p2
-state_s
),(uint64_t)(p3
-last_keylist
),*p1
);
123 while (compar_int(p1
, p2
) == -1) ++p1
;
124 while (compar_int(p1
, p2
) == 1) ++p2
;
127 key_count
= p3
- last_keylist
;;
132 printf("key_count:%d\n", key_count
);
134 // The list may still contain several key candidates. Test each of them with mfCheckKeys
137 for (i
= 0; i
< key_count
; i
++) {
138 key64
= *(last_keylist
+ i
);
139 num_to_bytes(key64
, 6, keyBlock
);
141 if (!mfCheckKeys(0, 0, TRUE
, 1, keyBlock
, &key64
)) { //block 0,A,
151 if ( t1
> 0 ) PrintAndLog("Time in nonce2key_special: %.0f ticks \n", (float)t1
);
154 last_keylist
= state_s
;
158 int tryMfk32(uint8_t *data
, uint64_t *outputkey
){
159 struct Crypto1State
*s
,*t
;
160 uint64_t key
; // recovered key
161 uint32_t uid
= le32toh(data
);
162 uint32_t nt
= le32toh(data
+4); // tag challenge
163 uint32_t nr0_enc
= le32toh(data
+8); // first encrypted reader challenge
164 uint32_t ar0_enc
= le32toh(data
+12); // first encrypted reader response
167 uint32_t nr1_enc
= le32toh(data
+24); // second encrypted reader challenge
168 uint32_t ar1_enc
= le32toh(data
+28); // second encrypted reader response
169 bool isSuccess
= FALSE
;
172 PrintAndLog("Enter mfkey32");
173 clock_t t1
= clock();
174 s
= lfsr_recovery32(ar0_enc
^ prng_successor(nt
, 64), 0);
176 for(t
= s
; t
->odd
| t
->even
; ++t
) {
177 lfsr_rollback_word(t
, 0, 0);
178 lfsr_rollback_word(t
, nr0_enc
, 1);
179 lfsr_rollback_word(t
, uid
^ nt
, 0);
180 crypto1_get_lfsr(t
, &key
);
181 crypto1_word(t
, uid
^ nt
, 0);
182 crypto1_word(t
, nr1_enc
, 1);
183 if (ar1_enc
== (crypto1_word(t
, 0, 0) ^ prng_successor(nt
, 64))) {
184 PrintAndLog("Found Key: [%012"llx
"]", key
);
192 if ( t1
> 0 ) PrintAndLog("Time in mf32key: %.0f ticks \n", (float)t1
);
193 *outputkey
= ( isSuccess
) ? key
: 0;
198 int tryMfk32_moebius(uint8_t *data
, uint64_t *outputkey
){
199 struct Crypto1State
*s
, *t
;
200 uint64_t key
= 0; // recovered key
201 uint32_t uid
= le32toh(data
);
202 uint32_t nt0
= le32toh(data
+4); // first tag challenge (nonce)
203 uint32_t nr0_enc
= le32toh(data
+8); // first encrypted reader challenge
204 uint32_t ar0_enc
= le32toh(data
+12); // first encrypted reader response
205 //uint32_t uid1 = le32toh(data+16);
206 uint32_t nt1
= le32toh(data
+20); // second tag challenge (nonce)
207 uint32_t nr1_enc
= le32toh(data
+24); // second encrypted reader challenge
208 uint32_t ar1_enc
= le32toh(data
+28); // second encrypted reader response
209 bool isSuccess
= FALSE
;
212 PrintAndLog("Enter mfkey32_moebius");
213 clock_t t1
= clock();
215 s
= lfsr_recovery32(ar0_enc
^ prng_successor(nt0
, 64), 0);
217 for(t
= s
; t
->odd
| t
->even
; ++t
) {
218 lfsr_rollback_word(t
, 0, 0);
219 lfsr_rollback_word(t
, nr0_enc
, 1);
220 lfsr_rollback_word(t
, uid
^ nt0
, 0);
221 crypto1_get_lfsr(t
, &key
);
223 crypto1_word(t
, uid
^ nt1
, 0);
224 crypto1_word(t
, nr1_enc
, 1);
225 if (ar1_enc
== (crypto1_word(t
, 0, 0) ^ prng_successor(nt1
, 64))) {
226 PrintAndLog("Found Key: [%012"llx
"]",key
);
234 if ( t1
> 0 ) PrintAndLog("Time in mfkey32_moebius: %.0f ticks \n", (float)t1
);
235 *outputkey
= ( isSuccess
) ? key
: 0;
240 int tryMfk64_ex(uint8_t *data
, uint64_t *outputkey
){
241 uint32_t uid
= le32toh(data
);
242 uint32_t nt
= le32toh(data
+4); // tag challenge
243 uint32_t nr_enc
= le32toh(data
+8); // encrypted reader challenge
244 uint32_t ar_enc
= le32toh(data
+12); // encrypted reader response
245 uint32_t at_enc
= le32toh(data
+16); // encrypted tag response
246 return tryMfk64(uid
, nt
, nr_enc
, ar_enc
, at_enc
, outputkey
);
249 int tryMfk64(uint32_t uid
, uint32_t nt
, uint32_t nr_enc
, uint32_t ar_enc
, uint32_t at_enc
, uint64_t *outputkey
){
250 uint64_t key
= 0; // recovered key
251 uint32_t ks2
; // keystream used to encrypt reader response
252 uint32_t ks3
; // keystream used to encrypt tag response
253 struct Crypto1State
*revstate
;
255 PrintAndLog("Enter mfkey64");
256 clock_t t1
= clock();
258 // Extract the keystream from the messages
259 ks2
= ar_enc
^ prng_successor(nt
, 64);
260 ks3
= at_enc
^ prng_successor(nt
, 96);
261 revstate
= lfsr_recovery64(ks2
, ks3
);
262 lfsr_rollback_word(revstate
, 0, 0);
263 lfsr_rollback_word(revstate
, 0, 0);
264 lfsr_rollback_word(revstate
, nr_enc
, 1);
265 lfsr_rollback_word(revstate
, uid
^ nt
, 0);
266 crypto1_get_lfsr(revstate
, &key
);
267 PrintAndLog("Found Key: [%012"llx
"]", key
);
268 crypto1_destroy(revstate
);
272 if ( t1
> 0 ) PrintAndLog("Time in mfkey64: %.0f ticks \n", (float)t1
);