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