2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
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>
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
51 # if (OpenBSD >= 200110)
52 # define HAVE_SYSLOG_R
56 #include <sys/types.h>
58 # include <crypto/cryptodev.h>
59 # include <sys/ioctl.h>
69 #include <openssl/dh.h>
70 #include <openssl/dsa.h>
71 #include <openssl/err.h>
72 #include <openssl/rsa.h>
74 #ifndef HAVE_CRYPTODEV
76 void engine_load_cryptodev_int(void)
78 /* This is a NOP on platforms without /dev/crypto */
84 /* Available on cryptodev-linux but not on FreeBSD 8.4 */
85 # ifndef CRYPTO_HMAC_MAX_KEY_LEN
86 # define CRYPTO_HMAC_MAX_KEY_LEN 512
88 # ifndef CRYPTO_CIPHER_MAX_KEY_LEN
89 # define CRYPTO_CIPHER_MAX_KEY_LEN 64
92 struct dev_crypto_state {
93 struct session_op d_sess;
95 # ifdef USE_CRYPTODEV_DIGESTS
96 unsigned char digest_res[HASH_MAX_LEN];
102 static u_int32_t cryptodev_asymfeat = 0;
104 static RSA_METHOD *cryptodev_rsa;
105 # ifndef OPENSSL_NO_DSA
106 static DSA_METHOD *cryptodev_dsa = NULL;
108 # ifndef OPENSSL_NO_DH
109 static DH_METHOD *cryptodev_dh;
112 static int get_asym_dev_crypto(void);
113 static int open_dev_crypto(void);
114 static int get_dev_crypto(void);
115 static int get_cryptodev_ciphers(const int **cnids);
116 # ifdef USE_CRYPTODEV_DIGESTS
117 static int get_cryptodev_digests(const int **cnids);
119 static int cryptodev_usable_ciphers(const int **nids);
120 static int cryptodev_usable_digests(const int **nids);
121 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
122 const unsigned char *in, size_t inl);
123 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
124 const unsigned char *iv, int enc);
125 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
126 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
127 const int **nids, int nid);
128 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
129 const int **nids, int nid);
130 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
131 static int crparam2bn(struct crparam *crp, BIGNUM *a);
132 static void zapparams(struct crypt_kop *kop);
133 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
134 int slen, BIGNUM *s);
136 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
137 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
139 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
141 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
143 # ifndef OPENSSL_NO_DSA
144 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a,
145 const BIGNUM *p, const BIGNUM *m,
146 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
147 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
148 const BIGNUM *u1, const BIGNUM *pub_key,
149 const BIGNUM *u2, const BIGNUM *p,
150 BN_CTX *ctx, BN_MONT_CTX *mont);
151 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
153 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
154 DSA_SIG *sig, DSA *dsa);
156 # ifndef OPENSSL_NO_DH
157 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
158 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
160 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
163 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
165 void engine_load_cryptodev_int(void);
167 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
177 {CRYPTO_ARC4, NID_rc4, 0, 16},
178 {CRYPTO_DES_CBC, NID_des_cbc, 8, 8},
179 {CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24},
180 # if !defined(CRYPTO_ALGORITHM_MIN) || defined(CRYPTO_3DES_ECB)
181 {CRYPTO_3DES_ECB, NID_des_ede3_ecb, 0, 24},
183 {CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16},
184 {CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24},
185 {CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32},
186 # if !defined(CRYPTO_ALGORITHM_MIN) || defined(CRYPTO_AES_CTR)
187 {CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16},
188 {CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24},
189 {CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32},
191 # if !defined(CRYPTO_ALGORITHM_MIN) || defined(CRYPTO_AES_ECB)
192 {CRYPTO_AES_ECB, NID_aes_128_ecb, 0, 16},
193 {CRYPTO_AES_ECB, NID_aes_192_ecb, 0, 24},
194 {CRYPTO_AES_ECB, NID_aes_256_ecb, 0, 32},
196 {CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16},
197 {CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16},
198 {CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0},
199 {0, NID_undef, 0, 0},
202 # ifdef USE_CRYPTODEV_DIGESTS
208 {CRYPTO_MD5, NID_md5, 16},
209 {CRYPTO_SHA1, NID_sha1, 20},
210 {CRYPTO_SHA2_256, NID_sha256, 32},
211 {CRYPTO_SHA2_384, NID_sha384, 48},
212 {CRYPTO_SHA2_512, NID_sha512, 64},
218 * Return a fd if /dev/crypto seems usable, 0 otherwise.
220 static int open_dev_crypto(void)
225 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
228 if (fcntl(fd, F_SETFD, 1) == -1) {
237 static int get_dev_crypto(void)
241 if ((fd = open_dev_crypto()) == -1)
243 # ifndef CRIOGET_NOT_NEEDED
244 if (ioctl(fd, CRIOGET, &retfd) == -1)
248 if (fcntl(retfd, F_SETFD, 1) == -1) {
258 static void put_dev_crypto(int fd)
260 # ifndef CRIOGET_NOT_NEEDED
265 /* Caching version for asym operations */
266 static int get_asym_dev_crypto(void)
271 fd = get_dev_crypto();
276 * Find out what ciphers /dev/crypto will let us have a session for.
277 * XXX note, that some of these openssl doesn't deal with yet!
278 * returning them here is harmless, as long as we return NULL
279 * when asked for a handler in the cryptodev_engine_ciphers routine
281 static int get_cryptodev_ciphers(const int **cnids)
283 static int nids[CRYPTO_ALGORITHM_MAX];
284 struct session_op sess;
285 int fd, i, count = 0;
286 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
288 if ((fd = get_dev_crypto()) < 0) {
292 memset(&sess, 0, sizeof(sess));
293 sess.key = (void *)fake_key;
295 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
296 if (ciphers[i].nid == NID_undef)
298 sess.cipher = ciphers[i].id;
299 sess.keylen = ciphers[i].keylen;
301 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
302 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
303 nids[count++] = ciphers[i].nid;
314 # ifdef USE_CRYPTODEV_DIGESTS
316 * Find out what digests /dev/crypto will let us have a session for.
317 * XXX note, that some of these openssl doesn't deal with yet!
318 * returning them here is harmless, as long as we return NULL
319 * when asked for a handler in the cryptodev_engine_digests routine
321 static int get_cryptodev_digests(const int **cnids)
323 static int nids[CRYPTO_ALGORITHM_MAX];
324 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
325 struct session_op sess;
326 int fd, i, count = 0;
328 if ((fd = get_dev_crypto()) < 0) {
332 memset(&sess, 0, sizeof(sess));
333 sess.mackey = fake_key;
334 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
335 if (digests[i].nid == NID_undef)
337 sess.mac = digests[i].id;
340 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
341 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
342 nids[count++] = digests[i].nid;
355 * Find the useable ciphers|digests from dev/crypto - this is the first
356 * thing called by the engine init crud which determines what it
357 * can use for ciphers from this engine. We want to return
358 * only what we can do, anything else is handled by software.
360 * If we can't initialize the device to do anything useful for
361 * any reason, we want to return a NULL array, and 0 length,
362 * which forces everything to be done is software. By putting
363 * the initialization of the device in here, we ensure we can
364 * use this engine as the default, and if for whatever reason
365 * /dev/crypto won't do what we want it will just be done in
368 * This can (should) be greatly expanded to perhaps take into
369 * account speed of the device, and what we want to do.
370 * (although the disabling of particular alg's could be controlled
371 * by the device driver with sysctl's.) - this is where we
372 * want most of the decisions made about what we actually want
373 * to use from /dev/crypto.
375 static int cryptodev_usable_ciphers(const int **nids)
377 return (get_cryptodev_ciphers(nids));
380 static int cryptodev_usable_digests(const int **nids)
382 # ifdef USE_CRYPTODEV_DIGESTS
383 return (get_cryptodev_digests(nids));
386 * XXXX just disable all digests for now, because it sucks.
387 * we need a better way to decide this - i.e. I may not
388 * want digests on slow cards like hifn on fast machines,
389 * but might want them on slow or loaded machines, etc.
390 * will also want them when using crypto cards that don't
391 * suck moose gonads - would be nice to be able to decide something
392 * as reasonable default without having hackery that's card dependent.
393 * of course, the default should probably be just do everything,
394 * with perhaps a sysctl to turn algorithms off (or have them off
395 * by default) on cards that generally suck like the hifn.
403 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
404 const unsigned char *in, size_t inl)
406 struct crypt_op cryp;
407 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
408 struct session_op *sess = &state->d_sess;
410 unsigned char save_iv[EVP_MAX_IV_LENGTH];
416 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
419 memset(&cryp, 0, sizeof(cryp));
421 cryp.ses = sess->ses;
424 cryp.src = (void *)in;
425 cryp.dst = (void *)out;
428 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
430 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
431 cryp.iv = (void *)EVP_CIPHER_CTX_iv(ctx);
432 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
433 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
434 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
439 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
441 * XXX need better error handling this can fail for a number of
447 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
448 if (EVP_CIPHER_CTX_encrypting(ctx))
449 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
452 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
453 EVP_CIPHER_CTX_iv_length(ctx));
459 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
460 const unsigned char *iv, int enc)
462 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
463 struct session_op *sess = &state->d_sess;
466 for (i = 0; ciphers[i].id; i++)
467 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
468 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
469 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
470 cipher = ciphers[i].id;
474 if (!ciphers[i].id) {
479 memset(sess, 0, sizeof(*sess));
481 if ((state->d_fd = get_dev_crypto()) < 0)
484 sess->key = (void *)key;
485 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
486 sess->cipher = cipher;
488 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
489 put_dev_crypto(state->d_fd);
497 * free anything we allocated earlier when initing a
498 * session, and close the session.
500 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
503 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
504 struct session_op *sess = &state->d_sess;
510 * XXX if this ioctl fails, something's wrong. the invoker may have called
511 * us with a bogus ctx, or we could have a device that for whatever
512 * reason just doesn't want to play ball - it's not clear what's right
513 * here - should this be an error? should it just increase a counter,
514 * hmm. For right now, we return 0 - I don't believe that to be "right".
515 * we could call the gorpy openssl lib error handlers that print messages
516 * to users of the library. hmm..
519 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
524 put_dev_crypto(state->d_fd);
531 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
532 * gets called when libcrypto requests a cipher NID.
535 static int cryptodev_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void *p2)
537 struct dev_crypto_state *state = ctx->cipher_data;
538 struct session_op *sess = &state->d_sess;
540 if (type == EVP_CTRL_COPY) {
541 EVP_CIPHER_CTX *out = p2;
542 return cryptodev_init_key(out, sess->key, ctx->iv, 0);
549 static EVP_CIPHER *rc4_cipher = NULL;
550 static const EVP_CIPHER *cryptodev_rc4(void)
552 if (rc4_cipher == NULL) {
553 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16);
556 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
557 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH
558 | EVP_CIPH_CUSTOM_COPY)
559 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
560 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
561 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
562 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
563 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
565 dev_crypto_state))) {
566 EVP_CIPHER_meth_free(cipher);
575 static EVP_CIPHER *des_cbc_cipher = NULL;
576 static const EVP_CIPHER *cryptodev_des_cbc(void)
578 if (des_cbc_cipher == NULL) {
579 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8);
582 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
583 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
584 | EVP_CIPH_CUSTOM_COPY)
585 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
586 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
587 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
588 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
589 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
592 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
593 EVP_CIPHER_set_asn1_iv)
594 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
595 EVP_CIPHER_get_asn1_iv)) {
596 EVP_CIPHER_meth_free(cipher);
599 des_cbc_cipher = cipher;
601 return des_cbc_cipher;
605 static EVP_CIPHER *des3_cbc_cipher = NULL;
606 static const EVP_CIPHER *cryptodev_3des_cbc(void)
608 if (des3_cbc_cipher == NULL) {
609 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24);
612 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
613 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
614 | EVP_CIPH_CUSTOM_COPY)
615 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
616 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
617 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
618 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
619 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
622 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
623 EVP_CIPHER_set_asn1_iv)
624 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
625 EVP_CIPHER_get_asn1_iv)) {
626 EVP_CIPHER_meth_free(cipher);
629 des3_cbc_cipher = cipher;
631 return des3_cbc_cipher;
635 static EVP_CIPHER *des3_ecb_cipher = NULL;
636 static const EVP_CIPHER *cryptodev_3des_ecb(void)
638 if (des3_ecb_cipher == NULL) {
639 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_ede3_ecb, 8, 24);
642 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
643 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
644 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
645 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
646 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
647 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
649 dev_crypto_state))) {
650 EVP_CIPHER_meth_free(cipher);
653 des3_ecb_cipher = cipher;
655 return des3_ecb_cipher;
658 static EVP_CIPHER *bf_cbc_cipher = NULL;
659 static const EVP_CIPHER *cryptodev_bf_cbc(void)
661 if (bf_cbc_cipher == NULL) {
662 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16);
665 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
666 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
667 | EVP_CIPH_CUSTOM_COPY)
668 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
669 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
670 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
671 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
672 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
675 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
676 EVP_CIPHER_set_asn1_iv)
677 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
678 EVP_CIPHER_get_asn1_iv)) {
679 EVP_CIPHER_meth_free(cipher);
682 bf_cbc_cipher = cipher;
684 return bf_cbc_cipher;
687 static EVP_CIPHER *cast_cbc_cipher = NULL;
688 static const EVP_CIPHER *cryptodev_cast_cbc(void)
690 if (cast_cbc_cipher == NULL) {
691 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16);
694 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
695 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
696 | EVP_CIPH_CUSTOM_COPY)
697 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
698 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
699 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
700 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
701 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
704 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
705 EVP_CIPHER_set_asn1_iv)
706 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
707 EVP_CIPHER_get_asn1_iv)) {
708 EVP_CIPHER_meth_free(cipher);
711 cast_cbc_cipher = cipher;
713 return cast_cbc_cipher;
716 static EVP_CIPHER *aes_cbc_cipher = NULL;
717 static const EVP_CIPHER *cryptodev_aes_cbc(void)
719 if (aes_cbc_cipher == NULL) {
720 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16);
723 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
724 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
725 | EVP_CIPH_CUSTOM_COPY)
726 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
727 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
728 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
729 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
730 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
733 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
734 EVP_CIPHER_set_asn1_iv)
735 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
736 EVP_CIPHER_get_asn1_iv)) {
737 EVP_CIPHER_meth_free(cipher);
740 aes_cbc_cipher = cipher;
742 return aes_cbc_cipher;
745 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
746 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
748 if (aes_192_cbc_cipher == NULL) {
749 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24);
752 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
753 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
754 | EVP_CIPH_CUSTOM_COPY)
755 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
756 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
757 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
758 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
759 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
762 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
763 EVP_CIPHER_set_asn1_iv)
764 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
765 EVP_CIPHER_get_asn1_iv)) {
766 EVP_CIPHER_meth_free(cipher);
769 aes_192_cbc_cipher = cipher;
771 return aes_192_cbc_cipher;
774 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
775 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
777 if (aes_256_cbc_cipher == NULL) {
778 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32);
781 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
782 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
783 | EVP_CIPH_CUSTOM_COPY)
784 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
785 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
786 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
787 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
788 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
791 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
792 EVP_CIPHER_set_asn1_iv)
793 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
794 EVP_CIPHER_get_asn1_iv)) {
795 EVP_CIPHER_meth_free(cipher);
798 aes_256_cbc_cipher = cipher;
800 return aes_256_cbc_cipher;
803 static EVP_CIPHER *aes_ctr_cipher = NULL;
804 static const EVP_CIPHER *cryptodev_aes_ctr(void)
806 if (aes_ctr_cipher == NULL) {
807 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16);
810 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
811 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
812 | EVP_CIPH_CUSTOM_COPY)
813 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
814 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
815 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
816 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
817 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
820 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
821 EVP_CIPHER_set_asn1_iv)
822 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
823 EVP_CIPHER_get_asn1_iv)) {
824 EVP_CIPHER_meth_free(cipher);
827 aes_ctr_cipher = cipher;
829 return aes_ctr_cipher;
832 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
833 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
835 if (aes_192_ctr_cipher == NULL) {
836 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24);
839 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
840 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
841 | EVP_CIPH_CUSTOM_COPY)
842 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
843 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
844 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
845 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
846 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
849 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
850 EVP_CIPHER_set_asn1_iv)
851 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
852 EVP_CIPHER_get_asn1_iv)) {
853 EVP_CIPHER_meth_free(cipher);
856 aes_192_ctr_cipher = cipher;
858 return aes_192_ctr_cipher;
861 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
862 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
864 if (aes_256_ctr_cipher == NULL) {
865 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32);
868 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
869 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
870 | EVP_CIPH_CUSTOM_COPY)
871 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
872 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
873 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
874 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
875 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
878 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
879 EVP_CIPHER_set_asn1_iv)
880 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
881 EVP_CIPHER_get_asn1_iv)) {
882 EVP_CIPHER_meth_free(cipher);
885 aes_256_ctr_cipher = cipher;
887 return aes_256_ctr_cipher;
890 static EVP_CIPHER *aes_ecb_cipher = NULL;
891 static const EVP_CIPHER *cryptodev_aes_ecb(void)
893 if (aes_ecb_cipher == NULL) {
894 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_ecb, 16, 16);
897 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
898 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
899 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
900 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
901 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
902 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
905 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
906 EVP_CIPHER_set_asn1_iv)
907 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
908 EVP_CIPHER_get_asn1_iv)) {
909 EVP_CIPHER_meth_free(cipher);
912 aes_ecb_cipher = cipher;
914 return aes_ecb_cipher;
917 static EVP_CIPHER *aes_192_ecb_cipher = NULL;
918 static const EVP_CIPHER *cryptodev_aes_192_ecb(void)
920 if (aes_192_ecb_cipher == NULL) {
921 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_ecb, 16, 24);
924 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
925 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
926 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
927 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
928 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
929 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
932 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
933 EVP_CIPHER_set_asn1_iv)
934 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
935 EVP_CIPHER_get_asn1_iv)) {
936 EVP_CIPHER_meth_free(cipher);
939 aes_192_ecb_cipher = cipher;
941 return aes_192_ecb_cipher;
944 static EVP_CIPHER *aes_256_ecb_cipher = NULL;
945 static const EVP_CIPHER *cryptodev_aes_256_ecb(void)
947 if (aes_256_ecb_cipher == NULL) {
948 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_ecb, 16, 32);
951 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
952 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
953 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
954 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
955 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
956 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
959 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
960 EVP_CIPHER_set_asn1_iv)
961 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
962 EVP_CIPHER_get_asn1_iv)) {
963 EVP_CIPHER_meth_free(cipher);
966 aes_256_ecb_cipher = cipher;
968 return aes_256_ecb_cipher;
972 * Registered by the ENGINE when used to find out how to deal with
973 * a particular NID in the ENGINE. this says what we'll do at the
974 * top level - note, that list is restricted by what we answer with
977 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
978 const int **nids, int nid)
981 return (cryptodev_usable_ciphers(nids));
985 *cipher = cryptodev_rc4();
987 case NID_des_ede3_cbc:
988 *cipher = cryptodev_3des_cbc();
990 case NID_des_ede3_ecb:
991 *cipher = cryptodev_3des_ecb();
994 *cipher = cryptodev_des_cbc();
997 *cipher = cryptodev_bf_cbc();
1000 *cipher = cryptodev_cast_cbc();
1002 case NID_aes_128_cbc:
1003 *cipher = cryptodev_aes_cbc();
1005 case NID_aes_192_cbc:
1006 *cipher = cryptodev_aes_192_cbc();
1008 case NID_aes_256_cbc:
1009 *cipher = cryptodev_aes_256_cbc();
1011 case NID_aes_128_ctr:
1012 *cipher = cryptodev_aes_ctr();
1014 case NID_aes_192_ctr:
1015 *cipher = cryptodev_aes_192_ctr();
1017 case NID_aes_256_ctr:
1018 *cipher = cryptodev_aes_256_ctr();
1020 case NID_aes_128_ecb:
1021 *cipher = cryptodev_aes_ecb();
1023 case NID_aes_192_ecb:
1024 *cipher = cryptodev_aes_192_ecb();
1026 case NID_aes_256_ecb:
1027 *cipher = cryptodev_aes_256_ecb();
1033 return (*cipher != NULL);
1036 # ifdef USE_CRYPTODEV_DIGESTS
1038 /* convert digest type to cryptodev */
1039 static int digest_nid_to_cryptodev(int nid)
1043 for (i = 0; digests[i].id; i++)
1044 if (digests[i].nid == nid)
1045 return (digests[i].id);
1049 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
1051 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1052 struct session_op *sess = &state->d_sess;
1055 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
1056 printf("cryptodev_digest_init: Can't get digest \n");
1060 memset(state, 0, sizeof(*state));
1062 if ((state->d_fd = get_dev_crypto()) < 0) {
1063 printf("cryptodev_digest_init: Can't get Dev \n");
1067 sess->mackey = NULL;
1068 sess->mackeylen = 0;
1071 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
1072 put_dev_crypto(state->d_fd);
1074 printf("cryptodev_digest_init: Open session failed\n");
1081 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
1084 struct crypt_op cryp;
1085 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1086 struct session_op *sess = &state->d_sess;
1089 if (!data || state->d_fd < 0) {
1090 printf("cryptodev_digest_update: illegal inputs \n");
1098 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
1099 /* if application doesn't support one buffer */
1100 new_mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
1102 if (!new_mac_data) {
1103 printf("cryptodev_digest_update: realloc failed\n");
1106 state->mac_data = new_mac_data;
1108 memcpy(state->mac_data + state->mac_len, data, count);
1109 state->mac_len += count;
1114 memset(&cryp, 0, sizeof(cryp));
1116 cryp.ses = sess->ses;
1119 cryp.src = (void *)data;
1121 cryp.mac = (void *)state->digest_res;
1122 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1123 printf("cryptodev_digest_update: digest failed\n");
1129 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
1131 struct crypt_op cryp;
1132 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1133 struct session_op *sess = &state->d_sess;
1135 if (!md || state->d_fd < 0) {
1136 printf("cryptodev_digest_final: illegal input\n");
1140 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
1141 /* if application doesn't support one buffer */
1142 memset(&cryp, 0, sizeof(cryp));
1143 cryp.ses = sess->ses;
1145 cryp.len = state->mac_len;
1146 cryp.src = state->mac_data;
1148 cryp.mac = (void *)md;
1149 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1150 printf("cryptodev_digest_final: digest failed\n");
1157 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
1162 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
1165 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1166 struct session_op *sess = &state->d_sess;
1171 if (state->d_fd < 0) {
1172 printf("cryptodev_digest_cleanup: illegal input\n");
1176 OPENSSL_free(state->mac_data);
1177 state->mac_data = NULL;
1180 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1181 printf("cryptodev_digest_cleanup: failed to close session\n");
1186 put_dev_crypto(state->d_fd);
1192 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1194 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1195 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1196 struct session_op *sess;
1199 if (dstate == NULL || fstate == NULL)
1202 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1204 sess = &dstate->d_sess;
1206 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1208 sess->mackey = NULL;
1209 sess->mackeylen = 0;
1212 dstate->d_fd = get_dev_crypto();
1214 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1215 put_dev_crypto(dstate->d_fd);
1217 printf("cryptodev_digest_copy: Open session failed\n");
1221 if (fstate->mac_len != 0) {
1222 if (fstate->mac_data != NULL) {
1223 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1224 if (dstate->mac_data == NULL) {
1225 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1228 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1229 dstate->mac_len = fstate->mac_len;
1236 static EVP_MD *sha1_md = NULL;
1237 static const EVP_MD *cryptodev_sha1(void)
1239 if (sha1_md == NULL) {
1240 EVP_MD *md = EVP_MD_meth_new(NID_sha1, NID_undef);
1243 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1244 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1245 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1246 || !EVP_MD_meth_set_app_datasize(md,
1247 sizeof(struct dev_crypto_state))
1248 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1249 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1250 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1251 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1252 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1253 EVP_MD_meth_free(md);
1261 static EVP_MD *sha256_md = NULL;
1262 static const EVP_MD *cryptodev_sha256(void)
1264 if (sha256_md == NULL) {
1265 EVP_MD *md = EVP_MD_meth_new(NID_sha256, NID_undef);
1268 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
1269 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1270 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
1271 || !EVP_MD_meth_set_app_datasize(md,
1272 sizeof(struct dev_crypto_state))
1273 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1274 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1275 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1276 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1277 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1278 EVP_MD_meth_free(md);
1286 static EVP_MD *sha224_md = NULL;
1287 static const EVP_MD *cryptodev_sha224(void)
1289 if (sha224_md == NULL) {
1290 EVP_MD *md = EVP_MD_meth_new(NID_sha224, NID_undef);
1293 || !EVP_MD_meth_set_result_size(md, SHA224_DIGEST_LENGTH)
1294 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1295 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
1296 || !EVP_MD_meth_set_app_datasize(md,
1297 sizeof(struct dev_crypto_state))
1298 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1299 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1300 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1301 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1302 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1303 EVP_MD_meth_free(md);
1311 static EVP_MD *sha384_md = NULL;
1312 static const EVP_MD *cryptodev_sha384(void)
1314 if (sha384_md == NULL) {
1315 EVP_MD *md = EVP_MD_meth_new(NID_sha384, NID_undef);
1318 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
1319 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1320 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
1321 || !EVP_MD_meth_set_app_datasize(md,
1322 sizeof(struct dev_crypto_state))
1323 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1324 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1325 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1326 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1327 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1328 EVP_MD_meth_free(md);
1336 static EVP_MD *sha512_md = NULL;
1337 static const EVP_MD *cryptodev_sha512(void)
1339 if (sha512_md == NULL) {
1340 EVP_MD *md = EVP_MD_meth_new(NID_sha512, NID_undef);
1343 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
1344 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1345 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
1346 || !EVP_MD_meth_set_app_datasize(md,
1347 sizeof(struct dev_crypto_state))
1348 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1349 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1350 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1351 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1352 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1353 EVP_MD_meth_free(md);
1361 static EVP_MD *md5_md = NULL;
1362 static const EVP_MD *cryptodev_md5(void)
1364 if (md5_md == NULL) {
1365 EVP_MD *md = EVP_MD_meth_new(NID_md5, NID_undef);
1368 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */ )
1369 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1370 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */ )
1371 || !EVP_MD_meth_set_app_datasize(md,
1372 sizeof(struct dev_crypto_state))
1373 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1374 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1375 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1376 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1377 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1378 EVP_MD_meth_free(md);
1386 # endif /* USE_CRYPTODEV_DIGESTS */
1389 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1390 const int **nids, int nid)
1393 return (cryptodev_usable_digests(nids));
1396 # ifdef USE_CRYPTODEV_DIGESTS
1398 *digest = cryptodev_md5();
1401 *digest = cryptodev_sha1();
1404 *digest = cryptodev_sha256();
1407 *digest = cryptodev_sha224();
1410 *digest = cryptodev_sha384();
1413 *digest = cryptodev_sha512();
1416 # endif /* USE_CRYPTODEV_DIGESTS */
1420 return (*digest != NULL);
1423 static int cryptodev_engine_destroy(ENGINE *e)
1425 EVP_CIPHER_meth_free(rc4_cipher);
1427 EVP_CIPHER_meth_free(des_cbc_cipher);
1428 des_cbc_cipher = NULL;
1429 EVP_CIPHER_meth_free(des3_cbc_cipher);
1430 des3_cbc_cipher = NULL;
1431 EVP_CIPHER_meth_free(des3_ecb_cipher);
1432 des3_ecb_cipher = NULL;
1433 EVP_CIPHER_meth_free(bf_cbc_cipher);
1434 bf_cbc_cipher = NULL;
1435 EVP_CIPHER_meth_free(cast_cbc_cipher);
1436 cast_cbc_cipher = NULL;
1437 EVP_CIPHER_meth_free(aes_cbc_cipher);
1438 aes_cbc_cipher = NULL;
1439 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1440 aes_192_cbc_cipher = NULL;
1441 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1442 aes_256_cbc_cipher = NULL;
1443 EVP_CIPHER_meth_free(aes_ctr_cipher);
1444 aes_ctr_cipher = NULL;
1445 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1446 aes_192_ctr_cipher = NULL;
1447 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1448 aes_256_ctr_cipher = NULL;
1449 EVP_CIPHER_meth_free(aes_ecb_cipher);
1450 aes_ecb_cipher = NULL;
1451 EVP_CIPHER_meth_free(aes_192_ecb_cipher);
1452 aes_192_ecb_cipher = NULL;
1453 EVP_CIPHER_meth_free(aes_256_ecb_cipher);
1454 aes_256_ecb_cipher = NULL;
1455 # ifdef USE_CRYPTODEV_DIGESTS
1456 EVP_MD_meth_free(sha1_md);
1458 EVP_MD_meth_free(sha256_md);
1460 EVP_MD_meth_free(sha224_md);
1462 EVP_MD_meth_free(sha384_md);
1464 EVP_MD_meth_free(sha512_md);
1466 EVP_MD_meth_free(md5_md);
1469 RSA_meth_free(cryptodev_rsa);
1470 cryptodev_rsa = NULL;
1471 # ifndef OPENSSL_NO_DSA
1472 DSA_meth_free(cryptodev_dsa);
1473 cryptodev_dsa = NULL;
1475 # ifndef OPENSSL_NO_DH
1476 DH_meth_free(cryptodev_dh);
1477 cryptodev_dh = NULL;
1483 * Convert a BIGNUM to the representation that /dev/crypto needs.
1484 * Upon completion of use, the caller is responsible for freeing
1487 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1489 ssize_t bytes, bits;
1495 bits = BN_num_bits(a);
1496 bytes = BN_num_bytes(a);
1498 b = OPENSSL_zalloc(bytes);
1502 crp->crp_p = (void *)b;
1503 crp->crp_nbits = bits;
1509 /* Convert a /dev/crypto parameter to a BIGNUM */
1510 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1515 bytes = (crp->crp_nbits + 7) / 8;
1520 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1523 for (i = 0; i < bytes; i++)
1524 pd[i] = crp->crp_p[bytes - i - 1];
1526 BN_bin2bn(pd, bytes, a);
1532 static void zapparams(struct crypt_kop *kop)
1536 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1537 OPENSSL_free(kop->crk_param[i].crp_p);
1538 kop->crk_param[i].crp_p = NULL;
1539 kop->crk_param[i].crp_nbits = 0;
1544 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1548 if ((fd = get_asym_dev_crypto()) < 0)
1552 kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_zalloc(rlen);
1553 if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
1555 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1559 kop->crk_param[kop->crk_iparams + 1].crp_p = OPENSSL_zalloc(slen);
1560 /* No need to free the kop->crk_iparams parameter if it was allocated,
1561 * callers of this routine have to free allocated parameters through
1562 * zapparams both in case of success and failure
1564 if (kop->crk_param[kop->crk_iparams + 1].crp_p == NULL)
1566 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1570 if (ioctl(fd, CIOCKEY, kop) == 0) {
1572 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1574 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1582 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1583 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1585 struct crypt_kop kop;
1589 * Currently, we know we can do mod exp iff we can do any asymmetric
1590 * operations at all.
1592 if (cryptodev_asymfeat == 0) {
1593 ret = BN_mod_exp(r, a, p, m, ctx);
1597 memset(&kop, 0, sizeof(kop));
1598 kop.crk_op = CRK_MOD_EXP;
1600 /* inputs: a^p % m */
1601 if (bn2crparam(a, &kop.crk_param[0]))
1603 if (bn2crparam(p, &kop.crk_param[1]))
1605 if (bn2crparam(m, &kop.crk_param[2]))
1607 kop.crk_iparams = 3;
1609 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1610 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1611 printf("OCF asym process failed, Running in software\n");
1612 ret = RSA_meth_get_bn_mod_exp(meth) (r, a, p, m, ctx, in_mont);
1614 } else if (ECANCELED == kop.crk_status) {
1615 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1616 printf("OCF hardware operation cancelled. Running in Software\n");
1617 ret = RSA_meth_get_bn_mod_exp(meth) (r, a, p, m, ctx, in_mont);
1619 /* else cryptodev operation worked ok ==> ret = 1 */
1627 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1630 const BIGNUM *n = NULL;
1631 const BIGNUM *d = NULL;
1634 RSA_get0_key(rsa, &n, NULL, &d);
1635 r = cryptodev_bn_mod_exp(r0, I, d, n, ctx, NULL);
1641 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1643 struct crypt_kop kop;
1645 const BIGNUM *p = NULL;
1646 const BIGNUM *q = NULL;
1647 const BIGNUM *dmp1 = NULL;
1648 const BIGNUM *dmq1 = NULL;
1649 const BIGNUM *iqmp = NULL;
1650 const BIGNUM *n = NULL;
1652 RSA_get0_factors(rsa, &p, &q);
1653 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
1654 RSA_get0_key(rsa, &n, NULL, NULL);
1656 if (!p || !q || !dmp1 || !dmq1 || !iqmp) {
1657 /* XXX 0 means failure?? */
1661 memset(&kop, 0, sizeof(kop));
1662 kop.crk_op = CRK_MOD_EXP_CRT;
1663 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1664 if (bn2crparam(p, &kop.crk_param[0]))
1666 if (bn2crparam(q, &kop.crk_param[1]))
1668 if (bn2crparam(I, &kop.crk_param[2]))
1670 if (bn2crparam(dmp1, &kop.crk_param[3]))
1672 if (bn2crparam(dmq1, &kop.crk_param[4]))
1674 if (bn2crparam(iqmp, &kop.crk_param[5]))
1676 kop.crk_iparams = 6;
1678 if (cryptodev_asym(&kop, BN_num_bytes(n), r0, 0, NULL)) {
1679 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1680 printf("OCF asym process failed, running in Software\n");
1681 ret = RSA_meth_get_mod_exp(meth) (r0, I, rsa, ctx);
1683 } else if (ECANCELED == kop.crk_status) {
1684 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1685 printf("OCF hardware operation cancelled. Running in Software\n");
1686 ret = RSA_meth_get_mod_exp(meth) (r0, I, rsa, ctx);
1688 /* else cryptodev operation worked ok ==> ret = 1 */
1695 # ifndef OPENSSL_NO_DSA
1697 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1698 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1700 return cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx);
1704 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
1705 const BIGNUM *u1, const BIGNUM *pub_key,
1706 const BIGNUM *u2, const BIGNUM *p, BN_CTX *ctx,
1709 const BIGNUM *dsag, *dsap, *dsapub_key;
1712 const DSA_METHOD *meth;
1713 int (*bn_mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *,
1714 const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
1720 /* v = ( g^u1 * y^u2 mod p ) mod q */
1721 /* let t1 = g ^ u1 mod p */
1724 DSA_get0_pqg(dsa, &dsap, NULL, &dsag);
1725 DSA_get0_key(dsa, &dsapub_key, NULL);
1727 meth = DSA_get_method(dsa);
1730 bn_mod_exp = DSA_meth_get_bn_mod_exp(meth);
1731 if (bn_mod_exp == NULL)
1734 if (!bn_mod_exp(dsa, t1, dsag, u1, dsap, ctx, mont))
1737 /* let t2 = y ^ u2 mod p */
1738 if (!bn_mod_exp(dsa, t2, dsapub_key, u2, dsap, ctx, mont))
1740 /* let t1 = t1 * t2 mod p */
1741 if (!BN_mod_mul(t1, t1, t2, dsap, ctx))
1750 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1753 struct crypt_kop kop;
1755 const BIGNUM *dsap = NULL, *dsaq = NULL, *dsag = NULL;
1756 const BIGNUM *priv_key = NULL;
1757 DSA_SIG *dsasig, *dsaret = NULL;
1759 dsasig = DSA_SIG_new();
1763 memset(&kop, 0, sizeof(kop));
1764 kop.crk_op = CRK_DSA_SIGN;
1766 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1767 kop.crk_param[0].crp_p = (void *)dgst;
1768 kop.crk_param[0].crp_nbits = dlen * 8;
1769 DSA_get0_pqg(dsa, &dsap, &dsaq, &dsag);
1770 DSA_get0_key(dsa, NULL, &priv_key);
1771 if (bn2crparam(dsap, &kop.crk_param[1]))
1773 if (bn2crparam(dsaq, &kop.crk_param[2]))
1775 if (bn2crparam(dsag, &kop.crk_param[3]))
1777 if (bn2crparam(priv_key, &kop.crk_param[4]))
1779 kop.crk_iparams = 5;
1787 if (cryptodev_asym(&kop, BN_num_bytes(dsaq), r, BN_num_bytes(dsaq), s) == 0) {
1788 DSA_SIG_set0(dsasig, r, s);
1791 dsaret = DSA_meth_get_sign(DSA_OpenSSL())(dgst, dlen, dsa);
1794 if (dsaret != dsasig)
1795 DSA_SIG_free(dsasig);
1796 kop.crk_param[0].crp_p = NULL;
1802 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1803 DSA_SIG *sig, DSA *dsa)
1805 struct crypt_kop kop;
1807 const BIGNUM *pr, *ps, *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL;
1809 memset(&kop, 0, sizeof(kop));
1810 kop.crk_op = CRK_DSA_VERIFY;
1812 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1813 kop.crk_param[0].crp_p = (void *)dgst;
1814 kop.crk_param[0].crp_nbits = dlen * 8;
1815 DSA_get0_pqg(dsa, &p, &q, &g);
1816 if (bn2crparam(p, &kop.crk_param[1]))
1818 if (bn2crparam(q, &kop.crk_param[2]))
1820 if (bn2crparam(g, &kop.crk_param[3]))
1822 DSA_get0_key(dsa, &pub_key, NULL);
1823 if (bn2crparam(pub_key, &kop.crk_param[4]))
1825 DSA_SIG_get0(sig, &pr, &ps);
1826 if (bn2crparam(pr, &kop.crk_param[5]))
1828 if (bn2crparam(ps, &kop.crk_param[6]))
1830 kop.crk_iparams = 7;
1832 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1834 * OCF success value is 0, if not zero, change dsaret to fail
1836 if (0 != kop.crk_status)
1839 dsaret = DSA_meth_get_verify(DSA_OpenSSL())(dgst, dlen, sig, dsa);
1842 kop.crk_param[0].crp_p = NULL;
1848 # ifndef OPENSSL_NO_DH
1850 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1851 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1854 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1858 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1860 struct crypt_kop kop;
1863 const BIGNUM *p = NULL;
1864 const BIGNUM *priv_key = NULL;
1866 if ((fd = get_asym_dev_crypto()) < 0) {
1867 const DH_METHOD *meth = DH_OpenSSL();
1869 return DH_meth_get_compute_key(meth) (key, pub_key, dh);
1872 DH_get0_pqg(dh, &p, NULL, NULL);
1873 DH_get0_key(dh, NULL, &priv_key);
1875 keylen = BN_num_bits(p);
1877 memset(&kop, 0, sizeof(kop));
1878 kop.crk_op = CRK_DH_COMPUTE_KEY;
1880 /* inputs: dh->priv_key pub_key dh->p key */
1881 if (bn2crparam(priv_key, &kop.crk_param[0]))
1883 if (bn2crparam(pub_key, &kop.crk_param[1]))
1885 if (bn2crparam(p, &kop.crk_param[2]))
1887 kop.crk_iparams = 3;
1889 kop.crk_param[3].crp_p = (void *)key;
1890 kop.crk_param[3].crp_nbits = keylen;
1891 kop.crk_oparams = 1;
1893 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1894 const DH_METHOD *meth = DH_OpenSSL();
1896 dhret = DH_meth_get_compute_key(meth) (key, pub_key, dh);
1899 kop.crk_param[3].crp_p = NULL;
1904 # endif /* ndef OPENSSL_NO_DH */
1907 * ctrl right now is just a wrapper that doesn't do much
1908 * but I expect we'll want some options soon.
1910 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1912 # ifdef HAVE_SYSLOG_R
1913 struct syslog_data sd = SYSLOG_DATA_INIT;
1918 # ifdef HAVE_SYSLOG_R
1919 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1921 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1928 void engine_load_cryptodev_int(void)
1930 ENGINE *engine = ENGINE_new();
1935 if ((fd = get_dev_crypto()) < 0) {
1936 ENGINE_free(engine);
1941 * find out what asymmetric crypto algorithms we support
1943 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1945 ENGINE_free(engine);
1950 if (!ENGINE_set_id(engine, "cryptodev") ||
1951 !ENGINE_set_name(engine, "cryptodev engine") ||
1952 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1953 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1954 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1955 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1956 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1957 ENGINE_free(engine);
1961 cryptodev_rsa = RSA_meth_dup(RSA_PKCS1_OpenSSL());
1962 if (cryptodev_rsa != NULL) {
1963 RSA_meth_set1_name(cryptodev_rsa, "cryptodev RSA method");
1964 RSA_meth_set_flags(cryptodev_rsa, 0);
1965 if (ENGINE_set_RSA(engine, cryptodev_rsa)) {
1966 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1967 RSA_meth_set_bn_mod_exp(cryptodev_rsa, cryptodev_bn_mod_exp);
1968 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1969 RSA_meth_set_mod_exp(cryptodev_rsa, cryptodev_rsa_mod_exp);
1971 RSA_meth_set_mod_exp(cryptodev_rsa,
1972 cryptodev_rsa_nocrt_mod_exp);
1976 ENGINE_free(engine);
1980 # ifndef OPENSSL_NO_DSA
1981 cryptodev_dsa = DSA_meth_dup(DSA_OpenSSL());
1982 if (cryptodev_dsa != NULL) {
1983 DSA_meth_set1_name(cryptodev_dsa, "cryptodev DSA method");
1984 DSA_meth_set_flags(cryptodev_dsa, 0);
1985 if (ENGINE_set_DSA(engine, cryptodev_dsa)) {
1986 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1987 DSA_meth_set_sign(cryptodev_dsa, cryptodev_dsa_do_sign);
1988 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1989 DSA_meth_set_bn_mod_exp(cryptodev_dsa,
1990 cryptodev_dsa_bn_mod_exp);
1991 DSA_meth_set_mod_exp(cryptodev_dsa, cryptodev_dsa_dsa_mod_exp);
1993 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1994 DSA_meth_set_verify(cryptodev_dsa, cryptodev_dsa_verify);
1997 ENGINE_free(engine);
2002 # ifndef OPENSSL_NO_DH
2003 cryptodev_dh = DH_meth_dup(DH_OpenSSL());
2004 if (cryptodev_dh != NULL) {
2005 DH_meth_set1_name(cryptodev_dh, "cryptodev DH method");
2006 DH_meth_set_flags(cryptodev_dh, 0);
2007 if (ENGINE_set_DH(engine, cryptodev_dh)) {
2008 if (cryptodev_asymfeat & CRF_MOD_EXP) {
2009 DH_meth_set_bn_mod_exp(cryptodev_dh, cryptodev_mod_exp_dh);
2010 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
2011 DH_meth_set_compute_key(cryptodev_dh,
2012 cryptodev_dh_compute_key);
2016 ENGINE_free(engine);
2022 ENGINE_free(engine);
2026 #endif /* HAVE_CRYPTODEV */