]> git.zerfleddert.de Git - proxmark3-svn/blame - tools/mfkey/mfkey64.c
Code cleanup:
[proxmark3-svn] / tools / mfkey / mfkey64.c
CommitLineData
cda056bb 1#define __STDC_FORMAT_MACROS
2#include <inttypes.h>
3#define llx PRIx64
4#define lli PRIi64
5
7847961b 6// Test-file: test2.c
7#include "crapto1.h"
8#include <stdio.h>
a7823834 9#include <string.h>
7847961b 10
11int main (int argc, char *argv[]) {
12 struct Crypto1State *revstate;
cda056bb 13 uint64_t key; // recovered key
7847961b 14 uint32_t uid; // serial number
15 uint32_t nt; // tag challenge
16 uint32_t nr_enc; // encrypted reader challenge
17 uint32_t ar_enc; // encrypted reader response
18 uint32_t at_enc; // encrypted tag response
998064f8 19 uint32_t ks2; // keystream used to encrypt reader response
20 uint32_t ks3; // keystream used to encrypt tag response
7847961b 21
cda056bb 22 printf("MIFARE Classic key recovery - based 64 bits of keystream\n");
23 printf("Recover key from only one complete authentication!\n\n");
7847961b 24
a7823834 25 if (argc < 6 ) {
8eea1dc2 26 printf(" syntax: %s <uid> <nt> <{nr}> <{ar}> <{at}> [enc] [enc...]\n\n", argv[0]);
7847961b 27 return 1;
28 }
29
a7823834
PZ
30 int encc = argc - 6;
31 int enclen[encc];
32 uint8_t enc[encc][120];
7847961b 33
a7823834
PZ
34 sscanf(argv[1], "%x", &uid);
35 sscanf(argv[2], "%x", &nt);
36 sscanf(argv[3], "%x", &nr_enc);
37 sscanf(argv[4], "%x", &ar_enc);
38 sscanf(argv[5], "%x", &at_enc);
39 for (int i = 0; i < encc; i++) {
40 enclen[i] = strlen(argv[i + 6]) / 2;
41 for (int i2 = 0; i2 < enclen[i]; i2++) {
42 sscanf(argv[i+6] + i2*2,"%2x", (uint8_t*)&enc[i][i2]);
43 }
44 }
7847961b 45 printf("Recovering key for:\n");
7847961b 46
a7823834
PZ
47 printf(" uid: %08x\n", uid);
48 printf(" nt: %08x\n", nt);
49 printf(" {nr}: %08x\n", nr_enc);
50 printf(" {ar}: %08x\n", ar_enc);
51 printf(" {at}: %08x\n", at_enc);
52 for (int i = 0; i < encc; i++) {
53 printf("{enc%d}: ", i);
54 for (int i2 = 0; i2 < enclen[i]; i2++) {
55 printf("%02x", enc[i][i2]);
56 }
57 printf("\n");
58 }
59
60
61 /*
7847961b 62 uint32_t uid = 0x9c599b32;
63 uint32_t tag_challenge = 0x82a4166c;
64 uint32_t nr_enc = 0xa1e458ce;
65 uint32_t reader_response = 0x6eea41e0;
66 uint32_t tag_response = 0x5cadf439;
67*/
a7823834 68 // Generate lfsr succesors of the tag challenge
7847961b 69 printf("\nLFSR succesors of the tag challenge:\n");
70 printf(" nt': %08x\n",prng_successor(nt, 64));
71 printf(" nt'': %08x\n",prng_successor(nt, 96));
72
73 // Extract the keystream from the messages
74 printf("\nKeystream used to generate {ar} and {at}:\n");
998064f8 75 ks2 = ar_enc ^ prng_successor(nt, 64);
7847961b 76 ks3 = at_enc ^ prng_successor(nt, 96);
77 printf(" ks2: %08x\n",ks2);
78 printf(" ks3: %08x\n",ks3);
79
80 revstate = lfsr_recovery64(ks2, ks3);
a7823834
PZ
81
82 // Decrypting communication using keystream if presented
83 if (argc > 6 ) {
84 printf("\nDecrypted communication:\n");
85 uint8_t ks4;
86 int rollb = 0;
87 for (int i = 0; i < encc; i++) {
88 printf("{dec%d}: ", i);
89 for (int i2 = 0; i2 < enclen[i]; i2++) {
90 ks4 = crypto1_byte(revstate, 0, 0);
91 printf("%02x", ks4 ^ enc[i][i2]);
92 rollb += 1;
93 }
94 printf("\n");
95 }
96 for (int i = 0; i < rollb; i++) {
97 lfsr_rollback_byte(revstate, 0, 0);
98 }
99 }
100
7847961b 101 lfsr_rollback_word(revstate, 0, 0);
102 lfsr_rollback_word(revstate, 0, 0);
103 lfsr_rollback_word(revstate, nr_enc, 1);
104 lfsr_rollback_word(revstate, uid ^ nt, 0);
cda056bb 105 crypto1_get_lfsr(revstate, &key);
106 printf("\nFound Key: [%012"llx"]\n\n",key);
b62328cc 107 crypto1_destroy(revstate);
7847961b 108
109 return 0;
8eea1dc2 110}
Impressum, Datenschutz