This source file includes following definitions.
- krb5_make_rc4_seq_num
- krb5_make_seq_num
- krb5_get_rc4_seq_num
- krb5_get_seq_num
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 }