gss_krb5_crypto.c 18.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  linux/net/sunrpc/gss_krb5_crypto.c
 *
4
 *  Copyright (c) 2000-2008 The Regents of the University of Michigan.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *  All rights reserved.
 *
 *  Andy Adamson   <andros@umich.edu>
 *  Bruce Fields   <bfields@umich.edu>
 */

/*
 * Copyright (C) 1998 by the FundsXpress, INC.
 *
 * All rights reserved.
 *
 * Export of this software from the United States of America may require
 * a specific license from the United States Government.  It is the
 * responsibility of any person or organization contemplating export to
 * obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of FundsXpress. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  FundsXpress makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

37
#include <linux/err.h>
L
Linus Torvalds 已提交
38 39
#include <linux/types.h>
#include <linux/mm.h>
40
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
41 42 43
#include <linux/crypto.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
44
#include <linux/random.h>
L
Linus Torvalds 已提交
45
#include <linux/sunrpc/gss_krb5.h>
O
Olga Kornievskaia 已提交
46
#include <linux/sunrpc/xdr.h>
L
Linus Torvalds 已提交
47 48 49 50 51 52 53

#ifdef RPC_DEBUG
# define RPCDBG_FACILITY        RPCDBG_AUTH
#endif

u32
krb5_encrypt(
54
	struct crypto_blkcipher *tfm,
L
Linus Torvalds 已提交
55 56 57 58 59 60
	void * iv,
	void * in,
	void * out,
	int length)
{
	u32 ret = -EINVAL;
61
	struct scatterlist sg[1];
62
	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
63
	struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
L
Linus Torvalds 已提交
64

65
	if (length % crypto_blkcipher_blocksize(tfm) != 0)
L
Linus Torvalds 已提交
66 67
		goto out;

68
	if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
69 70
		dprintk("RPC:       gss_k5encrypt: tfm iv size too large %d\n",
			crypto_blkcipher_ivsize(tfm));
L
Linus Torvalds 已提交
71 72 73 74
		goto out;
	}

	if (iv)
75
		memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
L
Linus Torvalds 已提交
76 77

	memcpy(out, in, length);
78
	sg_init_one(sg, out, length);
L
Linus Torvalds 已提交
79

80
	ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
L
Linus Torvalds 已提交
81
out:
82
	dprintk("RPC:       krb5_encrypt returns %d\n", ret);
83
	return ret;
L
Linus Torvalds 已提交
84 85 86 87
}

u32
krb5_decrypt(
88
     struct crypto_blkcipher *tfm,
L
Linus Torvalds 已提交
89 90 91 92 93 94 95
     void * iv,
     void * in,
     void * out,
     int length)
{
	u32 ret = -EINVAL;
	struct scatterlist sg[1];
96
	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
97
	struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
L
Linus Torvalds 已提交
98

99
	if (length % crypto_blkcipher_blocksize(tfm) != 0)
L
Linus Torvalds 已提交
100 101
		goto out;

102
	if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
103
		dprintk("RPC:       gss_k5decrypt: tfm iv size too large %d\n",
104
			crypto_blkcipher_ivsize(tfm));
L
Linus Torvalds 已提交
105 106 107
		goto out;
	}
	if (iv)
108
		memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
L
Linus Torvalds 已提交
109 110

	memcpy(out, in, length);
111
	sg_init_one(sg, out, length);
L
Linus Torvalds 已提交
112

113
	ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
L
Linus Torvalds 已提交
114
out:
115
	dprintk("RPC:       gss_k5decrypt returns %d\n",ret);
116
	return ret;
L
Linus Torvalds 已提交
117 118
}

119 120 121
static int
checksummer(struct scatterlist *sg, void *data)
{
122
	struct hash_desc *desc = data;
123

124
	return crypto_hash_update(desc, sg, sg->length);
125 126
}

127 128 129 130 131 132 133 134
/*
 * checksum the plaintext data and hdrlen bytes of the token header
 * The checksum is performed over the first 8 bytes of the
 * gss token header and then over the data body
 */
