Blame view

net/sunrpc/auth_gss/gss_krb5_keys.c 8.76 KB
4891f2d00   Kevin Coffman   gss_krb5: import ...
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /*
   * COPYRIGHT (c) 2008
   * The Regents of the University of Michigan
   * ALL RIGHTS RESERVED
   *
   * Permission is granted to use, copy, create derivative works
   * and redistribute this software and such derivative works
   * for any purpose, so long as the name of The University of
   * Michigan is not used in any advertising or publicity
   * pertaining to the use of distribution of this software
   * without specific, written prior authorization.  If the
   * above copyright notice or any other identification of the
   * University of Michigan is included in any copy of any
   * portion of this software, then the disclaimer below must
   * also be included.
   *
   * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
   * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
   * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
   * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
   * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
   * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
   * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
   * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
   * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
   * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGES.
   */
  
  /*
   * Copyright (C) 1998 by the FundsXpress, INC.
   *
   * All rights reserved.
   *
   * Export of this software from the United States of America may require
   * a specific license from the United States Government.  It is the
   * responsibility of any person or organization contemplating export to
   * obtain such a license before exporting.
   *
   * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
   * distribute this software and its documentation for any purpose and
   * without fee is hereby granted, provided that the above copyright
   * notice appear in all copies and that both that copyright notice and
   * this permission notice appear in supporting documentation, and that
   * the name of FundsXpress. not be used in advertising or publicity pertaining
   * to distribution of the software without specific, written prior
   * permission.  FundsXpress makes no representations about the suitability of
   * this software for any purpose.  It is provided "as is" without express
   * or implied warranty.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   */
3b5cf20cf   Herbert Xu   sunrpc: Use skcip...
56
  #include <crypto/skcipher.h>
4891f2d00   Kevin Coffman   gss_krb5: import ...
57
58
  #include <linux/err.h>
  #include <linux/types.h>
4891f2d00   Kevin Coffman   gss_krb5: import ...
59
60
  #include <linux/sunrpc/gss_krb5.h>
  #include <linux/sunrpc/xdr.h>
c692554bf   Luis Henriques   gss_krb5: use lcm...
61
  #include <linux/lcm.h>
4891f2d00   Kevin Coffman   gss_krb5: import ...
62

f895b252d   Jeff Layton   sunrpc: eliminate...
63
  #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
4891f2d00   Kevin Coffman   gss_krb5: import ...
64
65
66
67
68
69
70
71
72
73
74
  # define RPCDBG_FACILITY        RPCDBG_AUTH
  #endif
  
  /*
   * This is the n-fold function as described in rfc3961, sec 5.1
   * Taken from MIT Kerberos and modified.
   */
  
  static void krb5_nfold(u32 inbits, const u8 *in,
  		       u32 outbits, u8 *out)
  {
c692554bf   Luis Henriques   gss_krb5: use lcm...
75
  	unsigned long ulcm;
4891f2d00   Kevin Coffman   gss_krb5: import ...
76
77
78
79
80
81
82
83
84
  	int byte, i, msbit;
  
  	/* the code below is more readable if I make these bytes
  	   instead of bits */
  
  	inbits >>= 3;
  	outbits >>= 3;
  
  	/* first compute lcm(n,k) */
c692554bf   Luis Henriques   gss_krb5: use lcm...
85
  	ulcm = lcm(inbits, outbits);
4891f2d00   Kevin Coffman   gss_krb5: import ...
86
87
88
89
90
91
92
93
  
  	/* now do the real work */
  
  	memset(out, 0, outbits);
  	byte = 0;
  
  	/* this will end up cycling through k lcm(k,n)/k times, which
  	   is correct */
c692554bf   Luis Henriques   gss_krb5: use lcm...
94
  	for (i = ulcm-1; i >= 0; i--) {
4891f2d00   Kevin Coffman   gss_krb5: import ...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  		/* compute the msbit in k which gets added into this byte */
  		msbit = (
  			/* first, start with the msbit in the first,
  			 * unrotated byte */
  			 ((inbits << 3) - 1)
  			 /* then, for each byte, shift to the right
  			  * for each repetition */
  			 + (((inbits << 3) + 13) * (i/inbits))
  			 /* last, pick out the correct byte within
  			  * that shifted repetition */
  			 + ((inbits - (i % inbits)) << 3)
  			 ) % (inbits << 3);
  
  		/* pull out the byte value itself */
  		byte += (((in[((inbits - 1) - (msbit >> 3)) % inbits] << 8)|
  				  (in[((inbits) - (msbit >> 3)) % inbits]))
  				 >> ((msbit & 7) + 1)) & 0xff;
  
  		/* do the addition */
  		byte += out[i % outbits];
  		out[i % outbits] = byte & 0xff;
  
  		/* keep around the carry bit, if any */
  		byte >>= 8;
  
  	}
  
  	/* if there's a carry bit left over, add it back in */
  	if (byte) {
  		for (i = outbits - 1; i >= 0; i--) {
  			/* do the addition */
  			byte += out[i];
  			out[i] = byte & 0xff;
  
  			/* keep around the carry bit, if any */
  			byte >>= 8;
  		}
  	}
  }
  
  /*
   * This is the DK (derive_key) function as described in rfc3961, sec 5.1
   * Taken from MIT Kerberos and modified.
   */
