Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
35058687 | 37 | #include <linux/err.h> |
1da177e4 LT |
38 | #include <linux/types.h> |
39 | #include <linux/mm.h> | |
40 | #include <linux/slab.h> | |
378f058c | 41 | #include <linux/scatterlist.h> |
1da177e4 LT |
42 | #include <linux/crypto.h> |
43 | #include <linux/highmem.h> | |
44 | #include <linux/pagemap.h> | |
45 | #include <linux/sunrpc/gss_krb5.h> | |
37a4e6cb | 46 | #include <linux/sunrpc/xdr.h> |
1da177e4 LT |
47 | |
48 | #ifdef RPC_DEBUG | |
49 | # define RPCDBG_FACILITY RPCDBG_AUTH | |
50 | #endif | |
51 | ||
52 | u32 | |
53 | krb5_encrypt( | |
378c6697 | 54 | struct crypto_blkcipher *tfm, |
1da177e4 LT |
55 | void * iv, |
56 | void * in, | |
57 | void * out, | |
58 | int length) | |
59 | { | |
60 | u32 ret = -EINVAL; | |
61 | struct scatterlist sg[1]; | |
62 | u8 local_iv[16] = {0}; | |
378c6697 | 63 | struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv }; |
1da177e4 | 64 | |
378c6697 | 65 | if (length % crypto_blkcipher_blocksize(tfm) != 0) |
1da177e4 LT |
66 | goto out; |
67 | ||
378c6697 | 68 | if (crypto_blkcipher_ivsize(tfm) > 16) { |
1da177e4 | 69 | dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n", |
378c6697 | 70 | crypto_blkcipher_ivsize(tfm)); |
1da177e4 LT |
71 | goto out; |
72 | } | |
73 | ||
74 | if (iv) | |
378c6697 | 75 | memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm)); |
1da177e4 LT |
76 | |
77 | memcpy(out, in, length); | |
6df5b9f4 | 78 | sg_set_buf(sg, out, length); |
1da177e4 | 79 | |
378c6697 | 80 | ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length); |
1da177e4 LT |
81 | out: |
82 | dprintk("RPC: krb5_encrypt returns %d\n",ret); | |
8fc7500b | 83 | return ret; |
1da177e4 LT |
84 | } |
85 | ||
86 | EXPORT_SYMBOL(krb5_encrypt); | |
87 | ||
88 | u32 | |
89 | krb5_decrypt( | |
378c6697 | 90 | struct crypto_blkcipher *tfm, |
1da177e4 LT |
91 | 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}; | |
378c6697 | 99 | struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv }; |
1da177e4 | 100 | |
378c6697 | 101 | if (length % crypto_blkcipher_blocksize(tfm) != 0) |
1da177e4 LT |
102 | goto out; |
103 | ||
378c6697 | 104 | if (crypto_blkcipher_ivsize(tfm) > 16) { |
1da177e4 | 105 | dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n", |
378c6697 | 106 | crypto_blkcipher_ivsize(tfm)); |
1da177e4 LT |
107 | goto out; |
108 | } | |
109 | if (iv) | |
378c6697 | 110 | memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm)); |
1da177e4 LT |
111 | |
112 | memcpy(out, in, length); | |
6df5b9f4 | 113 | sg_set_buf(sg, out, length); |
1da177e4 | 114 | |
378c6697 | 115 | ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length); |
1da177e4 LT |
116 | out: |
117 | dprintk("RPC: gss_k5decrypt returns %d\n",ret); | |
8fc7500b | 118 | return ret; |
1da177e4 LT |
119 | } |
120 | ||
121 | EXPORT_SYMBOL(krb5_decrypt); | |
122 | ||
f7b3af64 BF |
123 | static int |
124 | checksummer(struct scatterlist *sg, void *data) | |
125 | { | |
35058687 | 126 | struct hash_desc *desc = data; |
f7b3af64 | 127 | |
35058687 | 128 | return crypto_hash_update(desc, sg, sg->length); |
f7b3af64 BF |
129 | } |
130 | ||
1da177e4 LT |
131 | /* checksum the plaintext data and hdrlen bytes of the token header */ |
132 | s32 | |
133 | make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body, | |
14ae162c | 134 | int body_offset, struct xdr_netobj *cksum) |
1da177e4 LT |
135 | { |
136 | char *cksumname; | |
35058687 | 137 | struct hash_desc desc; /* XXX add to ctx? */ |
1da177e4 | 138 | struct scatterlist sg[1]; |
35058687 | 139 | int err; |
1da177e4 LT |
140 | |
141 | switch (cksumtype) { | |
142 | case CKSUMTYPE_RSA_MD5: | |
143 | cksumname = "md5"; | |
144 | break; | |
145 | default: | |
146 | dprintk("RPC: krb5_make_checksum:" | |
147 | " unsupported checksum %d", cksumtype); | |
d4a30e7e | 148 | return GSS_S_FAILURE; |
1da177e4 | 149 | } |
35058687 HX |
150 | desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC); |
151 | if (IS_ERR(desc.tfm)) | |
d4a30e7e | 152 | return GSS_S_FAILURE; |
35058687 HX |
153 | cksum->len = crypto_hash_digestsize(desc.tfm); |
154 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; | |
1da177e4 | 155 | |
35058687 HX |
156 | err = crypto_hash_init(&desc); |
157 | if (err) | |
158 | goto out; | |
378f058c | 159 | sg_set_buf(sg, header, hdrlen); |
35058687 HX |
160 | err = crypto_hash_update(&desc, sg, hdrlen); |
161 | if (err) | |
162 | goto out; | |
37a4e6cb | 163 | err = xdr_process_buf(body, body_offset, body->len - body_offset, |
35058687 HX |
164 | checksummer, &desc); |
165 | if (err) | |
166 | goto out; | |
167 | err = crypto_hash_final(&desc, cksum->data); | |
168 | ||
169 | out: | |
170 | crypto_free_hash(desc.tfm); | |
171 | return err ? GSS_S_FAILURE : 0; | |
1da177e4 LT |
172 | } |
173 | ||
174 | EXPORT_SYMBOL(make_checksum); | |
14ae162c BF |
175 | |
176 | struct encryptor_desc { | |
177 | u8 iv[8]; /* XXX hard-coded blocksize */ | |
378c6697 | 178 | struct blkcipher_desc desc; |
14ae162c BF |
179 | int pos; |
180 | struct xdr_buf *outbuf; | |
181 | struct page **pages; | |
182 | struct scatterlist infrags[4]; | |
183 | struct scatterlist outfrags[4]; | |
184 | int fragno; | |
185 | int fraglen; | |
186 | }; | |
187 | ||
188 | static int | |
189 | encryptor(struct scatterlist *sg, void *data) | |
190 | { | |
191 | struct encryptor_desc *desc = data; | |
192 | struct xdr_buf *outbuf = desc->outbuf; | |
193 | struct page *in_page; | |
194 | int thislen = desc->fraglen + sg->length; | |
195 | int fraglen, ret; | |
196 | int page_pos; | |
197 | ||
198 | /* Worst case is 4 fragments: head, end of page 1, start | |
199 | * of page 2, tail. Anything more is a bug. */ | |
200 | BUG_ON(desc->fragno > 3); | |
201 | desc->infrags[desc->fragno] = *sg; | |
202 | desc->outfrags[desc->fragno] = *sg; | |
203 | ||
204 | page_pos = desc->pos - outbuf->head[0].iov_len; | |
205 | if (page_pos >= 0 && page_pos < outbuf->page_len) { | |
206 | /* pages are not in place: */ | |
207 | int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT; | |
208 | in_page = desc->pages[i]; | |
209 | } else { | |
210 | in_page = sg->page; | |
211 | } | |
212 | desc->infrags[desc->fragno].page = in_page; | |
213 | desc->fragno++; | |
214 | desc->fraglen += sg->length; | |
215 | desc->pos += sg->length; | |
216 | ||
217 | fraglen = thislen & 7; /* XXX hardcoded blocksize */ | |
218 | thislen -= fraglen; | |
219 | ||
220 | if (thislen == 0) | |
221 | return 0; | |
222 | ||
378c6697 HX |
223 | ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags, |
224 | desc->infrags, thislen); | |
14ae162c BF |
225 | if (ret) |
226 | return ret; | |
227 | if (fraglen) { | |
228 | desc->outfrags[0].page = sg->page; | |
229 | desc->outfrags[0].offset = sg->offset + sg->length - fraglen; | |
230 | desc->outfrags[0].length = fraglen; | |
231 | desc->infrags[0] = desc->outfrags[0]; | |
232 | desc->infrags[0].page = in_page; | |
233 | desc->fragno = 1; | |
234 | desc->fraglen = fraglen; | |
235 | } else { | |
236 | desc->fragno = 0; | |
237 | desc->fraglen = 0; | |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
242 | int | |
378c6697 HX |
243 | gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf, |
244 | int offset, struct page **pages) | |
14ae162c BF |
245 | { |
246 | int ret; | |
247 | struct encryptor_desc desc; | |
248 | ||
378c6697 | 249 | BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0); |
14ae162c BF |
250 | |
251 | memset(desc.iv, 0, sizeof(desc.iv)); | |
378c6697 HX |
252 | desc.desc.tfm = tfm; |
253 | desc.desc.info = desc.iv; | |
254 | desc.desc.flags = 0; | |
14ae162c BF |
255 | desc.pos = offset; |
256 | desc.outbuf = buf; | |
257 | desc.pages = pages; | |
258 | desc.fragno = 0; | |
259 | desc.fraglen = 0; | |
260 | ||
37a4e6cb | 261 | ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc); |
14ae162c BF |
262 | return ret; |
263 | } | |
264 | ||
265 | EXPORT_SYMBOL(gss_encrypt_xdr_buf); | |
266 | ||
267 | struct decryptor_desc { | |
268 | u8 iv[8]; /* XXX hard-coded blocksize */ | |
378c6697 | 269 | struct blkcipher_desc desc; |
14ae162c BF |
270 | struct scatterlist frags[4]; |
271 | int fragno; | |
272 | int fraglen; | |
273 | }; | |
274 | ||
275 | static int | |
276 | decryptor(struct scatterlist *sg, void *data) | |
277 | { | |
278 | struct decryptor_desc *desc = data; | |
279 | int thislen = desc->fraglen + sg->length; | |
280 | int fraglen, ret; | |
281 | ||
282 | /* Worst case is 4 fragments: head, end of page 1, start | |
283 | * of page 2, tail. Anything more is a bug. */ | |
284 | BUG_ON(desc->fragno > 3); | |
285 | desc->frags[desc->fragno] = *sg; | |
286 | desc->fragno++; | |
287 | desc->fraglen += sg->length; | |
288 | ||
289 | fraglen = thislen & 7; /* XXX hardcoded blocksize */ | |
290 | thislen -= fraglen; | |
291 | ||
292 | if (thislen == 0) | |
293 | return 0; | |
294 | ||
378c6697 HX |
295 | ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags, |
296 | desc->frags, thislen); | |
14ae162c BF |
297 | if (ret) |
298 | return ret; | |
299 | if (fraglen) { | |
300 | desc->frags[0].page = sg->page; | |
301 | desc->frags[0].offset = sg->offset + sg->length - fraglen; | |
302 | desc->frags[0].length = fraglen; | |
303 | desc->fragno = 1; | |
304 | desc->fraglen = fraglen; | |
305 | } else { | |
306 | desc->fragno = 0; | |
307 | desc->fraglen = 0; | |
308 | } | |
309 | return 0; | |
310 | } | |
311 | ||
312 | int | |
378c6697 HX |
313 | gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf, |
314 | int offset) | |
14ae162c BF |
315 | { |
316 | struct decryptor_desc desc; | |
317 | ||
318 | /* XXXJBF: */ | |
378c6697 | 319 | BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0); |
14ae162c BF |
320 | |
321 | memset(desc.iv, 0, sizeof(desc.iv)); | |
378c6697 HX |
322 | desc.desc.tfm = tfm; |
323 | desc.desc.info = desc.iv; | |
324 | desc.desc.flags = 0; | |
14ae162c BF |
325 | desc.fragno = 0; |
326 | desc.fraglen = 0; | |
37a4e6cb | 327 | return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc); |
14ae162c BF |
328 | } |
329 | ||
330 | EXPORT_SYMBOL(gss_decrypt_xdr_buf); |