]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/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 //-----------------------------------------------------------------------------
13 #include "nonce2key.h"
18 #include "mifarehost.h"
20 #include "crapto1/crapto1.h"
22 // recover key from 2 different reader responses on same tag challenge
23 bool mfkey32(nonces_t data
, uint64_t *outputkey
) {
24 struct Crypto1State
*s
,*t
;
26 uint64_t key
= 0; // recovered key
27 bool isSuccess
= false;
30 uint64_t t1
= msclock();
32 s
= lfsr_recovery32(data
.ar
^ prng_successor(data
.nonce
, 64), 0);
34 for(t
= s
; t
->odd
| t
->even
; ++t
) {
35 lfsr_rollback_word(t
, 0, 0);
36 lfsr_rollback_word(t
, data
.nr
, 1);
37 lfsr_rollback_word(t
, data
.cuid
^ data
.nonce
, 0);
38 crypto1_get_lfsr(t
, &key
);
39 crypto1_word(t
, data
.cuid
^ data
.nonce
, 0);
40 crypto1_word(t
, data
.nr2
, 1);
41 if (data
.ar2
== (crypto1_word(t
, 0, 0) ^ prng_successor(data
.nonce
, 64))) {
42 //PrintAndLog("Found Key: [%012" PRIx64 "]",key);
45 if (counter
== 20) break;
48 isSuccess
= (counter
== 1);
50 //if ( t1 > 0 ) PrintAndLog("Time in mfkey32: %.1f seconds \nFound %d possible keys", (float)t1/1000.0, counter);
51 *outputkey
= ( isSuccess
) ? outkey
: 0;
53 /* //un-comment to save all keys to a stats.txt file
55 if ((fout = fopen("stats.txt","ab")) == NULL) {
56 PrintAndLog("Could not create file name stats.txt");
59 fprintf(fout, "mfkey32,%d,%08x,%d,%s,%04x%08x,%.0Lf\r\n", counter, data.cuid, data.sector, (data.keytype) ? "B" : "A", (uint32_t)(outkey>>32) & 0xFFFF,(uint32_t)(outkey&0xFFFFFFFF),(long double)t1);
65 // recover key from 2 reader responses on 2 different tag challenges
66 bool mfkey32_moebius(nonces_t data
, uint64_t *outputkey
) {
67 struct Crypto1State
*s
, *t
;
69 uint64_t key
= 0; // recovered key
70 bool isSuccess
= false;
73 //PrintAndLog("Enter mfkey32_moebius");
74 uint64_t t1
= msclock();
76 s
= lfsr_recovery32(data
.ar
^ prng_successor(data
.nonce
, 64), 0);
78 for(t
= s
; t
->odd
| t
->even
; ++t
) {
79 lfsr_rollback_word(t
, 0, 0);
80 lfsr_rollback_word(t
, data
.nr
, 1);
81 lfsr_rollback_word(t
, data
.cuid
^ data
.nonce
, 0);
82 crypto1_get_lfsr(t
, &key
);
84 crypto1_word(t
, data
.cuid
^ data
.nonce2
, 0);
85 crypto1_word(t
, data
.nr2
, 1);
86 if (data
.ar2
== (crypto1_word(t
, 0, 0) ^ prng_successor(data
.nonce2
, 64))) {
87 //PrintAndLog("Found Key: [%012" PRIx64 "]",key);
94 isSuccess
= (counter
== 1);
96 // PrintAndLog("Time in mfkey32_moebius: %.1f seconds \nFound %d possible keys", (float)t1/1000.0, counter);
97 *outputkey
= ( isSuccess
) ? outkey
: 0;
99 /* // un-comment to output all keys to stats.txt
101 if ((fout = fopen("stats.txt","ab")) == NULL) {
102 PrintAndLog("Could not create file name stats.txt");
105 fprintf(fout, "moebius,%d,%08x,%d,%s,%04x%08x,%0.Lf\r\n", counter, data.cuid, data.sector, (data.keytype) ? "B" : "A", (uint32_t) (outkey>>32),(uint32_t)(outkey&0xFFFFFFFF),(long double)t1);
111 // recover key from reader response and tag response of one authentication sequence
112 int mfkey64(nonces_t data
, uint64_t *outputkey
){
113 uint64_t key
= 0; // recovered key
114 uint32_t ks2
; // keystream used to encrypt reader response
115 uint32_t ks3
; // keystream used to encrypt tag response
116 struct Crypto1State
*revstate
;
118 // PrintAndLog("Enter mfkey64");
119 uint64_t t1
= msclock();
121 // Extract the keystream from the messages
122 ks2
= data
.ar
^ prng_successor(data
.nonce
, 64);
123 ks3
= data
.at
^ prng_successor(data
.nonce
, 96);
124 revstate
= lfsr_recovery64(ks2
, ks3
);
125 lfsr_rollback_word(revstate
, 0, 0);
126 lfsr_rollback_word(revstate
, 0, 0);
127 lfsr_rollback_word(revstate
, data
.nr
, 1);
128 lfsr_rollback_word(revstate
, data
.cuid
^ data
.nonce
, 0);
129 crypto1_get_lfsr(revstate
, &key
);
130 // PrintAndLog("Found Key: [%012" PRIx64 "]", key);
131 crypto1_destroy(revstate
);
135 // PrintAndLog("Time in mfkey64: %.1f seconds \n", (float)t1/1000.0);