2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2015 Dmitry Eremin-Solenikov
4 * Copyright (C) 2017 Merlok
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
22 #include "crypto_backend.h"
29 #include "mbedtls/rsa.h"
30 #include "mbedtls/sha1.h"
32 struct crypto_hash_polarssl
{
33 struct crypto_hash ch
;
34 mbedtls_sha1_context ctx
;
37 static void crypto_hash_polarssl_close(struct crypto_hash
*_ch
)
39 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
)
46 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
48 mbedtls_sha1_update(&(ch
->ctx
), buf
, len
);
51 static unsigned char *crypto_hash_polarssl_read(struct crypto_hash
*_ch
)
53 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
55 static unsigned char sha1sum
[20];
56 mbedtls_sha1_finish(&(ch
->ctx
), sha1sum
);
60 static size_t crypto_hash_polarssl_get_size(const struct crypto_hash
*ch
)
62 if (ch
->algo
== HASH_SHA_1
)
68 static struct crypto_hash
*crypto_hash_polarssl_open(enum crypto_algo_hash hash
)
70 if (hash
!= HASH_SHA_1
)
73 struct crypto_hash_polarssl
*ch
= malloc(sizeof(*ch
));
75 mbedtls_sha1_starts(&(ch
->ctx
));
77 ch
->ch
.write
= crypto_hash_polarssl_write
;
78 ch
->ch
.read
= crypto_hash_polarssl_read
;
79 ch
->ch
.close
= crypto_hash_polarssl_close
;
80 ch
->ch
.get_size
= crypto_hash_polarssl_get_size
;
85 struct crypto_pk_polarssl
{
87 mbedtls_rsa_context ctx
;
90 static struct crypto_pk
*crypto_pk_polarssl_open_rsa(va_list vl
)
92 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
93 memset(cp
, 0x00, sizeof(*cp
));
95 char *mod
= va_arg(vl
, char *); // N
96 int modlen
= va_arg(vl
, size_t);
97 char *exp
= va_arg(vl
, char *); // E
98 int explen
= va_arg(vl
, size_t);
100 mbedtls_rsa_init(&cp
->ctx
, MBEDTLS_RSA_PKCS_V15
, 0);
102 cp
->ctx
.len
= modlen
; // size(N) in bytes
103 mbedtls_mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
104 mbedtls_mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
106 int res
= mbedtls_rsa_check_pubkey(&cp
->ctx
);
108 fprintf(stderr
, "PolarSSL public key error res=%x exp=%d mod=%d.\n", res
* -1, explen
, modlen
);
116 static struct crypto_pk
*crypto_pk_polarssl_open_priv_rsa(va_list vl
)
118 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
119 memset(cp
, 0x00, sizeof(*cp
));
120 char *mod
= va_arg(vl
, char *);
121 int modlen
= va_arg(vl
, size_t);
122 char *exp
= va_arg(vl
, char *);
123 int explen
= va_arg(vl
, size_t);
124 char *d
= va_arg(vl
, char *);
125 int dlen
= va_arg(vl
, size_t);
126 char *p
= va_arg(vl
, char *);
127 int plen
= va_arg(vl
, size_t);
128 char *q
= va_arg(vl
, char *);
129 int qlen
= va_arg(vl
, size_t);
130 char *dp
= va_arg(vl
, char *);
131 int dplen
= va_arg(vl
, size_t);
132 char *dq
= va_arg(vl
, char *);
133 int dqlen
= va_arg(vl
, size_t);
134 // calc QP via Q and P
135 // char *inv = va_arg(vl, char *);
136 // int invlen = va_arg(vl, size_t);
138 mbedtls_rsa_init(&cp
->ctx
, MBEDTLS_RSA_PKCS_V15
, 0);
140 cp
->ctx
.len
= modlen
; // size(N) in bytes
141 mbedtls_mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
142 mbedtls_mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
144 mbedtls_mpi_read_binary(&cp
->ctx
.D
, (const unsigned char *)d
, dlen
);
145 mbedtls_mpi_read_binary(&cp
->ctx
.P
, (const unsigned char *)p
, plen
);
146 mbedtls_mpi_read_binary(&cp
->ctx
.Q
, (const unsigned char *)q
, qlen
);
147 mbedtls_mpi_read_binary(&cp
->ctx
.DP
, (const unsigned char *)dp
, dplen
);
148 mbedtls_mpi_read_binary(&cp
->ctx
.DQ
, (const unsigned char *)dq
, dqlen
);
149 mbedtls_mpi_inv_mod(&cp
->ctx
.QP
, &cp
->ctx
.Q
, &cp
->ctx
.P
);
151 int res
= mbedtls_rsa_check_privkey(&cp
->ctx
);
153 fprintf(stderr
, "PolarSSL private key error res=%x exp=%d mod=%d.\n", res
* -1, explen
, modlen
);
161 static int myrand(void *rng_state
, unsigned char *output
, size_t len
) {
164 if(rng_state
!= NULL
)
167 for( i
= 0; i
< len
; ++i
)
174 static struct crypto_pk
*crypto_pk_polarssl_genkey_rsa(va_list vl
)
176 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
177 memset(cp
, 0x00, sizeof(*cp
));
179 int transient
= va_arg(vl
, int);
180 unsigned int nbits
= va_arg(vl
, unsigned int);
181 unsigned int exp
= va_arg(vl
, unsigned int);
186 int res
= mbedtls_rsa_gen_key(&cp
->ctx
, &myrand
, NULL
, nbits
, exp
);
188 fprintf(stderr
, "PolarSSL private key generation error res=%x exp=%d nbits=%d.\n", res
* -1, exp
, nbits
);
196 static void crypto_pk_polarssl_close(struct crypto_pk
*_cp
)
198 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
200 mbedtls_rsa_free(&cp
->ctx
);
204 static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
206 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
208 unsigned char *result
;
211 size_t keylen
= mbedtls_mpi_size(&cp
->ctx
.N
);
213 result
= malloc(keylen
);
215 printf("RSA encrypt failed. Can't allocate result memory.\n");
219 res
= mbedtls_rsa_public(&cp
->ctx
, buf
, result
);
221 printf("RSA encrypt failed. Error: %x data len: %zd key len: %zd\n", res
* -1, len
, keylen
);
231 static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
233 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
235 unsigned char *result
;
238 size_t keylen
= mbedtls_mpi_size(&cp
->ctx
.N
);
240 result
= malloc(keylen
);
242 printf("RSA encrypt failed. Can't allocate result memory.\n");
246 res
= mbedtls_rsa_private(&cp
->ctx
, NULL
, NULL
, buf
, result
); // CHECK???
248 printf("RSA decrypt failed. Error: %x data len: %zd key len: %zd\n", res
* -1, len
, keylen
);
258 static size_t crypto_pk_polarssl_get_nbits(const struct crypto_pk
*_cp
)
260 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
262 return cp
->ctx
.len
* 8;
266 static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk
*_cp
, unsigned param
, size_t *plen
)
268 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
269 unsigned char *result
= NULL
;
273 *plen
= mbedtls_mpi_size(&cp
->ctx
.N
);
274 result
= malloc(*plen
);
275 memset(result
, 0x00, *plen
);
276 mbedtls_mpi_write_binary(&cp
->ctx
.N
, result
, *plen
);
280 *plen
= mbedtls_mpi_size(&cp
->ctx
.E
);
281 result
= malloc(*plen
);
282 memset(result
, 0x00, *plen
);
283 mbedtls_mpi_write_binary(&cp
->ctx
.E
, result
, *plen
);
286 printf("Error get parameter. Param=%d", param
);
293 static struct crypto_pk
*crypto_pk_polarssl_open(enum crypto_algo_pk pk
, va_list vl
)
295 struct crypto_pk
*cp
;
298 cp
= crypto_pk_polarssl_open_rsa(vl
);
302 cp
->close
= crypto_pk_polarssl_close
;
303 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
304 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
305 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
310 static struct crypto_pk
*crypto_pk_polarssl_open_priv(enum crypto_algo_pk pk
, va_list vl
)
312 struct crypto_pk
*cp
;
315 cp
= crypto_pk_polarssl_open_priv_rsa(vl
);
319 cp
->close
= crypto_pk_polarssl_close
;
320 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
321 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
322 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
323 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
328 static struct crypto_pk
*crypto_pk_polarssl_genkey(enum crypto_algo_pk pk
, va_list vl
)
330 struct crypto_pk
*cp
;
333 cp
= crypto_pk_polarssl_genkey_rsa(vl
);
337 cp
->close
= crypto_pk_polarssl_close
;
338 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
339 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
340 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
341 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
346 static struct crypto_backend crypto_polarssl_backend
= {
347 .hash_open
= crypto_hash_polarssl_open
,
348 .pk_open
= crypto_pk_polarssl_open
,
349 .pk_open_priv
= crypto_pk_polarssl_open_priv
,
350 .pk_genkey
= crypto_pk_polarssl_genkey
,
353 struct crypto_backend
*crypto_polarssl_init(void)
355 return &crypto_polarssl_backend
;