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