u32
make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
	      struct xdr_buf *body, int body_offset, u8 *cksumkey,
K
Kevin Coffman 已提交
135
	      unsigned int usage, struct xdr_netobj *cksumout)
L
Linus Torvalds 已提交
136
{
137
	struct hash_desc                desc;
L
Linus Torvalds 已提交
138
	struct scatterlist              sg[1];
139
	int err;
140 141 142 143 144 145 146 147
	u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
	unsigned int checksumlen;

	if (cksumout->len < kctx->gk5e->cksumlength) {
		dprintk("%s: checksum buffer length, %u, too small for %s\n",
			__func__, cksumout->len, kctx->gk5e->name);
		return GSS_S_FAILURE;
	}
L
Linus Torvalds 已提交
148

149
	desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
150
	if (IS_ERR(desc.tfm))
151
		return GSS_S_FAILURE;
152
	desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
L
Linus Torvalds 已提交
153

154 155 156 157 158 159 160 161 162
	checksumlen = crypto_hash_digestsize(desc.tfm);

	if (cksumkey != NULL) {
		err = crypto_hash_setkey(desc.tfm, cksumkey,
					 kctx->gk5e->keylength);
		if (err)
			goto out;
	}

163 164 165
	err = crypto_hash_init(&desc);
	if (err)
		goto out;
166
	sg_init_one(sg, header, hdrlen);
167 168 169
	err = crypto_hash_update(&desc, sg, hdrlen);
	if (err)
		goto out;
O
Olga Kornievskaia 已提交
170
	err = xdr_process_buf(body, body_offset, body->len - body_offset,
171 172 173
			      checksummer, &desc);
	if (err)
		goto out;
174 175 176
	err = crypto_hash_final(&desc, checksumdata);
	if (err)
		goto out;
177

178 179 180 181 182 183 184 185 186 187
	switch (kctx->gk5e->ctype) {
	case CKSUMTYPE_RSA_MD5:
		err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
					  checksumdata, checksumlen);
		if (err)
			goto out;
		memcpy(cksumout->data,
		       checksumdata + checksumlen - kctx->gk5e->cksumlength,
		       kctx->gk5e->cksumlength);
		break;
188 189 190
	case CKSUMTYPE_HMAC_SHA1_DES3:
		memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
		break;
191 192 193 194 195
	default:
		BUG();
		break;
	}
	cksumout->len = kctx->gk5e->cksumlength;
196 197 198
out:
	crypto_free_hash(desc.tfm);
	return err ? GSS_S_FAILURE : 0;
L
Linus Torvalds 已提交
199 200
}

201 202 203 204 205 206 207 208 209 210
/*
 * checksum the plaintext data and hdrlen bytes of the token header
 * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
 * body then over the first 16 octets of the MIC token
 * Inclusion of the header data in the calculation of the
 * checksum is optional.
 */
u32
make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen,
		 struct xdr_buf *body, int body_offset, u8 *cksumkey,
