Merge pull request #2664 from piotrva/hf-mf-ultimatecard-script-max-rw-blocks
[RRG-proxmark3.git] / client / src / emv / emv_roca.c
blob23300b410c4d190a01916d64834b301c9faa2f4d
1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://gist.github.com/robstradling/f525d423c79690b72e650e2ad38a161d
3 // Copyright (C) 2017-2018 Rob Stradling
4 // Copyright (C) 2017-2018 Sectigo Limited
5 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // See LICENSE.txt for the text of the license.
18 //-----------------------------------------------------------------------------
19 // roca.c - ROCA (CVE-2017-15361) fingerprint checker.
20 //-----------------------------------------------------------------------------
22 #include "emv_roca.h"
24 #include "ui.h" // Print...
25 #include "bignum.h"
27 static void rocacheck_init(mbedtls_mpi *prints) {
29 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++)
30 mbedtls_mpi_init(&prints[i]);
32 mbedtls_mpi_read_string(&prints[0], 10, "1026");
33 mbedtls_mpi_read_string(&prints[1], 10, "5658");
34 mbedtls_mpi_read_string(&prints[2], 10, "107286");
35 mbedtls_mpi_read_string(&prints[3], 10, "199410");
36 mbedtls_mpi_read_string(&prints[4], 10, "67109890");
37 mbedtls_mpi_read_string(&prints[5], 10, "5310023542746834");
38 mbedtls_mpi_read_string(&prints[6], 10, "1455791217086302986");
39 mbedtls_mpi_read_string(&prints[7], 10, "20052041432995567486");
40 mbedtls_mpi_read_string(&prints[8], 10, "6041388139249378920330");
41 mbedtls_mpi_read_string(&prints[9], 10, "207530445072488465666");
42 mbedtls_mpi_read_string(&prints[10], 10, "79228162521181866724264247298");
43 mbedtls_mpi_read_string(&prints[11], 10, "1760368345969468176824550810518");
44 mbedtls_mpi_read_string(&prints[12], 10, "50079290986288516948354744811034");
45 mbedtls_mpi_read_string(&prints[13], 10, "473022961816146413042658758988474");
46 mbedtls_mpi_read_string(&prints[14], 10, "144390480366845522447407333004847678774");
47 mbedtls_mpi_read_string(&prints[15], 10, "1800793591454480341970779146165214289059119882");
48 mbedtls_mpi_read_string(&prints[16], 10, "126304807362733370595828809000324029340048915994");
51 static void rocacheck_cleanup(mbedtls_mpi *prints) {
52 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++)
53 mbedtls_mpi_free(&prints[i]);
56 static int bitand_is_zero(mbedtls_mpi *a, mbedtls_mpi *b) {
58 for (int i = 0; i < mbedtls_mpi_bitlen(a); i++) {
60 if (mbedtls_mpi_get_bit(a, i) && mbedtls_mpi_get_bit(b, i))
61 return 0;
63 return 1;
67 static mbedtls_mpi_uint mpi_get_uint(const mbedtls_mpi *X) {
69 if (X->n == 1 && X->s > 0) {
70 return X->p[0];
73 PrintAndLogEx(WARNING, "ZERRRRO!!!\n");
74 return 0;
78 static void print_mpi(const char *msg, int radix, const mbedtls_mpi *X) {
80 char Xchar[400] = {0};
81 size_t len = 0;
83 mbedtls_mpi_write_string(X, radix, Xchar, sizeof(Xchar), &len);
84 PrintAndLogEx(INFO, "%s[%zu] %s\n", msg, len, Xchar);
87 bool emv_rocacheck(const unsigned char *buf, size_t buflen, bool verbose) {
89 mbedtls_mpi t_modulus;
90 mbedtls_mpi_init(&t_modulus);
92 bool ret = false;
93 mbedtls_mpi prints[ROCA_PRINTS_LENGTH];
94 uint8_t primes[ROCA_PRINTS_LENGTH] = {
95 11, 13, 17, 19, 37, 53, 61, 71, 73, 79, 97, 103, 107, 109, 127, 151, 157
98 rocacheck_init(prints);
100 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&t_modulus, buf, buflen));
102 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++) {
104 mbedtls_mpi t_temp;
105 mbedtls_mpi t_prime;
106 mbedtls_mpi g_one;
108 mbedtls_mpi_init(&t_temp);
109 mbedtls_mpi_init(&t_prime);
110 mbedtls_mpi_init(&g_one);
112 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&g_one, 10, "1"));
114 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&t_prime, &t_prime, primes[i]));
116 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&t_temp, &t_modulus, &t_prime));
118 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&g_one, mpi_get_uint(&t_temp)));
120 mbedtls_mpi_free(&t_temp);
121 mbedtls_mpi_free(&t_prime);
123 if (bitand_is_zero(&g_one, &prints[i])) {
124 if (verbose) {
125 PrintAndLogEx(FAILED, "No fingerprint found.\n");
127 mbedtls_mpi_free(&g_one);
128 goto cleanup;
130 mbedtls_mpi_free(&g_one);
133 ret = true;
134 if (verbose)
135 PrintAndLogEx(SUCCESS, "Fingerprint found!\n");
137 cleanup:
138 mbedtls_mpi_free(&t_modulus);
140 rocacheck_cleanup(prints);
141 return ret;
144 int roca_self_test(void) {
145 PrintAndLogEx(NORMAL, "");
146 PrintAndLogEx(INFO, "ROCA check vulnerability tests");
148 // positive
149 uint8_t keyp[] = "\x94\x4e\x13\x20\x8a\x28\x0c\x37\xef\xc3\x1c\x31\x14\x48\x5e\x59"\
150 "\x01\x92\xad\xbb\x8e\x11\xc8\x7c\xad\x60\xcd\xef\x00\x37\xce\x99"\
151 "\x27\x83\x30\xd3\xf4\x71\xa2\x53\x8f\xa6\x67\x80\x2e\xd2\xa3\xc4"\
152 "\x4a\x8b\x7d\xea\x82\x6e\x88\x8d\x0a\xa3\x41\xfd\x66\x4f\x7f\xa7";
154 int ret = 0;
155 if (emv_rocacheck(keyp, 64, false)) {
156 PrintAndLogEx(SUCCESS, "Weak modulus ( %s )", _GREEN_("ok"));
157 } else {
158 ret++;
159 PrintAndLogEx(FAILED, "Weak modulus ( %s )", _RED_("fail"));
162 // negative
163 uint8_t keyn[] = "\x84\x4e\x13\x20\x8a\x28\x0c\x37\xef\xc3\x1c\x31\x14\x48\x5e\x59"\
164 "\x01\x92\xad\xbb\x8e\x11\xc8\x7c\xad\x60\xcd\xef\x00\x37\xce\x99"\
165 "\x27\x83\x30\xd3\xf4\x71\xa2\x53\x8f\xa6\x67\x80\x2e\xd2\xa3\xc4"\
166 "\x4a\x8b\x7d\xea\x82\x6e\x88\x8d\x0a\xa3\x41\xfd\x66\x4f\x7f\xa7";
168 if (emv_rocacheck(keyn, 64, false)) {
169 ret++;
170 PrintAndLogEx(FAILED, "Strong modulus ( %s )", _RED_("fail"));
171 } else {
172 PrintAndLogEx(SUCCESS, "Strong modulus ( %s )", _GREEN_("ok"));
174 return ret;