xref: /freebsd/sys/kgssapi/krb5/kcrypto_aes.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Authors: Doug Rabson <dfr@rabson.org>
6  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 #include <sys/kobj.h>
36 #include <sys/mbuf.h>
37 #include <opencrypto/cryptodev.h>
38 
39 #include <kgssapi/gssapi.h>
40 #include <kgssapi/gssapi_impl.h>
41 
42 #include "kcrypto.h"
43 
44 struct aes_state {
45 	struct mtx	as_lock;
46 	crypto_session_t as_session_aes;
47 	crypto_session_t as_session_sha1;
48 };
49 
50 static void
51 aes_init(struct krb5_key_state *ks)
52 {
53 	struct aes_state *as;
54 
55 	as = malloc(sizeof(struct aes_state), M_GSSAPI, M_WAITOK|M_ZERO);
56 	mtx_init(&as->as_lock, "gss aes lock", NULL, MTX_DEF);
57 	ks->ks_priv = as;
58 }
59 
60 static void
61 aes_destroy(struct krb5_key_state *ks)
62 {
63 	struct aes_state *as = ks->ks_priv;
64 
65 	if (as->as_session_aes != 0)
66 		crypto_freesession(as->as_session_aes);
67 	if (as->as_session_sha1 != 0)
68 		crypto_freesession(as->as_session_sha1);
69 	mtx_destroy(&as->as_lock);
70 	free(ks->ks_priv, M_GSSAPI);
71 }
72 
73 static void
74 aes_set_key(struct krb5_key_state *ks, const void *in)
75 {
76 	void *kp = ks->ks_key;
77 	struct aes_state *as = ks->ks_priv;
78 	struct crypto_session_params csp;
79 
80 	if (kp != in)
81 		bcopy(in, kp, ks->ks_class->ec_keylen);
82 
83 	if (as->as_session_aes != 0)
84 		crypto_freesession(as->as_session_aes);
85 	if (as->as_session_sha1 != 0)
86 		crypto_freesession(as->as_session_sha1);
87 
88 	/*
89 	 * We only want the first 96 bits of the HMAC.
90 	 */
91 	memset(&csp, 0, sizeof(csp));
92 	csp.csp_mode = CSP_MODE_DIGEST;
93 	csp.csp_auth_alg = CRYPTO_SHA1_HMAC;
94 	csp.csp_auth_klen = ks->ks_class->ec_keybits / 8;
95 	csp.csp_auth_mlen = 12;
96 	csp.csp_auth_key = ks->ks_key;
97 	crypto_newsession(&as->as_session_sha1, &csp,
98 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
99 
100 	memset(&csp, 0, sizeof(csp));
101 	csp.csp_mode = CSP_MODE_CIPHER;
102 	csp.csp_cipher_alg = CRYPTO_AES_CBC;
103 	csp.csp_cipher_klen = ks->ks_class->ec_keybits / 8;
104 	csp.csp_cipher_key = ks->ks_key;
105 	csp.csp_ivlen = 16;
106 	crypto_newsession(&as->as_session_aes, &csp,
107 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
108 }
109 
110 static void
111 aes_random_to_key(struct krb5_key_state *ks, const void *in)
112 {
113 
114 	aes_set_key(ks, in);
115 }
116 
117 static int
118 aes_crypto_cb(struct cryptop *crp)
119 {
120 	int error;
121 	struct aes_state *as = (struct aes_state *) crp->crp_opaque;
122 
123 	if (CRYPTO_SESS_SYNC(crp->crp_session))
124 		return (0);
125 
126 	error = crp->crp_etype;
127 	if (error == EAGAIN)
128 		error = crypto_dispatch(crp);
129 	mtx_lock(&as->as_lock);
130 	if (error || (crp->crp_flags & CRYPTO_F_DONE))
131 		wakeup(crp);
132 	mtx_unlock(&as->as_lock);
133 
134 	return (0);
135 }
136 
137 static void
138 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
139     size_t skip, size_t len, void *ivec, bool encrypt)
140 {
141 	struct aes_state *as = ks->ks_priv;
142 	struct cryptop *crp;
143 	int error;
144 
145 	crp = crypto_getreq(as->as_session_aes, M_WAITOK);
146 
147 	crp->crp_payload_start = skip;
148 	crp->crp_payload_length = len;
149 	crp->crp_op = encrypt ? CRYPTO_OP_ENCRYPT : CRYPTO_OP_DECRYPT;
150 	crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_IV_SEPARATE;
151 	if (ivec) {
152 		memcpy(crp->crp_iv, ivec, 16);
153 	} else {
154 		memset(crp->crp_iv, 0, 16);
155 	}
156 
157 	if (buftype == CRYPTO_BUF_MBUF)
158 		crypto_use_mbuf(crp, buf);
159 	else
160 		crypto_use_buf(crp, buf, skip + len);
161 	crp->crp_opaque = as;
162 	crp->crp_callback = aes_crypto_cb;
163 
164 	error = crypto_dispatch(crp);
165 
166 	if (!CRYPTO_SESS_SYNC(as->as_session_aes)) {
167 		mtx_lock(&as->as_lock);
168 		if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
169 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
170 		mtx_unlock(&as->as_lock);
171 	}
172 
173 	crypto_freereq(crp);
174 }
175 
176 static void
177 aes_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
178     size_t skip, size_t len, void *ivec, size_t ivlen)
179 {
180 	size_t blocklen = 16, plen;
181 	struct {
182 		uint8_t cn_1[16], cn[16];
183 	} last2;
184 	int i, off;
185 
186 	/*
187 	 * AES encryption with cyphertext stealing:
188 	 *
189 	 * CTSencrypt(P[0], ..., P[n], IV, K):
190 	 *	len = length(P[n])
191 	 *	(C[0], ..., C[n-2], E[n-1]) =
192 	 *		CBCencrypt(P[0], ..., P[n-1], IV, K)
193 	 *	P = pad(P[n], 0, blocksize)
194 	 *	E[n] = CBCencrypt(P, E[n-1], K);
195 	 *	C[n-1] = E[n]
196 	 *	C[n] = E[n-1]{0..len-1}
197 	 */
198 	plen = len % blocklen;
199 	if (len == blocklen) {
200 		/*
201 		 * Note: caller will ensure len >= blocklen.
202 		 */
203 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
204 		    true);
205 	} else if (plen == 0) {
206 		/*
207 		 * This is equivalent to CBC mode followed by swapping
208 		 * the last two blocks. We assume that neither of the
209 		 * last two blocks cross iov boundaries.
210 		 */
211 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
212 		    true);
213 		off = skip + len - 2 * blocklen;
214 		m_copydata(inout, off, 2 * blocklen, (void*) &last2);
215 		m_copyback(inout, off, blocklen, last2.cn);
216 		m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
217 	} else {
218 		/*
219 		 * This is the difficult case. We encrypt all but the
220 		 * last partial block first. We then create a padded
221 		 * copy of the last block and encrypt that using the
222 		 * second to last encrypted block as IV. Once we have
223 		 * the encrypted versions of the last two blocks, we
224 		 * reshuffle to create the final result.
225 		 */
226 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
227 		    ivec, true);
228 
229 		/*
230 		 * Copy out the last two blocks, pad the last block
231 		 * and encrypt it. Rearrange to get the final
232 		 * result. The cyphertext for cn_1 is in cn. The
233 		 * cyphertext for cn is the first plen bytes of what
234 		 * is in cn_1 now.
235 		 */
236 		off = skip + len - blocklen - plen;
237 		m_copydata(inout, off, blocklen + plen, (void*) &last2);
238 		for (i = plen; i < blocklen; i++)
239 			last2.cn[i] = 0;
240 		aes_encrypt_1(ks, CRYPTO_BUF_CONTIG, last2.cn, 0, blocklen,
241 		    last2.cn_1, true);
242 		m_copyback(inout, off, blocklen, last2.cn);
243 		m_copyback(inout, off + blocklen, plen, last2.cn_1);
244 	}
245 }
246 
247 static void
248 aes_decrypt(const struct krb5_key_state *ks, struct mbuf *inout,
249     size_t skip, size_t len, void *ivec, size_t ivlen)
250 {
251 	size_t blocklen = 16, plen;
252 	struct {
253 		uint8_t cn_1[16], cn[16];
254 	} last2;
255 	int i, off, t;
256 
257 	/*
258 	 * AES decryption with cyphertext stealing:
259 	 *
260 	 * CTSencrypt(C[0], ..., C[n], IV, K):
261 	 *	len = length(C[n])
262 	 *	E[n] = C[n-1]
263 	 *	X = decrypt(E[n], K)
264 	 *	P[n] = (X ^ C[n]){0..len-1}
265 	 *	E[n-1] = {C[n,0],...,C[n,len-1],X[len],...,X[blocksize-1]}
266 	 *	(P[0],...,P[n-1]) = CBCdecrypt(C[0],...,C[n-2],E[n-1], IV, K)
267 	 */
268 	plen = len % blocklen;
269 	if (len == blocklen) {
270 		/*
271 		 * Note: caller will ensure len >= blocklen.
272 		 */
273 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
274 		    false);
275 	} else if (plen == 0) {
276 		/*
277 		 * This is equivalent to CBC mode followed by swapping
278 		 * the last two blocks.
279 		 */
280 		off = skip + len - 2 * blocklen;
281 		m_copydata(inout, off, 2 * blocklen, (void*) &last2);
282 		m_copyback(inout, off, blocklen, last2.cn);
283 		m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
284 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
285 		    false);
286 	} else {
287 		/*
288 		 * This is the difficult case. We first decrypt the
289 		 * second to last block with a zero IV to make X. The
290 		 * plaintext for the last block is the XOR of X and
291 		 * the last cyphertext block.
292 		 *
293 		 * We derive a new cypher text for the second to last
294 		 * block by mixing the unused bytes of X with the last
295 		 * cyphertext block. The result of that can be
296 		 * decrypted with the rest in CBC mode.
297 		 */
298 		off = skip + len - plen - blocklen;
299 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, off, blocklen,
300 		    NULL, false);
301 		m_copydata(inout, off, blocklen + plen, (void*) &last2);
302 
303 		for (i = 0; i < plen; i++) {
304 			t = last2.cn[i];
305 			last2.cn[i] ^= last2.cn_1[i];
306 			last2.cn_1[i] = t;
307 		}
308 
309 		m_copyback(inout, off, blocklen + plen, (void*) &last2);
310 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
311 		    ivec, false);
312 	}
313 
314 }
315 
316 static void
317 aes_checksum(const struct krb5_key_state *ks, int usage,
318     struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
319 {
320 	struct aes_state *as = ks->ks_priv;
321 	struct cryptop *crp;
322 	int error;
323 
324 	crp = crypto_getreq(as->as_session_sha1, M_WAITOK);
325 
326 	crp->crp_payload_start = skip;
327 	crp->crp_payload_length = inlen;
328 	crp->crp_digest_start = skip + inlen;
329 	crp->crp_flags = CRYPTO_F_CBIFSYNC;
330 	crypto_use_mbuf(crp, inout);
331 	crp->crp_opaque = as;
332 	crp->crp_callback = aes_crypto_cb;
333 
334 	error = crypto_dispatch(crp);
335 
336 	if (!CRYPTO_SESS_SYNC(as->as_session_sha1)) {
337 		mtx_lock(&as->as_lock);
338 		if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
339 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
340 		mtx_unlock(&as->as_lock);
341 	}
342 
343 	crypto_freereq(crp);
344 }
345 
346 struct krb5_encryption_class krb5_aes128_encryption_class = {
347 	"aes128-cts-hmac-sha1-96", /* name */
348 	ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
349 	EC_DERIVED_KEYS,	/* flags */
350 	16,			/* blocklen */
351 	1,			/* msgblocklen */
352 	12,			/* checksumlen */
353 	128,			/* keybits */
354 	16,			/* keylen */
355 	aes_init,
356 	aes_destroy,
357 	aes_set_key,
358 	aes_random_to_key,
359 	aes_encrypt,
360 	aes_decrypt,
361 	aes_checksum
362 };
363 
364 struct krb5_encryption_class krb5_aes256_encryption_class = {
365 	"aes256-cts-hmac-sha1-96", /* name */
366 	ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
367 	EC_DERIVED_KEYS,	/* flags */
368 	16,			/* blocklen */
369 	1,			/* msgblocklen */
370 	12,			/* checksumlen */
371 	256,			/* keybits */
372 	32,			/* keylen */
373 	aes_init,
374 	aes_destroy,
375 	aes_set_key,
376 	aes_random_to_key,
377 	aes_encrypt,
378 	aes_decrypt,
379 	aes_checksum
380 };
381