Merge tag 'sound-fix-4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux] / net / sunrpc / auth_gss / gss_krb5_seqnum.c
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 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
39 # define RPCDBG_FACILITY        RPCDBG_AUTH
40 #endif
41
42 static s32
43 krb5_make_rc4_seq_num(struct krb5_ctx *kctx, int direction, s32 seqnum,
44                       unsigned char *cksum, unsigned char *buf)
45 {
46         struct crypto_sync_skcipher *cipher;
47         unsigned char plain[8];
48         s32 code;
49
50         dprintk("RPC:       %s:\n", __func__);
51         cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name, 0, 0);
52         if (IS_ERR(cipher))
53                 return PTR_ERR(cipher);
54
55         plain[0] = (unsigned char) ((seqnum >> 24) & 0xff);
56         plain[1] = (unsigned char) ((seqnum >> 16) & 0xff);
57         plain[2] = (unsigned char) ((seqnum >> 8) & 0xff);
58         plain[3] = (unsigned char) ((seqnum >> 0) & 0xff);
59         plain[4] = direction;
60         plain[5] = direction;
61         plain[6] = direction;
62         plain[7] = direction;
63
64         code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
65         if (code)
66                 goto out;
67
68         code = krb5_encrypt(cipher, cksum, plain, buf, 8);
69 out:
70         crypto_free_sync_skcipher(cipher);
71         return code;
72 }
73 s32
74 krb5_make_seq_num(struct krb5_ctx *kctx,
75                 struct crypto_sync_skcipher *key,
76                 int direction,
77                 u32 seqnum,
78                 unsigned char *cksum, unsigned char *buf)
79 {
80         unsigned char plain[8];
81
82         if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
83                 return krb5_make_rc4_seq_num(kctx, direction, seqnum,
84                                              cksum, buf);
85
86         plain[0] = (unsigned char) (seqnum & 0xff);
87         plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
88         plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
89         plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
90
91         plain[4] = direction;
92         plain[5] = direction;
93         plain[6] = direction;
94         plain[7] = direction;
95
96         return krb5_encrypt(key, cksum, plain, buf, 8);
97 }
98
99 static s32
100 krb5_get_rc4_seq_num(struct krb5_ctx *kctx, unsigned char *cksum,
101                      unsigned char *buf, int *direction, s32 *seqnum)
102 {
103         struct crypto_sync_skcipher *cipher;
104         unsigned char plain[8];
105         s32 code;
106
107         dprintk("RPC:       %s:\n", __func__);
108         cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name, 0, 0);
109         if (IS_ERR(cipher))
110                 return PTR_ERR(cipher);
111
112         code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
113         if (code)
114                 goto out;
115
116         code = krb5_decrypt(cipher, cksum, buf, plain, 8);
117         if (code)
118                 goto out;
119
120         if ((plain[4] != plain[5]) || (plain[4] != plain[6])
121                                    || (plain[4] != plain[7])) {
122                 code = (s32)KG_BAD_SEQ;
123                 goto out;
124         }
125
126         *direction = plain[4];
127
128         *seqnum = ((plain[0] << 24) | (plain[1] << 16) |
129                                         (plain[2] << 8) | (plain[3]));
130 out:
131         crypto_free_sync_skcipher(cipher);
132         return code;
133 }
134
135 s32
136 krb5_get_seq_num(struct krb5_ctx *kctx,
137                unsigned char *cksum,
138                unsigned char *buf,
139                int *direction, u32 *seqnum)
140 {
141         s32 code;
142         unsigned char plain[8];
143         struct crypto_sync_skcipher *key = kctx->seq;
144
145         dprintk("RPC:       krb5_get_seq_num:\n");
146
147         if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
148                 return krb5_get_rc4_seq_num(kctx, cksum, buf,
149                                             direction, seqnum);
150
151         if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
152                 return code;
153
154         if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
155             (plain[4] != plain[7]))
156                 return (s32)KG_BAD_SEQ;
157
158         *direction = plain[4];
159
160         *seqnum = ((plain[0]) |
161                    (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
162
163         return 0;
164 }