3 * The Regents of the University of Michigan
6 * Permission is granted to use, copy, create derivative works
7 * and redistribute this software and such derivative works
8 * for any purpose, so long as the name of The University of
9 * Michigan is not used in any advertising or publicity
10 * pertaining to the use of distribution of this software
11 * without specific, written prior authorization. If the
12 * above copyright notice or any other identification of the
13 * University of Michigan is included in any copy of any
14 * portion of this software, then the disclaimer below must
17 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
32 * Copyright (C) 1998 by the FundsXpress, INC.
34 * All rights reserved.
36 * Export of this software from the United States of America may require
37 * a specific license from the United States Government. It is the
38 * responsibility of any person or organization contemplating export to
39 * obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of FundsXpress. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. FundsXpress makes no representations about the suitability of
49 * this software for any purpose. It is provided "as is" without express
50 * or implied warranty.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
54 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
57 #include <crypto/skcipher.h>
58 #include <linux/err.h>
59 #include <linux/types.h>
60 #include <linux/sunrpc/gss_krb5.h>
61 #include <linux/sunrpc/xdr.h>
62 #include <linux/lcm.h>
63 #include <crypto/hash.h>
64 #include <kunit/visibility.h>
66 #include "gss_krb5_internal.h"
68 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
69 # define RPCDBG_FACILITY RPCDBG_AUTH
73 * krb5_nfold - n-fold function
74 * @inbits: number of bits in @in
75 * @in: buffer containing input to fold
76 * @outbits: number of bits in the output buffer
77 * @out: buffer to hold the result
79 * This is the n-fold function as described in rfc3961, sec 5.1
80 * Taken from MIT Kerberos and modified.
83 void krb5_nfold(u32 inbits
, const u8
*in
, u32 outbits
, u8
*out
)
88 /* the code below is more readable if I make these bytes
94 /* first compute lcm(n,k) */
95 ulcm
= lcm(inbits
, outbits
);
97 /* now do the real work */
99 memset(out
, 0, outbits
);
102 /* this will end up cycling through k lcm(k,n)/k times, which
104 for (i
= ulcm
-1; i
>= 0; i
--) {
105 /* compute the msbit in k which gets added into this byte */
107 /* first, start with the msbit in the first,
110 /* then, for each byte, shift to the right
111 * for each repetition */
112 + (((inbits
<< 3) + 13) * (i
/inbits
))
113 /* last, pick out the correct byte within
114 * that shifted repetition */
115 + ((inbits
- (i
% inbits
)) << 3)
118 /* pull out the byte value itself */
119 byte
+= (((in
[((inbits
- 1) - (msbit
>> 3)) % inbits
] << 8)|
120 (in
[((inbits
) - (msbit
>> 3)) % inbits
]))
121 >> ((msbit
& 7) + 1)) & 0xff;
123 /* do the addition */
124 byte
+= out
[i
% outbits
];
125 out
[i
% outbits
] = byte
& 0xff;
127 /* keep around the carry bit, if any */
132 /* if there's a carry bit left over, add it back in */
134 for (i
= outbits
- 1; i
>= 0; i
--) {
135 /* do the addition */
137 out
[i
] = byte
& 0xff;
139 /* keep around the carry bit, if any */
144 EXPORT_SYMBOL_IF_KUNIT(krb5_nfold
);
147 * This is the DK (derive_key) function as described in rfc3961, sec 5.1
148 * Taken from MIT Kerberos and modified.
150 static int krb5_DK(const struct gss_krb5_enctype
*gk5e
,
151 const struct xdr_netobj
*inkey
, u8
*rawkey
,
152 const struct xdr_netobj
*in_constant
, gfp_t gfp_mask
)
154 size_t blocksize
, keybytes
, keylength
, n
;
155 unsigned char *inblockdata
, *outblockdata
;
156 struct xdr_netobj inblock
, outblock
;
157 struct crypto_sync_skcipher
*cipher
;
160 keybytes
= gk5e
->keybytes
;
161 keylength
= gk5e
->keylength
;
163 if (inkey
->len
!= keylength
)
166 cipher
= crypto_alloc_sync_skcipher(gk5e
->encrypt_name
, 0, 0);
169 blocksize
= crypto_sync_skcipher_blocksize(cipher
);
170 if (crypto_sync_skcipher_setkey(cipher
, inkey
->data
, inkey
->len
))
171 goto err_free_cipher
;
174 inblockdata
= kmalloc(blocksize
, gfp_mask
);
175 if (inblockdata
== NULL
)
176 goto err_free_cipher
;
178 outblockdata
= kmalloc(blocksize
, gfp_mask
);
179 if (outblockdata
== NULL
)
182 inblock
.data
= (char *) inblockdata
;
183 inblock
.len
= blocksize
;
185 outblock
.data
= (char *) outblockdata
;
186 outblock
.len
= blocksize
;
188 /* initialize the input block */
190 if (in_constant
->len
== inblock
.len
) {
191 memcpy(inblock
.data
, in_constant
->data
, inblock
.len
);
193 krb5_nfold(in_constant
->len
* 8, in_constant
->data
,
194 inblock
.len
* 8, inblock
.data
);
197 /* loop encrypting the blocks until enough key bytes are generated */
200 while (n
< keybytes
) {
201 krb5_encrypt(cipher
, NULL
, inblock
.data
, outblock
.data
,
204 if ((keybytes
- n
) <= outblock
.len
) {
205 memcpy(rawkey
+ n
, outblock
.data
, (keybytes
- n
));
209 memcpy(rawkey
+ n
, outblock
.data
, outblock
.len
);
210 memcpy(inblock
.data
, outblock
.data
, outblock
.len
);
216 kfree_sensitive(outblockdata
);
218 kfree_sensitive(inblockdata
);
220 crypto_free_sync_skcipher(cipher
);
226 * This is the identity function, with some sanity checking.
228 static int krb5_random_to_key_v2(const struct gss_krb5_enctype
*gk5e
,
229 struct xdr_netobj
*randombits
,
230 struct xdr_netobj
*key
)
234 if (key
->len
!= 16 && key
->len
!= 32) {
235 dprintk("%s: key->len is %d\n", __func__
, key
->len
);
238 if (randombits
->len
!= 16 && randombits
->len
!= 32) {
239 dprintk("%s: randombits->len is %d\n",
240 __func__
, randombits
->len
);
243 if (randombits
->len
!= key
->len
) {
244 dprintk("%s: randombits->len is %d, key->len is %d\n",
245 __func__
, randombits
->len
, key
->len
);
248 memcpy(key
->data
, randombits
->data
, key
->len
);
255 * krb5_derive_key_v2 - Derive a subkey for an RFC 3962 enctype
256 * @gk5e: Kerberos 5 enctype profile
257 * @inkey: base protocol key
258 * @outkey: OUT: derived key
259 * @label: subkey usage label
260 * @gfp_mask: memory allocation control flags
262 * Caller sets @outkey->len to the desired length of the derived key.
264 * On success, returns 0 and fills in @outkey. A negative errno value
265 * is returned on failure.
267 int krb5_derive_key_v2(const struct gss_krb5_enctype
*gk5e
,
268 const struct xdr_netobj
*inkey
,
269 struct xdr_netobj
*outkey
,
270 const struct xdr_netobj
*label
,
273 struct xdr_netobj inblock
;
276 inblock
.len
= gk5e
->keybytes
;
277 inblock
.data
= kmalloc(inblock
.len
, gfp_mask
);
281 ret
= krb5_DK(gk5e
, inkey
, inblock
.data
, label
, gfp_mask
);
283 ret
= krb5_random_to_key_v2(gk5e
, &inblock
, outkey
);
285 kfree_sensitive(inblock
.data
);
290 * K(i) = CMAC(key, K(i-1) | i | constant | 0x00 | k)
292 * i: A block counter is used with a length of 4 bytes, represented
293 * in big-endian order.
295 * constant: The label input to the KDF is the usage constant supplied
296 * to the key derivation function
298 * k: The length of the output key in bits, represented as a 4-byte
299 * string in big-endian order.
301 * Caller fills in K(i-1) in @step, and receives the result K(i)
302 * in the same buffer.
305 krb5_cmac_Ki(struct crypto_shash
*tfm
, const struct xdr_netobj
*constant
,
306 u32 outlen
, u32 count
, struct xdr_netobj
*step
)
308 __be32 k
= cpu_to_be32(outlen
* 8);
309 SHASH_DESC_ON_STACK(desc
, tfm
);
310 __be32 i
= cpu_to_be32(count
);
315 ret
= crypto_shash_init(desc
);
319 ret
= crypto_shash_update(desc
, step
->data
, step
->len
);
322 ret
= crypto_shash_update(desc
, (u8
*)&i
, sizeof(i
));
325 ret
= crypto_shash_update(desc
, constant
->data
, constant
->len
);
328 ret
= crypto_shash_update(desc
, &zero
, sizeof(zero
));
331 ret
= crypto_shash_update(desc
, (u8
*)&k
, sizeof(k
));
334 ret
= crypto_shash_final(desc
, step
->data
);
339 shash_desc_zero(desc
);
344 * krb5_kdf_feedback_cmac - Derive a subkey for a Camellia/CMAC-based enctype
345 * @gk5e: Kerberos 5 enctype parameters
346 * @inkey: base protocol key
347 * @outkey: OUT: derived key
348 * @constant: subkey usage label
349 * @gfp_mask: memory allocation control flags
351 * RFC 6803 Section 3:
353 * "We use a key derivation function from the family specified in
354 * [SP800-108], Section 5.2, 'KDF in Feedback Mode'."
356 * n = ceiling(k / 128)
358 * K(i) = CMAC(key, K(i-1) | i | constant | 0x00 | k)
359 * DR(key, constant) = k-truncate(K(1) | K(2) | ... | K(n))
360 * KDF-FEEDBACK-CMAC(key, constant) = random-to-key(DR(key, constant))
362 * Caller sets @outkey->len to the desired length of the derived key (k).
364 * On success, returns 0 and fills in @outkey. A negative errno value
365 * is returned on failure.
368 krb5_kdf_feedback_cmac(const struct gss_krb5_enctype
*gk5e
,
369 const struct xdr_netobj
*inkey
,
370 struct xdr_netobj
*outkey
,
371 const struct xdr_netobj
*constant
,
374 struct xdr_netobj step
= { .data
= NULL
};
375 struct xdr_netobj DR
= { .data
= NULL
};
376 unsigned int blocksize
, offset
;
377 struct crypto_shash
*tfm
;
381 * This implementation assumes the CMAC used for an enctype's
382 * key derivation is the same as the CMAC used for its
383 * checksumming. This happens to be true for enctypes that
384 * are currently supported by this implementation.
386 tfm
= crypto_alloc_shash(gk5e
->cksum_name
, 0, 0);
391 ret
= crypto_shash_setkey(tfm
, inkey
->data
, inkey
->len
);
395 blocksize
= crypto_shash_digestsize(tfm
);
396 n
= (outkey
->len
+ blocksize
- 1) / blocksize
;
398 /* K(0) is all zeroes */
400 step
.len
= blocksize
;
401 step
.data
= kzalloc(step
.len
, gfp_mask
);
405 DR
.len
= blocksize
* n
;
406 DR
.data
= kmalloc(DR
.len
, gfp_mask
);
410 /* XXX: Does not handle partial-block key sizes */
411 for (offset
= 0, count
= 1; count
<= n
; count
++) {
412 ret
= krb5_cmac_Ki(tfm
, constant
, outkey
->len
, count
, &step
);
416 memcpy(DR
.data
+ offset
, step
.data
, blocksize
);
420 /* k-truncate and random-to-key */
421 memcpy(outkey
->data
, DR
.data
, outkey
->len
);
425 crypto_free_shash(tfm
);
427 kfree_sensitive(step
.data
);
428 kfree_sensitive(DR
.data
);
433 * K1 = HMAC-SHA(key, 0x00000001 | label | 0x00 | k)
435 * key: The source of entropy from which subsequent keys are derived.
437 * label: An octet string describing the intended usage of the
440 * k: Length in bits of the key to be outputted, expressed in
441 * big-endian binary representation in 4 bytes.
444 krb5_hmac_K1(struct crypto_shash
*tfm
, const struct xdr_netobj
*label
,
445 u32 outlen
, struct xdr_netobj
*K1
)
447 __be32 k
= cpu_to_be32(outlen
* 8);
448 SHASH_DESC_ON_STACK(desc
, tfm
);
449 __be32 one
= cpu_to_be32(1);
454 ret
= crypto_shash_init(desc
);
457 ret
= crypto_shash_update(desc
, (u8
*)&one
, sizeof(one
));
460 ret
= crypto_shash_update(desc
, label
->data
, label
->len
);
463 ret
= crypto_shash_update(desc
, &zero
, sizeof(zero
));
466 ret
= crypto_shash_update(desc
, (u8
*)&k
, sizeof(k
));
469 ret
= crypto_shash_final(desc
, K1
->data
);
474 shash_desc_zero(desc
);
479 * krb5_kdf_hmac_sha2 - Derive a subkey for an AES/SHA2-based enctype
480 * @gk5e: Kerberos 5 enctype policy parameters
481 * @inkey: base protocol key
482 * @outkey: OUT: derived key
483 * @label: subkey usage label
484 * @gfp_mask: memory allocation control flags
486 * RFC 8009 Section 3:
488 * "We use a key derivation function from Section 5.1 of [SP800-108],
489 * which uses the HMAC algorithm as the PRF."
491 * function KDF-HMAC-SHA2(key, label, [context,] k):
494 * Caller sets @outkey->len to the desired length of the derived key.
496 * On success, returns 0 and fills in @outkey. A negative errno value
497 * is returned on failure.
500 krb5_kdf_hmac_sha2(const struct gss_krb5_enctype
*gk5e
,
501 const struct xdr_netobj
*inkey
,
502 struct xdr_netobj
*outkey
,
503 const struct xdr_netobj
*label
,
506 struct crypto_shash
*tfm
;
507 struct xdr_netobj K1
= {
513 * This implementation assumes the HMAC used for an enctype's
514 * key derivation is the same as the HMAC used for its
515 * checksumming. This happens to be true for enctypes that
516 * are currently supported by this implementation.
518 tfm
= crypto_alloc_shash(gk5e
->cksum_name
, 0, 0);
523 ret
= crypto_shash_setkey(tfm
, inkey
->data
, inkey
->len
);
527 K1
.len
= crypto_shash_digestsize(tfm
);
528 K1
.data
= kmalloc(K1
.len
, gfp_mask
);
534 ret
= krb5_hmac_K1(tfm
, label
, outkey
->len
, &K1
);
538 /* k-truncate and random-to-key */
539 memcpy(outkey
->data
, K1
.data
, outkey
->len
);
542 kfree_sensitive(K1
.data
);
543 crypto_free_shash(tfm
);