]>
Commit | Line | Data |
---|---|---|
cda056bb | 1 | #define __STDC_FORMAT_MACROS |
7847961b | 2 | #include <stdio.h> |
77dee16f | 3 | #include <string.h> |
838c15a6 | 4 | #include <time.h> |
77dee16f | 5 | #include <inttypes.h> |
6 | #include "crapto1.h" | |
838c15a6 | 7 | |
7847961b | 8 | int main (int argc, char *argv[]) { |
838c15a6 | 9 | struct Crypto1State *revstate; |
10 | uint64_t key; // recovered key | |
11 | uint32_t uid; // serial number | |
12 | uint32_t nt; // tag challenge | |
13 | uint32_t nr_enc; // encrypted reader challenge | |
14 | uint32_t ar_enc; // encrypted reader response | |
15 | uint32_t at_enc; // encrypted tag response | |
16 | uint32_t ks2; // keystream used to encrypt reader response | |
17 | uint32_t ks3; // keystream used to encrypt tag response | |
7847961b | 18 | |
838c15a6 | 19 | printf("MIFARE Classic key recovery - based 64 bits of keystream\n"); |
20 | printf("Recover key from only one complete authentication!\n\n"); | |
7847961b | 21 | |
838c15a6 | 22 | if (argc < 6) { |
a81b99b9 | 23 | printf(" syntax: %s <uid> <nt> <{nr}> <{ar}> <{at}> [enc...]\n\n", argv[0]); |
838c15a6 | 24 | return 1; |
25 | } | |
7847961b | 26 | |
a81b99b9 | 27 | int encc = argc - 6; |
28 | int enclen[encc]; | |
29 | uint8_t enc[encc][120]; | |
30 | ||
838c15a6 | 31 | sscanf(argv[1],"%x",&uid); |
32 | sscanf(argv[2],"%x",&nt); | |
33 | sscanf(argv[3],"%x",&nr_enc); | |
34 | sscanf(argv[4],"%x",&ar_enc); | |
35 | sscanf(argv[5],"%x",&at_enc); | |
a81b99b9 | 36 | for (int i = 0; i < encc; i++) { |
37 | enclen[i] = strlen(argv[i + 6]) / 2; | |
38 | for (int i2 = 0; i2 < enclen[i]; i2++) { | |
77dee16f | 39 | sscanf(argv[i+6] + i2*2, "%2x", (unsigned int *)&enc[i][i2]); |
a81b99b9 | 40 | } |
41 | } | |
42 | ||
838c15a6 | 43 | printf("Recovering key for:\n"); |
a81b99b9 | 44 | |
838c15a6 | 45 | printf(" uid: %08x\n",uid); |
46 | printf(" nt: %08x\n",nt); | |
47 | printf(" {nr}: %08x\n",nr_enc); | |
48 | printf(" {ar}: %08x\n",ar_enc); | |
49 | printf(" {at}: %08x\n",at_enc); | |
7847961b | 50 | |
a81b99b9 | 51 | for (int i = 0; i < encc; i++) { |
52 | printf("{enc%d}: ", i); | |
53 | for (int i2 = 0; i2 < enclen[i]; i2++) { | |
54 | printf("%02x", enc[i][i2]); | |
55 | } | |
56 | printf("\n"); | |
57 | } | |
58 | ||
7847961b | 59 | // Generate lfsr succesors of the tag challenge |
838c15a6 | 60 | printf("\nLFSR succesors of the tag challenge:\n"); |
61 | printf(" nt': %08x\n",prng_successor(nt, 64)); | |
62 | printf(" nt'': %08x\n",prng_successor(nt, 96)); | |
63 | ||
64 | clock_t t1 = clock(); | |
65 | ||
66 | // Extract the keystream from the messages | |
67 | printf("\nKeystream used to generate {ar} and {at}:\n"); | |
68 | ks2 = ar_enc ^ prng_successor(nt, 64); | |
69 | ks3 = at_enc ^ prng_successor(nt, 96); | |
70 | printf(" ks2: %08x\n",ks2); | |
71 | printf(" ks3: %08x\n",ks3); | |
7847961b | 72 | |
838c15a6 | 73 | revstate = lfsr_recovery64(ks2, ks3); |
a81b99b9 | 74 | |
75 | // Decrypting communication using keystream if presented | |
76 | if (argc > 6 ) { | |
77 | printf("\nDecrypted communication:\n"); | |
78 | uint8_t ks4; | |
79 | int rollb = 0; | |
80 | for (int i = 0; i < encc; i++) { | |
81 | printf("{dec%d}: ", i); | |
82 | for (int i2 = 0; i2 < enclen[i]; i2++) { | |
83 | ks4 = crypto1_byte(revstate, 0, 0); | |
84 | printf("%02x", ks4 ^ enc[i][i2]); | |
85 | rollb += 1; | |
86 | } | |
87 | printf("\n"); | |
88 | } | |
89 | for (int i = 0; i < rollb; i++) | |
90 | lfsr_rollback_byte(revstate, 0, 0); | |
91 | } | |
92 | ||
838c15a6 | 93 | lfsr_rollback_word(revstate, 0, 0); |
94 | lfsr_rollback_word(revstate, 0, 0); | |
95 | lfsr_rollback_word(revstate, nr_enc, 1); | |
96 | lfsr_rollback_word(revstate, uid ^ nt, 0); | |
97 | crypto1_get_lfsr(revstate, &key); | |
9c624f67 | 98 | printf("\nFound Key: [%012" PRIx64 "]\n\n", key); |
838c15a6 | 99 | crypto1_destroy(revstate); |
100 | ||
101 | t1 = clock() - t1; | |
102 | if ( t1 > 0 ) printf("Time : %.0f ticks \n", (float)t1); | |
103 | return 0; | |
7847961b | 104 | } |