K
Kevin Coffman 已提交
211
		 unsigned int usage, struct xdr_netobj *cksumout)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
{
	struct hash_desc desc;
	struct scatterlist sg[1];
	int err;
	u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
	unsigned int checksumlen;

	if (kctx->gk5e->keyed_cksum == 0) {
		dprintk("%s: expected keyed hash for %s\n",
			__func__, kctx->gk5e->name);
		return GSS_S_FAILURE;
	}
	if (cksumkey == NULL) {
		dprintk("%s: no key supplied for %s\n",
			__func__, kctx->gk5e->name);
		return GSS_S_FAILURE;
	}

	desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0,
							CRYPTO_ALG_ASYNC);
	if (IS_ERR(desc.tfm))
		return GSS_S_FAILURE;
	checksumlen = crypto_hash_digestsize(desc.tfm);
	desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;

	err = crypto_hash_setkey(desc.tfm, cksumkey, kctx->gk5e->keylength);
	if (err)
		goto out;

	err = crypto_hash_init(&desc);
	if (err)
		goto out;
	err = xdr_process_buf(body, body_offset, body->len - body_offset,
			      checksummer, &desc);
	if (err)
		goto out;
	if (header != NULL) {
		sg_init_one(sg, header, hdrlen);
		err = crypto_hash_update(&desc, sg, hdrlen);
		if (err)
			goto out;
	}
	err = crypto_hash_final(&desc, checksumdata);
	if (err)
		goto out;

	cksumout->len = kctx->gk5e->cksumlength;

	switch (kctx->gk5e->ctype) {
	case CKSUMTYPE_HMAC_SHA1_96_AES128:
	case CKSUMTYPE_HMAC_SHA1_96_AES256:
		/* note that this truncates the hash */
		memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
		break;
	default:
		BUG();
		break;
	}
out:
	crypto_free_hash(desc.tfm);
	return err ? GSS_S_FAILURE : 0;
}

275
struct encryptor_desc {
276
	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
277
	struct blkcipher_desc desc;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	int pos;
	struct xdr_buf *outbuf;
	struct page **pages;
	struct scatterlist infrags[4];
	struct scatterlist outfrags[4];
	int fragno;
	int fraglen;
};

static int
encryptor(struct scatterlist *sg, void *data)
{
	struct encryptor_desc *desc = data;
	struct xdr_buf *outbuf = desc->outbuf;
	struct page *in_page;
	int thislen = desc->fraglen + sg->length;
	int fraglen, ret;
	int page_pos;

	/* Worst case is 4 fragments: head, end of page 1, start
	 * of page 2, tail.  Anything more is a bug. */
	BUG_ON(desc->fragno > 3);

	page_pos = desc->pos - outbuf->head[0].iov_len;
	if (page_pos >= 0 && page_pos < outbuf->page_len) {
		/* pages are not in place: */
		int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
		in_page = desc->pages[i];
	} else {
J
Jens Axboe 已提交
307
		in_page = sg_page(sg);
308
	}
309 310 311 312
	sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
		    sg->offset);
	sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
		    sg->offset);
313 314 315 316
	desc->fragno++;
	desc->fraglen += sg->length;
	desc->pos += sg->length;

317
	fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
318 319 320 321 322
	thislen -= fraglen;

	if (thislen == 0)
		return 0;

J
Jens Axboe 已提交
323 324
	sg_mark_end(&desc->infrags[desc->fragno - 1]);
	sg_mark_end(&desc->outfrags[desc->fragno - 1]);
325

326 327
	ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
					  desc->infrags, thislen);
328 329
	if (ret)
		return ret;
330 331 332 333

	sg_init_table(desc->infrags, 4);
	sg_init_table(desc->outfrags, 4);

334
	if (fraglen) {
335 336
		sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
				sg->offset + sg->length - fraglen);
337
		desc->infrags[0] = desc->outfrags[0];
338
		sg_assign_page(&desc->infrags[0], in_page);
339 340 341 342 343 344 345 346 347 348
		desc->fragno = 1;
		desc->fraglen = fraglen;
	} else {
		desc->fragno = 0;
		desc->fraglen = 0;
	}
	return 0;
}

int
349 350
gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
		    int offset, struct page **pages)
351 352 353 354
{
	int ret;
	struct encryptor_desc desc;

355
	BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
356 357

	memset(desc.iv, 0, sizeof(desc.iv));
358 359 360
	desc.desc.tfm = tfm;
	desc.desc.info = desc.iv;
	desc.desc.flags = 0;
361 362 363 364 365 366
	desc.pos = offset;
	desc.outbuf = buf;
	desc.pages = pages;
	desc.fragno = 0;
	desc.fraglen = 0;

367 368 369
	sg_init_table(desc.infrags, 4);
	sg_init_table(desc.outfrags, 4);

O
Olga Kornievskaia 已提交
370
	ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
371 372 373 374
	return ret;
}

