xref: /freebsd/sys/kgssapi/krb5/kcrypto_aes.c (revision 2fa185f9bf5948ead9c3920d452ddd6bcad8f569)
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/param.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
34 #include <sys/kobj.h>
35 #include <sys/mbuf.h>
36 #include <opencrypto/cryptodev.h>
37 
38 #include <kgssapi/gssapi.h>
39 #include <kgssapi/gssapi_impl.h>
40 
41 #include "kcrypto.h"
42 
43 struct aes_state {
44 	struct mtx	as_lock;
45 	crypto_session_t as_session_aes;
46 	crypto_session_t as_session_sha1;
47 };
48 
49 static void
aes_init(struct krb5_key_state * ks)50 aes_init(struct krb5_key_state *ks)
51 {
52 	struct aes_state *as;
53 
54 	as = malloc(sizeof(struct aes_state), M_GSSAPI, M_WAITOK|M_ZERO);
55 	mtx_init(&as->as_lock, "gss aes lock", NULL, MTX_DEF);
56 	ks->ks_priv = as;
57 }
58 
59 static void
aes_destroy(struct krb5_key_state * ks)60 aes_destroy(struct krb5_key_state *ks)
61 {
62 	struct aes_state *as = ks->ks_priv;
63 
64 	if (as->as_session_aes != 0)
65 		crypto_freesession(as->as_session_aes);
66 	if (as->as_session_sha1 != 0)
67 		crypto_freesession(as->as_session_sha1);
68 	mtx_destroy(&as->as_lock);
69 	free(ks->ks_priv, M_GSSAPI);
70 }
71 
72 static void
aes_set_key(struct krb5_key_state * ks,const void * in)73 aes_set_key(struct krb5_key_state *ks, const void *in)
74 {
75 	void *kp = ks->ks_key;
76 	struct aes_state *as = ks->ks_priv;
77 	struct crypto_session_params csp;
78 
79 	if (kp != in)
80 		bcopy(in, kp, ks->ks_class->ec_keylen);
81 
82 	if (as->as_session_aes != 0)
83 		crypto_freesession(as->as_session_aes);
84 	if (as->as_session_sha1 != 0)
85 		crypto_freesession(as->as_session_sha1);
86 
87 	/*
88 	 * We only want the first 96 bits of the HMAC.
89 	 */
90 	memset(&csp, 0, sizeof(csp));
91 	csp.csp_mode = CSP_MODE_DIGEST;
92 	csp.csp_auth_alg = CRYPTO_SHA1_HMAC;
93 	csp.csp_auth_klen = ks->ks_class->ec_keybits / 8;
94 	csp.csp_auth_mlen = 12;
95 	csp.csp_auth_key = ks->ks_key;
96 	crypto_newsession(&as->as_session_sha1, &csp,
97 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
98 
99 	memset(&csp, 0, sizeof(csp));
100 	csp.csp_mode = CSP_MODE_CIPHER;
101 	csp.csp_cipher_alg = CRYPTO_AES_CBC;
102 	csp.csp_cipher_klen = ks->ks_class->ec_keybits / 8;
103 	csp.csp_cipher_key = ks->ks_key;
104 	csp.csp_ivlen = 16;
105 	crypto_newsession(&as->as_session_aes, &csp,
106 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
107 }
108 
109 static void
aes_random_to_key(struct krb5_key_state * ks,const void * in)110 aes_random_to_key(struct krb5_key_state *ks, const void *in)
111 {
112 
113 	aes_set_key(ks, in);
114 }
115 
116 static int
aes_crypto_cb(struct cryptop * crp)117 aes_crypto_cb(struct cryptop *crp)
118 {
119 	struct aes_state *as = (struct aes_state *) crp->crp_opaque;
120 
121 	if (CRYPTO_SESS_SYNC(crp->crp_session)) {
122 		KASSERT(crp->crp_etype == 0,
123 		    ("%s: callback with error %d", __func__, crp->crp_etype));
124 		return (0);
125 	}
126 
127 	if (crp->crp_etype == EAGAIN) {
128 		crp->crp_etype = 0;
129 		(void)crypto_dispatch(crp);
130 	} else {
131 		mtx_lock(&as->as_lock);
132 		crp->crp_opaque = NULL;
133 		wakeup(crp);
134 		mtx_unlock(&as->as_lock);
135 	}
136 
137 	return (0);
138 }
139 
140 static void
aes_encrypt_1(const struct krb5_key_state * ks,int buftype,void * buf,size_t skip,size_t len,void * ivec,bool encrypt)141 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
142     size_t skip, size_t len, void *ivec, bool encrypt)
143 {
144 	struct aes_state *as = ks->ks_priv;
145 	struct cryptop *crp;
146 	int error;
147 
148 	crp = crypto_getreq(as->as_session_aes, M_WAITOK);
149 
150 	crp->crp_payload_start = skip;
151 	crp->crp_payload_length = len;
152 	crp->crp_op = encrypt ? CRYPTO_OP_ENCRYPT : CRYPTO_OP_DECRYPT;
153 	crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_IV_SEPARATE;
154 	if (ivec) {
155 		memcpy(crp->crp_iv, ivec, 16);
156 	} else {
157 		memset(crp->crp_iv, 0, 16);
158 	}
159 
160 	if (buftype == CRYPTO_BUF_MBUF)
161 		crypto_use_mbuf(crp, buf);
162 	else
163 		crypto_use_buf(crp, buf, skip + len);
164 	crp->crp_opaque = as;
165 	crp->crp_callback = aes_crypto_cb;
166 
167 	error = crypto_dispatch(crp);
168 
169 	if (!CRYPTO_SESS_SYNC(as->as_session_aes)) {
170 		mtx_lock(&as->as_lock);
171 		if (error == 0 && crp->crp_opaque != NULL)
172 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
173 		mtx_unlock(&as->as_lock);
174 	}
175 	if (crp->crp_etype != 0)
176 		panic("%s: crypto req failed: %d", __func__, crp->crp_etype);
177 	crypto_freereq(crp);
178 }
179 
180 static void
aes_encrypt(const struct krb5_key_state * ks,struct mbuf * inout,size_t skip,size_t len,void * ivec,size_t ivlen)181 aes_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
182     size_t skip, size_t len, void *ivec, size_t ivlen)
183 {
184 	size_t blocklen = 16, plen;
185 	struct {
186 		uint8_t cn_1[16], cn[16];
187 	} last2;
188 	int i, off;
189 
190 	/*
191 	 * AES encryption with cyphertext stealing:
192 	 *
193 	 * CTSencrypt(P[0], ..., P[n], IV, K):
194 	 *	len = length(P[n])
195 	 *	(C[0], ..., C[n-2], E[n-1]) =
196 	 *		CBCencrypt(P[0], ..., P[n-1], IV, K)
197 	 *	P = pad(P[n], 0, blocksize)
198 	 *	E[n] = CBCencrypt(P, E[n-1], K);
199 	 *	C[n-1] = E[n]
200 	 *	C[n] = E[n-1]{0..len-1}
201 	 */
202 	plen = len % blocklen;
203 	if (len == blocklen) {
204 		/*
205 		 * Note: caller will ensure len >= blocklen.
206 		 */
207 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
208 		    true);
209 	} else if (plen == 0) {
210 		/*
211 		 * This is equivalent to CBC mode followed by swapping
212 		 * the last two blocks. We assume that neither of the
213 		 * last two blocks cross iov boundaries.
214 		 */
215 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
216 		    true);
217 		off = skip + len - 2 * blocklen;
218 		m_copydata(inout, off, 2 * blocklen, (void*) &last2);
219 		m_copyback(inout, off, blocklen, last2.cn);
220 		m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
221 	} else {
222 		/*
223 		 * This is the difficult case. We encrypt all but the
224 		 * last partial block first. We then create a padded
225 		 * copy of the last block and encrypt that using the
226 		 * second to last encrypted block as IV. Once we have
227 		 * the encrypted versions of the last two blocks, we
228 		 * reshuffle to create the final result.
229 		 */
230 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
231 		    ivec, true);
232 
233 		/*
234 		 * Copy out the last two blocks, pad the last block
235 		 * and encrypt it. Rearrange to get the final
236 		 * result. The cyphertext for cn_1 is in cn. The
237 		 * cyphertext for cn is the first plen bytes of what
238 		 * is in cn_1 now.
239 		 */
240 		off = skip + len - blocklen - plen;
241 		m_copydata(inout, off, blocklen + plen, (void*) &last2);
242 		for (i = plen; i < blocklen; i++)
243 			last2.cn[i] = 0;
244 		aes_encrypt_1(ks, CRYPTO_BUF_CONTIG, last2.cn, 0, blocklen,
245 		    last2.cn_1, true);
246 		m_copyback(inout, off, blocklen, last2.cn);
247 		m_copyback(inout, off + blocklen, plen, last2.cn_1);
248 	}
249 }
250 
251 static void
aes_decrypt(const struct krb5_key_state * ks,struct mbuf * inout,size_t skip,size_t len,void * ivec,size_t ivlen)252 aes_decrypt(const struct krb5_key_state *ks, struct mbuf *inout,
253     size_t skip, size_t len, void *ivec, size_t ivlen)
254 {
255 	size_t blocklen = 16, plen;
256 	struct {
257 		uint8_t cn_1[16], cn[16];
258 	} last2;
259 	int i, off, t;
260 
261 	/*
262 	 * AES decryption with cyphertext stealing:
263 	 *
264 	 * CTSencrypt(C[0], ..., C[n], IV, K):
265 	 *	len = length(C[n])
266 	 *	E[n] = C[n-1]
267 	 *	X = decrypt(E[n], K)
268 	 *	P[n] = (X ^ C[n]){0..len-1}
269 	 *	E[n-1] = {C[n,0],...,C[n,len-1],X[len],...,X[blocksize-1]}
270 	 *	(P[0],...,P[n-1]) = CBCdecrypt(C[0],...,C[n-2],E[n-1], IV, K)
271 	 */
272 	plen = len % blocklen;
273 	if (len == blocklen) {
274 		/*
275 		 * Note: caller will ensure len >= blocklen.
276 		 */
277 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
278 		    false);
279 	} else if (plen == 0) {
280 		/*
281 		 * This is equivalent to CBC mode followed by swapping
282 		 * the last two blocks.
283 		 */
284 		off = skip + len - 2 * blocklen;
285 		m_copydata(inout, off, 2 * blocklen, (void*) &last2);
286 		m_copyback(inout, off, blocklen, last2.cn);
287 		m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
288 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
289 		    false);
290 	} else {
291 		/*
292 		 * This is the difficult case. We first decrypt the
293 		 * second to last block with a zero IV to make X. The
294 		 * plaintext for the last block is the XOR of X and
295 		 * the last cyphertext block.
296 		 *
297 		 * We derive a new cypher text for the second to last
298 		 * block by mixing the unused bytes of X with the last
299 		 * cyphertext block. The result of that can be
300 		 * decrypted with the rest in CBC mode.
301 		 */
302 		off = skip + len - plen - blocklen;
303 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, off, blocklen,
304 		    NULL, false);
305 		m_copydata(inout, off, blocklen + plen, (void*) &last2);
306 
307 		for (i = 0; i < plen; i++) {
308 			t = last2.cn[i];
309 			last2.cn[i] ^= last2.cn_1[i];
310 			last2.cn_1[i] = t;
311 		}
312 
313 		m_copyback(inout, off, blocklen + plen, (void*) &last2);
314 		aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
315 		    ivec, false);
316 	}
317 
318 }
319 
320 static void
aes_checksum(const struct krb5_key_state * ks,int usage,struct mbuf * inout,size_t skip,size_t inlen,size_t outlen)321 aes_checksum(const struct krb5_key_state *ks, int usage,
322     struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
323 {
324 	struct aes_state *as = ks->ks_priv;
325 	struct cryptop *crp;
326 	int error;
327 
328 	crp = crypto_getreq(as->as_session_sha1, M_WAITOK);
329 
330 	crp->crp_payload_start = skip;
331 	crp->crp_payload_length = inlen;
332 	crp->crp_digest_start = skip + inlen;
333 	crp->crp_flags = CRYPTO_F_CBIFSYNC;
334 	crypto_use_mbuf(crp, inout);
335 	crp->crp_opaque = as;
336 	crp->crp_callback = aes_crypto_cb;
337 
338 	error = crypto_dispatch(crp);
339 
340 	if (!CRYPTO_SESS_SYNC(as->as_session_sha1)) {
341 		mtx_lock(&as->as_lock);
342 		if (error == 0 && crp->crp_opaque != NULL)
343 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
344 		mtx_unlock(&as->as_lock);
345 	}
346 
347 	if (crp->crp_etype != 0)
348 		panic("%s: crypto req failed: %d", __func__, crp->crp_etype);
349 	crypto_freereq(crp);
350 }
351 
352 struct krb5_encryption_class krb5_aes128_encryption_class = {
353 	"aes128-cts-hmac-sha1-96", /* name */
354 	ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
355 	EC_DERIVED_KEYS,	/* flags */
356 	16,			/* blocklen */
357 	1,			/* msgblocklen */
358 	12,			/* checksumlen */
359 	128,			/* keybits */
360 	16,			/* keylen */
361 	aes_init,
362 	aes_destroy,
363 	aes_set_key,
364 	aes_random_to_key,
365 	aes_encrypt,
366 	aes_decrypt,
367 	aes_checksum
368 };
369 
370 struct krb5_encryption_class krb5_aes256_encryption_class = {
371 	"aes256-cts-hmac-sha1-96", /* name */
372 	ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
373 	EC_DERIVED_KEYS,	/* flags */
374 	16,			/* blocklen */
375 	1,			/* msgblocklen */
376 	12,			/* checksumlen */
377 	256,			/* keybits */
378 	32,			/* keylen */
379 	aes_init,
380 	aes_destroy,
381 	aes_set_key,
382 	aes_random_to_key,
383 	aes_encrypt,
384 	aes_decrypt,
385 	aes_checksum
386 };
387