Linux Audio

Check our new training course

Loading...
v6.8
   1/*
   2 *  linux/net/sunrpc/gss_krb5_crypto.c
   3 *
   4 *  Copyright (c) 2000-2008 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
  37#include <crypto/hash.h>
  38#include <crypto/skcipher.h>
  39#include <crypto/utils.h>
  40#include <linux/err.h>
  41#include <linux/types.h>
  42#include <linux/mm.h>
  43#include <linux/scatterlist.h>
  44#include <linux/highmem.h>
  45#include <linux/pagemap.h>
  46#include <linux/random.h>
  47#include <linux/sunrpc/gss_krb5.h>
  48#include <linux/sunrpc/xdr.h>
  49#include <kunit/visibility.h>
  50
  51#include "gss_krb5_internal.h"
  52
  53#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  54# define RPCDBG_FACILITY        RPCDBG_AUTH
  55#endif
  56
  57/**
  58 * krb5_make_confounder - Generate a confounder string
  59 * @p: memory location into which to write the string
  60 * @conflen: string length to write, in octets
  61 *
  62 * RFCs 1964 and 3961 mention only "a random confounder" without going
  63 * into detail about its function or cryptographic requirements. The
  64 * assumed purpose is to prevent repeated encryption of a plaintext with
  65 * the same key from generating the same ciphertext. It is also used to
  66 * pad minimum plaintext length to at least a single cipher block.
  67 *
  68 * However, in situations like the GSS Kerberos 5 mechanism, where the
  69 * encryption IV is always all zeroes, the confounder also effectively
  70 * functions like an IV. Thus, not only must it be unique from message
  71 * to message, but it must also be difficult to predict. Otherwise an
  72 * attacker can correlate the confounder to previous or future values,
  73 * making the encryption easier to break.
  74 *
  75 * Given that the primary consumer of this encryption mechanism is a
  76 * network storage protocol, a type of traffic that often carries
  77 * predictable payloads (eg, all zeroes when reading unallocated blocks
  78 * from a file), our confounder generation has to be cryptographically
  79 * strong.
  80 */
  81void krb5_make_confounder(u8 *p, int conflen)
  82{
  83	get_random_bytes(p, conflen);
  84}
  85
  86/**
  87 * krb5_encrypt - simple encryption of an RPCSEC GSS payload
  88 * @tfm: initialized cipher transform
  89 * @iv: pointer to an IV
  90 * @in: plaintext to encrypt
  91 * @out: OUT: ciphertext
  92 * @length: length of input and output buffers, in bytes
  93 *
  94 * @iv may be NULL to force the use of an all-zero IV.
  95 * The buffer containing the IV must be as large as the
  96 * cipher's ivsize.
  97 *
  98 * Return values:
  99 *   %0: @in successfully encrypted into @out
 100 *   negative errno: @in not encrypted
 101 */
 102u32
 103krb5_encrypt(
 104	struct crypto_sync_skcipher *tfm,
 105	void * iv,
 106	void * in,
 107	void * out,
 108	int length)
 109{
 110	u32 ret = -EINVAL;
 111	struct scatterlist sg[1];
 112	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
 113	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
 114
 115	if (length % crypto_sync_skcipher_blocksize(tfm) != 0)
 116		goto out;
 117
 118	if (crypto_sync_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
 119		dprintk("RPC:       gss_k5encrypt: tfm iv size too large %d\n",
 120			crypto_sync_skcipher_ivsize(tfm));
 121		goto out;
 122	}
 123
 124	if (iv)
 125		memcpy(local_iv, iv, crypto_sync_skcipher_ivsize(tfm));
 126
 127	memcpy(out, in, length);
 128	sg_init_one(sg, out, length);
 129
 130	skcipher_request_set_sync_tfm(req, tfm);
 131	skcipher_request_set_callback(req, 0, NULL, NULL);
 132	skcipher_request_set_crypt(req, sg, sg, length, local_iv);
 133
 134	ret = crypto_skcipher_encrypt(req);
 135	skcipher_request_zero(req);
 136out:
 137	dprintk("RPC:       krb5_encrypt returns %d\n", ret);
 138	return ret;
 139}
 140
 141/**
 142 * krb5_decrypt - simple decryption of an RPCSEC GSS payload
 143 * @tfm: initialized cipher transform
 144 * @iv: pointer to an IV
 145 * @in: ciphertext to decrypt
 146 * @out: OUT: plaintext
 147 * @length: length of input and output buffers, in bytes
 148 *
 149 * @iv may be NULL to force the use of an all-zero IV.
 150 * The buffer containing the IV must be as large as the
 151 * cipher's ivsize.
 152 *
 153 * Return values:
 154 *   %0: @in successfully decrypted into @out
 155 *   negative errno: @in not decrypted
 156 */
 157u32
 158krb5_decrypt(
 159     struct crypto_sync_skcipher *tfm,
 160     void * iv,
 161     void * in,
 162     void * out,
 163     int length)
 164{
 165	u32 ret = -EINVAL;
 166	struct scatterlist sg[1];
 167	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
 168	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
 169
 170	if (length % crypto_sync_skcipher_blocksize(tfm) != 0)
 171		goto out;
 172
 173	if (crypto_sync_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
 174		dprintk("RPC:       gss_k5decrypt: tfm iv size too large %d\n",
 175			crypto_sync_skcipher_ivsize(tfm));
 176		goto out;
 177	}
 178	if (iv)
 179		memcpy(local_iv, iv, crypto_sync_skcipher_ivsize(tfm));
 180
 181	memcpy(out, in, length);
 182	sg_init_one(sg, out, length);
 183
 184	skcipher_request_set_sync_tfm(req, tfm);
 185	skcipher_request_set_callback(req, 0, NULL, NULL);
 186	skcipher_request_set_crypt(req, sg, sg, length, local_iv);
 187
 188	ret = crypto_skcipher_decrypt(req);
 189	skcipher_request_zero(req);
 190out:
 191	dprintk("RPC:       gss_k5decrypt returns %d\n",ret);
 192	return ret;
 193}
 194
 195static int
 196checksummer(struct scatterlist *sg, void *data)
 197{
 198	struct ahash_request *req = data;
 199
 200	ahash_request_set_crypt(req, sg, NULL, sg->length);
 201
 202	return crypto_ahash_update(req);
 203}
 204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 205/*
 206 * checksum the plaintext data and hdrlen bytes of the token header
 207 * The checksum is performed over the first 8 bytes of the
 208 * gss token header and then over the data body
 209 */
 210u32
 211make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
 212	      struct xdr_buf *body, int body_offset, u8 *cksumkey,
 213	      unsigned int usage, struct xdr_netobj *cksumout)
 214{
 215	struct crypto_ahash *tfm;
 216	struct ahash_request *req;
 217	struct scatterlist              sg[1];
 218	int err = -1;
 219	u8 *checksumdata;
 220	unsigned int checksumlen;
 221
 
 
 
 
 
 222	if (cksumout->len < kctx->gk5e->cksumlength) {
 223		dprintk("%s: checksum buffer length, %u, too small for %s\n",
 224			__func__, cksumout->len, kctx->gk5e->name);
 225		return GSS_S_FAILURE;
 226	}
 227
 228	checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_KERNEL);
 229	if (checksumdata == NULL)
 230		return GSS_S_FAILURE;
 231
 232	tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
 233	if (IS_ERR(tfm))
 234		goto out_free_cksum;
 235
 236	req = ahash_request_alloc(tfm, GFP_KERNEL);
 237	if (!req)
 238		goto out_free_ahash;
 239
 240	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 241
 242	checksumlen = crypto_ahash_digestsize(tfm);
 243
 244	if (cksumkey != NULL) {
 245		err = crypto_ahash_setkey(tfm, cksumkey,
 246					  kctx->gk5e->keylength);
 247		if (err)
 248			goto out;
 249	}
 250
 251	err = crypto_ahash_init(req);
 252	if (err)
 253		goto out;
 254	sg_init_one(sg, header, hdrlen);
 255	ahash_request_set_crypt(req, sg, NULL, hdrlen);
 256	err = crypto_ahash_update(req);
 257	if (err)
 258		goto out;
 259	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 260			      checksummer, req);
 261	if (err)
 262		goto out;
 263	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 264	err = crypto_ahash_final(req);
 265	if (err)
 266		goto out;
 267
 268	switch (kctx->gk5e->ctype) {
 269	case CKSUMTYPE_RSA_MD5:
 270		err = krb5_encrypt(kctx->seq, NULL, checksumdata,
 271				   checksumdata, checksumlen);
 272		if (err)
 273			goto out;
 274		memcpy(cksumout->data,
 275		       checksumdata + checksumlen - kctx->gk5e->cksumlength,
 276		       kctx->gk5e->cksumlength);
 277		break;
 278	case CKSUMTYPE_HMAC_SHA1_DES3:
 279		memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
 280		break;
 281	default:
 282		BUG();
 283		break;
 284	}
 285	cksumout->len = kctx->gk5e->cksumlength;
 286out:
 287	ahash_request_free(req);
 288out_free_ahash:
 289	crypto_free_ahash(tfm);
 290out_free_cksum:
 291	kfree(checksumdata);
 292	return err ? GSS_S_FAILURE : 0;
 293}
 294
 295/**
 296 * gss_krb5_checksum - Compute the MAC for a GSS Wrap or MIC token
 297 * @tfm: an initialized hash transform
 298 * @header: pointer to a buffer containing the token header, or NULL
 299 * @hdrlen: number of octets in @header
 300 * @body: xdr_buf containing an RPC message (body.len is the message length)
 301 * @body_offset: byte offset into @body to start checksumming
 302 * @cksumout: OUT: a buffer to be filled in with the computed HMAC
 303 *
 304 * Usually expressed as H = HMAC(K, message)[1..h] .
 305 *
 306 * Caller provides the truncation length of the output token (h) in
 307 * cksumout.len.
 308 *
 309 * Return values:
 310 *   %GSS_S_COMPLETE: Digest computed, @cksumout filled in
 311 *   %GSS_S_FAILURE: Call failed
 312 */
 313u32
 314gss_krb5_checksum(struct crypto_ahash *tfm, char *header, int hdrlen,
 315		  const struct xdr_buf *body, int body_offset,
 316		  struct xdr_netobj *cksumout)
 317{
 
 318	struct ahash_request *req;
 319	int err = -ENOMEM;
 
 320	u8 *checksumdata;
 
 321
 322	checksumdata = kmalloc(crypto_ahash_digestsize(tfm), GFP_KERNEL);
 
 
 
 
 
 
 
 
 
 
 
 323	if (!checksumdata)
 324		return GSS_S_FAILURE;
 325
 326	req = ahash_request_alloc(tfm, GFP_KERNEL);
 327	if (!req)
 328		goto out_free_cksum;
 
 
 
 
 
 
 329	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 330	err = crypto_ahash_init(req);
 
 331	if (err)
 332		goto out_free_ahash;
 333
 334	/*
 335	 * Per RFC 4121 Section 4.2.4, the checksum is performed over the
 336	 * data body first, then over the octets in "header".
 337	 */
 338	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 339			      checksummer, req);
 340	if (err)
 341		goto out_free_ahash;
 342	if (header) {
 343		struct scatterlist sg[1];
 344
 345		sg_init_one(sg, header, hdrlen);
 346		ahash_request_set_crypt(req, sg, NULL, hdrlen);
 347		err = crypto_ahash_update(req);
 348		if (err)
 349			goto out_free_ahash;
 350	}
 351
 352	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 353	err = crypto_ahash_final(req);
 354	if (err)
 355		goto out_free_ahash;
 356
 357	memcpy(cksumout->data, checksumdata,
 358	       min_t(int, cksumout->len, crypto_ahash_digestsize(tfm)));
 359
 360out_free_ahash:
 
 
 
 
 
 
 
 
 
 
 361	ahash_request_free(req);
 
 
 362out_free_cksum:
 363	kfree_sensitive(checksumdata);
 364	return err ? GSS_S_FAILURE : GSS_S_COMPLETE;
 365}
 366EXPORT_SYMBOL_IF_KUNIT(gss_krb5_checksum);
 367
 368struct encryptor_desc {
 369	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
 370	struct skcipher_request *req;
 371	int pos;
 372	struct xdr_buf *outbuf;
 373	struct page **pages;
 374	struct scatterlist infrags[4];
 375	struct scatterlist outfrags[4];
 376	int fragno;
 377	int fraglen;
 378};
 379
 380static int
 381encryptor(struct scatterlist *sg, void *data)
 382{
 383	struct encryptor_desc *desc = data;
 384	struct xdr_buf *outbuf = desc->outbuf;
 385	struct crypto_sync_skcipher *tfm =
 386		crypto_sync_skcipher_reqtfm(desc->req);
 387	struct page *in_page;
 388	int thislen = desc->fraglen + sg->length;
 389	int fraglen, ret;
 390	int page_pos;
 391
 392	/* Worst case is 4 fragments: head, end of page 1, start
 393	 * of page 2, tail.  Anything more is a bug. */
 394	BUG_ON(desc->fragno > 3);
 395
 396	page_pos = desc->pos - outbuf->head[0].iov_len;
 397	if (page_pos >= 0 && page_pos < outbuf->page_len) {
 398		/* pages are not in place: */
 399		int i = (page_pos + outbuf->page_base) >> PAGE_SHIFT;
 400		in_page = desc->pages[i];
 401	} else {
 402		in_page = sg_page(sg);
 403	}
 404	sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
 405		    sg->offset);
 406	sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
 407		    sg->offset);
 408	desc->fragno++;
 409	desc->fraglen += sg->length;
 410	desc->pos += sg->length;
 411
 412	fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1);
 413	thislen -= fraglen;
 414
 415	if (thislen == 0)
 416		return 0;
 417
 418	sg_mark_end(&desc->infrags[desc->fragno - 1]);
 419	sg_mark_end(&desc->outfrags[desc->fragno - 1]);
 420
 421	skcipher_request_set_crypt(desc->req, desc->infrags, desc->outfrags,
 422				   thislen, desc->iv);
 423
 424	ret = crypto_skcipher_encrypt(desc->req);
 425	if (ret)
 426		return ret;
 427
 428	sg_init_table(desc->infrags, 4);
 429	sg_init_table(desc->outfrags, 4);
 430
 431	if (fraglen) {
 432		sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
 433				sg->offset + sg->length - fraglen);
 434		desc->infrags[0] = desc->outfrags[0];
 435		sg_assign_page(&desc->infrags[0], in_page);
 436		desc->fragno = 1;
 437		desc->fraglen = fraglen;
 438	} else {
 439		desc->fragno = 0;
 440		desc->fraglen = 0;
 441	}
 442	return 0;
 443}
 444
 445int
 446gss_encrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf,
 447		    int offset, struct page **pages)
 448{
 449	int ret;
 450	struct encryptor_desc desc;
 451	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
 452
 453	BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0);
 454
 455	skcipher_request_set_sync_tfm(req, tfm);
 456	skcipher_request_set_callback(req, 0, NULL, NULL);
 457
 458	memset(desc.iv, 0, sizeof(desc.iv));
 459	desc.req = req;
 460	desc.pos = offset;
 461	desc.outbuf = buf;
 462	desc.pages = pages;
 463	desc.fragno = 0;
 464	desc.fraglen = 0;
 465
 466	sg_init_table(desc.infrags, 4);
 467	sg_init_table(desc.outfrags, 4);
 468
 469	ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
 470	skcipher_request_zero(req);
 471	return ret;
 472}
 473
 474struct decryptor_desc {
 475	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
 476	struct skcipher_request *req;
 477	struct scatterlist frags[4];
 478	int fragno;
 479	int fraglen;
 480};
 481
 482static int
 483decryptor(struct scatterlist *sg, void *data)
 484{
 485	struct decryptor_desc *desc = data;
 486	int thislen = desc->fraglen + sg->length;
 487	struct crypto_sync_skcipher *tfm =
 488		crypto_sync_skcipher_reqtfm(desc->req);
 489	int fraglen, ret;
 490
 491	/* Worst case is 4 fragments: head, end of page 1, start
 492	 * of page 2, tail.  Anything more is a bug. */
 493	BUG_ON(desc->fragno > 3);
 494	sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
 495		    sg->offset);
 496	desc->fragno++;
 497	desc->fraglen += sg->length;
 498
 499	fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1);
 500	thislen -= fraglen;
 501
 502	if (thislen == 0)
 503		return 0;
 504
 505	sg_mark_end(&desc->frags[desc->fragno - 1]);
 506
 507	skcipher_request_set_crypt(desc->req, desc->frags, desc->frags,
 508				   thislen, desc->iv);
 509
 510	ret = crypto_skcipher_decrypt(desc->req);
 511	if (ret)
 512		return ret;
 513
 514	sg_init_table(desc->frags, 4);
 515
 516	if (fraglen) {
 517		sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
 518				sg->offset + sg->length - fraglen);
 519		desc->fragno = 1;
 520		desc->fraglen = fraglen;
 521	} else {
 522		desc->fragno = 0;
 523		desc->fraglen = 0;
 524	}
 525	return 0;
 526}
 527
 528int
 529gss_decrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf,
 530		    int offset)
 531{
 532	int ret;
 533	struct decryptor_desc desc;
 534	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
 535
 536	/* XXXJBF: */
 537	BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0);
 538
 539	skcipher_request_set_sync_tfm(req, tfm);
 540	skcipher_request_set_callback(req, 0, NULL, NULL);
 541
 542	memset(desc.iv, 0, sizeof(desc.iv));
 543	desc.req = req;
 544	desc.fragno = 0;
 545	desc.fraglen = 0;
 546
 547	sg_init_table(desc.frags, 4);
 548
 549	ret = xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
 550	skcipher_request_zero(req);
 551	return ret;
 552}
 553
 554/*
 555 * This function makes the assumption that it was ultimately called
 556 * from gss_wrap().
 557 *
 558 * The client auth_gss code moves any existing tail data into a
 559 * separate page before calling gss_wrap.
 560 * The server svcauth_gss code ensures that both the head and the
 561 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
 562 *
 563 * Even with that guarantee, this function may be called more than
 564 * once in the processing of gss_wrap().  The best we can do is
 565 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
 566 * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
 567 * At run-time we can verify that a single invocation of this
 568 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
 569 */
 570
 571int
 572xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
 573{
 574	u8 *p;
 575
 576	if (shiftlen == 0)
 577		return 0;
 578
 
 579	BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
 580
 581	p = buf->head[0].iov_base + base;
 582
 583	memmove(p + shiftlen, p, buf->head[0].iov_len - base);
 584
 585	buf->head[0].iov_len += shiftlen;
 586	buf->len += shiftlen;
 587
 588	return 0;
 589}
 590
 591static u32
 592gss_krb5_cts_crypt(struct crypto_sync_skcipher *cipher, struct xdr_buf *buf,
 593		   u32 offset, u8 *iv, struct page **pages, int encrypt)
 594{
 595	u32 ret;
 596	struct scatterlist sg[1];
 597	SYNC_SKCIPHER_REQUEST_ON_STACK(req, cipher);
 598	u8 *data;
 599	struct page **save_pages;
 600	u32 len = buf->len - offset;
 601
 602	if (len > GSS_KRB5_MAX_BLOCKSIZE * 2) {
 603		WARN_ON(0);
 604		return -ENOMEM;
 605	}
 606	data = kmalloc(GSS_KRB5_MAX_BLOCKSIZE * 2, GFP_KERNEL);
 607	if (!data)
 608		return -ENOMEM;
 609
 610	/*
 611	 * For encryption, we want to read from the cleartext
 612	 * page cache pages, and write the encrypted data to
 613	 * the supplied xdr_buf pages.
 614	 */
 615	save_pages = buf->pages;
 616	if (encrypt)
 617		buf->pages = pages;
 618
 619	ret = read_bytes_from_xdr_buf(buf, offset, data, len);
 620	buf->pages = save_pages;
 621	if (ret)
 622		goto out;
 623
 624	sg_init_one(sg, data, len);
 625
 626	skcipher_request_set_sync_tfm(req, cipher);
 627	skcipher_request_set_callback(req, 0, NULL, NULL);
 628	skcipher_request_set_crypt(req, sg, sg, len, iv);
 629
 630	if (encrypt)
 631		ret = crypto_skcipher_encrypt(req);
 632	else
 633		ret = crypto_skcipher_decrypt(req);
 634
 635	skcipher_request_zero(req);
 636
 637	if (ret)
 638		goto out;
 639
 640	ret = write_bytes_to_xdr_buf(buf, offset, data, len);
 641
 642#if IS_ENABLED(CONFIG_KUNIT)
 643	/*
 644	 * CBC-CTS does not define an output IV but RFC 3962 defines it as the
 645	 * penultimate block of ciphertext, so copy that into the IV buffer
 646	 * before returning.
 647	 */
 648	if (encrypt)
 649		memcpy(iv, data, crypto_sync_skcipher_ivsize(cipher));
 650#endif
 651
 652out:
 653	kfree(data);
 654	return ret;
 655}
 656
 657/**
 658 * krb5_cbc_cts_encrypt - encrypt in CBC mode with CTS
 659 * @cts_tfm: CBC cipher with CTS
 660 * @cbc_tfm: base CBC cipher
 661 * @offset: starting byte offset for plaintext
 662 * @buf: OUT: output buffer
 663 * @pages: plaintext
 664 * @iv: output CBC initialization vector, or NULL
 665 * @ivsize: size of @iv, in octets
 666 *
 667 * To provide confidentiality, encrypt using cipher block chaining
 668 * with ciphertext stealing. Message integrity is handled separately.
 669 *
 670 * Return values:
 671 *   %0: encryption successful
 672 *   negative errno: encryption could not be completed
 673 */
 674VISIBLE_IF_KUNIT
 675int krb5_cbc_cts_encrypt(struct crypto_sync_skcipher *cts_tfm,
 676			 struct crypto_sync_skcipher *cbc_tfm,
 677			 u32 offset, struct xdr_buf *buf, struct page **pages,
 678			 u8 *iv, unsigned int ivsize)
 679{
 680	u32 blocksize, nbytes, nblocks, cbcbytes;
 681	struct encryptor_desc desc;
 682	int err;
 683
 684	blocksize = crypto_sync_skcipher_blocksize(cts_tfm);
 685	nbytes = buf->len - offset;
 686	nblocks = (nbytes + blocksize - 1) / blocksize;
 687	cbcbytes = 0;
 688	if (nblocks > 2)
 689		cbcbytes = (nblocks - 2) * blocksize;
 690
 691	memset(desc.iv, 0, sizeof(desc.iv));
 692
 693	/* Handle block-sized chunks of plaintext with CBC. */
 694	if (cbcbytes) {
 695		SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm);
 696
 697		desc.pos = offset;
 698		desc.fragno = 0;
 699		desc.fraglen = 0;
 700		desc.pages = pages;
 701		desc.outbuf = buf;
 702		desc.req = req;
 703
 704		skcipher_request_set_sync_tfm(req, cbc_tfm);
 705		skcipher_request_set_callback(req, 0, NULL, NULL);
 706
 707		sg_init_table(desc.infrags, 4);
 708		sg_init_table(desc.outfrags, 4);
 709
 710		err = xdr_process_buf(buf, offset, cbcbytes, encryptor, &desc);
 711		skcipher_request_zero(req);
 712		if (err)
 713			return err;
 714	}
 715
 716	/* Remaining plaintext is handled with CBC-CTS. */
 717	err = gss_krb5_cts_crypt(cts_tfm, buf, offset + cbcbytes,
 718				 desc.iv, pages, 1);
 719	if (err)
 720		return err;
 721
 722	if (unlikely(iv))
 723		memcpy(iv, desc.iv, ivsize);
 724	return 0;
 725}
 726EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_encrypt);
 727
 728/**
 729 * krb5_cbc_cts_decrypt - decrypt in CBC mode with CTS
 730 * @cts_tfm: CBC cipher with CTS
 731 * @cbc_tfm: base CBC cipher
 732 * @offset: starting byte offset for plaintext
 733 * @buf: OUT: output buffer
 734 *
 735 * Return values:
 736 *   %0: decryption successful
 737 *   negative errno: decryption could not be completed
 738 */
 739VISIBLE_IF_KUNIT
 740int krb5_cbc_cts_decrypt(struct crypto_sync_skcipher *cts_tfm,
 741			 struct crypto_sync_skcipher *cbc_tfm,
 742			 u32 offset, struct xdr_buf *buf)
 743{
 744	u32 blocksize, nblocks, cbcbytes;
 745	struct decryptor_desc desc;
 746	int err;
 747
 748	blocksize = crypto_sync_skcipher_blocksize(cts_tfm);
 749	nblocks = (buf->len + blocksize - 1) / blocksize;
 750	cbcbytes = 0;
 751	if (nblocks > 2)
 752		cbcbytes = (nblocks - 2) * blocksize;
 753
 754	memset(desc.iv, 0, sizeof(desc.iv));
 755
 756	/* Handle block-sized chunks of plaintext with CBC. */
 757	if (cbcbytes) {
 758		SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm);
 759
 760		desc.fragno = 0;
 761		desc.fraglen = 0;
 762		desc.req = req;
 763
 764		skcipher_request_set_sync_tfm(req, cbc_tfm);
 765		skcipher_request_set_callback(req, 0, NULL, NULL);
 766
 767		sg_init_table(desc.frags, 4);
 768
 769		err = xdr_process_buf(buf, 0, cbcbytes, decryptor, &desc);
 770		skcipher_request_zero(req);
 771		if (err)
 772			return err;
 773	}
 774
 775	/* Remaining plaintext is handled with CBC-CTS. */
 776	return gss_krb5_cts_crypt(cts_tfm, buf, cbcbytes, desc.iv, NULL, 0);
 777}
 778EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_decrypt);
 779
 780u32
 781gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
 782		     struct xdr_buf *buf, struct page **pages)
 783{
 784	u32 err;
 785	struct xdr_netobj hmac;
 
 786	u8 *ecptr;
 787	struct crypto_sync_skcipher *cipher, *aux_cipher;
 788	struct crypto_ahash *ahash;
 789	struct page **save_pages;
 790	unsigned int conflen;
 
 
 
 791
 792	if (kctx->initiate) {
 793		cipher = kctx->initiator_enc;
 794		aux_cipher = kctx->initiator_enc_aux;
 795		ahash = kctx->initiator_integ;
 
 796	} else {
 797		cipher = kctx->acceptor_enc;
 798		aux_cipher = kctx->acceptor_enc_aux;
 799		ahash = kctx->acceptor_integ;
 
 800	}
 801	conflen = crypto_sync_skcipher_blocksize(cipher);
 802
 803	/* hide the gss token header and insert the confounder */
 804	offset += GSS_KRB5_TOK_HDR_LEN;
 805	if (xdr_extend_head(buf, offset, conflen))
 806		return GSS_S_FAILURE;
 807	krb5_make_confounder(buf->head[0].iov_base + offset, conflen);
 808	offset -= GSS_KRB5_TOK_HDR_LEN;
 809
 810	if (buf->tail[0].iov_base != NULL) {
 811		ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
 812	} else {
 813		buf->tail[0].iov_base = buf->head[0].iov_base
 814							+ buf->head[0].iov_len;
 815		buf->tail[0].iov_len = 0;
 816		ecptr = buf->tail[0].iov_base;
 817	}
 818
 819	/* copy plaintext gss token header after filler (if any) */
 820	memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
 821	buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
 822	buf->len += GSS_KRB5_TOK_HDR_LEN;
 823
 824	hmac.len = kctx->gk5e->cksumlength;
 
 825	hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
 826
 827	/*
 828	 * When we are called, pages points to the real page cache
 829	 * data -- which we can't go and encrypt!  buf->pages points
 830	 * to scratch pages which we are going to send off to the
 831	 * client/server.  Swap in the plaintext pages to calculate
 832	 * the hmac.
 833	 */
 834	save_pages = buf->pages;
 835	buf->pages = pages;
 836
 837	err = gss_krb5_checksum(ahash, NULL, 0, buf,
 838				offset + GSS_KRB5_TOK_HDR_LEN, &hmac);
 
 839	buf->pages = save_pages;
 840	if (err)
 841		return GSS_S_FAILURE;
 842
 843	err = krb5_cbc_cts_encrypt(cipher, aux_cipher,
 844				   offset + GSS_KRB5_TOK_HDR_LEN,
 845				   buf, pages, NULL, 0);
 846	if (err)
 847		return GSS_S_FAILURE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 848
 849	/* Now update buf to account for HMAC */
 850	buf->tail[0].iov_len += kctx->gk5e->cksumlength;
 851	buf->len += kctx->gk5e->cksumlength;
 852
 853	return GSS_S_COMPLETE;
 
 
 
 854}
 855
 856u32
 857gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
 858		     struct xdr_buf *buf, u32 *headskip, u32 *tailskip)
 859{
 860	struct crypto_sync_skcipher *cipher, *aux_cipher;
 861	struct crypto_ahash *ahash;
 
 
 862	struct xdr_netobj our_hmac_obj;
 863	u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
 864	u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
 865	struct xdr_buf subbuf;
 866	u32 ret = 0;
 
 867
 868	if (kctx->initiate) {
 869		cipher = kctx->acceptor_enc;
 870		aux_cipher = kctx->acceptor_enc_aux;
 871		ahash = kctx->acceptor_integ;
 
 872	} else {
 873		cipher = kctx->initiator_enc;
 874		aux_cipher = kctx->initiator_enc_aux;
 875		ahash = kctx->initiator_integ;
 
 876	}
 
 
 877
 878	/* create a segment skipping the header and leaving out the checksum */
 879	xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
 880				    (len - offset - GSS_KRB5_TOK_HDR_LEN -
 881				     kctx->gk5e->cksumlength));
 882
 883	ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 884	if (ret)
 885		goto out_err;
 886
 887	our_hmac_obj.len = kctx->gk5e->cksumlength;
 
 
 888	our_hmac_obj.data = our_hmac;
 889	ret = gss_krb5_checksum(ahash, NULL, 0, &subbuf, 0, &our_hmac_obj);
 
 
 890	if (ret)
 891		goto out_err;
 892
 893	/* Get the packet's hmac value */
 894	ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength,
 895				      pkt_hmac, kctx->gk5e->cksumlength);
 896	if (ret)
 897		goto out_err;
 898
 899	if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
 900		ret = GSS_S_BAD_SIG;
 901		goto out_err;
 902	}
 903	*headskip = crypto_sync_skcipher_blocksize(cipher);
 904	*tailskip = kctx->gk5e->cksumlength;
 905out_err:
 906	if (ret && ret != GSS_S_BAD_SIG)
 907		ret = GSS_S_FAILURE;
 908	return ret;
 909}
 910
 911/**
 912 * krb5_etm_checksum - Compute a MAC for a GSS Wrap token
 913 * @cipher: an initialized cipher transform
 914 * @tfm: an initialized hash transform
 915 * @body: xdr_buf containing an RPC message (body.len is the message length)
 916 * @body_offset: byte offset into @body to start checksumming
 917 * @cksumout: OUT: a buffer to be filled in with the computed HMAC
 918 *
 919 * Usually expressed as H = HMAC(K, IV | ciphertext)[1..h] .
 920 *
 921 * Caller provides the truncation length of the output token (h) in
 922 * cksumout.len.
 923 *
 924 * Return values:
 925 *   %GSS_S_COMPLETE: Digest computed, @cksumout filled in
 926 *   %GSS_S_FAILURE: Call failed
 927 */
 928VISIBLE_IF_KUNIT
 929u32 krb5_etm_checksum(struct crypto_sync_skcipher *cipher,
 930		      struct crypto_ahash *tfm, const struct xdr_buf *body,
 931		      int body_offset, struct xdr_netobj *cksumout)
 932{
 933	unsigned int ivsize = crypto_sync_skcipher_ivsize(cipher);
 934	struct ahash_request *req;
 935	struct scatterlist sg[1];
 936	u8 *iv, *checksumdata;
 937	int err = -ENOMEM;
 938
 939	checksumdata = kmalloc(crypto_ahash_digestsize(tfm), GFP_KERNEL);
 940	if (!checksumdata)
 941		return GSS_S_FAILURE;
 942	/* For RPCSEC, the "initial cipher state" is always all zeroes. */
 943	iv = kzalloc(ivsize, GFP_KERNEL);
 944	if (!iv)
 945		goto out_free_mem;
 946
 947	req = ahash_request_alloc(tfm, GFP_KERNEL);
 948	if (!req)
 949		goto out_free_mem;
 950	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 951	err = crypto_ahash_init(req);
 952	if (err)
 953		goto out_free_ahash;
 954
 955	sg_init_one(sg, iv, ivsize);
 956	ahash_request_set_crypt(req, sg, NULL, ivsize);
 957	err = crypto_ahash_update(req);
 958	if (err)
 959		goto out_free_ahash;
 960	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 961			      checksummer, req);
 962	if (err)
 963		goto out_free_ahash;
 964
 965	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 966	err = crypto_ahash_final(req);
 967	if (err)
 968		goto out_free_ahash;
 969	memcpy(cksumout->data, checksumdata, cksumout->len);
 970
 971out_free_ahash:
 972	ahash_request_free(req);
 973out_free_mem:
 974	kfree(iv);
 975	kfree_sensitive(checksumdata);
 976	return err ? GSS_S_FAILURE : GSS_S_COMPLETE;
 977}
 978EXPORT_SYMBOL_IF_KUNIT(krb5_etm_checksum);
 979
 980/**
 981 * krb5_etm_encrypt - Encrypt using the RFC 8009 rules
 982 * @kctx: Kerberos context
 983 * @offset: starting offset of the payload, in bytes
 984 * @buf: OUT: send buffer to contain the encrypted payload
 985 * @pages: plaintext payload
 986 *
 987 * The main difference with aes_encrypt is that "The HMAC is
 988 * calculated over the cipher state concatenated with the AES
 989 * output, instead of being calculated over the confounder and
 990 * plaintext.  This allows the message receiver to verify the
 991 * integrity of the message before decrypting the message."
 992 *
 993 * RFC 8009 Section 5:
 994 *
 995 * encryption function: as follows, where E() is AES encryption in
 996 * CBC-CS3 mode, and h is the size of truncated HMAC (128 bits or
 997 * 192 bits as described above).
 998 *
 999 *    N = random value of length 128 bits (the AES block size)
1000 *    IV = cipher state
1001 *    C = E(Ke, N | plaintext, IV)
1002 *    H = HMAC(Ki, IV | C)
1003 *    ciphertext = C | H[1..h]
1004 *
1005 * This encryption formula provides AEAD EtM with key separation.
1006 *
1007 * Return values:
1008 *   %GSS_S_COMPLETE: Encryption successful
1009 *   %GSS_S_FAILURE: Encryption failed
1010 */
1011u32
1012krb5_etm_encrypt(struct krb5_ctx *kctx, u32 offset,
1013		 struct xdr_buf *buf, struct page **pages)
1014{
1015	struct crypto_sync_skcipher *cipher, *aux_cipher;
1016	struct crypto_ahash *ahash;
1017	struct xdr_netobj hmac;
1018	unsigned int conflen;
1019	u8 *ecptr;
1020	u32 err;
1021
1022	if (kctx->initiate) {
1023		cipher = kctx->initiator_enc;
1024		aux_cipher = kctx->initiator_enc_aux;
1025		ahash = kctx->initiator_integ;
1026	} else {
1027		cipher = kctx->acceptor_enc;
1028		aux_cipher = kctx->acceptor_enc_aux;
1029		ahash = kctx->acceptor_integ;
1030	}
1031	conflen = crypto_sync_skcipher_blocksize(cipher);
1032
1033	offset += GSS_KRB5_TOK_HDR_LEN;
1034	if (xdr_extend_head(buf, offset, conflen))
1035		return GSS_S_FAILURE;
1036	krb5_make_confounder(buf->head[0].iov_base + offset, conflen);
1037	offset -= GSS_KRB5_TOK_HDR_LEN;
1038
1039	if (buf->tail[0].iov_base) {
1040		ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
1041	} else {
1042		buf->tail[0].iov_base = buf->head[0].iov_base
1043							+ buf->head[0].iov_len;
1044		buf->tail[0].iov_len = 0;
1045		ecptr = buf->tail[0].iov_base;
1046	}
1047
1048	memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
1049	buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
1050	buf->len += GSS_KRB5_TOK_HDR_LEN;
1051
1052	err = krb5_cbc_cts_encrypt(cipher, aux_cipher,
1053				   offset + GSS_KRB5_TOK_HDR_LEN,
1054				   buf, pages, NULL, 0);
1055	if (err)
1056		return GSS_S_FAILURE;
1057
1058	hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
1059	hmac.len = kctx->gk5e->cksumlength;
1060	err = krb5_etm_checksum(cipher, ahash,
1061				buf, offset + GSS_KRB5_TOK_HDR_LEN, &hmac);
1062	if (err)
1063		goto out_err;
1064	buf->tail[0].iov_len += kctx->gk5e->cksumlength;
1065	buf->len += kctx->gk5e->cksumlength;
1066
1067	return GSS_S_COMPLETE;
 
 
 
 
1068
1069out_err:
1070	return GSS_S_FAILURE;
 
 
 
1071}
1072
1073/**
1074 * krb5_etm_decrypt - Decrypt using the RFC 8009 rules
1075 * @kctx: Kerberos context
1076 * @offset: starting offset of the ciphertext, in bytes
1077 * @len:
1078 * @buf:
1079 * @headskip: OUT: the enctype's confounder length, in octets
1080 * @tailskip: OUT: the enctype's HMAC length, in octets
1081 *
1082 * RFC 8009 Section 5:
1083 *
1084 * decryption function: as follows, where D() is AES decryption in
1085 * CBC-CS3 mode, and h is the size of truncated HMAC.
1086 *
1087 *    (C, H) = ciphertext
1088 *        (Note: H is the last h bits of the ciphertext.)
1089 *    IV = cipher state
1090 *    if H != HMAC(Ki, IV | C)[1..h]
1091 *        stop, report error
1092 *    (N, P) = D(Ke, C, IV)
1093 *
1094 * Return values:
1095 *   %GSS_S_COMPLETE: Decryption successful
1096 *   %GSS_S_BAD_SIG: computed HMAC != received HMAC
1097 *   %GSS_S_FAILURE: Decryption failed
1098 */
1099u32
1100krb5_etm_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
1101		 struct xdr_buf *buf, u32 *headskip, u32 *tailskip)
1102{
1103	struct crypto_sync_skcipher *cipher, *aux_cipher;
1104	u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
1105	u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
1106	struct xdr_netobj our_hmac_obj;
1107	struct crypto_ahash *ahash;
1108	struct xdr_buf subbuf;
1109	u32 ret = 0;
1110
1111	if (kctx->initiate) {
1112		cipher = kctx->acceptor_enc;
1113		aux_cipher = kctx->acceptor_enc_aux;
1114		ahash = kctx->acceptor_integ;
1115	} else {
1116		cipher = kctx->initiator_enc;
1117		aux_cipher = kctx->initiator_enc_aux;
1118		ahash = kctx->initiator_integ;
 
 
 
 
 
 
 
1119	}
1120
1121	/* Extract the ciphertext into @subbuf. */
1122	xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
1123			   (len - offset - GSS_KRB5_TOK_HDR_LEN -
1124			    kctx->gk5e->cksumlength));
1125
1126	our_hmac_obj.data = our_hmac;
1127	our_hmac_obj.len = kctx->gk5e->cksumlength;
1128	ret = krb5_etm_checksum(cipher, ahash, &subbuf, 0, &our_hmac_obj);
1129	if (ret)
 
 
1130		goto out_err;
1131	ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength,
1132				      pkt_hmac, kctx->gk5e->cksumlength);
1133	if (ret)
1134		goto out_err;
1135	if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
1136		ret = GSS_S_BAD_SIG;
 
 
1137		goto out_err;
1138	}
1139
1140	ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf);
1141	if (ret) {
1142		ret = GSS_S_FAILURE;
 
 
 
 
1143		goto out_err;
1144	}
1145
1146	*headskip = crypto_sync_skcipher_blocksize(cipher);
1147	*tailskip = kctx->gk5e->cksumlength;
1148	return GSS_S_COMPLETE;
 
 
1149
1150out_err:
1151	if (ret != GSS_S_BAD_SIG)
1152		ret = GSS_S_FAILURE;
1153	return ret;
 
