cryptodev: allow copying EVP contexts
[openssl.git] / crypto / engine / eng_cryptodev.c
1 /*
2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
12  * Copyright (c) 2002 Theo de Raadt
13  * Copyright (c) 2002 Markus Friedl
14  * Copyright (c) 2012 Nikos Mavrogiannopoulos
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38
39 #include <openssl/objects.h>
40 #include <internal/engine.h>
41 #include <openssl/evp.h>
42 #include <openssl/bn.h>
43 #include <openssl/crypto.h>
44
45 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
46         (defined(OpenBSD) || defined(__FreeBSD__) || defined(__DragonFly__))
47 # include <sys/param.h>
48 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) || defined(__DragonFly__)
49 #  define HAVE_CRYPTODEV
50 # endif
51 # if (OpenBSD >= 200110)
52 #  define HAVE_SYSLOG_R
53 # endif
54 #endif
55
56 #include <sys/types.h>
57 #ifdef HAVE_CRYPTODEV
58 # include <crypto/cryptodev.h>
59 # include <sys/ioctl.h>
60 # include <errno.h>
61 # include <stdio.h>
62 # include <unistd.h>
63 # include <fcntl.h>
64 # include <stdarg.h>
65 # include <syslog.h>
66 # include <errno.h>
67 # include <string.h>
68 #endif
69 #include <openssl/dh.h>
70 #include <openssl/dsa.h>
71 #include <openssl/err.h>
72 #include <openssl/rsa.h>
73
74 #ifndef HAVE_CRYPTODEV
75
76 void engine_load_cryptodev_int(void)
77 {
78     /* This is a NOP on platforms without /dev/crypto */
79     return;
80 }
81
82 #else
83
84 struct dev_crypto_state {
85     struct session_op d_sess;
86     int d_fd;
87 # ifdef USE_CRYPTODEV_DIGESTS
88     unsigned char digest_res[HASH_MAX_LEN];
89     char *mac_data;
90     int mac_len;
91 # endif
92 };
93
94 static u_int32_t cryptodev_asymfeat = 0;
95
96 static RSA_METHOD *cryptodev_rsa;
97 # ifndef OPENSSL_NO_DSA
98 static DSA_METHOD *cryptodev_dsa = NULL;
99 # endif
100 # ifndef OPENSSL_NO_DH
101 static DH_METHOD *cryptodev_dh;
102 # endif
103
104 static int get_asym_dev_crypto(void);
105 static int open_dev_crypto(void);
106 static int get_dev_crypto(void);
107 static int get_cryptodev_ciphers(const int **cnids);
108 # ifdef USE_CRYPTODEV_DIGESTS
109 static int get_cryptodev_digests(const int **cnids);
110 # endif
111 static int cryptodev_usable_ciphers(const int **nids);
112 static int cryptodev_usable_digests(const int **nids);
113 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
114                             const unsigned char *in, size_t inl);
115 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
116                               const unsigned char *iv, int enc);
117 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
118 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
119                                     const int **nids, int nid);
120 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
121                                     const int **nids, int nid);
122 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
123 static int crparam2bn(struct crparam *crp, BIGNUM *a);
124 static void zapparams(struct crypt_kop *kop);
125 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
126                           int slen, BIGNUM *s);
127
128 static int cryptodev_bn_mod_exp(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_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
132                                        BN_CTX *ctx);
133 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
134                                  BN_CTX *ctx);
135 # ifndef OPENSSL_NO_DSA
136 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a,
137                                     const BIGNUM *p, const BIGNUM *m,
138                                     BN_CTX *ctx, BN_MONT_CTX *m_ctx);
139 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
140                                      const BIGNUM *u1, const BIGNUM *pub_key,
141                                      const BIGNUM *u2, const BIGNUM *p,
142                                      BN_CTX *ctx, BN_MONT_CTX *mont);
143 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
144                                       DSA *dsa);
145 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
146                                 DSA_SIG *sig, DSA *dsa);
147 # endif
148 # ifndef OPENSSL_NO_DH
149 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
150                                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
151                                 BN_MONT_CTX *m_ctx);
152 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
153                                     DH *dh);
154 # endif
155 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
156                           void (*f) (void));
157 void engine_load_cryptodev_int(void);
158
159 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
160     {0, NULL, NULL, 0}
161 };
162
163 static struct {
164     int id;
165     int nid;
166     int ivmax;
167     int keylen;
168 } ciphers[] = {
169     {
170         CRYPTO_ARC4, NID_rc4, 0, 16,
171     },
172     {
173         CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
174     },
175     {
176         CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
177     },
178     {
179         CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
180     },
181     {
182         CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
183     },
184     {
185         CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
186     },
187 # ifdef CRYPTO_AES_CTR
188     {
189         CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
190     },
191     {
192         CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
193     },
194     {
195         CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
196     },
197 # endif
198     {
199         CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
200     },
201     {
202         CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
203     },
204     {
205         CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
206     },
207     {
208         0, NID_undef, 0, 0,
209     },
210 };
211
212 # ifdef USE_CRYPTODEV_DIGESTS
213 static struct {
214     int id;
215     int nid;
216     int  digestlen;
217 } digests[] = {
218 #  if 0
219     /* HMAC is not supported */
220     {
221         CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
222     },
223     {
224         CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
225     },
226     {
227         CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32
228     },
229     {
230         CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48
231     },
232     {
233         CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64
234     },
235 #  endif
236     {
237         CRYPTO_MD5, NID_md5, 16
238     },
239     {
240         CRYPTO_SHA1, NID_sha1, 20
241     },
242     {
243         CRYPTO_SHA2_256, NID_sha256, 32
244     },
245     {
246         CRYPTO_SHA2_384, NID_sha384, 48
247     },
248     {
249         CRYPTO_SHA2_512, NID_sha512, 64
250     },
251     {
252         0, NID_undef, 0
253     },
254 };
255 # endif
256
257 /*
258  * Return a fd if /dev/crypto seems usable, 0 otherwise.
259  */
260 static int open_dev_crypto(void)
261 {
262     static int fd = -1;
263
264     if (fd == -1) {
265         if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
266             return (-1);
267         /* close on exec */
268         if (fcntl(fd, F_SETFD, 1) == -1) {
269             close(fd);
270             fd = -1;
271             return (-1);
272         }
273     }
274     return (fd);
275 }
276
277 static int get_dev_crypto(void)
278 {
279     int fd, retfd;
280
281     if ((fd = open_dev_crypto()) == -1)
282         return (-1);
283 # ifndef CRIOGET_NOT_NEEDED
284     if (ioctl(fd, CRIOGET, &retfd) == -1)
285         return (-1);
286
287     /* close on exec */
288     if (fcntl(retfd, F_SETFD, 1) == -1) {
289         close(retfd);
290         return (-1);
291     }
292 # else
293     retfd = fd;
294 # endif
295     return (retfd);
296 }
297
298 static void put_dev_crypto(int fd)
299 {
300 # ifndef CRIOGET_NOT_NEEDED
301     close(fd);
302 # endif
303 }
304
305 /* Caching version for asym operations */
306 static int get_asym_dev_crypto(void)
307 {
308     static int fd = -1;
309
310     if (fd == -1)
311         fd = get_dev_crypto();
312     return fd;
313 }
314
315 /*
316  * Find out what ciphers /dev/crypto will let us have a session for.
317  * XXX note, that some of these openssl doesn't deal with yet!
318  * returning them here is harmless, as long as we return NULL
319  * when asked for a handler in the cryptodev_engine_ciphers routine
320  */
321 static int get_cryptodev_ciphers(const int **cnids)
322 {
323     static int nids[CRYPTO_ALGORITHM_MAX];
324     struct session_op sess;
325     int fd, i, count = 0;
326     unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
327
328     if ((fd = get_dev_crypto()) < 0) {
329         *cnids = NULL;
330         return (0);
331     }
332     memset(&sess, 0, sizeof(sess));
333     sess.key = (void *)fake_key;
334
335     for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
336         if (ciphers[i].nid == NID_undef)
337             continue;
338         sess.cipher = ciphers[i].id;
339         sess.keylen = ciphers[i].keylen;
340         sess.mac = 0;
341         if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
342             ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
343             nids[count++] = ciphers[i].nid;
344     }
345     put_dev_crypto(fd);
346
347     if (count > 0)
348         *cnids = nids;
349     else
350         *cnids = NULL;
351     return (count);
352 }
353
354 # ifdef USE_CRYPTODEV_DIGESTS
355 /*
356  * Find out what digests /dev/crypto will let us have a session for.
357  * XXX note, that some of these openssl doesn't deal with yet!
358  * returning them here is harmless, as long as we return NULL
359  * when asked for a handler in the cryptodev_engine_digests routine
360  */
361 static int get_cryptodev_digests(const int **cnids)
362 {
363     static int nids[CRYPTO_ALGORITHM_MAX];
364     unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
365     struct session_op sess;
366     int fd, i, count = 0;
367
368     if ((fd = get_dev_crypto()) < 0) {
369         *cnids = NULL;
370         return (0);
371     }
372     memset(&sess, 0, sizeof(sess));
373     sess.mackey = (void *)fake_key;
374     for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
375         if (digests[i].nid == NID_undef)
376             continue;
377         sess.mac = digests[i].id;
378         sess.mackeylen = digests[i].digestlen;
379         sess.cipher = 0;
380         if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
381             ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
382             nids[count++] = digests[i].nid;
383     }
384     put_dev_crypto(fd);
385
386     if (count > 0)
387         *cnids = nids;
388     else
389         *cnids = NULL;
390     return (count);
391 }
392 # endif                         /* 0 */
393
394 /*
395  * Find the useable ciphers|digests from dev/crypto - this is the first
396  * thing called by the engine init crud which determines what it
397  * can use for ciphers from this engine. We want to return
398  * only what we can do, anything else is handled by software.
399  *
400  * If we can't initialize the device to do anything useful for
401  * any reason, we want to return a NULL array, and 0 length,
402  * which forces everything to be done is software. By putting
403  * the initialization of the device in here, we ensure we can
404  * use this engine as the default, and if for whatever reason
405  * /dev/crypto won't do what we want it will just be done in
406  * software
407  *
408  * This can (should) be greatly expanded to perhaps take into
409  * account speed of the device, and what we want to do.
410  * (although the disabling of particular alg's could be controlled
411  * by the device driver with sysctl's.) - this is where we
412  * want most of the decisions made about what we actually want
413  * to use from /dev/crypto.
414  */
415 static int cryptodev_usable_ciphers(const int **nids)
416 {
417     return (get_cryptodev_ciphers(nids));
418 }
419
420 static int cryptodev_usable_digests(const int **nids)
421 {
422 # ifdef USE_CRYPTODEV_DIGESTS
423     return (get_cryptodev_digests(nids));
424 # else
425     /*
426      * XXXX just disable all digests for now, because it sucks.
427      * we need a better way to decide this - i.e. I may not
428      * want digests on slow cards like hifn on fast machines,
429      * but might want them on slow or loaded machines, etc.
430      * will also want them when using crypto cards that don't
431      * suck moose gonads - would be nice to be able to decide something
432      * as reasonable default without having hackery that's card dependent.
433      * of course, the default should probably be just do everything,
434      * with perhaps a sysctl to turn algorithms off (or have them off
435      * by default) on cards that generally suck like the hifn.
436      */
437     *nids = NULL;
438     return (0);
439 # endif
440 }
441
442 static int
443 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
444                  const unsigned char *in, size_t inl)
445 {
446     struct crypt_op cryp;
447     struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
448     struct session_op *sess = &state->d_sess;
449     const void *iiv;
450     unsigned char save_iv[EVP_MAX_IV_LENGTH];
451
452     if (state->d_fd < 0)
453         return (0);
454     if (!inl)
455         return (1);
456     if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
457         return (0);
458
459     memset(&cryp, 0, sizeof(cryp));
460
461     cryp.ses = sess->ses;
462     cryp.flags = 0;
463     cryp.len = inl;
464     cryp.src = (void *) in;
465     cryp.dst = (void *) out;
466     cryp.mac = 0;
467
468     cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
469
470     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
471         cryp.iv = (void *) EVP_CIPHER_CTX_iv(ctx);
472         if (!EVP_CIPHER_CTX_encrypting(ctx)) {
473             iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
474             memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
475         }
476     } else
477         cryp.iv = NULL;
478
479     if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
480         /*
481          * XXX need better error handling this can fail for a number of
482          * different reasons.
483          */
484         return (0);
485     }
486
487     if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
488         if (EVP_CIPHER_CTX_encrypting(ctx))
489             iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
490         else
491             iiv = save_iv;
492         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
493                EVP_CIPHER_CTX_iv_length(ctx));
494     }
495     return (1);
496 }
497
498 static int
499 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
500                    const unsigned char *iv, int enc)
501 {
502     struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
503     struct session_op *sess = &state->d_sess;
504     int cipher = -1, i;
505
506     for (i = 0; ciphers[i].id; i++)
507         if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
508             EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
509             EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
510             cipher = ciphers[i].id;
511             break;
512         }
513
514     if (!ciphers[i].id) {
515         state->d_fd = -1;
516         return (0);
517     }
518
519     memset(sess, 0, sizeof(*sess));
520
521     if ((state->d_fd = get_dev_crypto()) < 0)
522         return (0);
523
524     sess->key = (void *) key;
525     sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
526     sess->cipher = cipher;
527
528     if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
529         put_dev_crypto(state->d_fd);
530         state->d_fd = -1;
531         return (0);
532     }
533     return (1);
534 }
535
536 /*
537  * free anything we allocated earlier when initing a
538  * session, and close the session.
539  */
540 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
541 {
542     int ret = 0;
543     struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
544     struct session_op *sess = &state->d_sess;
545
546     if (state->d_fd < 0)
547         return (0);
548
549     /*
550      * XXX if this ioctl fails, something's wrong. the invoker may have called
551      * us with a bogus ctx, or we could have a device that for whatever
552      * reason just doesn't want to play ball - it's not clear what's right
553      * here - should this be an error? should it just increase a counter,
554      * hmm. For right now, we return 0 - I don't believe that to be "right".
555      * we could call the gorpy openssl lib error handlers that print messages
556      * to users of the library. hmm..
557      */
558
559     if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
560         ret = 0;
561     } else {
562         ret = 1;
563     }
564     put_dev_crypto(state->d_fd);
565     state->d_fd = -1;
566
567     return (ret);
568 }
569
570 /*
571  * libcrypto EVP stuff - this is how we get wired to EVP so the engine
572  * gets called when libcrypto requests a cipher NID.
573  */
574
575 static int cryptodev_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void *p2)
576 {
577     struct dev_crypto_state *state = ctx->cipher_data;
578     struct session_op *sess = &state->d_sess;
579
580     if (type == EVP_CTRL_COPY) {
581         EVP_CIPHER_CTX *out = p2;
582         return cryptodev_init_key(out, sess->key, ctx->iv, 0);
583     }
584
585     return 0;
586 }
587
588 /* RC4 */
589 static EVP_CIPHER *rc4_cipher = NULL;
590 static const EVP_CIPHER *cryptodev_rc4(void)
591 {
592     if (rc4_cipher == NULL) {
593         EVP_CIPHER *cipher;
594
595         if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16)) == NULL
596             || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
597             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH
598                                           | EVP_CIPH_CUSTOM_COPY)
599             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
600             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
601             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
602             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
603             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))) {
604             EVP_CIPHER_meth_free(cipher);
605             cipher = NULL;
606         }
607         rc4_cipher = cipher;
608     }
609     return rc4_cipher;
610 }
611
612 /* DES CBC EVP */
613 static EVP_CIPHER *des_cbc_cipher = NULL;
614 static const EVP_CIPHER *cryptodev_des_cbc(void)
615 {
616     if (des_cbc_cipher == NULL) {
617         EVP_CIPHER *cipher;
618
619         if ((cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8)) == NULL
620             || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
621             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
622                                           | EVP_CIPH_CUSTOM_COPY)
623             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
624             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
625             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
626             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
627             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
628             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
629             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
630             EVP_CIPHER_meth_free(cipher);
631             cipher = NULL;
632         }
633         des_cbc_cipher = cipher;
634     }
635     return des_cbc_cipher;
636 }
637
638 /* 3DES CBC EVP */
639 static EVP_CIPHER *des3_cbc_cipher = NULL;
640 static const EVP_CIPHER *cryptodev_3des_cbc(void)
641 {
642     if (des3_cbc_cipher == NULL) {
643         EVP_CIPHER *cipher;
644
645         if ((cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24)) == NULL
646             || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
647             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
648                                           | EVP_CIPH_CUSTOM_COPY)
649             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
650             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
651             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
652             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
653             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
654             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
655             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
656             EVP_CIPHER_meth_free(cipher);
657             cipher = NULL;
658         }
659         des3_cbc_cipher = cipher;
660     }
661     return des3_cbc_cipher;
662 }
663
664 static EVP_CIPHER *bf_cbc_cipher = NULL;
665 static const EVP_CIPHER *cryptodev_bf_cbc(void)
666 {
667     if (bf_cbc_cipher == NULL) {
668         EVP_CIPHER *cipher;
669
670         if ((cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16)) == NULL
671             || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
672             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
673                                           | EVP_CIPH_CUSTOM_COPY)
674             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
675             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
676             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
677             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
678             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
679             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
680             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
681             EVP_CIPHER_meth_free(cipher);
682             cipher = NULL;
683         }
684         bf_cbc_cipher = cipher;
685     }
686     return bf_cbc_cipher;
687 }
688
689 static EVP_CIPHER *cast_cbc_cipher = NULL;
690 static const EVP_CIPHER *cryptodev_cast_cbc(void)
691 {
692     if (cast_cbc_cipher == NULL) {
693         EVP_CIPHER *cipher;
694
695         if ((cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16)) == NULL
696             || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
697             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
698                                           | EVP_CIPH_CUSTOM_COPY)
699             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
700             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
701             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
702             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
703             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
704             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
705             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
706             EVP_CIPHER_meth_free(cipher);
707             cipher = NULL;
708         }
709         cast_cbc_cipher = cipher;
710     }
711     return cast_cbc_cipher;
712 }
713
714 static EVP_CIPHER *aes_cbc_cipher = NULL;
715 static const EVP_CIPHER *cryptodev_aes_cbc(void)
716 {
717     if (aes_cbc_cipher == NULL) {
718         EVP_CIPHER *cipher;
719
720         if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16)) == NULL
721             || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
722             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
723                                           | EVP_CIPH_CUSTOM_COPY)
724             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
725             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
726             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
727             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
728             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
729             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
730             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
731             EVP_CIPHER_meth_free(cipher);
732             cipher = NULL;
733         }
734         aes_cbc_cipher = cipher;
735     }
736     return aes_cbc_cipher;
737 }
738
739 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
740 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
741 {
742     if (aes_192_cbc_cipher == NULL) {
743         EVP_CIPHER *cipher;
744
745         if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24)) == NULL
746             || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
747             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
748                                           | EVP_CIPH_CUSTOM_COPY)
749             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
750             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
751             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
752             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
753             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
754             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
755             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
756             EVP_CIPHER_meth_free(cipher);
757             cipher = NULL;
758         }
759         aes_192_cbc_cipher = cipher;
760     }
761     return aes_192_cbc_cipher;
762 }
763
764 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
765 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
766 {
767     if (aes_256_cbc_cipher == NULL) {
768         EVP_CIPHER *cipher;
769
770         if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32)) == NULL
771             || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
772             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
773                                           | EVP_CIPH_CUSTOM_COPY)
774             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
775             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
776             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
777             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
778             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
779             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
780             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
781             EVP_CIPHER_meth_free(cipher);
782             cipher = NULL;
783         }
784         aes_256_cbc_cipher = cipher;
785     }
786     return aes_256_cbc_cipher;
787 }
788
789 # ifdef CRYPTO_AES_CTR
790 static EVP_CIPHER *aes_ctr_cipher = NULL;
791 static const EVP_CIPHER *cryptodev_aes_ctr(void)
792 {
793     if (aes_ctr_cipher == NULL) {
794         EVP_CIPHER *cipher;
795
796         if ((cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16)) == NULL
797             || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
798             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
799                                           | EVP_CIPH_CUSTOM_COPY)
800             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
801             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
802             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
803             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
804             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
805             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
806             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
807             EVP_CIPHER_meth_free(cipher);
808             cipher = NULL;
809         }
810         aes_ctr_cipher = cipher;
811     }
812     return aes_ctr_cipher;
813 }
814
815 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
816 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
817 {
818     if (aes_192_ctr_cipher == NULL) {
819         EVP_CIPHER *cipher;
820
821         if ((cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24)) == NULL
822             || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
823             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
824                                           | EVP_CIPH_CUSTOM_COPY)
825             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
826             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
827             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
828             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
829             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
830             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
831             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
832             EVP_CIPHER_meth_free(cipher);
833             cipher = NULL;
834         }
835         aes_192_ctr_cipher = cipher;
836     }
837     return aes_192_ctr_cipher;
838 }
839
840 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
841 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
842 {
843     if (aes_256_ctr_cipher == NULL) {
844         EVP_CIPHER *cipher;
845
846         if ((cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32)) == NULL
847             || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
848             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
849                                           | EVP_CIPH_CUSTOM_COPY)
850             || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
851             || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
852             || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
853             || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
854             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(struct dev_crypto_state))
855             || !EVP_CIPHER_meth_set_set_asn1_params(cipher, EVP_CIPHER_set_asn1_iv)
856             || !EVP_CIPHER_meth_set_get_asn1_params(cipher, EVP_CIPHER_get_asn1_iv)) {
857             EVP_CIPHER_meth_free(cipher);
858             cipher = NULL;
859         }
860         aes_256_ctr_cipher = cipher;
861     }
862     return aes_256_ctr_cipher;
863 }
864 # endif
865 /*
866  * Registered by the ENGINE when used to find out how to deal with
867  * a particular NID in the ENGINE. this says what we'll do at the
868  * top level - note, that list is restricted by what we answer with
869  */
870 static int
871 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
872                          const int **nids, int nid)
873 {
874     if (!cipher)
875         return (cryptodev_usable_ciphers(nids));
876
877     switch (nid) {
878     case NID_rc4:
879         *cipher = cryptodev_rc4();
880         break;
881     case NID_des_ede3_cbc:
882         *cipher = cryptodev_3des_cbc();
883         break;
884     case NID_des_cbc:
885         *cipher = cryptodev_des_cbc();
886         break;
887     case NID_bf_cbc:
888         *cipher = cryptodev_bf_cbc();
889         break;
890     case NID_cast5_cbc:
891         *cipher = cryptodev_cast_cbc();
892         break;
893     case NID_aes_128_cbc:
894         *cipher = cryptodev_aes_cbc();
895         break;
896     case NID_aes_192_cbc:
897         *cipher = cryptodev_aes_192_cbc();
898         break;
899     case NID_aes_256_cbc:
900         *cipher = cryptodev_aes_256_cbc();
901         break;
902 # ifdef CRYPTO_AES_CTR
903     case NID_aes_128_ctr:
904         *cipher = cryptodev_aes_ctr();
905         break;
906     case NID_aes_192_ctr:
907         *cipher = cryptodev_aes_192_ctr();
908         break;
909     case NID_aes_256_ctr:
910         *cipher = cryptodev_aes_256_ctr();
911         break;
912 # endif
913     default:
914         *cipher = NULL;
915         break;
916     }
917     return (*cipher != NULL);
918 }
919
920 # ifdef USE_CRYPTODEV_DIGESTS
921
922 /* convert digest type to cryptodev */
923 static int digest_nid_to_cryptodev(int nid)
924 {
925     int i;
926
927     for (i = 0; digests[i].id; i++)
928         if (digests[i].nid == nid)
929             return (digests[i].id);
930     return (0);
931 }
932
933 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
934 {
935     struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
936     struct session_op *sess = &state->d_sess;
937     int digest;
938
939     if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
940         printf("cryptodev_digest_init: Can't get digest \n");
941         return (0);
942     }
943
944     memset(state, 0, sizeof(*state));
945
946     if ((state->d_fd = get_dev_crypto()) < 0) {
947         printf("cryptodev_digest_init: Can't get Dev \n");
948         return (0);
949     }
950
951     sess->mackey = NULL;
952     sess->mackeylen = 0;
953     sess->mac = digest;
954
955     if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
956         put_dev_crypto(state->d_fd);
957         state->d_fd = -1;
958         printf("cryptodev_digest_init: Open session failed\n");
959         return (0);
960     }
961
962     return (1);
963 }
964
965 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
966                                    size_t count)
967 {
968     struct crypt_op cryp;
969     struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
970     struct session_op *sess = &state->d_sess;
971     char *new_mac_data;
972
973     if (!data || state->d_fd < 0) {
974         printf("cryptodev_digest_update: illegal inputs \n");
975         return (0);
976     }
977
978     if (!count) {
979         return (0);
980     }
981
982     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
983         /* if application doesn't support one buffer */
984         new_mac_data =
985             OPENSSL_realloc(state->mac_data, state->mac_len + count);
986
987         if (!new_mac_data) {
988             printf("cryptodev_digest_update: realloc failed\n");
989             return (0);
990         }
991         state->mac_data = new_mac_data;
992
993         memcpy(state->mac_data + state->mac_len, data, count);
994         state->mac_len += count;
995
996         return (1);
997     }
998
999     memset(&cryp, 0, sizeof(cryp));
1000
1001     cryp.ses = sess->ses;
1002     cryp.flags = 0;
1003     cryp.len = count;
1004     cryp.src = (void *) data;
1005     cryp.dst = NULL;
1006     cryp.mac = (void *) state->digest_res;
1007     if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1008         printf("cryptodev_digest_update: digest failed\n");
1009         return (0);
1010     }
1011     return (1);
1012 }
1013
1014 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
1015 {
1016     struct crypt_op cryp;
1017     struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1018     struct session_op *sess = &state->d_sess;
1019
1020     int ret = 1;
1021
1022     if (!md || state->d_fd < 0) {
1023         printf("cryptodev_digest_final: illegal input\n");
1024         return (0);
1025     }
1026
1027     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
1028         /* if application doesn't support one buffer */
1029         memset(&cryp, 0, sizeof(cryp));
1030         cryp.ses = sess->ses;
1031         cryp.flags = 0;
1032         cryp.len = state->mac_len;
1033         cryp.src = state->mac_data;
1034         cryp.dst = NULL;
1035         cryp.mac = (void *) md;
1036         if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1037             printf("cryptodev_digest_final: digest failed\n");
1038             return (0);
1039         }
1040
1041         return 1;
1042     }
1043
1044     memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
1045
1046     return (ret);
1047 }
1048
1049 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
1050 {
1051     int ret = 1;
1052     struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1053     struct session_op *sess = &state->d_sess;
1054
1055     if (state == NULL)
1056         return 0;
1057
1058     if (state->d_fd < 0) {
1059         printf("cryptodev_digest_cleanup: illegal input\n");
1060         return (0);
1061     }
1062
1063     OPENSSL_free(state->mac_data);
1064     state->mac_data = NULL;
1065     state->mac_len = 0;
1066
1067     if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1068         printf("cryptodev_digest_cleanup: failed to close session\n");
1069         ret = 0;
1070     } else {
1071         ret = 1;
1072     }
1073     put_dev_crypto(state->d_fd);
1074     state->d_fd = -1;
1075
1076     return (ret);
1077 }
1078
1079 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1080 {
1081     struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1082     struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1083     struct session_op *sess;
1084     int digest;
1085
1086     if (dstate == NULL || fstate == NULL)
1087         return 1;
1088
1089     memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1090
1091     sess = &dstate->d_sess;
1092
1093     digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1094
1095     sess->mackey = NULL;
1096     sess->mackeylen = 0;
1097     sess->mac = digest;
1098
1099     dstate->d_fd = get_dev_crypto();
1100
1101     if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1102         put_dev_crypto(dstate->d_fd);
1103         dstate->d_fd = -1;
1104         printf("cryptodev_digest_copy: Open session failed\n");
1105         return (0);
1106     }
1107
1108     if (fstate->mac_len != 0) {
1109         if (fstate->mac_data != NULL) {
1110             dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1111             if (dstate->mac_data == NULL) {
1112                 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1113                 return (0);
1114             }
1115             memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1116             dstate->mac_len = fstate->mac_len;
1117         }
1118     }
1119
1120     return 1;
1121 }
1122
1123 static EVP_MD *sha1_md = NULL;
1124 static const EVP_MD *cryptodev_sha1(void)
1125 {
1126     if (sha1_md == NULL) {
1127         EVP_MD *md;
1128
1129         if ((md = EVP_MD_meth_new(NID_sha1, NID_undef)) == NULL
1130             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1131             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1132             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1133             || !EVP_MD_meth_set_app_datasize(md,
1134                                              sizeof(struct dev_crypto_state))
1135             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1136             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1137             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1138             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1139             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1140             EVP_MD_meth_free(md);
1141             md = NULL;
1142         }
1143         sha1_md = md;
1144     }
1145     return sha1_md;
1146 }
1147
1148 static EVP_MD *sha256_md = NULL;
1149 static const EVP_MD *cryptodev_sha256(void)
1150 {
1151     if (sha256_md == NULL) {
1152         EVP_MD *md;
1153
1154         if ((md = EVP_MD_meth_new(NID_sha256, NID_undef)) == NULL
1155             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1156             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1157             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1158             || !EVP_MD_meth_set_app_datasize(md,
1159                                              sizeof(struct dev_crypto_state))
1160             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1161             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1162             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1163             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1164             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1165             EVP_MD_meth_free(md);
1166             md = NULL;
1167         }
1168         sha256_md = md;
1169     }
1170     return sha256_md;
1171 }
1172
1173 static EVP_MD *sha224_md = NULL;
1174 static const EVP_MD *cryptodev_sha224(void)
1175 {
1176     if (sha224_md == NULL) {
1177         EVP_MD *md;
1178
1179         if ((md = EVP_MD_meth_new(NID_sha224, NID_undef)) == NULL
1180             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1181             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1182             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1183             || !EVP_MD_meth_set_app_datasize(md,
1184                                              sizeof(struct dev_crypto_state))
1185             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1186             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1187             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1188             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1189             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1190             EVP_MD_meth_free(md);
1191             md = NULL;
1192         }
1193         sha224_md = md;
1194     }
1195     return sha224_md;
1196 }
1197
1198 static EVP_MD *sha384_md = NULL;
1199 static const EVP_MD *cryptodev_sha384(void)
1200 {
1201     if (sha384_md == NULL) {
1202         EVP_MD *md;
1203
1204         if ((md = EVP_MD_meth_new(NID_sha384, NID_undef)) == NULL
1205             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1206             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1207             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1208             || !EVP_MD_meth_set_app_datasize(md,
1209                                              sizeof(struct dev_crypto_state))
1210             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1211             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1212             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1213             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1214             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1215             EVP_MD_meth_free(md);
1216             md = NULL;
1217         }
1218         sha384_md = md;
1219     }
1220     return sha384_md;
1221 }
1222
1223 static EVP_MD *sha512_md = NULL;
1224 static const EVP_MD *cryptodev_sha512(void)
1225 {
1226     if (sha512_md == NULL) {
1227         EVP_MD *md;
1228
1229         if ((md = EVP_MD_meth_new(NID_sha512, NID_undef)) == NULL
1230             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1231             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1232             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1233             || !EVP_MD_meth_set_app_datasize(md,
1234                                              sizeof(struct dev_crypto_state))
1235             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1236             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1237             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1238             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1239             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1240             EVP_MD_meth_free(md);
1241             md = NULL;
1242         }
1243         sha512_md = md;
1244     }
1245     return sha512_md;
1246 }
1247
1248 static EVP_MD *md5_md = NULL;
1249 static const EVP_MD *cryptodev_md5(void)
1250 {
1251     if (md5_md == NULL) {
1252         EVP_MD *md;
1253
1254         if ((md = EVP_MD_meth_new(NID_md5, NID_undef)) == NULL
1255             || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */)
1256             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1257             || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */)
1258             || !EVP_MD_meth_set_app_datasize(md,
1259                                              sizeof(struct dev_crypto_state))
1260             || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1261             || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1262             || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1263             || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1264             || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1265             EVP_MD_meth_free(md);
1266             md = NULL;
1267         }
1268         md5_md = md;
1269     }
1270     return md5_md;
1271 }
1272
1273 # endif                         /* USE_CRYPTODEV_DIGESTS */
1274
1275 static int
1276 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1277                          const int **nids, int nid)
1278 {
1279     if (!digest)
1280         return (cryptodev_usable_digests(nids));
1281
1282     switch (nid) {
1283 # ifdef USE_CRYPTODEV_DIGESTS
1284     case NID_md5:
1285         *digest = cryptodev_md5();
1286         break;
1287     case NID_sha1:
1288         *digest = cryptodev_sha1();
1289         break;
1290     case NID_sha256:
1291         *digest = cryptodev_sha256();
1292         break;
1293     case NID_sha224:
1294         *digest = cryptodev_sha224();
1295         break;
1296     case NID_sha384:
1297         *digest = cryptodev_sha384();
1298         break;
1299     case NID_sha512:
1300         *digest = cryptodev_sha512();
1301         break;
1302     default:
1303 # endif                         /* USE_CRYPTODEV_DIGESTS */
1304         *digest = NULL;
1305         break;
1306     }
1307     return (*digest != NULL);
1308 }
1309
1310 static int cryptodev_engine_destroy(ENGINE *e)
1311 {
1312     EVP_CIPHER_meth_free(rc4_cipher);
1313     rc4_cipher = NULL;
1314     EVP_CIPHER_meth_free(des_cbc_cipher);
1315     des_cbc_cipher = NULL;
1316     EVP_CIPHER_meth_free(des3_cbc_cipher);
1317     des3_cbc_cipher = NULL;
1318     EVP_CIPHER_meth_free(bf_cbc_cipher);
1319     bf_cbc_cipher = NULL;
1320     EVP_CIPHER_meth_free(cast_cbc_cipher);
1321     cast_cbc_cipher = NULL;
1322     EVP_CIPHER_meth_free(aes_cbc_cipher);
1323     aes_cbc_cipher = NULL;
1324     EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1325     aes_192_cbc_cipher = NULL;
1326     EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1327     aes_256_cbc_cipher = NULL;
1328 # ifdef CRYPTO_AES_CTR
1329     EVP_CIPHER_meth_free(aes_ctr_cipher);
1330     aes_ctr_cipher = NULL;
1331     EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1332     aes_192_ctr_cipher = NULL;
1333     EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1334     aes_256_ctr_cipher = NULL;
1335 # endif
1336 # ifdef USE_CRYPTODEV_DIGESTS
1337     EVP_MD_meth_free(sha1_md);
1338     sha1_md = NULL;
1339     EVP_MD_meth_free(sha256_md);
1340     sha256_md = NULL;
1341     EVP_MD_meth_free(sha224_md);
1342     sha224_md = NULL;
1343     EVP_MD_meth_free(sha384_md);
1344     sha384_md = NULL;
1345     EVP_MD_meth_free(sha512_md);
1346     sha512_md = NULL;
1347     EVP_MD_meth_free(md5_md);
1348     md5_md = NULL;
1349 # endif
1350     RSA_meth_free(cryptodev_rsa);
1351     cryptodev_rsa = NULL;
1352 # ifndef OPENSSL_NO_DSA
1353     DSA_meth_free(cryptodev_dsa);
1354     cryptodev_dsa = NULL;
1355 # endif
1356 # ifndef OPENSSL_NO_DH
1357     DH_meth_free(cryptodev_dh);
1358     cryptodev_dh = NULL;
1359 # endif
1360     return 1;
1361 }
1362
1363 /*
1364  * Convert a BIGNUM to the representation that /dev/crypto needs.
1365  * Upon completion of use, the caller is responsible for freeing
1366  * crp->crp_p.
1367  */
1368 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1369 {
1370     ssize_t bytes, bits;
1371     u_char *b;
1372
1373     crp->crp_p = NULL;
1374     crp->crp_nbits = 0;
1375
1376     bits = BN_num_bits(a);
1377     bytes = BN_num_bytes(a);
1378
1379     b = OPENSSL_zalloc(bytes);
1380     if (b == NULL)
1381         return (1);
1382
1383     crp->crp_p = (void *) b;
1384     crp->crp_nbits = bits;
1385
1386     BN_bn2bin(a, b);
1387     return (0);
1388 }
1389
1390 /* Convert a /dev/crypto parameter to a BIGNUM */
1391 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1392 {
1393     u_int8_t *pd;
1394     int i, bytes;
1395
1396     bytes = (crp->crp_nbits + 7) / 8;
1397
1398     if (bytes == 0)
1399         return (-1);
1400
1401     if ((pd = OPENSSL_malloc(bytes)) == NULL)
1402         return (-1);
1403
1404     for (i = 0; i < bytes; i++)
1405         pd[i] = crp->crp_p[bytes - i - 1];
1406
1407     BN_bin2bn(pd, bytes, a);
1408     free(pd);
1409
1410     return (0);
1411 }
1412
1413 static void zapparams(struct crypt_kop *kop)
1414 {
1415     int i;
1416
1417     for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1418         OPENSSL_free(kop->crk_param[i].crp_p);
1419         kop->crk_param[i].crp_p = NULL;
1420         kop->crk_param[i].crp_nbits = 0;
1421     }
1422 }
1423
1424 static int
1425 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
1426                BIGNUM *s)
1427 {
1428     int fd, ret = -1;
1429
1430     if ((fd = get_asym_dev_crypto()) < 0)
1431         return ret;
1432
1433     if (r) {
1434         kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_zalloc(rlen);
1435         if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
1436             return ret;
1437         kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1438         kop->crk_oparams++;
1439     }
1440     if (s) {
1441         kop->crk_param[kop->crk_iparams + 1].crp_p =
1442             OPENSSL_zalloc(slen);
1443         /* No need to free the kop->crk_iparams parameter if it was allocated,
1444          * callers of this routine have to free allocated parameters through
1445          * zapparams both in case of success and failure
1446          */
1447         if (kop->crk_param[kop->crk_iparams+1].crp_p == NULL)
1448             return ret;
1449         kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1450         kop->crk_oparams++;
1451     }
1452
1453     if (ioctl(fd, CIOCKEY, kop) == 0) {
1454         if (r)
1455             crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1456         if (s)
1457             crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1458         ret = 0;
1459     }
1460
1461     return ret;
1462 }
1463
1464 static int
1465 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1466                      const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1467 {
1468     struct crypt_kop kop;
1469     int ret = 1;
1470
1471     /*
1472      * Currently, we know we can do mod exp iff we can do any asymmetric
1473      * operations at all.
1474      */
1475     if (cryptodev_asymfeat == 0) {
1476         ret = BN_mod_exp(r, a, p, m, ctx);
1477         return (ret);
1478     }
1479
1480     memset(&kop, 0, sizeof(kop));
1481     kop.crk_op = CRK_MOD_EXP;
1482
1483     /* inputs: a^p % m */
1484     if (bn2crparam(a, &kop.crk_param[0]))
1485         goto err;
1486     if (bn2crparam(p, &kop.crk_param[1]))
1487         goto err;
1488     if (bn2crparam(m, &kop.crk_param[2]))
1489         goto err;
1490     kop.crk_iparams = 3;
1491
1492     if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1493         const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1494         printf("OCF asym process failed, Running in software\n");
1495         ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1496
1497     } else if (ECANCELED == kop.crk_status) {
1498         const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1499         printf("OCF hardware operation cancelled. Running in Software\n");
1500         ret = RSA_meth_get_bn_mod_exp(meth)(r, a, p, m, ctx, in_mont);
1501     }
1502     /* else cryptodev operation worked ok ==> ret = 1 */
1503
1504  err:
1505     zapparams(&kop);
1506     return (ret);
1507 }
1508
1509 static int
1510 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
1511                             BN_CTX *ctx)
1512 {
1513     int r;
1514     const BIGNUM *n = NULL;
1515     const BIGNUM *d = NULL;
1516
1517     ctx = BN_CTX_new();
1518     RSA_get0_key(rsa, &n, NULL, &d);
1519     r = cryptodev_bn_mod_exp(r0, I, d, n, ctx, NULL);
1520     BN_CTX_free(ctx);
1521     return (r);
1522 }
1523
1524 static int
1525 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1526 {
1527     struct crypt_kop kop;
1528     int ret = 1;
1529     const BIGNUM *p = NULL;
1530     const BIGNUM *q = NULL;
1531     const BIGNUM *dmp1 = NULL;
1532     const BIGNUM *dmq1 = NULL;
1533     const BIGNUM *iqmp = NULL;
1534     const BIGNUM *n = NULL;
1535
1536     RSA_get0_factors(rsa, &p, &q);
1537     RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
1538     RSA_get0_key(rsa, &n, NULL, NULL);
1539
1540     if (!p || !q || !dmp1 || !dmq1 || !iqmp) {
1541         /* XXX 0 means failure?? */
1542         return (0);
1543     }
1544
1545     memset(&kop, 0, sizeof(kop));
1546     kop.crk_op = CRK_MOD_EXP_CRT;
1547     /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1548     if (bn2crparam(p, &kop.crk_param[0]))
1549         goto err;
1550     if (bn2crparam(q, &kop.crk_param[1]))
1551         goto err;
1552     if (bn2crparam(I, &kop.crk_param[2]))
1553         goto err;
1554     if (bn2crparam(dmp1, &kop.crk_param[3]))
1555         goto err;
1556     if (bn2crparam(dmq1, &kop.crk_param[4]))
1557         goto err;
1558     if (bn2crparam(iqmp, &kop.crk_param[5]))
1559         goto err;
1560     kop.crk_iparams = 6;
1561
1562     if (cryptodev_asym(&kop, BN_num_bytes(n), r0, 0, NULL)) {
1563         const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1564         printf("OCF asym process failed, running in Software\n");
1565         ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1566
1567     } else if (ECANCELED == kop.crk_status) {
1568         const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1569         printf("OCF hardware operation cancelled. Running in Software\n");
1570         ret = RSA_meth_get_mod_exp(meth)(r0, I, rsa, ctx);
1571     }
1572     /* else cryptodev operation worked ok ==> ret = 1 */
1573
1574  err:
1575     zapparams(&kop);
1576     return (ret);
1577 }
1578
1579 # ifndef OPENSSL_NO_DSA
1580 static int
1581 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1582                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1583 {
1584     return cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx);
1585 }
1586
1587 static int
1588 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
1589                           const BIGNUM *u1, const BIGNUM *pub_key,
1590                           const BIGNUM *u2, const BIGNUM *p, BN_CTX *ctx,
1591                           BN_MONT_CTX *mont)
1592 {
1593     const BIGNUM *dsag, *dsap, *dsapub_key;
1594     BIGNUM *t2;
1595     int ret = 0;
1596     const DSA_METHOD *meth;
1597     int (*bn_mod_exp)(DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
1598                       BN_CTX *, BN_MONT_CTX *);
1599
1600     t2 = BN_new();
1601     if (t2 == NULL)
1602         goto err;
1603
1604     /* v = ( g^u1 * y^u2 mod p ) mod q */
1605     /* let t1 = g ^ u1 mod p */
1606     ret = 0;
1607
1608     DSA_get0_pqg(dsa, &dsap, NULL, &dsag);
1609     DSA_get0_key(dsa, &dsapub_key, NULL);
1610
1611     meth = DSA_get_method(dsa);
1612     if (meth == NULL)
1613         goto err;
1614     bn_mod_exp = DSA_meth_get_bn_mod_exp(meth);
1615     if (bn_mod_exp == NULL)
1616         goto err;
1617
1618     if (!bn_mod_exp(dsa, t1, dsag, u1, dsap, ctx, mont))
1619         goto err;
1620
1621     /* let t2 = y ^ u2 mod p */
1622     if (!bn_mod_exp(dsa, t2, dsapub_key, u2, dsap, ctx, mont))
1623         goto err;
1624     /* let t1 = t1 * t2 mod p */
1625     if (!BN_mod_mul(t1, t1, t2, dsap, ctx))
1626         goto err;
1627
1628     ret = 1;
1629  err:
1630     BN_free(t2);
1631     return (ret);
1632 }
1633
1634 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1635                                       DSA *dsa)
1636 {
1637     struct crypt_kop kop;
1638     BIGNUM *r, *s;
1639     const BIGNUM *dsap = NULL, *dsaq = NULL, *dsag = NULL;
1640     const BIGNUM *priv_key = NULL;
1641     DSA_SIG *dsasig, *dsaret = NULL;
1642
1643     dsasig = DSA_SIG_new();
1644     if (dsasig == NULL)
1645         goto err;
1646
1647     memset(&kop, 0, sizeof(kop));
1648     kop.crk_op = CRK_DSA_SIGN;
1649
1650     /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1651     kop.crk_param[0].crp_p = (void *) dgst;
1652     kop.crk_param[0].crp_nbits = dlen * 8;
1653     DSA_get0_pqg(dsa, &dsap, &dsaq, &dsag);
1654     DSA_get0_key(dsa, NULL, &priv_key);
1655     if (bn2crparam(dsap, &kop.crk_param[1]))
1656         goto err;
1657     if (bn2crparam(dsaq, &kop.crk_param[2]))
1658         goto err;
1659     if (bn2crparam(dsag, &kop.crk_param[3]))
1660         goto err;
1661     if (bn2crparam(priv_key, &kop.crk_param[4]))
1662         goto err;
1663     kop.crk_iparams = 5;
1664
1665     r = BN_new();
1666     if (r == NULL)
1667         goto err;
1668     s = BN_new();
1669     if (s == NULL)
1670         goto err;
1671     if (cryptodev_asym(&kop, BN_num_bytes(dsaq), r,
1672                        BN_num_bytes(dsaq), s) == 0) {
1673         DSA_SIG_set0(dsasig, r, s);
1674         dsaret = dsasig;
1675     } else {
1676         dsaret = DSA_meth_get_sign(DSA_OpenSSL())(dgst, dlen, dsa);
1677     }
1678  err:
1679     if (dsaret != dsasig)
1680         DSA_SIG_free(dsasig);
1681     kop.crk_param[0].crp_p = NULL;
1682     zapparams(&kop);
1683     return dsaret;
1684 }
1685
1686 static int
1687 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1688                      DSA_SIG *sig, DSA *dsa)
1689 {
1690     struct crypt_kop kop;
1691     int dsaret = 1;
1692     const BIGNUM *pr, *ps, *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL;
1693
1694     memset(&kop, 0, sizeof(kop));
1695     kop.crk_op = CRK_DSA_VERIFY;
1696
1697     /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1698     kop.crk_param[0].crp_p = (void *) dgst;
1699     kop.crk_param[0].crp_nbits = dlen * 8;
1700     DSA_get0_pqg(dsa, &p, &q, &g);
1701     if (bn2crparam(p, &kop.crk_param[1]))
1702         goto err;
1703     if (bn2crparam(q, &kop.crk_param[2]))
1704         goto err;
1705     if (bn2crparam(g, &kop.crk_param[3]))
1706         goto err;
1707     DSA_get0_key(dsa, &pub_key, NULL);
1708     if (bn2crparam(pub_key, &kop.crk_param[4]))
1709         goto err;
1710     DSA_SIG_get0(sig, &pr, &ps);
1711     if (bn2crparam(pr, &kop.crk_param[5]))
1712         goto err;
1713     if (bn2crparam(ps, &kop.crk_param[6]))
1714         goto err;
1715     kop.crk_iparams = 7;
1716
1717     if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1718         /*
1719          * OCF success value is 0, if not zero, change dsaret to fail
1720          */
1721         if (0 != kop.crk_status)
1722             dsaret = 0;
1723     } else {
1724         dsaret = DSA_meth_get_verify(DSA_OpenSSL())(dgst, dlen, sig, dsa);
1725     }
1726  err:
1727     kop.crk_param[0].crp_p = NULL;
1728     zapparams(&kop);
1729     return (dsaret);
1730 }
1731 # endif
1732
1733 # ifndef OPENSSL_NO_DH
1734 static int
1735 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1736                      const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1737                      BN_MONT_CTX *m_ctx)
1738 {
1739     return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1740 }
1741
1742 static int
1743 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1744 {
1745     struct crypt_kop kop;
1746     int dhret = 1;
1747     int fd, keylen;
1748     const BIGNUM *p = NULL;
1749     const BIGNUM *priv_key = NULL;
1750
1751     if ((fd = get_asym_dev_crypto()) < 0) {
1752         const DH_METHOD *meth = DH_OpenSSL();
1753
1754         return DH_meth_get_compute_key(meth)(key, pub_key, dh);
1755     }
1756
1757     DH_get0_pqg(dh, &p, NULL, NULL);
1758     DH_get0_key(dh, NULL, &priv_key);
1759
1760     keylen = BN_num_bits(p);
1761
1762     memset(&kop, 0, sizeof(kop));
1763     kop.crk_op = CRK_DH_COMPUTE_KEY;
1764
1765     /* inputs: dh->priv_key pub_key dh->p key */
1766     if (bn2crparam(priv_key, &kop.crk_param[0]))
1767         goto err;
1768     if (bn2crparam(pub_key, &kop.crk_param[1]))
1769         goto err;
1770     if (bn2crparam(p, &kop.crk_param[2]))
1771         goto err;
1772     kop.crk_iparams = 3;
1773
1774     kop.crk_param[3].crp_p = (void *) key;
1775     kop.crk_param[3].crp_nbits = keylen * 8;
1776     kop.crk_oparams = 1;
1777
1778     if (ioctl(fd, CIOCKEY, &kop) == -1) {
1779         const DH_METHOD *meth = DH_OpenSSL();
1780
1781         dhret = DH_meth_get_compute_key(meth)(key, pub_key, dh);
1782     }
1783  err:
1784     kop.crk_param[3].crp_p = NULL;
1785     zapparams(&kop);
1786     return (dhret);
1787 }
1788
1789 # endif /* ndef OPENSSL_NO_DH */
1790
1791 /*
1792  * ctrl right now is just a wrapper that doesn't do much
1793  * but I expect we'll want some options soon.
1794  */
1795 static int
1796 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1797 {
1798 # ifdef HAVE_SYSLOG_R
1799     struct syslog_data sd = SYSLOG_DATA_INIT;
1800 # endif
1801
1802     switch (cmd) {
1803     default:
1804 # ifdef HAVE_SYSLOG_R
1805         syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1806 # else
1807         syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1808 # endif
1809         break;
1810     }
1811     return (1);
1812 }
1813
1814 void engine_load_cryptodev_int(void)
1815 {
1816     ENGINE *engine = ENGINE_new();
1817     int fd;
1818
1819     if (engine == NULL)
1820         return;
1821     if ((fd = get_dev_crypto()) < 0) {
1822         ENGINE_free(engine);
1823         return;
1824     }
1825
1826     /*
1827      * find out what asymmetric crypto algorithms we support
1828      */
1829     if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1830         put_dev_crypto(fd);
1831         ENGINE_free(engine);
1832         return;
1833     }
1834     put_dev_crypto(fd);
1835
1836     if (!ENGINE_set_id(engine, "cryptodev") ||
1837         !ENGINE_set_name(engine, "cryptodev engine") ||
1838         !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1839         !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1840         !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1841         !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1842         !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1843         ENGINE_free(engine);
1844         return;
1845     }
1846
1847     cryptodev_rsa = RSA_meth_dup(RSA_PKCS1_OpenSSL());
1848     if (cryptodev_rsa != NULL) {
1849         RSA_meth_set1_name(cryptodev_rsa, "cryptodev RSA method");
1850         RSA_meth_set_flags(cryptodev_rsa, 0);
1851         if (ENGINE_set_RSA(engine, cryptodev_rsa)) {
1852             if (cryptodev_asymfeat & CRF_MOD_EXP) {
1853                 RSA_meth_set_bn_mod_exp(cryptodev_rsa, cryptodev_bn_mod_exp);
1854                 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1855                     RSA_meth_set_mod_exp(cryptodev_rsa, cryptodev_rsa_mod_exp);
1856                 else
1857                     RSA_meth_set_mod_exp(cryptodev_rsa,
1858                                          cryptodev_rsa_nocrt_mod_exp);
1859             }
1860         }
1861     } else {
1862         ENGINE_free(engine);
1863         return;
1864     }
1865
1866 # ifndef OPENSSL_NO_DSA
1867     cryptodev_dsa = DSA_meth_dup(DSA_OpenSSL());
1868     if (cryptodev_dsa != NULL) {
1869         DSA_meth_set1_name(cryptodev_dsa, "cryptodev DSA method");
1870         DSA_meth_set_flags(cryptodev_dsa, 0);
1871         if (ENGINE_set_DSA(engine, cryptodev_dsa)) {
1872             if (cryptodev_asymfeat & CRF_DSA_SIGN)
1873                 DSA_meth_set_sign(cryptodev_dsa, cryptodev_dsa_do_sign);
1874             if (cryptodev_asymfeat & CRF_MOD_EXP) {
1875                 DSA_meth_set_bn_mod_exp(cryptodev_dsa,
1876                                         cryptodev_dsa_bn_mod_exp);
1877                 DSA_meth_set_mod_exp(cryptodev_dsa, cryptodev_dsa_dsa_mod_exp);
1878             }
1879             if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1880                 DSA_meth_set_verify(cryptodev_dsa, cryptodev_dsa_verify);
1881         }
1882     } else {
1883         ENGINE_free(engine);
1884         return;
1885     }
1886 # endif
1887
1888 # ifndef OPENSSL_NO_DH
1889     cryptodev_dh = DH_meth_dup(DH_OpenSSL());
1890     if (cryptodev_dh != NULL) {
1891         DH_meth_set1_name(cryptodev_dh, "cryptodev DH method");
1892         DH_meth_set_flags(cryptodev_dh, 0);
1893         if (ENGINE_set_DH(engine, cryptodev_dh)) {
1894             if (cryptodev_asymfeat & CRF_MOD_EXP) {
1895                 DH_meth_set_bn_mod_exp(cryptodev_dh, cryptodev_mod_exp_dh);
1896                 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1897                     DH_meth_set_compute_key(cryptodev_dh,
1898                                             cryptodev_dh_compute_key);
1899             }
1900         }
1901     } else {
1902         ENGINE_free(engine);
1903         return;
1904     }
1905 # endif
1906
1907     ENGINE_add(engine);
1908     ENGINE_free(engine);
1909     ERR_clear_error();
1910 }
1911
1912 #endif                          /* HAVE_CRYPTODEV */