2 * linux/net/sunrpc/gss_krb5_crypto.c
4 * Copyright (c) 2000-2008 The Regents of the University of Michigan.
7 * Andy Adamson <andros@umich.edu>
8 * Bruce Fields <bfields@umich.edu>
12 * Copyright (C) 1998 by the FundsXpress, INC.
14 * All rights reserved.
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.
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.
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.
37 #include <crypto/hash.h>
38 #include <crypto/skcipher.h>
39 #include <linux/err.h>
40 #include <linux/types.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>
49 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
50 # define RPCDBG_FACILITY RPCDBG_AUTH
55 struct crypto_skcipher
*tfm
,
62 struct scatterlist sg
[1];
63 u8 local_iv
[GSS_KRB5_MAX_BLOCKSIZE
] = {0};
64 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
66 if (length
% crypto_skcipher_blocksize(tfm
) != 0)
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
));
76 memcpy(local_iv
, iv
, crypto_skcipher_ivsize(tfm
));
78 memcpy(out
, in
, length
);
79 sg_init_one(sg
, out
, length
);
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
);
85 ret
= crypto_skcipher_encrypt(req
);
86 skcipher_request_zero(req
);
88 dprintk("RPC: krb5_encrypt returns %d\n", ret
);
94 struct crypto_skcipher
*tfm
,
101 struct scatterlist sg
[1];
102 u8 local_iv
[GSS_KRB5_MAX_BLOCKSIZE
] = {0};
103 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
105 if (length
% crypto_skcipher_blocksize(tfm
) != 0)
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
));
114 memcpy(local_iv
,iv
, crypto_skcipher_ivsize(tfm
));
116 memcpy(out
, in
, length
);
117 sg_init_one(sg
, out
, length
);
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
);
123 ret
= crypto_skcipher_decrypt(req
);
124 skcipher_request_zero(req
);
126 dprintk("RPC: gss_k5decrypt returns %d\n",ret
);
131 checksummer(struct scatterlist
*sg
, void *data
)
133 struct ahash_request
*req
= data
;
135 ahash_request_set_crypt(req
, sg
, NULL
, sg
->length
);
137 return crypto_ahash_update(req
);
141 arcfour_hmac_md5_usage_to_salt(unsigned int usage
, u8 salt
[4])
143 unsigned int ms_usage
;
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;
164 make_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
)
168 struct scatterlist sg
[1];
172 struct crypto_ahash
*md5
;
173 struct crypto_ahash
*hmac_md5
;
174 struct ahash_request
*req
;
176 if (cksumkey
== NULL
)
177 return GSS_S_FAILURE
;
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
;
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
;
190 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
192 return GSS_S_FAILURE
;
194 md5
= crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC
);
198 hmac_md5
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0,
200 if (IS_ERR(hmac_md5
))
203 req
= ahash_request_alloc(md5
, GFP_NOFS
);
205 goto out_free_hmac_md5
;
207 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
209 err
= crypto_ahash_init(req
);
212 sg_init_one(sg
, rc4salt
, 4);
213 ahash_request_set_crypt(req
, sg
, NULL
, 4);
214 err
= crypto_ahash_update(req
);
218 sg_init_one(sg
, header
, hdrlen
);
219 ahash_request_set_crypt(req
, sg
, NULL
, hdrlen
);
220 err
= crypto_ahash_update(req
);
223 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
227 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
228 err
= crypto_ahash_final(req
);
232 ahash_request_free(req
);
233 req
= ahash_request_alloc(hmac_md5
, GFP_NOFS
);
235 goto out_free_hmac_md5
;
237 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
239 err
= crypto_ahash_init(req
);
242 err
= crypto_ahash_setkey(hmac_md5
, cksumkey
, kctx
->gk5e
->keylength
);
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
);
253 memcpy(cksumout
->data
, checksumdata
, kctx
->gk5e
->cksumlength
);
254 cksumout
->len
= kctx
->gk5e
->cksumlength
;
256 ahash_request_free(req
);
258 crypto_free_ahash(hmac_md5
);
260 crypto_free_ahash(md5
);
263 return err
? GSS_S_FAILURE
: 0;
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
272 make_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
)
276 struct crypto_ahash
*tfm
;
277 struct ahash_request
*req
;
278 struct scatterlist sg
[1];
281 unsigned int checksumlen
;
283 if (kctx
->gk5e
->ctype
== CKSUMTYPE_HMAC_MD5_ARCFOUR
)
284 return make_checksum_hmac_md5(kctx
, header
, hdrlen
,
286 cksumkey
, usage
, cksumout
);
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
;
294 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
295 if (checksumdata
== NULL
)
296 return GSS_S_FAILURE
;
298 tfm
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0, CRYPTO_ALG_ASYNC
);
302 req
= ahash_request_alloc(tfm
, GFP_NOFS
);
306 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
308 checksumlen
= crypto_ahash_digestsize(tfm
);
310 if (cksumkey
!= NULL
) {
311 err
= crypto_ahash_setkey(tfm
, cksumkey
,
312 kctx
->gk5e
->keylength
);
317 err
= crypto_ahash_init(req
);
320 sg_init_one(sg
, header
, hdrlen
);
321 ahash_request_set_crypt(req
, sg
, NULL
, hdrlen
);
322 err
= crypto_ahash_update(req
);
325 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
329 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
330 err
= crypto_ahash_final(req
);
334 switch (kctx
->gk5e
->ctype
) {
335 case CKSUMTYPE_RSA_MD5
:
336 err
= kctx
->gk5e
->encrypt(kctx
->seq
, NULL
, checksumdata
,
337 checksumdata
, checksumlen
);
340 memcpy(cksumout
->data
,
341 checksumdata
+ checksumlen
- kctx
->gk5e
->cksumlength
,
342 kctx
->gk5e
->cksumlength
);
344 case CKSUMTYPE_HMAC_SHA1_DES3
:
345 memcpy(cksumout
->data
, checksumdata
, kctx
->gk5e
->cksumlength
);
351 cksumout
->len
= kctx
->gk5e
->cksumlength
;
353 ahash_request_free(req
);
355 crypto_free_ahash(tfm
);
358 return err
? GSS_S_FAILURE
: 0;
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.
369 make_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
)
373 struct crypto_ahash
*tfm
;
374 struct ahash_request
*req
;
375 struct scatterlist sg
[1];
378 unsigned int checksumlen
;
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
;
385 if (cksumkey
== NULL
) {
386 dprintk("%s: no key supplied for %s\n",
387 __func__
, kctx
->gk5e
->name
);
388 return GSS_S_FAILURE
;
391 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
393 return GSS_S_FAILURE
;
395 tfm
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0, CRYPTO_ALG_ASYNC
);
398 checksumlen
= crypto_ahash_digestsize(tfm
);
400 req
= ahash_request_alloc(tfm
, GFP_NOFS
);
404 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
406 err
= crypto_ahash_setkey(tfm
, cksumkey
, kctx
->gk5e
->keylength
);
410 err
= crypto_ahash_init(req
);
413 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
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
);
424 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
425 err
= crypto_ahash_final(req
);
429 cksumout
->len
= kctx
->gk5e
->cksumlength
;
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
);
442 ahash_request_free(req
);
444 crypto_free_ahash(tfm
);
447 return err
? GSS_S_FAILURE
: 0;
450 struct encryptor_desc
{
451 u8 iv
[GSS_KRB5_MAX_BLOCKSIZE
];
452 struct skcipher_request
*req
;
454 struct xdr_buf
*outbuf
;
456 struct scatterlist infrags
[4];
457 struct scatterlist outfrags
[4];
463 encryptor(struct scatterlist
*sg
, void *data
)
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
;
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);
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
];
483 in_page
= sg_page(sg
);
485 sg_set_page(&desc
->infrags
[desc
->fragno
], in_page
, sg
->length
,
487 sg_set_page(&desc
->outfrags
[desc
->fragno
], sg_page(sg
), sg
->length
,
490 desc
->fraglen
+= sg
->length
;
491 desc
->pos
+= sg
->length
;
493 fraglen
= thislen
& (crypto_skcipher_blocksize(tfm
) - 1);
499 sg_mark_end(&desc
->infrags
[desc
->fragno
- 1]);
500 sg_mark_end(&desc
->outfrags
[desc
->fragno
- 1]);
502 skcipher_request_set_crypt(desc
->req
, desc
->infrags
, desc
->outfrags
,
505 ret
= crypto_skcipher_encrypt(desc
->req
);
509 sg_init_table(desc
->infrags
, 4);
510 sg_init_table(desc
->outfrags
, 4);
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
);
518 desc
->fraglen
= fraglen
;
527 gss_encrypt_xdr_buf(struct crypto_skcipher
*tfm
, struct xdr_buf
*buf
,
528 int offset
, struct page
**pages
)
531 struct encryptor_desc desc
;
532 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
534 BUG_ON((buf
->len
- offset
) % crypto_skcipher_blocksize(tfm
) != 0);
536 skcipher_request_set_tfm(req
, tfm
);
537 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
539 memset(desc
.iv
, 0, sizeof(desc
.iv
));
547 sg_init_table(desc
.infrags
, 4);
548 sg_init_table(desc
.outfrags
, 4);
550 ret
= xdr_process_buf(buf
, offset
, buf
->len
- offset
, encryptor
, &desc
);
551 skcipher_request_zero(req
);
555 struct decryptor_desc
{
556 u8 iv
[GSS_KRB5_MAX_BLOCKSIZE
];
557 struct skcipher_request
*req
;
558 struct scatterlist frags
[4];
564 decryptor(struct scatterlist
*sg
, void *data
)
566 struct decryptor_desc
*desc
= data
;
567 int thislen
= desc
->fraglen
+ sg
->length
;
568 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(desc
->req
);
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
,
577 desc
->fraglen
+= sg
->length
;
579 fraglen
= thislen
& (crypto_skcipher_blocksize(tfm
) - 1);
585 sg_mark_end(&desc
->frags
[desc
->fragno
- 1]);
587 skcipher_request_set_crypt(desc
->req
, desc
->frags
, desc
->frags
,
590 ret
= crypto_skcipher_decrypt(desc
->req
);
594 sg_init_table(desc
->frags
, 4);
597 sg_set_page(&desc
->frags
[0], sg_page(sg
), fraglen
,
598 sg
->offset
+ sg
->length
- fraglen
);
600 desc
->fraglen
= fraglen
;
609 gss_decrypt_xdr_buf(struct crypto_skcipher
*tfm
, struct xdr_buf
*buf
,
613 struct decryptor_desc desc
;
614 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
617 BUG_ON((buf
->len
- offset
) % crypto_skcipher_blocksize(tfm
) != 0);
619 skcipher_request_set_tfm(req
, tfm
);
620 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
622 memset(desc
.iv
, 0, sizeof(desc
.iv
));
627 sg_init_table(desc
.frags
, 4);
629 ret
= xdr_process_buf(buf
, offset
, buf
->len
- offset
, decryptor
, &desc
);
630 skcipher_request_zero(req
);
635 * This function makes the assumption that it was ultimately called
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.
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.
652 xdr_extend_head(struct xdr_buf
*buf
, unsigned int base
, unsigned int shiftlen
)
659 BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED
> RPC_MAX_AUTH_SIZE
);
660 BUG_ON(shiftlen
> RPC_MAX_AUTH_SIZE
);
662 p
= buf
->head
[0].iov_base
+ base
;
664 memmove(p
+ shiftlen
, p
, buf
->head
[0].iov_len
- base
);
666 buf
->head
[0].iov_len
+= shiftlen
;
667 buf
->len
+= shiftlen
;
673 gss_krb5_cts_crypt(struct crypto_skcipher
*cipher
, struct xdr_buf
*buf
,
674 u32 offset
, u8
*iv
, struct page
**pages
, int encrypt
)
677 struct scatterlist sg
[1];
678 SKCIPHER_REQUEST_ON_STACK(req
, cipher
);
680 struct page
**save_pages
;
681 u32 len
= buf
->len
- offset
;
683 if (len
> GSS_KRB5_MAX_BLOCKSIZE
* 2) {
687 data
= kmalloc(GSS_KRB5_MAX_BLOCKSIZE
* 2, GFP_NOFS
);
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.
696 save_pages
= buf
->pages
;
700 ret
= read_bytes_from_xdr_buf(buf
, offset
, data
, len
);
701 buf
->pages
= save_pages
;
705 sg_init_one(sg
, data
, len
);
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
);
712 ret
= crypto_skcipher_encrypt(req
);
714 ret
= crypto_skcipher_decrypt(req
);
716 skcipher_request_zero(req
);
721 ret
= write_bytes_to_xdr_buf(buf
, offset
, data
, len
);
729 gss_krb5_aes_encrypt(struct krb5_ctx
*kctx
, u32 offset
,
730 struct xdr_buf
*buf
, struct page
**pages
)
733 struct xdr_netobj hmac
;
736 struct crypto_skcipher
*cipher
, *aux_cipher
;
738 struct page
**save_pages
;
740 struct encryptor_desc desc
;
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
;
750 cipher
= kctx
->acceptor_enc
;
751 aux_cipher
= kctx
->acceptor_enc_aux
;
752 cksumkey
= kctx
->acceptor_integ
;
753 usage
= KG_USAGE_ACCEPTOR_SEAL
;
755 blocksize
= crypto_skcipher_blocksize(cipher
);
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
;
764 if (buf
->tail
[0].iov_base
!= NULL
) {
765 ecptr
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
;
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
;
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
;
779 hmac
.len
= GSS_KRB5_MAX_CKSUM_LEN
;
780 hmac
.data
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
;
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
789 save_pages
= buf
->pages
;
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
;
797 return GSS_S_FAILURE
;
799 nbytes
= buf
->len
- offset
- GSS_KRB5_TOK_HDR_LEN
;
800 nblocks
= (nbytes
+ blocksize
- 1) / blocksize
;
803 cbcbytes
= (nblocks
- 2) * blocksize
;
805 memset(desc
.iv
, 0, sizeof(desc
.iv
));
808 SKCIPHER_REQUEST_ON_STACK(req
, aux_cipher
);
810 desc
.pos
= offset
+ GSS_KRB5_TOK_HDR_LEN
;
817 skcipher_request_set_tfm(req
, aux_cipher
);
818 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
820 sg_init_table(desc
.infrags
, 4);
821 sg_init_table(desc
.outfrags
, 4);
823 err
= xdr_process_buf(buf
, offset
+ GSS_KRB5_TOK_HDR_LEN
,
824 cbcbytes
, encryptor
, &desc
);
825 skcipher_request_zero(req
);
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
,
839 /* Now update buf to account for HMAC */
840 buf
->tail
[0].iov_len
+= kctx
->gk5e
->cksumlength
;
841 buf
->len
+= kctx
->gk5e
->cksumlength
;
850 gss_krb5_aes_decrypt(struct krb5_ctx
*kctx
, u32 offset
, struct xdr_buf
*buf
,
851 u32
*headskip
, u32
*tailskip
)
853 struct xdr_buf subbuf
;
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
;
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
;
870 cipher
= kctx
->initiator_enc
;
871 aux_cipher
= kctx
->initiator_enc_aux
;
872 cksum_key
= kctx
->initiator_integ
;
873 usage
= KG_USAGE_INITIATOR_SEAL
;
875 blocksize
= crypto_skcipher_blocksize(cipher
);
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
));
883 nblocks
= (subbuf
.len
+ blocksize
- 1) / blocksize
;
887 cbcbytes
= (nblocks
- 2) * blocksize
;
889 memset(desc
.iv
, 0, sizeof(desc
.iv
));
892 SKCIPHER_REQUEST_ON_STACK(req
, aux_cipher
);
898 skcipher_request_set_tfm(req
, aux_cipher
);
899 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
901 sg_init_table(desc
.frags
, 4);
903 ret
= xdr_process_buf(&subbuf
, 0, cbcbytes
, decryptor
, &desc
);
904 skcipher_request_zero(req
);
909 /* Make sure IV carries forward from any CBC results. */
910 ret
= gss_krb5_cts_crypt(cipher
, &subbuf
, cbcbytes
, desc
.iv
, NULL
, 0);
915 /* Calculate our hmac over the plaintext data */
916 our_hmac_obj
.len
= sizeof(our_hmac
);
917 our_hmac_obj
.data
= our_hmac
;
919 ret
= make_checksum_v2(kctx
, NULL
, 0, &subbuf
, 0,
920 cksum_key
, usage
, &our_hmac_obj
);
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
);
930 if (memcmp(pkt_hmac
, our_hmac
, kctx
->gk5e
->cksumlength
) != 0) {
934 *headskip
= kctx
->gk5e
->conflen
;
935 *tailskip
= kctx
->gk5e
->cksumlength
;
937 if (ret
&& ret
!= GSS_S_BAD_SIG
)
943 * Compute Kseq given the initial session key and the checksum.
944 * Set the key of the given cipher.
947 krb5_rc4_setup_seq_key(struct krb5_ctx
*kctx
, struct crypto_skcipher
*cipher
,
948 unsigned char *cksum
)
950 struct crypto_shash
*hmac
;
951 struct shash_desc
*desc
;
952 u8 Kseq
[GSS_KRB5_MAX_KEYLEN
];
953 u32 zeroconstant
= 0;
956 dprintk("%s: entered\n", __func__
);
958 hmac
= crypto_alloc_shash(kctx
->gk5e
->cksum_name
, 0, 0);
960 dprintk("%s: error %ld, allocating hash '%s'\n",
961 __func__
, PTR_ERR(hmac
), kctx
->gk5e
->cksum_name
);
962 return PTR_ERR(hmac
);
965 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(hmac
),
968 dprintk("%s: failed to allocate shash descriptor for '%s'\n",
969 __func__
, kctx
->gk5e
->cksum_name
);
970 crypto_free_shash(hmac
);
977 /* Compute intermediate Kseq from session key */
978 err
= crypto_shash_setkey(hmac
, kctx
->Ksess
, kctx
->gk5e
->keylength
);
982 err
= crypto_shash_digest(desc
, (u8
*)&zeroconstant
, 4, Kseq
);
986 /* Compute final Kseq from the checksum and intermediate Kseq */
987 err
= crypto_shash_setkey(hmac
, Kseq
, kctx
->gk5e
->keylength
);
991 err
= crypto_shash_digest(desc
, cksum
, 8, Kseq
);
995 err
= crypto_skcipher_setkey(cipher
, Kseq
, kctx
->gk5e
->keylength
);
1003 crypto_free_shash(hmac
);
1004 dprintk("%s: returning %d\n", __func__
, err
);
1009 * Compute Kcrypt given the initial session key and the plaintext seqnum.
1010 * Set the key of cipher kctx->enc.
1013 krb5_rc4_setup_enc_key(struct krb5_ctx
*kctx
, struct crypto_skcipher
*cipher
,
1016 struct crypto_shash
*hmac
;
1017 struct shash_desc
*desc
;
1018 u8 Kcrypt
[GSS_KRB5_MAX_KEYLEN
];
1019 u8 zeroconstant
[4] = {0};
1023 dprintk("%s: entered, seqnum %u\n", __func__
, seqnum
);
1025 hmac
= crypto_alloc_shash(kctx
->gk5e
->cksum_name
, 0, 0);
1027 dprintk("%s: error %ld, allocating hash '%s'\n",
1028 __func__
, PTR_ERR(hmac
), kctx
->gk5e
->cksum_name
);
1029 return PTR_ERR(hmac
);
1032 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(hmac
),
1035 dprintk("%s: failed to allocate shash descriptor for '%s'\n",
1036 __func__
, kctx
->gk5e
->cksum_name
);
1037 crypto_free_shash(hmac
);
1044 /* Compute intermediate Kcrypt from session key */
1045 for (i
= 0; i
< kctx
->gk5e
->keylength
; i
++)
1046 Kcrypt
[i
] = kctx
->Ksess
[i
] ^ 0xf0;
1048 err
= crypto_shash_setkey(hmac
, Kcrypt
, kctx
->gk5e
->keylength
);
1052 err
= crypto_shash_digest(desc
, zeroconstant
, 4, Kcrypt
);
1056 /* Compute final Kcrypt from the seqnum and intermediate Kcrypt */
1057 err
= crypto_shash_setkey(hmac
, Kcrypt
, kctx
->gk5e
->keylength
);
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);
1066 err
= crypto_shash_digest(desc
, seqnumarray
, 4, Kcrypt
);
1070 err
= crypto_skcipher_setkey(cipher
, Kcrypt
, kctx
->gk5e
->keylength
);
1078 crypto_free_shash(hmac
);
1079 dprintk("%s: returning %d\n", __func__
, err
);