2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
37 #include <opencrypto/cryptodev.h>
39 #include <kgssapi/gssapi.h>
40 #include <kgssapi/gssapi_impl.h>
46 uint64_t as_session_aes;
47 uint64_t as_session_sha1;
51 aes_init(struct krb5_key_state *ks)
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);
61 aes_destroy(struct krb5_key_state *ks)
63 struct aes_state *as = ks->ks_priv;
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);
74 aes_set_key(struct krb5_key_state *ks, const void *in)
76 void *kp = ks->ks_key;
77 struct aes_state *as = ks->ks_priv;
81 bcopy(in, kp, ks->ks_class->ec_keylen);
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);
89 * We only want the first 96 bits of the HMAC.
91 bzero(&cri, sizeof(cri));
92 cri.cri_alg = CRYPTO_SHA1_HMAC;
93 cri.cri_klen = ks->ks_class->ec_keybits;
95 cri.cri_key = ks->ks_key;
97 crypto_newsession(&as->as_session_sha1, &cri,
98 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
100 bzero(&cri, sizeof(cri));
101 cri.cri_alg = CRYPTO_AES_CBC;
102 cri.cri_klen = ks->ks_class->ec_keybits;
104 cri.cri_key = ks->ks_key;
106 crypto_newsession(&as->as_session_aes, &cri,
107 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
111 aes_random_to_key(struct krb5_key_state *ks, const void *in)
118 aes_crypto_cb(struct cryptop *crp)
121 struct aes_state *as = (struct aes_state *) crp->crp_opaque;
123 if (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC)
126 error = crp->crp_etype;
128 error = crypto_dispatch(crp);
129 mtx_lock(&as->as_lock);
130 if (error || (crp->crp_flags & CRYPTO_F_DONE))
132 mtx_unlock(&as->as_lock);
138 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
139 size_t skip, size_t len, void *ivec, int encdec)
141 struct aes_state *as = ks->ks_priv;
143 struct cryptodesc *crd;
146 crp = crypto_getreq(1);
149 crd->crd_skip = skip;
151 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT | encdec;
153 bcopy(ivec, crd->crd_iv, 16);
155 bzero(crd->crd_iv, 16);
157 crd->crd_next = NULL;
158 crd->crd_alg = CRYPTO_AES_CBC;
160 crp->crp_sid = as->as_session_aes;
161 crp->crp_flags = buftype | CRYPTO_F_CBIFSYNC;
163 crp->crp_opaque = (void *) as;
164 crp->crp_callback = aes_crypto_cb;
166 error = crypto_dispatch(crp);
168 if ((CRYPTO_SESID2CAPS(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_F_IMBUF, 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_F_IMBUF, 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_F_IMBUF, inout, skip, len - plen,
229 ivec, CRD_F_ENCRYPT);
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, 0, last2.cn, 0, blocklen, last2.cn_1,
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_F_IMBUF, inout, skip, len, ivec, 0);
276 } else if (plen == 0) {
278 * This is equivalent to CBC mode followed by swapping
279 * the last two blocks.
281 off = skip + len - 2 * blocklen;
282 m_copydata(inout, off, 2 * blocklen, (void*) &last2);
283 m_copyback(inout, off, blocklen, last2.cn);
284 m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
285 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec, 0);
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.
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.
298 off = skip + len - plen - blocklen;
299 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, off, blocklen,
301 m_copydata(inout, off, blocklen + plen, (void*) &last2);
303 for (i = 0; i < plen; i++) {
305 last2.cn[i] ^= last2.cn_1[i];
309 m_copyback(inout, off, blocklen + plen, (void*) &last2);
310 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len - plen,
317 aes_checksum(const struct krb5_key_state *ks, int usage,
318 struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
320 struct aes_state *as = ks->ks_priv;
322 struct cryptodesc *crd;
325 crp = crypto_getreq(1);
328 crd->crd_skip = skip;
329 crd->crd_len = inlen;
330 crd->crd_inject = skip + inlen;
332 crd->crd_next = NULL;
333 crd->crd_alg = CRYPTO_SHA1_HMAC;
335 crp->crp_sid = as->as_session_sha1;
336 crp->crp_ilen = inlen;
339 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
340 crp->crp_buf = (void *) inout;
341 crp->crp_opaque = (void *) as;
342 crp->crp_callback = aes_crypto_cb;
344 error = crypto_dispatch(crp);
346 if ((CRYPTO_SESID2CAPS(as->as_session_sha1) & CRYPTOCAP_F_SYNC) == 0) {
347 mtx_lock(&as->as_lock);
348 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
349 error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
350 mtx_unlock(&as->as_lock);
356 struct krb5_encryption_class krb5_aes128_encryption_class = {
357 "aes128-cts-hmac-sha1-96", /* name */
358 ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
359 EC_DERIVED_KEYS, /* flags */
362 12, /* checksumlen */
374 struct krb5_encryption_class krb5_aes256_encryption_class = {
375 "aes256-cts-hmac-sha1-96", /* name */
376 ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
377 EC_DERIVED_KEYS, /* flags */
380 12, /* checksumlen */