gss_krb5_keys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * COPYRIGHT (c) 2008
  3. * The Regents of the University of Michigan
  4. * ALL RIGHTS RESERVED
  5. *
  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
  15. * also be included.
  16. *
  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
  28. * SUCH DAMAGES.
  29. */
  30. /*
  31. * Copyright (C) 1998 by the FundsXpress, INC.
  32. *
  33. * All rights reserved.
  34. *
  35. * Export of this software from the United States of America may require
  36. * a specific license from the United States Government. It is the
  37. * responsibility of any person or organization contemplating export to
  38. * obtain such a license before exporting.
  39. *
  40. * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  41. * distribute this software and its documentation for any purpose and
  42. * without fee is hereby granted, provided that the above copyright
  43. * notice appear in all copies and that both that copyright notice and
  44. * this permission notice appear in supporting documentation, and that
  45. * the name of FundsXpress. not be used in advertising or publicity pertaining
  46. * to distribution of the software without specific, written prior
  47. * permission. FundsXpress makes no representations about the suitability of
  48. * this software for any purpose. It is provided "as is" without express
  49. * or implied warranty.
  50. *
  51. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  52. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  53. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  54. */
  55. #include <crypto/skcipher.h>
  56. #include <linux/err.h>
  57. #include <linux/types.h>
  58. #include <linux/sunrpc/gss_krb5.h>
  59. #include <linux/sunrpc/xdr.h>
  60. #include <linux/lcm.h>
  61. #include <crypto/hash.h>
  62. #include <kunit/visibility.h>
  63. #include "gss_krb5_internal.h"
  64. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  65. # define RPCDBG_FACILITY RPCDBG_AUTH
  66. #endif
  67. /**
  68. * krb5_nfold - n-fold function
  69. * @inbits: number of bits in @in
  70. * @in: buffer containing input to fold
  71. * @outbits: number of bits in the output buffer
  72. * @out: buffer to hold the result
  73. *
  74. * This is the n-fold function as described in rfc3961, sec 5.1
  75. * Taken from MIT Kerberos and modified.
  76. */
  77. VISIBLE_IF_KUNIT
  78. void krb5_nfold(u32 inbits, const u8 *in, u32 outbits, u8 *out)
  79. {
  80. unsigned long ulcm;
  81. int byte, i, msbit;
  82. /* the code below is more readable if I make these bytes
  83. instead of bits */
  84. inbits >>= 3;
  85. outbits >>= 3;
  86. /* first compute lcm(n,k) */
  87. ulcm = lcm(inbits, outbits);
  88. /* now do the real work */
  89. memset(out, 0, outbits);
  90. byte = 0;
  91. /* this will end up cycling through k lcm(k,n)/k times, which
  92. is correct */
  93. for (i = ulcm-1; i >= 0; i--) {
  94. /* compute the msbit in k which gets added into this byte */
  95. msbit = (
  96. /* first, start with the msbit in the first,
  97. * unrotated byte */
  98. ((inbits << 3) - 1)
  99. /* then, for each byte, shift to the right
  100. * for each repetition */
  101. + (((inbits << 3) + 13) * (i/inbits))
  102. /* last, pick out the correct byte within
  103. * that shifted repetition */
  104. + ((inbits - (i % inbits)) << 3)
  105. ) % (inbits << 3);
  106. /* pull out the byte value itself */
  107. byte += (((in[((inbits - 1) - (msbit >> 3)) % inbits] << 8)|
  108. (in[((inbits) - (msbit >> 3)) % inbits]))
  109. >> ((msbit & 7) + 1)) & 0xff;
  110. /* do the addition */
  111. byte += out[i % outbits];
  112. out[i % outbits] = byte & 0xff;
  113. /* keep around the carry bit, if any */
  114. byte >>= 8;
  115. }
  116. /* if there's a carry bit left over, add it back in */
  117. if (byte) {
  118. for (i = outbits - 1; i >= 0; i--) {
  119. /* do the addition */
  120. byte += out[i];
  121. out[i] = byte & 0xff;
  122. /* keep around the carry bit, if any */
  123. byte >>= 8;
  124. }
  125. }
  126. }
  127. EXPORT_SYMBOL_IF_KUNIT(krb5_nfold);
  128. /*
  129. * This is the DK (derive_key) function as described in rfc3961, sec 5.1
  130. * Taken from MIT Kerberos and modified.
  131. */
  132. static int krb5_DK(const struct gss_krb5_enctype *gk5e,
  133. const struct xdr_netobj *inkey, u8 *rawkey,
  134. const struct xdr_netobj *in_constant, gfp_t gfp_mask)
  135. {
  136. size_t blocksize, keybytes, keylength, n;
  137. unsigned char *inblockdata, *outblockdata;
  138. struct xdr_netobj inblock, outblock;
  139. struct crypto_sync_skcipher *cipher;
  140. int ret = -EINVAL;
  141. keybytes = gk5e->keybytes;
  142. keylength = gk5e->keylength;
  143. if (inkey->len != keylength)
  144. goto err_return;
  145. cipher = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
  146. if (IS_ERR(cipher))
  147. goto err_return;
  148. blocksize = crypto_sync_skcipher_blocksize(cipher);
  149. if (crypto_sync_skcipher_setkey(cipher, inkey->data, inkey->len))
  150. goto err_free_cipher;
  151. ret = -ENOMEM;
  152. inblockdata = kmalloc(blocksize, gfp_mask);
  153. if (inblockdata == NULL)
  154. goto err_free_cipher;
  155. outblockdata = kmalloc(blocksize, gfp_mask);
  156. if (outblockdata == NULL)
  157. goto err_free_in;
  158. inblock.data = (char *) inblockdata;
  159. inblock.len = blocksize;
  160. outblock.data = (char *) outblockdata;
  161. outblock.len = blocksize;
  162. /* initialize the input block */
  163. if (in_constant->len == inblock.len) {
  164. memcpy(inblock.data, in_constant->data, inblock.len);
  165. } else {
  166. krb5_nfold(in_constant->len * 8, in_constant->data,
  167. inblock.len * 8, inblock.data);
  168. }
  169. /* loop encrypting the blocks until enough key bytes are generated */
  170. n = 0;
  171. while (n < keybytes) {
  172. krb5_encrypt(cipher, NULL, inblock.data, outblock.data,
  173. inblock.len);
  174. if ((keybytes - n) <= outblock.len) {
  175. memcpy(rawkey + n, outblock.data, (keybytes - n));
  176. break;
  177. }
  178. memcpy(rawkey + n, outblock.data, outblock.len);
  179. memcpy(inblock.data, outblock.data, outblock.len);
  180. n += outblock.len;
  181. }
  182. ret = 0;
  183. kfree_sensitive(outblockdata);
  184. err_free_in:
  185. kfree_sensitive(inblockdata);
  186. err_free_cipher:
  187. crypto_free_sync_skcipher(cipher);
  188. err_return:
  189. return ret;
  190. }
  191. /*
  192. * This is the identity function, with some sanity checking.
  193. */
  194. static int krb5_random_to_key_v2(const struct gss_krb5_enctype *gk5e,
  195. struct xdr_netobj *randombits,
  196. struct xdr_netobj *key)
  197. {
  198. int ret = -EINVAL;
  199. if (key->len != 16 && key->len != 32) {
  200. dprintk("%s: key->len is %d\n", __func__, key->len);
  201. goto err_out;
  202. }
  203. if (randombits->len != 16 && randombits->len != 32) {
  204. dprintk("%s: randombits->len is %d\n",
  205. __func__, randombits->len);
  206. goto err_out;
  207. }
  208. if (randombits->len != key->len) {
  209. dprintk("%s: randombits->len is %d, key->len is %d\n",
  210. __func__, randombits->len, key->len);
  211. goto err_out;
  212. }
  213. memcpy(key->data, randombits->data, key->len);
  214. ret = 0;
  215. err_out:
  216. return ret;
  217. }
  218. /**
  219. * krb5_derive_key_v2 - Derive a subkey for an RFC 3962 enctype
  220. * @gk5e: Kerberos 5 enctype profile
  221. * @inkey: base protocol key
  222. * @outkey: OUT: derived key
  223. * @label: subkey usage label
  224. * @gfp_mask: memory allocation control flags
  225. *
  226. * Caller sets @outkey->len to the desired length of the derived key.
  227. *
  228. * On success, returns 0 and fills in @outkey. A negative errno value
  229. * is returned on failure.
  230. */
  231. int krb5_derive_key_v2(const struct gss_krb5_enctype *gk5e,
  232. const struct xdr_netobj *inkey,
  233. struct xdr_netobj *outkey,
  234. const struct xdr_netobj *label,
  235. gfp_t gfp_mask)
  236. {
  237. struct xdr_netobj inblock;
  238. int ret;
  239. inblock.len = gk5e->keybytes;
  240. inblock.data = kmalloc(inblock.len, gfp_mask);
  241. if (!inblock.data)
  242. return -ENOMEM;
  243. ret = krb5_DK(gk5e, inkey, inblock.data, label, gfp_mask);
  244. if (!ret)
  245. ret = krb5_random_to_key_v2(gk5e, &inblock, outkey);
  246. kfree_sensitive(inblock.data);
  247. return ret;
  248. }
  249. /*
  250. * K(i) = CMAC(key, K(i-1) | i | constant | 0x00 | k)
  251. *
  252. * i: A block counter is used with a length of 4 bytes, represented
  253. * in big-endian order.
  254. *
  255. * constant: The label input to the KDF is the usage constant supplied
  256. * to the key derivation function
  257. *
  258. * k: The length of the output key in bits, represented as a 4-byte
  259. * string in big-endian order.
  260. *
  261. * Caller fills in K(i-1) in @step, and receives the result K(i)
  262. * in the same buffer.
  263. */
  264. static int
  265. krb5_cmac_Ki(struct crypto_shash *tfm, const struct xdr_netobj *constant,
  266. u32 outlen, u32 count, struct xdr_netobj *step)
  267. {
  268. __be32 k = cpu_to_be32(outlen * 8);
  269. SHASH_DESC_ON_STACK(desc, tfm);
  270. __be32 i = cpu_to_be32(count);
  271. u8 zero = 0;
  272. int ret;
  273. desc->tfm = tfm;
  274. ret = crypto_shash_init(desc);
  275. if (ret)
  276. goto out_err;
  277. ret = crypto_shash_update(desc, step->data, step->len);
  278. if (ret)
  279. goto out_err;
  280. ret = crypto_shash_update(desc, (u8 *)&i, sizeof(i));
  281. if (ret)
  282. goto out_err;
  283. ret = crypto_shash_update(desc, constant->data, constant->len);
  284. if (ret)
  285. goto out_err;
  286. ret = crypto_shash_update(desc, &zero, sizeof(zero));
  287. if (ret)
  288. goto out_err;
  289. ret = crypto_shash_update(desc, (u8 *)&k, sizeof(k));
  290. if (ret)
  291. goto out_err;
  292. ret = crypto_shash_final(desc, step->data);
  293. if (ret)
  294. goto out_err;
  295. out_err:
  296. shash_desc_zero(desc);
  297. return ret;
  298. }
  299. /**
  300. * krb5_kdf_feedback_cmac - Derive a subkey for a Camellia/CMAC-based enctype
  301. * @gk5e: Kerberos 5 enctype parameters
  302. * @inkey: base protocol key
  303. * @outkey: OUT: derived key
  304. * @constant: subkey usage label
  305. * @gfp_mask: memory allocation control flags
  306. *
  307. * RFC 6803 Section 3:
  308. *
  309. * "We use a key derivation function from the family specified in
  310. * [SP800-108], Section 5.2, 'KDF in Feedback Mode'."
  311. *
  312. * n = ceiling(k / 128)
  313. * K(0) = zeros
  314. * K(i) = CMAC(key, K(i-1) | i | constant | 0x00 | k)
  315. * DR(key, constant) = k-truncate(K(1) | K(2) | ... | K(n))
  316. * KDF-FEEDBACK-CMAC(key, constant) = random-to-key(DR(key, constant))
  317. *
  318. * Caller sets @outkey->len to the desired length of the derived key (k).
  319. *
  320. * On success, returns 0 and fills in @outkey. A negative errno value
  321. * is returned on failure.
  322. */
  323. int
  324. krb5_kdf_feedback_cmac(const struct gss_krb5_enctype *gk5e,
  325. const struct xdr_netobj *inkey,
  326. struct xdr_netobj *outkey,
  327. const struct xdr_netobj *constant,
  328. gfp_t gfp_mask)
  329. {
  330. struct xdr_netobj step = { .data = NULL };
  331. struct xdr_netobj DR = { .data = NULL };
  332. unsigned int blocksize, offset;
  333. struct crypto_shash *tfm;
  334. int n, count, ret;
  335. /*
  336. * This implementation assumes the CMAC used for an enctype's
  337. * key derivation is the same as the CMAC used for its
  338. * checksumming. This happens to be true for enctypes that
  339. * are currently supported by this implementation.
  340. */
  341. tfm = crypto_alloc_shash(gk5e->cksum_name, 0, 0);
  342. if (IS_ERR(tfm)) {
  343. ret = PTR_ERR(tfm);
  344. goto out;
  345. }
  346. ret = crypto_shash_setkey(tfm, inkey->data, inkey->len);
  347. if (ret)
  348. goto out_free_tfm;
  349. blocksize = crypto_shash_digestsize(tfm);
  350. n = (outkey->len + blocksize - 1) / blocksize;
  351. /* K(0) is all zeroes */
  352. ret = -ENOMEM;
  353. step.len = blocksize;
  354. step.data = kzalloc(step.len, gfp_mask);
  355. if (!step.data)
  356. goto out_free_tfm;
  357. DR.len = blocksize * n;
  358. DR.data = kmalloc(DR.len, gfp_mask);
  359. if (!DR.data)
  360. goto out_free_tfm;
  361. /* XXX: Does not handle partial-block key sizes */
  362. for (offset = 0, count = 1; count <= n; count++) {
  363. ret = krb5_cmac_Ki(tfm, constant, outkey->len, count, &step);
  364. if (ret)
  365. goto out_free_tfm;
  366. memcpy(DR.data + offset, step.data, blocksize);
  367. offset += blocksize;
  368. }
  369. /* k-truncate and random-to-key */
  370. memcpy(outkey->data, DR.data, outkey->len);
  371. ret = 0;
  372. out_free_tfm:
  373. crypto_free_shash(tfm);
  374. out:
  375. kfree_sensitive(step.data);
  376. kfree_sensitive(DR.data);
  377. return ret;
  378. }
  379. /*
  380. * K1 = HMAC-SHA(key, 0x00000001 | label | 0x00 | k)
  381. *
  382. * key: The source of entropy from which subsequent keys are derived.
  383. *
  384. * label: An octet string describing the intended usage of the
  385. * derived key.
  386. *
  387. * k: Length in bits of the key to be outputted, expressed in
  388. * big-endian binary representation in 4 bytes.
  389. */
  390. static int
  391. krb5_hmac_K1(struct crypto_shash *tfm, const struct xdr_netobj *label,
  392. u32 outlen, struct xdr_netobj *K1)
  393. {
  394. __be32 k = cpu_to_be32(outlen * 8);
  395. SHASH_DESC_ON_STACK(desc, tfm);
  396. __be32 one = cpu_to_be32(1);
  397. u8 zero = 0;
  398. int ret;
  399. desc->tfm = tfm;
  400. ret = crypto_shash_init(desc);
  401. if (ret)
  402. goto out_err;
  403. ret = crypto_shash_update(desc, (u8 *)&one, sizeof(one));
  404. if (ret)
  405. goto out_err;
  406. ret = crypto_shash_update(desc, label->data, label->len);
  407. if (ret)
  408. goto out_err;
  409. ret = crypto_shash_update(desc, &zero, sizeof(zero));
  410. if (ret)
  411. goto out_err;
  412. ret = crypto_shash_update(desc, (u8 *)&k, sizeof(k));
  413. if (ret)
  414. goto out_err;
  415. ret = crypto_shash_final(desc, K1->data);
  416. if (ret)
  417. goto out_err;
  418. out_err:
  419. shash_desc_zero(desc);
  420. return ret;
  421. }
  422. /**
  423. * krb5_kdf_hmac_sha2 - Derive a subkey for an AES/SHA2-based enctype
  424. * @gk5e: Kerberos 5 enctype policy parameters
  425. * @inkey: base protocol key
  426. * @outkey: OUT: derived key
  427. * @label: subkey usage label
  428. * @gfp_mask: memory allocation control flags
  429. *
  430. * RFC 8009 Section 3:
  431. *
  432. * "We use a key derivation function from Section 5.1 of [SP800-108],
  433. * which uses the HMAC algorithm as the PRF."
  434. *
  435. * function KDF-HMAC-SHA2(key, label, [context,] k):
  436. * k-truncate(K1)
  437. *
  438. * Caller sets @outkey->len to the desired length of the derived key.
  439. *
  440. * On success, returns 0 and fills in @outkey. A negative errno value
  441. * is returned on failure.
  442. */
  443. int
  444. krb5_kdf_hmac_sha2(const struct gss_krb5_enctype *gk5e,
  445. const struct xdr_netobj *inkey,
  446. struct xdr_netobj *outkey,
  447. const struct xdr_netobj *label,
  448. gfp_t gfp_mask)
  449. {
  450. struct crypto_shash *tfm;
  451. struct xdr_netobj K1 = {
  452. .data = NULL,
  453. };
  454. int ret;
  455. /*
  456. * This implementation assumes the HMAC used for an enctype's
  457. * key derivation is the same as the HMAC used for its
  458. * checksumming. This happens to be true for enctypes that
  459. * are currently supported by this implementation.
  460. */
  461. tfm = crypto_alloc_shash(gk5e->cksum_name, 0, 0);
  462. if (IS_ERR(tfm)) {
  463. ret = PTR_ERR(tfm);
  464. goto out;
  465. }
  466. ret = crypto_shash_setkey(tfm, inkey->data, inkey->len);
  467. if (ret)
  468. goto out_free_tfm;
  469. K1.len = crypto_shash_digestsize(tfm);
  470. K1.data = kmalloc(K1.len, gfp_mask);
  471. if (!K1.data) {
  472. ret = -ENOMEM;
  473. goto out_free_tfm;
  474. }
  475. ret = krb5_hmac_K1(tfm, label, outkey->len, &K1);
  476. if (ret)
  477. goto out_free_tfm;
  478. /* k-truncate and random-to-key */
  479. memcpy(outkey->data, K1.data, outkey->len);
  480. out_free_tfm:
  481. kfree_sensitive(K1.data);
  482. crypto_free_shash(tfm);
  483. out:
  484. return ret;
  485. }