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_tfm *tfm,
59 struct scatterlist sg[1];
60 u8 local_iv[16] = {0};
62 dprintk("RPC: krb5_encrypt: input data:\n");
63 print_hexl((u32 *)in, length, 0);
65 if (length % crypto_tfm_alg_blocksize(tfm) != 0)
68 if (crypto_tfm_alg_ivsize(tfm) > 16) {
69 dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
70 crypto_tfm_alg_ivsize(tfm));
75 memcpy(local_iv, iv, crypto_tfm_alg_ivsize(tfm));
77 memcpy(out, in, length);
78 sg_set_buf(sg, out, length);
80 ret = crypto_cipher_encrypt_iv(tfm, sg, sg, length, local_iv);
82 dprintk("RPC: krb5_encrypt: output data:\n");
83 print_hexl((u32 *)out, length, 0);
85 dprintk("RPC: krb5_encrypt returns %d\n",ret);
89 EXPORT_SYMBOL(krb5_encrypt);
93 struct crypto_tfm *tfm,
100 struct scatterlist sg[1];
101 u8 local_iv[16] = {0};
103 dprintk("RPC: krb5_decrypt: input data:\n");
104 print_hexl((u32 *)in, length, 0);
106 if (length % crypto_tfm_alg_blocksize(tfm) != 0)
109 if (crypto_tfm_alg_ivsize(tfm) > 16) {
110 dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
111 crypto_tfm_alg_ivsize(tfm));
115 memcpy(local_iv,iv, crypto_tfm_alg_ivsize(tfm));
117 memcpy(out, in, length);
118 sg_set_buf(sg, out, length);
120 ret = crypto_cipher_decrypt_iv(tfm, sg, sg, length, local_iv);
122 dprintk("RPC: krb5_decrypt: output_data:\n");
123 print_hexl((u32 *)out, length, 0);
125 dprintk("RPC: gss_k5decrypt returns %d\n",ret);
129 EXPORT_SYMBOL(krb5_decrypt);
132 process_xdr_buf(struct xdr_buf *buf, int offset, int len,
133 int (*actor)(struct scatterlist *, void *), void *data)
135 int i, page_len, thislen, page_offset, ret = 0;
136 struct scatterlist sg[1];
138 if (offset >= buf->head[0].iov_len) {
139 offset -= buf->head[0].iov_len;
141 thislen = buf->head[0].iov_len - offset;
144 sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
145 ret = actor(sg, data);
154 if (offset >= buf->page_len) {
155 offset -= buf->page_len;
157 page_len = buf->page_len - offset;
161 page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
162 i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
163 thislen = PAGE_CACHE_SIZE - page_offset;
165 if (thislen > page_len)
167 sg->page = buf->pages[i];
168 sg->offset = page_offset;
169 sg->length = thislen;
170 ret = actor(sg, data);
176 thislen = PAGE_CACHE_SIZE;
177 } while (page_len != 0);
183 if (offset < buf->tail[0].iov_len) {
184 thislen = buf->tail[0].iov_len - offset;
187 sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
188 ret = actor(sg, data);
198 checksummer(struct scatterlist *sg, void *data)
200 struct crypto_tfm *tfm = (struct crypto_tfm *)data;
202 crypto_digest_update(tfm, sg, 1);
207 /* checksum the plaintext data and hdrlen bytes of the token header */
209 make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body,
210 int body_offset, struct xdr_netobj *cksum)
213 struct crypto_tfm *tfm = NULL; /* XXX add to ctx? */
214 struct scatterlist sg[1];
215 u32 code = GSS_S_FAILURE;
218 case CKSUMTYPE_RSA_MD5:
222 dprintk("RPC: krb5_make_checksum:"
223 " unsupported checksum %d", cksumtype);
226 if (!(tfm = crypto_alloc_tfm(cksumname, CRYPTO_TFM_REQ_MAY_SLEEP)))
228 cksum->len = crypto_tfm_alg_digestsize(tfm);
229 if ((cksum->data = kmalloc(cksum->len, GFP_KERNEL)) == NULL)
232 crypto_digest_init(tfm);
233 sg_set_buf(sg, header, hdrlen);
234 crypto_digest_update(tfm, sg, 1);
235 process_xdr_buf(body, body_offset, body->len - body_offset,
237 crypto_digest_final(tfm, cksum->data);
240 crypto_free_tfm(tfm);
244 EXPORT_SYMBOL(make_checksum);
246 struct encryptor_desc {
247 u8 iv[8]; /* XXX hard-coded blocksize */
248 struct crypto_tfm *tfm;
250 struct xdr_buf *outbuf;
252 struct scatterlist infrags[4];
253 struct scatterlist outfrags[4];
259 encryptor(struct scatterlist *sg, void *data)
261 struct encryptor_desc *desc = data;
262 struct xdr_buf *outbuf = desc->outbuf;
263 struct page *in_page;
264 int thislen = desc->fraglen + sg->length;
268 /* Worst case is 4 fragments: head, end of page 1, start
269 * of page 2, tail. Anything more is a bug. */
270 BUG_ON(desc->fragno > 3);
271 desc->infrags[desc->fragno] = *sg;
272 desc->outfrags[desc->fragno] = *sg;
274 page_pos = desc->pos - outbuf->head[0].iov_len;
275 if (page_pos >= 0 && page_pos < outbuf->page_len) {
276 /* pages are not in place: */
277 int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
278 in_page = desc->pages[i];
282 desc->infrags[desc->fragno].page = in_page;
284 desc->fraglen += sg->length;
285 desc->pos += sg->length;
287 fraglen = thislen & 7; /* XXX hardcoded blocksize */
293 ret = crypto_cipher_encrypt_iv(desc->tfm, desc->outfrags, desc->infrags,
298 desc->outfrags[0].page = sg->page;
299 desc->outfrags[0].offset = sg->offset + sg->length - fraglen;
300 desc->outfrags[0].length = fraglen;
301 desc->infrags[0] = desc->outfrags[0];
302 desc->infrags[0].page = in_page;
304 desc->fraglen = fraglen;
313 gss_encrypt_xdr_buf(struct crypto_tfm *tfm, struct xdr_buf *buf, int offset,
317 struct encryptor_desc desc;
319 BUG_ON((buf->len - offset) % crypto_tfm_alg_blocksize(tfm) != 0);
321 memset(desc.iv, 0, sizeof(desc.iv));
329 ret = process_xdr_buf(buf, offset, buf->len - offset, encryptor, &desc);
333 EXPORT_SYMBOL(gss_encrypt_xdr_buf);
335 struct decryptor_desc {
336 u8 iv[8]; /* XXX hard-coded blocksize */
337 struct crypto_tfm *tfm;
338 struct scatterlist frags[4];
344 decryptor(struct scatterlist *sg, void *data)
346 struct decryptor_desc *desc = data;
347 int thislen = desc->fraglen + sg->length;
350 /* Worst case is 4 fragments: head, end of page 1, start
351 * of page 2, tail. Anything more is a bug. */
352 BUG_ON(desc->fragno > 3);
353 desc->frags[desc->fragno] = *sg;
355 desc->fraglen += sg->length;
357 fraglen = thislen & 7; /* XXX hardcoded blocksize */
363 ret = crypto_cipher_decrypt_iv(desc->tfm, desc->frags, desc->frags,
368 desc->frags[0].page = sg->page;
369 desc->frags[0].offset = sg->offset + sg->length - fraglen;
370 desc->frags[0].length = fraglen;
372 desc->fraglen = fraglen;
381 gss_decrypt_xdr_buf(struct crypto_tfm *tfm, struct xdr_buf *buf, int offset)
383 struct decryptor_desc desc;
386 BUG_ON((buf->len - offset) % crypto_tfm_alg_blocksize(tfm) != 0);
388 memset(desc.iv, 0, sizeof(desc.iv));
392 return process_xdr_buf(buf, offset, buf->len - offset, decryptor, &desc);
395 EXPORT_SYMBOL(gss_decrypt_xdr_buf);