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