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
51 #include <libcfs/linux/linux-crypto.h>
53 #include <obd_support.h>
55 #include "gss_internal.h"
56 #include "gss_crypto.h"
58 int gss_keyblock_init(struct gss_keyblock *kb, char *alg_name,
63 kb->kb_tfm = crypto_alloc_blkcipher(alg_name, alg_mode, 0);
64 if (IS_ERR(kb->kb_tfm)) {
65 rc = PTR_ERR(kb->kb_tfm);
67 CERROR("failed to alloc tfm: %s, mode %d: rc = %d\n", alg_name,
72 rc = crypto_blkcipher_setkey(kb->kb_tfm, kb->kb_key.data,
75 CERROR("failed to set %s key, len %d, rc = %d\n", alg_name,
83 void gss_keyblock_free(struct gss_keyblock *kb)
85 rawobj_free(&kb->kb_key);
87 crypto_free_blkcipher(kb->kb_tfm);
90 int gss_keyblock_dup(struct gss_keyblock *new, struct gss_keyblock *kb)
92 return rawobj_dup(&new->kb_key, &kb->kb_key);
95 int gss_get_bytes(char **ptr, const char *end, void *res, size_t len)
100 if (q > end || q < p)
107 int gss_get_rawobj(char **ptr, const char *end, rawobj_t *res)
113 if (gss_get_bytes(&p, end, &len, sizeof(len)))
117 if (q > end || q < p)
120 /* Support empty objects */
122 OBD_ALLOC_LARGE(res->data, len);
132 memcpy(res->data, p, len);
137 int gss_get_keyblock(char **ptr, const char *end,
138 struct gss_keyblock *kb, __u32 keysize)
143 OBD_ALLOC_LARGE(buf, keysize);
147 rc = gss_get_bytes(ptr, end, buf, keysize);
149 OBD_FREE_LARGE(buf, keysize);
153 kb->kb_key.len = keysize;
154 kb->kb_key.data = buf;
159 * Should be used for buffers allocated with k/vmalloc().
161 * Dispose of @sgt with gss_teardown_sgtable().
163 * @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
164 * in cases where a single sg is sufficient. No attempt to reduce the
165 * number of sgs by squeezing physically contiguous pages together is
166 * made though, for simplicity.
168 * This function is copied from the ceph filesystem code.
170 int gss_setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
171 const void *buf, unsigned int buf_len)
173 struct scatterlist *sg;
174 const bool is_vmalloc = is_vmalloc_addr(buf);
175 unsigned int off = offset_in_page(buf);
176 unsigned int chunk_cnt = 1;
177 unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
182 memset(sgt, 0, sizeof(*sgt));
187 chunk_cnt = chunk_len >> PAGE_SHIFT;
188 chunk_len = PAGE_SIZE;
192 rc = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
196 WARN_ON_ONCE(chunk_cnt != 1);
197 sg_init_table(prealloc_sg, 1);
198 sgt->sgl = prealloc_sg;
199 sgt->nents = sgt->orig_nents = 1;
202 for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
204 unsigned int len = min(chunk_len - off, buf_len);
207 page = vmalloc_to_page(buf);
209 page = virt_to_page(buf);
211 sg_set_page(sg, page, len, off);
218 WARN_ON_ONCE(buf_len != 0);
223 void gss_teardown_sgtable(struct sg_table *sgt)
225 if (sgt->orig_nents > 1)
229 int gss_crypt_generic(struct crypto_blkcipher *tfm, int decrypt, const void *iv,
230 const void *in, void *out, size_t length)
232 struct blkcipher_desc desc;
233 struct scatterlist sg;
234 struct sg_table sg_out;
235 __u8 local_iv[16] = {0};
240 desc.info = local_iv;
243 if (length % crypto_blkcipher_blocksize(tfm) != 0) {
244 CERROR("output length %zu mismatch blocksize %d\n",
245 length, crypto_blkcipher_blocksize(tfm));
249 if (crypto_blkcipher_ivsize(tfm) > ARRAY_SIZE(local_iv)) {
250 CERROR("iv size too large %d\n", crypto_blkcipher_ivsize(tfm));
255 memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
257 memcpy(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_hmac(struct crypto_ahash *tfm,
276 int msgcnt, rawobj_t *msgs,
277 int iovcnt, lnet_kiov_t *iovs,
280 struct ahash_request *req;
281 struct scatterlist sg[1];
286 rc = crypto_ahash_setkey(tfm, key->data, key->len);
290 req = ahash_request_alloc(tfm, GFP_KERNEL);
292 crypto_free_ahash(tfm);
296 rc = crypto_ahash_init(req);
300 for (i = 0; i < msgcnt; i++) {
301 if (msgs[i].len == 0)
304 rc = gss_setup_sgtable(&sgt, sg, msgs[i].data, msgs[i].len);
307 ahash_request_set_crypt(req, sg, NULL, msgs[i].len);
310 rc = crypto_ahash_update(req);
314 gss_teardown_sgtable(&sgt);
317 for (i = 0; i < iovcnt; i++) {
318 if (iovs[i].kiov_len == 0)
321 sg_init_table(sg, 1);
322 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
323 iovs[i].kiov_offset);
325 ahash_request_set_crypt(req, sg, NULL, iovs[i].kiov_len);
328 rc = crypto_ahash_update(req);
334 rc = gss_setup_sgtable(&sgt, sg, hdr, sizeof(*hdr));
338 ahash_request_set_crypt(req, sg, NULL, sizeof(hdr->len));
341 rc = crypto_ahash_update(req);
345 gss_teardown_sgtable(&sgt);
348 return crypto_ahash_final(req);
351 int gss_digest_norm(struct crypto_ahash *tfm,
352 struct gss_keyblock *kb,
354 int msgcnt, rawobj_t *msgs,
355 int iovcnt, lnet_kiov_t *iovs,
358 struct ahash_request *req;
359 struct scatterlist sg[1];
366 req = ahash_request_alloc(tfm, GFP_KERNEL);
368 crypto_free_ahash(tfm);
372 rc = crypto_ahash_init(req);
376 for (i = 0; i < msgcnt; i++) {
377 if (msgs[i].len == 0)
380 rc = gss_setup_sgtable(&sgt, sg, msgs[i].data, msgs[i].len);
384 ahash_request_set_crypt(req, sg, NULL, msgs[i].len);
387 rc = crypto_ahash_update(req);
391 gss_teardown_sgtable(&sgt);
394 for (i = 0; i < iovcnt; i++) {
395 if (iovs[i].kiov_len == 0)
398 sg_init_table(sg, 1);
399 sg_set_page(&sg[0], iovs[i].kiov_page, iovs[i].kiov_len,
400 iovs[i].kiov_offset);
401 ahash_request_set_crypt(req, sg, NULL, iovs[i].kiov_len);
404 rc = crypto_ahash_update(req);
410 rc = gss_setup_sgtable(&sgt, sg, hdr, sizeof(*hdr));
414 ahash_request_set_crypt(req, sg, NULL, sizeof(*hdr));
417 rc = crypto_ahash_update(req);
421 gss_teardown_sgtable(&sgt);
424 rc = crypto_ahash_final(req);
428 return gss_crypt_generic(kb->kb_tfm, 0, NULL, cksum->data,
429 cksum->data, cksum->len);
432 int gss_add_padding(rawobj_t *msg, int msg_buflen, int blocksize)
436 padding = (blocksize - (msg->len & (blocksize - 1))) &
441 if (msg->len + padding > msg_buflen) {
442 CERROR("bufsize %u too small: datalen %u, padding %u\n",
443 msg_buflen, msg->len, padding);
447 memset(msg->data + msg->len, padding, padding);
452 int gss_crypt_rawobjs(struct crypto_blkcipher *tfm, __u8 *iv,
453 int inobj_cnt, rawobj_t *inobjs, rawobj_t *outobj,
456 struct blkcipher_desc desc;
457 struct scatterlist src;
458 struct scatterlist dst;
459 struct sg_table sg_dst;
460 struct sg_table sg_src;
471 for (i = 0; i < inobj_cnt; i++) {
472 LASSERT(buf + inobjs[i].len <= outobj->data + outobj->len);
474 rc = gss_setup_sgtable(&sg_src, &src, inobjs[i].data,
479 rc = gss_setup_sgtable(&sg_dst, &dst, buf,
480 outobj->len - datalen);
482 gss_teardown_sgtable(&sg_src);
488 rc = crypto_blkcipher_encrypt_iv(&desc, &dst,
492 rc = crypto_blkcipher_decrypt_iv(&desc, &dst,
497 rc = crypto_blkcipher_encrypt(&desc, &dst, &src,
500 rc = crypto_blkcipher_decrypt(&desc, &dst, &src,
504 gss_teardown_sgtable(&sg_src);
505 gss_teardown_sgtable(&sg_dst);
508 CERROR("encrypt error %d\n", rc);
512 datalen += inobjs[i].len;
513 buf += inobjs[i].len;
516 outobj->len = datalen;