]> git.zerfleddert.de Git - proxmark3-svn/blame - client/emv/emv_roca.c
Add ROCA vulnerability test (RRG repository PR 76 by @merlokk) (#762)
[proxmark3-svn] / client / emv / emv_roca.c
CommitLineData
1511ea28 1/* roca.c - ROCA (CVE-2017-15361) fingerprint checker.
2 * Written by Rob Stradling (based on https://github.com/crocs-muni/roca/blob/master/roca/detect.py)
3 * Copyright (C) 2017-2018 Sectigo Limited
4 * modified 2018 iceman (dropped openssl bignum, now use mbedtls lib)
5 * modified 2018 merlok
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 2 of the License, or
10 * (at your option) any later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20//-----------------------------------------------------------------------------
21// EMV roca commands
22//-----------------------------------------------------------------------------
23
24#include "emv_roca.h"
25
26#include <stddef.h>
27#include <stdint.h>
28#include <stdbool.h>
29
30#include "ui.h"
31#include "mbedtls/bignum.h"
32
33
34static uint8_t g_primes[ROCA_PRINTS_LENGTH] = {
35 11, 13, 17, 19, 37, 53, 61, 71, 73, 79, 97, 103, 107, 109, 127, 151, 157
36};
37
38mbedtls_mpi g_prints[ROCA_PRINTS_LENGTH];
39
40void rocacheck_init(void) {
41
42 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++)
43 mbedtls_mpi_init(&g_prints[i]);
44
45 mbedtls_mpi_read_string(&g_prints[0], 10, "1026");
46 mbedtls_mpi_read_string(&g_prints[1], 10, "5658");
47 mbedtls_mpi_read_string(&g_prints[2], 10, "107286");
48 mbedtls_mpi_read_string(&g_prints[3], 10, "199410");
49 mbedtls_mpi_read_string(&g_prints[4], 10, "67109890");
50 mbedtls_mpi_read_string(&g_prints[5], 10, "5310023542746834");
51 mbedtls_mpi_read_string(&g_prints[6], 10, "1455791217086302986");
52 mbedtls_mpi_read_string(&g_prints[7], 10, "20052041432995567486");
53 mbedtls_mpi_read_string(&g_prints[8], 10, "6041388139249378920330");
54 mbedtls_mpi_read_string(&g_prints[9], 10, "207530445072488465666");
55 mbedtls_mpi_read_string(&g_prints[10], 10, "79228162521181866724264247298");
56 mbedtls_mpi_read_string(&g_prints[11], 10, "1760368345969468176824550810518");
57 mbedtls_mpi_read_string(&g_prints[12], 10, "50079290986288516948354744811034");
58 mbedtls_mpi_read_string(&g_prints[13], 10, "473022961816146413042658758988474");
59 mbedtls_mpi_read_string(&g_prints[14], 10, "144390480366845522447407333004847678774");
60 mbedtls_mpi_read_string(&g_prints[15], 10, "1800793591454480341970779146165214289059119882");
61 mbedtls_mpi_read_string(&g_prints[16], 10, "126304807362733370595828809000324029340048915994");
62}
63
64void rocacheck_cleanup(void) {
65 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++)
66 mbedtls_mpi_free(&g_prints[i]);
67}
68
69int bitand_is_zero( mbedtls_mpi* a, mbedtls_mpi* b ) {
70
71 for (int i = 0; i < mbedtls_mpi_bitlen(a); i++) {
72
73 if (mbedtls_mpi_get_bit(a, i) && mbedtls_mpi_get_bit(b, i))
74 return 0;
75 }
76 return 1;
77}
78
79
80mbedtls_mpi_uint mpi_get_uint(const mbedtls_mpi *X) {
81
82 if (X->n == 1 && X->s > 0) {
83 return X->p[0];
84 }
85 printf("ZERRRRO!!!\n");
86 return 0;
87}
88
89void print_mpi(const char *msg, int radix, const mbedtls_mpi *X) {
90
91 char Xchar[400] = {0};
92 size_t len = 0;
93
94 mbedtls_mpi_write_string(X, radix, Xchar, sizeof(Xchar), &len);
95 printf("%s[%zd] %s\n", msg, len, Xchar);
96}
97
98bool emv_rocacheck(const unsigned char *buf, size_t buflen, bool verbose) {
99
100 mbedtls_mpi t_modulus;
101 mbedtls_mpi_init(&t_modulus);
102
103 bool ret = false;
104
105 rocacheck_init();
106
107 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary(&t_modulus, buf, buflen) );
108
109 for (int i = 0; i < ROCA_PRINTS_LENGTH; i++) {
110
111 mbedtls_mpi t_temp;
112 mbedtls_mpi t_prime;
113 mbedtls_mpi g_one;
114
115 mbedtls_mpi_init(&t_temp);
116 mbedtls_mpi_init(&t_prime);
117 mbedtls_mpi_init(&g_one);
118
119 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string(&g_one, 10, "1") );
120
121 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int(&t_prime, &t_prime, g_primes[i]) );
122
123 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi(&t_temp, &t_modulus, &t_prime) );
124
125 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l(&g_one, mpi_get_uint(&t_temp)) );
126
127 if (bitand_is_zero(&g_one, &g_prints[i])) {
128 if (verbose)
129 PrintAndLogEx(FAILED, "No fingerprint found.\n");
130 goto cleanup;
131 }
132
133 mbedtls_mpi_free(&g_one);
134 mbedtls_mpi_free(&t_temp);
135 mbedtls_mpi_free(&t_prime);
136 }
137
138 ret = true;
139 if (verbose)
140 PrintAndLogEx(SUCCESS, "Fingerprint found!\n");
141
142cleanup:
143 mbedtls_mpi_free(&t_modulus);
144
145 rocacheck_cleanup();
146 return ret;
147}
148
149int roca_self_test( int verbose ) {
150 int ret = 0;
151
152 if( verbose != 0 )
153 printf( "\nROCA check vulnerability tests\n" );
154
155 // positive
156 uint8_t keyp[] = "\x94\x4e\x13\x20\x8a\x28\x0c\x37\xef\xc3\x1c\x31\x14\x48\x5e\x59"\
157 "\x01\x92\xad\xbb\x8e\x11\xc8\x7c\xad\x60\xcd\xef\x00\x37\xce\x99"\
158 "\x27\x83\x30\xd3\xf4\x71\xa2\x53\x8f\xa6\x67\x80\x2e\xd2\xa3\xc4"\
159 "\x4a\x8b\x7d\xea\x82\x6e\x88\x8d\x0a\xa3\x41\xfd\x66\x4f\x7f\xa7";
160
161 if( verbose != 0 )
162 printf( " ROCA positive test: " );
163
164 if (emv_rocacheck(keyp, 64, false)) {
165 if( verbose != 0 )
166 printf( "passed\n" );
167 } else {
168 ret = 1;
169 if( verbose != 0 )
170 printf( "failed\n" );
171 }
172
173 // negative
174 uint8_t keyn[] = "\x84\x4e\x13\x20\x8a\x28\x0c\x37\xef\xc3\x1c\x31\x14\x48\x5e\x59"\
175 "\x01\x92\xad\xbb\x8e\x11\xc8\x7c\xad\x60\xcd\xef\x00\x37\xce\x99"\
176 "\x27\x83\x30\xd3\xf4\x71\xa2\x53\x8f\xa6\x67\x80\x2e\xd2\xa3\xc4"\
177 "\x4a\x8b\x7d\xea\x82\x6e\x88\x8d\x0a\xa3\x41\xfd\x66\x4f\x7f\xa7";
178
179 if( verbose != 0 )
180 printf( " ROCA negative test: " );
181
182 if (emv_rocacheck(keyn, 64, false)) {
183 ret = 1;
184 if( verbose != 0 )
185 printf( "failed\n" );
186 } else {
187 if( verbose != 0 )
188 printf( "passed\n" );
189 }
190
191
192 return ret;
193}
Impressum, Datenschutz