47d848077   Kevin Coffman   gss_krb5: handle ...
139
  u32 krb5_derive_key(const struct gss_krb5_enctype *gk5e,
4891f2d00   Kevin Coffman   gss_krb5: import ...
140
141
  		    const struct xdr_netobj *inkey,
  		    struct xdr_netobj *outkey,
1f4c86c0b   Trond Myklebust   NFS: Don't use GF...
142
143
  		    const struct xdr_netobj *in_constant,
  		    gfp_t gfp_mask)
4891f2d00   Kevin Coffman   gss_krb5: import ...
144
145
146
147
  {
  	size_t blocksize, keybytes, keylength, n;
  	unsigned char *inblockdata, *outblockdata, *rawkey;
  	struct xdr_netobj inblock, outblock;
e9e575b8f   Kees Cook   gss_krb5: Remove ...
148
  	struct crypto_sync_skcipher *cipher;
4891f2d00   Kevin Coffman   gss_krb5: import ...
149
150
151
152
153
154
155
156
  	u32 ret = EINVAL;
  
  	blocksize = gk5e->blocksize;
  	keybytes = gk5e->keybytes;
  	keylength = gk5e->keylength;
  
  	if ((inkey->len != keylength) || (outkey->len != keylength))
  		goto err_return;
e9e575b8f   Kees Cook   gss_krb5: Remove ...
157
  	cipher = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
4891f2d00   Kevin Coffman   gss_krb5: import ...
158
159
  	if (IS_ERR(cipher))
  		goto err_return;
e9e575b8f   Kees Cook   gss_krb5: Remove ...
160
  	if (crypto_sync_skcipher_setkey(cipher, inkey->data, inkey->len))
4891f2d00   Kevin Coffman   gss_krb5: import ...
161
162
163
164
165
  		goto err_return;
  
  	/* allocate and set up buffers */
  
  	ret = ENOMEM;
1f4c86c0b   Trond Myklebust   NFS: Don't use GF...
166
  	inblockdata = kmalloc(blocksize, gfp_mask);
4891f2d00   Kevin Coffman   gss_krb5: import ...
167
168
  	if (inblockdata == NULL)
  		goto err_free_cipher;
1f4c86c0b   Trond Myklebust   NFS: Don't use GF...
169
  	outblockdata = kmalloc(blocksize, gfp_mask);
4891f2d00   Kevin Coffman   gss_krb5: import ...
170
171
  	if (outblockdata == NULL)
  		goto err_free_in;
1f4c86c0b   Trond Myklebust   NFS: Don't use GF...
172
  	rawkey = kmalloc(keybytes, gfp_mask);
4891f2d00   Kevin Coffman   gss_krb5: import ...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	if (rawkey == NULL)
  		goto err_free_out;
  
  	inblock.data = (char *) inblockdata;
  	inblock.len = blocksize;
  
  	outblock.data = (char *) outblockdata;
  	outblock.len = blocksize;
  
  	/* initialize the input block */
  
  	if (in_constant->len == inblock.len) {
  		memcpy(inblock.data, in_constant->data, inblock.len);
  	} else {
  		krb5_nfold(in_constant->len * 8, in_constant->data,
  			   inblock.len * 8, inblock.data);
  	}
  
  	/* loop encrypting the blocks until enough key bytes are generated */
  
  	n = 0;
  	while (n < keybytes) {
  		(*(gk5e->encrypt))(cipher, NULL, inblock.data,
  				   outblock.data, inblock.len);
  
  		if ((keybytes - n) <= outblock.len) {
  			memcpy(rawkey + n, outblock.data, (keybytes - n));
  			break;
  		}
  
  		memcpy(rawkey + n, outblock.data, outblock.len);
  		memcpy(inblock.data, outblock.data, outblock.len);
  		n += outblock.len;
  	}
  
  	/* postprocess the key */
  
  	inblock.data = (char *) rawkey;
  	inblock.len = keybytes;
  
  	BUG_ON(gk5e->mk_key == NULL);
  	ret = (*(gk5e->mk_key))(gk5e, &inblock, outkey);
  	if (ret) {
  		dprintk("%s: got %d from mk_key function for '%s'
  ",
  			__func__, ret, gk5e->encrypt_name);
  		goto err_free_raw;
  	}
  
  	/* clean memory, free resources and exit */
  
  	ret = 0;
  
  err_free_raw:
60b3990c2   zhong jiang   sunrpc: Use kzfre...
227
  	kzfree(rawkey);
4891f2d00   Kevin Coffman   gss_krb5: import ...
228
  err_free_out:
60b3990c2   zhong jiang   sunrpc: Use kzfre...
229
  	kzfree(outblockdata);
4891f2d00   Kevin Coffman   gss_krb5: import ...
230
  err_free_in:
60b3990c2   zhong jiang   sunrpc: Use kzfre...
231
  	kzfree(inblockdata);
4891f2d00   Kevin Coffman   gss_krb5: import ...
232
  err_free_cipher:
e9e575b8f   Kees Cook   gss_krb5: Remove ...
233
  	crypto_free_sync_skcipher(cipher);
4891f2d00   Kevin Coffman   gss_krb5: import ...
234
235
236
  err_return:
  	return ret;
  }
