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