1154}
v4.10.11
   1/*
   2 *  linux/net/sunrpc/gss_krb5_crypto.c
   3 *
   4 *  Copyright (c) 2000-2008 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
  37#include <crypto/hash.h>
  38#include <crypto/skcipher.h>
 
  39#include <linux/err.h>
  40#include <linux/types.h>
  41#include <linux/mm.h>
  42#include <linux/scatterlist.h>
  43#include <linux/highmem.h>
  44#include <linux/pagemap.h>
  45#include <linux/random.h>
  46#include <linux/sunrpc/gss_krb5.h>
  47#include <linux/sunrpc/xdr.h>
 
 
 
  48
  49#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  50# define RPCDBG_FACILITY        RPCDBG_AUTH
  51#endif
  52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  53u32
  54krb5_encrypt(
  55	struct crypto_skcipher *tfm,
  56	void * iv,
  57	void * in,
  58	void * out,
  59	int length)
  60{
  61	u32 ret = -EINVAL;
  62	struct scatterlist sg[1];
  63	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  64	SKCIPHER_REQUEST_ON_STACK(req, tfm);
  65
  66	if (length % crypto_skcipher_blocksize(tfm) != 0)
  67		goto out;
  68
  69	if (crypto_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  70		dprintk("RPC:       gss_k5encrypt: tfm iv size too large %d\n",
  71			crypto_skcipher_ivsize(tfm));
  72		goto out;
  73	}
  74
  75	if (iv)
  76		memcpy(local_iv, iv, crypto_skcipher_ivsize(tfm));
  77
  78	memcpy(out, in, length);
  79	sg_init_one(sg, out, length);
  80
  81	skcipher_request_set_tfm(req, tfm);
  82	skcipher_request_set_callback(req, 0, NULL, NULL);
  83	skcipher_request_set_crypt(req, sg, sg, length, local_iv);
  84
  85	ret = crypto_skcipher_encrypt(req);
  86	skcipher_request_zero(req);
  87out:
  88	dprintk("RPC:       krb5_encrypt returns %d\n", ret);
  89	return ret;
  90}
  91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  92u32
  93krb5_decrypt(
  94     struct crypto_skcipher *tfm,
  95     void * iv,
  96     void * in,
  97     void * out,
  98     int length)
  99{
 100	u32 ret = -EINVAL;
 101	struct scatterlist sg[1];
 102	u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
 103	SKCIPHER_REQUEST_ON_STACK(req, tfm);
 104
 105	if (length % crypto_skcipher_blocksize(tfm) != 0)
 106		goto out;
 107
 108	if (crypto_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
 109		dprintk("RPC:       gss_k5decrypt: tfm iv size too large %d\n",
 110			crypto_skcipher_ivsize(tfm));
 111		goto out;
 112	}
 113	if (iv)
 114		memcpy(local_iv,iv, crypto_skcipher_ivsize(tfm));
 115
 116	memcpy(out, in, length);
 117	sg_init_one(sg, out, length);
 118
 119	skcipher_request_set_tfm(req, tfm);
 120	skcipher_request_set_callback(req, 0, NULL, NULL);
 121	skcipher_request_set_crypt(req, sg, sg, length, local_iv);
 122
 123	ret = crypto_skcipher_decrypt(req);
 124	skcipher_request_zero(req);
 125out:
 126	dprintk("RPC:       gss_k5decrypt returns %d\n",ret);
 127	return ret;
 128}
 129
 130static int
 131checksummer(struct scatterlist *sg, void *data)
 132{
 133	struct ahash_request *req = data;
 134
 135	ahash_request_set_crypt(req, sg, NULL, sg->length);
 136
 137	return crypto_ahash_update(req);
 138}
 139
 140static int
 141arcfour_hmac_md5_usage_to_salt(unsigned int usage, u8 salt[4])
 142{
 143	unsigned int ms_usage;
 144
 145	switch (usage) {
 146	case KG_USAGE_SIGN:
 147		ms_usage = 15;
 148		break;
 149	case KG_USAGE_SEAL:
 150		ms_usage = 13;
 151		break;
 152	default:
 153		return -EINVAL;
 154	}
 155	salt[0] = (ms_usage >> 0) & 0xff;
 156	salt[1] = (ms_usage >> 8) & 0xff;
 157	salt[2] = (ms_usage >> 16) & 0xff;
 158	salt[3] = (ms_usage >> 24) & 0xff;
 159
 160	return 0;
 161}
 162
 163static u32
 164make_checksum_hmac_md5(struct krb5_ctx *kctx, char *header, int hdrlen,
 165		       struct xdr_buf *body, int body_offset, u8 *cksumkey,
 166		       unsigned int usage, struct xdr_netobj *cksumout)
 167{
 168	struct scatterlist              sg[1];
 169	int err = -1;
 170	u8 *checksumdata;
 171	u8 rc4salt[4];
 172	struct crypto_ahash *md5;
 173	struct crypto_ahash *hmac_md5;
 174	struct ahash_request *req;
 175
 176	if (cksumkey == NULL)
 177		return GSS_S_FAILURE;
 178
 179	if (cksumout->len < kctx->gk5e->cksumlength) {
 180		dprintk("%s: checksum buffer length, %u, too small for %s\n",
 181			__func__, cksumout->len, kctx->gk5e->name);
 182		return GSS_S_FAILURE;
 183	}
 184
 185	if (arcfour_hmac_md5_usage_to_salt(usage, rc4salt)) {
 186		dprintk("%s: invalid usage value %u\n", __func__, usage);
 187		return GSS_S_FAILURE;
 188	}
 189
 190	checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
 191	if (!checksumdata)
 192		return GSS_S_FAILURE;
 193
 194	md5 = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
 195	if (IS_ERR(md5))
 196		goto out_free_cksum;
 197
 198	hmac_md5 = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0,
 199				      CRYPTO_ALG_ASYNC);
 200	if (IS_ERR(hmac_md5))
 201		goto out_free_md5;
 202
 203	req = ahash_request_alloc(md5, GFP_NOFS);
 204	if (!req)
 205		goto out_free_hmac_md5;
 206
 207	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 208
 209	err = crypto_ahash_init(req);
 210	if (err)
 211		goto out;
 212	sg_init_one(sg, rc4salt, 4);
 213	ahash_request_set_crypt(req, sg, NULL, 4);
 214	err = crypto_ahash_update(req);
 215	if (err)
 216		goto out;
 217
 218	sg_init_one(sg, header, hdrlen);
 219	ahash_request_set_crypt(req, sg, NULL, hdrlen);
 220	err = crypto_ahash_update(req);
 221	if (err)
 222		goto out;
 223	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 224			      checksummer, req);
 225	if (err)
 226		goto out;
 227	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 228	err = crypto_ahash_final(req);
 229	if (err)
 230		goto out;
 231
 232	ahash_request_free(req);
 233	req = ahash_request_alloc(hmac_md5, GFP_NOFS);
 234	if (!req)
 235		goto out_free_hmac_md5;
 236
 237	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 238
 239	err = crypto_ahash_init(req);
 240	if (err)
 241		goto out;
 242	err = crypto_ahash_setkey(hmac_md5, cksumkey, kctx->gk5e->keylength);
 243	if (err)
 244		goto out;
 245
 246	sg_init_one(sg, checksumdata, crypto_ahash_digestsize(md5));
 247	ahash_request_set_crypt(req, sg, checksumdata,
 248				crypto_ahash_digestsize(md5));
 249	err = crypto_ahash_digest(req);
 250	if (err)
 251		goto out;
 252
 253	memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
 254	cksumout->len = kctx->gk5e->cksumlength;
 255out:
 256	ahash_request_free(req);
 257out_free_hmac_md5:
 258	crypto_free_ahash(hmac_md5);
 259out_free_md5:
 260	crypto_free_ahash(md5);
 261out_free_cksum:
 262	kfree(checksumdata);
 263	return err ? GSS_S_FAILURE : 0;
 264}
 265
 266/*
 267 * checksum the plaintext data and hdrlen bytes of the token header
 268 * The checksum is performed over the first 8 bytes of the
 269 * gss token header and then over the data body
 270 */
 271u32
 272make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
 273	      struct xdr_buf *body, int body_offset, u8 *cksumkey,
 274	      unsigned int usage, struct xdr_netobj *cksumout)
 275{
 276	struct crypto_ahash *tfm;
 277	struct ahash_request *req;
 278	struct scatterlist              sg[1];
 279	int err = -1;
 280	u8 *checksumdata;
 281	unsigned int checksumlen;
 282
 283	if (kctx->gk5e->ctype == CKSUMTYPE_HMAC_MD5_ARCFOUR)
 284		return make_checksum_hmac_md5(kctx, header, hdrlen,
 285					      body, body_offset,
 286					      cksumkey, usage, cksumout);
 287
 288	if (cksumout->len < kctx->gk5e->cksumlength) {
 289		dprintk("%s: checksum buffer length, %u, too small for %s\n",
 290			__func__, cksumout->len, kctx->gk5e->name);
 291		return GSS_S_FAILURE;
 292	}
 293
 294	checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
 295	if (checksumdata == NULL)
 296		return GSS_S_FAILURE;
 297
 298	tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
 299	if (IS_ERR(tfm))
 300		goto out_free_cksum;
 301
 302	req = ahash_request_alloc(tfm, GFP_NOFS);
 303	if (!req)
 304		goto out_free_ahash;
 305
 306	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 307
 308	checksumlen = crypto_ahash_digestsize(tfm);
 309
 310	if (cksumkey != NULL) {
 311		err = crypto_ahash_setkey(tfm, cksumkey,
 312					  kctx->gk5e->keylength);
 313		if (err)
 314			goto out;
 315	}
 316
 317	err = crypto_ahash_init(req);
 318	if (err)
 319		goto out;
 320	sg_init_one(sg, header, hdrlen);
 321	ahash_request_set_crypt(req, sg, NULL, hdrlen);
 322	err = crypto_ahash_update(req);
 323	if (err)
 324		goto out;
 325	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 326			      checksummer, req);
 327	if (err)
 328		goto out;
 329	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 330	err = crypto_ahash_final(req);
 331	if (err)
 332		goto out;
 333
 334	switch (kctx->gk5e->ctype) {
 335	case CKSUMTYPE_RSA_MD5:
 336		err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
 337					  checksumdata, checksumlen);
 338		if (err)
 339			goto out;
 340		memcpy(cksumout->data,
 341		       checksumdata + checksumlen - kctx->gk5e->cksumlength,
 342		       kctx->gk5e->cksumlength);
 343		break;
 344	case CKSUMTYPE_HMAC_SHA1_DES3:
 345		memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
 346		break;
 347	default:
 348		BUG();
 349		break;
 350	}
 351	cksumout->len = kctx->gk5e->cksumlength;
 352out:
 353	ahash_request_free(req);
 354out_free_ahash:
 355	crypto_free_ahash(tfm);
 356out_free_cksum:
 357	kfree(checksumdata);
 358	return err ? GSS_S_FAILURE : 0;
 359}
 360
 361/*
 362 * checksum the plaintext data and hdrlen bytes of the token header
 363 * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
 364 * body then over the first 16 octets of the MIC token
 365 * Inclusion of the header data in the calculation of the
 366 * checksum is optional.
 
 
 
 
 
 
 
 
 
 
 
 367 */
 368u32
 369make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen,
 370		 struct xdr_buf *body, int body_offset, u8 *cksumkey,
 371		 unsigned int usage, struct xdr_netobj *cksumout)
 372{
 373	struct crypto_ahash *tfm;
 374	struct ahash_request *req;
 375	struct scatterlist sg[1];
 376	int err = -1;
 377	u8 *checksumdata;
 378	unsigned int checksumlen;
 379
 380	if (kctx->gk5e->keyed_cksum == 0) {
 381		dprintk("%s: expected keyed hash for %s\n",
 382			__func__, kctx->gk5e->name);
 383		return GSS_S_FAILURE;
 384	}
 385	if (cksumkey == NULL) {
 386		dprintk("%s: no key supplied for %s\n",
 387			__func__, kctx->gk5e->name);
 388		return GSS_S_FAILURE;
 389	}
 390
 391	checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
 392	if (!checksumdata)
 393		return GSS_S_FAILURE;
 394
 395	tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
 396	if (IS_ERR(tfm))
 397		goto out_free_cksum;
 398	checksumlen = crypto_ahash_digestsize(tfm);
 399
 400	req = ahash_request_alloc(tfm, GFP_NOFS);
 401	if (!req)
 402		goto out_free_ahash;
 403
 404	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 405
 406	err = crypto_ahash_setkey(tfm, cksumkey, kctx->gk5e->keylength);
 407	if (err)
 408		goto out;
 409
 410	err = crypto_ahash_init(req);
 411	if (err)
 412		goto out;
 
 413	err = xdr_process_buf(body, body_offset, body->len - body_offset,
 414			      checksummer, req);
 415	if (err)
 416		goto out;
 417	if (header != NULL) {
 
 
 418		sg_init_one(sg, header, hdrlen);
 419		ahash_request_set_crypt(req, sg, NULL, hdrlen);
 420		err = crypto_ahash_update(req);
 421		if (err)
 422			goto out;
 423	}
 
 424	ahash_request_set_crypt(req, NULL, checksumdata, 0);
 425	err = crypto_ahash_final(req);
 426	if (err)
 427		goto out;
 428
 429	cksumout->len = kctx->gk5e->cksumlength;
 
 430
 431	switch (kctx->gk5e->ctype) {
 432	case CKSUMTYPE_HMAC_SHA1_96_AES128:
 433	case CKSUMTYPE_HMAC_SHA1_96_AES256:
 434		/* note that this truncates the hash */
 435		memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
 436		break;
 437	default:
 438		BUG();
 439		break;
 440	}
 441out:
 442	ahash_request_free(req);
 443out_free_ahash:
 444	crypto_free_ahash(tfm);
 445out_free_cksum:
 446	kfree(checksumdata);
 447	return err ? GSS_S_FAILURE : 0;
 448}
 
 449
 450struct encryptor_desc {
 451	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
 452	struct skcipher_request *req;
 453	int pos;
 454	struct xdr_buf *outbuf;
 455	struct page **pages;
 456	struct scatterlist infrags[4];
 457	struct scatterlist outfrags[4];
 458	int fragno;
 459	int fraglen;
 460};
 461
 462static int
 463encryptor(struct scatterlist *sg, void *data)
 464{
 465	struct encryptor_desc *desc = data;
 466	struct xdr_buf *outbuf = desc->outbuf;
 467	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(desc->req);
 
 468	struct page *in_page;
 469	int thislen = desc->fraglen + sg->length;
 470	int fraglen, ret;
 471	int page_pos;
 472
 473	/* Worst case is 4 fragments: head, end of page 1, start
 474	 * of page 2, tail.  Anything more is a bug. */
 475	BUG_ON(desc->fragno > 3);
 476
 477	page_pos = desc->pos - outbuf->head[0].iov_len;
 478	if (page_pos >= 0 && page_pos < outbuf->page_len) {
 479		/* pages are not in place: */
 480		int i = (page_pos + outbuf->page_base) >> PAGE_SHIFT;
 481		in_page = desc->pages[i];
 482	} else {
 483		in_page = sg_page(sg);
 484	}
 485	sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
 486		    sg->offset);
 487	sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
 488		    sg->offset);
 489	desc->fragno++;
 490	desc->fraglen += sg->length;
 491	desc->pos += sg->length;
 492
 493	fraglen = thislen & (crypto_skcipher_blocksize(tfm) - 1);
 494	thislen -= fraglen;
 495
 496	if (thislen == 0)
 497		return 0;
 498
 499	sg_mark_end(&desc->infrags[desc->fragno - 1]);
 500	sg_mark_end(&desc->outfrags[desc->fragno - 1]);
 501
 502	skcipher_request_set_crypt(desc->req, desc->infrags, desc->outfrags,
 503				   thislen, desc->iv);
 504
 505	ret = crypto_skcipher_encrypt(desc->req);
 506	if (ret)
 507		return ret;
 508
 509	sg_init_table(desc->infrags, 4);
 510	sg_init_table(desc->outfrags, 4);
 511
 512	if (fraglen) {
 513		sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
 514				sg->offset + sg->length - fraglen);
 515		desc->infrags[0] = desc->outfrags[0];
 516		sg_assign_page(&desc->infrags[0], in_page);
 517		desc->fragno = 1;
 518		desc->fraglen = fraglen;
 519	} else {
 520		desc->fragno = 0;
 521		desc->fraglen = 0;
 522	}
 523	return 0;
 524}
 525
 526int
 527gss_encrypt_xdr_buf(struct crypto_skcipher *tfm, struct xdr_buf *buf,
 528		    int offset, struct page **pages)
 529{
 530	int ret;
 531	struct encryptor_desc desc;
 532	SKCIPHER_REQUEST_ON_STACK(req, tfm);
 533
 534	BUG_ON((buf->len - offset) % crypto_skcipher_blocksize(tfm) != 0);
 535
 536	skcipher_request_set_tfm(req, tfm);
 537	skcipher_request_set_callback(req, 0, NULL, NULL);
 538
 539	memset(desc.iv, 0, sizeof(desc.iv));
 540	desc.req = req;
 541	desc.pos = offset;
 542	desc.outbuf = buf;
 543	desc.pages = pages;
 544	desc.fragno = 0;
 545	desc.fraglen = 0;
 546
 547	sg_init_table(desc.infrags, 4);
 548	sg_init_table(desc.outfrags, 4);
 549
 550	ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
 551	skcipher_request_zero(req);
 552	return ret;
 553}
 554
 555struct decryptor_desc {
 556	u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
 557	struct skcipher_request *req;
 558	struct scatterlist frags[4];
 559	int fragno;
 560	int fraglen;
 561};
 562
 563static int
 564decryptor(struct scatterlist *sg, void *data)
 565{
 566	struct decryptor_desc *desc = data;
 567	int thislen = desc->fraglen + sg->length;
 568	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(desc->req);
 
 569	int fraglen, ret;
 570
 571	/* Worst case is 4 fragments: head, end of page 1, start
 572	 * of page 2, tail.  Anything more is a bug. */
 573	BUG_ON(desc->fragno > 3);
 574	sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
 575		    sg->offset);
 576	desc->fragno++;
 577	desc->fraglen += sg->length;
 578
 579	fraglen = thislen & (crypto_skcipher_blocksize(tfm) - 1);
 580	thislen -= fraglen;
 581
 582	if (thislen == 0)
 583		return 0;
 584
 585	sg_mark_end(&desc->frags[desc->fragno - 1]);
 586
 587	skcipher_request_set_crypt(desc->req, desc->frags, desc->frags,
 588				   thislen, desc->iv);
 589
 590	ret = crypto_skcipher_decrypt(desc->req);
 591	if (ret)
 592		return ret;
 593
 594	sg_init_table(desc->frags, 4);
 595
 596	if (fraglen) {
 597		sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
 598				sg->offset + sg->length - fraglen);
 599		desc->fragno = 1;
 600		desc->fraglen = fraglen;
 601	} else {
 602		desc->fragno = 0;
 603		desc->fraglen = 0;
 604	}
 605	return 0;
 606}
 607
 608int
 609gss_decrypt_xdr_buf(struct crypto_skcipher *tfm, struct xdr_buf *buf,
 610		    int offset)
 611{
 612	int ret;
 613	struct decryptor_desc desc;
 614	SKCIPHER_REQUEST_ON_STACK(req, tfm);
 615
 616	/* XXXJBF: */
 617	BUG_ON((buf->len - offset) % crypto_skcipher_blocksize(tfm) != 0);
 618
 619	skcipher_request_set_tfm(req, tfm);
 620	skcipher_request_set_callback(req, 0, NULL, NULL);
 621
 622	memset(desc.iv, 0, sizeof(desc.iv));
 623	desc.req = req;
 624	desc.fragno = 0;
 625	desc.fraglen = 0;
 626
 627	sg_init_table(desc.frags, 4);
 628
 629	ret = xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
 630	skcipher_request_zero(req);
 631	return ret;
 632}
 633
 634/*
 635 * This function makes the assumption that it was ultimately called
 636 * from gss_wrap().
 637 *
 638 * The client auth_gss code moves any existing tail data into a
 639 * separate page before calling gss_wrap.
 640 * The server svcauth_gss code ensures that both the head and the
 641 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
 642 *
 643 * Even with that guarantee, this function may be called more than
 644 * once in the processing of gss_wrap().  The best we can do is
 645 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
 646 * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
 647 * At run-time we can verify that a single invocation of this
 648 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
 649 */
 650
 651int
 652xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
 653{
 654	u8 *p;
 655
 656	if (shiftlen == 0)
 657		return 0;
 658
 659	BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
 660	BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
 661
 662	p = buf->head[0].iov_base + base;
 663
 664	memmove(p + shiftlen, p, buf->head[0].iov_len - base);
 665
 666	buf->head[0].iov_len += shiftlen;
 667	buf->len += shiftlen;
 668
 669	return 0;
 670}
 671
 672static u32
 673gss_krb5_cts_crypt(struct crypto_skcipher *cipher, struct xdr_buf *buf,
 674		   u32 offset, u8 *iv, struct page **pages, int encrypt)
 675{
 676	u32 ret;
 677	struct scatterlist sg[1];
 678	SKCIPHER_REQUEST_ON_STACK(req, cipher);
 679	u8 *data;
 680	struct page **save_pages;
 681	u32 len = buf->len - offset;
 682
 683	if (len > GSS_KRB5_MAX_BLOCKSIZE * 2) {
 684		WARN_ON(0);
 685		return -ENOMEM;
 686	}
 687	data = kmalloc(GSS_KRB5_MAX_BLOCKSIZE * 2, GFP_NOFS);
 688	if (!data)
 689		return -ENOMEM;
 690
 691	/*
 692	 * For encryption, we want to read from the cleartext
 693	 * page cache pages, and write the encrypted data to
 694	 * the supplied xdr_buf pages.
 695	 */
 696	save_pages = buf->pages;
 697	if (encrypt)
 698		buf->pages = pages;
 699
 700	ret = read_bytes_from_xdr_buf(buf, offset, data, len);
 701	buf->pages = save_pages;
 702	if (ret)
 703		goto out;
 704
 705	sg_init_one(sg, data, len);
 706
 707	skcipher_request_set_tfm(req, cipher);
 708	skcipher_request_set_callback(req, 0, NULL, NULL);
 709	skcipher_request_set_crypt(req, sg, sg, len, iv);
 710
 711	if (encrypt)
 712		ret = crypto_skcipher_encrypt(req);
 713	else
 714		ret = crypto_skcipher_decrypt(req);
 715
 716	skcipher_request_zero(req);
 717
 718	if (ret)
 719		goto out;
 720
 721	ret = write_bytes_to_xdr_buf(buf, offset, data, len);
 722
 
 
 
 
 
 
 
 
 
 
 723out:
 724	kfree(data);
 725	return ret;
 726}
 727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 728u32
 729gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
 730		     struct xdr_buf *buf, struct page **pages)
 731{
 732	u32 err;
 733	struct xdr_netobj hmac;
 734	u8 *cksumkey;
 735	u8 *ecptr;
 736	struct crypto_skcipher *cipher, *aux_cipher;
 737	int blocksize;
 738	struct page **save_pages;
 739	int nblocks, nbytes;
 740	struct encryptor_desc desc;
 741	u32 cbcbytes;
 742	unsigned int usage;
 743
 744	if (kctx->initiate) {
 745		cipher = kctx->initiator_enc;
 746		aux_cipher = kctx->initiator_enc_aux;
 747		cksumkey = kctx->initiator_integ;
 748		usage = KG_USAGE_INITIATOR_SEAL;
 749	} else {
 750		cipher = kctx->acceptor_enc;
 751		aux_cipher = kctx->acceptor_enc_aux;
 752		cksumkey = kctx->acceptor_integ;
 753		usage = KG_USAGE_ACCEPTOR_SEAL;
 754	}
 755	blocksize = crypto_skcipher_blocksize(cipher);
 756
 757	/* hide the gss token header and insert the confounder */
 758	offset += GSS_KRB5_TOK_HDR_LEN;
 759	if (xdr_extend_head(buf, offset, kctx->gk5e->conflen))
 760		return GSS_S_FAILURE;
 761	gss_krb5_make_confounder(buf->head[0].iov_base + offset, kctx->gk5e->conflen);
 762	offset -= GSS_KRB5_TOK_HDR_LEN;
 763
 764	if (buf->tail[0].iov_base != NULL) {
 765		ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
 766	} else {
 767		buf->tail[0].iov_base = buf->head[0].iov_base
 768							+ buf->head[0].iov_len;
 769		buf->tail[0].iov_len = 0;
 770		ecptr = buf->tail[0].iov_base;
 771	}
 772
 773	/* copy plaintext gss token header after filler (if any) */
 774	memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
 775	buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
 776	buf->len += GSS_KRB5_TOK_HDR_LEN;
 777
 778	/* Do the HMAC */
 779	hmac.len = GSS_KRB5_MAX_CKSUM_LEN;
 780	hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
 781
 782	/*
 783	 * When we are called, pages points to the real page cache
 784	 * data -- which we can't go and encrypt!  buf->pages points
 785	 * to scratch pages which we are going to send off to the
 786	 * client/server.  Swap in the plaintext pages to calculate
 787	 * the hmac.
 788	 */
 789	save_pages = buf->pages;
 790	buf->pages = pages;
 791
 792	err = make_checksum_v2(kctx, NULL, 0, buf,
 793			       offset + GSS_KRB5_TOK_HDR_LEN,
 794			       cksumkey, usage, &hmac);
 795	buf->pages = save_pages;
 796	if (err)
 797		return GSS_S_FAILURE;
 798
 799	nbytes = buf->len - offset - GSS_KRB5_TOK_HDR_LEN;
 800	nblocks = (nbytes + blocksize - 1) / blocksize;
 801	cbcbytes = 0;
 802	if (nblocks > 2)
 803		cbcbytes = (nblocks - 2) * blocksize;
 804
 805	memset(desc.iv, 0, sizeof(desc.iv));
 806
 807	if (cbcbytes) {
 808		SKCIPHER_REQUEST_ON_STACK(req, aux_cipher);
 809
 810		desc.pos = offset + GSS_KRB5_TOK_HDR_LEN;
 811		desc.fragno = 0;
 812		desc.fraglen = 0;
 813		desc.pages = pages;
 814		desc.outbuf = buf;
 815		desc.req = req;
 816
 817		skcipher_request_set_tfm(req, aux_cipher);
 818		skcipher_request_set_callback(req, 0, NULL, NULL);
 819
 820		sg_init_table(desc.infrags, 4);
 821		sg_init_table(desc.outfrags, 4);
 822
 823		err = xdr_process_buf(buf, offset + GSS_KRB5_TOK_HDR_LEN,
 824				      cbcbytes, encryptor, &desc);
 825		skcipher_request_zero(req);
 826		if (err)
 827			goto out_err;
 828	}
 829
 830	/* Make sure IV carries forward from any CBC results. */
 831	err = gss_krb5_cts_crypt(cipher, buf,
 832				 offset + GSS_KRB5_TOK_HDR_LEN + cbcbytes,
 833				 desc.iv, pages, 1);
 834	if (err) {
 835		err = GSS_S_FAILURE;
 836		goto out_err;
 837	}
 838
 839	/* Now update buf to account for HMAC */
 840	buf->tail[0].iov_len += kctx->gk5e->cksumlength;
 841	buf->len += kctx->gk5e->cksumlength;
 842
 843out_err:
 844	if (err)
 845		err = GSS_S_FAILURE;
 846	return err;
 847}
 848
 849u32
 850gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf,
 851		     u32 *headskip, u32 *tailskip)
 852{
 853	struct xdr_buf subbuf;
 854	u32 ret = 0;
 855	u8 *cksum_key;
 856	struct crypto_skcipher *cipher, *aux_cipher;
 857	struct xdr_netobj our_hmac_obj;
 858	u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
 859	u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
 860	int nblocks, blocksize, cbcbytes;
 861	struct decryptor_desc desc;
 862	unsigned int usage;
 863
 864	if (kctx->initiate) {
 865		cipher = kctx->acceptor_enc;
 866		aux_cipher = kctx->acceptor_enc_aux;
 867		cksum_key = kctx->acceptor_integ;
 868		usage = KG_USAGE_ACCEPTOR_SEAL;
 869	} else {
 870		cipher = kctx->initiator_enc;
 871		aux_cipher = kctx->initiator_enc_aux;
 872		cksum_key = kctx->initiator_integ;
 873		usage = KG_USAGE_INITIATOR_SEAL;
 874	}
 875	blocksize = crypto_skcipher_blocksize(cipher);
 876
 877
 878	/* create a segment skipping the header and leaving out the checksum */
 879	xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
 880				    (buf->len - offset - GSS_KRB5_TOK_HDR_LEN -
 881				     kctx->gk5e->cksumlength));
 882
 883	nblocks = (subbuf.len + blocksize - 1) / blocksize;
 884
 885	cbcbytes = 0;
 886	if (nblocks > 2)
 887		cbcbytes = (nblocks - 2) * blocksize;
 888
 889	memset(desc.iv, 0, sizeof(desc.iv));
 890
 891	if (cbcbytes) {
 892		SKCIPHER_REQUEST_ON_STACK(req, aux_cipher);
 893
 894		desc.fragno = 0;
 895		desc.fraglen = 0;
 896		desc.req = req;
 897
 898		skcipher_request_set_tfm(req, aux_cipher);
 899		skcipher_request_set_callback(req, 0, NULL, NULL);
 900
 901		sg_init_table(desc.frags, 4);
 902
 903		ret = xdr_process_buf(&subbuf, 0, cbcbytes, decryptor, &desc);
 904		skcipher_request_zero(req);
 905		if (ret)
 906			goto out_err;
 907	}
 908
 909	/* Make sure IV carries forward from any CBC results. */
 910	ret = gss_krb5_cts_crypt(cipher, &subbuf, cbcbytes, desc.iv, NULL, 0);
 911	if (ret)
 912		goto out_err;
 913
 914
 915	/* Calculate our hmac over the plaintext data */
 916	our_hmac_obj.len = sizeof(our_hmac);
 917	our_hmac_obj.data = our_hmac;
 918
 919	ret = make_checksum_v2(kctx, NULL, 0, &subbuf, 0,
 920			       cksum_key, usage, &our_hmac_obj);
 921	if (ret)
 922		goto out_err;
 923
 924	/* Get the packet's hmac value */
 925	ret = read_bytes_from_xdr_buf(buf, buf->len - kctx->gk5e->cksumlength,
 926				      pkt_hmac, kctx->gk5e->cksumlength);
 927	if (ret)
 928		goto out_err;
 929
 930	if (memcmp(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
 931		ret = GSS_S_BAD_SIG;
 932		goto out_err;
 933	}
 934	*headskip = kctx->gk5e->conflen;
 935	*tailskip = kctx->gk5e->cksumlength;
 936out_err:
 937	if (ret && ret != GSS_S_BAD_SIG)
 938		ret = GSS_S_FAILURE;
 939	return ret;
 940}
 941
 942/*
 943 * Compute Kseq given the initial session key and the checksum.
 944 * Set the key of the given cipher.
 
 
 
 
 
 
 
 
 
 
 
 
 
 945 */
 946int
 947krb5_rc4_setup_seq_key(struct krb5_ctx *kctx, struct crypto_skcipher *cipher,
 948		       unsigned char *cksum)
 
 949{
 950	struct crypto_shash *hmac;
 951	struct shash_desc *desc;
 952	u8 Kseq[GSS_KRB5_MAX_KEYLEN];
 953	u32 zeroconstant = 0;
 954	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 955
 956	dprintk("%s: entered\n", __func__);
 
 
 
 
 
 
 
 957
 958	hmac = crypto_alloc_shash(kctx->gk5e->cksum_name, 0, 0);
 959	if (IS_ERR(hmac)) {
 960		dprintk("%s: error %ld, allocating hash '%s'\n",
 961			__func__, PTR_ERR(hmac), kctx->gk5e->cksum_name);
 962		return PTR_ERR(hmac);
 963	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 964
 965	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac),
 966		       GFP_NOFS);
 967	if (!desc) {
 968		dprintk("%s: failed to allocate shash descriptor for '%s'\n",
 969			__func__, kctx->gk5e->cksum_name);
 970		crypto_free_shash(hmac);
 971		return -ENOMEM;
 
 972	}
 
 973
 974	desc->tfm = hmac;
 975	desc->flags = 0;
 
 
 
 976
 977	/* Compute intermediate Kseq from session key */
 978	err = crypto_shash_setkey(hmac, kctx->Ksess, kctx->gk5e->keylength);
 979	if (err)
 980		goto out_err;
 
 
 
 
 981
 982	err = crypto_shash_digest(desc, (u8 *)&zeroconstant, 4, Kseq);
 983	if (err)
 984		goto out_err;
 985
 986	/* Compute final Kseq from the checksum and intermediate Kseq */
 987	err = crypto_shash_setkey(hmac, Kseq, kctx->gk5e->keylength);
 
 988	if (err)
 989		goto out_err;
 990
 991	err = crypto_shash_digest(desc, cksum, 8, Kseq);
 
 
 
 992	if (err)
 993		goto out_err;
 
 
 994
 995	err = crypto_skcipher_setkey(cipher, Kseq, kctx->gk5e->keylength);
 996	if (err)
 997		goto out_err;
 998
 999	err = 0;
