1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://github.com/lumag/emv-tools/
3 // Copyright (C) 2012, 2015 Dmitry Eremin-Solenikov
4 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // See LICENSE.txt for the text of the license.
17 //-----------------------------------------------------------------------------
18 // libopenemv - a library to work with EMV family of smart cards
19 //-----------------------------------------------------------------------------
25 #include "crypto_backend.h"
27 #include <stdlib.h> // malloc
28 #include <string.h> // memset
32 #include "ui.h" // printandlog
34 struct crypto_hash_polarssl
{
35 struct crypto_hash ch
;
36 mbedtls_sha1_context ctx
;
39 static void crypto_hash_polarssl_close(struct crypto_hash
*_ch
) {
40 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
44 static void crypto_hash_polarssl_write(struct crypto_hash
*_ch
, const unsigned char *buf
, size_t len
) {
45 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
46 mbedtls_sha1_update(&(ch
->ctx
), buf
, len
);
49 static unsigned char *crypto_hash_polarssl_read(struct crypto_hash
*_ch
) {
50 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
51 static unsigned char sha1sum
[20];
52 mbedtls_sha1_finish(&(ch
->ctx
), sha1sum
);
56 static size_t crypto_hash_polarssl_get_size(const struct crypto_hash
*ch
) {
57 if (ch
->algo
== HASH_SHA_1
)
62 static struct crypto_hash
*crypto_hash_polarssl_open(enum crypto_algo_hash hash
) {
63 if (hash
!= HASH_SHA_1
)
66 struct crypto_hash_polarssl
*ch
= calloc(1, sizeof(*ch
));
68 mbedtls_sha1_starts(&(ch
->ctx
));
70 ch
->ch
.write
= crypto_hash_polarssl_write
;
71 ch
->ch
.read
= crypto_hash_polarssl_read
;
72 ch
->ch
.close
= crypto_hash_polarssl_close
;
73 ch
->ch
.get_size
= crypto_hash_polarssl_get_size
;
78 struct crypto_pk_polarssl
{
80 mbedtls_rsa_context ctx
;
83 static struct crypto_pk
*crypto_pk_polarssl_open_rsa(va_list vl
) {
84 struct crypto_pk_polarssl
*cp
= calloc(1, sizeof(*cp
));
85 memset(cp
, 0x00, sizeof(*cp
));
87 char *mod
= va_arg(vl
, char *); // N
88 int modlen
= va_arg(vl
, size_t);
89 char *exp
= va_arg(vl
, char *); // E
90 int explen
= va_arg(vl
, size_t);
92 mbedtls_rsa_init(&cp
->ctx
, MBEDTLS_RSA_PKCS_V15
, 0);
94 cp
->ctx
.len
= modlen
; // size(N) in bytes
95 mbedtls_mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
96 mbedtls_mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
98 int res
= mbedtls_rsa_check_pubkey(&cp
->ctx
);
100 PrintAndLogEx(WARNING
, "PolarSSL public key error res=%x exp=%d mod=%d", res
* -1, explen
, modlen
);
108 static struct crypto_pk
*crypto_pk_polarssl_open_priv_rsa(va_list vl
) {
109 struct crypto_pk_polarssl
*cp
= calloc(1, sizeof(*cp
));
110 memset(cp
, 0x00, sizeof(*cp
));
111 char *mod
= va_arg(vl
, char *);
112 int modlen
= va_arg(vl
, size_t);
113 char *exp
= va_arg(vl
, char *);
114 int explen
= va_arg(vl
, size_t);
115 char *d
= va_arg(vl
, char *);
116 int dlen
= va_arg(vl
, size_t);
117 char *p
= va_arg(vl
, char *);
118 int plen
= va_arg(vl
, size_t);
119 char *q
= va_arg(vl
, char *);
120 int qlen
= va_arg(vl
, size_t);
121 char *dp
= va_arg(vl
, char *);
122 int dplen
= va_arg(vl
, size_t);
123 char *dq
= va_arg(vl
, char *);
124 int dqlen
= va_arg(vl
, size_t);
125 // calc QP via Q and P
126 // char *inv = va_arg(vl, char *);
127 // int invlen = va_arg(vl, size_t);
129 mbedtls_rsa_init(&cp
->ctx
, MBEDTLS_RSA_PKCS_V15
, 0);
131 cp
->ctx
.len
= modlen
; // size(N) in bytes
132 mbedtls_mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
133 mbedtls_mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
135 mbedtls_mpi_read_binary(&cp
->ctx
.D
, (const unsigned char *)d
, dlen
);
136 mbedtls_mpi_read_binary(&cp
->ctx
.P
, (const unsigned char *)p
, plen
);
137 mbedtls_mpi_read_binary(&cp
->ctx
.Q
, (const unsigned char *)q
, qlen
);
138 mbedtls_mpi_read_binary(&cp
->ctx
.DP
, (const unsigned char *)dp
, dplen
);
139 mbedtls_mpi_read_binary(&cp
->ctx
.DQ
, (const unsigned char *)dq
, dqlen
);
141 int res
= mbedtls_mpi_inv_mod(&cp
->ctx
.QP
, &cp
->ctx
.Q
, &cp
->ctx
.P
);
143 PrintAndLogEx(WARNING
, "PolarSSL private key error res=%x exp=%d mod=%d", res
* -1, explen
, modlen
);
148 res
= mbedtls_rsa_check_privkey(&cp
->ctx
);
150 PrintAndLogEx(WARNING
, "PolarSSL private key error res=%x exp=%d mod=%d", res
* -1, explen
, modlen
);
158 static int myrand(void *rng_state
, unsigned char *output
, size_t len
) {
162 for (i
= 0; i
< len
; ++i
)
168 static struct crypto_pk
*crypto_pk_polarssl_genkey_rsa(va_list vl
) {
169 struct crypto_pk_polarssl
*cp
= calloc(1, sizeof(*cp
));
170 memset(cp
, 0x00, sizeof(*cp
));
172 int transient
= va_arg(vl
, int);
173 unsigned int nbits
= va_arg(vl
, unsigned int);
174 unsigned int exp
= va_arg(vl
, unsigned int);
179 int res
= mbedtls_rsa_gen_key(&cp
->ctx
, &myrand
, NULL
, nbits
, exp
);
181 PrintAndLogEx(WARNING
, "PolarSSL private key generation error res=%x exp=%u nbits=%u", res
* -1, exp
, nbits
);
189 static void crypto_pk_polarssl_close(struct crypto_pk
*_cp
) {
190 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
192 mbedtls_rsa_free(&cp
->ctx
);
196 static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
) {
197 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
199 size_t keylen
= mbedtls_mpi_size(&cp
->ctx
.N
);
201 unsigned char *result
= calloc(1, keylen
);
203 PrintAndLogEx(WARNING
, "RSA encrypt failed. Can't allocate result memory");
207 int res
= mbedtls_rsa_public(&cp
->ctx
, buf
, result
);
209 PrintAndLogEx(WARNING
, "RSA encrypt failed. Error: %x data len: %zu key len: %zu", res
* -1, len
, keylen
);
218 static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
) {
219 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
221 size_t keylen
= mbedtls_mpi_size(&cp
->ctx
.N
);
223 unsigned char *result
= calloc(1, keylen
);
225 PrintAndLogEx(WARNING
, "RSA encrypt failed. Can't allocate result memory");
229 int res
= mbedtls_rsa_private(&cp
->ctx
, NULL
, NULL
, buf
, result
); // CHECK???
231 PrintAndLogEx(WARNING
, "RSA decrypt failed. Error: %x data len: %zu key len: %zu", res
* -1, len
, keylen
);
240 static size_t crypto_pk_polarssl_get_nbits(const struct crypto_pk
*_cp
) {
241 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
242 return cp
->ctx
.len
* 8;
245 static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk
*_cp
, unsigned param
, size_t *plen
) {
246 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
247 unsigned char *result
= NULL
;
252 *plen
= mbedtls_mpi_size(&cp
->ctx
.N
);
253 result
= calloc(1, *plen
);
254 memset(result
, 0x00, *plen
);
255 res
= mbedtls_mpi_write_binary(&cp
->ctx
.N
, result
, *plen
);
257 PrintAndLogEx(WARNING
, "Error write_binary");
264 *plen
= mbedtls_mpi_size(&cp
->ctx
.E
);
265 result
= calloc(1, *plen
);
266 memset(result
, 0x00, *plen
);
267 res
= mbedtls_mpi_write_binary(&cp
->ctx
.E
, result
, *plen
);
269 PrintAndLogEx(WARNING
, "Error write_binary");
275 PrintAndLogEx(WARNING
, "Error get parameter. Param = %u", param
);
281 static struct crypto_pk
*crypto_pk_polarssl_open(enum crypto_algo_pk pk
, va_list vl
) {
282 struct crypto_pk
*cp
;
285 cp
= crypto_pk_polarssl_open_rsa(vl
);
289 cp
->close
= crypto_pk_polarssl_close
;
290 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
291 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
292 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
297 static struct crypto_pk
*crypto_pk_polarssl_open_priv(enum crypto_algo_pk pk
, va_list vl
) {
298 struct crypto_pk
*cp
;
301 cp
= crypto_pk_polarssl_open_priv_rsa(vl
);
305 cp
->close
= crypto_pk_polarssl_close
;
306 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
307 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
308 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
309 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
314 static struct crypto_pk
*crypto_pk_polarssl_genkey(enum crypto_algo_pk pk
, va_list vl
) {
315 struct crypto_pk
*cp
;
318 cp
= crypto_pk_polarssl_genkey_rsa(vl
);
322 cp
->close
= crypto_pk_polarssl_close
;
323 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
324 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
325 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
326 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
331 static struct crypto_backend crypto_polarssl_backend
= {
332 .hash_open
= crypto_hash_polarssl_open
,
333 .pk_open
= crypto_pk_polarssl_open
,
334 .pk_open_priv
= crypto_pk_polarssl_open_priv
,
335 .pk_genkey
= crypto_pk_polarssl_genkey
,
338 struct crypto_backend
*crypto_polarssl_init(void) {
339 return &crypto_polarssl_backend
;