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