Merge pull request #2593 from Akury83/master
[RRG-proxmark3.git] / tools / mfc / card_only / nonce2key.c
bloba3cc145e86127d363f2d6751069aaa73b37e1019
1 #include "crapto1/crapto1.h"
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <stdio.h>
6 int main(const int argc, const char *argv[]) {
7 struct Crypto1State *state;
8 uint32_t pos, uid, nt, nr, rr;
9 uint8_t ks3x[8], par[8][8];
10 uint64_t key_recovered;
11 uint64_t par_info;
12 uint64_t ks_info;
13 nr = rr = 0;
15 if (argc < 5) {
16 printf("\nsyntax: %s <uid> <nt> <par> <ks>\n", argv[0]);
17 printf("example: %s 92c0456b 73294ab7 a3fbfb537343eb7b 070608090e060a02\n\n", argv[0]);
18 return 1;
20 sscanf(argv[1], "%08x", &uid);
21 sscanf(argv[2], "%08x", &nt);
22 sscanf(argv[3], "%016" SCNx64, &par_info);
23 sscanf(argv[4], "%016" SCNx64, &ks_info);
25 // Reset the last three significant bits of the reader nonce
26 nr &= 0xffffff1f;
28 printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n", uid, nt, par_info, ks_info);
30 for (pos = 0; pos < 8; pos++) {
31 ks3x[7 - pos] = (ks_info >> (pos * 8)) & 0x0f;
32 uint8_t bt = (par_info >> (pos * 8)) & 0xff;
34 for (uint8_t i = 0; i < 8; i++) {
35 par[7 - pos][i] = (bt >> i) & 0x01;
39 printf("|diff|{nr} |ks3|ks3^5|parity |\n");
40 printf("+----+--------+---+-----+---------------+\n");
42 for (uint8_t i = 0; i < 8; i++) {
43 uint32_t nr_diff = nr | i << 5;
44 printf("| %02x |%08x| %01x | %01x |", i << 5, nr_diff, ks3x[i], ks3x[i] ^ 5);
46 for (pos = 0; pos < 7; pos++)
47 printf("%01x,", par[i][pos]);
48 printf("%01x|\n", par[i][7]);
50 printf("+----+--------+---+-----+---------------+\n");
52 state = lfsr_common_prefix(nr, rr, ks3x, par, false);
53 lfsr_rollback_word(state, uid ^ nt, 0);
54 crypto1_get_lfsr(state, &key_recovered);
55 printf("\nkey recovered: %012" PRIx64 "\n\n", key_recovered);
56 crypto1_destroy(state);
57 return 0;