struct decryptor_desc {
375
	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
376
	struct blkcipher_desc desc;
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	struct scatterlist frags[4];
	int fragno;
	int fraglen;
};

static int
decryptor(struct scatterlist *sg, void *data)
{
	struct decryptor_desc *desc = data;
	int thislen = desc->fraglen + sg->length;
	int fraglen, ret;

	/* Worst case is 4 fragments: head, end of page 1, start
	 * of page 2, tail.  Anything more is a bug. */
	BUG_ON(desc->fragno > 3);
392 393
	sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
		    sg->offset);
394 395 396
	desc->fragno++;
	desc->fraglen += sg->length;

397
	fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
398 399 400 401 402
	thislen -= fraglen;

	if (thislen == 0)
		return 0;

J
Jens Axboe 已提交
403
	sg_mark_end(&desc->frags[desc->fragno - 1]);
404

405 406
	ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
					  desc->frags, thislen);
407 408
	if (ret)
		return ret;
409 410 411

	sg_init_table(desc->frags, 4);

412
	if (fraglen) {
413 414
		sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
				sg->offset + sg->length - fraglen);
415 416 417 418 419 420 421 422 423 424
		desc->fragno = 1;
		desc->fraglen = fraglen;
	} else {
		desc->fragno = 0;
		desc->fraglen = 0;
	}
	return 0;
}

int
425 426
gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
		    int offset)
427 428 429 430
{
	struct decryptor_desc desc;

	/* XXXJBF: */
431
	BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
432 433

	memset(desc.iv, 0, sizeof(desc.iv));
434 435 436
	desc.desc.tfm = tfm;
	desc.desc.info = desc.iv;
	desc.desc.flags = 0;
437 438
	desc.fragno = 0;
	desc.fraglen = 0;
439 440 441

	sg_init_table(desc.frags, 4);

O
Olga Kornievskaia 已提交
442
	return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
443
}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

/*
 * This function makes the assumption that it was ultimately called
 * from gss_wrap().
 *
 * The client auth_gss code moves any existing tail data into a
 * separate page before calling gss_wrap.
 * The server svcauth_gss code ensures that both the head and the
 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
 *
 * Even with that guarantee, this function may be called more than
 * once in the processing of gss_wrap().  The best we can do is
 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
 * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
 * At run-time we can verify that a single invocation of this
 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
 */

int
xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
{
	u8 *p;

	if (shiftlen == 0)
		return 0;

	BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
	BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);

	p = buf->head[0].iov_base + base;

	memmove(p + shiftlen, p, buf->head[0].iov_len - base);

	buf->head[0].iov_len += shiftlen;
	buf->len += shiftlen;

	return 0;
}
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

static u32
gss_krb5_cts_crypt(struct crypto_blkcipher *cipher, struct xdr_buf *buf,
		   u32 offset, u8 *iv, struct page **pages, int encrypt)
{
	u32 ret;
	struct scatterlist sg[1];
	struct blkcipher_desc desc = { .tfm = cipher, .info = iv };
	u8 data[crypto_blkcipher_blocksize(cipher) * 2];
	struct page **save_pages;
	u32 len = buf->len - offset;

	BUG_ON(len > crypto_blkcipher_blocksize(cipher) * 2);

	/*
	 * For encryption, we want to read from the cleartext
	 * page cache pages, and write the encrypted data to
	 * the supplied xdr_buf pages.
	 */
	save_pages = buf->pages;
	if (encrypt)
		buf->pages = pages;

	ret = read_bytes_from_xdr_buf(buf, offset, data, len);
	buf->pages = save_pages;
	if (ret)
		goto out;

	sg_init_one(sg, data, len);

	if (encrypt)
		ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
	else
		ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, len);

	if (ret)
		goto out;

	ret = write_bytes_to_xdr_buf(buf, offset, data, len);

out:
	return ret;
}

