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
);
160 static int myrand(void *rng_state
, unsigned char *output
, size_t len
) {
163 if(rng_state
!= NULL
)
166 for( i
= 0; i
< len
; ++i
)
173 static struct crypto_pk
*crypto_pk_polarssl_genkey_rsa(va_list vl
)
175 struct crypto_pk_polarssl
*cp
= malloc(sizeof(*cp
));
176 memset(cp
, 0x00, sizeof(*cp
));
178 int transient
= va_arg(vl
, int);
179 unsigned int nbits
= va_arg(vl
, unsigned int);
180 unsigned int exp
= va_arg(vl
, unsigned int);
185 int res
= rsa_gen_key(&cp
->ctx
, &myrand
, NULL
, nbits
, exp
);
187 fprintf(stderr
, "PolarSSL private key generation error res=%x exp=%d nbits=%d.\n", res
* -1, exp
, nbits
);
195 static void crypto_pk_polarssl_close(struct crypto_pk
*_cp
)
197 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
203 static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
205 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
207 unsigned char *result
;
210 size_t keylen
= mpi_size(&cp
->ctx
.N
);
212 result
= malloc(keylen
);
214 printf("RSA encrypt failed. Can't allocate result memory.\n");
218 res
= rsa_public(&cp
->ctx
, buf
, result
);
220 printf("RSA encrypt failed. Error: %x data len: %zd key len: %zd\n", res
* -1, len
, keylen
);
230 static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk
*_cp
, const unsigned char *buf
, size_t len
, size_t *clen
)
232 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
234 unsigned char *result
;
237 size_t keylen
= mpi_size(&cp
->ctx
.N
);
239 result
= malloc(keylen
);
241 printf("RSA encrypt failed. Can't allocate result memory.\n");
245 res
= rsa_private(&cp
->ctx
, buf
, result
); // CHECK???
247 printf("RSA decrypt failed. Error: %x data len: %zd key len: %zd\n", res
* -1, len
, keylen
);
257 static size_t crypto_pk_polarssl_get_nbits(const struct crypto_pk
*_cp
)
259 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
261 return cp
->ctx
.len
* 8;
265 static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk
*_cp
, unsigned param
, size_t *plen
)
267 struct crypto_pk_polarssl
*cp
= (struct crypto_pk_polarssl
*)_cp
;
268 unsigned char *result
= NULL
;
272 *plen
= mpi_size(&cp
->ctx
.N
);
273 result
= malloc(*plen
);
274 memset(result
, 0x00, *plen
);
275 mpi_write_binary(&cp
->ctx
.N
, result
, *plen
);
279 *plen
= mpi_size(&cp
->ctx
.E
);
280 result
= malloc(*plen
);
281 memset(result
, 0x00, *plen
);
282 mpi_write_binary(&cp
->ctx
.E
, result
, *plen
);
285 printf("Error get parameter. Param=%d", param
);
292 static struct crypto_pk
*crypto_pk_polarssl_open(enum crypto_algo_pk pk
, va_list vl
)
294 struct crypto_pk
*cp
;
297 cp
= crypto_pk_polarssl_open_rsa(vl
);
301 cp
->close
= crypto_pk_polarssl_close
;
302 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
303 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
304 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
309 static struct crypto_pk
*crypto_pk_polarssl_open_priv(enum crypto_algo_pk pk
, va_list vl
)
311 struct crypto_pk
*cp
;
314 cp
= crypto_pk_polarssl_open_priv_rsa(vl
);
318 cp
->close
= crypto_pk_polarssl_close
;
319 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
320 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
321 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
322 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
327 static struct crypto_pk
*crypto_pk_polarssl_genkey(enum crypto_algo_pk pk
, va_list vl
)
329 struct crypto_pk
*cp
;
332 cp
= crypto_pk_polarssl_genkey_rsa(vl
);
336 cp
->close
= crypto_pk_polarssl_close
;
337 cp
->encrypt
= crypto_pk_polarssl_encrypt
;
338 cp
->decrypt
= crypto_pk_polarssl_decrypt
;
339 cp
->get_parameter
= crypto_pk_polarssl_get_parameter
;
340 cp
->get_nbits
= crypto_pk_polarssl_get_nbits
;
345 static struct crypto_backend crypto_polarssl_backend
= {
346 .hash_open
= crypto_hash_polarssl_open
,
347 .pk_open
= crypto_pk_polarssl_open
,
348 .pk_open_priv
= crypto_pk_polarssl_open_priv
,
349 .pk_genkey
= crypto_pk_polarssl_genkey
,
352 struct crypto_backend
*crypto_polarssl_init(void)
354 return &crypto_polarssl_backend
;