summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/engine/hw_cryptodev.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/engine/hw_cryptodev.c')
-rw-r--r--src/lib/libcrypto/engine/hw_cryptodev.c1107
1 files changed, 1107 insertions, 0 deletions
diff --git a/src/lib/libcrypto/engine/hw_cryptodev.c b/src/lib/libcrypto/engine/hw_cryptodev.c
new file mode 100644
index 0000000000..954eb85207
--- /dev/null
+++ b/src/lib/libcrypto/engine/hw_cryptodev.c
@@ -0,0 +1,1107 @@
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 * 3. Neither the name of the author nor the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <crypto/cryptodev.h>
35#include <sys/ioctl.h>
36#include <errno.h>
37#include <stdio.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <stdarg.h>
41#include <syslog.h>
42#include <ssl/objects.h>
43#include <ssl/engine.h>
44#include <ssl/evp.h>
45#include <errno.h>
46#include <string.h>
47
48struct dev_crypto_state {
49 struct session_op d_sess;
50 int d_fd;
51};
52
53static u_int32_t cryptodev_asymfeat = 0;
54
55static int get_asym_dev_crypto(void);
56static int open_dev_crypto(void);
57static int get_dev_crypto(void);
58static int cryptodev_max_iv(int cipher);
59static int cryptodev_key_length_valid(int cipher, int len);
60static int cipher_nid_to_cryptodev(int nid);
61static int get_cryptodev_ciphers(const int **cnids);
62static int get_cryptodev_digests(const int **cnids);
63static int cryptodev_usable_ciphers(const int **nids);
64static int cryptodev_usable_digests(const int **nids);
65static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
66 const unsigned char *in, unsigned int inl);
67static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
68 const unsigned char *iv, int enc);
69static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
70static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
71 const int **nids, int nid);
72static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
73 const int **nids, int nid);
74static int bn2crparam(const BIGNUM *a, struct crparam *crp);
75static int crparam2bn(struct crparam *crp, BIGNUM *a);
76static void zapparams(struct crypt_kop *kop);
77static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
78 int slen, BIGNUM *s);
79
80static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
81 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
82static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
83 RSA *rsa);
84static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
85static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
86 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
87static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
88 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
89 BN_CTX *ctx, BN_MONT_CTX *mont);
90static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
91 int dlen, DSA *dsa);
92static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
93 DSA_SIG *sig, DSA *dsa);
94static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
95 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
96 BN_MONT_CTX *m_ctx);
97static int cryptodev_dh_compute_key(unsigned char *key,
98 const BIGNUM *pub_key, DH *dh);
99static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
100 void (*f)());
101void ENGINE_load_cryptodev(void);
102
103static const ENGINE_CMD_DEFN cryptodev_defns[] = {
104 {ENGINE_CMD_BASE,
105 "SO_PATH",
106 "Specifies the path to the some stupid shared library",
107 ENGINE_CMD_FLAG_STRING},
108 { 0, NULL, NULL, 0 }
109};
110
111static struct {
112 int id;
113 int nid;
114 int ivmax;
115 int keylen;
116} ciphers[] = {
117 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
118 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
119 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
120 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
121 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
122 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
123 { 0, NID_undef, 0, 0, },
124};
125
126static struct {
127 int id;
128 int nid;
129} digests[] = {
130 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
131 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, },
132 { CRYPTO_MD5_KPDK, NID_undef, },
133 { CRYPTO_SHA1_KPDK, NID_undef, },
134 { CRYPTO_MD5, NID_md5, },
135 { CRYPTO_SHA1, NID_undef, },
136 { 0, NID_undef, },
137};
138
139/*
140 * Return a fd if /dev/crypto seems usable, 0 otherwise.
141 */
142static int
143open_dev_crypto(void)
144{
145 static int fd = -1;
146
147 if (fd == -1) {
148 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
149 return (-1);
150 /* close on exec */
151 if (fcntl(fd, F_SETFD, 1) == -1) {
152 close(fd);
153 fd = -1;
154 return (-1);
155 }
156 }
157 return (fd);
158}
159
160static int
161get_dev_crypto(void)
162{
163 int fd, retfd;
164
165 if ((fd = open_dev_crypto()) == -1)
166 return (-1);
167 if (ioctl(fd, CRIOGET, &retfd) == -1)
168 return (-1);
169
170 /* close on exec */
171 if (fcntl(retfd, F_SETFD, 1) == -1) {
172 close(retfd);
173 return (-1);
174 }
175 return (retfd);
176}
177
178/* Caching version for asym operations */
179static int
180get_asym_dev_crypto(void)
181{
182 static int fd = -1;
183
184 if (fd == -1)
185 fd = get_dev_crypto();
186 return fd;
187}
188
189/*
190 * XXXX this needs to be set for each alg - and determined from
191 * a running card.
192 */
193static int
194cryptodev_max_iv(int cipher)
195{
196 int i;
197
198 for (i = 0; ciphers[i].id; i++)
199 if (ciphers[i].id == cipher)
200 return (ciphers[i].ivmax);
201 return (0);
202}
203
204/*
205 * XXXX this needs to be set for each alg - and determined from
206 * a running card. For now, fake it out - but most of these
207 * for real devices should return 1 for the supported key
208 * sizes the device can handle.
209 */
210static int
211cryptodev_key_length_valid(int cipher, int len)
212{
213 int i;
214
215 for (i = 0; ciphers[i].id; i++)
216 if (ciphers[i].id == cipher)
217 return (ciphers[i].keylen == len);
218 return (0);
219}
220
221/* convert libcrypto nids to cryptodev */
222static int
223cipher_nid_to_cryptodev(int nid)
224{
225 int i;
226
227 for (i = 0; ciphers[i].id; i++)
228 if (ciphers[i].nid == nid)
229 return (ciphers[i].id);
230 return (0);
231}
232
233/*
234 * Find out what ciphers /dev/crypto will let us have a session for.
235 * XXX note, that some of these openssl doesn't deal with yet!
236 * returning them here is harmless, as long as we return NULL
237 * when asked for a handler in the cryptodev_engine_ciphers routine
238 */
239static int
240get_cryptodev_ciphers(const int **cnids)
241{
242 static int nids[CRYPTO_ALGORITHM_MAX];
243 struct session_op sess;
244 int fd, i, count = 0;
245
246 if ((fd = get_dev_crypto()) < 0) {
247 *nids = NULL;
248 return (0);
249 }
250 memset(&sess, 0, sizeof(sess));
251 sess.key = (caddr_t)"123456781234567812345678";
252
253 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
254 if (ciphers[i].nid == NID_undef)
255 continue;
256 sess.cipher = ciphers[i].id;
257 sess.keylen = ciphers[i].keylen;
258 sess.mac = 0;
259 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
260 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
261 nids[count++] = ciphers[i].nid;
262 }
263 close(fd);
264
265 if (count > 0)
266 *cnids = nids;
267 else
268 *cnids = NULL;
269 return (count);
270}
271
272/*
273 * Find out what digests /dev/crypto will let us have a session for.
274 * XXX note, that some of these openssl doesn't deal with yet!
275 * returning them here is harmless, as long as we return NULL
276 * when asked for a handler in the cryptodev_engine_digests routine
277 */
278static int
279get_cryptodev_digests(const int **cnids)
280{
281 static int nids[CRYPTO_ALGORITHM_MAX];
282 struct session_op sess;
283 int fd, i, count = 0;
284
285 if ((fd = get_dev_crypto()) < 0) {
286 *nids = NULL;
287 return (0);
288 }
289 memset(&sess, 0, sizeof(sess));
290 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
291 if (digests[i].nid == NID_undef)
292 continue;
293 sess.mac = digests[i].id;
294 sess.cipher = 0;
295 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
296 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
297 nids[count++] = digests[i].nid;
298 }
299 close(fd);
300
301 if (count > 0)
302 *cnids = nids;
303 else
304 *cnids = NULL;
305 return (count);
306}
307
308/*
309 * Find the useable ciphers|digests from dev/crypto - this is the first
310 * thing called by the engine init crud which determines what it
311 * can use for ciphers from this engine. We want to return
312 * only what we can do, anythine else is handled by software.
313 *
314 * If we can't initialize the device to do anything useful for
315 * any reason, we want to return a NULL array, and 0 length,
316 * which forces everything to be done is software. By putting
317 * the initalization of the device in here, we ensure we can
318 * use this engine as the default, and if for whatever reason
319 * /dev/crypto won't do what we want it will just be done in
320 * software
321 *
322 * This can (should) be greatly expanded to perhaps take into
323 * account speed of the device, and what we want to do.
324 * (although the disabling of particular alg's could be controlled
325 * by the device driver with sysctl's.) - this is where we
326 * want most of the decisions made about what we actually want
327 * to use from /dev/crypto.
328 */
329static int
330cryptodev_usable_ciphers(const int **nids)
331{
332 return (get_cryptodev_ciphers(nids));
333}
334
335static int
336cryptodev_usable_digests(const int **nids)
337{
338 /*
339 * XXXX just disable all digests for now, because it sucks.
340 * we need a better way to decide this - i.e. I may not
341 * want digests on slow cards like hifn on fast machines,
342 * but might want them on slow or loaded machines, etc.
343 * will also want them when using crypto cards that don't
344 * suck moose gonads - would be nice to be able to decide something
345 * as reasonable default without having hackery that's card dependent.
346 * of course, the default should probably be just do everything,
347 * with perhaps a sysctl to turn algoritms off (or have them off
348 * by default) on cards that generally suck like the hifn.
349 */
350 *nids = NULL;
351 return (0);
352}
353
354static int
355cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
356 const unsigned char *in, unsigned int inl)
357{
358 struct crypt_op cryp;
359 struct dev_crypto_state *state = ctx->cipher_data;
360 struct session_op *sess = &state->d_sess;
361 void *iiv;
362 unsigned char save_iv[EVP_MAX_IV_LENGTH];
363
364 if (state->d_fd < 0)
365 return (0);
366 if (!inl)
367 return (1);
368 if ((inl % ctx->cipher->block_size) != 0)
369 return (0);
370
371 memset(&cryp, 0, sizeof(cryp));
372
373 cryp.ses = sess->ses;
374 cryp.flags = 0;
375 cryp.len = inl;
376 cryp.src = (caddr_t) in;
377 cryp.dst = (caddr_t) out;
378 cryp.mac = 0;
379
380 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
381
382 if (ctx->cipher->iv_len) {
383 cryp.iv = (caddr_t) ctx->iv;
384 if (!ctx->encrypt) {
385 iiv = (void *) in + inl - ctx->cipher->iv_len;
386 memcpy(save_iv, iiv, ctx->cipher->iv_len);
387 }
388 } else
389 cryp.iv = NULL;
390
391 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
392 /* XXX need better errror handling
393 * this can fail for a number of different reasons.
394 */
395 return (0);
396 }
397
398 if (ctx->cipher->iv_len) {
399 if (ctx->encrypt)
400 iiv = (void *) out + inl - ctx->cipher->iv_len;
401 else
402 iiv = save_iv;
403 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
404 }
405 return (1);
406}
407
408static int
409cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
410 const unsigned char *iv, int enc)
411{
412 struct dev_crypto_state *state = ctx->cipher_data;
413 struct session_op *sess = &state->d_sess;
414 int cipher;
415
416 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
417 return (0);
418
419 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
420 return (0);
421
422 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
423 return (0);
424
425 memset(sess, 0, sizeof(struct session_op));
426
427 if ((state->d_fd = get_dev_crypto()) < 0)
428 return (0);
429
430 sess->key = (unsigned char *)key;
431 sess->keylen = ctx->key_len;
432 sess->cipher = cipher;
433
434 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
435 close(state->d_fd);
436 state->d_fd = -1;
437 return (0);
438 }
439 return (1);
440}
441
442/*
443 * free anything we allocated earlier when initting a
444 * session, and close the session.
445 */
446static int
447cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
448{
449 int ret = 0;
450 struct dev_crypto_state *state = ctx->cipher_data;
451 struct session_op *sess = &state->d_sess;
452
453 if (state->d_fd < 0)
454 return (0);
455
456 /* XXX if this ioctl fails, someting's wrong. the invoker
457 * may have called us with a bogus ctx, or we could
458 * have a device that for whatever reason just doesn't
459 * want to play ball - it's not clear what's right
460 * here - should this be an error? should it just
461 * increase a counter, hmm. For right now, we return
462 * 0 - I don't believe that to be "right". we could
463 * call the gorpy openssl lib error handlers that
464 * print messages to users of the library. hmm..
465 */
466
467 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
468 ret = 0;
469 } else {
470 ret = 1;
471 }
472 close(state->d_fd);
473 state->d_fd = -1;
474
475 return (ret);
476}
477
478/*
479 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
480 * gets called when libcrypto requests a cipher NID.
481 */
482
483/* DES CBC EVP */
484const EVP_CIPHER cryptodev_des_cbc = {
485 NID_des_cbc,
486 8, 8, 8,
487 EVP_CIPH_CBC_MODE,
488 cryptodev_init_key,
489 cryptodev_cipher,
490 cryptodev_cleanup,
491 sizeof(struct dev_crypto_state),
492 EVP_CIPHER_set_asn1_iv,
493 EVP_CIPHER_get_asn1_iv,
494 NULL
495};
496
497/* 3DES CBC EVP */
498const EVP_CIPHER cryptodev_3des_cbc = {
499 NID_des_ede3_cbc,
500 8, 24, 8,
501 EVP_CIPH_CBC_MODE,
502 cryptodev_init_key,
503 cryptodev_cipher,
504 cryptodev_cleanup,
505 sizeof(struct dev_crypto_state),
506 EVP_CIPHER_set_asn1_iv,
507 EVP_CIPHER_get_asn1_iv,
508 NULL
509};
510
511const EVP_CIPHER cryptodev_bf_cbc = {
512 NID_bf_cbc,
513 8, 16, 8,
514 EVP_CIPH_CBC_MODE,
515 cryptodev_init_key,
516 cryptodev_cipher,
517 cryptodev_cleanup,
518 sizeof(struct dev_crypto_state),
519 EVP_CIPHER_set_asn1_iv,
520 EVP_CIPHER_get_asn1_iv,
521 NULL
522};
523
524const EVP_CIPHER cryptodev_cast_cbc = {
525 NID_cast5_cbc,
526 8, 16, 8,
527 EVP_CIPH_CBC_MODE,
528 cryptodev_init_key,
529 cryptodev_cipher,
530 cryptodev_cleanup,
531 sizeof(struct dev_crypto_state),
532 EVP_CIPHER_set_asn1_iv,
533 EVP_CIPHER_get_asn1_iv,
534 NULL
535};
536
537const EVP_CIPHER cryptodev_aes_cbc = {
538 NID_aes_128_cbc,
539 16, 16, 16,
540 EVP_CIPH_CBC_MODE,
541 cryptodev_init_key,
542 cryptodev_cipher,
543 cryptodev_cleanup,
544 sizeof(struct dev_crypto_state),
545 EVP_CIPHER_set_asn1_iv,
546 EVP_CIPHER_get_asn1_iv,
547 NULL
548};
549
550/*
551 * Registered by the ENGINE when used to find out how to deal with
552 * a particular NID in the ENGINE. this says what we'll do at the
553 * top level - note, that list is restricted by what we answer with
554 */
555static int
556cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
557 const int **nids, int nid)
558{
559 if (!cipher)
560 return (cryptodev_usable_ciphers(nids));
561
562 switch (nid) {
563 case NID_des_ede3_cbc:
564 *cipher = &cryptodev_3des_cbc;
565 break;
566 case NID_des_cbc:
567 *cipher = &cryptodev_des_cbc;
568 break;
569 case NID_bf_cbc:
570 *cipher = &cryptodev_bf_cbc;
571 break;
572 case NID_cast5_cbc:
573 *cipher = &cryptodev_cast_cbc;
574 break;
575 case NID_aes_128_cbc:
576 *cipher = &cryptodev_aes_cbc;
577 break;
578 default:
579 *cipher = NULL;
580 break;
581 }
582 return (*cipher != NULL);
583}
584
585static int
586cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
587 const int **nids, int nid)
588{
589 if (!digest)
590 return (cryptodev_usable_digests(nids));
591
592 switch (nid) {
593 case NID_md5:
594 *digest = NULL; /* need to make a clean md5 critter */
595 break;
596 default:
597 *digest = NULL;
598 break;
599 }
600 return (*digest != NULL);
601}
602
603/*
604 * Convert a BIGNUM to the representation that /dev/crypto needs.
605 * Upon completion of use, the caller is responsible for freeing
606 * crp->crp_p.
607 */
608static int
609bn2crparam(const BIGNUM *a, struct crparam *crp)
610{
611 int i, j, k;
612 ssize_t words, bytes, bits;
613 u_char *b;
614
615 crp->crp_p = NULL;
616 crp->crp_nbits = 0;
617
618 bits = BN_num_bits(a);
619 bytes = (bits + 7) / 8;
620
621 b = malloc(bytes);
622 if (b == NULL)
623 return (1);
624
625 crp->crp_p = b;
626 crp->crp_nbits = bits;
627
628 for (i = 0, j = 0; i < a->top; i++) {
629 for (k = 0; k < BN_BITS2 / 8; k++) {
630 if ((j + k) >= bytes)
631 return (0);
632 b[j + k] = a->d[i] >> (k * 8);
633 }
634 j += BN_BITS2 / 8;
635 }
636 return (0);
637}
638
639/* Convert a /dev/crypto parameter to a BIGNUM */
640static int
641crparam2bn(struct crparam *crp, BIGNUM *a)
642{
643 u_int8_t *pd;
644 int i, bytes;
645
646 bytes = (crp->crp_nbits + 7) / 8;
647
648 if (bytes == 0)
649 return (-1);
650
651 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
652 return (-1);
653
654 for (i = 0; i < bytes; i++)
655 pd[i] = crp->crp_p[bytes - i - 1];
656
657 BN_bin2bn(pd, bytes, a);
658 free(pd);
659
660 return (0);
661}
662
663static void
664zapparams(struct crypt_kop *kop)
665{
666 int i;
667
668 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
669 if (kop->crk_param[i].crp_p)
670 free(kop->crk_param[i].crp_p);
671 kop->crk_param[i].crp_p = NULL;
672 kop->crk_param[i].crp_nbits = 0;
673 }
674}
675
676static int
677cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
678{
679 int fd, ret = -1;
680
681 if ((fd = get_asym_dev_crypto()) < 0)
682 return (ret);
683
684 if (r) {
685 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
686 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
687 kop->crk_oparams++;
688 }
689 if (s) {
690 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
691 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
692 kop->crk_oparams++;
693 }
694
695 if (ioctl(fd, CIOCKEY, kop) == 0) {
696 if (r)
697 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
698 if (s)
699 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
700 ret = 0;
701 }
702
703 return (ret);
704}
705
706static int
707cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
708 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
709{
710 struct crypt_kop kop;
711 int ret = 1;
712
713 /* Currently, we know we can do mod exp iff we can do any
714 * asymmetric operations at all.
715 */
716 if (cryptodev_asymfeat == 0) {
717 ret = BN_mod_exp(r, a, p, m, ctx);
718 return (ret);
719 }
720
721 memset(&kop, 0, sizeof kop);
722 kop.crk_op = CRK_MOD_EXP;
723
724 /* inputs: a^p % m */
725 if (bn2crparam(a, &kop.crk_param[0]))
726 goto err;
727 if (bn2crparam(p, &kop.crk_param[1]))
728 goto err;
729 if (bn2crparam(m, &kop.crk_param[2]))
730 goto err;
731 kop.crk_iparams = 3;
732
733 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
734 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
735 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
736 }
737err:
738 zapparams(&kop);
739 return (ret);
740}
741
742static int
743cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
744{
745 int r;
746 BN_CTX *ctx;
747
748 ctx = BN_CTX_new();
749 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
750 BN_CTX_free(ctx);
751 return (r);
752}
753
754static int
755cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
756{
757 struct crypt_kop kop;
758 int ret = 1;
759
760 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
761 /* XXX 0 means failure?? */
762 return (0);
763 }
764
765 memset(&kop, 0, sizeof kop);
766 kop.crk_op = CRK_MOD_EXP_CRT;
767 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
768 if (bn2crparam(rsa->p, &kop.crk_param[0]))
769 goto err;
770 if (bn2crparam(rsa->q, &kop.crk_param[1]))
771 goto err;
772 if (bn2crparam(I, &kop.crk_param[2]))
773 goto err;
774 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
775 goto err;
776 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
777 goto err;
778 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
779 goto err;
780 kop.crk_iparams = 6;
781
782 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
783 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
784 ret = (*meth->rsa_mod_exp)(r0, I, rsa);
785 }
786err:
787 zapparams(&kop);
788 return (ret);
789}
790
791static RSA_METHOD cryptodev_rsa = {
792 "cryptodev RSA method",
793 NULL, /* rsa_pub_enc */
794 NULL, /* rsa_pub_dec */
795 NULL, /* rsa_priv_enc */
796 NULL, /* rsa_priv_dec */
797 NULL,
798 NULL,
799 NULL, /* init */
800 NULL, /* finish */
801 0, /* flags */
802 NULL, /* app_data */
803 NULL, /* rsa_sign */
804 NULL /* rsa_verify */
805};
806
807static int
808cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
809 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
810{
811 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
812}
813
814static int
815cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
816 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
817 BN_CTX *ctx, BN_MONT_CTX *mont)
818{
819 BIGNUM t2;
820 int ret = 0;
821
822 BN_init(&t2);
823
824 /* v = ( g^u1 * y^u2 mod p ) mod q */
825 /* let t1 = g ^ u1 mod p */
826 ret = 0;
827
828 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
829 goto err;
830
831 /* let t2 = y ^ u2 mod p */
832 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
833 goto err;
834 /* let u1 = t1 * t2 mod p */
835 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
836 goto err;
837
838 BN_copy(t1,u1);
839
840 ret = 1;
841err:
842 BN_free(&t2);
843 return(ret);
844}
845
846static DSA_SIG *
847cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
848{
849 struct crypt_kop kop;
850 BIGNUM *r = NULL, *s = NULL;
851 DSA_SIG *dsaret = NULL;
852
853 if ((r = BN_new()) == NULL)
854 goto err;
855 if ((s = BN_new()) == NULL) {
856 BN_free(r);
857 goto err;
858 }
859
860 printf("bar\n");
861 memset(&kop, 0, sizeof kop);
862 kop.crk_op = CRK_DSA_SIGN;
863
864 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
865 kop.crk_param[0].crp_p = (caddr_t)dgst;
866 kop.crk_param[0].crp_nbits = dlen * 8;
867 if (bn2crparam(dsa->p, &kop.crk_param[1]))
868 goto err;
869 if (bn2crparam(dsa->q, &kop.crk_param[2]))
870 goto err;
871 if (bn2crparam(dsa->g, &kop.crk_param[3]))
872 goto err;
873 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
874 goto err;
875 kop.crk_iparams = 5;
876
877 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
878 BN_num_bytes(dsa->q), s) == 0) {
879 dsaret = DSA_SIG_new();
880 dsaret->r = r;
881 dsaret->s = s;
882 } else {
883 const DSA_METHOD *meth = DSA_OpenSSL();
884 BN_free(r);
885 BN_free(s);
886 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
887 }
888err:
889 kop.crk_param[0].crp_p = NULL;
890 zapparams(&kop);
891 return (dsaret);
892}
893
894static int
895cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
896 DSA_SIG *sig, DSA *dsa)
897{
898 struct crypt_kop kop;
899 int dsaret = 1;
900
901 memset(&kop, 0, sizeof kop);
902 kop.crk_op = CRK_DSA_VERIFY;
903
904 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
905 kop.crk_param[0].crp_p = (caddr_t)dgst;
906 kop.crk_param[0].crp_nbits = dlen * 8;
907 if (bn2crparam(dsa->p, &kop.crk_param[1]))
908 goto err;
909 if (bn2crparam(dsa->q, &kop.crk_param[2]))
910 goto err;
911 if (bn2crparam(dsa->g, &kop.crk_param[3]))
912 goto err;
913 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
914 goto err;
915 if (bn2crparam(sig->r, &kop.crk_param[5]))
916 goto err;
917 if (bn2crparam(sig->s, &kop.crk_param[6]))
918 goto err;
919 kop.crk_iparams = 7;
920
921 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
922 dsaret = kop.crk_status;
923 } else {
924 const DSA_METHOD *meth = DSA_OpenSSL();
925
926 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
927 }
928err:
929 kop.crk_param[0].crp_p = NULL;
930 zapparams(&kop);
931 return (dsaret);
932}
933
934static DSA_METHOD cryptodev_dsa = {
935 "cryptodev DSA method",
936 NULL,
937 NULL, /* dsa_sign_setup */
938 NULL,
939 NULL, /* dsa_mod_exp */
940 NULL,
941 NULL, /* init */
942 NULL, /* finish */
943 0, /* flags */
944 NULL /* app_data */
945};
946
947static int
948cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
949 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
950 BN_MONT_CTX *m_ctx)
951{
952 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
953}
954
955static int
956cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
957{
958 struct crypt_kop kop;
959 int dhret = 1;
960 int fd, keylen;
961
962 if ((fd = get_asym_dev_crypto()) < 0) {
963 const DH_METHOD *meth = DH_OpenSSL();
964
965 return ((meth->compute_key)(key, pub_key, dh));
966 }
967
968 keylen = BN_num_bits(dh->p);
969
970 memset(&kop, 0, sizeof kop);
971 kop.crk_op = CRK_DH_COMPUTE_KEY;
972
973 /* inputs: dh->priv_key pub_key dh->p key */
974 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
975 goto err;
976 if (bn2crparam(pub_key, &kop.crk_param[1]))
977 goto err;
978 if (bn2crparam(dh->p, &kop.crk_param[2]))
979 goto err;
980 kop.crk_iparams = 3;
981
982 kop.crk_param[3].crp_p = key;
983 kop.crk_param[3].crp_nbits = keylen * 8;
984 kop.crk_oparams = 1;
985
986 if (ioctl(fd, CIOCKEY, &kop) == -1) {
987 const DH_METHOD *meth = DH_OpenSSL();
988
989 dhret = (meth->compute_key)(key, pub_key, dh);
990 }
991err:
992 kop.crk_param[3].crp_p = NULL;
993 zapparams(&kop);
994 return (dhret);
995}
996
997static DH_METHOD cryptodev_dh = {
998 "cryptodev DH method",
999 NULL, /* cryptodev_dh_generate_key */
1000 NULL,
1001 NULL,
1002 NULL,
1003 NULL,
1004 0, /* flags */
1005 NULL /* app_data */
1006};
1007
1008/*
1009 * ctrl right now is just a wrapper that doesn't do much
1010 * but I expect we'll want some options soon.
1011 */
1012static int
1013cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1014{
1015 struct syslog_data sd = SYSLOG_DATA_INIT;
1016
1017 switch (cmd) {
1018 default:
1019 syslog_r(LOG_ERR, &sd,
1020 "cryptodev_ctrl: unknown command %d", cmd);
1021 break;
1022 }
1023 return (1);
1024}
1025
1026void
1027ENGINE_load_cryptodev(void)
1028{
1029 ENGINE *engine = ENGINE_new();
1030 int fd;
1031
1032 if (engine == NULL)
1033 return;
1034 if ((fd = get_dev_crypto()) < 0)
1035 return;
1036
1037 /*
1038 * find out what asymmetric crypto algorithms we support
1039 */
1040 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1041 close(fd);
1042 return;
1043 }
1044 close(fd);
1045
1046 if (!ENGINE_set_id(engine, "cryptodev") ||
1047 !ENGINE_set_name(engine, "OpenBSD cryptodev engine") ||
1048 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1049 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1050 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1051 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1052 ENGINE_free(engine);
1053 return;
1054 }
1055
1056 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1057 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1058
1059 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1060 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1061 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1062 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1063 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1064 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1065 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1066 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1067 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1068 cryptodev_rsa.rsa_mod_exp =
1069 cryptodev_rsa_mod_exp;
1070 else
1071 cryptodev_rsa.rsa_mod_exp =
1072 cryptodev_rsa_nocrt_mod_exp;
1073 }
1074 }
1075
1076 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1077 const DSA_METHOD *meth = DSA_OpenSSL();
1078
1079 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1080 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1081 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1082 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1083 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1084 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1085 }
1086 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1087 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1088 }
1089
1090 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1091 const DH_METHOD *dh_meth = DH_OpenSSL();
1092
1093 cryptodev_dh.generate_key = dh_meth->generate_key;
1094 cryptodev_dh.compute_key = dh_meth->compute_key;
1095 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1096 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1097 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1098 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1099 cryptodev_dh.compute_key =
1100 cryptodev_dh_compute_key;
1101 }
1102 }
1103
1104 ENGINE_add(engine);
1105 ENGINE_free(engine);
1106 ERR_clear_error();
1107}