u32
gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
		     struct xdr_buf *buf, int ec, struct page **pages)
{
	u32 err;
	struct xdr_netobj hmac;
	u8 *cksumkey;
	u8 *ecptr;
	struct crypto_blkcipher *cipher, *aux_cipher;
	int blocksize;
	struct page **save_pages;
	int nblocks, nbytes;
	struct encryptor_desc desc;
	u32 cbcbytes;
K
Kevin Coffman 已提交
540
	unsigned int usage;
541 542 543 544 545

	if (kctx->initiate) {
		cipher = kctx->initiator_enc;
		aux_cipher = kctx->initiator_enc_aux;
		cksumkey = kctx->initiator_integ;
K
Kevin Coffman 已提交
546
		usage = KG_USAGE_INITIATOR_SEAL;
547 548 549 550
	} else {
		cipher = kctx->acceptor_enc;
		aux_cipher = kctx->acceptor_enc_aux;
		cksumkey = kctx->acceptor_integ;
K
Kevin Coffman 已提交
551
		usage = KG_USAGE_ACCEPTOR_SEAL;
552 553 554 555 556
	}
	blocksize = crypto_blkcipher_blocksize(cipher);

	/* hide the gss token header and insert the confounder */
	offset += GSS_KRB5_TOK_HDR_LEN;
557
	if (xdr_extend_head(buf, offset, kctx->gk5e->conflen))
558
		return GSS_S_FAILURE;
559
	gss_krb5_make_confounder(buf->head[0].iov_base + offset, kctx->gk5e->conflen);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	offset -= GSS_KRB5_TOK_HDR_LEN;

	if (buf->tail[0].iov_base != NULL) {
		ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
	} else {
		buf->tail[0].iov_base = buf->head[0].iov_base
							+ buf->head[0].iov_len;
		buf->tail[0].iov_len = 0;
		ecptr = buf->tail[0].iov_base;
	}

	memset(ecptr, 'X', ec);
	buf->tail[0].iov_len += ec;
	buf->len += ec;

	/* copy plaintext gss token header after filler (if any) */
	memcpy(ecptr + ec, buf->head[0].iov_base + offset,
						GSS_KRB5_TOK_HDR_LEN);
	buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
	buf->len += GSS_KRB5_TOK_HDR_LEN;

	/* Do the HMAC */
	hmac.len = GSS_KRB5_MAX_CKSUM_LEN;
	hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;

	/*
	 * When we are called, pages points to the real page cache
	 * data -- which we can't go and encrypt!  buf->pages points
	 * to scratch pages which we are going to send off to the
	 * client/server.  Swap in the plaintext pages to calculate
	 * the hmac.
	 */
	save_pages = buf->pages;
	buf->pages = pages;

	err = make_checksum_v2(kctx, NULL, 0, buf,
K
Kevin Coffman 已提交
596 597
			       offset + GSS_KRB5_TOK_HDR_LEN,
			       cksumkey, usage, &hmac);
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	buf->pages = save_pages;
	if (err)
		return GSS_S_FAILURE;

	nbytes = buf->len - offset - GSS_KRB5_TOK_HDR_LEN;
	nblocks = (nbytes + blocksize - 1) / blocksize;
	cbcbytes = 0;
	if (nblocks > 2)
		cbcbytes = (nblocks - 2) * blocksize;

	memset(desc.iv, 0, sizeof(desc.iv));

	if (cbcbytes) {
		desc.pos = offset + GSS_KRB5_TOK_HDR_LEN;
		desc.fragno = 0;
		desc.fraglen = 0;
		desc.pages = pages;
		desc.outbuf = buf;
		desc.desc.info = desc.iv;
		desc.desc.flags = 0;
		desc.desc.tfm = aux_cipher;

		sg_init_table(desc.infrags, 4);
		sg_init_table(desc.outfrags, 4);

		err = xdr_process_buf(buf, offset + GSS_KRB5_TOK_HDR_LEN,
				      cbcbytes, encryptor, &desc);
		if (err)
			goto out_err;
	}

