Merge branches 'clk-ingenic', 'clk-mtk-mux', 'clk-qcom-sdm845-pcie', 'clk-mtk-crit...
[linux-2.6-microblaze.git] / 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;
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 = kmalloc(8, GFP_NOFS);
56         if (!plain)
57                 return -ENOMEM;
58
59         plain[0] = (unsigned char) ((seqnum >> 24) & 0xff);
60         plain[1] = (unsigned char) ((seqnum >> 16) & 0xff);
61         plain[2] = (unsigned char) ((seqnum >> 8) & 0xff);
62         plain[3] = (unsigned char) ((seqnum >> 0) & 0xff);
63         plain[4] = direction;
64         plain[5] = direction;
65         plain[6] = direction;
66         plain[7] = direction;
67
68         code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
69         if (code)
70                 goto out;
71
72         code = krb5_encrypt(cipher, cksum, plain, buf, 8);
73 out:
74         kfree(plain);
75         crypto_free_sync_skcipher(cipher);
76         return code;
77 }
78 s32
79 krb5_make_seq_num(struct krb5_ctx *kctx,
80                 struct crypto_sync_skcipher *key,
81                 int direction,
82                 u32 seqnum,
83                 unsigned char *cksum, unsigned char *buf)
84 {
85         unsigned char *plain;
86         s32 code;
87
88         if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
89                 return krb5_make_rc4_seq_num(kctx, direction, seqnum,
90                                              cksum, buf);
91
92         plain = kmalloc(8, GFP_NOFS);
93         if (!plain)
94                 return -ENOMEM;
95
96         plain[0] = (unsigned char) (seqnum & 0xff);
97         plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
98         plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
99         plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
100
101         plain[4] = direction;
102         plain[5] = direction;
103         plain[6] = direction;
104         plain[7] = direction;
105
106         code = krb5_encrypt(key, cksum, plain, buf, 8);
107         kfree(plain);
108         return code;
109 }
110
111 static s32
112 krb5_get_rc4_seq_num(struct krb5_ctx *kctx, unsigned char *cksum,
113                      unsigned char *buf, int *direction, s32 *seqnum)
114 {
115         struct crypto_sync_skcipher *cipher;
116         unsigned char *plain;
117         s32 code;
118
119         dprintk("RPC:       %s:\n", __func__);
120         cipher = crypto_alloc_sync_skcipher(kctx->gk5e->encrypt_name, 0, 0);
121         if (IS_ERR(cipher))
122                 return PTR_ERR(cipher);
123
124         code = krb5_rc4_setup_seq_key(kctx, cipher, cksum);
125         if (code)
126                 goto out;
127
128         plain = kmalloc(8, GFP_NOFS);
129         if (!plain) {
130                 code = -ENOMEM;
131                 goto out;
132         }
133
134         code = krb5_decrypt(cipher, cksum, buf, plain, 8);
135         if (code)
136                 goto out_plain;
137
138         if ((plain[4] != plain[5]) || (plain[4] != plain[6])
139                                    || (plain[4] != plain[7])) {
140                 code = (s32)KG_BAD_SEQ;
141                 goto out_plain;
142         }
143
144         *direction = plain[4];
145
146         *seqnum = ((plain[0] << 24) | (plain[1] << 16) |
147                                         (plain[2] << 8) | (plain[3]));
148 out_plain:
149         kfree(plain);
150 out:
151         crypto_free_sync_skcipher(cipher);
152         return code;
153 }
154
155 s32
156 krb5_get_seq_num(struct krb5_ctx *kctx,
157                unsigned char *cksum,
158                unsigned char *buf,
159                int *direction, u32 *seqnum)
160 {
161         s32 code;
162         unsigned char *plain;
163         struct crypto_sync_skcipher *key = kctx->seq;
164
165         dprintk("RPC:       krb5_get_seq_num:\n");
166
167         if (kctx->enctype == ENCTYPE_ARCFOUR_HMAC)
168                 return krb5_get_rc4_seq_num(kctx, cksum, buf,
169                                             direction, seqnum);
170         plain = kmalloc(8, GFP_NOFS);
171         if (!plain)
172                 return -ENOMEM;
173
174         if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
175                 goto out;
176
177         if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
178             (plain[4] != plain[7])) {
179                 code = (s32)KG_BAD_SEQ;
180                 goto out;
181         }
182
183         *direction = plain[4];
184
185         *seqnum = ((plain[0]) |
186                    (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
187
188 out:
189         kfree(plain);
190         return code;
191 }