]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/engine/eng_cryptodev.c
cryptodev_digest_copy: return error if allocating dstate->mac_data 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_copy: 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 if (dstate->mac_data == NULL)
913 {
914 printf("cryptodev_digest_copy: mac_data allocation failed\n");
915 return (0);
916 }
917 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
918 dstate->mac_len = fstate->mac_len;
919 }
920 }
921
922 return 1;
923 }
924
925
926 const EVP_MD cryptodev_sha1 = {
927 NID_sha1,
928 NID_undef,
929 SHA_DIGEST_LENGTH,
930 EVP_MD_FLAG_ONESHOT,
931 cryptodev_digest_init,
932 cryptodev_digest_update,
933 cryptodev_digest_final,
934 cryptodev_digest_copy,
935 cryptodev_digest_cleanup,
936 EVP_PKEY_NULL_method,
937 SHA_CBLOCK,
938 sizeof(struct dev_crypto_state),
939 };
940
941 const EVP_MD cryptodev_md5 = {
942 NID_md5,
943 NID_undef,
944 16 /* MD5_DIGEST_LENGTH */,
945 EVP_MD_FLAG_ONESHOT,
946 cryptodev_digest_init,
947 cryptodev_digest_update,
948 cryptodev_digest_final,
949 cryptodev_digest_copy,
950 cryptodev_digest_cleanup,
951 EVP_PKEY_NULL_method,
952 64 /* MD5_CBLOCK */,
953 sizeof(struct dev_crypto_state),
954 };
955
956 #endif /* USE_CRYPTODEV_DIGESTS */
957
958
959 static int
960 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
961 const int **nids, int nid)
962 {
963 if (!digest)
964 return (cryptodev_usable_digests(nids));
965
966 switch (nid) {
967 #ifdef USE_CRYPTODEV_DIGESTS
968 case NID_md5:
969 *digest = &cryptodev_md5;
970 break;
971 case NID_sha1:
972 *digest = &cryptodev_sha1;
973 break;
974 default:
975 #endif /* USE_CRYPTODEV_DIGESTS */
976 *digest = NULL;
977 break;
978 }
979 return (*digest != NULL);
980 }
981
982 /*
983 * Convert a BIGNUM to the representation that /dev/crypto needs.
984 * Upon completion of use, the caller is responsible for freeing
985 * crp->crp_p.
986 */
987 static int
988 bn2crparam(const BIGNUM *a, struct crparam *crp)
989 {
990 int i, j, k;
991 ssize_t bytes, bits;
992 u_char *b;
993
994 crp->crp_p = NULL;
995 crp->crp_nbits = 0;
996
997 bits = BN_num_bits(a);
998 bytes = (bits + 7) / 8;
999
1000 b = malloc(bytes);
1001 if (b == NULL)
1002 return (1);
1003 memset(b, 0, bytes);
1004
1005 crp->crp_p = (caddr_t) b;
1006 crp->crp_nbits = bits;
1007
1008 for (i = 0, j = 0; i < a->top; i++) {
1009 for (k = 0; k < BN_BITS2 / 8; k++) {
1010 if ((j + k) >= bytes)
1011 return (0);
1012 b[j + k] = a->d[i] >> (k * 8);
1013 }
1014 j += BN_BITS2 / 8;
1015 }
1016 return (0);
1017 }
1018
1019 /* Convert a /dev/crypto parameter to a BIGNUM */
1020 static int
1021 crparam2bn(struct crparam *crp, BIGNUM *a)
1022 {
1023 u_int8_t *pd;
1024 int i, bytes;
1025
1026 bytes = (crp->crp_nbits + 7) / 8;
1027
1028 if (bytes == 0)
1029 return (-1);
1030
1031 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
1032 return (-1);
1033
1034 for (i = 0; i < bytes; i++)
1035 pd[i] = crp->crp_p[bytes - i - 1];
1036
1037 BN_bin2bn(pd, bytes, a);
1038 free(pd);
1039
1040 return (0);
1041 }
1042
1043 static void
1044 zapparams(struct crypt_kop *kop)
1045 {
1046 int i;
1047
1048 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1049 if (kop->crk_param[i].crp_p)
1050 free(kop->crk_param[i].crp_p);
1051 kop->crk_param[i].crp_p = NULL;
1052 kop->crk_param[i].crp_nbits = 0;
1053 }
1054 }
1055
1056 static int
1057 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1058 {
1059 int fd, ret = -1;
1060
1061 if ((fd = get_asym_dev_crypto()) < 0)
1062 return (ret);
1063
1064 if (r) {
1065 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
1066 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1067 kop->crk_oparams++;
1068 }
1069 if (s) {
1070 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
1071 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
1072 kop->crk_oparams++;
1073 }
1074
1075 if (ioctl(fd, CIOCKEY, kop) == 0) {
1076 if (r)
1077 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1078 if (s)
1079 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
1080 ret = 0;
1081 }
1082
1083 return (ret);
1084 }
1085
1086 static int
1087 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1088 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1089 {
1090 struct crypt_kop kop;
1091 int ret = 1;
1092
1093 /* Currently, we know we can do mod exp iff we can do any
1094 * asymmetric operations at all.
1095 */
1096 if (cryptodev_asymfeat == 0) {
1097 ret = BN_mod_exp(r, a, p, m, ctx);
1098 return (ret);
1099 }
1100
1101 memset(&kop, 0, sizeof kop);
1102 kop.crk_op = CRK_MOD_EXP;
1103
1104 /* inputs: a^p % m */
1105 if (bn2crparam(a, &kop.crk_param[0]))
1106 goto err;
1107 if (bn2crparam(p, &kop.crk_param[1]))
1108 goto err;
1109 if (bn2crparam(m, &kop.crk_param[2]))
1110 goto err;
1111 kop.crk_iparams = 3;
1112
1113 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1114 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1115 printf("OCF asym process failed, Running in software\n");
1116 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1117
1118 } else if (ECANCELED == kop.crk_status) {
1119 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1120 printf("OCF hardware operation cancelled. Running in Software\n");
1121 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
1122 }
1123 /* else cryptodev operation worked ok ==> ret = 1*/
1124
1125 err:
1126 zapparams(&kop);
1127 return (ret);
1128 }
1129
1130 static int
1131 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1132 {
1133 int r;
1134 ctx = BN_CTX_new();
1135 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
1136 BN_CTX_free(ctx);
1137 return (r);
1138 }
1139
1140 static int
1141 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1142 {
1143 struct crypt_kop kop;
1144 int ret = 1;
1145
1146 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1147 /* XXX 0 means failure?? */
1148 return (0);
1149 }
1150
1151 memset(&kop, 0, sizeof kop);
1152 kop.crk_op = CRK_MOD_EXP_CRT;
1153 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1154 if (bn2crparam(rsa->p, &kop.crk_param[0]))
1155 goto err;
1156 if (bn2crparam(rsa->q, &kop.crk_param[1]))
1157 goto err;
1158 if (bn2crparam(I, &kop.crk_param[2]))
1159 goto err;
1160 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
1161 goto err;
1162 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
1163 goto err;
1164 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
1165 goto err;
1166 kop.crk_iparams = 6;
1167
1168 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
1169 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1170 printf("OCF asym process failed, running in Software\n");
1171 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1172
1173 } else if (ECANCELED == kop.crk_status) {
1174 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
1175 printf("OCF hardware operation cancelled. Running in Software\n");
1176 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
1177 }
1178 /* else cryptodev operation worked ok ==> ret = 1*/
1179
1180 err:
1181 zapparams(&kop);
1182 return (ret);
1183 }
1184
1185 static RSA_METHOD cryptodev_rsa = {
1186 "cryptodev RSA method",
1187 NULL, /* rsa_pub_enc */
1188 NULL, /* rsa_pub_dec */
1189 NULL, /* rsa_priv_enc */
1190 NULL, /* rsa_priv_dec */
1191 NULL,
1192 NULL,
1193 NULL, /* init */
1194 NULL, /* finish */
1195 0, /* flags */
1196 NULL, /* app_data */
1197 NULL, /* rsa_sign */
1198 NULL /* rsa_verify */
1199 };
1200
1201 static int
1202 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1203 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1204 {
1205 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1206 }
1207
1208 static int
1209 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
1210 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
1211 BN_CTX *ctx, BN_MONT_CTX *mont)
1212 {
1213 BIGNUM t2;
1214 int ret = 0;
1215
1216 BN_init(&t2);
1217
1218 /* v = ( g^u1 * y^u2 mod p ) mod q */
1219 /* let t1 = g ^ u1 mod p */
1220 ret = 0;
1221
1222 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
1223 goto err;
1224
1225 /* let t2 = y ^ u2 mod p */
1226 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
1227 goto err;
1228 /* let u1 = t1 * t2 mod p */
1229 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
1230 goto err;
1231
1232 BN_copy(t1,u1);
1233
1234 ret = 1;
1235 err:
1236 BN_free(&t2);
1237 return(ret);
1238 }
1239
1240 static DSA_SIG *
1241 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
1242 {
1243 struct crypt_kop kop;
1244 BIGNUM *r = NULL, *s = NULL;
1245 DSA_SIG *dsaret = NULL;
1246
1247 if ((r = BN_new()) == NULL)
1248 goto err;
1249 if ((s = BN_new()) == NULL) {
1250 BN_free(r);
1251 goto err;
1252 }
1253
1254 memset(&kop, 0, sizeof kop);
1255 kop.crk_op = CRK_DSA_SIGN;
1256
1257 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1258 kop.crk_param[0].crp_p = (caddr_t)dgst;
1259 kop.crk_param[0].crp_nbits = dlen * 8;
1260 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1261 goto err;
1262 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1263 goto err;
1264 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1265 goto err;
1266 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
1267 goto err;
1268 kop.crk_iparams = 5;
1269
1270 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
1271 BN_num_bytes(dsa->q), s) == 0) {
1272 dsaret = DSA_SIG_new();
1273 dsaret->r = r;
1274 dsaret->s = s;
1275 } else {
1276 const DSA_METHOD *meth = DSA_OpenSSL();
1277 BN_free(r);
1278 BN_free(s);
1279 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
1280 }
1281 err:
1282 kop.crk_param[0].crp_p = NULL;
1283 zapparams(&kop);
1284 return (dsaret);
1285 }
1286
1287 static int
1288 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1289 DSA_SIG *sig, DSA *dsa)
1290 {
1291 struct crypt_kop kop;
1292 int dsaret = 1;
1293
1294 memset(&kop, 0, sizeof kop);
1295 kop.crk_op = CRK_DSA_VERIFY;
1296
1297 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1298 kop.crk_param[0].crp_p = (caddr_t)dgst;
1299 kop.crk_param[0].crp_nbits = dlen * 8;
1300 if (bn2crparam(dsa->p, &kop.crk_param[1]))
1301 goto err;
1302 if (bn2crparam(dsa->q, &kop.crk_param[2]))
1303 goto err;
1304 if (bn2crparam(dsa->g, &kop.crk_param[3]))
1305 goto err;
1306 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
1307 goto err;
1308 if (bn2crparam(sig->r, &kop.crk_param[5]))
1309 goto err;
1310 if (bn2crparam(sig->s, &kop.crk_param[6]))
1311 goto err;
1312 kop.crk_iparams = 7;
1313
1314 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1315 /*OCF success value is 0, if not zero, change dsaret to fail*/
1316 if(0 != kop.crk_status) dsaret = 0;
1317 } else {
1318 const DSA_METHOD *meth = DSA_OpenSSL();
1319
1320 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
1321 }
1322 err:
1323 kop.crk_param[0].crp_p = NULL;
1324 zapparams(&kop);
1325 return (dsaret);
1326 }
1327
1328 static DSA_METHOD cryptodev_dsa = {
1329 "cryptodev DSA method",
1330 NULL,
1331 NULL, /* dsa_sign_setup */
1332 NULL,
1333 NULL, /* dsa_mod_exp */
1334 NULL,
1335 NULL, /* init */
1336 NULL, /* finish */
1337 0, /* flags */
1338 NULL /* app_data */
1339 };
1340
1341 static int
1342 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1343 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1344 BN_MONT_CTX *m_ctx)
1345 {
1346 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1347 }
1348
1349 static int
1350 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1351 {
1352 struct crypt_kop kop;
1353 int dhret = 1;
1354 int fd, keylen;
1355
1356 if ((fd = get_asym_dev_crypto()) < 0) {
1357 const DH_METHOD *meth = DH_OpenSSL();
1358
1359 return ((meth->compute_key)(key, pub_key, dh));
1360 }
1361
1362 keylen = BN_num_bits(dh->p);
1363
1364 memset(&kop, 0, sizeof kop);
1365 kop.crk_op = CRK_DH_COMPUTE_KEY;
1366
1367 /* inputs: dh->priv_key pub_key dh->p key */
1368 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
1369 goto err;
1370 if (bn2crparam(pub_key, &kop.crk_param[1]))
1371 goto err;
1372 if (bn2crparam(dh->p, &kop.crk_param[2]))
1373 goto err;
1374 kop.crk_iparams = 3;
1375
1376 kop.crk_param[3].crp_p = (caddr_t) key;
1377 kop.crk_param[3].crp_nbits = keylen * 8;
1378 kop.crk_oparams = 1;
1379
1380 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1381 const DH_METHOD *meth = DH_OpenSSL();
1382
1383 dhret = (meth->compute_key)(key, pub_key, dh);
1384 }
1385 err:
1386 kop.crk_param[3].crp_p = NULL;
1387 zapparams(&kop);
1388 return (dhret);
1389 }
1390
1391 static DH_METHOD cryptodev_dh = {
1392 "cryptodev DH method",
1393 NULL, /* cryptodev_dh_generate_key */
1394 NULL,
1395 NULL,
1396 NULL,
1397 NULL,
1398 0, /* flags */
1399 NULL /* app_data */
1400 };
1401
1402 /*
1403 * ctrl right now is just a wrapper that doesn't do much
1404 * but I expect we'll want some options soon.
1405 */
1406 static int
1407 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1408 {
1409 #ifdef HAVE_SYSLOG_R
1410 struct syslog_data sd = SYSLOG_DATA_INIT;
1411 #endif
1412
1413 switch (cmd) {
1414 default:
1415 #ifdef HAVE_SYSLOG_R
1416 syslog_r(LOG_ERR, &sd,
1417 "cryptodev_ctrl: unknown command %d", cmd);
1418 #else
1419 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1420 #endif
1421 break;
1422 }
1423 return (1);
1424 }
1425
1426 void
1427 ENGINE_load_cryptodev(void)
1428 {
1429 ENGINE *engine = ENGINE_new();
1430 int fd;
1431
1432 if (engine == NULL)
1433 return;
1434 if ((fd = get_dev_crypto()) < 0) {
1435 ENGINE_free(engine);
1436 return;
1437 }
1438
1439 /*
1440 * find out what asymmetric crypto algorithms we support
1441 */
1442 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1443 put_dev_crypto(fd);
1444 ENGINE_free(engine);
1445 return;
1446 }
1447 put_dev_crypto(fd);
1448
1449 if (!ENGINE_set_id(engine, "cryptodev") ||
1450 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1451 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1452 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1453 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1454 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1455 ENGINE_free(engine);
1456 return;
1457 }
1458
1459 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1460 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1461
1462 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1463 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1464 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1465 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1466 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1467 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1468 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1469 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1470 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1471 cryptodev_rsa.rsa_mod_exp =
1472 cryptodev_rsa_mod_exp;
1473 else
1474 cryptodev_rsa.rsa_mod_exp =
1475 cryptodev_rsa_nocrt_mod_exp;
1476 }
1477 }
1478
1479 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1480 const DSA_METHOD *meth = DSA_OpenSSL();
1481
1482 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1483 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1484 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1485 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1486 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1487 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1488 }
1489 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1490 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1491 }
1492
1493 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1494 const DH_METHOD *dh_meth = DH_OpenSSL();
1495
1496 cryptodev_dh.generate_key = dh_meth->generate_key;
1497 cryptodev_dh.compute_key = dh_meth->compute_key;
1498 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1499 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1500 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1501 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1502 cryptodev_dh.compute_key =
1503 cryptodev_dh_compute_key;
1504 }
1505 }
1506
1507 ENGINE_add(engine);
1508 ENGINE_free(engine);
1509 ERR_clear_error();
1510 }
1511
1512 #endif /* HAVE_CRYPTODEV */