Use the COP_FLAG_UPDATE/FINAL flags. Patch by Glenn Schmottlach.
[cryptodev-linux.git] / extras / eng_cryptodev.c
blobb3faa07a4b563c23125ef446662bf7242011755b
1 /*
2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3 * Copyright (c) 2002 Theo de Raadt
4 * Copyright (c) 2002 Markus Friedl
5 * Copyright (c) 2012 Nikos Mavrogiannopoulos
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <openssl/objects.h>
31 #include <openssl/engine.h>
32 #include <openssl/evp.h>
33 #include <openssl/bn.h>
35 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
36 (defined(OpenBSD) || defined(__FreeBSD__))
37 #include <sys/param.h>
38 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
39 # define HAVE_CRYPTODEV
40 # endif
41 # if (OpenBSD >= 200110)
42 # define HAVE_SYSLOG_R
43 # endif
44 #endif
46 #ifndef HAVE_CRYPTODEV
48 void
49 ENGINE_load_cryptodev(void)
51 /* This is a NOP on platforms without /dev/crypto */
52 return;
55 #else
57 #include <sys/types.h>
58 #include <crypto/cryptodev.h>
59 #include <crypto/dh/dh.h>
60 #include <crypto/dsa/dsa.h>
61 #include <crypto/err/err.h>
62 #include <crypto/rsa/rsa.h>
63 #include <sys/ioctl.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <stdarg.h>
69 #include <syslog.h>
70 #include <errno.h>
71 #include <string.h>
73 struct dev_crypto_state {
74 struct session_op d_sess;
75 int d_fd;
77 #ifdef USE_CRYPTODEV_DIGESTS
78 unsigned char digest_res[HASH_MAX_LEN];
79 char *mac_data;
80 int mac_len;
81 #endif
84 static u_int32_t cryptodev_asymfeat = 0;
86 static int get_asym_dev_crypto(void);
87 static int open_dev_crypto(void);
88 static int get_dev_crypto(void);
89 static int get_cryptodev_ciphers(const int **cnids);
90 #ifdef USE_CRYPTODEV_DIGESTS
91 static int get_cryptodev_digests(const int **cnids);
92 #endif
93 static int cryptodev_usable_ciphers(const int **nids);
94 static int cryptodev_usable_digests(const int **nids);
95 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
96 const unsigned char *in, size_t inl);
97 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
98 const unsigned char *iv, int enc);
99 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
100 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
101 const int **nids, int nid);
102 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
103 const int **nids, int nid);
104 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
105 static int crparam2bn(struct crparam *crp, BIGNUM *a);
106 static void zapparams(struct crypt_kop *kop);
107 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
108 int slen, BIGNUM *s);
110 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
111 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
112 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
113 RSA *rsa, BN_CTX *ctx);
114 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
115 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
116 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
117 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
118 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
119 BN_CTX *ctx, BN_MONT_CTX *mont);
120 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
121 int dlen, DSA *dsa);
122 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
123 DSA_SIG *sig, DSA *dsa);
124 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
125 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
126 BN_MONT_CTX *m_ctx);
127 static int cryptodev_dh_compute_key(unsigned char *key,
128 const BIGNUM *pub_key, DH *dh);
129 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
130 void (*f)(void));
131 void ENGINE_load_cryptodev(void);
133 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
134 { 0, NULL, NULL, 0 }
137 static struct {
138 int id;
139 int nid;
140 int ivmax;
141 int keylen;
142 } ciphers[] = {
143 { CRYPTO_ARC4, NID_rc4, 0, 16, },
144 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
145 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
146 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
147 { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
148 { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
149 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
150 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
151 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
152 { 0, NID_undef, 0, 0, },
155 #ifdef USE_CRYPTODEV_DIGESTS
156 static struct {
157 int id;
158 int nid;
159 int digestlen;
160 } digests[] = {
161 #if 0
162 /* HMAC is not supported */
163 { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
164 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
165 { CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32},
166 { CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48},
167 { CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64},
168 #endif
169 { CRYPTO_MD5, NID_md5, 16},
170 { CRYPTO_SHA1, NID_sha1, 20},
171 { CRYPTO_SHA2_256, NID_sha256, 32},
172 { CRYPTO_SHA2_384, NID_sha384, 48},
173 { CRYPTO_SHA2_512, NID_sha512, 64},
174 { 0, NID_undef, 0},
176 #endif
179 * Return a fd if /dev/crypto seems usable, 0 otherwise.
181 static int
182 open_dev_crypto(void)
184 static int fd = -1;
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);
196 return (fd);
199 static int
200 get_dev_crypto(void)
202 int fd, retfd;
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);
210 /* close on exec */
211 if (fcntl(retfd, F_SETFD, 1) == -1) {
212 close(retfd);
213 return (-1);
215 #else
216 retfd = fd;
217 #endif
218 return (retfd);
221 static void put_dev_crypto(int fd)
223 #ifndef CRIOGET_NOT_NEEDED
224 close(fd);
225 #endif
228 /* Caching version for asym operations */
229 static int
230 get_asym_dev_crypto(void)
232 static int fd = -1;
234 if (fd == -1)
235 fd = get_dev_crypto();
236 return fd;
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
245 static int
246 get_cryptodev_ciphers(const int **cnids)
248 static int nids[CRYPTO_ALGORITHM_MAX];
249 struct session_op sess;
250 int fd, i, count = 0;
251 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
253 if ((fd = get_dev_crypto()) < 0) {
254 *cnids = NULL;
255 return (0);
257 memset(&sess, 0, sizeof(sess));
258 sess.key = (void*)fake_key;
260 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
261 if (ciphers[i].nid == NID_undef)
262 continue;
263 sess.cipher = ciphers[i].id;
264 sess.keylen = ciphers[i].keylen;
265 sess.mac = 0;
266 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
267 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
268 nids[count++] = ciphers[i].nid;
270 put_dev_crypto(fd);
272 if (count > 0)
273 *cnids = nids;
274 else
275 *cnids = NULL;
276 return (count);
279 #ifdef USE_CRYPTODEV_DIGESTS
281 * Find out what digests /dev/crypto will let us have a session for.
282 * XXX note, that some of these openssl doesn't deal with yet!
283 * returning them here is harmless, as long as we return NULL
284 * when asked for a handler in the cryptodev_engine_digests routine
286 static int
287 get_cryptodev_digests(const int **cnids)
289 static int nids[CRYPTO_ALGORITHM_MAX];
290 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
291 struct session_op sess;
292 int fd, i, count = 0;
294 if ((fd = get_dev_crypto()) < 0) {
295 *cnids = NULL;
296 return (0);
298 memset(&sess, 0, sizeof(sess));
299 sess.mackey = fake_key;
300 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
301 if (digests[i].nid == NID_undef)
302 continue;
303 sess.mac = digests[i].id;
304 sess.mackeylen = 8;
305 sess.cipher = 0;
306 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
307 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
308 nids[count++] = digests[i].nid;
310 put_dev_crypto(fd);
312 if (count > 0)
313 *cnids = nids;
314 else
315 *cnids = NULL;
316 return (count);
318 #endif /* 0 */
321 * Find the useable ciphers|digests from dev/crypto - this is the first
322 * thing called by the engine init crud which determines what it
323 * can use for ciphers from this engine. We want to return
324 * only what we can do, anythine else is handled by software.
326 * If we can't initialize the device to do anything useful for
327 * any reason, we want to return a NULL array, and 0 length,
328 * which forces everything to be done is software. By putting
329 * the initalization of the device in here, we ensure we can
330 * use this engine as the default, and if for whatever reason
331 * /dev/crypto won't do what we want it will just be done in
332 * software
334 * This can (should) be greatly expanded to perhaps take into
335 * account speed of the device, and what we want to do.
336 * (although the disabling of particular alg's could be controlled
337 * by the device driver with sysctl's.) - this is where we
338 * want most of the decisions made about what we actually want
339 * to use from /dev/crypto.
341 static int
342 cryptodev_usable_ciphers(const int **nids)
344 return (get_cryptodev_ciphers(nids));
347 static int
348 cryptodev_usable_digests(const int **nids)
350 #ifdef USE_CRYPTODEV_DIGESTS
351 return (get_cryptodev_digests(nids));
352 #else
354 * XXXX just disable all digests for now, because it sucks.
355 * we need a better way to decide this - i.e. I may not
356 * want digests on slow cards like hifn on fast machines,
357 * but might want them on slow or loaded machines, etc.
358 * will also want them when using crypto cards that don't
359 * suck moose gonads - would be nice to be able to decide something
360 * as reasonable default without having hackery that's card dependent.
361 * of course, the default should probably be just do everything,
362 * with perhaps a sysctl to turn algoritms off (or have them off
363 * by default) on cards that generally suck like the hifn.
365 *nids = NULL;
366 return (0);
367 #endif
370 static int
371 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
372 const unsigned char *in, size_t inl)
374 struct crypt_op cryp;
375 struct dev_crypto_state *state = ctx->cipher_data;
376 struct session_op *sess = &state->d_sess;
377 const void *iiv;
378 unsigned char save_iv[EVP_MAX_IV_LENGTH];
380 if (state->d_fd < 0)
381 return (0);
382 if (!inl)
383 return (1);
384 if ((inl % ctx->cipher->block_size) != 0)
385 return (0);
387 memset(&cryp, 0, sizeof(cryp));
389 cryp.ses = sess->ses;
390 cryp.flags = 0;
391 cryp.len = inl;
392 cryp.src = (void*) in;
393 cryp.dst = (void*) out;
394 cryp.mac = 0;
396 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
398 if (ctx->cipher->iv_len) {
399 cryp.iv = (void*) ctx->iv;
400 if (!ctx->encrypt) {
401 iiv = in + inl - ctx->cipher->iv_len;
402 memcpy(save_iv, iiv, ctx->cipher->iv_len);
404 } else
405 cryp.iv = NULL;
407 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
408 /* XXX need better errror handling
409 * this can fail for a number of different reasons.
411 return (0);
414 if (ctx->cipher->iv_len) {
415 if (ctx->encrypt)
416 iiv = out + inl - ctx->cipher->iv_len;
417 else
418 iiv = save_iv;
419 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
421 return (1);
424 static int
425 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
426 const unsigned char *iv, int enc)
428 struct dev_crypto_state *state = ctx->cipher_data;
429 struct session_op *sess = &state->d_sess;
430 int cipher = -1, i;
432 for (i = 0; ciphers[i].id; i++)
433 if (ctx->cipher->nid == ciphers[i].nid &&
434 ctx->cipher->iv_len <= ciphers[i].ivmax &&
435 ctx->key_len == ciphers[i].keylen) {
436 cipher = ciphers[i].id;
437 break;
440 if (!ciphers[i].id) {
441 state->d_fd = -1;
442 return (0);
445 memset(sess, 0, sizeof(struct session_op));
447 if ((state->d_fd = get_dev_crypto()) < 0)
448 return (0);
450 sess->key = (void*)key;
451 sess->keylen = ctx->key_len;
452 sess->cipher = cipher;
454 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
455 put_dev_crypto(state->d_fd);
456 state->d_fd = -1;
457 return (0);
459 return (1);
463 * free anything we allocated earlier when initting a
464 * session, and close the session.
466 static int
467 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
469 int ret = 0;
470 struct dev_crypto_state *state = ctx->cipher_data;
471 struct session_op *sess = &state->d_sess;
473 if (state->d_fd < 0)
474 return (0);
476 /* XXX if this ioctl fails, someting's wrong. the invoker
477 * may have called us with a bogus ctx, or we could
478 * have a device that for whatever reason just doesn't
479 * want to play ball - it's not clear what's right
480 * here - should this be an error? should it just
481 * increase a counter, hmm. For right now, we return
482 * 0 - I don't believe that to be "right". we could
483 * call the gorpy openssl lib error handlers that
484 * print messages to users of the library. hmm..
487 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
488 ret = 0;
489 } else {
490 ret = 1;
492 put_dev_crypto(state->d_fd);
493 state->d_fd = -1;
495 return (ret);
499 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
500 * gets called when libcrypto requests a cipher NID.
503 /* RC4 */
504 const EVP_CIPHER cryptodev_rc4 = {
505 NID_rc4,
506 1, 16, 0,
507 EVP_CIPH_VARIABLE_LENGTH,
508 cryptodev_init_key,
509 cryptodev_cipher,
510 cryptodev_cleanup,
511 sizeof(struct dev_crypto_state),
512 NULL,
513 NULL,
514 NULL
517 /* DES CBC EVP */
518 const EVP_CIPHER cryptodev_des_cbc = {
519 NID_des_cbc,
520 8, 8, 8,
521 EVP_CIPH_CBC_MODE,
522 cryptodev_init_key,
523 cryptodev_cipher,
524 cryptodev_cleanup,
525 sizeof(struct dev_crypto_state),
526 EVP_CIPHER_set_asn1_iv,
527 EVP_CIPHER_get_asn1_iv,
528 NULL
531 /* 3DES CBC EVP */
532 const EVP_CIPHER cryptodev_3des_cbc = {
533 NID_des_ede3_cbc,
534 8, 24, 8,
535 EVP_CIPH_CBC_MODE,
536 cryptodev_init_key,
537 cryptodev_cipher,
538 cryptodev_cleanup,
539 sizeof(struct dev_crypto_state),
540 EVP_CIPHER_set_asn1_iv,
541 EVP_CIPHER_get_asn1_iv,
542 NULL
545 const EVP_CIPHER cryptodev_bf_cbc = {
546 NID_bf_cbc,
547 8, 16, 8,
548 EVP_CIPH_CBC_MODE,
549 cryptodev_init_key,
550 cryptodev_cipher,
551 cryptodev_cleanup,
552 sizeof(struct dev_crypto_state),
553 EVP_CIPHER_set_asn1_iv,
554 EVP_CIPHER_get_asn1_iv,
555 NULL
558 const EVP_CIPHER cryptodev_cast_cbc = {
559 NID_cast5_cbc,
560 8, 16, 8,
561 EVP_CIPH_CBC_MODE,
562 cryptodev_init_key,
563 cryptodev_cipher,
564 cryptodev_cleanup,
565 sizeof(struct dev_crypto_state),
566 EVP_CIPHER_set_asn1_iv,
567 EVP_CIPHER_get_asn1_iv,
568 NULL
571 const EVP_CIPHER cryptodev_aes_cbc = {
572 NID_aes_128_cbc,
573 16, 16, 16,
574 EVP_CIPH_CBC_MODE,
575 cryptodev_init_key,
576 cryptodev_cipher,
577 cryptodev_cleanup,
578 sizeof(struct dev_crypto_state),
579 EVP_CIPHER_set_asn1_iv,
580 EVP_CIPHER_get_asn1_iv,
581 NULL
584 const EVP_CIPHER cryptodev_aes_192_cbc = {
585 NID_aes_192_cbc,
586 16, 24, 16,
587 EVP_CIPH_CBC_MODE,
588 cryptodev_init_key,
589 cryptodev_cipher,
590 cryptodev_cleanup,
591 sizeof(struct dev_crypto_state),
592 EVP_CIPHER_set_asn1_iv,
593 EVP_CIPHER_get_asn1_iv,
594 NULL
597 const EVP_CIPHER cryptodev_aes_256_cbc = {
598 NID_aes_256_cbc,
599 16, 32, 16,
600 EVP_CIPH_CBC_MODE,
601 cryptodev_init_key,
602 cryptodev_cipher,
603 cryptodev_cleanup,
604 sizeof(struct dev_crypto_state),
605 EVP_CIPHER_set_asn1_iv,
606 EVP_CIPHER_get_asn1_iv,
607 NULL
611 * Registered by the ENGINE when used to find out how to deal with
612 * a particular NID in the ENGINE. this says what we'll do at the
613 * top level - note, that list is restricted by what we answer with
615 static int
616 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
617 const int **nids, int nid)
619 if (!cipher)
620 return (cryptodev_usable_ciphers(nids));
622 switch (nid) {
623 case NID_rc4:
624 *cipher = &cryptodev_rc4;
625 break;
626 case NID_des_ede3_cbc:
627 *cipher = &cryptodev_3des_cbc;
628 break;
629 case NID_des_cbc:
630 *cipher = &cryptodev_des_cbc;
631 break;
632 case NID_bf_cbc:
633 *cipher = &cryptodev_bf_cbc;
634 break;
635 case NID_cast5_cbc:
636 *cipher = &cryptodev_cast_cbc;
637 break;
638 case NID_aes_128_cbc:
639 *cipher = &cryptodev_aes_cbc;
640 break;
641 case NID_aes_192_cbc:
642 *cipher = &cryptodev_aes_192_cbc;
643 break;
644 case NID_aes_256_cbc:
645 *cipher = &cryptodev_aes_256_cbc;
646 break;
647 default:
648 *cipher = NULL;
649 break;
651 return (*cipher != NULL);
655 #ifdef USE_CRYPTODEV_DIGESTS
657 /* convert digest type to cryptodev */
658 static int
659 digest_nid_to_cryptodev(int nid)
661 int i;
663 for (i = 0; digests[i].id; i++)
664 if (digests[i].nid == nid)
665 return (digests[i].id);
666 return (0);
670 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
672 struct dev_crypto_state *state = ctx->md_data;
673 struct session_op *sess = &state->d_sess;
674 int digest;
676 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
677 printf("cryptodev_digest_init: Can't get digest \n");
678 return (0);
680 memset(state, 0, sizeof(struct dev_crypto_state));
682 if ((state->d_fd = get_dev_crypto()) < 0) {
683 printf("cryptodev_digest_init: Can't get Dev \n");
684 return (0);
687 sess->mackey = NULL;
688 sess->mackeylen = 0;
689 sess->mac = digest;
691 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
692 put_dev_crypto(state->d_fd);
693 state->d_fd = -1;
694 printf("cryptodev_digest_init: Open session failed\n");
695 return (0);
698 return (1);
701 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
702 size_t count)
704 struct dev_crypto_state *state = ctx->md_data;
705 struct crypt_op cryp;
706 struct session_op *sess = &state->d_sess;
708 if (!data || state->d_fd < 0) {
709 printf("cryptodev_digest_update: illegal inputs \n");
710 return (0);
713 if (!count) {
714 return (1);
717 /* If an application wants to compute the hash in one big chunk (one-shot mode)
718 * then tack the data onto the end of the buffer (and grow as necessary).
720 if ( ctx->flags & EVP_MD_CTX_FLAG_ONESHOT ) {
721 state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
723 if (!state->mac_data) {
724 printf("cryptodev_digest_update: realloc failed\n");
725 return (0);
728 memcpy(state->mac_data + state->mac_len, data, count);
729 state->mac_len += count;
731 return (1);
734 memset(&cryp, 0, sizeof(cryp));
736 cryp.ses = sess->ses;
737 #ifdef COP_FLAG_UPDATE
738 cryp.flags = COP_FLAG_UPDATE;
739 #endif
740 cryp.len = count;
741 cryp.src = (void*) data;
742 cryp.dst = NULL;
743 cryp.mac = (void*) state->digest_res;
744 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
745 printf("cryptodev_digest_update: digest failed\n");
746 return (0);
748 return (1);
752 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
754 struct crypt_op cryp;
755 struct dev_crypto_state *state = ctx->md_data;
756 struct session_op *sess = &state->d_sess;
758 if (!md || state->d_fd < 0) {
759 printf("cryptodev_digest_final: illegal input\n");
760 return(0);
763 memset(&cryp, 0, sizeof(cryp));
764 cryp.ses = sess->ses;
765 #ifdef COP_FLAG_FINAL
766 cryp.flags = COP_FLAG_FINAL;
767 #endif
768 cryp.len = state->mac_len;
769 cryp.src = state->mac_data;
770 cryp.dst = NULL;
771 cryp.mac = (void*)md;
772 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
773 printf("cryptodev_digest_final: digest failed\n");
774 return (0);
777 return 1;
781 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
783 int ret = 1;
784 struct dev_crypto_state *state = ctx->md_data;
785 struct session_op *sess = &state->d_sess;
787 if (state == NULL)
788 return 0;
790 if (state->d_fd < 0) {
791 printf("cryptodev_digest_cleanup: illegal input\n");
792 return (0);
795 if (state->mac_data) {
796 OPENSSL_free(state->mac_data);
797 state->mac_data = NULL;
798 state->mac_len = 0;
801 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
802 printf("cryptodev_digest_cleanup: failed to close session\n");
803 ret = 0;
804 } else {
805 ret = 1;
807 put_dev_crypto(state->d_fd);
808 state->d_fd = -1;
810 return (ret);
813 static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
815 struct dev_crypto_state *fstate = from->md_data;
816 struct dev_crypto_state *dstate = to->md_data;
817 struct session_op *sess;
818 int digest;
820 if (dstate == NULL || fstate == NULL)
821 return 1;
823 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
825 sess = &dstate->d_sess;
827 digest = digest_nid_to_cryptodev(to->digest->type);
829 sess->mackey = NULL;
830 sess->mackeylen = 0;
831 sess->mac = digest;
833 dstate->d_fd = get_dev_crypto();
835 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
836 put_dev_crypto(dstate->d_fd);
837 dstate->d_fd = -1;
838 printf("cryptodev_digest_init: Open session failed\n");
839 return (0);
842 if (fstate->mac_len != 0) {
843 if (fstate->mac_data != NULL)
845 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
846 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
847 dstate->mac_len = fstate->mac_len;
851 return 1;
855 static const EVP_MD cryptodev_sha1 = {
856 NID_sha1,
857 NID_sha1WithRSAEncryption,
858 SHA_DIGEST_LENGTH,
859 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
860 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
861 EVP_MD_FLAG_DIGALGID_ABSENT|
862 #endif
863 EVP_MD_FLAG_ONESHOT,
864 cryptodev_digest_init,
865 cryptodev_digest_update,
866 cryptodev_digest_final,
867 cryptodev_digest_copy,
868 cryptodev_digest_cleanup,
869 EVP_PKEY_RSA_method,
870 SHA_CBLOCK,
871 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
874 static const EVP_MD cryptodev_sha256 = {
875 NID_sha256,
876 NID_sha256WithRSAEncryption,
877 SHA256_DIGEST_LENGTH,
878 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
879 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
880 EVP_MD_FLAG_DIGALGID_ABSENT|
881 #endif
882 EVP_MD_FLAG_ONESHOT,
883 cryptodev_digest_init,
884 cryptodev_digest_update,
885 cryptodev_digest_final,
886 cryptodev_digest_copy,
887 cryptodev_digest_cleanup,
888 EVP_PKEY_RSA_method,
889 SHA256_CBLOCK,
890 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
893 static const EVP_MD cryptodev_sha384 = {
894 NID_sha384,
895 NID_sha384WithRSAEncryption,
896 SHA384_DIGEST_LENGTH,
897 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
898 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
899 EVP_MD_FLAG_DIGALGID_ABSENT|
900 #endif
901 EVP_MD_FLAG_ONESHOT,
902 cryptodev_digest_init,
903 cryptodev_digest_update,
904 cryptodev_digest_final,
905 cryptodev_digest_copy,
906 cryptodev_digest_cleanup,
907 EVP_PKEY_RSA_method,
908 SHA512_CBLOCK,
909 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
912 static const EVP_MD cryptodev_sha512 = {
913 NID_sha512,
914 NID_sha512WithRSAEncryption,
915 SHA512_DIGEST_LENGTH,
916 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
917 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
918 EVP_MD_FLAG_DIGALGID_ABSENT|
919 #endif
920 EVP_MD_FLAG_ONESHOT,
921 cryptodev_digest_init,
922 cryptodev_digest_update,
923 cryptodev_digest_final,
924 cryptodev_digest_copy,
925 cryptodev_digest_cleanup,
926 EVP_PKEY_RSA_method,
927 SHA512_CBLOCK,
928 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
931 static const EVP_MD cryptodev_md5 = {
932 NID_md5,
933 NID_md5WithRSAEncryption,
934 16 /* MD5_DIGEST_LENGTH */,
935 #if defined(EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) && defined(EVP_MD_FLAG_DIGALGID_ABSENT)
936 EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|
937 EVP_MD_FLAG_DIGALGID_ABSENT|
938 #endif
939 EVP_MD_FLAG_ONESHOT,
940 cryptodev_digest_init,
941 cryptodev_digest_update,
942 cryptodev_digest_final,
943 cryptodev_digest_copy,
944 cryptodev_digest_cleanup,
945 EVP_PKEY_RSA_method,
946 64 /* MD5_CBLOCK */,
947 sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
950 #endif /* USE_CRYPTODEV_DIGESTS */
953 static int
954 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
955 const int **nids, int nid)
957 if (!digest)
958 return (cryptodev_usable_digests(nids));
960 switch (nid) {
961 #ifdef USE_CRYPTODEV_DIGESTS
962 case NID_md5:
963 *digest = &cryptodev_md5;
964 break;
965 case NID_sha1:
966 *digest = &cryptodev_sha1;
967 break;
968 case NID_sha256:
969 *digest = &cryptodev_sha256;
970 break;
971 case NID_sha384:
972 *digest = &cryptodev_sha384;
973 break;
974 case NID_sha512:
975 *digest = &cryptodev_sha512;
976 break;
977 default:
978 #endif /* USE_CRYPTODEV_DIGESTS */
979 *digest = NULL;
980 break;
982 return (*digest != NULL);
986 * Convert a BIGNUM to the representation that /dev/crypto needs.
987 * Upon completion of use, the caller is responsible for freeing
988 * crp->crp_p.
990 static int
991 bn2crparam(const BIGNUM *a, struct crparam *crp)
993 int i, j, k;
994 ssize_t bytes, bits;
995 u_char *b;
997 crp->crp_p = NULL;
998 crp->crp_nbits = 0;
1000 bits = BN_num_bits(a);
1001 bytes = (bits + 7) / 8;
1003 b = malloc(bytes);
1004 if (b == NULL)
1005 return (1);
1006 memset(b, 0, bytes);
1008 crp->crp_p = (void*) b;
1009 crp->crp_nbits = bits;
1011 for (i = 0, j = 0; i < a->top; i++) {
1012 for (k = 0; k < BN_BITS2 / 8; k++) {
1013 if ((j + k) >= bytes)
1014 return (0);
1015 b[j + k] = a->d[i] >> (k * 8);
1017 j += BN_BITS2 / 8;
1019 return (0);
1022 /* Convert a /dev/crypto parameter to a BIGNUM */
1023 static int
1024 crparam2bn(struct crparam *crp, BIGNUM *a)
1026 u_int8_t *pd;
1027 int i, bytes;
1029 bytes = (crp->crp_nbits + 7) / 8;
1031 if (bytes == 0)
1032 return (-1);
1034 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1035 return (-1);
1037 for (i = 0; i < bytes; i++)
1038 pd[i] = crp->crp_p[bytes - i - 1];
1040 BN_bin2bn(pd, bytes, a);
1041 free(pd);
1043 return (0);
1046 static void
1047 zapparams(struct crypt_kop *kop)
1049 int i;
1051 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1052 if (kop->crk_param[i].crp_p)
1053 free(kop->crk_param[i].crp_p);
1054 kop->crk_param[i].crp_p = NULL;
1055 kop->crk_param[i].crp_nbits = 0;
1059 static int
1060 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1062 int fd, ret = -1;
1064 if ((fd = get_asym_dev_crypto()) < 0)
1065 return (ret);
1067 if (r) {
1068 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1069 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1070 kop->crk_oparams++;
1072 if (s) {
1073 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1074 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1075 kop->crk_oparams++;
1078 if (ioctl(fd, CIOCKEY, kop) == 0) {
1079 if (r)
1080 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1081 if (s)
1082 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1083 ret = 0;
1086 return (ret);
1089 static int
1090 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1091 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1093 struct crypt_kop kop;
1094 int ret = 1;
1096 /* Currently, we know we can do mod exp iff we can do any
1097 * asymmetric operations at all.
1099 if (cryptodev_asymfeat == 0) {
1100 ret = BN_mod_exp(r, a, p, m, ctx);
1101 return (ret);
1104 memset(&kop, 0, sizeof kop);
1105 kop.crk_op = CRK_MOD_EXP;
1107 /* inputs: a^p % m */
1108 if (bn2crparam(a, &kop.crk_param[0]))
1109 goto err;
1110 if (bn2crparam(p, &kop.crk_param[1]))
1111 goto err;
1112 if (bn2crparam(m, &kop.crk_param[2]))
1113 goto err;
1114 kop.crk_iparams = 3;
1116 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1117 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1118 printf("OCF asym process failed, Running in software\n");
1119 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1121 } else if (ECANCELED == kop.crk_status) {
1122 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1123 printf("OCF hardware operation cancelled. Running in Software\n");
1124 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1126 /* else cryptodev operation worked ok ==> ret = 1*/
1128 err:
1129 zapparams(&kop);
1130 return (ret);
1133 static int
1134 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1136 int r;
1137 ctx = BN_CTX_new();
1138 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1139 BN_CTX_free(ctx);
1140 return (r);
1143 static int
1144 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1146 struct crypt_kop kop;
1147 int ret = 1;
1149 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1150 /* XXX 0 means failure?? */
1151 return (0);
1154 memset(&kop, 0, sizeof kop);
1155 kop.crk_op = CRK_MOD_EXP_CRT;
1156 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1157 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1158 goto err;
1159 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1160 goto err;
1161 if (bn2crparam(I, &kop.crk_param[2]))
1162 goto err;
1163 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1164 goto err;
1165 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1166 goto err;
1167 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1168 goto err;
1169 kop.crk_iparams = 6;
1171 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1172 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1173 printf("OCF asym process failed, running in Software\n");
1174 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1176 } else if (ECANCELED == kop.crk_status) {
1177 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1178 printf("OCF hardware operation cancelled. Running in Software\n");
1179 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1181 /* else cryptodev operation worked ok ==> ret = 1*/
1183 err:
1184 zapparams(&kop);
1185 return (ret);
1188 static RSA_METHOD cryptodev_rsa = {
1189 "cryptodev RSA method",
1190 NULL, /* rsa_pub_enc */
1191 NULL, /* rsa_pub_dec */
1192 NULL, /* rsa_priv_enc */
1193 NULL, /* rsa_priv_dec */
1194 NULL,
1195 NULL,
1196 NULL, /* init */
1197 NULL, /* finish */
1198 0, /* flags */
1199 NULL, /* app_data */
1200 NULL, /* rsa_sign */
1201 NULL /* rsa_verify */
1204 static int
1205 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1206 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1208 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1211 static int
1212 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1213 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1214 BN_CTX *ctx, BN_MONT_CTX *mont)
1216 BIGNUM t2;
1217 int ret = 0;
1219 BN_init(&t2);
1221 /* v = ( g^u1 * y^u2 mod p ) mod q */
1222 /* let t1 = g ^ u1 mod p */
1223 ret = 0;
1225 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1226 goto err;
1228 /* let t2 = y ^ u2 mod p */
1229 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1230 goto err;
1231 /* let u1 = t1 * t2 mod p */
1232 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1233 goto err;
1235 BN_copy(t1,u1);
1237 ret = 1;
1238 err:
1239 BN_free(&t2);
1240 return(ret);
1243 static DSA_SIG *
1244 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1246 struct crypt_kop kop;
1247 BIGNUM *r = NULL, *s = NULL;
1248 DSA_SIG *dsaret = NULL;
1250 if ((r = BN_new()) == NULL)
1251 goto err;
1252 if ((s = BN_new()) == NULL) {
1253 BN_free(r);
1254 goto err;
1257 memset(&kop, 0, sizeof kop);
1258 kop.crk_op = CRK_DSA_SIGN;
1260 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1261 kop.crk_param[0].crp_p = (void*)dgst;
1262 kop.crk_param[0].crp_nbits = dlen * 8;
1263 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1264 goto err;
1265 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1266 goto err;
1267 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1268 goto err;
1269 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1270 goto err;
1271 kop.crk_iparams = 5;
1273 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1274 BN_num_bytes(dsa->q), s) == 0) {
1275 dsaret = DSA_SIG_new();
1276 dsaret->r = r;
1277 dsaret->s = s;
1278 } else {
1279 const DSA_METHOD *meth = DSA_OpenSSL();
1280 BN_free(r);
1281 BN_free(s);
1282 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1284 err:
1285 kop.crk_param[0].crp_p = NULL;
1286 zapparams(&kop);
1287 return (dsaret);
1290 static int
1291 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1292 DSA_SIG *sig, DSA *dsa)
1294 struct crypt_kop kop;
1295 int dsaret = 1;
1297 memset(&kop, 0, sizeof kop);
1298 kop.crk_op = CRK_DSA_VERIFY;
1300 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1301 kop.crk_param[0].crp_p = (void*)dgst;
1302 kop.crk_param[0].crp_nbits = dlen * 8;
1303 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1304 goto err;
1305 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1306 goto err;
1307 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1308 goto err;
1309 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1310 goto err;
1311 if (bn2crparam(sig->r, &kop.crk_param[5]))
1312 goto err;
1313 if (bn2crparam(sig->s, &kop.crk_param[6]))
1314 goto err;
1315 kop.crk_iparams = 7;
1317 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1318 /*OCF success value is 0, if not zero, change dsaret to fail*/
1319 if(0 != kop.crk_status) dsaret = 0;
1320 } else {
1321 const DSA_METHOD *meth = DSA_OpenSSL();
1323 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1325 err:
1326 kop.crk_param[0].crp_p = NULL;
1327 zapparams(&kop);
1328 return (dsaret);
1331 static DSA_METHOD cryptodev_dsa = {
1332 "cryptodev DSA method",
1333 NULL,
1334 NULL, /* dsa_sign_setup */
1335 NULL,
1336 NULL, /* dsa_mod_exp */
1337 NULL,
1338 NULL, /* init */
1339 NULL, /* finish */
1340 0, /* flags */
1341 NULL /* app_data */
1344 static int
1345 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1346 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1347 BN_MONT_CTX *m_ctx)
1349 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1352 static int
1353 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1355 struct crypt_kop kop;
1356 int dhret = 1;
1357 int fd, keylen;
1359 if ((fd = get_asym_dev_crypto()) < 0) {
1360 const DH_METHOD *meth = DH_OpenSSL();
1362 return ((meth->compute_key)(key, pub_key, dh));
1365 keylen = BN_num_bits(dh->p);
1367 memset(&kop, 0, sizeof kop);
1368 kop.crk_op = CRK_DH_COMPUTE_KEY;
1370 /* inputs: dh->priv_key pub_key dh->p key */
1371 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1372 goto err;
1373 if (bn2crparam(pub_key, &kop.crk_param[1]))
1374 goto err;
1375 if (bn2crparam(dh->p, &kop.crk_param[2]))
1376 goto err;
1377 kop.crk_iparams = 3;
1379 kop.crk_param[3].crp_p = (void*) key;
1380 kop.crk_param[3].crp_nbits = keylen * 8;
1381 kop.crk_oparams = 1;
1383 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1384 const DH_METHOD *meth = DH_OpenSSL();
1386 dhret = (meth->compute_key)(key, pub_key, dh);
1388 err:
1389 kop.crk_param[3].crp_p = NULL;
1390 zapparams(&kop);
1391 return (dhret);
1394 static DH_METHOD cryptodev_dh = {
1395 "cryptodev DH method",
1396 NULL, /* cryptodev_dh_generate_key */
1397 NULL,
1398 NULL,
1399 NULL,
1400 NULL,
1401 0, /* flags */
1402 NULL /* app_data */
1406 * ctrl right now is just a wrapper that doesn't do much
1407 * but I expect we'll want some options soon.
1409 static int
1410 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1412 #ifdef HAVE_SYSLOG_R
1413 struct syslog_data sd = SYSLOG_DATA_INIT;
1414 #endif
1416 switch (cmd) {
1417 default:
1418 #ifdef HAVE_SYSLOG_R
1419 syslog_r(LOG_ERR, &sd,
1420 "cryptodev_ctrl: unknown command %d", cmd);
1421 #else
1422 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1423 #endif
1424 break;
1426 return (1);
1429 void
1430 ENGINE_load_cryptodev(void)
1432 ENGINE *engine = ENGINE_new();
1433 int fd;
1435 if (engine == NULL)
1436 return;
1437 if ((fd = get_dev_crypto()) < 0) {
1438 ENGINE_free(engine);
1439 return;
1443 * find out what asymmetric crypto algorithms we support
1445 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1446 put_dev_crypto(fd);
1447 ENGINE_free(engine);
1448 return;
1450 put_dev_crypto(fd);
1452 if (!ENGINE_set_id(engine, "cryptodev") ||
1453 !ENGINE_set_name(engine, "cryptodev engine") ||
1454 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1455 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1456 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1457 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1458 ENGINE_free(engine);
1459 return;
1462 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1463 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1465 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1466 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1467 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1468 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1469 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1470 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1471 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1472 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1473 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1474 cryptodev_rsa.rsa_mod_exp =
1475 cryptodev_rsa_mod_exp;
1476 else
1477 cryptodev_rsa.rsa_mod_exp =
1478 cryptodev_rsa_nocrt_mod_exp;
1482 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1483 const DSA_METHOD *meth = DSA_OpenSSL();
1485 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1486 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1487 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1488 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1489 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1490 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1492 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1493 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1496 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1497 const DH_METHOD *dh_meth = DH_OpenSSL();
1499 cryptodev_dh.generate_key = dh_meth->generate_key;
1500 cryptodev_dh.compute_key = dh_meth->compute_key;
1501 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1502 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1503 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1504 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1505 cryptodev_dh.compute_key =
1506 cryptodev_dh_compute_key;
1510 ENGINE_add(engine);
1511 ENGINE_free(engine);
1512 ERR_clear_error();
1515 #endif /* HAVE_CRYPTODEV */