SG: Change sg_set_page() to take length and offset argument
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / net / sunrpc / auth_gss / gss_krb5_crypto.c
CommitLineData
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
52u32
53krb5_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;
cca5172a 61 struct scatterlist sg[1];
1da177e4 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) {
8885cb36 69 dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
fb1416a5 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 81out:
8885cb36 82 dprintk("RPC: krb5_encrypt returns %d\n", ret);
8fc7500b 83 return ret;
1da177e4
LT
84}
85
86EXPORT_SYMBOL(krb5_encrypt);
87
88u32
89krb5_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) {
8885cb36 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 116out:
8885cb36 117 dprintk("RPC: gss_k5decrypt returns %d\n",ret);
8fc7500b 118 return ret;
1da177e4
LT
119}
120
121EXPORT_SYMBOL(krb5_decrypt);
122
f7b3af64
BF
123static int
124checksummer(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 */
132s32
ca54f896 133make_checksum(char *cksumname, char *header, int hdrlen, struct xdr_buf *body,
14ae162c 134 int body_offset, struct xdr_netobj *cksum)
1da177e4 135{
35058687 136 struct hash_desc desc; /* XXX add to ctx? */
1da177e4 137 struct scatterlist sg[1];
35058687 138 int err;
1da177e4 139
35058687
HX
140 desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
141 if (IS_ERR(desc.tfm))
d4a30e7e 142 return GSS_S_FAILURE;
35058687
HX
143 cksum->len = crypto_hash_digestsize(desc.tfm);
144 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
1da177e4 145
35058687
HX
146 err = crypto_hash_init(&desc);
147 if (err)
148 goto out;
378f058c 149 sg_set_buf(sg, header, hdrlen);
35058687
HX
150 err = crypto_hash_update(&desc, sg, hdrlen);
151 if (err)
152 goto out;
37a4e6cb 153 err = xdr_process_buf(body, body_offset, body->len - body_offset,
35058687
HX
154 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;
1da177e4
LT
162}
163
164EXPORT_SYMBOL(make_checksum);
14ae162c
BF
165
166struct encryptor_desc {
167 u8 iv[8]; /* XXX hard-coded blocksize */
378c6697 168 struct blkcipher_desc desc;
14ae162c
BF
169 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 {
fa05f128 200 in_page = sg_page(sg);
14ae162c 201 }
642f1490 202 sg_assign_page(&desc->infrags[desc->fragno], in_page);
14ae162c
BF
203 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
378c6697
HX
213 ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
214 desc->infrags, thislen);
14ae162c
BF
215 if (ret)
216 return ret;
217 if (fraglen) {
642f1490
JA
218 sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
219 sg->offset + sg->length - fraglen);
14ae162c 220 desc->infrags[0] = desc->outfrags[0];
642f1490 221 sg_assign_page(&desc->infrags[0], in_page);
14ae162c
BF
222 desc->fragno = 1;
223 desc->fraglen = fraglen;
224 } else {
225 desc->fragno = 0;
226 desc->fraglen = 0;
227 }
228 return 0;
229}
230
231int
378c6697
HX
232gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
233 int offset, struct page **pages)
14ae162c
BF
234{
235 int ret;
236 struct encryptor_desc desc;
237
378c6697 238 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
14ae162c
BF
239
240 memset(desc.iv, 0, sizeof(desc.iv));
378c6697
HX
241 desc.desc.tfm = tfm;
242 desc.desc.info = desc.iv;
243 desc.desc.flags = 0;
14ae162c
BF
244 desc.pos = offset;
245 desc.outbuf = buf;
246 desc.pages = pages;
247 desc.fragno = 0;
248 desc.fraglen = 0;
249
37a4e6cb 250 ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
14ae162c
BF
251 return ret;
252}
253
254EXPORT_SYMBOL(gss_encrypt_xdr_buf);
255
256struct decryptor_desc {
257 u8 iv[8]; /* XXX hard-coded blocksize */
378c6697 258 struct blkcipher_desc desc;
14ae162c
BF
259 struct scatterlist frags[4];
260 int fragno;
261 int fraglen;
262};
263
264static int
265decryptor(struct scatterlist *sg, void *data)
266{
267 struct decryptor_desc *desc = data;
268 int thislen = desc->fraglen + sg->length;
269 int fraglen, ret;
270
271 /* Worst case is 4 fragments: head, end of page 1, start
272 * of page 2, tail. Anything more is a bug. */
273 BUG_ON(desc->fragno > 3);
274 desc->frags[desc->fragno] = *sg;
275 desc->fragno++;
276 desc->fraglen += sg->length;
277
278 fraglen = thislen & 7; /* XXX hardcoded blocksize */
279 thislen -= fraglen;
280
281 if (thislen == 0)
282 return 0;
283
378c6697
HX
284 ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
285 desc->frags, thislen);
14ae162c
BF
286 if (ret)
287 return ret;
288 if (fraglen) {
642f1490
JA
289 sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
290 sg->offset + sg->length - fraglen);
14ae162c
BF
291 desc->fragno = 1;
292 desc->fraglen = fraglen;
293 } else {
294 desc->fragno = 0;
295 desc->fraglen = 0;
296 }
297 return 0;
298}
299
300int
378c6697
HX
301gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
302 int offset)
14ae162c
BF
303{
304 struct decryptor_desc desc;
305
306 /* XXXJBF: */
378c6697 307 BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
14ae162c
BF
308
309 memset(desc.iv, 0, sizeof(desc.iv));
378c6697
HX
310 desc.desc.tfm = tfm;
311 desc.desc.info = desc.iv;
312 desc.desc.flags = 0;
14ae162c
BF
313 desc.fragno = 0;
314 desc.fraglen = 0;
37a4e6cb 315 return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
14ae162c
BF
316}
317
318EXPORT_SYMBOL(gss_decrypt_xdr_buf);