2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
43 #include <openssl/evp.h>
46 #include <geom/eli/g_eli.h>
49 MALLOC_DECLARE(M_ELI);
52 g_eli_crypto_done(struct cryptop *crp)
55 crp->crp_opaque = (void *)crp;
61 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
62 const u_char *key, size_t keysize)
64 struct crypto_session_params csp;
69 KASSERT(algo != CRYPTO_AES_XTS,
70 ("%s: CRYPTO_AES_XTS unexpected here", __func__));
72 memset(&csp, 0, sizeof(csp));
73 csp.csp_mode = CSP_MODE_CIPHER;
74 csp.csp_cipher_alg = algo;
75 csp.csp_ivlen = g_eli_ivlen(algo);
76 csp.csp_cipher_key = key;
77 csp.csp_cipher_klen = keysize / 8;
78 error = crypto_newsession(&sid, &csp, CRYPTOCAP_F_SOFTWARE);
81 crp = crypto_getreq(sid, M_NOWAIT);
83 crypto_freesession(sid);
87 crp->crp_payload_start = 0;
88 crp->crp_payload_length = datasize;
89 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_IV_SEPARATE;
90 crp->crp_op = enc ? CRYPTO_OP_ENCRYPT : CRYPTO_OP_DECRYPT;
91 memset(crp->crp_iv, 0, sizeof(crp->crp_iv));
93 crp->crp_opaque = NULL;
94 crp->crp_callback = g_eli_crypto_done;
95 crypto_use_buf(crp, data, datasize);
97 error = crypto_dispatch(crp);
99 while (crp->crp_opaque == NULL)
100 tsleep(crp, PRIBIO, "geli", hz / 5);
101 error = crp->crp_etype;
105 crypto_freesession(sid);
110 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
111 const u_char *key, size_t keysize)
114 const EVP_CIPHER *type;
118 assert(algo != CRYPTO_AES_XTS);
121 case CRYPTO_NULL_CBC:
122 type = EVP_enc_null();
127 type = EVP_aes_128_cbc();
130 type = EVP_aes_192_cbc();
133 type = EVP_aes_256_cbc();
139 #ifndef OPENSSL_NO_CAMELLIA
140 case CRYPTO_CAMELLIA_CBC:
143 type = EVP_camellia_128_cbc();
146 type = EVP_camellia_192_cbc();
149 type = EVP_camellia_256_cbc();
160 ctx = EVP_CIPHER_CTX_new();
164 EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, enc);
165 EVP_CIPHER_CTX_set_key_length(ctx, keysize / 8);
166 EVP_CIPHER_CTX_set_padding(ctx, 0);
167 bzero(iv, sizeof(iv));
168 EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc);
170 if (EVP_CipherUpdate(ctx, data, &outsize, data, datasize) == 0) {
171 EVP_CIPHER_CTX_free(ctx);
174 assert(outsize == (int)datasize);
176 if (EVP_CipherFinal_ex(ctx, data + outsize, &outsize) == 0) {
177 EVP_CIPHER_CTX_free(ctx);
180 assert(outsize == 0);
182 EVP_CIPHER_CTX_free(ctx);
185 #endif /* !_KERNEL */
188 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
189 const u_char *key, size_t keysize)
192 /* We prefer AES-CBC for metadata protection. */
193 if (algo == CRYPTO_AES_XTS)
194 algo = CRYPTO_AES_CBC;
196 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize));
200 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
201 const u_char *key, size_t keysize)
204 /* We prefer AES-CBC for metadata protection. */
205 if (algo == CRYPTO_AES_XTS)
206 algo = CRYPTO_AES_CBC;
208 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize));