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