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/algapi.h>
38 #include <crypto/hash.h>
39 #include <crypto/skcipher.h>
40 #include <linux/err.h>
41 #include <linux/types.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>
50 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
51 # define RPCDBG_FACILITY RPCDBG_AUTH
56 struct crypto_skcipher
*tfm
,
63 struct scatterlist sg
[1];
64 u8 local_iv
[GSS_KRB5_MAX_BLOCKSIZE
] = {0};
65 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
67 if (length
% crypto_skcipher_blocksize(tfm
) != 0)
70 if (crypto_skcipher_ivsize(tfm
) > GSS_KRB5_MAX_BLOCKSIZE
) {
71 dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
72 crypto_skcipher_ivsize(tfm
));
77 memcpy(local_iv
, iv
, crypto_skcipher_ivsize(tfm
));
79 memcpy(out
, in
, length
);
80 sg_init_one(sg
, out
, length
);
82 skcipher_request_set_tfm(req
, tfm
);
83 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
84 skcipher_request_set_crypt(req
, sg
, sg
, length
, local_iv
);
86 ret
= crypto_skcipher_encrypt(req
);
87 skcipher_request_zero(req
);
89 dprintk("RPC: krb5_encrypt returns %d\n", ret
);
95 struct crypto_skcipher
*tfm
,
102 struct scatterlist sg
[1];
103 u8 local_iv
[GSS_KRB5_MAX_BLOCKSIZE
] = {0};
104 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
106 if (length
% crypto_skcipher_blocksize(tfm
) != 0)
109 if (crypto_skcipher_ivsize(tfm
) > GSS_KRB5_MAX_BLOCKSIZE
) {
110 dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
111 crypto_skcipher_ivsize(tfm
));
115 memcpy(local_iv
,iv
, crypto_skcipher_ivsize(tfm
));
117 memcpy(out
, in
, length
);
118 sg_init_one(sg
, out
, length
);
120 skcipher_request_set_tfm(req
, tfm
);
121 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
122 skcipher_request_set_crypt(req
, sg
, sg
, length
, local_iv
);
124 ret
= crypto_skcipher_decrypt(req
);
125 skcipher_request_zero(req
);
127 dprintk("RPC: gss_k5decrypt returns %d\n",ret
);
132 checksummer(struct scatterlist
*sg
, void *data
)
134 struct ahash_request
*req
= data
;
136 ahash_request_set_crypt(req
, sg
, NULL
, sg
->length
);
138 return crypto_ahash_update(req
);
142 arcfour_hmac_md5_usage_to_salt(unsigned int usage
, u8 salt
[4])
144 unsigned int ms_usage
;
156 salt
[0] = (ms_usage
>> 0) & 0xff;
157 salt
[1] = (ms_usage
>> 8) & 0xff;
158 salt
[2] = (ms_usage
>> 16) & 0xff;
159 salt
[3] = (ms_usage
>> 24) & 0xff;
165 make_checksum_hmac_md5(struct krb5_ctx
*kctx
, char *header
, int hdrlen
,
166 struct xdr_buf
*body
, int body_offset
, u8
*cksumkey
,
167 unsigned int usage
, struct xdr_netobj
*cksumout
)
169 struct scatterlist sg
[1];
173 struct crypto_ahash
*md5
;
174 struct crypto_ahash
*hmac_md5
;
175 struct ahash_request
*req
;
177 if (cksumkey
== NULL
)
178 return GSS_S_FAILURE
;
180 if (cksumout
->len
< kctx
->gk5e
->cksumlength
) {
181 dprintk("%s: checksum buffer length, %u, too small for %s\n",
182 __func__
, cksumout
->len
, kctx
->gk5e
->name
);
183 return GSS_S_FAILURE
;
186 if (arcfour_hmac_md5_usage_to_salt(usage
, rc4salt
)) {
187 dprintk("%s: invalid usage value %u\n", __func__
, usage
);
188 return GSS_S_FAILURE
;
191 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
193 return GSS_S_FAILURE
;
195 md5
= crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC
);
199 hmac_md5
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0,
201 if (IS_ERR(hmac_md5
))
204 req
= ahash_request_alloc(md5
, GFP_NOFS
);
206 goto out_free_hmac_md5
;
208 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
210 err
= crypto_ahash_init(req
);
213 sg_init_one(sg
, rc4salt
, 4);
214 ahash_request_set_crypt(req
, sg
, NULL
, 4);
215 err
= crypto_ahash_update(req
);
219 sg_init_one(sg
, header
, hdrlen
);
220 ahash_request_set_crypt(req
, sg
, NULL
, hdrlen
);
221 err
= crypto_ahash_update(req
);
224 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
228 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
229 err
= crypto_ahash_final(req
);
233 ahash_request_free(req
);
234 req
= ahash_request_alloc(hmac_md5
, GFP_NOFS
);
236 goto out_free_hmac_md5
;
238 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
240 err
= crypto_ahash_setkey(hmac_md5
, cksumkey
, kctx
->gk5e
->keylength
);
244 sg_init_one(sg
, checksumdata
, crypto_ahash_digestsize(md5
));
245 ahash_request_set_crypt(req
, sg
, checksumdata
,
246 crypto_ahash_digestsize(md5
));
247 err
= crypto_ahash_digest(req
);
251 memcpy(cksumout
->data
, checksumdata
, kctx
->gk5e
->cksumlength
);
252 cksumout
->len
= kctx
->gk5e
->cksumlength
;
254 ahash_request_free(req
);
256 crypto_free_ahash(hmac_md5
);
258 crypto_free_ahash(md5
);
261 return err
? GSS_S_FAILURE
: 0;
265 * checksum the plaintext data and hdrlen bytes of the token header
266 * The checksum is performed over the first 8 bytes of the
267 * gss token header and then over the data body
270 make_checksum(struct krb5_ctx
*kctx
, char *header
, int hdrlen
,
271 struct xdr_buf
*body
, int body_offset
, u8
*cksumkey
,
272 unsigned int usage
, struct xdr_netobj
*cksumout
)
274 struct crypto_ahash
*tfm
;
275 struct ahash_request
*req
;
276 struct scatterlist sg
[1];
279 unsigned int checksumlen
;
281 if (kctx
->gk5e
->ctype
== CKSUMTYPE_HMAC_MD5_ARCFOUR
)
282 return make_checksum_hmac_md5(kctx
, header
, hdrlen
,
284 cksumkey
, usage
, cksumout
);
286 if (cksumout
->len
< kctx
->gk5e
->cksumlength
) {
287 dprintk("%s: checksum buffer length, %u, too small for %s\n",
288 __func__
, cksumout
->len
, kctx
->gk5e
->name
);
289 return GSS_S_FAILURE
;
292 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
293 if (checksumdata
== NULL
)
294 return GSS_S_FAILURE
;
296 tfm
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0, CRYPTO_ALG_ASYNC
);
300 req
= ahash_request_alloc(tfm
, GFP_NOFS
);
304 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
306 checksumlen
= crypto_ahash_digestsize(tfm
);
308 if (cksumkey
!= NULL
) {
309 err
= crypto_ahash_setkey(tfm
, cksumkey
,
310 kctx
->gk5e
->keylength
);
315 err
= crypto_ahash_init(req
);
318 sg_init_one(sg
, header
, hdrlen
);
319 ahash_request_set_crypt(req
, sg
, NULL
, hdrlen
);
320 err
= crypto_ahash_update(req
);
323 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
327 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
328 err
= crypto_ahash_final(req
);
332 switch (kctx
->gk5e
->ctype
) {
333 case CKSUMTYPE_RSA_MD5
:
334 err
= kctx
->gk5e
->encrypt(kctx
->seq
, NULL
, checksumdata
,
335 checksumdata
, checksumlen
);
338 memcpy(cksumout
->data
,
339 checksumdata
+ checksumlen
- kctx
->gk5e
->cksumlength
,
340 kctx
->gk5e
->cksumlength
);
342 case CKSUMTYPE_HMAC_SHA1_DES3
:
343 memcpy(cksumout
->data
, checksumdata
, kctx
->gk5e
->cksumlength
);
349 cksumout
->len
= kctx
->gk5e
->cksumlength
;
351 ahash_request_free(req
);
353 crypto_free_ahash(tfm
);
356 return err
? GSS_S_FAILURE
: 0;
360 * checksum the plaintext data and hdrlen bytes of the token header
361 * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
362 * body then over the first 16 octets of the MIC token
363 * Inclusion of the header data in the calculation of the
364 * checksum is optional.
367 make_checksum_v2(struct krb5_ctx
*kctx
, char *header
, int hdrlen
,
368 struct xdr_buf
*body
, int body_offset
, u8
*cksumkey
,
369 unsigned int usage
, struct xdr_netobj
*cksumout
)
371 struct crypto_ahash
*tfm
;
372 struct ahash_request
*req
;
373 struct scatterlist sg
[1];
376 unsigned int checksumlen
;
378 if (kctx
->gk5e
->keyed_cksum
== 0) {
379 dprintk("%s: expected keyed hash for %s\n",
380 __func__
, kctx
->gk5e
->name
);
381 return GSS_S_FAILURE
;
383 if (cksumkey
== NULL
) {
384 dprintk("%s: no key supplied for %s\n",
385 __func__
, kctx
->gk5e
->name
);
386 return GSS_S_FAILURE
;
389 checksumdata
= kmalloc(GSS_KRB5_MAX_CKSUM_LEN
, GFP_NOFS
);
391 return GSS_S_FAILURE
;
393 tfm
= crypto_alloc_ahash(kctx
->gk5e
->cksum_name
, 0, CRYPTO_ALG_ASYNC
);
396 checksumlen
= crypto_ahash_digestsize(tfm
);
398 req
= ahash_request_alloc(tfm
, GFP_NOFS
);
402 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_SLEEP
, NULL
, NULL
);
404 err
= crypto_ahash_setkey(tfm
, cksumkey
, kctx
->gk5e
->keylength
);
408 err
= crypto_ahash_init(req
);
411 err
= xdr_process_buf(body
, body_offset
, body
->len
- body_offset
,
415 if (header
!= NULL
) {
416 sg_init_one(sg
, header
, hdrlen
);
417 ahash_request_set_crypt(req
, sg
, NULL
, hdrlen
);
418 err
= crypto_ahash_update(req
);
422 ahash_request_set_crypt(req
, NULL
, checksumdata
, 0);
423 err
= crypto_ahash_final(req
);
427 cksumout
->len
= kctx
->gk5e
->cksumlength
;
429 switch (kctx
->gk5e
->ctype
) {
430 case CKSUMTYPE_HMAC_SHA1_96_AES128
:
431 case CKSUMTYPE_HMAC_SHA1_96_AES256
:
432 /* note that this truncates the hash */
433 memcpy(cksumout
->data
, checksumdata
, kctx
->gk5e
->cksumlength
);
440 ahash_request_free(req
);
442 crypto_free_ahash(tfm
);
445 return err
? GSS_S_FAILURE
: 0;
448 struct encryptor_desc
{
449 u8 iv
[GSS_KRB5_MAX_BLOCKSIZE
];
450 struct skcipher_request
*req
;
452 struct xdr_buf
*outbuf
;
454 struct scatterlist infrags
[4];
455 struct scatterlist outfrags
[4];
461 encryptor(struct scatterlist
*sg
, void *data
)
463 struct encryptor_desc
*desc
= data
;
464 struct xdr_buf
*outbuf
= desc
->outbuf
;
465 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(desc
->req
);
466 struct page
*in_page
;
467 int thislen
= desc
->fraglen
+ sg
->length
;
471 /* Worst case is 4 fragments: head, end of page 1, start
472 * of page 2, tail. Anything more is a bug. */
473 BUG_ON(desc
->fragno
> 3);
475 page_pos
= desc
->pos
- outbuf
->head
[0].iov_len
;
476 if (page_pos
>= 0 && page_pos
< outbuf
->page_len
) {
477 /* pages are not in place: */
478 int i
= (page_pos
+ outbuf
->page_base
) >> PAGE_SHIFT
;
479 in_page
= desc
->pages
[i
];
481 in_page
= sg_page(sg
);
483 sg_set_page(&desc
->infrags
[desc
->fragno
], in_page
, sg
->length
,
485 sg_set_page(&desc
->outfrags
[desc
->fragno
], sg_page(sg
), sg
->length
,
488 desc
->fraglen
+= sg
->length
;
489 desc
->pos
+= sg
->length
;
491 fraglen
= thislen
& (crypto_skcipher_blocksize(tfm
) - 1);
497 sg_mark_end(&desc
->infrags
[desc
->fragno
- 1]);
498 sg_mark_end(&desc
->outfrags
[desc
->fragno
- 1]);
500 skcipher_request_set_crypt(desc
->req
, desc
->infrags
, desc
->outfrags
,
503 ret
= crypto_skcipher_encrypt(desc
->req
);
507 sg_init_table(desc
->infrags
, 4);
508 sg_init_table(desc
->outfrags
, 4);
511 sg_set_page(&desc
->outfrags
[0], sg_page(sg
), fraglen
,
512 sg
->offset
+ sg
->length
- fraglen
);
513 desc
->infrags
[0] = desc
->outfrags
[0];
514 sg_assign_page(&desc
->infrags
[0], in_page
);
516 desc
->fraglen
= fraglen
;
525 gss_encrypt_xdr_buf(struct crypto_skcipher
*tfm
, struct xdr_buf
*buf
,
526 int offset
, struct page
**pages
)
529 struct encryptor_desc desc
;
530 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
532 BUG_ON((buf
->len
- offset
) % crypto_skcipher_blocksize(tfm
) != 0);
534 skcipher_request_set_tfm(req
, tfm
);
535 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
537 memset(desc
.iv
, 0, sizeof(desc
.iv
));
545 sg_init_table(desc
.infrags
, 4);
546 sg_init_table(desc
.outfrags
, 4);
548 ret
= xdr_process_buf(buf
, offset
, buf
->len
- offset
, encryptor
, &desc
);
549 skcipher_request_zero(req
);
553 struct decryptor_desc
{
554 u8 iv
[GSS_KRB5_MAX_BLOCKSIZE
];
555 struct skcipher_request
*req
;
556 struct scatterlist frags
[4];
562 decryptor(struct scatterlist
*sg
, void *data
)
564 struct decryptor_desc
*desc
= data
;
565 int thislen
= desc
->fraglen
+ sg
->length
;
566 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(desc
->req
);
569 /* Worst case is 4 fragments: head, end of page 1, start
570 * of page 2, tail. Anything more is a bug. */
571 BUG_ON(desc
->fragno
> 3);
572 sg_set_page(&desc
->frags
[desc
->fragno
], sg_page(sg
), sg
->length
,
575 desc
->fraglen
+= sg
->length
;
577 fraglen
= thislen
& (crypto_skcipher_blocksize(tfm
) - 1);
583 sg_mark_end(&desc
->frags
[desc
->fragno
- 1]);
585 skcipher_request_set_crypt(desc
->req
, desc
->frags
, desc
->frags
,
588 ret
= crypto_skcipher_decrypt(desc
->req
);
592 sg_init_table(desc
->frags
, 4);
595 sg_set_page(&desc
->frags
[0], sg_page(sg
), fraglen
,
596 sg
->offset
+ sg
->length
- fraglen
);
598 desc
->fraglen
= fraglen
;
607 gss_decrypt_xdr_buf(struct crypto_skcipher
*tfm
, struct xdr_buf
*buf
,
611 struct decryptor_desc desc
;
612 SKCIPHER_REQUEST_ON_STACK(req
, tfm
);
615 BUG_ON((buf
->len
- offset
) % crypto_skcipher_blocksize(tfm
) != 0);
617 skcipher_request_set_tfm(req
, tfm
);
618 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
620 memset(desc
.iv
, 0, sizeof(desc
.iv
));
625 sg_init_table(desc
.frags
, 4);
627 ret
= xdr_process_buf(buf
, offset
, buf
->len
- offset
, decryptor
, &desc
);
628 skcipher_request_zero(req
);
633 * This function makes the assumption that it was ultimately called
636 * The client auth_gss code moves any existing tail data into a
637 * separate page before calling gss_wrap.
638 * The server svcauth_gss code ensures that both the head and the
639 * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
641 * Even with that guarantee, this function may be called more than
642 * once in the processing of gss_wrap(). The best we can do is
643 * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
644 * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
645 * At run-time we can verify that a single invocation of this
646 * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
650 xdr_extend_head(struct xdr_buf
*buf
, unsigned int base
, unsigned int shiftlen
)
657 BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED
> RPC_MAX_AUTH_SIZE
);
658 BUG_ON(shiftlen
> RPC_MAX_AUTH_SIZE
);
660 p
= buf
->head
[0].iov_base
+ base
;
662 memmove(p
+ shiftlen
, p
, buf
->head
[0].iov_len
- base
);
664 buf
->head
[0].iov_len
+= shiftlen
;
665 buf
->len
+= shiftlen
;
671 gss_krb5_cts_crypt(struct crypto_skcipher
*cipher
, struct xdr_buf
*buf
,
672 u32 offset
, u8
*iv
, struct page
**pages
, int encrypt
)
675 struct scatterlist sg
[1];
676 SKCIPHER_REQUEST_ON_STACK(req
, cipher
);
678 struct page
**save_pages
;
679 u32 len
= buf
->len
- offset
;
681 if (len
> GSS_KRB5_MAX_BLOCKSIZE
* 2) {
685 data
= kmalloc(GSS_KRB5_MAX_BLOCKSIZE
* 2, GFP_NOFS
);
690 * For encryption, we want to read from the cleartext
691 * page cache pages, and write the encrypted data to
692 * the supplied xdr_buf pages.
694 save_pages
= buf
->pages
;
698 ret
= read_bytes_from_xdr_buf(buf
, offset
, data
, len
);
699 buf
->pages
= save_pages
;
703 sg_init_one(sg
, data
, len
);
705 skcipher_request_set_tfm(req
, cipher
);
706 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
707 skcipher_request_set_crypt(req
, sg
, sg
, len
, iv
);
710 ret
= crypto_skcipher_encrypt(req
);
712 ret
= crypto_skcipher_decrypt(req
);
714 skcipher_request_zero(req
);
719 ret
= write_bytes_to_xdr_buf(buf
, offset
, data
, len
);
727 gss_krb5_aes_encrypt(struct krb5_ctx
*kctx
, u32 offset
,
728 struct xdr_buf
*buf
, struct page
**pages
)
731 struct xdr_netobj hmac
;
734 struct crypto_skcipher
*cipher
, *aux_cipher
;
736 struct page
**save_pages
;
738 struct encryptor_desc desc
;
742 if (kctx
->initiate
) {
743 cipher
= kctx
->initiator_enc
;
744 aux_cipher
= kctx
->initiator_enc_aux
;
745 cksumkey
= kctx
->initiator_integ
;
746 usage
= KG_USAGE_INITIATOR_SEAL
;
748 cipher
= kctx
->acceptor_enc
;
749 aux_cipher
= kctx
->acceptor_enc_aux
;
750 cksumkey
= kctx
->acceptor_integ
;
751 usage
= KG_USAGE_ACCEPTOR_SEAL
;
753 blocksize
= crypto_skcipher_blocksize(cipher
);
755 /* hide the gss token header and insert the confounder */
756 offset
+= GSS_KRB5_TOK_HDR_LEN
;
757 if (xdr_extend_head(buf
, offset
, kctx
->gk5e
->conflen
))
758 return GSS_S_FAILURE
;
759 gss_krb5_make_confounder(buf
->head
[0].iov_base
+ offset
, kctx
->gk5e
->conflen
);
760 offset
-= GSS_KRB5_TOK_HDR_LEN
;
762 if (buf
->tail
[0].iov_base
!= NULL
) {
763 ecptr
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
;
765 buf
->tail
[0].iov_base
= buf
->head
[0].iov_base
766 + buf
->head
[0].iov_len
;
767 buf
->tail
[0].iov_len
= 0;
768 ecptr
= buf
->tail
[0].iov_base
;
771 /* copy plaintext gss token header after filler (if any) */
772 memcpy(ecptr
, buf
->head
[0].iov_base
+ offset
, GSS_KRB5_TOK_HDR_LEN
);
773 buf
->tail
[0].iov_len
+= GSS_KRB5_TOK_HDR_LEN
;
774 buf
->len
+= GSS_KRB5_TOK_HDR_LEN
;
777 hmac
.len
= GSS_KRB5_MAX_CKSUM_LEN
;
778 hmac
.data
= buf
->tail
[0].iov_base
+ buf
->tail
[0].iov_len
;
781 * When we are called, pages points to the real page cache
782 * data -- which we can't go and encrypt! buf->pages points
783 * to scratch pages which we are going to send off to the
784 * client/server. Swap in the plaintext pages to calculate
787 save_pages
= buf
->pages
;
790 err
= make_checksum_v2(kctx
, NULL
, 0, buf
,
791 offset
+ GSS_KRB5_TOK_HDR_LEN
,
792 cksumkey
, usage
, &hmac
);
793 buf
->pages
= save_pages
;
795 return GSS_S_FAILURE
;
797 nbytes
= buf
->len
- offset
- GSS_KRB5_TOK_HDR_LEN
;
798 nblocks
= (nbytes
+ blocksize
- 1) / blocksize
;
801 cbcbytes
= (nblocks
- 2) * blocksize
;
803 memset(desc
.iv
, 0, sizeof(desc
.iv
));
806 SKCIPHER_REQUEST_ON_STACK(req
, aux_cipher
);
808 desc
.pos
= offset
+ GSS_KRB5_TOK_HDR_LEN
;
815 skcipher_request_set_tfm(req
, aux_cipher
);
816 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
818 sg_init_table(desc
.infrags
, 4);
819 sg_init_table(desc
.outfrags
, 4);
821 err
= xdr_process_buf(buf
, offset
+ GSS_KRB5_TOK_HDR_LEN
,
822 cbcbytes
, encryptor
, &desc
);
823 skcipher_request_zero(req
);
828 /* Make sure IV carries forward from any CBC results. */
829 err
= gss_krb5_cts_crypt(cipher
, buf
,
830 offset
+ GSS_KRB5_TOK_HDR_LEN
+ cbcbytes
,
837 /* Now update buf to account for HMAC */
838 buf
->tail
[0].iov_len
+= kctx
->gk5e
->cksumlength
;
839 buf
->len
+= kctx
->gk5e
->cksumlength
;
848 gss_krb5_aes_decrypt(struct krb5_ctx
*kctx
, u32 offset
, struct xdr_buf
*buf
,
849 u32
*headskip
, u32
*tailskip
)
851 struct xdr_buf subbuf
;
854 struct crypto_skcipher
*cipher
, *aux_cipher
;
855 struct xdr_netobj our_hmac_obj
;
856 u8 our_hmac
[GSS_KRB5_MAX_CKSUM_LEN
];
857 u8 pkt_hmac
[GSS_KRB5_MAX_CKSUM_LEN
];
858 int nblocks
, blocksize
, cbcbytes
;
859 struct decryptor_desc desc
;
862 if (kctx
->initiate
) {
863 cipher
= kctx
->acceptor_enc
;
864 aux_cipher
= kctx
->acceptor_enc_aux
;
865 cksum_key
= kctx
->acceptor_integ
;
866 usage
= KG_USAGE_ACCEPTOR_SEAL
;
868 cipher
= kctx
->initiator_enc
;
869 aux_cipher
= kctx
->initiator_enc_aux
;
870 cksum_key
= kctx
->initiator_integ
;
871 usage
= KG_USAGE_INITIATOR_SEAL
;
873 blocksize
= crypto_skcipher_blocksize(cipher
);
876 /* create a segment skipping the header and leaving out the checksum */
877 xdr_buf_subsegment(buf
, &subbuf
, offset
+ GSS_KRB5_TOK_HDR_LEN
,
878 (buf
->len
- offset
- GSS_KRB5_TOK_HDR_LEN
-
879 kctx
->gk5e
->cksumlength
));
881 nblocks
= (subbuf
.len
+ blocksize
- 1) / blocksize
;
885 cbcbytes
= (nblocks
- 2) * blocksize
;
887 memset(desc
.iv
, 0, sizeof(desc
.iv
));
890 SKCIPHER_REQUEST_ON_STACK(req
, aux_cipher
);
896 skcipher_request_set_tfm(req
, aux_cipher
);
897 skcipher_request_set_callback(req
, 0, NULL
, NULL
);
899 sg_init_table(desc
.frags
, 4);
901 ret
= xdr_process_buf(&subbuf
, 0, cbcbytes
, decryptor
, &desc
);
902 skcipher_request_zero(req
);
907 /* Make sure IV carries forward from any CBC results. */
908 ret
= gss_krb5_cts_crypt(cipher
, &subbuf
, cbcbytes
, desc
.iv
, NULL
, 0);
913 /* Calculate our hmac over the plaintext data */
914 our_hmac_obj
.len
= sizeof(our_hmac
);
915 our_hmac_obj
.data
= our_hmac
;
917 ret
= make_checksum_v2(kctx
, NULL
, 0, &subbuf
, 0,
918 cksum_key
, usage
, &our_hmac_obj
);
922 /* Get the packet's hmac value */
923 ret
= read_bytes_from_xdr_buf(buf
, buf
->len
- kctx
->gk5e
->cksumlength
,
924 pkt_hmac
, kctx
->gk5e
->cksumlength
);
928 if (crypto_memneq(pkt_hmac
, our_hmac
, kctx
->gk5e
->cksumlength
) != 0) {
932 *headskip
= kctx
->gk5e
->conflen
;
933 *tailskip
= kctx
->gk5e
->cksumlength
;
935 if (ret
&& ret
!= GSS_S_BAD_SIG
)
941 * Compute Kseq given the initial session key and the checksum.
942 * Set the key of the given cipher.
945 krb5_rc4_setup_seq_key(struct krb5_ctx
*kctx
, struct crypto_skcipher
*cipher
,
946 unsigned char *cksum
)
948 struct crypto_shash
*hmac
;
949 struct shash_desc
*desc
;
950 u8 Kseq
[GSS_KRB5_MAX_KEYLEN
];
951 u32 zeroconstant
= 0;
954 dprintk("%s: entered\n", __func__
);
956 hmac
= crypto_alloc_shash(kctx
->gk5e
->cksum_name
, 0, 0);
958 dprintk("%s: error %ld, allocating hash '%s'\n",
959 __func__
, PTR_ERR(hmac
), kctx
->gk5e
->cksum_name
);
960 return PTR_ERR(hmac
);
963 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(hmac
),
966 dprintk("%s: failed to allocate shash descriptor for '%s'\n",
967 __func__
, kctx
->gk5e
->cksum_name
);
968 crypto_free_shash(hmac
);
975 /* Compute intermediate Kseq from session key */
976 err
= crypto_shash_setkey(hmac
, kctx
->Ksess
, kctx
->gk5e
->keylength
);
980 err
= crypto_shash_digest(desc
, (u8
*)&zeroconstant
, 4, Kseq
);
984 /* Compute final Kseq from the checksum and intermediate Kseq */
985 err
= crypto_shash_setkey(hmac
, Kseq
, kctx
->gk5e
->keylength
);
989 err
= crypto_shash_digest(desc
, cksum
, 8, Kseq
);
993 err
= crypto_skcipher_setkey(cipher
, Kseq
, kctx
->gk5e
->keylength
);
1001 crypto_free_shash(hmac
);
1002 dprintk("%s: returning %d\n", __func__
, err
);
1007 * Compute Kcrypt given the initial session key and the plaintext seqnum.
1008 * Set the key of cipher kctx->enc.
1011 krb5_rc4_setup_enc_key(struct krb5_ctx
*kctx
, struct crypto_skcipher
*cipher
,
1014 struct crypto_shash
*hmac
;
1015 struct shash_desc
*desc
;
1016 u8 Kcrypt
[GSS_KRB5_MAX_KEYLEN
];
1017 u8 zeroconstant
[4] = {0};
1021 dprintk("%s: entered, seqnum %u\n", __func__
, seqnum
);
1023 hmac
= crypto_alloc_shash(kctx
->gk5e
->cksum_name
, 0, 0);
1025 dprintk("%s: error %ld, allocating hash '%s'\n",
1026 __func__
, PTR_ERR(hmac
), kctx
->gk5e
->cksum_name
);
1027 return PTR_ERR(hmac
);
1030 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(hmac
),
1033 dprintk("%s: failed to allocate shash descriptor for '%s'\n",
1034 __func__
, kctx
->gk5e
->cksum_name
);
1035 crypto_free_shash(hmac
);
1042 /* Compute intermediate Kcrypt from session key */
1043 for (i
= 0; i
< kctx
->gk5e
->keylength
; i
++)
1044 Kcrypt
[i
] = kctx
->Ksess
[i
] ^ 0xf0;
1046 err
= crypto_shash_setkey(hmac
, Kcrypt
, kctx
->gk5e
->keylength
);
1050 err
= crypto_shash_digest(desc
, zeroconstant
, 4, Kcrypt
);
1054 /* Compute final Kcrypt from the seqnum and intermediate Kcrypt */
1055 err
= crypto_shash_setkey(hmac
, Kcrypt
, kctx
->gk5e
->keylength
);
1059 seqnumarray
[0] = (unsigned char) ((seqnum
>> 24) & 0xff);
1060 seqnumarray
[1] = (unsigned char) ((seqnum
>> 16) & 0xff);
1061 seqnumarray
[2] = (unsigned char) ((seqnum
>> 8) & 0xff);
1062 seqnumarray
[3] = (unsigned char) ((seqnum
>> 0) & 0xff);
1064 err
= crypto_shash_digest(desc
, seqnumarray
, 4, Kcrypt
);
1068 err
= crypto_skcipher_setkey(cipher
, Kcrypt
, kctx
->gk5e
->keylength
);
1076 crypto_free_shash(hmac
);
1077 dprintk("%s: returning %d\n", __func__
, err
);