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/err.h>
38 #include <linux/types.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 #include <linux/sunrpc/xdr.h>
49 # define RPCDBG_FACILITY RPCDBG_AUTH
54 struct crypto_blkcipher
*tfm
,
61 struct scatterlist sg
[1];
62 u8 local_iv
[16] = {0};
63 struct blkcipher_desc desc
= { .tfm
= tfm
, .info
= local_iv
};
65 if (length
% crypto_blkcipher_blocksize(tfm
) != 0)
68 if (crypto_blkcipher_ivsize(tfm
) > 16) {
69 dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
70 crypto_blkcipher_ivsize(tfm
));
75 memcpy(local_iv
, iv
, crypto_blkcipher_ivsize(tfm
));
77 memcpy(out
, in
, length
);
78 sg_set_buf(sg
, out
, length
);
80 ret
= crypto_blkcipher_encrypt_iv(&desc
, sg
, sg
, length
);
82 dprintk("RPC: krb5_encrypt returns %d\n",ret
);
86 EXPORT_SYMBOL(krb5_encrypt
);
90 struct crypto_blkcipher
*tfm
,
97 struct scatterlist sg
[1];
98 u8 local_iv
[16] = {0};
99 struct blkcipher_desc desc
= { .tfm
= tfm
, .info
= local_iv
};
101 if (length
% crypto_blkcipher_blocksize(tfm
) != 0)
104 if (crypto_blkcipher_ivsize(tfm
) > 16) {
105 dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
106 crypto_blkcipher_ivsize(tfm
));
110 memcpy(local_iv
,iv
, crypto_blkcipher_ivsize(tfm
));
112 memcpy(out
, in
, length
);
113 sg_set_buf(sg
, out
, length
);
115 ret
= crypto_blkcipher_decrypt_iv(&desc
, sg
, sg
, length
);
117 dprintk("RPC: gss_k5decrypt returns %d\n",ret
);
121 EXPORT_SYMBOL(krb5_decrypt
);
124 checksummer(struct scatterlist
*sg
, void *data
)
126 struct hash_desc
*desc
= data
;
128 return crypto_hash_update(desc
, sg
, sg
->length
);
131 /* checksum the plaintext data and hdrlen bytes of the token header */
133 make_checksum(s32 cksumtype
, char *header
, int hdrlen
, struct xdr_buf
*body
,
134 int body_offset
, struct xdr_netobj
*cksum
)
137 struct hash_desc desc
; /* XXX add to ctx? */
138 struct scatterlist sg
[1];
142 case CKSUMTYPE_RSA_MD5
:
146 dprintk("RPC: krb5_make_checksum:"
147 " unsupported checksum %d", cksumtype
);
148 return GSS_S_FAILURE
;
150 desc
.tfm
= crypto_alloc_hash(cksumname
, 0, CRYPTO_ALG_ASYNC
);
151 if (IS_ERR(desc
.tfm
))
152 return GSS_S_FAILURE
;
153 cksum
->len
= crypto_hash_digestsize(desc
.tfm
);
154 desc
.flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
156 err
= crypto_hash_init(&desc
);
159 sg_set_buf(sg
, header
, hdrlen
);
160 err
= crypto_hash_update(&desc
, sg
, hdrlen
);
163 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
167 err
= crypto_hash_final(&desc
, cksum
->data
);
170 crypto_free_hash(desc
.tfm
);
171 return err
? GSS_S_FAILURE
: 0;
174 EXPORT_SYMBOL(make_checksum
);
176 struct encryptor_desc
{
177 u8 iv
[8]; /* XXX hard-coded blocksize */
178 struct blkcipher_desc desc
;
180 struct xdr_buf
*outbuf
;
182 struct scatterlist infrags
[4];
183 struct scatterlist outfrags
[4];
189 encryptor(struct scatterlist
*sg
, void *data
)
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
;
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
;
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
];
212 desc
->infrags
[desc
->fragno
].page
= in_page
;
214 desc
->fraglen
+= sg
->length
;
215 desc
->pos
+= sg
->length
;
217 fraglen
= thislen
& 7; /* XXX hardcoded blocksize */
223 ret
= crypto_blkcipher_encrypt_iv(&desc
->desc
, desc
->outfrags
,
224 desc
->infrags
, thislen
);
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
;
234 desc
->fraglen
= fraglen
;
243 gss_encrypt_xdr_buf(struct crypto_blkcipher
*tfm
, struct xdr_buf
*buf
,
244 int offset
, struct page
**pages
)
247 struct encryptor_desc desc
;
249 BUG_ON((buf
->len
- offset
) % crypto_blkcipher_blocksize(tfm
) != 0);
251 memset(desc
.iv
, 0, sizeof(desc
.iv
));
253 desc
.desc
.info
= desc
.iv
;
261 ret
= xdr_process_buf(buf
, offset
, buf
->len
- offset
, encryptor
, &desc
);
265 EXPORT_SYMBOL(gss_encrypt_xdr_buf
);
267 struct decryptor_desc
{
268 u8 iv
[8]; /* XXX hard-coded blocksize */
269 struct blkcipher_desc desc
;
270 struct scatterlist frags
[4];
276 decryptor(struct scatterlist
*sg
, void *data
)
278 struct decryptor_desc
*desc
= data
;
279 int thislen
= desc
->fraglen
+ sg
->length
;
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
;
287 desc
->fraglen
+= sg
->length
;
289 fraglen
= thislen
& 7; /* XXX hardcoded blocksize */
295 ret
= crypto_blkcipher_decrypt_iv(&desc
->desc
, desc
->frags
,
296 desc
->frags
, thislen
);
300 desc
->frags
[0].page
= sg
->page
;
301 desc
->frags
[0].offset
= sg
->offset
+ sg
->length
- fraglen
;
302 desc
->frags
[0].length
= fraglen
;
304 desc
->fraglen
= fraglen
;
313 gss_decrypt_xdr_buf(struct crypto_blkcipher
*tfm
, struct xdr_buf
*buf
,
316 struct decryptor_desc desc
;
319 BUG_ON((buf
->len
- offset
) % crypto_blkcipher_blocksize(tfm
) != 0);
321 memset(desc
.iv
, 0, sizeof(desc
.iv
));
323 desc
.desc
.info
= desc
.iv
;
327 return xdr_process_buf(buf
, offset
, buf
->len
- offset
, decryptor
, &desc
);
330 EXPORT_SYMBOL(gss_decrypt_xdr_buf
);