hf mf fchk - output style
[RRG-proxmark3.git] / tools / nonce2key / nonce2key.c
blobd7ccec0bb5201bd332b548adba2aa9237ebff87e
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\n", argv[0]);
17 return 1;
19 sscanf(argv[1], "%08x", &uid);
20 sscanf(argv[2], "%08x", &nt);
21 sscanf(argv[3], "%016" SCNx64, &par_info);
22 sscanf(argv[4], "%016" SCNx64, &ks_info);
24 // Reset the last three significant bits of the reader nonce
25 nr &= 0xffffff1f;
27 printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n", uid, nt, par_info, ks_info);
29 for (pos = 0; pos < 8; pos++) {
30 ks3x[7 - pos] = (ks_info >> (pos * 8)) & 0x0f;
31 uint8_t bt = (par_info >> (pos * 8)) & 0xff;
33 for (uint8_t i = 0; i < 8; i++) {
34 par[7 - pos][i] = (bt >> i) & 0x01;
38 printf("|diff|{nr} |ks3|ks3^5|parity |\n");
39 printf("+----+--------+---+-----+---------------+\n");
41 for (uint8_t i = 0; i < 8; i++) {
42 uint32_t nr_diff = nr | i << 5;
43 printf("| %02x |%08x| %01x | %01x |", i << 5, nr_diff, ks3x[i], ks3x[i] ^ 5);
45 for (pos = 0; pos < 7; pos++)
46 printf("%01x,", par[i][pos]);
47 printf("%01x|\n", par[i][7]);
49 printf("+----+--------+---+-----+---------------+\n");
51 state = lfsr_common_prefix(nr, rr, ks3x, par, false);
52 lfsr_rollback_word(state, uid ^ nt, 0);
53 crypto1_get_lfsr(state, &key_recovered);
54 printf("\nkey recovered: %012" PRIx64 "\n\n", key_recovered);
55 crypto1_destroy(state);
56 return 0;