1 /*
2  *  linux/net/sunrpc/gss_krb5_seqnum.c
3  *
4  *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/util_seqnum.c
5  *
6  *  Copyright (c) 2000 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Andy Adamson   <andros@umich.edu>
10  */
11 
12 /*
13  * Copyright 1993 by OpenVision Technologies, Inc.
14  *
15  * Permission to use, copy, modify, distribute, and sell this software
16  * and its documentation for any purpose is hereby granted without fee,
17  * provided that the above copyright notice appears in all copies and
18  * that both that copyright notice and this permission notice appear in
19  * supporting documentation, and that the name of OpenVision not be used
20  * in advertising or publicity pertaining to distribution of the software
21  * without specific, written prior permission. OpenVision makes no
22  * representations about the suitability of this software for any
23  * purpose.  It is provided "as is" without express or implied warranty.
24  *
25  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
27  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
28  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
29  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31  * PERFORMANCE OF THIS SOFTWARE.
32  */
33 
34 #include <crypto/skcipher.h>
35 #include <linux/types.h>
36 #include <linux/sunrpc/gss_krb5.h>
37 
38 #include "gss_krb5_internal.h"
39 
40 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
41 # define RPCDBG_FACILITY        RPCDBG_AUTH
42 #endif
43 
44 s32
krb5_make_seq_num(struct krb5_ctx * kctx,struct crypto_sync_skcipher * key,int direction,u32 seqnum,unsigned char * cksum,unsigned char * buf)45 krb5_make_seq_num(struct krb5_ctx *kctx,
46 		struct crypto_sync_skcipher *key,
47 		int direction,
48 		u32 seqnum,
49 		unsigned char *cksum, unsigned char *buf)
50 {
51 	unsigned char *plain;
52 	s32 code;
53 
54 	plain = kmalloc(8, GFP_KERNEL);
55 	if (!plain)
56 		return -ENOMEM;
57 
58 	plain[0] = (unsigned char) (seqnum & 0xff);
59 	plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
60 	plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
61 	plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
62 
63 	plain[4] = direction;
64 	plain[5] = direction;
65 	plain[6] = direction;
66 	plain[7] = direction;
67 
68 	code = krb5_encrypt(key, cksum, plain, buf, 8);
69 	kfree(plain);
70 	return code;
71 }
72 
73 s32
krb5_get_seq_num(struct krb5_ctx * kctx,unsigned char * cksum,unsigned char * buf,int * direction,u32 * seqnum)74 krb5_get_seq_num(struct krb5_ctx *kctx,
75 	       unsigned char *cksum,
76 	       unsigned char *buf,
77 	       int *direction, u32 *seqnum)
78 {
79 	s32 code;
80 	unsigned char *plain;
81 	struct crypto_sync_skcipher *key = kctx->seq;
82 
83 	dprintk("RPC:       krb5_get_seq_num:\n");
84 
85 	plain = kmalloc(8, GFP_KERNEL);
86 	if (!plain)
87 		return -ENOMEM;
88 
89 	if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
90 		goto out;
91 
92 	if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
93 	    (plain[4] != plain[7])) {
94 		code = (s32)KG_BAD_SEQ;
95 		goto out;
96 	}
97 
98 	*direction = plain[4];
99 
100 	*seqnum = ((plain[0]) |
101 		   (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
102 
103 out:
104 	kfree(plain);
105 	return code;
106 }
107