958142e97   Kevin Coffman   gss_krb5: add sup...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  
  #define smask(step) ((1<<step)-1)
  #define pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
  #define parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
  
  static void mit_des_fixup_key_parity(u8 key[8])
  {
  	int i;
  	for (i = 0; i < 8; i++) {
  		key[i] &= 0xfe;
  		key[i] |= 1^parity_char(key[i]);
  	}
  }
  
  /*
   * This is the des3 key derivation postprocess function
   */
  u32 gss_krb5_des3_make_key(const struct gss_krb5_enctype *gk5e,
  			   struct xdr_netobj *randombits,
  			   struct xdr_netobj *key)
  {
  	int i;
  	u32 ret = EINVAL;
  
  	if (key->len != 24) {
  		dprintk("%s: key->len is %d
  ", __func__, key->len);
  		goto err_out;
  	}
  	if (randombits->len != 21) {
  		dprintk("%s: randombits->len is %d
  ",
  			__func__, randombits->len);
  		goto err_out;
  	}
  
  	/* take the seven bytes, move them around into the top 7 bits of the
  	   8 key bytes, then compute the parity bits.  Do this three times. */
  
  	for (i = 0; i < 3; i++) {
  		memcpy(key->data + i*8, randombits->data + i*7, 7);
  		key->data[i*8+7] = (((key->data[i*8]&1)<<1) |
  				    ((key->data[i*8+1]&1)<<2) |
  				    ((key->data[i*8+2]&1)<<3) |
  				    ((key->data[i*8+3]&1)<<4) |
  				    ((key->data[i*8+4]&1)<<5) |
  				    ((key->data[i*8+5]&1)<<6) |
  				    ((key->data[i*8+6]&1)<<7));
  
  		mit_des_fixup_key_parity(key->data + i*8);
  	}
  	ret = 0;
  err_out:
  	return ret;
  }
934a95aa1   Kevin Coffman   gss_krb5: add rem...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  
  /*
   * This is the aes key derivation postprocess function
   */
  u32 gss_krb5_aes_make_key(const struct gss_krb5_enctype *gk5e,
  			  struct xdr_netobj *randombits,
  			  struct xdr_netobj *key)
  {
  	u32 ret = EINVAL;
  
  	if (key->len != 16 && key->len != 32) {
  		dprintk("%s: key->len is %d
  ", __func__, key->len);
  		goto err_out;
  	}
  	if (randombits->len != 16 && randombits->len != 32) {
  		dprintk("%s: randombits->len is %d
  ",
  			__func__, randombits->len);
  		goto err_out;
  	}
  	if (randombits->len != key->len) {
  		dprintk("%s: randombits->len is %d, key->len is %d
  ",
  			__func__, randombits->len, key->len);
  		goto err_out;
  	}
  	memcpy(key->data, randombits->data, key->len);
  	ret = 0;
  err_out:
  	return ret;
  }