xref: /freebsd/sys/kgssapi/krb5/kcrypto_aes.c (revision cc426dd31990b8b50b210efc450e404596548ca1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/kobj.h>
38 #include <sys/mbuf.h>
39 #include <opencrypto/cryptodev.h>
40 
41 #include <kgssapi/gssapi.h>
42 #include <kgssapi/gssapi_impl.h>
43 
44 #include "kcrypto.h"
45 
46 struct aes_state {
47 	struct mtx	as_lock;
48 	crypto_session_t as_session_aes;
49 	crypto_session_t as_session_sha1;
50 };
51 
52 static void
53 aes_init(struct krb5_key_state *ks)
54 {
55 	struct aes_state *as;
56 
57 	as = malloc(sizeof(struct aes_state), M_GSSAPI, M_WAITOK|M_ZERO);
58 	mtx_init(&as->as_lock, "gss aes lock", NULL, MTX_DEF);
59 	ks->ks_priv = as;
60 }
61 
62 static void
63 aes_destroy(struct krb5_key_state *ks)
64 {
65 	struct aes_state *as = ks->ks_priv;
66 
67 	if (as->as_session_aes != 0)
68 		crypto_freesession(as->as_session_aes);
69 	if (as->as_session_sha1 != 0)
70 		crypto_freesession(as->as_session_sha1);
71 	mtx_destroy(&as->as_lock);
72 	free(ks->ks_priv, M_GSSAPI);
73 }
74 
75 static void
76 aes_set_key(struct krb5_key_state *ks, const void *in)
77 {
78 	void *kp = ks->ks_key;
79 	struct aes_state *as = ks->ks_priv;
80 	struct cryptoini cri;
81 
82 	if (kp != in)
83 		bcopy(in, kp, ks->ks_class->ec_keylen);
84 
85 	if (as->as_session_aes != 0)
86 		crypto_freesession(as->as_session_aes);
87 	if (as->as_session_sha1 != 0)
88 		crypto_freesession(as->as_session_sha1);
89 
90 	/*
91 	 * We only want the first 96 bits of the HMAC.
92 	 */
93 	bzero(&cri, sizeof(cri));
94 	cri.cri_alg = CRYPTO_SHA1_HMAC;
95 	cri.cri_klen = ks->ks_class->ec_keybits;
96 	cri.cri_mlen = 12;
97 	cri.cri_key = ks->ks_key;
98 	cri.cri_next = NULL;
99 	crypto_newsession(&as->as_session_sha1, &cri,
100 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
101 
102 	bzero(&cri, sizeof(cri));
103 	cri.cri_alg = CRYPTO_AES_CBC;
104 	cri.cri_klen = ks->ks_class->ec_keybits;
105 	cri.cri_mlen = 0;
106 	cri.cri_key = ks->ks_key;
107 	cri.cri_next = NULL;
108 	crypto_newsession(&as->as_session_aes, &cri,
109 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
110 }
111 
112 static void
113 aes_random_to_key(struct krb5_key_state *ks, const void *in)
114 {
115 
116 	aes_set_key(ks, in);
117 }
118 
119 static int
120 aes_crypto_cb(struct cryptop *crp)
121 {
122 	int error;
123 	struct aes_state *as = (struct aes_state *) crp->crp_opaque;
124 
125 	if (crypto_ses2caps(crp->crp_session) & CRYPTOCAP_F_SYNC)
126 		return (0);
127 
128 	error = crp->crp_etype;
129 	if (error == EAGAIN)
130 		error = crypto_dispatch(crp);
131 	mtx_lock(&as->as_lock);
132 	if (error || (crp->crp_flags & CRYPTO_F_DONE))
133 		wakeup(crp);
134 	mtx_unlock(&as->as_lock);
135 
136 	return (0);
137 }
138 
139 static void
140 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
141     size_t skip, size_t len, void *ivec, int encdec)
142 {
143 	struct aes_state *as = ks->ks_priv;
144 	struct cryptop *crp;
145 	struct cryptodesc *crd;
146 	int error;
147 
148 	crp = crypto_getreq(1);
149 	crd = crp->crp_desc;
150 
151 	crd->crd_skip = skip;
152 	crd->crd_len = len;
153 	crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT | encdec;
154 	if (ivec) {
155 		bcopy(ivec, crd->crd_iv, 16);
156 	} else {
157 		bzero(crd->crd_iv, 16);
158 	}
159 	crd->crd_next = NULL;
160 	crd->crd_alg = CRYPTO_AES_CBC;
161 
162 	crp->crp_session = as->as_session_aes;
163 	crp->crp_flags = buftype | CRYPTO_F_CBIFSYNC;
164 	crp->crp_buf = buf;
165 	crp->crp_opaque = (void *) as;
166 	crp->crp_callback = aes_crypto_cb;
167 
168 	error = crypto_dispatch(crp);
169 
170 	if ((crypto_ses2caps(as->as_session_aes) & CRYPTOCAP_F_SYNC) == 0) {
171 		mtx_lock(&as->as_lock);
172 		if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
173 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
174 		mtx_unlock(&as->as_lock);
175 	}
176 
177 	crypto_freereq(crp);
178 }
179 
180 static void
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_F_IMBUF, inout, skip, len, ivec,
208 		    CRD_F_ENCRYPT);
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_F_IMBUF, inout, skip, len, ivec,
216 		    CRD_F_ENCRYPT);
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_F_IMBUF, inout, skip, len - plen,
231 		    ivec, CRD_F_ENCRYPT);
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, 0, last2.cn, 0, blocklen, last2.cn_1,
245 		    CRD_F_ENCRYPT);
246 		m_copyback(inout, off, blocklen, last2.cn);
247 		m_copyback(inout, off + blocklen, plen, last2.cn_1);
248 	}
249 }
250 
251 static void
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_F_IMBUF, inout, skip, len, ivec, 0);
278 	} else if (plen == 0) {
279 		/*
280 		 * This is equivalent to CBC mode followed by swapping
281 		 * the last two blocks.
282 		 */
283 		off = skip + len - 2 * blocklen;
284 		m_copydata(inout, off, 2 * blocklen, (void*) &last2);
285 		m_copyback(inout, off, blocklen, last2.cn);
286 		m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
287 		aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec, 0);
288 	} else {
289 		/*
290 		 * This is the difficult case. We first decrypt the
291 		 * second to last block with a zero IV to make X. The
292 		 * plaintext for the last block is the XOR of X and
293 		 * the last cyphertext block.
294 		 *
295 		 * We derive a new cypher text for the second to last
296 		 * block by mixing the unused bytes of X with the last
297 		 * cyphertext block. The result of that can be
298 		 * decrypted with the rest in CBC mode.
299 		 */
300 		off = skip + len - plen - blocklen;
301 		aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, off, blocklen,
302 		    NULL, 0);
303 		m_copydata(inout, off, blocklen + plen, (void*) &last2);
304 
305 		for (i = 0; i < plen; i++) {
306 			t = last2.cn[i];
307 			last2.cn[i] ^= last2.cn_1[i];
308 			last2.cn_1[i] = t;
309 		}
310 
311 		m_copyback(inout, off, blocklen + plen, (void*) &last2);
312 		aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len - plen,
313 		    ivec, 0);
314 	}
315 
316 }
317 
318 static void
319 aes_checksum(const struct krb5_key_state *ks, int usage,
320     struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
321 {
322 	struct aes_state *as = ks->ks_priv;
323 	struct cryptop *crp;
324 	struct cryptodesc *crd;
325 	int error;
326 
327 	crp = crypto_getreq(1);
328 	crd = crp->crp_desc;
329 
330 	crd->crd_skip = skip;
331 	crd->crd_len = inlen;
332 	crd->crd_inject = skip + inlen;
333 	crd->crd_flags = 0;
334 	crd->crd_next = NULL;
335 	crd->crd_alg = CRYPTO_SHA1_HMAC;
336 
337 	crp->crp_session = as->as_session_sha1;
338 	crp->crp_ilen = inlen;
339 	crp->crp_olen = 12;
340 	crp->crp_etype = 0;
341 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
342 	crp->crp_buf = (void *) inout;
343 	crp->crp_opaque = (void *) as;
344 	crp->crp_callback = aes_crypto_cb;
345 
346 	error = crypto_dispatch(crp);
347 
348 	if ((crypto_ses2caps(as->as_session_sha1) & CRYPTOCAP_F_SYNC) == 0) {
349 		mtx_lock(&as->as_lock);
350 		if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
351 			error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
352 		mtx_unlock(&as->as_lock);
353 	}
354 
355 	crypto_freereq(crp);
356 }
357 
358 struct krb5_encryption_class krb5_aes128_encryption_class = {
359 	"aes128-cts-hmac-sha1-96", /* name */
360 	ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
361 	EC_DERIVED_KEYS,	/* flags */
362 	16,			/* blocklen */
363 	1,			/* msgblocklen */
364 	12,			/* checksumlen */
365 	128,			/* keybits */
366 	16,			/* keylen */
367 	aes_init,
368 	aes_destroy,
369 	aes_set_key,
370 	aes_random_to_key,
371 	aes_encrypt,
372 	aes_decrypt,
373 	aes_checksum
374 };
375 
376 struct krb5_encryption_class krb5_aes256_encryption_class = {
377 	"aes256-cts-hmac-sha1-96", /* name */
378 	ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
379 	EC_DERIVED_KEYS,	/* flags */
380 	16,			/* blocklen */
381 	1,			/* msgblocklen */
382 	12,			/* checksumlen */
383 	256,			/* keybits */
384 	32,			/* keylen */
385 	aes_init,
386 	aes_destroy,
387 	aes_set_key,
388 	aes_random_to_key,
389 	aes_encrypt,
390 	aes_decrypt,
391 	aes_checksum
392 };
393