xref: /freebsd/sys/geom/eli/g_eli_crypto.c (revision f5b7695d2d5abd735064870ad43f4b9c723940c1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #ifdef _KERNEL
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #else
38 #include <stdint.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <errno.h>
42 #include <assert.h>
43 #include <openssl/evp.h>
44 #define	_OpenSSL_
45 #endif
46 #include <geom/eli/g_eli.h>
47 
48 #ifdef _KERNEL
49 MALLOC_DECLARE(M_ELI);
50 
51 static int
52 g_eli_crypto_done(struct cryptop *crp)
53 {
54 
55 	crp->crp_opaque = (void *)crp;
56 	wakeup(crp);
57 	return (0);
58 }
59 
60 static int
61 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
62     const u_char *key, size_t keysize)
63 {
64 	struct crypto_session_params csp;
65 	struct cryptop *crp;
66 	crypto_session_t sid;
67 	int error;
68 
69 	KASSERT(algo != CRYPTO_AES_XTS,
70 	    ("%s: CRYPTO_AES_XTS unexpected here", __func__));
71 
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);
79 	if (error != 0)
80 		return (error);
81 	crp = crypto_getreq(sid, M_NOWAIT);
82 	if (crp == NULL) {
83 		crypto_freesession(sid);
84 		return (ENOMEM);
85 	}
86 
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));
92 
93 	crp->crp_opaque = NULL;
94 	crp->crp_callback = g_eli_crypto_done;
95 	crp->crp_buf_type = CRYPTO_BUF_CONTIG;
96 	crp->crp_ilen = datasize;
97 	crp->crp_buf = (void *)data;
98 
99 	error = crypto_dispatch(crp);
100 	if (error == 0) {
101 		while (crp->crp_opaque == NULL)
102 			tsleep(crp, PRIBIO, "geli", hz / 5);
103 		error = crp->crp_etype;
104 	}
105 
106 	crypto_freereq(crp);
107 	crypto_freesession(sid);
108 	return (error);
109 }
110 #else	/* !_KERNEL */
111 static int
112 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
113     const u_char *key, size_t keysize)
114 {
115 	EVP_CIPHER_CTX *ctx;
116 	const EVP_CIPHER *type;
117 	u_char iv[keysize];
118 	int outsize;
119 
120 	assert(algo != CRYPTO_AES_XTS);
121 
122 	switch (algo) {
123 	case CRYPTO_NULL_CBC:
124 		type = EVP_enc_null();
125 		break;
126 	case CRYPTO_AES_CBC:
127 		switch (keysize) {
128 		case 128:
129 			type = EVP_aes_128_cbc();
130 			break;
131 		case 192:
132 			type = EVP_aes_192_cbc();
133 			break;
134 		case 256:
135 			type = EVP_aes_256_cbc();
136 			break;
137 		default:
138 			return (EINVAL);
139 		}
140 		break;
141 	case CRYPTO_BLF_CBC:
142 		type = EVP_bf_cbc();
143 		break;
144 #ifndef OPENSSL_NO_CAMELLIA
145 	case CRYPTO_CAMELLIA_CBC:
146 		switch (keysize) {
147 		case 128:
148 			type = EVP_camellia_128_cbc();
149 			break;
150 		case 192:
151 			type = EVP_camellia_192_cbc();
152 			break;
153 		case 256:
154 			type = EVP_camellia_256_cbc();
155 			break;
156 		default:
157 			return (EINVAL);
158 		}
159 		break;
160 #endif
161 	case CRYPTO_3DES_CBC:
162 		type = EVP_des_ede3_cbc();
163 		break;
164 	default:
165 		return (EINVAL);
166 	}
167 
168 	ctx = EVP_CIPHER_CTX_new();
169 	if (ctx == NULL)
170 		return (ENOMEM);
171 
172 	EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, enc);
173 	EVP_CIPHER_CTX_set_key_length(ctx, keysize / 8);
174 	EVP_CIPHER_CTX_set_padding(ctx, 0);
175 	bzero(iv, sizeof(iv));
176 	EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc);
177 
178 	if (EVP_CipherUpdate(ctx, data, &outsize, data, datasize) == 0) {
179 		EVP_CIPHER_CTX_free(ctx);
180 		return (EINVAL);
181 	}
182 	assert(outsize == (int)datasize);
183 
184 	if (EVP_CipherFinal_ex(ctx, data + outsize, &outsize) == 0) {
185 		EVP_CIPHER_CTX_free(ctx);
186 		return (EINVAL);
187 	}
188 	assert(outsize == 0);
189 
190 	EVP_CIPHER_CTX_free(ctx);
191 	return (0);
192 }
193 #endif	/* !_KERNEL */
194 
195 int
196 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
197     const u_char *key, size_t keysize)
198 {
199 
200 	/* We prefer AES-CBC for metadata protection. */
201 	if (algo == CRYPTO_AES_XTS)
202 		algo = CRYPTO_AES_CBC;
203 
204 	return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize));
205 }
206 
207 int
208 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
209     const u_char *key, size_t keysize)
210 {
211 
212 	/* We prefer AES-CBC for metadata protection. */
213 	if (algo == CRYPTO_AES_XTS)
214 		algo = CRYPTO_AES_CBC;
215 
216 	return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize));
217 }
218