blob: 32be431affcf2b80eac2147b313e09e4a0a9d1fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_krb5_crypto.c
3 *
4 * Copyright (c) 2000 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Andy Adamson <andros@umich.edu>
8 * Bruce Fields <bfields@umich.edu>
9 */
10
11/*
12 * Copyright (C) 1998 by the FundsXpress, INC.
13 *
14 * All rights reserved.
15 *
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.
20 *
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.
31 *
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.
35 */
36
Herbert Xu35058682006-08-24 19:10:20 +100037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/types.h>
39#include <linux/mm.h>
40#include <linux/slab.h>
David Hardeman378f0582005-09-17 17:55:31 +100041#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/crypto.h>
43#include <linux/highmem.h>
44#include <linux/pagemap.h>
45#include <linux/sunrpc/gss_krb5.h>
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -050046#include <linux/sunrpc/xdr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#ifdef RPC_DEBUG
49# define RPCDBG_FACILITY RPCDBG_AUTH
50#endif
51
52u32
53krb5_encrypt(
Herbert Xu378c6692006-08-22 20:33:54 +100054 struct crypto_blkcipher *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void * iv,
56 void * in,
57 void * out,
58 int length)
59{
60 u32 ret = -EINVAL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080061 struct scatterlist sg[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 u8 local_iv[16] = {0};
Herbert Xu378c6692006-08-22 20:33:54 +100063 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Herbert Xu378c6692006-08-22 20:33:54 +100065 if (length % crypto_blkcipher_blocksize(tfm) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto out;
67
Herbert Xu378c6692006-08-22 20:33:54 +100068 if (crypto_blkcipher_ivsize(tfm) > 16) {
Chuck Lever8885cb32007-01-31 12:14:05 -050069 dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
YOSHIFUJI Hideakifb1416a2007-07-19 10:44:53 +090070 crypto_blkcipher_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto out;
72 }
73
74 if (iv)
Herbert Xu378c6692006-08-22 20:33:54 +100075 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 memcpy(out, in, length);
Herbert Xu6df5b9f2005-09-19 22:30:11 +100078 sg_set_buf(sg, out, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Herbert Xu378c6692006-08-22 20:33:54 +100080 ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081out:
Chuck Lever8885cb32007-01-31 12:14:05 -050082 dprintk("RPC: krb5_encrypt returns %d\n", ret);
J. Bruce Fields8fc75002006-12-04 20:22:31 -050083 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86EXPORT_SYMBOL(krb5_encrypt);
87
88u32
89krb5_decrypt(
Herbert Xu378c6692006-08-22 20:33:54 +100090 struct crypto_blkcipher *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 void * iv,
92 void * in,
93 void * out,
94 int length)
95{
96 u32 ret = -EINVAL;
97 struct scatterlist sg[1];
98 u8 local_iv[16] = {0};
Herbert Xu378c6692006-08-22 20:33:54 +100099 struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Herbert Xu378c6692006-08-22 20:33:54 +1000101 if (length % crypto_blkcipher_blocksize(tfm) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 goto out;
103
Herbert Xu378c6692006-08-22 20:33:54 +1000104 if (crypto_blkcipher_ivsize(tfm) > 16) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500105 dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
Herbert Xu378c6692006-08-22 20:33:54 +1000106 crypto_blkcipher_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto out;
108 }
109 if (iv)
Herbert Xu378c6692006-08-22 20:33:54 +1000110 memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 memcpy(out, in, length);
Herbert Xu6df5b9f2005-09-19 22:30:11 +1000113 sg_set_buf(sg, out, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Herbert Xu378c6692006-08-22 20:33:54 +1000115 ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116out:
Chuck Lever8885cb32007-01-31 12:14:05 -0500117 dprintk("RPC: gss_k5decrypt returns %d\n",ret);
J. Bruce Fields8fc75002006-12-04 20:22:31 -0500118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121EXPORT_SYMBOL(krb5_decrypt);
122
J. Bruce Fieldsf7b3af62005-10-13 16:55:03 -0400123static int
J. Bruce Fieldsf7b3af62005-10-13 16:55:03 -0400124checksummer(struct scatterlist *sg, void *data)
125{
Herbert Xu35058682006-08-24 19:10:20 +1000126 struct hash_desc *desc = data;
J. Bruce Fieldsf7b3af62005-10-13 16:55:03 -0400127
Herbert Xu35058682006-08-24 19:10:20 +1000128 return crypto_hash_update(desc, sg, sg->length);
J. Bruce Fieldsf7b3af62005-10-13 16:55:03 -0400129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* checksum the plaintext data and hdrlen bytes of the token header */
132s32
J. Bruce Fieldsca54f892006-12-04 20:22:38 -0500133make_checksum(char *cksumname, char *header, int hdrlen, struct xdr_buf *body,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400134 int body_offset, struct xdr_netobj *cksum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Herbert Xu35058682006-08-24 19:10:20 +1000136 struct hash_desc desc; /* XXX add to ctx? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct scatterlist sg[1];
Herbert Xu35058682006-08-24 19:10:20 +1000138 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Herbert Xu35058682006-08-24 19:10:20 +1000140 desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
141 if (IS_ERR(desc.tfm))
J. Bruce Fieldsd4a30e72006-04-18 13:14:02 -0400142 return GSS_S_FAILURE;
Herbert Xu35058682006-08-24 19:10:20 +1000143 cksum->len = crypto_hash_digestsize(desc.tfm);
144 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Herbert Xu35058682006-08-24 19:10:20 +1000146 err = crypto_hash_init(&desc);
147 if (err)
148 goto out;
David Hardeman378f0582005-09-17 17:55:31 +1000149 sg_set_buf(sg, header, hdrlen);
Herbert Xu35058682006-08-24 19:10:20 +1000150 err = crypto_hash_update(&desc, sg, hdrlen);
151 if (err)
152 goto out;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -0500153 err = xdr_process_buf(body, body_offset, body->len - body_offset,
Herbert Xu35058682006-08-24 19:10:20 +1000154 checksummer, &desc);
155 if (err)
156 goto out;
157 err = crypto_hash_final(&desc, cksum->data);
158
159out:
160 crypto_free_hash(desc.tfm);
161 return err ? GSS_S_FAILURE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164EXPORT_SYMBOL(make_checksum);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400165
166struct encryptor_desc {
167 u8 iv[8]; /* XXX hard-coded blocksize */
Herbert Xu378c6692006-08-22 20:33:54 +1000168 struct blkcipher_desc desc;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400169 int pos;
170 struct xdr_buf *outbuf;
171 struct page **pages;
172 struct scatterlist infrags[4];
173 struct scatterlist outfrags[4];
174 int fragno;
175 int fraglen;
176};
177
178static int
179encryptor(struct scatterlist *sg, void *data)
180{
181 struct encryptor_desc *desc = data;
182 struct xdr_buf *outbuf = desc->outbuf;
183 struct page *in_page;
184 int thislen = desc->fraglen + sg->length;
185 int fraglen, ret;
186 int page_pos;
187
188 /* Worst case is 4 fragments: head, end of page 1, start
189 * of page 2, tail. Anything more is a bug. */
190 BUG_ON(desc->fragno > 3);
191 desc->infrags[desc->fragno] = *sg;
192 desc->outfrags[desc->fragno] = *sg;
193
194 page_pos = desc->pos - outbuf->head[0].iov_len;
195 if (page_pos >= 0 && page_pos < outbuf->page_len) {
196 /* pages are not in place: */
197 int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
198 in_page = desc->pages[i];
199 } else {
Jens Axboefa05f122007-10-22 19:44:26 +0200200 in_page = sg_page(sg);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400201 }
Jens Axboefa05f122007-10-22 19:44:26 +0200202 sg_set_page(&desc->infrags[desc->fragno], in_page);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400203 desc->fragno++;
204 desc->fraglen += sg->length;
205 desc->pos += sg->length;
206
207 fraglen = thislen & 7; /* XXX hardcoded blocksize */
208 thislen -= fraglen;
209
210 if (thislen == 0)
211 return 0;
212
Herbert Xu378c6692006-08-22 20:33:54 +1000213 ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
214 desc->infrags, thislen);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400215 if (ret)
216 return ret;
217 if (fraglen) {
Jens Axboefa05f122007-10-22 19:44:26 +0200218 sg_set_page(&desc->outfrags[0], sg_page(sg));
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400219 desc->outfrags[0].offset = sg->offset + sg->length - fraglen;
220 desc->outfrags[0].length = fraglen;
221 desc->infrags[0] = desc->outfrags[0];
Jens Axboefa05f122007-10-22 19:44:26 +0200222 sg_set_page(&desc->infrags[0], in_page);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400223 desc->fragno = 1;
224 desc->fraglen = fraglen;
225 } else {
226 desc->fragno = 0;
227 desc->fraglen = 0;
228 }
229 return 0;
230}
231
232int
Herbert Xu378c6692006-08-22 20:33:54 +1000233gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
234 int offset, struct page **pages)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400235{
236 int ret;
237 struct encryptor_desc desc;
238
Herbert Xu378c6692006-08-22 20:33:54 +1000239 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400240
241 memset(desc.iv, 0, sizeof(desc.iv));
Herbert Xu378c6692006-08-22 20:33:54 +1000242 desc.desc.tfm = tfm;
243 desc.desc.info = desc.iv;
244 desc.desc.flags = 0;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400245 desc.pos = offset;
246 desc.outbuf = buf;
247 desc.pages = pages;
248 desc.fragno = 0;
249 desc.fraglen = 0;
250
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -0500251 ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400252 return ret;
253}
254
255EXPORT_SYMBOL(gss_encrypt_xdr_buf);
256
257struct decryptor_desc {
258 u8 iv[8]; /* XXX hard-coded blocksize */
Herbert Xu378c6692006-08-22 20:33:54 +1000259 struct blkcipher_desc desc;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400260 struct scatterlist frags[4];
261 int fragno;
262 int fraglen;
263};
264
265static int
266decryptor(struct scatterlist *sg, void *data)
267{
268 struct decryptor_desc *desc = data;
269 int thislen = desc->fraglen + sg->length;
270 int fraglen, ret;
271
272 /* Worst case is 4 fragments: head, end of page 1, start
273 * of page 2, tail. Anything more is a bug. */
274 BUG_ON(desc->fragno > 3);
275 desc->frags[desc->fragno] = *sg;
276 desc->fragno++;
277 desc->fraglen += sg->length;
278
279 fraglen = thislen & 7; /* XXX hardcoded blocksize */
280 thislen -= fraglen;
281
282 if (thislen == 0)
283 return 0;
284
Herbert Xu378c6692006-08-22 20:33:54 +1000285 ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
286 desc->frags, thislen);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400287 if (ret)
288 return ret;
289 if (fraglen) {
Jens Axboefa05f122007-10-22 19:44:26 +0200290 sg_set_page(&desc->frags[0], sg_page(sg));
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400291 desc->frags[0].offset = sg->offset + sg->length - fraglen;
292 desc->frags[0].length = fraglen;
293 desc->fragno = 1;
294 desc->fraglen = fraglen;
295 } else {
296 desc->fragno = 0;
297 desc->fraglen = 0;
298 }
299 return 0;
300}
301
302int
Herbert Xu378c6692006-08-22 20:33:54 +1000303gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
304 int offset)
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400305{
306 struct decryptor_desc desc;
307
308 /* XXXJBF: */
Herbert Xu378c6692006-08-22 20:33:54 +1000309 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400310
311 memset(desc.iv, 0, sizeof(desc.iv));
Herbert Xu378c6692006-08-22 20:33:54 +1000312 desc.desc.tfm = tfm;
313 desc.desc.info = desc.iv;
314 desc.desc.flags = 0;
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400315 desc.fragno = 0;
316 desc.fraglen = 0;
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -0500317 return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400318}
319
320EXPORT_SYMBOL(gss_decrypt_xdr_buf);