2 * linux/net/sunrpc/gss_krb5_crypto.c
4 * Copyright (c) 2000-2008 The Regents of the University of Michigan.
7 * Andy Adamson <andros@umich.edu>
8 * Bruce Fields <bfields@umich.edu>
12 * Copyright (C) 1998 by the FundsXpress, INC.
14 * All rights reserved.
16 * Export of this software from the United States of America may require
17 * a specific license from the United States Government. It is the
18 * responsibility of any person or organization contemplating export to
19 * obtain such a license before exporting.
21 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
22 * distribute this software and its documentation for any purpose and
23 * without fee is hereby granted, provided that the above copyright
24 * notice appear in all copies and that both that copyright notice and
25 * this permission notice appear in supporting documentation, and that
26 * the name of FundsXpress. not be used in advertising or publicity pertaining
27 * to distribution of the software without specific, written prior
28 * permission. FundsXpress makes no representations about the suitability of
29 * this software for any purpose. It is provided "as is" without express
30 * or implied warranty.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
34 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
37 #include <linux/err.h>
38 #include <linux/types.h>
40 #include <linux/scatterlist.h>
41 #include <linux/crypto.h>
42 #include <linux/highmem.h>
43 #include <linux/pagemap.h>
44 #include <linux/sunrpc/gss_krb5.h>
45 #include <linux/sunrpc/xdr.h>
48 # define RPCDBG_FACILITY RPCDBG_AUTH
53 struct crypto_blkcipher *tfm,
60 struct scatterlist sg[1];
61 u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
62 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
64 if (length % crypto_blkcipher_blocksize(tfm) != 0)
67 if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
68 dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
69 crypto_blkcipher_ivsize(tfm));
74 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
76 memcpy(out, in, length);
77 sg_init_one(sg, out, length);
79 ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
81 dprintk("RPC: krb5_encrypt returns %d\n", ret);
87 struct crypto_blkcipher *tfm,
94 struct scatterlist sg[1];
95 u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
96 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
98 if (length % crypto_blkcipher_blocksize(tfm) != 0)
101 if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
102 dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
103 crypto_blkcipher_ivsize(tfm));
107 memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
109 memcpy(out, in, length);
110 sg_init_one(sg, out, length);
112 ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
114 dprintk("RPC: gss_k5decrypt returns %d\n",ret);
119 checksummer(struct scatterlist *sg, void *data)
121 struct hash_desc *desc = data;
123 return crypto_hash_update(desc, sg, sg->length);
127 * checksum the plaintext data and hdrlen bytes of the token header
128 * The checksum is performed over the first 8 bytes of the
129 * gss token header and then over the data body
132 make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
133 struct xdr_buf *body, int body_offset, u8 *cksumkey,
134 struct xdr_netobj *cksumout)
136 struct hash_desc desc;
137 struct scatterlist sg[1];
139 u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
140 unsigned int checksumlen;
142 if (cksumout->len < kctx->gk5e->cksumlength) {
143 dprintk("%s: checksum buffer length, %u, too small for %s\n",
144 __func__, cksumout->len, kctx->gk5e->name);
145 return GSS_S_FAILURE;
148 desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
149 if (IS_ERR(desc.tfm))
150 return GSS_S_FAILURE;
151 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
153 checksumlen = crypto_hash_digestsize(desc.tfm);
155 if (cksumkey != NULL) {
156 err = crypto_hash_setkey(desc.tfm, cksumkey,
157 kctx->gk5e->keylength);
162 err = crypto_hash_init(&desc);
165 sg_init_one(sg, header, hdrlen);
166 err = crypto_hash_update(&desc, sg, hdrlen);
169 err = xdr_process_buf(body, body_offset, body->len - body_offset,
173 err = crypto_hash_final(&desc, checksumdata);
177 switch (kctx->gk5e->ctype) {
178 case CKSUMTYPE_RSA_MD5:
179 err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
180 checksumdata, checksumlen);
183 memcpy(cksumout->data,
184 checksumdata + checksumlen - kctx->gk5e->cksumlength,
185 kctx->gk5e->cksumlength);
187 case CKSUMTYPE_HMAC_SHA1_DES3:
188 memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
194 cksumout->len = kctx->gk5e->cksumlength;
196 crypto_free_hash(desc.tfm);
197 return err ? GSS_S_FAILURE : 0;
201 * checksum the plaintext data and hdrlen bytes of the token header
202 * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
203 * body then over the first 16 octets of the MIC token
204 * Inclusion of the header data in the calculation of the
205 * checksum is optional.
208 make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen,
209 struct xdr_buf *body, int body_offset, u8 *cksumkey,
210 struct xdr_netobj *cksumout)
212 struct hash_desc desc;
213 struct scatterlist sg[1];
215 u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
216 unsigned int checksumlen;
218 if (kctx->gk5e->keyed_cksum == 0) {
219 dprintk("%s: expected keyed hash for %s\n",
220 __func__, kctx->gk5e->name);
221 return GSS_S_FAILURE;
223 if (cksumkey == NULL) {
224 dprintk("%s: no key supplied for %s\n",
225 __func__, kctx->gk5e->name);
226 return GSS_S_FAILURE;
229 desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0,
231 if (IS_ERR(desc.tfm))
232 return GSS_S_FAILURE;
233 checksumlen = crypto_hash_digestsize(desc.tfm);
234 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
236 err = crypto_hash_setkey(desc.tfm, cksumkey, kctx->gk5e->keylength);
240 err = crypto_hash_init(&desc);
243 err = xdr_process_buf(body, body_offset, body->len - body_offset,
247 if (header != NULL) {
248 sg_init_one(sg, header, hdrlen);
249 err = crypto_hash_update(&desc, sg, hdrlen);
253 err = crypto_hash_final(&desc, checksumdata);
257 cksumout->len = kctx->gk5e->cksumlength;
259 switch (kctx->gk5e->ctype) {
260 case CKSUMTYPE_HMAC_SHA1_96_AES128:
261 case CKSUMTYPE_HMAC_SHA1_96_AES256:
262 /* note that this truncates the hash */
263 memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
270 crypto_free_hash(desc.tfm);
271 return err ? GSS_S_FAILURE : 0;
274 struct encryptor_desc {
275 u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
276 struct blkcipher_desc desc;
278 struct xdr_buf *outbuf;
280 struct scatterlist infrags[4];
281 struct scatterlist outfrags[4];
287 encryptor(struct scatterlist *sg, void *data)
289 struct encryptor_desc *desc = data;
290 struct xdr_buf *outbuf = desc->outbuf;
291 struct page *in_page;
292 int thislen = desc->fraglen + sg->length;
296 /* Worst case is 4 fragments: head, end of page 1, start
297 * of page 2, tail. Anything more is a bug. */
298 BUG_ON(desc->fragno > 3);
300 page_pos = desc->pos - outbuf->head[0].iov_len;
301 if (page_pos >= 0 && page_pos < outbuf->page_len) {
302 /* pages are not in place: */
303 int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
304 in_page = desc->pages[i];
306 in_page = sg_page(sg);
308 sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
310 sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
313 desc->fraglen += sg->length;
314 desc->pos += sg->length;
316 fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
322 sg_mark_end(&desc->infrags[desc->fragno - 1]);
323 sg_mark_end(&desc->outfrags[desc->fragno - 1]);
325 ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
326 desc->infrags, thislen);
330 sg_init_table(desc->infrags, 4);
331 sg_init_table(desc->outfrags, 4);
334 sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
335 sg->offset + sg->length - fraglen);
336 desc->infrags[0] = desc->outfrags[0];
337 sg_assign_page(&desc->infrags[0], in_page);
339 desc->fraglen = fraglen;
348 gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
349 int offset, struct page **pages)
352 struct encryptor_desc desc;
354 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
356 memset(desc.iv, 0, sizeof(desc.iv));
358 desc.desc.info = desc.iv;
366 sg_init_table(desc.infrags, 4);
367 sg_init_table(desc.outfrags, 4);
369 ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
373 struct decryptor_desc {
374 u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
375 struct blkcipher_desc desc;
376 struct scatterlist frags[4];
382 decryptor(struct scatterlist *sg, void *data)
384 struct decryptor_desc *desc = data;
385 int thislen = desc->fraglen + sg->length;
388 /* Worst case is 4 fragments: head, end of page 1, start
389 * of page 2, tail. Anything more is a bug. */
390 BUG_ON(desc->fragno > 3);
391 sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
394 desc->fraglen += sg->length;
396 fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
402 sg_mark_end(&desc->frags[desc->fragno - 1]);
404 ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
405 desc->frags, thislen);
409 sg_init_table(desc->frags, 4);
412 sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
413 sg->offset + sg->length - fraglen);
415 desc->fraglen = fraglen;
424 gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
427 struct decryptor_desc desc;
430 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
432 memset(desc.iv, 0, sizeof(desc.iv));
434 desc.desc.info = desc.iv;
439 sg_init_table(desc.frags, 4);
441 return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
445 * This function makes the assumption that it was ultimately called
448 * The client auth_gss code moves any existing tail data into a
449 * separate page before calling gss_wrap.
450 * The server svcauth_gss code ensures that both the head and the
451 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
453 * Even with that guarantee, this function may be called more than
454 * once in the processing of gss_wrap(). The best we can do is
455 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
456 * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
457 * At run-time we can verify that a single invocation of this
458 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
462 xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
469 BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
470 BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
472 p = buf->head[0].iov_base + base;
474 memmove(p + shiftlen, p, buf->head[0].iov_len - base);
476 buf->head[0].iov_len += shiftlen;
477 buf->len += shiftlen;