	/* Make sure IV carries forward from any CBC results. */
	err = gss_krb5_cts_crypt(cipher, buf,
				 offset + GSS_KRB5_TOK_HDR_LEN + cbcbytes,
				 desc.iv, pages, 1);
	if (err) {
		err = GSS_S_FAILURE;
		goto out_err;
	}

	/* Now update buf to account for HMAC */
	buf->tail[0].iov_len += kctx->gk5e->cksumlength;
	buf->len += kctx->gk5e->cksumlength;

out_err:
	if (err)
		err = GSS_S_FAILURE;
	return err;
}

u32
gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf,
		     u32 *headskip, u32 *tailskip)
{
	struct xdr_buf subbuf;
	u32 ret = 0;
	u8 *cksum_key;
	struct crypto_blkcipher *cipher, *aux_cipher;
	struct xdr_netobj our_hmac_obj;
	u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
	u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
	int nblocks, blocksize, cbcbytes;
	struct decryptor_desc desc;
K
Kevin Coffman 已提交
661
	unsigned int usage;
662 663 664 665 666

	if (kctx->initiate) {
		cipher = kctx->acceptor_enc;
		aux_cipher = kctx->acceptor_enc_aux;
		cksum_key = kctx->acceptor_integ;
K
Kevin Coffman 已提交
667
		usage = KG_USAGE_ACCEPTOR_SEAL;
668 669 670 671
	} else {
		cipher = kctx->initiator_enc;
		aux_cipher = kctx->initiator_enc_aux;
		cksum_key = kctx->initiator_integ;
K
Kevin Coffman 已提交
672
		usage = KG_USAGE_INITIATOR_SEAL;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	}
	blocksize = crypto_blkcipher_blocksize(cipher);


	/* create a segment skipping the header and leaving out the checksum */
	xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
				    (buf->len - offset - GSS_KRB5_TOK_HDR_LEN -
				     kctx->gk5e->cksumlength));

	nblocks = (subbuf.len + blocksize - 1) / blocksize;

	cbcbytes = 0;
	if (nblocks > 2)
		cbcbytes = (nblocks - 2) * blocksize;

	memset(desc.iv, 0, sizeof(desc.iv));

	if (cbcbytes) {
		desc.fragno = 0;
		desc.fraglen = 0;
		desc.desc.info = desc.iv;
		desc.desc.flags = 0;
		desc.desc.tfm = aux_cipher;

		sg_init_table(desc.frags, 4);

		ret = xdr_process_buf(&subbuf, 0, cbcbytes, decryptor, &desc);
		if (ret)
			goto out_err;
	}

	/* Make sure IV carries forward from any CBC results. */
	ret = gss_krb5_cts_crypt(cipher, &subbuf, cbcbytes, desc.iv, NULL, 0);
	if (ret)
		goto out_err;


	/* Calculate our hmac over the plaintext data */
	our_hmac_obj.len = sizeof(our_hmac);
	our_hmac_obj.data = our_hmac;

	ret = make_checksum_v2(kctx, NULL, 0, &subbuf, 0,
K
Kevin Coffman 已提交
715
			       cksum_key, usage, &our_hmac_obj);
716 717 718 719 720 721 722 723 724 725 726 727 728
	if (ret)
		goto out_err;

	/* Get the packet's hmac value */
	ret = read_bytes_from_xdr_buf(buf, buf->len - kctx->gk5e->cksumlength,
				      pkt_hmac, kctx->gk5e->cksumlength);
	if (ret)
		goto out_err;

	if (memcmp(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
		ret = GSS_S_BAD_SIG;
		goto out_err;
	}
729
	*headskip = kctx->gk5e->conflen;
730 731 732 733 734 735
	*tailskip = kctx->gk5e->cksumlength;
out_err:
	if (ret && ret != GSS_S_BAD_SIG)
		ret = GSS_S_FAILURE;
	return ret;
}