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 struct dev_crypto_state {
85 struct session_op d_sess;
87 # ifdef USE_CRYPTODEV_DIGESTS
88 unsigned char digest_res[HASH_MAX_LEN];
94 static u_int32_t cryptodev_asymfeat = 0;
96 static RSA_METHOD *cryptodev_rsa;
97 # ifndef OPENSSL_NO_DSA
98 static DSA_METHOD *cryptodev_dsa = NULL;
100 # ifndef OPENSSL_NO_DH
101 static DH_METHOD *cryptodev_dh;
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);
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);
128 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
129 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
131 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
133 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
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,
145 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
146 DSA_SIG *sig, DSA *dsa);
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,
152 static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
155 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
157 void engine_load_cryptodev_int(void);
159 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
169 {CRYPTO_ARC4, NID_rc4, 0, 16},
170 {CRYPTO_DES_CBC, NID_des_cbc, 8, 8},
171 {CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24},
172 {CRYPTO_3DES_ECB, NID_des_ede3_ecb, 0, 24},
173 {CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16},
174 {CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24},
175 {CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32},
176 {CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16},
177 {CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24},
178 {CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32},
179 {CRYPTO_AES_ECB, NID_aes_128_ecb, 0, 16},
180 {CRYPTO_AES_ECB, NID_aes_192_ecb, 0, 24},
181 {CRYPTO_AES_ECB, NID_aes_256_ecb, 0, 32},
182 {CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16},
183 {CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16},
184 {CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0},
185 {0, NID_undef, 0, 0},
188 # ifdef USE_CRYPTODEV_DIGESTS
195 /* HMAC is not supported */
196 {CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
197 {CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
199 {CRYPTO_MD5, NID_md5, 16},
200 {CRYPTO_SHA1, NID_sha1, 20},
201 {CRYPTO_SHA2_256, NID_sha256, 32},
202 {CRYPTO_SHA2_384, NID_sha384, 48},
203 {CRYPTO_SHA2_512, NID_sha512, 64},
209 * Return a fd if /dev/crypto seems usable, 0 otherwise.
211 static int open_dev_crypto(void)
216 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
219 if (fcntl(fd, F_SETFD, 1) == -1) {
228 static int get_dev_crypto(void)
232 if ((fd = open_dev_crypto()) == -1)
234 # ifndef CRIOGET_NOT_NEEDED
235 if (ioctl(fd, CRIOGET, &retfd) == -1)
239 if (fcntl(retfd, F_SETFD, 1) == -1) {
249 static void put_dev_crypto(int fd)
251 # ifndef CRIOGET_NOT_NEEDED
256 /* Caching version for asym operations */
257 static int get_asym_dev_crypto(void)
262 fd = get_dev_crypto();
267 * Find out what ciphers /dev/crypto will let us have a session for.
268 * XXX note, that some of these openssl doesn't deal with yet!
269 * returning them here is harmless, as long as we return NULL
270 * when asked for a handler in the cryptodev_engine_ciphers routine
272 static int get_cryptodev_ciphers(const int **cnids)
274 static int nids[CRYPTO_ALGORITHM_MAX];
275 struct session_op sess;
276 int fd, i, count = 0;
277 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
279 if ((fd = get_dev_crypto()) < 0) {
283 memset(&sess, 0, sizeof(sess));
284 sess.key = (void *)fake_key;
286 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
287 if (ciphers[i].nid == NID_undef)
289 sess.cipher = ciphers[i].id;
290 sess.keylen = ciphers[i].keylen;
292 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
293 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
294 nids[count++] = ciphers[i].nid;
305 # ifdef USE_CRYPTODEV_DIGESTS
307 * Find out what digests /dev/crypto will let us have a session for.
308 * XXX note, that some of these openssl doesn't deal with yet!
309 * returning them here is harmless, as long as we return NULL
310 * when asked for a handler in the cryptodev_engine_digests routine
312 static int get_cryptodev_digests(const int **cnids)
314 static int nids[CRYPTO_ALGORITHM_MAX];
315 unsigned char fake_key[CRYPTO_CIPHER_MAX_KEY_LEN];
316 struct session_op sess;
317 int fd, i, count = 0;
319 if ((fd = get_dev_crypto()) < 0) {
323 memset(&sess, 0, sizeof(sess));
324 sess.mackey = fake_key;
325 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
326 if (digests[i].nid == NID_undef)
328 sess.mac = digests[i].id;
331 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
332 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
333 nids[count++] = digests[i].nid;
346 * Find the useable ciphers|digests from dev/crypto - this is the first
347 * thing called by the engine init crud which determines what it
348 * can use for ciphers from this engine. We want to return
349 * only what we can do, anything else is handled by software.
351 * If we can't initialize the device to do anything useful for
352 * any reason, we want to return a NULL array, and 0 length,
353 * which forces everything to be done is software. By putting
354 * the initialization of the device in here, we ensure we can
355 * use this engine as the default, and if for whatever reason
356 * /dev/crypto won't do what we want it will just be done in
359 * This can (should) be greatly expanded to perhaps take into
360 * account speed of the device, and what we want to do.
361 * (although the disabling of particular alg's could be controlled
362 * by the device driver with sysctl's.) - this is where we
363 * want most of the decisions made about what we actually want
364 * to use from /dev/crypto.
366 static int cryptodev_usable_ciphers(const int **nids)
368 return (get_cryptodev_ciphers(nids));
371 static int cryptodev_usable_digests(const int **nids)
373 # ifdef USE_CRYPTODEV_DIGESTS
374 return (get_cryptodev_digests(nids));
377 * XXXX just disable all digests for now, because it sucks.
378 * we need a better way to decide this - i.e. I may not
379 * want digests on slow cards like hifn on fast machines,
380 * but might want them on slow or loaded machines, etc.
381 * will also want them when using crypto cards that don't
382 * suck moose gonads - would be nice to be able to decide something
383 * as reasonable default without having hackery that's card dependent.
384 * of course, the default should probably be just do everything,
385 * with perhaps a sysctl to turn algorithms off (or have them off
386 * by default) on cards that generally suck like the hifn.
394 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
395 const unsigned char *in, size_t inl)
397 struct crypt_op cryp;
398 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
399 struct session_op *sess = &state->d_sess;
401 unsigned char save_iv[EVP_MAX_IV_LENGTH];
407 if ((inl % EVP_CIPHER_CTX_block_size(ctx)) != 0)
410 memset(&cryp, 0, sizeof(cryp));
412 cryp.ses = sess->ses;
415 cryp.src = (void *)in;
416 cryp.dst = (void *)out;
419 cryp.op = EVP_CIPHER_CTX_encrypting(ctx) ? COP_ENCRYPT : COP_DECRYPT;
421 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
422 cryp.iv = (void *)EVP_CIPHER_CTX_iv(ctx);
423 if (!EVP_CIPHER_CTX_encrypting(ctx)) {
424 iiv = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
425 memcpy(save_iv, iiv, EVP_CIPHER_CTX_iv_length(ctx));
430 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
432 * XXX need better error handling this can fail for a number of
438 if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
439 if (EVP_CIPHER_CTX_encrypting(ctx))
440 iiv = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
443 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iiv,
444 EVP_CIPHER_CTX_iv_length(ctx));
450 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
451 const unsigned char *iv, int enc)
453 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
454 struct session_op *sess = &state->d_sess;
457 for (i = 0; ciphers[i].id; i++)
458 if (EVP_CIPHER_CTX_nid(ctx) == ciphers[i].nid &&
459 EVP_CIPHER_CTX_iv_length(ctx) <= ciphers[i].ivmax &&
460 EVP_CIPHER_CTX_key_length(ctx) == ciphers[i].keylen) {
461 cipher = ciphers[i].id;
465 if (!ciphers[i].id) {
470 memset(sess, 0, sizeof(*sess));
472 if ((state->d_fd = get_dev_crypto()) < 0)
475 sess->key = (void *)key;
476 sess->keylen = EVP_CIPHER_CTX_key_length(ctx);
477 sess->cipher = cipher;
479 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
480 put_dev_crypto(state->d_fd);
488 * free anything we allocated earlier when initing a
489 * session, and close the session.
491 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
494 struct dev_crypto_state *state = EVP_CIPHER_CTX_get_cipher_data(ctx);
495 struct session_op *sess = &state->d_sess;
501 * XXX if this ioctl fails, something's wrong. the invoker may have called
502 * us with a bogus ctx, or we could have a device that for whatever
503 * reason just doesn't want to play ball - it's not clear what's right
504 * here - should this be an error? should it just increase a counter,
505 * hmm. For right now, we return 0 - I don't believe that to be "right".
506 * we could call the gorpy openssl lib error handlers that print messages
507 * to users of the library. hmm..
510 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
515 put_dev_crypto(state->d_fd);
522 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
523 * gets called when libcrypto requests a cipher NID.
526 static int cryptodev_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void *p2)
528 struct dev_crypto_state *state = ctx->cipher_data;
529 struct session_op *sess = &state->d_sess;
531 if (type == EVP_CTRL_COPY) {
532 EVP_CIPHER_CTX *out = p2;
533 return cryptodev_init_key(out, sess->key, ctx->iv, 0);
540 static EVP_CIPHER *rc4_cipher = NULL;
541 static const EVP_CIPHER *cryptodev_rc4(void)
543 if (rc4_cipher == NULL) {
544 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 16);
547 || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
548 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH
549 | EVP_CIPH_CUSTOM_COPY)
550 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
551 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
552 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
553 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
554 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
556 dev_crypto_state))) {
557 EVP_CIPHER_meth_free(cipher);
566 static EVP_CIPHER *des_cbc_cipher = NULL;
567 static const EVP_CIPHER *cryptodev_des_cbc(void)
569 if (des_cbc_cipher == NULL) {
570 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_cbc, 8, 8);
573 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
574 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
575 | EVP_CIPH_CUSTOM_COPY)
576 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
577 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
578 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
579 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
580 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
583 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
584 EVP_CIPHER_set_asn1_iv)
585 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
586 EVP_CIPHER_get_asn1_iv)) {
587 EVP_CIPHER_meth_free(cipher);
590 des_cbc_cipher = cipher;
592 return des_cbc_cipher;
596 static EVP_CIPHER *des3_cbc_cipher = NULL;
597 static const EVP_CIPHER *cryptodev_3des_cbc(void)
599 if (des3_cbc_cipher == NULL) {
600 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_ede3_cbc, 8, 24);
603 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
604 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
605 | EVP_CIPH_CUSTOM_COPY)
606 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
607 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
608 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
609 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
610 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
613 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
614 EVP_CIPHER_set_asn1_iv)
615 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
616 EVP_CIPHER_get_asn1_iv)) {
617 EVP_CIPHER_meth_free(cipher);
620 des3_cbc_cipher = cipher;
622 return des3_cbc_cipher;
626 static EVP_CIPHER *des3_ecb_cipher = NULL;
627 static const EVP_CIPHER *cryptodev_3des_ecb(void)
629 if (des3_ecb_cipher == NULL) {
630 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_des_ede3_ecb, 8, 24);
633 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
634 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
635 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
636 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
637 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
638 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
640 dev_crypto_state))) {
641 EVP_CIPHER_meth_free(cipher);
644 des3_ecb_cipher = cipher;
646 return des3_ecb_cipher;
649 static EVP_CIPHER *bf_cbc_cipher = NULL;
650 static const EVP_CIPHER *cryptodev_bf_cbc(void)
652 if (bf_cbc_cipher == NULL) {
653 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_bf_cbc, 8, 16);
656 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
657 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
658 | EVP_CIPH_CUSTOM_COPY)
659 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
660 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
661 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
662 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
663 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
666 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
667 EVP_CIPHER_set_asn1_iv)
668 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
669 EVP_CIPHER_get_asn1_iv)) {
670 EVP_CIPHER_meth_free(cipher);
673 bf_cbc_cipher = cipher;
675 return bf_cbc_cipher;
678 static EVP_CIPHER *cast_cbc_cipher = NULL;
679 static const EVP_CIPHER *cryptodev_cast_cbc(void)
681 if (cast_cbc_cipher == NULL) {
682 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_cast5_cbc, 8, 16);
685 || !EVP_CIPHER_meth_set_iv_length(cipher, 8)
686 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
687 | EVP_CIPH_CUSTOM_COPY)
688 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
689 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
690 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
691 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
692 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
695 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
696 EVP_CIPHER_set_asn1_iv)
697 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
698 EVP_CIPHER_get_asn1_iv)) {
699 EVP_CIPHER_meth_free(cipher);
702 cast_cbc_cipher = cipher;
704 return cast_cbc_cipher;
707 static EVP_CIPHER *aes_cbc_cipher = NULL;
708 static const EVP_CIPHER *cryptodev_aes_cbc(void)
710 if (aes_cbc_cipher == NULL) {
711 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_cbc, 16, 16);
714 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
715 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
716 | EVP_CIPH_CUSTOM_COPY)
717 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
718 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
719 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
720 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
721 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
724 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
725 EVP_CIPHER_set_asn1_iv)
726 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
727 EVP_CIPHER_get_asn1_iv)) {
728 EVP_CIPHER_meth_free(cipher);
731 aes_cbc_cipher = cipher;
733 return aes_cbc_cipher;
736 static EVP_CIPHER *aes_192_cbc_cipher = NULL;
737 static const EVP_CIPHER *cryptodev_aes_192_cbc(void)
739 if (aes_192_cbc_cipher == NULL) {
740 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_cbc, 16, 24);
743 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
744 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
745 | EVP_CIPH_CUSTOM_COPY)
746 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
747 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
748 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
749 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
750 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
753 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
754 EVP_CIPHER_set_asn1_iv)
755 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
756 EVP_CIPHER_get_asn1_iv)) {
757 EVP_CIPHER_meth_free(cipher);
760 aes_192_cbc_cipher = cipher;
762 return aes_192_cbc_cipher;
765 static EVP_CIPHER *aes_256_cbc_cipher = NULL;
766 static const EVP_CIPHER *cryptodev_aes_256_cbc(void)
768 if (aes_256_cbc_cipher == NULL) {
769 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_cbc, 16, 32);
772 || !EVP_CIPHER_meth_set_iv_length(cipher, 16)
773 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE
774 | EVP_CIPH_CUSTOM_COPY)
775 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
776 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
777 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
778 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
779 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
782 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
783 EVP_CIPHER_set_asn1_iv)
784 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
785 EVP_CIPHER_get_asn1_iv)) {
786 EVP_CIPHER_meth_free(cipher);
789 aes_256_cbc_cipher = cipher;
791 return aes_256_cbc_cipher;
794 static EVP_CIPHER *aes_ctr_cipher = NULL;
795 static const EVP_CIPHER *cryptodev_aes_ctr(void)
797 if (aes_ctr_cipher == NULL) {
798 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_ctr, 16, 16);
801 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
802 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
803 | EVP_CIPH_CUSTOM_COPY)
804 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
805 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
806 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
807 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
808 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
811 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
812 EVP_CIPHER_set_asn1_iv)
813 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
814 EVP_CIPHER_get_asn1_iv)) {
815 EVP_CIPHER_meth_free(cipher);
818 aes_ctr_cipher = cipher;
820 return aes_ctr_cipher;
823 static EVP_CIPHER *aes_192_ctr_cipher = NULL;
824 static const EVP_CIPHER *cryptodev_aes_192_ctr(void)
826 if (aes_192_ctr_cipher == NULL) {
827 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_ctr, 16, 24);
830 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
831 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
832 | EVP_CIPH_CUSTOM_COPY)
833 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
834 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
835 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
836 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
837 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
840 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
841 EVP_CIPHER_set_asn1_iv)
842 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
843 EVP_CIPHER_get_asn1_iv)) {
844 EVP_CIPHER_meth_free(cipher);
847 aes_192_ctr_cipher = cipher;
849 return aes_192_ctr_cipher;
852 static EVP_CIPHER *aes_256_ctr_cipher = NULL;
853 static const EVP_CIPHER *cryptodev_aes_256_ctr(void)
855 if (aes_256_ctr_cipher == NULL) {
856 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_ctr, 16, 32);
859 || !EVP_CIPHER_meth_set_iv_length(cipher, 14)
860 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CTR_MODE
861 | EVP_CIPH_CUSTOM_COPY)
862 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
863 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
864 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
865 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
866 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
869 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
870 EVP_CIPHER_set_asn1_iv)
871 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
872 EVP_CIPHER_get_asn1_iv)) {
873 EVP_CIPHER_meth_free(cipher);
876 aes_256_ctr_cipher = cipher;
878 return aes_256_ctr_cipher;
881 static EVP_CIPHER *aes_ecb_cipher = NULL;
882 static const EVP_CIPHER *cryptodev_aes_ecb(void)
884 if (aes_ecb_cipher == NULL) {
885 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_128_ecb, 16, 16);
888 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
889 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
890 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
891 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
892 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
893 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
896 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
897 EVP_CIPHER_set_asn1_iv)
898 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
899 EVP_CIPHER_get_asn1_iv)) {
900 EVP_CIPHER_meth_free(cipher);
903 aes_ecb_cipher = cipher;
905 return aes_ecb_cipher;
908 static EVP_CIPHER *aes_192_ecb_cipher = NULL;
909 static const EVP_CIPHER *cryptodev_aes_192_ecb(void)
911 if (aes_192_ecb_cipher == NULL) {
912 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_192_ecb, 16, 24);
915 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
916 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
917 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
918 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
919 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
920 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
923 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
924 EVP_CIPHER_set_asn1_iv)
925 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
926 EVP_CIPHER_get_asn1_iv)) {
927 EVP_CIPHER_meth_free(cipher);
930 aes_192_ecb_cipher = cipher;
932 return aes_192_ecb_cipher;
935 static EVP_CIPHER *aes_256_ecb_cipher = NULL;
936 static const EVP_CIPHER *cryptodev_aes_256_ecb(void)
938 if (aes_256_ecb_cipher == NULL) {
939 EVP_CIPHER *cipher = EVP_CIPHER_meth_new(NID_aes_256_ecb, 16, 32);
942 || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_ECB_MODE)
943 || !EVP_CIPHER_meth_set_init(cipher, cryptodev_init_key)
944 || !EVP_CIPHER_meth_set_do_cipher(cipher, cryptodev_cipher)
945 || !EVP_CIPHER_meth_set_cleanup(cipher, cryptodev_cleanup)
946 || !EVP_CIPHER_meth_set_ctrl(cipher, cryptodev_cipher_ctrl)
947 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher,
950 || !EVP_CIPHER_meth_set_set_asn1_params(cipher,
951 EVP_CIPHER_set_asn1_iv)
952 || !EVP_CIPHER_meth_set_get_asn1_params(cipher,
953 EVP_CIPHER_get_asn1_iv)) {
954 EVP_CIPHER_meth_free(cipher);
957 aes_256_ecb_cipher = cipher;
959 return aes_256_ecb_cipher;
963 * Registered by the ENGINE when used to find out how to deal with
964 * a particular NID in the ENGINE. this says what we'll do at the
965 * top level - note, that list is restricted by what we answer with
968 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
969 const int **nids, int nid)
972 return (cryptodev_usable_ciphers(nids));
976 *cipher = cryptodev_rc4();
978 case NID_des_ede3_cbc:
979 *cipher = cryptodev_3des_cbc();
981 case NID_des_ede3_ecb:
982 *cipher = cryptodev_3des_ecb();
985 *cipher = cryptodev_des_cbc();
988 *cipher = cryptodev_bf_cbc();
991 *cipher = cryptodev_cast_cbc();
993 case NID_aes_128_cbc:
994 *cipher = cryptodev_aes_cbc();
996 case NID_aes_192_cbc:
997 *cipher = cryptodev_aes_192_cbc();
999 case NID_aes_256_cbc:
1000 *cipher = cryptodev_aes_256_cbc();
1002 case NID_aes_128_ctr:
1003 *cipher = cryptodev_aes_ctr();
1005 case NID_aes_192_ctr:
1006 *cipher = cryptodev_aes_192_ctr();
1008 case NID_aes_256_ctr:
1009 *cipher = cryptodev_aes_256_ctr();
1011 case NID_aes_128_ecb:
1012 *cipher = cryptodev_aes_ecb();
1014 case NID_aes_192_ecb:
1015 *cipher = cryptodev_aes_192_ecb();
1017 case NID_aes_256_ecb:
1018 *cipher = cryptodev_aes_256_ecb();
1024 return (*cipher != NULL);
1027 # ifdef USE_CRYPTODEV_DIGESTS
1029 /* convert digest type to cryptodev */
1030 static int digest_nid_to_cryptodev(int nid)
1034 for (i = 0; digests[i].id; i++)
1035 if (digests[i].nid == nid)
1036 return (digests[i].id);
1040 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
1042 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1043 struct session_op *sess = &state->d_sess;
1046 if ((digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(ctx))) == NID_undef) {
1047 printf("cryptodev_digest_init: Can't get digest \n");
1051 memset(state, 0, sizeof(*state));
1053 if ((state->d_fd = get_dev_crypto()) < 0) {
1054 printf("cryptodev_digest_init: Can't get Dev \n");
1058 sess->mackey = NULL;
1059 sess->mackeylen = 0;
1062 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
1063 put_dev_crypto(state->d_fd);
1065 printf("cryptodev_digest_init: Open session failed\n");
1072 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
1075 struct crypt_op cryp;
1076 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1077 struct session_op *sess = &state->d_sess;
1080 if (!data || state->d_fd < 0) {
1081 printf("cryptodev_digest_update: illegal inputs \n");
1089 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
1090 /* if application doesn't support one buffer */
1091 new_mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
1093 if (!new_mac_data) {
1094 printf("cryptodev_digest_update: realloc failed\n");
1097 state->mac_data = new_mac_data;
1099 memcpy(state->mac_data + state->mac_len, data, count);
1100 state->mac_len += count;
1105 memset(&cryp, 0, sizeof(cryp));
1107 cryp.ses = sess->ses;
1110 cryp.src = (void *)data;
1112 cryp.mac = (void *)state->digest_res;
1113 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1114 printf("cryptodev_digest_update: digest failed\n");
1120 static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
1122 struct crypt_op cryp;
1123 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1124 struct session_op *sess = &state->d_sess;
1126 if (!md || state->d_fd < 0) {
1127 printf("cryptodev_digest_final: illegal input\n");
1131 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
1132 /* if application doesn't support one buffer */
1133 memset(&cryp, 0, sizeof(cryp));
1134 cryp.ses = sess->ses;
1136 cryp.len = state->mac_len;
1137 cryp.src = state->mac_data;
1139 cryp.mac = (void *)md;
1140 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
1141 printf("cryptodev_digest_final: digest failed\n");
1148 memcpy(md, state->digest_res, EVP_MD_CTX_size(ctx));
1153 static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
1156 struct dev_crypto_state *state = EVP_MD_CTX_md_data(ctx);
1157 struct session_op *sess = &state->d_sess;
1162 if (state->d_fd < 0) {
1163 printf("cryptodev_digest_cleanup: illegal input\n");
1167 OPENSSL_free(state->mac_data);
1168 state->mac_data = NULL;
1171 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
1172 printf("cryptodev_digest_cleanup: failed to close session\n");
1177 put_dev_crypto(state->d_fd);
1183 static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1185 struct dev_crypto_state *fstate = EVP_MD_CTX_md_data(from);
1186 struct dev_crypto_state *dstate = EVP_MD_CTX_md_data(to);
1187 struct session_op *sess;
1190 if (dstate == NULL || fstate == NULL)
1193 memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
1195 sess = &dstate->d_sess;
1197 digest = digest_nid_to_cryptodev(EVP_MD_CTX_type(to));
1199 sess->mackey = NULL;
1200 sess->mackeylen = 0;
1203 dstate->d_fd = get_dev_crypto();
1205 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
1206 put_dev_crypto(dstate->d_fd);
1208 printf("cryptodev_digest_copy: Open session failed\n");
1212 if (fstate->mac_len != 0) {
1213 if (fstate->mac_data != NULL) {
1214 dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
1215 if (dstate->mac_data == NULL) {
1216 printf("cryptodev_digest_copy: mac_data allocation failed\n");
1219 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
1220 dstate->mac_len = fstate->mac_len;
1227 static EVP_MD *sha1_md = NULL;
1228 static const EVP_MD *cryptodev_sha1(void)
1230 if (sha1_md == NULL) {
1231 EVP_MD *md = EVP_MD_meth_new(NID_sha1, NID_undef);
1234 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
1235 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1236 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
1237 || !EVP_MD_meth_set_app_datasize(md,
1238 sizeof(struct dev_crypto_state))
1239 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1240 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1241 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1242 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1243 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1244 EVP_MD_meth_free(md);
1252 static EVP_MD *sha256_md = NULL;
1253 static const EVP_MD *cryptodev_sha256(void)
1255 if (sha256_md == NULL) {
1256 EVP_MD *md = EVP_MD_meth_new(NID_sha256, NID_undef);
1259 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
1260 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1261 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
1262 || !EVP_MD_meth_set_app_datasize(md,
1263 sizeof(struct dev_crypto_state))
1264 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1265 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1266 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1267 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1268 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1269 EVP_MD_meth_free(md);
1277 static EVP_MD *sha224_md = NULL;
1278 static const EVP_MD *cryptodev_sha224(void)
1280 if (sha224_md == NULL) {
1281 EVP_MD *md = EVP_MD_meth_new(NID_sha224, NID_undef);
1284 || !EVP_MD_meth_set_result_size(md, SHA224_DIGEST_LENGTH)
1285 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1286 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
1287 || !EVP_MD_meth_set_app_datasize(md,
1288 sizeof(struct dev_crypto_state))
1289 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1290 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1291 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1292 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1293 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1294 EVP_MD_meth_free(md);
1302 static EVP_MD *sha384_md = NULL;
1303 static const EVP_MD *cryptodev_sha384(void)
1305 if (sha384_md == NULL) {
1306 EVP_MD *md = EVP_MD_meth_new(NID_sha384, NID_undef);
1309 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
1310 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1311 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
1312 || !EVP_MD_meth_set_app_datasize(md,
1313 sizeof(struct dev_crypto_state))
1314 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1315 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1316 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1317 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1318 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1319 EVP_MD_meth_free(md);
1327 static EVP_MD *sha512_md = NULL;
1328 static const EVP_MD *cryptodev_sha512(void)
1330 if (sha512_md == NULL) {
1331 EVP_MD *md = EVP_MD_meth_new(NID_sha512, NID_undef);
1334 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
1335 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1336 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
1337 || !EVP_MD_meth_set_app_datasize(md,
1338 sizeof(struct dev_crypto_state))
1339 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1340 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1341 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1342 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1343 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1344 EVP_MD_meth_free(md);
1352 static EVP_MD *md5_md = NULL;
1353 static const EVP_MD *cryptodev_md5(void)
1355 if (md5_md == NULL) {
1356 EVP_MD *md = EVP_MD_meth_new(NID_md5, NID_undef);
1359 || !EVP_MD_meth_set_result_size(md, 16 /* MD5_DIGEST_LENGTH */ )
1360 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_ONESHOT)
1361 || !EVP_MD_meth_set_input_blocksize(md, 64 /* MD5_CBLOCK */ )
1362 || !EVP_MD_meth_set_app_datasize(md,
1363 sizeof(struct dev_crypto_state))
1364 || !EVP_MD_meth_set_init(md, cryptodev_digest_init)
1365 || !EVP_MD_meth_set_update(md, cryptodev_digest_update)
1366 || !EVP_MD_meth_set_final(md, cryptodev_digest_final)
1367 || !EVP_MD_meth_set_copy(md, cryptodev_digest_copy)
1368 || !EVP_MD_meth_set_cleanup(md, cryptodev_digest_cleanup)) {
1369 EVP_MD_meth_free(md);
1377 # endif /* USE_CRYPTODEV_DIGESTS */
1380 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
1381 const int **nids, int nid)
1384 return (cryptodev_usable_digests(nids));
1387 # ifdef USE_CRYPTODEV_DIGESTS
1389 *digest = cryptodev_md5();
1392 *digest = cryptodev_sha1();
1395 *digest = cryptodev_sha256();
1398 *digest = cryptodev_sha224();
1401 *digest = cryptodev_sha384();
1404 *digest = cryptodev_sha512();
1407 # endif /* USE_CRYPTODEV_DIGESTS */
1411 return (*digest != NULL);
1414 static int cryptodev_engine_destroy(ENGINE *e)
1416 EVP_CIPHER_meth_free(rc4_cipher);
1418 EVP_CIPHER_meth_free(des_cbc_cipher);
1419 des_cbc_cipher = NULL;
1420 EVP_CIPHER_meth_free(des3_cbc_cipher);
1421 des3_cbc_cipher = NULL;
1422 EVP_CIPHER_meth_free(des3_ecb_cipher);
1423 des3_ecb_cipher = NULL;
1424 EVP_CIPHER_meth_free(bf_cbc_cipher);
1425 bf_cbc_cipher = NULL;
1426 EVP_CIPHER_meth_free(cast_cbc_cipher);
1427 cast_cbc_cipher = NULL;
1428 EVP_CIPHER_meth_free(aes_cbc_cipher);
1429 aes_cbc_cipher = NULL;
1430 EVP_CIPHER_meth_free(aes_192_cbc_cipher);
1431 aes_192_cbc_cipher = NULL;
1432 EVP_CIPHER_meth_free(aes_256_cbc_cipher);
1433 aes_256_cbc_cipher = NULL;
1434 EVP_CIPHER_meth_free(aes_ctr_cipher);
1435 aes_ctr_cipher = NULL;
1436 EVP_CIPHER_meth_free(aes_192_ctr_cipher);
1437 aes_192_ctr_cipher = NULL;
1438 EVP_CIPHER_meth_free(aes_256_ctr_cipher);
1439 aes_256_ctr_cipher = NULL;
1440 EVP_CIPHER_meth_free(aes_ecb_cipher);
1441 aes_ecb_cipher = NULL;
1442 EVP_CIPHER_meth_free(aes_192_ecb_cipher);
1443 aes_192_ecb_cipher = NULL;
1444 EVP_CIPHER_meth_free(aes_256_ecb_cipher);
1445 aes_256_ecb_cipher = NULL;
1446 # ifdef USE_CRYPTODEV_DIGESTS
1447 EVP_MD_meth_free(sha1_md);
1449 EVP_MD_meth_free(sha256_md);
1451 EVP_MD_meth_free(sha224_md);
1453 EVP_MD_meth_free(sha384_md);
1455 EVP_MD_meth_free(sha512_md);
1457 EVP_MD_meth_free(md5_md);
1460 RSA_meth_free(cryptodev_rsa);
1461 cryptodev_rsa = NULL;
1462 # ifndef OPENSSL_NO_DSA
1463 DSA_meth_free(cryptodev_dsa);
1464 cryptodev_dsa = NULL;
1466 # ifndef OPENSSL_NO_DH
1467 DH_meth_free(cryptodev_dh);
1468 cryptodev_dh = NULL;
1474 * Convert a BIGNUM to the representation that /dev/crypto needs.
1475 * Upon completion of use, the caller is responsible for freeing
1478 static int bn2crparam(const BIGNUM *a, struct crparam *crp)
1480 ssize_t bytes, bits;
1486 bits = BN_num_bits(a);
1487 bytes = BN_num_bytes(a);
1489 b = OPENSSL_zalloc(bytes);
1493 crp->crp_p = (void *)b;
1494 crp->crp_nbits = bits;
1500 /* Convert a /dev/crypto parameter to a BIGNUM */
1501 static int crparam2bn(struct crparam *crp, BIGNUM *a)
1506 bytes = (crp->crp_nbits + 7) / 8;
1511 if ((pd = OPENSSL_malloc(bytes)) == NULL)
1514 for (i = 0; i < bytes; i++)
1515 pd[i] = crp->crp_p[bytes - i - 1];
1517 BN_bin2bn(pd, bytes, a);
1523 static void zapparams(struct crypt_kop *kop)
1527 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
1528 OPENSSL_free(kop->crk_param[i].crp_p);
1529 kop->crk_param[i].crp_p = NULL;
1530 kop->crk_param[i].crp_nbits = 0;
1535 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
1539 if ((fd = get_asym_dev_crypto()) < 0)
1543 kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_zalloc(rlen);
1544 if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
1546 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
1550 kop->crk_param[kop->crk_iparams + 1].crp_p = OPENSSL_zalloc(slen);
1551 /* No need to free the kop->crk_iparams parameter if it was allocated,
1552 * callers of this routine have to free allocated parameters through
1553 * zapparams both in case of success and failure
1555 if (kop->crk_param[kop->crk_iparams + 1].crp_p == NULL)
1557 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
1561 if (ioctl(fd, CIOCKEY, kop) == 0) {
1563 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
1565 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
1573 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1574 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1576 struct crypt_kop kop;
1580 * Currently, we know we can do mod exp iff we can do any asymmetric
1581 * operations at all.
1583 if (cryptodev_asymfeat == 0) {
1584 ret = BN_mod_exp(r, a, p, m, ctx);
1588 memset(&kop, 0, sizeof(kop));
1589 kop.crk_op = CRK_MOD_EXP;
1591 /* inputs: a^p % m */
1592 if (bn2crparam(a, &kop.crk_param[0]))
1594 if (bn2crparam(p, &kop.crk_param[1]))
1596 if (bn2crparam(m, &kop.crk_param[2]))
1598 kop.crk_iparams = 3;
1600 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
1601 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1602 printf("OCF asym process failed, Running in software\n");
1603 ret = RSA_meth_get_bn_mod_exp(meth) (r, a, p, m, ctx, in_mont);
1605 } else if (ECANCELED == kop.crk_status) {
1606 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1607 printf("OCF hardware operation cancelled. Running in Software\n");
1608 ret = RSA_meth_get_bn_mod_exp(meth) (r, a, p, m, ctx, in_mont);
1610 /* else cryptodev operation worked ok ==> ret = 1 */
1618 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1621 const BIGNUM *n = NULL;
1622 const BIGNUM *d = NULL;
1625 RSA_get0_key(rsa, &n, NULL, &d);
1626 r = cryptodev_bn_mod_exp(r0, I, d, n, ctx, NULL);
1632 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
1634 struct crypt_kop kop;
1636 const BIGNUM *p = NULL;
1637 const BIGNUM *q = NULL;
1638 const BIGNUM *dmp1 = NULL;
1639 const BIGNUM *dmq1 = NULL;
1640 const BIGNUM *iqmp = NULL;
1641 const BIGNUM *n = NULL;
1643 RSA_get0_factors(rsa, &p, &q);
1644 RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
1645 RSA_get0_key(rsa, &n, NULL, NULL);
1647 if (!p || !q || !dmp1 || !dmq1 || !iqmp) {
1648 /* XXX 0 means failure?? */
1652 memset(&kop, 0, sizeof(kop));
1653 kop.crk_op = CRK_MOD_EXP_CRT;
1654 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
1655 if (bn2crparam(p, &kop.crk_param[0]))
1657 if (bn2crparam(q, &kop.crk_param[1]))
1659 if (bn2crparam(I, &kop.crk_param[2]))
1661 if (bn2crparam(dmp1, &kop.crk_param[3]))
1663 if (bn2crparam(dmq1, &kop.crk_param[4]))
1665 if (bn2crparam(iqmp, &kop.crk_param[5]))
1667 kop.crk_iparams = 6;
1669 if (cryptodev_asym(&kop, BN_num_bytes(n), r0, 0, NULL)) {
1670 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1671 printf("OCF asym process failed, running in Software\n");
1672 ret = RSA_meth_get_mod_exp(meth) (r0, I, rsa, ctx);
1674 } else if (ECANCELED == kop.crk_status) {
1675 const RSA_METHOD *meth = RSA_PKCS1_OpenSSL();
1676 printf("OCF hardware operation cancelled. Running in Software\n");
1677 ret = RSA_meth_get_mod_exp(meth) (r0, I, rsa, ctx);
1679 /* else cryptodev operation worked ok ==> ret = 1 */
1686 # ifndef OPENSSL_NO_DSA
1688 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1689 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1691 return cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx);
1695 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, const BIGNUM *g,
1696 const BIGNUM *u1, const BIGNUM *pub_key,
1697 const BIGNUM *u2, const BIGNUM *p, BN_CTX *ctx,
1700 const BIGNUM *dsag, *dsap, *dsapub_key;
1703 const DSA_METHOD *meth;
1704 int (*bn_mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *,
1705 const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
1711 /* v = ( g^u1 * y^u2 mod p ) mod q */
1712 /* let t1 = g ^ u1 mod p */
1715 DSA_get0_pqg(dsa, &dsap, NULL, &dsag);
1716 DSA_get0_key(dsa, &dsapub_key, NULL);
1718 meth = DSA_get_method(dsa);
1721 bn_mod_exp = DSA_meth_get_bn_mod_exp(meth);
1722 if (bn_mod_exp == NULL)
1725 if (!bn_mod_exp(dsa, t1, dsag, u1, dsap, ctx, mont))
1728 /* let t2 = y ^ u2 mod p */
1729 if (!bn_mod_exp(dsa, t2, dsapub_key, u2, dsap, ctx, mont))
1731 /* let t1 = t1 * t2 mod p */
1732 if (!BN_mod_mul(t1, t1, t2, dsap, ctx))
1741 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
1744 struct crypt_kop kop;
1746 const BIGNUM *dsap = NULL, *dsaq = NULL, *dsag = NULL;
1747 const BIGNUM *priv_key = NULL;
1748 DSA_SIG *dsasig, *dsaret = NULL;
1750 dsasig = DSA_SIG_new();
1754 memset(&kop, 0, sizeof(kop));
1755 kop.crk_op = CRK_DSA_SIGN;
1757 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
1758 kop.crk_param[0].crp_p = (void *)dgst;
1759 kop.crk_param[0].crp_nbits = dlen * 8;
1760 DSA_get0_pqg(dsa, &dsap, &dsaq, &dsag);
1761 DSA_get0_key(dsa, NULL, &priv_key);
1762 if (bn2crparam(dsap, &kop.crk_param[1]))
1764 if (bn2crparam(dsaq, &kop.crk_param[2]))
1766 if (bn2crparam(dsag, &kop.crk_param[3]))
1768 if (bn2crparam(priv_key, &kop.crk_param[4]))
1770 kop.crk_iparams = 5;
1778 if (cryptodev_asym(&kop, BN_num_bytes(dsaq), r, BN_num_bytes(dsaq), s) == 0) {
1779 DSA_SIG_set0(dsasig, r, s);
1782 dsaret = DSA_meth_get_sign(DSA_OpenSSL())(dgst, dlen, dsa);
1785 if (dsaret != dsasig)
1786 DSA_SIG_free(dsasig);
1787 kop.crk_param[0].crp_p = NULL;
1793 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
1794 DSA_SIG *sig, DSA *dsa)
1796 struct crypt_kop kop;
1798 const BIGNUM *pr, *ps, *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL;
1800 memset(&kop, 0, sizeof(kop));
1801 kop.crk_op = CRK_DSA_VERIFY;
1803 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
1804 kop.crk_param[0].crp_p = (void *)dgst;
1805 kop.crk_param[0].crp_nbits = dlen * 8;
1806 DSA_get0_pqg(dsa, &p, &q, &g);
1807 if (bn2crparam(p, &kop.crk_param[1]))
1809 if (bn2crparam(q, &kop.crk_param[2]))
1811 if (bn2crparam(g, &kop.crk_param[3]))
1813 DSA_get0_key(dsa, &pub_key, NULL);
1814 if (bn2crparam(pub_key, &kop.crk_param[4]))
1816 DSA_SIG_get0(sig, &pr, &ps);
1817 if (bn2crparam(pr, &kop.crk_param[5]))
1819 if (bn2crparam(ps, &kop.crk_param[6]))
1821 kop.crk_iparams = 7;
1823 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
1825 * OCF success value is 0, if not zero, change dsaret to fail
1827 if (0 != kop.crk_status)
1830 dsaret = DSA_meth_get_verify(DSA_OpenSSL())(dgst, dlen, sig, dsa);
1833 kop.crk_param[0].crp_p = NULL;
1839 # ifndef OPENSSL_NO_DH
1841 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
1842 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1845 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
1849 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
1851 struct crypt_kop kop;
1854 const BIGNUM *p = NULL;
1855 const BIGNUM *priv_key = NULL;
1857 if ((fd = get_asym_dev_crypto()) < 0) {
1858 const DH_METHOD *meth = DH_OpenSSL();
1860 return DH_meth_get_compute_key(meth) (key, pub_key, dh);
1863 DH_get0_pqg(dh, &p, NULL, NULL);
1864 DH_get0_key(dh, NULL, &priv_key);
1866 keylen = BN_num_bits(p);
1868 memset(&kop, 0, sizeof(kop));
1869 kop.crk_op = CRK_DH_COMPUTE_KEY;
1871 /* inputs: dh->priv_key pub_key dh->p key */
1872 if (bn2crparam(priv_key, &kop.crk_param[0]))
1874 if (bn2crparam(pub_key, &kop.crk_param[1]))
1876 if (bn2crparam(p, &kop.crk_param[2]))
1878 kop.crk_iparams = 3;
1880 kop.crk_param[3].crp_p = (void *)key;
1881 kop.crk_param[3].crp_nbits = keylen;
1882 kop.crk_oparams = 1;
1884 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1885 const DH_METHOD *meth = DH_OpenSSL();
1887 dhret = DH_meth_get_compute_key(meth) (key, pub_key, dh);
1890 kop.crk_param[3].crp_p = NULL;
1895 # endif /* ndef OPENSSL_NO_DH */
1898 * ctrl right now is just a wrapper that doesn't do much
1899 * but I expect we'll want some options soon.
1901 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
1903 # ifdef HAVE_SYSLOG_R
1904 struct syslog_data sd = SYSLOG_DATA_INIT;
1909 # ifdef HAVE_SYSLOG_R
1910 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
1912 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1919 void engine_load_cryptodev_int(void)
1921 ENGINE *engine = ENGINE_new();
1926 if ((fd = get_dev_crypto()) < 0) {
1927 ENGINE_free(engine);
1932 * find out what asymmetric crypto algorithms we support
1934 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1936 ENGINE_free(engine);
1941 if (!ENGINE_set_id(engine, "cryptodev") ||
1942 !ENGINE_set_name(engine, "cryptodev engine") ||
1943 !ENGINE_set_destroy_function(engine, cryptodev_engine_destroy) ||
1944 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1945 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1946 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1947 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1948 ENGINE_free(engine);
1952 cryptodev_rsa = RSA_meth_dup(RSA_PKCS1_OpenSSL());
1953 if (cryptodev_rsa != NULL) {
1954 RSA_meth_set1_name(cryptodev_rsa, "cryptodev RSA method");
1955 RSA_meth_set_flags(cryptodev_rsa, 0);
1956 if (ENGINE_set_RSA(engine, cryptodev_rsa)) {
1957 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1958 RSA_meth_set_bn_mod_exp(cryptodev_rsa, cryptodev_bn_mod_exp);
1959 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1960 RSA_meth_set_mod_exp(cryptodev_rsa, cryptodev_rsa_mod_exp);
1962 RSA_meth_set_mod_exp(cryptodev_rsa,
1963 cryptodev_rsa_nocrt_mod_exp);
1967 ENGINE_free(engine);
1971 # ifndef OPENSSL_NO_DSA
1972 cryptodev_dsa = DSA_meth_dup(DSA_OpenSSL());
1973 if (cryptodev_dsa != NULL) {
1974 DSA_meth_set1_name(cryptodev_dsa, "cryptodev DSA method");
1975 DSA_meth_set_flags(cryptodev_dsa, 0);
1976 if (ENGINE_set_DSA(engine, cryptodev_dsa)) {
1977 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1978 DSA_meth_set_sign(cryptodev_dsa, cryptodev_dsa_do_sign);
1979 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1980 DSA_meth_set_bn_mod_exp(cryptodev_dsa,
1981 cryptodev_dsa_bn_mod_exp);
1982 DSA_meth_set_mod_exp(cryptodev_dsa, cryptodev_dsa_dsa_mod_exp);
1984 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1985 DSA_meth_set_verify(cryptodev_dsa, cryptodev_dsa_verify);
1988 ENGINE_free(engine);
1993 # ifndef OPENSSL_NO_DH
1994 cryptodev_dh = DH_meth_dup(DH_OpenSSL());
1995 if (cryptodev_dh != NULL) {
1996 DH_meth_set1_name(cryptodev_dh, "cryptodev DH method");
1997 DH_meth_set_flags(cryptodev_dh, 0);
1998 if (ENGINE_set_DH(engine, cryptodev_dh)) {
1999 if (cryptodev_asymfeat & CRF_MOD_EXP) {
2000 DH_meth_set_bn_mod_exp(cryptodev_dh, cryptodev_mod_exp_dh);
2001 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
2002 DH_meth_set_compute_key(cryptodev_dh,
2003 cryptodev_dh_compute_key);
2007 ENGINE_free(engine);
2013 ENGINE_free(engine);
2017 #endif /* HAVE_CRYPTODEV */