1000
1001out_err:
1002	kzfree(desc);
1003	crypto_free_shash(hmac);
1004	dprintk("%s: returning %d\n", __func__, err);
1005	return err;
1006}
1007
1008/*
1009 * Compute Kcrypt given the initial session key and the plaintext seqnum.
1010 * Set the key of cipher kctx->enc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1011 */
1012int
1013krb5_rc4_setup_enc_key(struct krb5_ctx *kctx, struct crypto_skcipher *cipher,
1014		       s32 seqnum)
1015{
1016	struct crypto_shash *hmac;
1017	struct shash_desc *desc;
1018	u8 Kcrypt[GSS_KRB5_MAX_KEYLEN];
1019	u8 zeroconstant[4] = {0};
1020	u8 seqnumarray[4];
1021	int err, i;
1022
1023	dprintk("%s: entered, seqnum %u\n", __func__, seqnum);
1024
1025	hmac = crypto_alloc_shash(kctx->gk5e->cksum_name, 0, 0);
1026	if (IS_ERR(hmac)) {
1027		dprintk("%s: error %ld, allocating hash '%s'\n",
1028			__func__, PTR_ERR(hmac), kctx->gk5e->cksum_name);
1029		return PTR_ERR(hmac);
1030	}
1031
1032	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac),
1033		       GFP_NOFS);
1034	if (!desc) {
1035		dprintk("%s: failed to allocate shash descriptor for '%s'\n",
1036			__func__, kctx->gk5e->cksum_name);
1037		crypto_free_shash(hmac);
1038		return -ENOMEM;
1039	}
1040
1041	desc->tfm = hmac;
1042	desc->flags = 0;
 
 
1043
1044	/* Compute intermediate Kcrypt from session key */
1045	for (i = 0; i < kctx->gk5e->keylength; i++)
1046		Kcrypt[i] = kctx->Ksess[i] ^ 0xf0;
1047
1048	err = crypto_shash_setkey(hmac, Kcrypt, kctx->gk5e->keylength);
1049	if (err)
1050		goto out_err;
1051
1052	err = crypto_shash_digest(desc, zeroconstant, 4, Kcrypt);
1053	if (err)
1054		goto out_err;
1055
1056	/* Compute final Kcrypt from the seqnum and intermediate Kcrypt */
1057	err = crypto_shash_setkey(hmac, Kcrypt, kctx->gk5e->keylength);
1058	if (err)
1059		goto out_err;
 
1060
1061	seqnumarray[0] = (unsigned char) ((seqnum >> 24) & 0xff);
1062	seqnumarray[1] = (unsigned char) ((seqnum >> 16) & 0xff);
1063	seqnumarray[2] = (unsigned char) ((seqnum >> 8) & 0xff);
1064	seqnumarray[3] = (unsigned char) ((seqnum >> 0) & 0xff);
1065
1066	err = crypto_shash_digest(desc, seqnumarray, 4, Kcrypt);
1067	if (err)
1068		goto out_err;
 
1069
1070	err = crypto_skcipher_setkey(cipher, Kcrypt, kctx->gk5e->keylength);
1071	if (err)
1072		goto out_err;
1073
1074	err = 0;
1075
1076out_err:
1077	kzfree(desc);
1078	crypto_free_shash(hmac);
1079	dprintk("%s: returning %d\n", __func__, err);
1080	return err;
1081}
1082