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/algapi.h> 38 #include <crypto/hash.h> 39 #include <crypto/skcipher.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 */ 81 void 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 */ 102 u32 103 krb5_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); 136 out: 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 */ 157 u32 158 krb5_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); 190 out: 191 dprintk("RPC: gss_k5decrypt returns %d\n",ret); 192 return ret; 193 } 194 195 static int 196 checksummer(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 */ 210 u32 211 make_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; 286 out: 287 ahash_request_free(req); 288 out_free_ahash: 289 crypto_free_ahash(tfm); 290 out_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 */ 313 u32 314 gss_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 memcpy(cksumout->data, checksumdata, cksumout->len); 357 358 out_free_ahash: 359 ahash_request_free(req); 360 out_free_cksum: 361 kfree_sensitive(checksumdata); 362 return err ? GSS_S_FAILURE : GSS_S_COMPLETE; 363 } 364 EXPORT_SYMBOL_IF_KUNIT(gss_krb5_checksum); 365 366 struct encryptor_desc { 367 u8 iv[GSS_KRB5_MAX_BLOCKSIZE]; 368 struct skcipher_request *req; 369 int pos; 370 struct xdr_buf *outbuf; 371 struct page **pages; 372 struct scatterlist infrags[4]; 373 struct scatterlist outfrags[4]; 374 int fragno; 375 int fraglen; 376 }; 377 378 static int 379 encryptor(struct scatterlist *sg, void *data) 380 { 381 struct encryptor_desc *desc = data; 382 struct xdr_buf *outbuf = desc->outbuf; 383 struct crypto_sync_skcipher *tfm = 384 crypto_sync_skcipher_reqtfm(desc->req); 385 struct page *in_page; 386 int thislen = desc->fraglen + sg->length; 387 int fraglen, ret; 388 int page_pos; 389 390 /* Worst case is 4 fragments: head, end of page 1, start 391 * of page 2, tail. Anything more is a bug. */ 392 BUG_ON(desc->fragno > 3); 393 394 page_pos = desc->pos - outbuf->head[0].iov_len; 395 if (page_pos >= 0 && page_pos < outbuf->page_len) { 396 /* pages are not in place: */ 397 int i = (page_pos + outbuf->page_base) >> PAGE_SHIFT; 398 in_page = desc->pages[i]; 399 } else { 400 in_page = sg_page(sg); 401 } 402 sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length, 403 sg->offset); 404 sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length, 405 sg->offset); 406 desc->fragno++; 407 desc->fraglen += sg->length; 408 desc->pos += sg->length; 409 410 fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1); 411 thislen -= fraglen; 412 413 if (thislen == 0) 414 return 0; 415 416 sg_mark_end(&desc->infrags[desc->fragno - 1]); 417 sg_mark_end(&desc->outfrags[desc->fragno - 1]); 418 419 skcipher_request_set_crypt(desc->req, desc->infrags, desc->outfrags, 420 thislen, desc->iv); 421 422 ret = crypto_skcipher_encrypt(desc->req); 423 if (ret) 424 return ret; 425 426 sg_init_table(desc->infrags, 4); 427 sg_init_table(desc->outfrags, 4); 428 429 if (fraglen) { 430 sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen, 431 sg->offset + sg->length - fraglen); 432 desc->infrags[0] = desc->outfrags[0]; 433 sg_assign_page(&desc->infrags[0], in_page); 434 desc->fragno = 1; 435 desc->fraglen = fraglen; 436 } else { 437 desc->fragno = 0; 438 desc->fraglen = 0; 439 } 440 return 0; 441 } 442 443 int 444 gss_encrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf, 445 int offset, struct page **pages) 446 { 447 int ret; 448 struct encryptor_desc desc; 449 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm); 450 451 BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0); 452 453 skcipher_request_set_sync_tfm(req, tfm); 454 skcipher_request_set_callback(req, 0, NULL, NULL); 455 456 memset(desc.iv, 0, sizeof(desc.iv)); 457 desc.req = req; 458 desc.pos = offset; 459 desc.outbuf = buf; 460 desc.pages = pages; 461 desc.fragno = 0; 462 desc.fraglen = 0; 463 464 sg_init_table(desc.infrags, 4); 465 sg_init_table(desc.outfrags, 4); 466 467 ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc); 468 skcipher_request_zero(req); 469 return ret; 470 } 471 472 struct decryptor_desc { 473 u8 iv[GSS_KRB5_MAX_BLOCKSIZE]; 474 struct skcipher_request *req; 475 struct scatterlist frags[4]; 476 int fragno; 477 int fraglen; 478 }; 479 480 static int 481 decryptor(struct scatterlist *sg, void *data) 482 { 483 struct decryptor_desc *desc = data; 484 int thislen = desc->fraglen + sg->length; 485 struct crypto_sync_skcipher *tfm = 486 crypto_sync_skcipher_reqtfm(desc->req); 487 int fraglen, ret; 488 489 /* Worst case is 4 fragments: head, end of page 1, start 490 * of page 2, tail. Anything more is a bug. */ 491 BUG_ON(desc->fragno > 3); 492 sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length, 493 sg->offset); 494 desc->fragno++; 495 desc->fraglen += sg->length; 496 497 fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1); 498 thislen -= fraglen; 499 500 if (thislen == 0) 501 return 0; 502 503 sg_mark_end(&desc->frags[desc->fragno - 1]); 504 505 skcipher_request_set_crypt(desc->req, desc->frags, desc->frags, 506 thislen, desc->iv); 507 508 ret = crypto_skcipher_decrypt(desc->req); 509 if (ret) 510 return ret; 511 512 sg_init_table(desc->frags, 4); 513 514 if (fraglen) { 515 sg_set_page(&desc->frags[0], sg_page(sg), fraglen, 516 sg->offset + sg->length - fraglen); 517 desc->fragno = 1; 518 desc->fraglen = fraglen; 519 } else { 520 desc->fragno = 0; 521 desc->fraglen = 0; 522 } 523 return 0; 524 } 525 526 int 527 gss_decrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf, 528 int offset) 529 { 530 int ret; 531 struct decryptor_desc desc; 532 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm); 533 534 /* XXXJBF: */ 535 BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0); 536 537 skcipher_request_set_sync_tfm(req, tfm); 538 skcipher_request_set_callback(req, 0, NULL, NULL); 539 540 memset(desc.iv, 0, sizeof(desc.iv)); 541 desc.req = req; 542 desc.fragno = 0; 543 desc.fraglen = 0; 544 545 sg_init_table(desc.frags, 4); 546 547 ret = xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc); 548 skcipher_request_zero(req); 549 return ret; 550 } 551 552 /* 553 * This function makes the assumption that it was ultimately called 554 * from gss_wrap(). 555 * 556 * The client auth_gss code moves any existing tail data into a 557 * separate page before calling gss_wrap. 558 * The server svcauth_gss code ensures that both the head and the 559 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap. 560 * 561 * Even with that guarantee, this function may be called more than 562 * once in the processing of gss_wrap(). The best we can do is 563 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the 564 * largest expected shift will fit within RPC_MAX_AUTH_SIZE. 565 * At run-time we can verify that a single invocation of this 566 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE. 567 */ 568 569 int 570 xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen) 571 { 572 u8 *p; 573 574 if (shiftlen == 0) 575 return 0; 576 577 BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE); 578 579 p = buf->head[0].iov_base + base; 580 581 memmove(p + shiftlen, p, buf->head[0].iov_len - base); 582 583 buf->head[0].iov_len += shiftlen; 584 buf->len += shiftlen; 585 586 return 0; 587 } 588 589 static u32 590 gss_krb5_cts_crypt(struct crypto_sync_skcipher *cipher, struct xdr_buf *buf, 591 u32 offset, u8 *iv, struct page **pages, int encrypt) 592 { 593 u32 ret; 594 struct scatterlist sg[1]; 595 SYNC_SKCIPHER_REQUEST_ON_STACK(req, cipher); 596 u8 *data; 597 struct page **save_pages; 598 u32 len = buf->len - offset; 599 600 if (len > GSS_KRB5_MAX_BLOCKSIZE * 2) { 601 WARN_ON(0); 602 return -ENOMEM; 603 } 604 data = kmalloc(GSS_KRB5_MAX_BLOCKSIZE * 2, GFP_KERNEL); 605 if (!data) 606 return -ENOMEM; 607 608 /* 609 * For encryption, we want to read from the cleartext 610 * page cache pages, and write the encrypted data to 611 * the supplied xdr_buf pages. 612 */ 613 save_pages = buf->pages; 614 if (encrypt) 615 buf->pages = pages; 616 617 ret = read_bytes_from_xdr_buf(buf, offset, data, len); 618 buf->pages = save_pages; 619 if (ret) 620 goto out; 621 622 sg_init_one(sg, data, len); 623 624 skcipher_request_set_sync_tfm(req, cipher); 625 skcipher_request_set_callback(req, 0, NULL, NULL); 626 skcipher_request_set_crypt(req, sg, sg, len, iv); 627 628 if (encrypt) 629 ret = crypto_skcipher_encrypt(req); 630 else 631 ret = crypto_skcipher_decrypt(req); 632 633 skcipher_request_zero(req); 634 635 if (ret) 636 goto out; 637 638 ret = write_bytes_to_xdr_buf(buf, offset, data, len); 639 640 out: 641 kfree(data); 642 return ret; 643 } 644 645 /** 646 * krb5_cbc_cts_encrypt - encrypt in CBC mode with CTS 647 * @cts_tfm: CBC cipher with CTS 648 * @cbc_tfm: base CBC cipher 649 * @offset: starting byte offset for plaintext 650 * @buf: OUT: output buffer 651 * @pages: plaintext 652 * @iv: output CBC initialization vector, or NULL 653 * @ivsize: size of @iv, in octets 654 * 655 * To provide confidentiality, encrypt using cipher block chaining 656 * with ciphertext stealing. Message integrity is handled separately. 657 * 658 * Return values: 659 * %0: encryption successful 660 * negative errno: encryption could not be completed 661 */ 662 VISIBLE_IF_KUNIT 663 int krb5_cbc_cts_encrypt(struct crypto_sync_skcipher *cts_tfm, 664 struct crypto_sync_skcipher *cbc_tfm, 665 u32 offset, struct xdr_buf *buf, struct page **pages, 666 u8 *iv, unsigned int ivsize) 667 { 668 u32 blocksize, nbytes, nblocks, cbcbytes; 669 struct encryptor_desc desc; 670 int err; 671 672 blocksize = crypto_sync_skcipher_blocksize(cts_tfm); 673 nbytes = buf->len - offset; 674 nblocks = (nbytes + blocksize - 1) / blocksize; 675 cbcbytes = 0; 676 if (nblocks > 2) 677 cbcbytes = (nblocks - 2) * blocksize; 678 679 memset(desc.iv, 0, sizeof(desc.iv)); 680 681 /* Handle block-sized chunks of plaintext with CBC. */ 682 if (cbcbytes) { 683 SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm); 684 685 desc.pos = offset; 686 desc.fragno = 0; 687 desc.fraglen = 0; 688 desc.pages = pages; 689 desc.outbuf = buf; 690 desc.req = req; 691 692 skcipher_request_set_sync_tfm(req, cbc_tfm); 693 skcipher_request_set_callback(req, 0, NULL, NULL); 694 695 sg_init_table(desc.infrags, 4); 696 sg_init_table(desc.outfrags, 4); 697 698 err = xdr_process_buf(buf, offset, cbcbytes, encryptor, &desc); 699 skcipher_request_zero(req); 700 if (err) 701 return err; 702 } 703 704 /* Remaining plaintext is handled with CBC-CTS. */ 705 err = gss_krb5_cts_crypt(cts_tfm, buf, offset + cbcbytes, 706 desc.iv, pages, 1); 707 if (err) 708 return err; 709 710 if (unlikely(iv)) 711 memcpy(iv, desc.iv, ivsize); 712 return 0; 713 } 714 EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_encrypt); 715 716 /** 717 * krb5_cbc_cts_decrypt - decrypt in CBC mode with CTS 718 * @cts_tfm: CBC cipher with CTS 719 * @cbc_tfm: base CBC cipher 720 * @offset: starting byte offset for plaintext 721 * @buf: OUT: output buffer 722 * 723 * Return values: 724 * %0: decryption successful 725 * negative errno: decryption could not be completed 726 */ 727 VISIBLE_IF_KUNIT 728 int krb5_cbc_cts_decrypt(struct crypto_sync_skcipher *cts_tfm, 729 struct crypto_sync_skcipher *cbc_tfm, 730 u32 offset, struct xdr_buf *buf) 731 { 732 u32 blocksize, nblocks, cbcbytes; 733 struct decryptor_desc desc; 734 int err; 735 736 blocksize = crypto_sync_skcipher_blocksize(cts_tfm); 737 nblocks = (buf->len + blocksize - 1) / blocksize; 738 cbcbytes = 0; 739 if (nblocks > 2) 740 cbcbytes = (nblocks - 2) * blocksize; 741 742 memset(desc.iv, 0, sizeof(desc.iv)); 743 744 /* Handle block-sized chunks of plaintext with CBC. */ 745 if (cbcbytes) { 746 SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm); 747 748 desc.fragno = 0; 749 desc.fraglen = 0; 750 desc.req = req; 751 752 skcipher_request_set_sync_tfm(req, cbc_tfm); 753 skcipher_request_set_callback(req, 0, NULL, NULL); 754 755 sg_init_table(desc.frags, 4); 756 757 err = xdr_process_buf(buf, 0, cbcbytes, decryptor, &desc); 758 skcipher_request_zero(req); 759 if (err) 760 return err; 761 } 762 763 /* Remaining plaintext is handled with CBC-CTS. */ 764 return gss_krb5_cts_crypt(cts_tfm, buf, cbcbytes, desc.iv, NULL, 0); 765 } 766 EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_decrypt); 767 768 u32 769 gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset, 770 struct xdr_buf *buf, struct page **pages) 771 { 772 u32 err; 773 struct xdr_netobj hmac; 774 u8 *ecptr; 775 struct crypto_sync_skcipher *cipher, *aux_cipher; 776 struct crypto_ahash *ahash; 777 struct page **save_pages; 778 unsigned int conflen; 779 780 if (kctx->initiate) { 781 cipher = kctx->initiator_enc; 782 aux_cipher = kctx->initiator_enc_aux; 783 ahash = kctx->initiator_integ; 784 } else { 785 cipher = kctx->acceptor_enc; 786 aux_cipher = kctx->acceptor_enc_aux; 787 ahash = kctx->acceptor_integ; 788 } 789 conflen = crypto_sync_skcipher_blocksize(cipher); 790 791 /* hide the gss token header and insert the confounder */ 792 offset += GSS_KRB5_TOK_HDR_LEN; 793 if (xdr_extend_head(buf, offset, conflen)) 794 return GSS_S_FAILURE; 795 krb5_make_confounder(buf->head[0].iov_base + offset, conflen); 796 offset -= GSS_KRB5_TOK_HDR_LEN; 797 798 if (buf->tail[0].iov_base != NULL) { 799 ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len; 800 } else { 801 buf->tail[0].iov_base = buf->head[0].iov_base 802 + buf->head[0].iov_len; 803 buf->tail[0].iov_len = 0; 804 ecptr = buf->tail[0].iov_base; 805 } 806 807 /* copy plaintext gss token header after filler (if any) */ 808 memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN); 809 buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN; 810 buf->len += GSS_KRB5_TOK_HDR_LEN; 811 812 /* Do the HMAC */ 813 hmac.len = GSS_KRB5_MAX_CKSUM_LEN; 814 hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len; 815 816 /* 817 * When we are called, pages points to the real page cache 818 * data -- which we can't go and encrypt! buf->pages points 819 * to scratch pages which we are going to send off to the 820 * client/server. Swap in the plaintext pages to calculate 821 * the hmac. 822 */ 823 save_pages = buf->pages; 824 buf->pages = pages; 825 826 err = gss_krb5_checksum(ahash, NULL, 0, buf, 827 offset + GSS_KRB5_TOK_HDR_LEN, &hmac); 828 buf->pages = save_pages; 829 if (err) 830 return GSS_S_FAILURE; 831 832 err = krb5_cbc_cts_encrypt(cipher, aux_cipher, 833 offset + GSS_KRB5_TOK_HDR_LEN, 834 buf, pages, NULL, 0); 835 if (err) 836 return GSS_S_FAILURE; 837 838 /* Now update buf to account for HMAC */ 839 buf->tail[0].iov_len += kctx->gk5e->cksumlength; 840 buf->len += kctx->gk5e->cksumlength; 841 842 return GSS_S_COMPLETE; 843 } 844 845 u32 846 gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len, 847 struct xdr_buf *buf, u32 *headskip, u32 *tailskip) 848 { 849 struct crypto_sync_skcipher *cipher, *aux_cipher; 850 struct crypto_ahash *ahash; 851 struct xdr_netobj our_hmac_obj; 852 u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN]; 853 u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN]; 854 struct xdr_buf subbuf; 855 u32 ret = 0; 856 857 if (kctx->initiate) { 858 cipher = kctx->acceptor_enc; 859 aux_cipher = kctx->acceptor_enc_aux; 860 ahash = kctx->acceptor_integ; 861 } else { 862 cipher = kctx->initiator_enc; 863 aux_cipher = kctx->initiator_enc_aux; 864 ahash = kctx->initiator_integ; 865 } 866 867 /* create a segment skipping the header and leaving out the checksum */ 868 xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN, 869 (len - offset - GSS_KRB5_TOK_HDR_LEN - 870 kctx->gk5e->cksumlength)); 871 872 ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf); 873 if (ret) 874 goto out_err; 875 876 /* Calculate our hmac over the plaintext data */ 877 our_hmac_obj.len = sizeof(our_hmac); 878 our_hmac_obj.data = our_hmac; 879 ret = gss_krb5_checksum(ahash, NULL, 0, &subbuf, 0, &our_hmac_obj); 880 if (ret) 881 goto out_err; 882 883 /* Get the packet's hmac value */ 884 ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength, 885 pkt_hmac, kctx->gk5e->cksumlength); 886 if (ret) 887 goto out_err; 888 889 if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) { 890 ret = GSS_S_BAD_SIG; 891 goto out_err; 892 } 893 *headskip = crypto_sync_skcipher_blocksize(cipher); 894 *tailskip = kctx->gk5e->cksumlength; 895 out_err: 896 if (ret && ret != GSS_S_BAD_SIG) 897 ret = GSS_S_FAILURE; 898 return ret; 899 } 900 901 /** 902 * krb5_etm_checksum - Compute a MAC for a GSS Wrap token 903 * @cipher: an initialized cipher transform 904 * @tfm: an initialized hash transform 905 * @body: xdr_buf containing an RPC message (body.len is the message length) 906 * @body_offset: byte offset into @body to start checksumming 907 * @cksumout: OUT: a buffer to be filled in with the computed HMAC 908 * 909 * Usually expressed as H = HMAC(K, IV | ciphertext)[1..h] . 910 * 911 * Caller provides the truncation length of the output token (h) in 912 * cksumout.len. 913 * 914 * Return values: 915 * %GSS_S_COMPLETE: Digest computed, @cksumout filled in 916 * %GSS_S_FAILURE: Call failed 917 */ 918 VISIBLE_IF_KUNIT 919 u32 krb5_etm_checksum(struct crypto_sync_skcipher *cipher, 920 struct crypto_ahash *tfm, const struct xdr_buf *body, 921 int body_offset, struct xdr_netobj *cksumout) 922 { 923 unsigned int ivsize = crypto_sync_skcipher_ivsize(cipher); 924 struct ahash_request *req; 925 struct scatterlist sg[1]; 926 u8 *iv, *checksumdata; 927 int err = -ENOMEM; 928 929 checksumdata = kmalloc(crypto_ahash_digestsize(tfm), GFP_KERNEL); 930 if (!checksumdata) 931 return GSS_S_FAILURE; 932 /* For RPCSEC, the "initial cipher state" is always all zeroes. */ 933 iv = kzalloc(ivsize, GFP_KERNEL); 934 if (!iv) 935 goto out_free_mem; 936 937 req = ahash_request_alloc(tfm, GFP_KERNEL); 938 if (!req) 939 goto out_free_mem; 940 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 941 err = crypto_ahash_init(req); 942 if (err) 943 goto out_free_ahash; 944 945 sg_init_one(sg, iv, ivsize); 946 ahash_request_set_crypt(req, sg, NULL, ivsize); 947 err = crypto_ahash_update(req); 948 if (err) 949 goto out_free_ahash; 950 err = xdr_process_buf(body, body_offset, body->len - body_offset, 951 checksummer, req); 952 if (err) 953 goto out_free_ahash; 954 955 ahash_request_set_crypt(req, NULL, checksumdata, 0); 956 err = crypto_ahash_final(req); 957 if (err) 958 goto out_free_ahash; 959 memcpy(cksumout->data, checksumdata, cksumout->len); 960 961 out_free_ahash: 962 ahash_request_free(req); 963 out_free_mem: 964 kfree(iv); 965 kfree_sensitive(checksumdata); 966 return err ? GSS_S_FAILURE : GSS_S_COMPLETE; 967 } 968 EXPORT_SYMBOL_IF_KUNIT(krb5_etm_checksum); 969 970 /** 971 * krb5_etm_encrypt - Encrypt using the RFC 8009 rules 972 * @kctx: Kerberos context 973 * @offset: starting offset of the payload, in bytes 974 * @buf: OUT: send buffer to contain the encrypted payload 975 * @pages: plaintext payload 976 * 977 * The main difference with aes_encrypt is that "The HMAC is 978 * calculated over the cipher state concatenated with the AES 979 * output, instead of being calculated over the confounder and 980 * plaintext. This allows the message receiver to verify the 981 * integrity of the message before decrypting the message." 982 * 983 * RFC 8009 Section 5: 984 * 985 * encryption function: as follows, where E() is AES encryption in 986 * CBC-CS3 mode, and h is the size of truncated HMAC (128 bits or 987 * 192 bits as described above). 988 * 989 * N = random value of length 128 bits (the AES block size) 990 * IV = cipher state 991 * C = E(Ke, N | plaintext, IV) 992 * H = HMAC(Ki, IV | C) 993 * ciphertext = C | H[1..h] 994 * 995 * This encryption formula provides AEAD EtM with key separation. 996 * 997 * Return values: 998 * %GSS_S_COMPLETE: Encryption successful 999 * %GSS_S_FAILURE: Encryption failed 1000 */ 1001 u32 1002 krb5_etm_encrypt(struct krb5_ctx *kctx, u32 offset, 1003 struct xdr_buf *buf, struct page **pages) 1004 { 1005 struct crypto_sync_skcipher *cipher, *aux_cipher; 1006 struct crypto_ahash *ahash; 1007 struct xdr_netobj hmac; 1008 unsigned int conflen; 1009 u8 *ecptr; 1010 u32 err; 1011 1012 if (kctx->initiate) { 1013 cipher = kctx->initiator_enc; 1014 aux_cipher = kctx->initiator_enc_aux; 1015 ahash = kctx->initiator_integ; 1016 } else { 1017 cipher = kctx->acceptor_enc; 1018 aux_cipher = kctx->acceptor_enc_aux; 1019 ahash = kctx->acceptor_integ; 1020 } 1021 conflen = crypto_sync_skcipher_blocksize(cipher); 1022 1023 offset += GSS_KRB5_TOK_HDR_LEN; 1024 if (xdr_extend_head(buf, offset, conflen)) 1025 return GSS_S_FAILURE; 1026 krb5_make_confounder(buf->head[0].iov_base + offset, conflen); 1027 offset -= GSS_KRB5_TOK_HDR_LEN; 1028 1029 if (buf->tail[0].iov_base) { 1030 ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len; 1031 } else { 1032 buf->tail[0].iov_base = buf->head[0].iov_base 1033 + buf->head[0].iov_len; 1034 buf->tail[0].iov_len = 0; 1035 ecptr = buf->tail[0].iov_base; 1036 } 1037 1038 memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN); 1039 buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN; 1040 buf->len += GSS_KRB5_TOK_HDR_LEN; 1041 1042 err = krb5_cbc_cts_encrypt(cipher, aux_cipher, 1043 offset + GSS_KRB5_TOK_HDR_LEN, 1044 buf, pages, NULL, 0); 1045 if (err) 1046 return GSS_S_FAILURE; 1047 1048 hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len; 1049 hmac.len = kctx->gk5e->cksumlength; 1050 err = krb5_etm_checksum(cipher, ahash, 1051 buf, offset + GSS_KRB5_TOK_HDR_LEN, &hmac); 1052 if (err) 1053 goto out_err; 1054 buf->tail[0].iov_len += kctx->gk5e->cksumlength; 1055 buf->len += kctx->gk5e->cksumlength; 1056 1057 return GSS_S_COMPLETE; 1058 1059 out_err: 1060 return GSS_S_FAILURE; 1061 } 1062 1063 /** 1064 * krb5_etm_decrypt - Decrypt using the RFC 8009 rules 1065 * @kctx: Kerberos context 1066 * @offset: starting offset of the ciphertext, in bytes 1067 * @len: 1068 * @buf: 1069 * @headskip: OUT: the enctype's confounder length, in octets 1070 * @tailskip: OUT: the enctype's HMAC length, in octets 1071 * 1072 * RFC 8009 Section 5: 1073 * 1074 * decryption function: as follows, where D() is AES decryption in 1075 * CBC-CS3 mode, and h is the size of truncated HMAC. 1076 * 1077 * (C, H) = ciphertext 1078 * (Note: H is the last h bits of the ciphertext.) 1079 * IV = cipher state 1080 * if H != HMAC(Ki, IV | C)[1..h] 1081 * stop, report error 1082 * (N, P) = D(Ke, C, IV) 1083 * 1084 * Return values: 1085 * %GSS_S_COMPLETE: Decryption successful 1086 * %GSS_S_BAD_SIG: computed HMAC != received HMAC 1087 * %GSS_S_FAILURE: Decryption failed 1088 */ 1089 u32 1090 krb5_etm_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len, 1091 struct xdr_buf *buf, u32 *headskip, u32 *tailskip) 1092 { 1093 struct crypto_sync_skcipher *cipher, *aux_cipher; 1094 u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN]; 1095 u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN]; 1096 struct xdr_netobj our_hmac_obj; 1097 struct crypto_ahash *ahash; 1098 struct xdr_buf subbuf; 1099 u32 ret = 0; 1100 1101 if (kctx->initiate) { 1102 cipher = kctx->acceptor_enc; 1103 aux_cipher = kctx->acceptor_enc_aux; 1104 ahash = kctx->acceptor_integ; 1105 } else { 1106 cipher = kctx->initiator_enc; 1107 aux_cipher = kctx->initiator_enc_aux; 1108 ahash = kctx->initiator_integ; 1109 } 1110 1111 /* Extract the ciphertext into @subbuf. */ 1112 xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN, 1113 (len - offset - GSS_KRB5_TOK_HDR_LEN - 1114 kctx->gk5e->cksumlength)); 1115 1116 our_hmac_obj.data = our_hmac; 1117 our_hmac_obj.len = kctx->gk5e->cksumlength; 1118 ret = krb5_etm_checksum(cipher, ahash, &subbuf, 0, &our_hmac_obj); 1119 if (ret) 1120 goto out_err; 1121 ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength, 1122 pkt_hmac, kctx->gk5e->cksumlength); 1123 if (ret) 1124 goto out_err; 1125 if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) { 1126 ret = GSS_S_BAD_SIG; 1127 goto out_err; 1128 } 1129 1130 ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf); 1131 if (ret) { 1132 ret = GSS_S_FAILURE; 1133 goto out_err; 1134 } 1135 1136 *headskip = crypto_sync_skcipher_blocksize(cipher); 1137 *tailskip = kctx->gk5e->cksumlength; 1138 return GSS_S_COMPLETE; 1139 1140 out_err: 1141 if (ret != GSS_S_BAD_SIG) 1142 ret = GSS_S_FAILURE; 1143 return ret; 1144 } 1145