]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/engine/eng_cryptodev.c
cryptodev_digest_update: don't leak original state->mac_data if realloc fails
[thirdparty/openssl.git] / crypto / engine / eng_cryptodev.c
1 /*
2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3 * Copyright (c) 2002 Theo de Raadt
4 * Copyright (c) 2002 Markus Friedl
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <openssl/objects.h>
30 #include <openssl/engine.h>
31 #include <openssl/evp.h>
32 #include <openssl/bn.h>
33
34 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35 (defined(OpenBSD) || defined(__FreeBSD__))
36 #include <sys/param.h>
37 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38 # define HAVE_CRYPTODEV
39 # endif
40 # if (OpenBSD >= 200110)
41 # define HAVE_SYSLOG_R
42 # endif
43 #endif
44
45 #ifndef HAVE_CRYPTODEV
46
47 void
48 ENGINE_load_cryptodev(void)
49 {
50 /* This is a NOP on platforms without /dev/crypto */
51 return;
52 }
53
54 #else
55
56 #include <sys/types.h>
57 #include <crypto/cryptodev.h>
58 #include <openssl/dh.h>
59 #include <openssl/dsa.h>
60 #include <openssl/err.h>
61 #include <openssl/rsa.h>
62 #include <sys/ioctl.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #include <stdarg.h>
68 #include <syslog.h>
69 #include <errno.h>
70 #include <string.h>
71
72 struct dev_crypto_state {
73 struct session_op d_sess;
74 int d_fd;
75
76 #ifdef USE_CRYPTODEV_DIGESTS
77 char dummy_mac_key[HASH_MAX_LEN];
78
79 unsigned char digest_res[HASH_MAX_LEN];
80 char *mac_data;
81 int mac_len;
82 #endif
83 };
84
85 static u_int32_t cryptodev_asymfeat = 0;
86
87 static int get_asym_dev_crypto(void);
88 static int open_dev_crypto(void);
89 static int get_dev_crypto(void);
90 static int get_cryptodev_ciphers(const int **cnids);
91 #ifdef USE_CRYPTODEV_DIGESTS
92 static int get_cryptodev_digests(const int **cnids);
93 #endif
94 static int cryptodev_usable_ciphers(const int **nids);
95 static int cryptodev_usable_digests(const int **nids);
96 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
97 const unsigned char *in, size_t inl);
98 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
99 const unsigned char *iv, int enc);
100 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
101 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
102 const int **nids, int nid);
103 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
104 const int **nids, int nid);
105 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
106 static int crparam2bn(struct crparam *crp, BIGNUM *a);
107 static void zapparams(struct crypt_kop *kop);
108 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
109 int slen, BIGNUM *s);
110
111 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
112 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
113 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
114 RSA *rsa, BN_CTX *ctx);
115 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
116 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
117 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
118 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
119 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
120 BN_CTX *ctx, BN_MONT_CTX *mont);
121 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
122 int dlen, DSA *dsa);
123 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
124 DSA_SIG *sig, DSA *dsa);
125 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
126 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
127 BN_MONT_CTX *m_ctx);
128 static int cryptodev_dh_compute_key(unsigned char *key,
129 const BIGNUM *pub_key, DH *dh);
130 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
131 void (*f)(void));
132 void ENGINE_load_cryptodev(void);
133
134 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
135 { 0, NULL, NULL, 0 }
136 };
137
138 static struct {
139 int id;
140 int nid;
141 int ivmax;
142 int keylen;
143 } ciphers[] = {
144 { CRYPTO_ARC4, NID_rc4, 0, 16, },
145 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
146 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
147 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
148 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
149 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
150 #ifdef CRYPTO_AES_CTR
151 { CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16, },
152 { CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24, },
153 { CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32, },
154 #endif
155 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
156 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
157 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
158 { 0, NID_undef, 0, 0, },
159 };
160
161 #ifdef USE_CRYPTODEV_DIGESTS
162 static struct {
163 int id;
164 int nid;
165 int keylen;
166 } digests[] = {
167 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
168 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
169 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/},
170 { CRYPTO_MD5_KPDK, NID_undef, 0},
171 { CRYPTO_SHA1_KPDK, NID_undef, 0},
172 { CRYPTO_MD5, NID_md5, 16},
173 { CRYPTO_SHA1, NID_sha1, 20},
174 { 0, NID_undef, 0},
175 };
176 #endif
177
178 /*
179 * Return a fd if /dev/crypto seems usable, 0 otherwise.
180 */
181 static int
182 open_dev_crypto(void)
183 {
184 static int fd = -1;
185
186 if (fd == -1) {
187 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
188 return (-1);
189 /* close on exec */
190 if (fcntl(fd, F_SETFD, 1) == -1) {
191 close(fd);
192 fd = -1;
193 return (-1);
194 }
195 }
196 return (fd);
197 }
198
199 static int
200 get_dev_crypto(void)
201 {
202 int fd, retfd;
203
204 if ((fd = open_dev_crypto()) == -1)
205 return (-1);
206 #ifndef CRIOGET_NOT_NEEDED
207 if (ioctl(fd, CRIOGET, &retfd) == -1)
208 return (-1);
209
210 /* close on exec */
211 if (fcntl(retfd, F_SETFD, 1) == -1) {
212 close(retfd);
213 return (-1);
214 }
215 #else
216 retfd = fd;
217 #endif
218 return (retfd);
219 }
220
221 static void put_dev_crypto(int fd)
222 {
223 #ifndef CRIOGET_NOT_NEEDED
224 close(fd);
225 #endif
226 }
227
228 /* Caching version for asym operations */
229 static int
230 get_asym_dev_crypto(void)
231 {
232 static int fd = -1;
233
234 if (fd == -1)
235 fd = get_dev_crypto();
236 return fd;
237 }
238
239 /*
240 * Find out what ciphers /dev/crypto will let us have a session for.
241 * XXX note, that some of these openssl doesn't deal with yet!
242 * returning them here is harmless, as long as we return NULL
243 * when asked for a handler in the cryptodev_engine_ciphers routine
244 */
245 static int
246 get_cryptodev_ciphers(const int **cnids)
247 {
248 static int nids[CRYPTO_ALGORITHM_MAX];
249 struct session_op sess;
250 int fd, i, count = 0;
251
252 if ((fd = get_dev_crypto()) < 0) {
253 *cnids = NULL;
254 return (0);
255 }
256 memset(&sess, 0, sizeof(sess));
257 sess.key = (caddr_t)"123456789abcdefghijklmno";
258
259 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
260 if (ciphers[i].nid == NID_undef)
261 continue;
262 sess.cipher = ciphers[i].id;
263 sess.keylen = ciphers[i].keylen;
264 sess.mac = 0;
265 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
266 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
267 nids[count++] = ciphers[i].nid;
268 }
269 put_dev_crypto(fd);
270
271 if (count > 0)
272 *cnids = nids;
273 else
274 *cnids = NULL;
275 return (count);
276 }
277
278 #ifdef USE_CRYPTODEV_DIGESTS
279 /*
280 * Find out what digests /dev/crypto will let us have a session for.
281 * XXX note, that some of these openssl doesn't deal with yet!
282 * returning them here is harmless, as long as we return NULL
283 * when asked for a handler in the cryptodev_engine_digests routine
284 */
285 static int
286 get_cryptodev_digests(const int **cnids)
287 {
288 static int nids[CRYPTO_ALGORITHM_MAX];
289 struct session_op sess;
290 int fd, i, count = 0;
291
292 if ((fd = get_dev_crypto()) < 0) {
293 *cnids = NULL;
294 return (0);
295 }
296 memset(&sess, 0, sizeof(sess));
297 sess.mackey = (caddr_t)"123456789abcdefghijklmno";
298 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
299 if (digests[i].nid == NID_undef)
300 continue;
301 sess.mac = digests[i].id;
302 sess.mackeylen = digests[i].keylen;
303 sess.cipher = 0;
304 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
305 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
306 nids[count++] = digests[i].nid;
307 }
308 put_dev_crypto(fd);
309
310 if (count > 0)
311 *cnids = nids;
312 else
313 *cnids = NULL;
314 return (count);
315 }
316 #endif /* 0 */
317
318 /*
319 * Find the useable ciphers|digests from dev/crypto - this is the first
320 * thing called by the engine init crud which determines what it
321 * can use for ciphers from this engine. We want to return
322 * only what we can do, anythine else is handled by software.
323 *
324 * If we can't initialize the device to do anything useful for
325 * any reason, we want to return a NULL array, and 0 length,
326 * which forces everything to be done is software. By putting
327 * the initalization of the device in here, we ensure we can
328 * use this engine as the default, and if for whatever reason
329 * /dev/crypto won't do what we want it will just be done in
330 * software
331 *
332 * This can (should) be greatly expanded to perhaps take into
333 * account speed of the device, and what we want to do.
334 * (although the disabling of particular alg's could be controlled
335 * by the device driver with sysctl's.) - this is where we
336 * want most of the decisions made about what we actually want
337 * to use from /dev/crypto.
338 */
339 static int
340 cryptodev_usable_ciphers(const int **nids)
341 {
342 return (get_cryptodev_ciphers(nids));
343 }
344
345 static int
346 cryptodev_usable_digests(const int **nids)
347 {
348 #ifdef USE_CRYPTODEV_DIGESTS
349 return (get_cryptodev_digests(nids));
350 #else
351 /*
352 * XXXX just disable all digests for now, because it sucks.
353 * we need a better way to decide this - i.e. I may not
354 * want digests on slow cards like hifn on fast machines,
355 * but might want them on slow or loaded machines, etc.
356 * will also want them when using crypto cards that don't
357 * suck moose gonads - would be nice to be able to decide something
358 * as reasonable default without having hackery that's card dependent.
359 * of course, the default should probably be just do everything,
360 * with perhaps a sysctl to turn algoritms off (or have them off
361 * by default) on cards that generally suck like the hifn.
362 */
363 *nids = NULL;
364 return (0);
365 #endif
366 }
367
368 static int
369 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
370 const unsigned char *in, size_t inl)
371 {
372 struct crypt_op cryp;
373 struct dev_crypto_state *state = ctx->cipher_data;
374 struct session_op *sess = &state->d_sess;
375 const void *iiv;
376 unsigned char save_iv[EVP_MAX_IV_LENGTH];
377
378 if (state->d_fd < 0)
379 return (0);
380 if (!inl)
381 return (1);
382 if ((inl % ctx->cipher->block_size) != 0)
383 return (0);
384
385 memset(&cryp, 0, sizeof(cryp));
386
387 cryp.ses = sess->ses;
388 cryp.flags = 0;
389 cryp.len = inl;
390 cryp.src = (caddr_t) in;
391 cryp.dst = (caddr_t) out;
392 cryp.mac = 0;
393
394 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
395
396 if (ctx->cipher->iv_len) {
397 cryp.iv = (caddr_t) ctx->iv;
398 if (!ctx->encrypt) {
399 iiv = in + inl - ctx->cipher->iv_len;
400 memcpy(save_iv, iiv, ctx->cipher->iv_len);
401 }
402 } else
403 cryp.iv = NULL;
404
405 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
406 /* XXX need better errror handling
407 * this can fail for a number of different reasons.
408 */
409 return (0);
410 }
411
412 if (ctx->cipher->iv_len) {
413 if (ctx->encrypt)
414 iiv = out + inl - ctx->cipher->iv_len;
415 else
416 iiv = save_iv;
417 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
418 }
419 return (1);
420 }
421
422 static int
423 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
424 const unsigned char *iv, int enc)
425 {
426 struct dev_crypto_state *state = ctx->cipher_data;
427 struct session_op *sess = &state->d_sess;
428 int cipher = -1, i;
429
430 for (i = 0; ciphers[i].id; i++)
431 if (ctx->cipher->nid == ciphers[i].nid &&
432 ctx->cipher->iv_len <= ciphers[i].ivmax &&
433 ctx->key_len == ciphers[i].keylen) {
434 cipher = ciphers[i].id;
435 break;
436 }
437
438 if (!ciphers[i].id) {
439 state->d_fd = -1;
440 return (0);
441 }
442
443 memset(sess, 0, sizeof(struct session_op));
444
445 if ((state->d_fd = get_dev_crypto()) < 0)
446 return (0);
447
448 sess->key = (caddr_t)key;
449 sess->keylen = ctx->key_len;
450 sess->cipher = cipher;
451
452 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
453 put_dev_crypto(state->d_fd);
454 state->d_fd = -1;
455 return (0);
456 }
457 return (1);
458 }
459
460 /*
461 * free anything we allocated earlier when initting a
462 * session, and close the session.
463 */
464 static int
465 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
466 {
467 int ret = 0;
468 struct dev_crypto_state *state = ctx->cipher_data;
469 struct session_op *sess = &state->d_sess;
470
471 if (state->d_fd < 0)
472 return (0);
473
474 /* XXX if this ioctl fails, someting's wrong. the invoker
475 * may have called us with a bogus ctx, or we could
476 * have a device that for whatever reason just doesn't
477 * want to play ball - it's not clear what's right
478 * here - should this be an error? should it just
479 * increase a counter, hmm. For right now, we return
480 * 0 - I don't believe that to be "right". we could
481 * call the gorpy openssl lib error handlers that
482 * print messages to users of the library. hmm..
483 */
484
485 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
486 ret = 0;
487 } else {
488 ret = 1;
489 }
490 put_dev_crypto(state->d_fd);
491 state->d_fd = -1;
492
493 return (ret);
494 }
495
496 /*
497 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
498 * gets called when libcrypto requests a cipher NID.
499 */
500
501 /* RC4 */
502 const EVP_CIPHER cryptodev_rc4 = {
503 NID_rc4,
504 1, 16, 0,
505 EVP_CIPH_VARIABLE_LENGTH,
506 cryptodev_init_key,
507 cryptodev_cipher,
508 cryptodev_cleanup,
509 sizeof(struct dev_crypto_state),
510 NULL,
511 NULL,
512 NULL
513 };
514
515 /* DES CBC EVP */
516 const EVP_CIPHER cryptodev_des_cbc = {
517 NID_des_cbc,
518 8, 8, 8,
519 EVP_CIPH_CBC_MODE,
520 cryptodev_init_key,
521 cryptodev_cipher,
522 cryptodev_cleanup,
523 sizeof(struct dev_crypto_state),
524 EVP_CIPHER_set_asn1_iv,
525 EVP_CIPHER_get_asn1_iv,
526 NULL
527 };
528
529 /* 3DES CBC EVP */
530 const EVP_CIPHER cryptodev_3des_cbc = {
531 NID_des_ede3_cbc,
532 8, 24, 8,
533 EVP_CIPH_CBC_MODE,
534 cryptodev_init_key,
535 cryptodev_cipher,
536 cryptodev_cleanup,
537 sizeof(struct dev_crypto_state),
538 EVP_CIPHER_set_asn1_iv,
539 EVP_CIPHER_get_asn1_iv,
540 NULL
541 };
542
543 const EVP_CIPHER cryptodev_bf_cbc = {
544 NID_bf_cbc,
545 8, 16, 8,
546 EVP_CIPH_CBC_MODE,
547 cryptodev_init_key,
548 cryptodev_cipher,
549 cryptodev_cleanup,
550 sizeof(struct dev_crypto_state),
551 EVP_CIPHER_set_asn1_iv,
552 EVP_CIPHER_get_asn1_iv,
553 NULL
554 };
555
556 const EVP_CIPHER cryptodev_cast_cbc = {
557 NID_cast5_cbc,
558 8, 16, 8,
559 EVP_CIPH_CBC_MODE,
560 cryptodev_init_key,
561 cryptodev_cipher,
562 cryptodev_cleanup,
563 sizeof(struct dev_crypto_state),
564 EVP_CIPHER_set_asn1_iv,
565 EVP_CIPHER_get_asn1_iv,
566 NULL
567 };
568
569 const EVP_CIPHER cryptodev_aes_cbc = {
570 NID_aes_128_cbc,
571 16, 16, 16,
572 EVP_CIPH_CBC_MODE,
573 cryptodev_init_key,
574 cryptodev_cipher,
575 cryptodev_cleanup,
576 sizeof(struct dev_crypto_state),
577 EVP_CIPHER_set_asn1_iv,
578 EVP_CIPHER_get_asn1_iv,
579 NULL
580 };
581
582 const EVP_CIPHER cryptodev_aes_192_cbc = {
583 NID_aes_192_cbc,
584 16, 24, 16,
585 EVP_CIPH_CBC_MODE,
586 cryptodev_init_key,
587 cryptodev_cipher,
588 cryptodev_cleanup,
589 sizeof(struct dev_crypto_state),
590 EVP_CIPHER_set_asn1_iv,
591 EVP_CIPHER_get_asn1_iv,
592 NULL
593 };
594
595 const EVP_CIPHER cryptodev_aes_256_cbc = {
596 NID_aes_256_cbc,
597 16, 32, 16,
598 EVP_CIPH_CBC_MODE,
599 cryptodev_init_key,
600 cryptodev_cipher,
601 cryptodev_cleanup,
602 sizeof(struct dev_crypto_state),
603 EVP_CIPHER_set_asn1_iv,
604 EVP_CIPHER_get_asn1_iv,
605 NULL
606 };
607 #ifdef CRYPTO_AES_CTR
608 const EVP_CIPHER cryptodev_aes_ctr = {
609 NID_aes_128_ctr,
610 16, 16, 14,
611 EVP_CIPH_CTR_MODE,
612 cryptodev_init_key,
613 cryptodev_cipher,
614 cryptodev_cleanup,
615 sizeof(struct dev_crypto_state),
616 EVP_CIPHER_set_asn1_iv,
617 EVP_CIPHER_get_asn1_iv,
618 NULL
619 };
620
621 const EVP_CIPHER cryptodev_aes_ctr_192 = {
622 NID_aes_192_ctr,
623 16, 24, 14,
624 EVP_CIPH_CTR_MODE,
625 cryptodev_init_key,
626 cryptodev_cipher,
627 cryptodev_cleanup,
628 sizeof(struct dev_crypto_state),
629 EVP_CIPHER_set_asn1_iv,
630 EVP_CIPHER_get_asn1_iv,
631 NULL
632 };
633
634 const EVP_CIPHER cryptodev_aes_ctr_256 = {
635 NID_aes_256_ctr,
636 16, 32, 14,
637 EVP_CIPH_CTR_MODE,
638 cryptodev_init_key,
639 cryptodev_cipher,
640 cryptodev_cleanup,
641 sizeof(struct dev_crypto_state),
642 EVP_CIPHER_set_asn1_iv,
643 EVP_CIPHER_get_asn1_iv,
644 NULL
645 };
646 #endif
647 /*
648 * Registered by the ENGINE when used to find out how to deal with
649 * a particular NID in the ENGINE. this says what we'll do at the
650 * top level - note, that list is restricted by what we answer with
651 */
652 static int
653 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
654 const int **nids, int nid)
655 {
656 if (!cipher)
657 return (cryptodev_usable_ciphers(nids));
658
659 switch (nid) {
660 case NID_rc4:
661 *cipher = &cryptodev_rc4;
662 break;
663 case NID_des_ede3_cbc:
664 *cipher = &cryptodev_3des_cbc;
665 break;
666 case NID_des_cbc:
667 *cipher = &cryptodev_des_cbc;
668 break;
669 case NID_bf_cbc:
670 *cipher = &cryptodev_bf_cbc;
671 break;
672 case NID_cast5_cbc:
673 *cipher = &cryptodev_cast_cbc;
674 break;
675 case NID_aes_128_cbc:
676 *cipher = &cryptodev_aes_cbc;
677 break;
678 case NID_aes_192_cbc:
679 *cipher = &cryptodev_aes_192_cbc;
680 break;
681 case NID_aes_256_cbc:
682 *cipher = &cryptodev_aes_256_cbc;
683 break;
684 #ifdef CRYPTO_AES_CTR
685 case NID_aes_128_ctr:
686 *cipher = &cryptodev_aes_ctr;
687 break;
688 case NID_aes_192_ctr:
689 *cipher = &cryptodev_aes_ctr_192;
690 break;
691 case NID_aes_256_ctr:
692 *cipher = &cryptodev_aes_ctr_256;
693 break;
694 #endif
695 default:
696 *cipher = NULL;
697 break;
698 }
699 return (*cipher != NULL);
700 }
701
702
703 #ifdef USE_CRYPTODEV_DIGESTS
704
705 /* convert digest type to cryptodev */
706 static int
707 digest_nid_to_cryptodev(int nid)
708 {
709 int i;
710
711 for (i = 0; digests[i].id; i++)
712 if (digests[i].nid == nid)
713 return (digests[i].id);
714 return (0);
715 }
716
717
718 static int
719 digest_key_length(int nid)
720 {
721 int i;
722
723 for (i = 0; digests[i].id; i++)
724 if (digests[i].nid == nid)
725 return digests[i].keylen;
726 return (0);
727 }
728
729
730 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
731 {
732 struct dev_crypto_state *state = ctx->md_data;
733 struct session_op *sess = &state->d_sess;
734 int digest;
735
736 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
737 printf("cryptodev_digest_init: Can't get digest \n");
738 return (0);
739 }
740
741 memset(state, 0, sizeof(struct dev_crypto_state));
742
743 if ((state->d_fd = get_dev_crypto()) < 0) {
744 printf("cryptodev_digest_init: Can't get Dev \n");
745 return (0);
746 }
747
748 sess->mackey = state->dummy_mac_key;
749 sess->mackeylen = digest_key_length(ctx->digest->type);
750 sess->mac = digest;
751
752 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
753 put_dev_crypto(state->d_fd);
754 state->d_fd = -1;
755 printf("cryptodev_digest_init: Open session failed\n");
756 return (0);
757 }
758
759 return (1);
760 }
761
762 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
763 size_t count)
764 {
765 struct crypt_op cryp;
766 struct dev_crypto_state *state = ctx->md_data;
767 struct session_op *sess = &state->d_sess;
768 char *new_mac_data;
769
770 if (!data || state->d_fd < 0) {
771 printf("cryptodev_digest_update: illegal inputs \n");
772 return (0);
773 }
774
775 if (!count) {
776 return (0);
777 }
778
779 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
780 /* if application doesn't support one buffer */
781 new_mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
782
783 if (!new_mac_data) {
784 printf("cryptodev_digest_update: realloc failed\n");
785 return (0);
786 }
787 state->mac_data = new_mac_data;
788
789 memcpy(state->mac_data + state->mac_len, data, count);
790 state->mac_len += count;
791
792 return (1);
793 }
794
795 memset(&cryp, 0, sizeof(cryp));
796
797 cryp.ses = sess->ses;
798 cryp.flags = 0;
799 cryp.len = count;
800 cryp.src = (caddr_t) data;
801 cryp.dst = NULL;
802 cryp.mac = (caddr_t) state->digest_res;
803 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
804 printf("cryptodev_digest_update: digest failed\n");
805 return (0);
806 }
807 return (1);
808 }
809
810
811 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
812 {
813 struct crypt_op cryp;
814 struct dev_crypto_state *state = ctx->md_data;
815 struct session_op *sess = &state->d_sess;
816
817 int ret = 1;
818
819 if (!md || state->d_fd < 0) {
820 printf("cryptodev_digest_final: illegal input\n");
821 return(0);
822 }
823
824 if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
825 /* if application doesn't support one buffer */
826 memset(&cryp, 0, sizeof(cryp));
827 cryp.ses = sess->ses;
828 cryp.flags = 0;
829 cryp.len = state->mac_len;
830 cryp.src = state->mac_data;
831 cryp.dst = NULL;
832 cryp.mac = (caddr_t)md;
833 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
834 printf("cryptodev_digest_final: digest failed\n");
835 return (0);
836 }
837
838 return 1;
839 }
840
841 memcpy(md, state->digest_res, ctx->digest->md_size);
842
843 return (ret);
844 }
845
846
847 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
848 {
849 int ret = 1;
850 struct dev_crypto_state *state = ctx->md_data;
851 struct session_op *sess = &state->d_sess;
852
853 if (state == NULL)
854 return 0;
855
856 if (state->d_fd < 0) {
857 printf("cryptodev_digest_cleanup: illegal input\n");
858 return (0);
859 }
860
861 if (state->mac_data) {
862 OPENSSL_free(state->mac_data);
863 state->mac_data = NULL;
864 state->mac_len = 0;
865 }
866
867 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
868 printf("cryptodev_digest_cleanup: failed to close session\n");
869 ret = 0;
870 } else {
871 ret = 1;
872 }
873 put_dev_crypto(state->d_fd);
874 state->d_fd = -1;
875
876 return (ret);
877 }
878
879 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
880 {
881 struct dev_crypto_state *fstate = from->md_data;
882 struct dev_crypto_state *dstate = to->md_data;
883 struct session_op *sess;
884 int digest;
885
886 if (dstate == NULL || fstate == NULL)
887 return 1;
888
889 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
890
891 sess = &dstate->d_sess;
892
893 digest = digest_nid_to_cryptodev(to->digest->type);
894
895 sess->mackey = dstate->dummy_mac_key;
896 sess->mackeylen = digest_key_length(to->digest->type);
897 sess->mac = digest;
898
899 dstate->d_fd = get_dev_crypto();
900
901 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
902 put_dev_crypto(dstate->d_fd);
903 dstate->d_fd = -1;
904 printf("cryptodev_digest_init: Open session failed\n");
905 return (0);
906 }
907
908 if (fstate->mac_len != 0) {
909 if (fstate->mac_data != NULL)
910 {
911 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
912 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
913 dstate->mac_len = fstate->mac_len;
914 }
915 }
916
917 return 1;
918 }
919
920
921 const EVP_MD cryptodev_sha1 = {
922 NID_sha1,
923 NID_undef,
924 SHA_DIGEST_LENGTH,
925 EVP_MD_FLAG_ONESHOT,
926 cryptodev_digest_init,
927 cryptodev_digest_update,
928 cryptodev_digest_final,
929 cryptodev_digest_copy,
930 cryptodev_digest_cleanup,
931 EVP_PKEY_NULL_method,
932 SHA_CBLOCK,
933 sizeof(struct dev_crypto_state),
934 };
935
936 const EVP_MD cryptodev_md5 = {
937 NID_md5,
938 NID_undef,
939 16 /* MD5_DIGEST_LENGTH */,
940 EVP_MD_FLAG_ONESHOT,
941 cryptodev_digest_init,
942 cryptodev_digest_update,
943 cryptodev_digest_final,
944 cryptodev_digest_copy,
945 cryptodev_digest_cleanup,
946 EVP_PKEY_NULL_method,
947 64 /* MD5_CBLOCK */,
948 sizeof(struct dev_crypto_state),
949 };
950
951 #endif /* USE_CRYPTODEV_DIGESTS */
952
953
954 static int
955 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
956 const int **nids, int nid)
957 {
958 if (!digest)
959 return (cryptodev_usable_digests(nids));
960
961 switch (nid) {
962 #ifdef USE_CRYPTODEV_DIGESTS
963 case NID_md5:
964 *digest = &cryptodev_md5;
965 break;
966 case NID_sha1:
967 *digest = &cryptodev_sha1;
968 break;
969 default:
970 #endif /* USE_CRYPTODEV_DIGESTS */
971 *digest = NULL;
972 break;
973 }
974 return (*digest != NULL);
975 }
976
977 /*
978 * Convert a BIGNUM to the representation that /dev/crypto needs.
979 * Upon completion of use, the caller is responsible for freeing
980 * crp->crp_p.
981 */
982 static int
983 bn2crparam(const BIGNUM *a, struct crparam *crp)
984 {
985 int i, j, k;
986 ssize_t bytes, bits;
987 u_char *b;
988
989 crp->crp_p = NULL;
990 crp->crp_nbits = 0;
991
992 bits = BN_num_bits(a);
993 bytes = (bits + 7) / 8;
994
995 b = malloc(bytes);
996 if (b == NULL)
997 return (1);
998 memset(b, 0, bytes);
999
1000 crp->crp_p = (caddr_t) b;
1001 crp->crp_nbits = bits;
1002
1003 for (i = 0, j = 0; i < a->top; i++) {
1004 for (k = 0; k < BN_BITS2 / 8; k++) {
1005 if ((j + k) >= bytes)
1006 return (0);
1007 b[j + k] = a->d[i] >> (k * 8);
1008 }
1009 j += BN_BITS2 / 8;
1010 }
1011 return (0);
1012 }
1013
1014 /* Convert a /dev/crypto parameter to a BIGNUM */
1015 static int
1016 crparam2bn(struct crparam *crp, BIGNUM *a)
1017 {
1018 u_int8_t *pd;
1019 int i, bytes;
1020
1021 bytes = (crp->crp_nbits + 7) / 8;
1022
1023 if (bytes == 0)
1024 return (-1);
1025
1026 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1027 return (-1);
1028
1029 for (i = 0; i < bytes; i++)
1030 pd[i] = crp->crp_p[bytes - i - 1];
1031
1032 BN_bin2bn(pd, bytes, a);
1033 free(pd);
1034
1035 return (0);
1036 }
1037
1038 static void
1039 zapparams(struct crypt_kop *kop)
1040 {
1041 int i;
1042
1043 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1044 if (kop->crk_param[i].crp_p)
1045 free(kop->crk_param[i].crp_p);
1046 kop->crk_param[i].crp_p = NULL;
1047 kop->crk_param[i].crp_nbits = 0;
1048 }
1049 }
1050
1051 static int
1052 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1053 {
1054 int fd, ret = -1;
1055
1056 if ((fd = get_asym_dev_crypto()) < 0)
1057 return (ret);
1058
1059 if (r) {
1060 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1061 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1062 kop->crk_oparams++;
1063 }
1064 if (s) {
1065 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1066 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1067 kop->crk_oparams++;
1068 }
1069
1070 if (ioctl(fd, CIOCKEY, kop) == 0) {
1071 if (r)
1072 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1073 if (s)
1074 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1075 ret = 0;
1076 }
1077
1078 return (ret);
1079 }
1080
1081 static int
1082 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1083 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1084 {
1085 struct crypt_kop kop;
1086 int ret = 1;
1087
1088 /* Currently, we know we can do mod exp iff we can do any
1089 * asymmetric operations at all.
1090 */
1091 if (cryptodev_asymfeat == 0) {
1092 ret = BN_mod_exp(r, a, p, m, ctx);
1093 return (ret);
1094 }
1095
1096 memset(&kop, 0, sizeof kop);
1097 kop.crk_op = CRK_MOD_EXP;
1098
1099 /* inputs: a^p % m */
1100 if (bn2crparam(a, &kop.crk_param[0]))
1101 goto err;
1102 if (bn2crparam(p, &kop.crk_param[1]))
1103 goto err;
1104 if (bn2crparam(m, &kop.crk_param[2]))
1105 goto err;
1106 kop.crk_iparams = 3;
1107
1108 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1109 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1110 printf("OCF asym process failed, Running in software\n");
1111 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1112
1113 } else if (ECANCELED == kop.crk_status) {
1114 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1115 printf("OCF hardware operation cancelled. Running in Software\n");
1116 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1117 }
1118 /* else cryptodev operation worked ok ==> ret = 1*/
1119
1120 err:
1121 zapparams(&kop);
1122 return (ret);
1123 }
1124
1125 static int
1126 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1127 {
1128 int r;
1129 ctx = BN_CTX_new();
1130 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1131 BN_CTX_free(ctx);
1132 return (r);
1133 }
1134
1135 static int
1136 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1137 {
1138 struct crypt_kop kop;
1139 int ret = 1;
1140
1141 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1142 /* XXX 0 means failure?? */
1143 return (0);
1144 }
1145
1146 memset(&kop, 0, sizeof kop);
1147 kop.crk_op = CRK_MOD_EXP_CRT;
1148 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1149 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1150 goto err;
1151 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1152 goto err;
1153 if (bn2crparam(I, &kop.crk_param[2]))
1154 goto err;
1155 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1156 goto err;
1157 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1158 goto err;
1159 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1160 goto err;
1161 kop.crk_iparams = 6;
1162
1163 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1164 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1165 printf("OCF asym process failed, running in Software\n");
1166 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1167
1168 } else if (ECANCELED == kop.crk_status) {
1169 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1170 printf("OCF hardware operation cancelled. Running in Software\n");
1171 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1172 }
1173 /* else cryptodev operation worked ok ==> ret = 1*/
1174
1175 err:
1176 zapparams(&kop);
1177 return (ret);
1178 }
1179
1180 static RSA_METHOD cryptodev_rsa = {
1181 "cryptodev RSA method",
1182 NULL, /* rsa_pub_enc */
1183 NULL, /* rsa_pub_dec */
1184 NULL, /* rsa_priv_enc */
1185 NULL, /* rsa_priv_dec */
1186 NULL,
1187 NULL,
1188 NULL, /* init */
1189 NULL, /* finish */
1190 0, /* flags */
1191 NULL, /* app_data */
1192 NULL, /* rsa_sign */
1193 NULL /* rsa_verify */
1194 };
1195
1196 static int
1197 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1198 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1199 {
1200 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1201 }
1202
1203 static int
1204 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1205 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1206 BN_CTX *ctx, BN_MONT_CTX *mont)
1207 {
1208 BIGNUM t2;
1209 int ret = 0;
1210
1211 BN_init(&t2);
1212
1213 /* v = ( g^u1 * y^u2 mod p ) mod q */
1214 /* let t1 = g ^ u1 mod p */
1215 ret = 0;
1216
1217 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1218 goto err;
1219
1220 /* let t2 = y ^ u2 mod p */
1221 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1222 goto err;
1223 /* let u1 = t1 * t2 mod p */
1224 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1225 goto err;
1226
1227 BN_copy(t1,u1);
1228
1229 ret = 1;
1230 err:
1231 BN_free(&t2);
1232 return(ret);
1233 }
1234
1235 static DSA_SIG *
1236 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1237 {
1238 struct crypt_kop kop;
1239 BIGNUM *r = NULL, *s = NULL;
1240 DSA_SIG *dsaret = NULL;
1241
1242 if ((r = BN_new()) == NULL)
1243 goto err;
1244 if ((s = BN_new()) == NULL) {
1245 BN_free(r);
1246 goto err;
1247 }
1248
1249 memset(&kop, 0, sizeof kop);
1250 kop.crk_op = CRK_DSA_SIGN;
1251
1252 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1253 kop.crk_param[0].crp_p = (caddr_t)dgst;
1254 kop.crk_param[0].crp_nbits = dlen * 8;
1255 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1256 goto err;
1257 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1258 goto err;
1259 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1260 goto err;
1261 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1262 goto err;
1263 kop.crk_iparams = 5;
1264
1265 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1266 BN_num_bytes(dsa->q), s) == 0) {
1267 dsaret = DSA_SIG_new();
1268 dsaret->r = r;
1269 dsaret->s = s;
1270 } else {
1271 const DSA_METHOD *meth = DSA_OpenSSL();
1272 BN_free(r);
1273 BN_free(s);
1274 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1275 }
1276 err:
1277 kop.crk_param[0].crp_p = NULL;
1278 zapparams(&kop);
1279 return (dsaret);
1280 }
1281
1282 static int
1283 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1284 DSA_SIG *sig, DSA *dsa)
1285 {
1286 struct crypt_kop kop;
1287 int dsaret = 1;
1288
1289 memset(&kop, 0, sizeof kop);
1290 kop.crk_op = CRK_DSA_VERIFY;
1291
1292 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1293 kop.crk_param[0].crp_p = (caddr_t)dgst;
1294 kop.crk_param[0].crp_nbits = dlen * 8;
1295 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1296 goto err;
1297 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1298 goto err;
1299 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1300 goto err;
1301 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1302 goto err;
1303 if (bn2crparam(sig->r, &kop.crk_param[5]))
1304 goto err;
1305 if (bn2crparam(sig->s, &kop.crk_param[6]))
1306 goto err;
1307 kop.crk_iparams = 7;
1308
1309 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1310 /*OCF success value is 0, if not zero, change dsaret to fail*/
1311 if(0 != kop.crk_status) dsaret = 0;
1312 } else {
1313 const DSA_METHOD *meth = DSA_OpenSSL();
1314
1315 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1316 }
1317 err:
1318 kop.crk_param[0].crp_p = NULL;
1319 zapparams(&kop);
1320 return (dsaret);
1321 }
1322
1323 static DSA_METHOD cryptodev_dsa = {
1324 "cryptodev DSA method",
1325 NULL,
1326 NULL, /* dsa_sign_setup */
1327 NULL,
1328 NULL, /* dsa_mod_exp */
1329 NULL,
1330 NULL, /* init */
1331 NULL, /* finish */
1332 0, /* flags */
1333 NULL /* app_data */
1334 };
1335
1336 static int
1337 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1338 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1339 BN_MONT_CTX *m_ctx)
1340 {
1341 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1342 }
1343
1344 static int
1345 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1346 {
1347 struct crypt_kop kop;
1348 int dhret = 1;
1349 int fd, keylen;
1350
1351 if ((fd = get_asym_dev_crypto()) < 0) {
1352 const DH_METHOD *meth = DH_OpenSSL();
1353
1354 return ((meth->compute_key)(key, pub_key, dh));
1355 }
1356
1357 keylen = BN_num_bits(dh->p);
1358
1359 memset(&kop, 0, sizeof kop);
1360 kop.crk_op = CRK_DH_COMPUTE_KEY;
1361
1362 /* inputs: dh->priv_key pub_key dh->p key */
1363 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1364 goto err;
1365 if (bn2crparam(pub_key, &kop.crk_param[1]))
1366 goto err;
1367 if (bn2crparam(dh->p, &kop.crk_param[2]))
1368 goto err;
1369 kop.crk_iparams = 3;
1370
1371 kop.crk_param[3].crp_p = (caddr_t) key;
1372 kop.crk_param[3].crp_nbits = keylen * 8;
1373 kop.crk_oparams = 1;
1374
1375 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1376 const DH_METHOD *meth = DH_OpenSSL();
1377
1378 dhret = (meth->compute_key)(key, pub_key, dh);
1379 }
1380 err:
1381 kop.crk_param[3].crp_p = NULL;
1382 zapparams(&kop);
1383 return (dhret);
1384 }
1385
1386 static DH_METHOD cryptodev_dh = {
1387 "cryptodev DH method",
1388 NULL, /* cryptodev_dh_generate_key */
1389 NULL,
1390 NULL,
1391 NULL,
1392 NULL,
1393 0, /* flags */
1394 NULL /* app_data */
1395 };
1396
1397 /*
1398 * ctrl right now is just a wrapper that doesn't do much
1399 * but I expect we'll want some options soon.
1400 */
1401 static int
1402 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1403 {
1404 #ifdef HAVE_SYSLOG_R
1405 struct syslog_data sd = SYSLOG_DATA_INIT;
1406 #endif
1407
1408 switch (cmd) {
1409 default:
1410 #ifdef HAVE_SYSLOG_R
1411 syslog_r(LOG_ERR, &sd,
1412 "cryptodev_ctrl: unknown command %d", cmd);
1413 #else
1414 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1415 #endif
1416 break;
1417 }
1418 return (1);
1419 }
1420
1421 void
1422 ENGINE_load_cryptodev(void)
1423 {
1424 ENGINE *engine = ENGINE_new();
1425 int fd;
1426
1427 if (engine == NULL)
1428 return;
1429 if ((fd = get_dev_crypto()) < 0) {
1430 ENGINE_free(engine);
1431 return;
1432 }
1433
1434 /*
1435 * find out what asymmetric crypto algorithms we support
1436 */
1437 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1438 put_dev_crypto(fd);
1439 ENGINE_free(engine);
1440 return;
1441 }
1442 put_dev_crypto(fd);
1443
1444 if (!ENGINE_set_id(engine, "cryptodev") ||
1445 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1446 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1447 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1448 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1449 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1450 ENGINE_free(engine);
1451 return;
1452 }
1453
1454 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1455 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1456
1457 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1458 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1459 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1460 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1461 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1462 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1463 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1464 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1465 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1466 cryptodev_rsa.rsa_mod_exp =
1467 cryptodev_rsa_mod_exp;
1468 else
1469 cryptodev_rsa.rsa_mod_exp =
1470 cryptodev_rsa_nocrt_mod_exp;
1471 }
1472 }
1473
1474 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1475 const DSA_METHOD *meth = DSA_OpenSSL();
1476
1477 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1478 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1479 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1480 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1481 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1482 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1483 }
1484 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1485 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1486 }
1487
1488 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1489 const DH_METHOD *dh_meth = DH_OpenSSL();
1490
1491 cryptodev_dh.generate_key = dh_meth->generate_key;
1492 cryptodev_dh.compute_key = dh_meth->compute_key;
1493 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1494 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1495 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1496 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1497 cryptodev_dh.compute_key =
1498 cryptodev_dh_compute_key;
1499 }
1500 }
1501
1502 ENGINE_add(engine);
1503 ENGINE_free(engine);
1504 ERR_clear_error();
1505 }
1506
1507 #endif /* HAVE_CRYPTODEV */