Makefile: display firmware size
[RRG-proxmark3.git] / tools / cryptorf / cm.c
blobbe7b79f5440bd59ae4be327bd93710db82f3d5a8
1 /*
3 * CryptoMemory simulation
5 * Copyright (C) 2010, Flavio D. Garcia, Peter van Rossum, Roel Verdult
6 * and Ronny Wichers Schreur. Radboud University Nijmegen
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <inttypes.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include "cryptolib.h"
27 #include "util.h"
28 #ifdef _MSC_VER
29 // avoid scanf warnings in Visual Studio
30 #define _CRT_SECURE_NO_WARNINGS
31 #define _CRT_SECURE_NO_DEPRECATE
32 #endif
34 int main(int argc, const char *argv[]) {
35 // Cryptomemory state
36 crypto_state_t s;
38 // Main authentication values
39 uint8_t Q[8]; // Reader key-auth random
40 uint8_t Gc[8]; // Secret seed
41 uint8_t Ci[8]; // Card random (last state)
42 uint8_t Ch[8]; // Reader answer (challenge)
43 uint8_t Ci_1[8]; // Card answer
44 uint8_t Ci_2[8]; // Session key
46 // Session authentication values
47 uint8_t Qs[8]; // Reader session-auth random
48 uint8_t Chs[8]; // Reader session-answer (challenge)
49 uint8_t Ci_1s[8]; // Card answer for session
50 uint8_t Ci_2s[8]; // Is this used?
52 // Various argument options
53 uint64_t nGc; // Card secret
54 uint64_t nCi; // Card random
55 uint64_t nQ; // Reader main-random
56 uint64_t nQs; // Reader session-random
58 // Show header and help syntax
59 printf("CryptoMemory simulator - (c) Radboud University Nijmegen\n");
60 if (argc < 5) {
61 printf("\nsyntax: cm <Gc> <Ci> <Q> <Q(s)>\n");
62 return 1;
65 // Parse arguments
66 sscanf(argv[1], "%016" SCNx64, &nGc);
67 num_to_bytes(nGc, 8, Gc);
68 sscanf(argv[2], "%016" SCNx64, &nCi);
69 num_to_bytes(nCi, 8, Ci);
70 sscanf(argv[3], "%016" SCNx64, &nQ);
71 num_to_bytes(nQ, 8, Q);
72 sscanf(argv[4], "%016" SCNx64, &nQs);
73 num_to_bytes(nQs, 8, Qs);
75 // Calculate authentication
76 cm_auth(Gc, Ci, Q, Ch, Ci_1, Ci_2, &s);
78 printf("\nAuthenticate\n");
79 printf(" Gc: ");
80 print_bytes(Gc, 8);
81 printf(" Ci: ");
82 print_bytes(Ci, 8);
83 printf(" Q: ");
84 print_bytes(Q, 8);
85 printf(" Ch: ");
86 print_bytes(Ch, 8);
87 printf(" Ci+1: ");
88 print_bytes(Ci_1, 8);
89 printf(" Ci+2: ");
90 print_bytes(Ci_2, 8);
92 cm_auth(Ci_2, Ci_1, Qs, Chs, Ci_1s, Ci_2s, &s);
94 printf("\nVerify Crypto (Session Key)\n");
95 printf(" Gc(s): ");
96 print_bytes(Ci_2, 8);
97 printf(" Ci(s): ");
98 print_bytes(Ci_1, 8);
99 printf(" Q(s): ");
100 print_bytes(Qs, 8);
101 printf(" Ch(s): ");
102 print_bytes(Chs, 8);
103 printf("Ci+1(s): ");
104 print_bytes(Ci_1s, 8);
105 printf("Ci+2(s): ");
106 print_bytes(Ci_2s, 8);
108 printf("\n");
109 return 0;