2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
39 #include <opencrypto/cryptodev.h>
41 #include <kgssapi/gssapi.h>
42 #include <kgssapi/gssapi_impl.h>
48 crypto_session_t as_session_aes;
49 crypto_session_t as_session_sha1;
53 aes_init(struct krb5_key_state *ks)
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);
63 aes_destroy(struct krb5_key_state *ks)
65 struct aes_state *as = ks->ks_priv;
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);
76 aes_set_key(struct krb5_key_state *ks, const void *in)
78 void *kp = ks->ks_key;
79 struct aes_state *as = ks->ks_priv;
80 struct crypto_session_params csp;
83 bcopy(in, kp, ks->ks_class->ec_keylen);
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);
91 * We only want the first 96 bits of the HMAC.
93 memset(&csp, 0, sizeof(csp));
94 csp.csp_mode = CSP_MODE_DIGEST;
95 csp.csp_auth_alg = CRYPTO_SHA1_HMAC;
96 csp.csp_auth_klen = ks->ks_class->ec_keybits / 8;
97 csp.csp_auth_mlen = 12;
98 csp.csp_auth_key = ks->ks_key;
99 crypto_newsession(&as->as_session_sha1, &csp,
100 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
102 memset(&csp, 0, sizeof(csp));
103 csp.csp_mode = CSP_MODE_CIPHER;
104 csp.csp_cipher_alg = CRYPTO_AES_CBC;
105 csp.csp_cipher_klen = ks->ks_class->ec_keybits / 8;
106 csp.csp_cipher_key = ks->ks_key;
108 crypto_newsession(&as->as_session_aes, &csp,
109 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
113 aes_random_to_key(struct krb5_key_state *ks, const void *in)
120 aes_crypto_cb(struct cryptop *crp)
123 struct aes_state *as = (struct aes_state *) crp->crp_opaque;
125 if (crypto_ses2caps(crp->crp_session) & CRYPTOCAP_F_SYNC)
128 error = crp->crp_etype;
130 error = crypto_dispatch(crp);
131 mtx_lock(&as->as_lock);
132 if (error || (crp->crp_flags & CRYPTO_F_DONE))
134 mtx_unlock(&as->as_lock);
140 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
141 size_t skip, size_t len, void *ivec, bool encrypt)
143 struct aes_state *as = ks->ks_priv;
147 crp = crypto_getreq(as->as_session_aes, M_WAITOK);
149 crp->crp_payload_start = skip;
150 crp->crp_payload_length = len;
151 crp->crp_op = encrypt ? CRYPTO_OP_ENCRYPT : CRYPTO_OP_DECRYPT;
152 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_IV_SEPARATE;
154 memcpy(crp->crp_iv, ivec, 16);
156 memset(crp->crp_iv, 0, 16);
159 if (buftype == CRYPTO_BUF_MBUF)
160 crypto_use_mbuf(crp, buf);
162 crypto_use_buf(crp, buf, skip + len);
163 crp->crp_opaque = as;
164 crp->crp_callback = aes_crypto_cb;
166 error = crypto_dispatch(crp);
168 if ((crypto_ses2caps(as->as_session_aes) & CRYPTOCAP_F_SYNC) == 0) {
169 mtx_lock(&as->as_lock);
170 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
171 error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
172 mtx_unlock(&as->as_lock);
179 aes_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
180 size_t skip, size_t len, void *ivec, size_t ivlen)
182 size_t blocklen = 16, plen;
184 uint8_t cn_1[16], cn[16];
189 * AES encryption with cyphertext stealing:
191 * CTSencrypt(P[0], ..., P[n], IV, K):
193 * (C[0], ..., C[n-2], E[n-1]) =
194 * CBCencrypt(P[0], ..., P[n-1], IV, K)
195 * P = pad(P[n], 0, blocksize)
196 * E[n] = CBCencrypt(P, E[n-1], K);
198 * C[n] = E[n-1]{0..len-1}
200 plen = len % blocklen;
201 if (len == blocklen) {
203 * Note: caller will ensure len >= blocklen.
205 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
207 } else if (plen == 0) {
209 * This is equivalent to CBC mode followed by swapping
210 * the last two blocks. We assume that neither of the
211 * last two blocks cross iov boundaries.
213 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
215 off = skip + len - 2 * blocklen;
216 m_copydata(inout, off, 2 * blocklen, (void*) &last2);
217 m_copyback(inout, off, blocklen, last2.cn);
218 m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
221 * This is the difficult case. We encrypt all but the
222 * last partial block first. We then create a padded
223 * copy of the last block and encrypt that using the
224 * second to last encrypted block as IV. Once we have
225 * the encrypted versions of the last two blocks, we
226 * reshuffle to create the final result.
228 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
232 * Copy out the last two blocks, pad the last block
233 * and encrypt it. Rearrange to get the final
234 * result. The cyphertext for cn_1 is in cn. The
235 * cyphertext for cn is the first plen bytes of what
238 off = skip + len - blocklen - plen;
239 m_copydata(inout, off, blocklen + plen, (void*) &last2);
240 for (i = plen; i < blocklen; i++)
242 aes_encrypt_1(ks, CRYPTO_BUF_CONTIG, last2.cn, 0, blocklen,
244 m_copyback(inout, off, blocklen, last2.cn);
245 m_copyback(inout, off + blocklen, plen, last2.cn_1);
250 aes_decrypt(const struct krb5_key_state *ks, struct mbuf *inout,
251 size_t skip, size_t len, void *ivec, size_t ivlen)
253 size_t blocklen = 16, plen;
255 uint8_t cn_1[16], cn[16];
260 * AES decryption with cyphertext stealing:
262 * CTSencrypt(C[0], ..., C[n], IV, K):
265 * X = decrypt(E[n], K)
266 * P[n] = (X ^ C[n]){0..len-1}
267 * E[n-1] = {C[n,0],...,C[n,len-1],X[len],...,X[blocksize-1]}
268 * (P[0],...,P[n-1]) = CBCdecrypt(C[0],...,C[n-2],E[n-1], IV, K)
270 plen = len % blocklen;
271 if (len == blocklen) {
273 * Note: caller will ensure len >= blocklen.
275 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
277 } else if (plen == 0) {
279 * This is equivalent to CBC mode followed by swapping
280 * the last two blocks.
282 off = skip + len - 2 * blocklen;
283 m_copydata(inout, off, 2 * blocklen, (void*) &last2);
284 m_copyback(inout, off, blocklen, last2.cn);
285 m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
286 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len, ivec,
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.
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.
300 off = skip + len - plen - blocklen;
301 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, off, blocklen,
303 m_copydata(inout, off, blocklen + plen, (void*) &last2);
305 for (i = 0; i < plen; i++) {
307 last2.cn[i] ^= last2.cn_1[i];
311 m_copyback(inout, off, blocklen + plen, (void*) &last2);
312 aes_encrypt_1(ks, CRYPTO_BUF_MBUF, inout, skip, len - plen,
319 aes_checksum(const struct krb5_key_state *ks, int usage,
320 struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
322 struct aes_state *as = ks->ks_priv;
326 crp = crypto_getreq(as->as_session_sha1, M_WAITOK);
328 crp->crp_payload_start = skip;
329 crp->crp_payload_length = inlen;
330 crp->crp_digest_start = skip + inlen;
331 crp->crp_flags = CRYPTO_F_CBIFSYNC;
332 crypto_use_mbuf(crp, inout);
333 crp->crp_opaque = as;
334 crp->crp_callback = aes_crypto_cb;
336 error = crypto_dispatch(crp);
338 if ((crypto_ses2caps(as->as_session_sha1) & CRYPTOCAP_F_SYNC) == 0) {
339 mtx_lock(&as->as_lock);
340 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
341 error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
342 mtx_unlock(&as->as_lock);
348 struct krb5_encryption_class krb5_aes128_encryption_class = {
349 "aes128-cts-hmac-sha1-96", /* name */
350 ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
351 EC_DERIVED_KEYS, /* flags */
354 12, /* checksumlen */
366 struct krb5_encryption_class krb5_aes256_encryption_class = {
367 "aes256-cts-hmac-sha1-96", /* name */
368 ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
369 EC_DERIVED_KEYS, /* flags */
372 12, /* checksumlen */