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