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>
50 aes_init(struct krb5_key_state *ks)
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);
60 aes_destroy(struct krb5_key_state *ks)
62 struct aes_state *as = ks->ks_priv;
65 crypto_freesession(as->as_session);
66 mtx_destroy(&as->as_lock);
67 free(ks->ks_priv, M_GSSAPI);
71 aes_set_key(struct krb5_key_state *ks, const void *in)
73 void *kp = ks->ks_key;
74 struct aes_state *as = ks->ks_priv;
75 struct cryptoini cri[2];
78 bcopy(in, kp, ks->ks_class->ec_keylen);
81 crypto_freesession(as->as_session);
83 bzero(cri, sizeof(cri));
86 * We only want the first 96 bits of the HMAC.
88 cri[0].cri_alg = CRYPTO_SHA1_HMAC;
89 cri[0].cri_klen = ks->ks_class->ec_keybits;
91 cri[0].cri_key = ks->ks_key;
92 cri[0].cri_next = &cri[1];
94 cri[1].cri_alg = CRYPTO_AES_CBC;
95 cri[1].cri_klen = ks->ks_class->ec_keybits;
97 cri[1].cri_key = ks->ks_key;
98 cri[1].cri_next = NULL;
100 crypto_newsession(&as->as_session, cri,
101 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
105 aes_random_to_key(struct krb5_key_state *ks, const void *in)
112 aes_crypto_cb(struct cryptop *crp)
115 struct aes_state *as = (struct aes_state *) crp->crp_opaque;
117 if (CRYPTO_SESID2CAPS(as->as_session) & CRYPTOCAP_F_SYNC)
120 error = crp->crp_etype;
122 error = crypto_dispatch(crp);
123 mtx_lock(&as->as_lock);
124 if (error || (crp->crp_flags & CRYPTO_F_DONE))
126 mtx_unlock(&as->as_lock);
132 aes_encrypt_1(const struct krb5_key_state *ks, int buftype, void *buf,
133 size_t skip, size_t len, void *ivec, int encdec)
135 struct aes_state *as = ks->ks_priv;
137 struct cryptodesc *crd;
140 crp = crypto_getreq(1);
143 crd->crd_skip = skip;
145 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT | encdec;
147 bcopy(ivec, crd->crd_iv, 16);
149 bzero(crd->crd_iv, 16);
151 crd->crd_next = NULL;
152 crd->crd_alg = CRYPTO_AES_CBC;
154 crp->crp_sid = as->as_session;
155 crp->crp_flags = buftype | CRYPTO_F_CBIFSYNC;
157 crp->crp_opaque = (void *) as;
158 crp->crp_callback = aes_crypto_cb;
160 error = crypto_dispatch(crp);
162 if ((CRYPTO_SESID2CAPS(as->as_session) & CRYPTOCAP_F_SYNC) == 0) {
163 mtx_lock(&as->as_lock);
164 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
165 error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
166 mtx_unlock(&as->as_lock);
173 aes_encrypt(const struct krb5_key_state *ks, struct mbuf *inout,
174 size_t skip, size_t len, void *ivec, size_t ivlen)
176 size_t blocklen = 16, plen;
178 uint8_t cn_1[16], cn[16];
183 * AES encryption with cyphertext stealing:
185 * CTSencrypt(P[0], ..., P[n], IV, K):
187 * (C[0], ..., C[n-2], E[n-1]) =
188 * CBCencrypt(P[0], ..., P[n-1], IV, K)
189 * P = pad(P[n], 0, blocksize)
190 * E[n] = CBCencrypt(P, E[n-1], K);
192 * C[n] = E[n-1]{0..len-1}
194 plen = len % blocklen;
195 if (len == blocklen) {
197 * Note: caller will ensure len >= blocklen.
199 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec,
201 } else if (plen == 0) {
203 * This is equivalent to CBC mode followed by swapping
204 * the last two blocks. We assume that neither of the
205 * last two blocks cross iov boundaries.
207 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec,
209 off = skip + len - 2 * blocklen;
210 m_copydata(inout, off, 2 * blocklen, (void*) &last2);
211 m_copyback(inout, off, blocklen, last2.cn);
212 m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
215 * This is the difficult case. We encrypt all but the
216 * last partial block first. We then create a padded
217 * copy of the last block and encrypt that using the
218 * second to last encrypted block as IV. Once we have
219 * the encrypted versions of the last two blocks, we
220 * reshuffle to create the final result.
222 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len - plen,
223 ivec, CRD_F_ENCRYPT);
226 * Copy out the last two blocks, pad the last block
227 * and encrypt it. Rearrange to get the final
228 * result. The cyphertext for cn_1 is in cn. The
229 * cyphertext for cn is the first plen bytes of what
232 off = skip + len - blocklen - plen;
233 m_copydata(inout, off, blocklen + plen, (void*) &last2);
234 for (i = plen; i < blocklen; i++)
236 aes_encrypt_1(ks, 0, last2.cn, 0, blocklen, last2.cn_1,
238 m_copyback(inout, off, blocklen, last2.cn);
239 m_copyback(inout, off + blocklen, plen, last2.cn_1);
244 aes_decrypt(const struct krb5_key_state *ks, struct mbuf *inout,
245 size_t skip, size_t len, void *ivec, size_t ivlen)
247 size_t blocklen = 16, plen;
249 uint8_t cn_1[16], cn[16];
254 * AES decryption with cyphertext stealing:
256 * CTSencrypt(C[0], ..., C[n], IV, K):
259 * X = decrypt(E[n], K)
260 * P[n] = (X ^ C[n]){0..len-1}
261 * E[n-1] = {C[n,0],...,C[n,len-1],X[len],...,X[blocksize-1]}
262 * (P[0],...,P[n-1]) = CBCdecrypt(C[0],...,C[n-2],E[n-1], IV, K)
264 plen = len % blocklen;
265 if (len == blocklen) {
267 * Note: caller will ensure len >= blocklen.
269 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec, 0);
270 } else if (plen == 0) {
272 * This is equivalent to CBC mode followed by swapping
273 * the last two blocks.
275 off = skip + len - 2 * blocklen;
276 m_copydata(inout, off, 2 * blocklen, (void*) &last2);
277 m_copyback(inout, off, blocklen, last2.cn);
278 m_copyback(inout, off + blocklen, blocklen, last2.cn_1);
279 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len, ivec, 0);
282 * This is the difficult case. We first decrypt the
283 * second to last block with a zero IV to make X. The
284 * plaintext for the last block is the XOR of X and
285 * the last cyphertext block.
287 * We derive a new cypher text for the second to last
288 * block by mixing the unused bytes of X with the last
289 * cyphertext block. The result of that can be
290 * decrypted with the rest in CBC mode.
292 off = skip + len - plen - blocklen;
293 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, off, blocklen,
295 m_copydata(inout, off, blocklen + plen, (void*) &last2);
297 for (i = 0; i < plen; i++) {
299 last2.cn[i] ^= last2.cn_1[i];
303 m_copyback(inout, off, blocklen + plen, (void*) &last2);
304 aes_encrypt_1(ks, CRYPTO_F_IMBUF, inout, skip, len - plen,
311 aes_checksum(const struct krb5_key_state *ks, int usage,
312 struct mbuf *inout, size_t skip, size_t inlen, size_t outlen)
314 struct aes_state *as = ks->ks_priv;
316 struct cryptodesc *crd;
319 crp = crypto_getreq(1);
322 crd->crd_skip = skip;
323 crd->crd_len = inlen;
324 crd->crd_inject = skip + inlen;
326 crd->crd_next = NULL;
327 crd->crd_alg = CRYPTO_SHA1_HMAC;
329 crp->crp_sid = as->as_session;
330 crp->crp_ilen = inlen;
333 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
334 crp->crp_buf = (void *) inout;
335 crp->crp_opaque = (void *) as;
336 crp->crp_callback = aes_crypto_cb;
338 error = crypto_dispatch(crp);
340 if ((CRYPTO_SESID2CAPS(as->as_session) & CRYPTOCAP_F_SYNC) == 0) {
341 mtx_lock(&as->as_lock);
342 if (!error && !(crp->crp_flags & CRYPTO_F_DONE))
343 error = msleep(crp, &as->as_lock, 0, "gssaes", 0);
344 mtx_unlock(&as->as_lock);
350 struct krb5_encryption_class krb5_aes128_encryption_class = {
351 "aes128-cts-hmac-sha1-96", /* name */
352 ETYPE_AES128_CTS_HMAC_SHA1_96, /* etype */
353 EC_DERIVED_KEYS, /* flags */
356 12, /* checksumlen */
368 struct krb5_encryption_class krb5_aes256_encryption_class = {
369 "aes256-cts-hmac-sha1-96", /* name */
370 ETYPE_AES256_CTS_HMAC_SHA1_96, /* etype */
371 EC_DERIVED_KEYS, /* flags */
374 12, /* checksumlen */