2 * linux/net/sunrpc/gss_krb5_crypto.c
4 * Copyright (c) 2000 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/types.h>
39 #include <linux/slab.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>
47 # define RPCDBG_FACILITY RPCDBG_AUTH
52 struct crypto_blkcipher *tfm,
59 struct scatterlist sg[1];
60 u8 local_iv[16] = {0};
61 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
63 dprintk("RPC: krb5_encrypt: input data:\n");
64 print_hexl((u32 *)in, length, 0);
66 if (length % crypto_blkcipher_blocksize(tfm) != 0)
69 if (crypto_blkcipher_ivsize(tfm) > 16) {
70 dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
71 crypto_blkcipher_ivsize(tfm));
76 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
78 memcpy(out, in, length);
79 sg_set_buf(sg, out, length);
81 ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
83 dprintk("RPC: krb5_encrypt: output data:\n");
84 print_hexl((u32 *)out, length, 0);
86 dprintk("RPC: krb5_encrypt returns %d\n",ret);
90 EXPORT_SYMBOL(krb5_encrypt);
94 struct crypto_blkcipher *tfm,
101 struct scatterlist sg[1];
102 u8 local_iv[16] = {0};
103 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
105 dprintk("RPC: krb5_decrypt: input data:\n");
106 print_hexl((u32 *)in, length, 0);
108 if (length % crypto_blkcipher_blocksize(tfm) != 0)
111 if (crypto_blkcipher_ivsize(tfm) > 16) {
112 dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
113 crypto_blkcipher_ivsize(tfm));
117 memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
119 memcpy(out, in, length);
120 sg_set_buf(sg, out, length);
122 ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
124 dprintk("RPC: krb5_decrypt: output_data:\n");
125 print_hexl((u32 *)out, length, 0);
127 dprintk("RPC: gss_k5decrypt returns %d\n",ret);
131 EXPORT_SYMBOL(krb5_decrypt);
134 process_xdr_buf(struct xdr_buf *buf, int offset, int len,
135 int (*actor)(struct scatterlist *, void *), void *data)
137 int i, page_len, thislen, page_offset, ret = 0;
138 struct scatterlist sg[1];
140 if (offset >= buf->head[0].iov_len) {
141 offset -= buf->head[0].iov_len;
143 thislen = buf->head[0].iov_len - offset;
146 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
147 ret = actor(sg, data);
156 if (offset >= buf->page_len) {
157 offset -= buf->page_len;
159 page_len = buf->page_len - offset;
163 page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
164 i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
165 thislen = PAGE_CACHE_SIZE - page_offset;
167 if (thislen > page_len)
169 sg->page = buf->pages[i];
170 sg->offset = page_offset;
171 sg->length = thislen;
172 ret = actor(sg, data);
178 thislen = PAGE_CACHE_SIZE;
179 } while (page_len != 0);
185 if (offset < buf->tail[0].iov_len) {
186 thislen = buf->tail[0].iov_len - offset;
189 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
190 ret = actor(sg, data);
200 checksummer(struct scatterlist *sg, void *data)
202 struct crypto_tfm *tfm = (struct crypto_tfm *)data;
204 crypto_digest_update(tfm, sg, 1);
209 /* checksum the plaintext data and hdrlen bytes of the token header */
211 make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body,
212 int body_offset, struct xdr_netobj *cksum)
215 struct crypto_tfm *tfm = NULL; /* XXX add to ctx? */
216 struct scatterlist sg[1];
219 case CKSUMTYPE_RSA_MD5:
223 dprintk("RPC: krb5_make_checksum:"
224 " unsupported checksum %d", cksumtype);
225 return GSS_S_FAILURE;
227 if (!(tfm = crypto_alloc_tfm(cksumname, CRYPTO_TFM_REQ_MAY_SLEEP)))
228 return GSS_S_FAILURE;
229 cksum->len = crypto_tfm_alg_digestsize(tfm);
231 crypto_digest_init(tfm);
232 sg_set_buf(sg, header, hdrlen);
233 crypto_digest_update(tfm, sg, 1);
234 process_xdr_buf(body, body_offset, body->len - body_offset,
236 crypto_digest_final(tfm, cksum->data);
237 crypto_free_tfm(tfm);
241 EXPORT_SYMBOL(make_checksum);
243 struct encryptor_desc {
244 u8 iv[8]; /* XXX hard-coded blocksize */
245 struct blkcipher_desc desc;
247 struct xdr_buf *outbuf;
249 struct scatterlist infrags[4];
250 struct scatterlist outfrags[4];
256 encryptor(struct scatterlist *sg, void *data)
258 struct encryptor_desc *desc = data;
259 struct xdr_buf *outbuf = desc->outbuf;
260 struct page *in_page;
261 int thislen = desc->fraglen + sg->length;
265 /* Worst case is 4 fragments: head, end of page 1, start
266 * of page 2, tail. Anything more is a bug. */
267 BUG_ON(desc->fragno > 3);
268 desc->infrags[desc->fragno] = *sg;
269 desc->outfrags[desc->fragno] = *sg;
271 page_pos = desc->pos - outbuf->head[0].iov_len;
272 if (page_pos >= 0 && page_pos < outbuf->page_len) {
273 /* pages are not in place: */
274 int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
275 in_page = desc->pages[i];
279 desc->infrags[desc->fragno].page = in_page;
281 desc->fraglen += sg->length;
282 desc->pos += sg->length;
284 fraglen = thislen & 7; /* XXX hardcoded blocksize */
290 ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
291 desc->infrags, thislen);
295 desc->outfrags[0].page = sg->page;
296 desc->outfrags[0].offset = sg->offset + sg->length - fraglen;
297 desc->outfrags[0].length = fraglen;
298 desc->infrags[0] = desc->outfrags[0];
299 desc->infrags[0].page = in_page;
301 desc->fraglen = fraglen;
310 gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
311 int offset, struct page **pages)
314 struct encryptor_desc desc;
316 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
318 memset(desc.iv, 0, sizeof(desc.iv));
320 desc.desc.info = desc.iv;
328 ret = process_xdr_buf(buf, offset, buf->len - offset, encryptor, &desc);
332 EXPORT_SYMBOL(gss_encrypt_xdr_buf);
334 struct decryptor_desc {
335 u8 iv[8]; /* XXX hard-coded blocksize */
336 struct blkcipher_desc desc;
337 struct scatterlist frags[4];
343 decryptor(struct scatterlist *sg, void *data)
345 struct decryptor_desc *desc = data;
346 int thislen = desc->fraglen + sg->length;
349 /* Worst case is 4 fragments: head, end of page 1, start
350 * of page 2, tail. Anything more is a bug. */
351 BUG_ON(desc->fragno > 3);
352 desc->frags[desc->fragno] = *sg;
354 desc->fraglen += sg->length;
356 fraglen = thislen & 7; /* XXX hardcoded blocksize */
362 ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
363 desc->frags, thislen);
367 desc->frags[0].page = sg->page;
368 desc->frags[0].offset = sg->offset + sg->length - fraglen;
369 desc->frags[0].length = fraglen;
371 desc->fraglen = fraglen;
380 gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
383 struct decryptor_desc desc;
386 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
388 memset(desc.iv, 0, sizeof(desc.iv));
390 desc.desc.info = desc.iv;
394 return process_xdr_buf(buf, offset, buf->len - offset, decryptor, &desc);
397 EXPORT_SYMBOL(gss_decrypt_xdr_buf);