2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Author: Eric Mei <ericm@clusterfs.com>
10 * linux/net/sunrpc/gss_krb5_mech.c
11 * linux/net/sunrpc/gss_krb5_crypto.c
12 * linux/net/sunrpc/gss_krb5_seal.c
13 * linux/net/sunrpc/gss_krb5_seqnum.c
14 * linux/net/sunrpc/gss_krb5_unseal.c
16 * Copyright (c) 2001 The Regents of the University of Michigan.
17 * All rights reserved.
19 * Andy Adamson <andros@umich.edu>
20 * J. Bruce Fields <bfields@umich.edu>
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 #define DEBUG_SUBSYSTEM S_SEC
52 #include <obd_support.h>
54 #include "gss_internal.h"
55 #include "gss_crypto.h"
57 int gss_keyblock_init(struct gss_keyblock *kb, const char *alg_name,
62 kb->kb_tfm = crypto_alloc_blkcipher(alg_name, alg_mode, 0);
63 if (IS_ERR(kb->kb_tfm)) {
64 rc = PTR_ERR(kb->kb_tfm);
66 CERROR("failed to alloc tfm: %s, mode %d: rc = %d\n", alg_name,
71 rc = crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data,
74 CERROR("failed to set %s key, len %d, rc = %d\n", alg_name,
82 void gss_keyblock_free(struct gss_keyblock *kb)
84 rawobj_free(&kb->kb_key);
86 crypto_free_blkcipher(kb->kb_tfm);
89 int gss_keyblock_dup(struct gss_keyblock *new, struct gss_keyblock *kb)
91 return rawobj_dup(&new->kb_key, &kb->kb_key);
94 int gss_get_bytes(char **ptr, const char *end, void *res, size_t len)
106 int gss_get_rawobj(char **ptr, const char *end, rawobj_t *res)
112 if (gss_get_bytes(&p, end, &len, sizeof(len)))
116 if (q > end || q < p)
119 /* Support empty objects */
121 OBD_ALLOC_LARGE(res->data, len);
131 memcpy(res->data, p, len);
136 int gss_get_keyblock(char **ptr, const char *end,
137 struct gss_keyblock *kb, __u32 keysize)
142 OBD_ALLOC_LARGE(buf, keysize);
146 rc = gss_get_bytes(ptr, end, buf, keysize);
148 OBD_FREE_LARGE(buf, keysize);
152 kb->kb_key.len = keysize;
153 kb->kb_key.data = buf;
158 * Should be used for buffers allocated with k/vmalloc().
160 * Dispose of @sgt with gss_teardown_sgtable().
162 * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
163 * in cases where a single sg is sufficient. No attempt to reduce the
164 * number of sgs by squeezing physically contiguous pages together is
165 * made though, for simplicity.
167 * This function is copied from the ceph filesystem code.
169 int gss_setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
170 const void *buf, unsigned int buf_len)
172 struct scatterlist *sg;
173 const bool is_vmalloc = is_vmalloc_addr(buf);
174 unsigned int off = offset_in_page(buf);
175 unsigned int chunk_cnt = 1;
176 unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
181 memset(sgt, 0, sizeof(*sgt));
186 chunk_cnt = chunk_len >> PAGE_SHIFT;
187 chunk_len = PAGE_SIZE;
191 rc = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
195 WARN_ON_ONCE(chunk_cnt != 1);
196 sg_init_table(prealloc_sg, 1);
197 sgt->sgl = prealloc_sg;
198 sgt->nents = sgt->orig_nents = 1;
201 for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
203 unsigned int len = min(chunk_len - off, buf_len);
206 page = vmalloc_to_page(buf);
208 page = virt_to_page(buf);
210 sg_set_page(sg, page, len, off);
217 WARN_ON_ONCE(buf_len != 0);
222 void gss_teardown_sgtable(struct sg_table *sgt)
224 if (sgt->orig_nents > 1)
228 int gss_crypt_generic(struct crypto_blkcipher *tfm, int decrypt, const void *iv,
229 const void *in, void *out, size_t length)
231 struct blkcipher_desc desc;
232 struct scatterlist sg;
233 struct sg_table sg_out;
234 __u8 local_iv[16] = {0};
239 desc.info = local_iv;
242 if (length % crypto_blkcipher_blocksize(tfm) != 0) {
243 CERROR("output length %zu mismatch blocksize %d\n",
244 length, crypto_blkcipher_blocksize(tfm));
248 if (crypto_blkcipher_ivsize(tfm) > ARRAY_SIZE(local_iv)) {
249 CERROR("iv size too large %d\n", crypto_blkcipher_ivsize(tfm));
254 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
257 memmove(out, in, length);
259 ret = gss_setup_sgtable(&sg_out, &sg, out, length);
264 ret = crypto_blkcipher_decrypt_iv(&desc, &sg, &sg, length);
266 ret = crypto_blkcipher_encrypt_iv(&desc, &sg, &sg, length);
268 gss_teardown_sgtable(&sg_out);
273 int gss_digest_hash(struct ahash_request *req,
274 rawobj_t *hdr, int msgcnt, rawobj_t *msgs,
275 int iovcnt, struct bio_vec *iovs)
277 struct scatterlist sg[1];
282 for (i = 0; i < msgcnt; i++) {
283 if (msgs[i].len == 0)
286 rc = gss_setup_sgtable(&sgt, sg, msgs[i].data, msgs[i].len);
290 ahash_request_set_crypt(req, sg, NULL, msgs[i].len);
291 rc = crypto_ahash_update(req);
292 gss_teardown_sgtable(&sgt);
297 for (i = 0; i < iovcnt; i++) {
298 if (iovs[i].bv_len == 0)
301 sg_init_table(sg, 1);
302 sg_set_page(&sg[0], iovs[i].bv_page, iovs[i].bv_len,
305 ahash_request_set_crypt(req, sg, NULL, iovs[i].bv_len);
306 rc = crypto_ahash_update(req);
312 rc = gss_setup_sgtable(&sgt, sg, hdr->data, hdr->len);
316 ahash_request_set_crypt(req, sg, NULL, hdr->len);
317 rc = crypto_ahash_update(req);
318 gss_teardown_sgtable(&sgt);
326 int gss_digest_hash_compat(struct ahash_request *req,
327 rawobj_t *hdr, int msgcnt, rawobj_t *msgs,
328 int iovcnt, struct bio_vec *iovs)
330 struct scatterlist sg[1];
335 for (i = 0; i < msgcnt; i++) {
336 if (msgs[i].len == 0)
339 rc = gss_setup_sgtable(&sgt, sg, msgs[i].data, msgs[i].len);
343 ahash_request_set_crypt(req, sg, NULL, msgs[i].len);
344 rc = crypto_ahash_update(req);
345 gss_teardown_sgtable(&sgt);
350 for (i = 0; i < iovcnt; i++) {
351 if (iovs[i].bv_len == 0)
354 sg_init_table(sg, 1);
355 sg_set_page(&sg[0], iovs[i].bv_page, iovs[i].bv_len,
358 ahash_request_set_crypt(req, sg, NULL, iovs[i].bv_len);
359 rc = crypto_ahash_update(req);
365 rc = gss_setup_sgtable(&sgt, sg, &(hdr->len), sizeof(hdr->len));
369 ahash_request_set_crypt(req, sg, NULL, sizeof(hdr->len));
370 rc = crypto_ahash_update(req);
371 gss_teardown_sgtable(&sgt);
379 int gss_add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
383 padding = (blocksize - (msg->len & (blocksize - 1))) &
388 if (msg->len + padding > msg_buflen) {
389 CERROR("bufsize %u too small: datalen %u, padding %u\n",
390 msg_buflen, msg->len, padding);
394 memset(msg->data + msg->len, padding, padding);
399 int gss_crypt_rawobjs(struct crypto_blkcipher *tfm, __u8 *iv,
400 int inobj_cnt, rawobj_t *inobjs, rawobj_t *outobj,
403 struct blkcipher_desc desc;
404 struct scatterlist src;
405 struct scatterlist dst;
406 struct sg_table sg_dst;
407 struct sg_table sg_src;
418 for (i = 0; i < inobj_cnt; i++) {
419 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
421 rc = gss_setup_sgtable(&sg_src, &src, inobjs[i].data,
426 rc = gss_setup_sgtable(&sg_dst, &dst, buf,
427 outobj->len - datalen);
429 gss_teardown_sgtable(&sg_src);
435 rc = crypto_blkcipher_encrypt_iv(&desc, &dst,
439 rc = crypto_blkcipher_decrypt_iv(&desc, &dst,
444 rc = crypto_blkcipher_encrypt(&desc, &dst, &src,
447 rc = crypto_blkcipher_decrypt(&desc, &dst, &src,
451 gss_teardown_sgtable(&sg_src);
452 gss_teardown_sgtable(&sg_dst);
455 CERROR("encrypt error %d\n", rc);
459 datalen += inobjs[i].len;
460 buf += inobjs[i].len;
463 outobj->len = datalen;