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"
31 struct crypto_hash_polarssl
{
32 struct crypto_hash ch
;
36 static void crypto_hash_polarssl_close(struct crypto_hash
*_ch
)
38 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
43 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
;
47 sha1_update(&(ch
->ctx
), buf
, len
);
50 static unsigned char *crypto_hash_polarssl_read(struct crypto_hash
*_ch
)
52 struct crypto_hash_polarssl
*ch
= (struct crypto_hash_polarssl
*)_ch
;
54 static unsigned char sha1sum
[20];
55 sha1_finish(&(ch
->ctx
), sha1sum
);
59 static size_t crypto_hash_polarssl_get_size(const struct crypto_hash
*ch
)
61 if (ch
->algo
== HASH_SHA_1
)
67 static struct crypto_hash
*crypto_hash_polarssl_open(enum crypto_algo_hash hash
)
69 if (hash
!= HASH_SHA_1
)
72 struct crypto_hash_polarssl
*ch
= malloc(sizeof(*ch
));
74 sha1_starts(&(ch
->ctx
));
76 ch
->ch
.write
= crypto_hash_polarssl_write
;
77 ch
->ch
.read
= crypto_hash_polarssl_read
;
78 ch
->ch
.close
= crypto_hash_polarssl_close
;
79 ch
->ch
.get_size
= crypto_hash_polarssl_get_size
;
84 struct crypto_pk_polarssl
{
89 static struct crypto_pk
*crypto_pk_polarssl_open_rsa(va_list vl
)
91 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
92 memset(cp
, 0x00, sizeof(*cp
));
94 char *mod
= va_arg(vl
, char *); // N
95 int modlen
= va_arg(vl
, size_t);
96 char *exp
= va_arg(vl
, char *); // E
97 int explen
= va_arg(vl
, size_t);
99 rsa_init(&cp
->ctx
, RSA_PKCS_V15
, 0);
101 cp
->ctx
.len
= modlen
; // size(N) in bytes
102 mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
103 mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
105 int res
= rsa_check_pubkey(&cp
->ctx
);
107 fprintf(stderr
, "PolarSSL public key error res=%x exp=%d mod=%d.\n", res
* -1, explen
, modlen
);
115 static struct crypto_pk
*crypto_pk_polarssl_open_priv_rsa(va_list vl
)
117 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
118 memset(cp
, 0x00, sizeof(*cp
));
119 char *mod
= va_arg(vl
, char *);
120 int modlen
= va_arg(vl
, size_t);
121 char *exp
= va_arg(vl
, char *);
122 int explen
= va_arg(vl
, size_t);
123 char *d
= va_arg(vl
, char *);
124 int dlen
= va_arg(vl
, size_t);
125 char *p
= va_arg(vl
, char *);
126 int plen
= va_arg(vl
, size_t);
127 char *q
= va_arg(vl
, char *);
128 int qlen
= va_arg(vl
, size_t);
129 char *dp
= va_arg(vl
, char *);
130 int dplen
= va_arg(vl
, size_t);
131 char *dq
= va_arg(vl
, char *);
132 int dqlen
= va_arg(vl
, size_t);
133 // calc QP via Q and P
134 // char *inv = va_arg(vl, char *);
135 // int invlen = va_arg(vl, size_t);
137 rsa_init(&cp
->ctx
, RSA_PKCS_V15
, 0);
139 cp
->ctx
.len
= modlen
; // size(N) in bytes
140 mpi_read_binary(&cp
->ctx
.N
, (const unsigned char *)mod
, modlen
);
141 mpi_read_binary(&cp
->ctx
.E
, (const unsigned char *)exp
, explen
);
143 mpi_read_binary(&cp
->ctx
.D
, (const unsigned char *)d
, dlen
);
144 mpi_read_binary(&cp
->ctx
.P
, (const unsigned char *)p
, plen
);
145 mpi_read_binary(&cp
->ctx
.Q
, (const unsigned char *)q
, qlen
);
146 mpi_read_binary(&cp
->ctx
.DP
, (const unsigned char *)dp
, dplen
);
147 mpi_read_binary(&cp
->ctx
.DQ
, (const unsigned char *)dq
, dqlen
);
148 mpi_inv_mod(&cp
->ctx
.QP
, &cp
->ctx
.Q
, &cp
->ctx
.P
);
150 int res
= rsa_check_privkey(&cp
->ctx
);
152 fprintf(stderr
, "PolarSSL private key error res=%x exp=%d mod=%d.\n", res
* -1, explen
, modlen
);
159 static int myrand(void *rng_state
, unsigned char *output
, size_t len
) {
162 if(rng_state
!= NULL
)
165 for( i
= 0; i
< len
; ++i
)
172 static struct crypto_pk
*crypto_pk_polarssl_genkey_rsa(va_list vl
)
174 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
175 memset(cp
, 0x00, sizeof(*cp
));
177 int transient
= va_arg(vl
, int);
178 unsigned int nbits
= va_arg(vl
, unsigned int);
179 unsigned int exp
= va_arg(vl
, unsigned int);
184 int res
= rsa_gen_key(&cp
->ctx
, &myrand
, NULL
, nbits
, exp
);
186 fprintf(stderr
, "PolarSSL private key generation error res=%x exp=%d nbits=%d.\n", res
* -1, exp
, nbits
);
193 static void crypto_pk_polarssl_close(struct crypto_pk
*_cp
)
195 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
201 static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
203 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
205 unsigned char *result
;
208 size_t keylen
= mpi_size(&cp
->ctx
.N
);
210 result
= malloc(keylen
);
212 printf("RSA encrypt failed. Can't allocate result memory.\n");
216 res
= rsa_public(&cp
->ctx
, buf
, result
);
218 printf("RSA encrypt failed. Error: %x data len: %d key len: %d\n", res
* -1, len
, keylen
);
227 static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
229 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
231 unsigned char *result
;
234 size_t keylen
= mpi_size(&cp
->ctx
.N
);
236 result
= malloc(keylen
);
238 printf("RSA encrypt failed. Can't allocate result memory.\n");
242 res
= rsa_private(&cp
->ctx
, buf
, result
); // CHECK???
244 printf("RSA decrypt failed. Error: %x data len: %d key len: %d\n", res
* -1, len
, keylen
);
253 static size_t crypto_pk_polarssl_get_nbits(const struct crypto_pk
*_cp
)
255 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
257 return cp
->ctx
.len
* 8;
261 static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk
*_cp
, unsigned param
, size_t *plen
)
263 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
264 unsigned char *result
= NULL
;
268 *plen
= mpi_size(&cp
->ctx
.N
);
269 result
= malloc(*plen
);
270 memset(result
, 0x00, *plen
);
271 mpi_write_binary(&cp
->ctx
.N
, result
, *plen
);
275 *plen
= mpi_size(&cp
->ctx
.E
);
276 result
= malloc(*plen
);
277 memset(result
, 0x00, *plen
);
278 mpi_write_binary(&cp
->ctx
.E
, result
, *plen
);
281 printf("Error get parameter. Param=%d", param
);
288 static struct crypto_pk
*crypto_pk_polarssl_open(enum crypto_algo_pk pk
, va_list vl
)
290 struct crypto_pk
*cp
;
293 cp
= crypto_pk_polarssl_open_rsa(vl
);
297 cp
->close
= crypto_pk_polarssl_close
;
298 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
299 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
300 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
305 static struct crypto_pk
*crypto_pk_polarssl_open_priv(enum crypto_algo_pk pk
, va_list vl
)
307 struct crypto_pk
*cp
;
310 cp
= crypto_pk_polarssl_open_priv_rsa(vl
);
314 cp
->close
= crypto_pk_polarssl_close
;
315 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
316 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
317 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
318 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
323 static struct crypto_pk
*crypto_pk_polarssl_genkey(enum crypto_algo_pk pk
, va_list vl
)
325 struct crypto_pk
*cp
;
328 cp
= crypto_pk_polarssl_genkey_rsa(vl
);
332 cp
->close
= crypto_pk_polarssl_close
;
333 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
334 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
335 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
336 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
341 static struct crypto_backend crypto_polarssl_backend
= {
342 .hash_open
= crypto_hash_polarssl_open
,
343 .pk_open
= crypto_pk_polarssl_open
,
344 .pk_open_priv
= crypto_pk_polarssl_open_priv
,
345 .pk_genkey
= crypto_pk_polarssl_genkey
,
348 struct crypto_backend
*crypto_polarssl_init(void)
350 return &crypto_polarssl_backend
;