krb5kdf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DES low level APIs are deprecated for public use, but still ok for internal
  11. * use. We access the DES_set_odd_parity(3) function here.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/des.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/kdf.h>
  21. #include <openssl/proverr.h>
  22. #include "internal/cryptlib.h"
  23. #include "crypto/evp.h"
  24. #include "internal/numbers.h"
  25. #include "prov/implementations.h"
  26. #include "prov/provider_ctx.h"
  27. #include "prov/provider_util.h"
  28. #include "prov/providercommon.h"
  29. /* KRB5 KDF defined in RFC 3961, Section 5.1 */
  30. static OSSL_FUNC_kdf_newctx_fn krb5kdf_new;
  31. static OSSL_FUNC_kdf_dupctx_fn krb5kdf_dup;
  32. static OSSL_FUNC_kdf_freectx_fn krb5kdf_free;
  33. static OSSL_FUNC_kdf_reset_fn krb5kdf_reset;
  34. static OSSL_FUNC_kdf_derive_fn krb5kdf_derive;
  35. static OSSL_FUNC_kdf_settable_ctx_params_fn krb5kdf_settable_ctx_params;
  36. static OSSL_FUNC_kdf_set_ctx_params_fn krb5kdf_set_ctx_params;
  37. static OSSL_FUNC_kdf_gettable_ctx_params_fn krb5kdf_gettable_ctx_params;
  38. static OSSL_FUNC_kdf_get_ctx_params_fn krb5kdf_get_ctx_params;
  39. static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
  40. const unsigned char *key, size_t key_len,
  41. const unsigned char *constant, size_t constant_len,
  42. unsigned char *okey, size_t okey_len);
  43. typedef struct {
  44. void *provctx;
  45. PROV_CIPHER cipher;
  46. unsigned char *key;
  47. size_t key_len;
  48. unsigned char *constant;
  49. size_t constant_len;
  50. } KRB5KDF_CTX;
  51. static void *krb5kdf_new(void *provctx)
  52. {
  53. KRB5KDF_CTX *ctx;
  54. if (!ossl_prov_is_running())
  55. return NULL;
  56. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  57. return NULL;
  58. ctx->provctx = provctx;
  59. return ctx;
  60. }
  61. static void krb5kdf_free(void *vctx)
  62. {
  63. KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
  64. if (ctx != NULL) {
  65. krb5kdf_reset(ctx);
  66. OPENSSL_free(ctx);
  67. }
  68. }
  69. static void krb5kdf_reset(void *vctx)
  70. {
  71. KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
  72. void *provctx = ctx->provctx;
  73. ossl_prov_cipher_reset(&ctx->cipher);
  74. OPENSSL_clear_free(ctx->key, ctx->key_len);
  75. OPENSSL_clear_free(ctx->constant, ctx->constant_len);
  76. memset(ctx, 0, sizeof(*ctx));
  77. ctx->provctx = provctx;
  78. }
  79. static int krb5kdf_set_membuf(unsigned char **dst, size_t *dst_len,
  80. const OSSL_PARAM *p)
  81. {
  82. OPENSSL_clear_free(*dst, *dst_len);
  83. *dst = NULL;
  84. *dst_len = 0;
  85. return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
  86. }
  87. static void *krb5kdf_dup(void *vctx)
  88. {
  89. const KRB5KDF_CTX *src = (const KRB5KDF_CTX *)vctx;
  90. KRB5KDF_CTX *dest;
  91. dest = krb5kdf_new(src->provctx);
  92. if (dest != NULL) {
  93. if (!ossl_prov_memdup(src->key, src->key_len,
  94. &dest->key, &dest->key_len)
  95. || !ossl_prov_memdup(src->constant, src->constant_len,
  96. &dest->constant , &dest->constant_len)
  97. || !ossl_prov_cipher_copy(&dest->cipher, &src->cipher))
  98. goto err;
  99. }
  100. return dest;
  101. err:
  102. krb5kdf_free(dest);
  103. return NULL;
  104. }
  105. static int krb5kdf_derive(void *vctx, unsigned char *key, size_t keylen,
  106. const OSSL_PARAM params[])
  107. {
  108. KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
  109. const EVP_CIPHER *cipher;
  110. ENGINE *engine;
  111. if (!ossl_prov_is_running() || !krb5kdf_set_ctx_params(ctx, params))
  112. return 0;
  113. cipher = ossl_prov_cipher_cipher(&ctx->cipher);
  114. if (cipher == NULL) {
  115. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
  116. return 0;
  117. }
  118. if (ctx->key == NULL) {
  119. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  120. return 0;
  121. }
  122. if (ctx->constant == NULL) {
  123. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONSTANT);
  124. return 0;
  125. }
  126. engine = ossl_prov_cipher_engine(&ctx->cipher);
  127. return KRB5KDF(cipher, engine, ctx->key, ctx->key_len,
  128. ctx->constant, ctx->constant_len,
  129. key, keylen);
  130. }
  131. static int krb5kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  132. {
  133. const OSSL_PARAM *p;
  134. KRB5KDF_CTX *ctx = vctx;
  135. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  136. if (params == NULL)
  137. return 1;
  138. if (!ossl_prov_cipher_load_from_params(&ctx->cipher, params, provctx))
  139. return 0;
  140. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
  141. if (!krb5kdf_set_membuf(&ctx->key, &ctx->key_len, p))
  142. return 0;
  143. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CONSTANT))
  144. != NULL)
  145. if (!krb5kdf_set_membuf(&ctx->constant, &ctx->constant_len, p))
  146. return 0;
  147. return 1;
  148. }
  149. static const OSSL_PARAM *krb5kdf_settable_ctx_params(ossl_unused void *ctx,
  150. ossl_unused void *provctx)
  151. {
  152. static const OSSL_PARAM known_settable_ctx_params[] = {
  153. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  154. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
  155. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  156. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_CONSTANT, NULL, 0),
  157. OSSL_PARAM_END
  158. };
  159. return known_settable_ctx_params;
  160. }
  161. static int krb5kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  162. {
  163. KRB5KDF_CTX *ctx = (KRB5KDF_CTX *)vctx;
  164. const EVP_CIPHER *cipher;
  165. size_t len;
  166. OSSL_PARAM *p;
  167. cipher = ossl_prov_cipher_cipher(&ctx->cipher);
  168. if (cipher)
  169. len = EVP_CIPHER_get_key_length(cipher);
  170. else
  171. len = EVP_MAX_KEY_LENGTH;
  172. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  173. return OSSL_PARAM_set_size_t(p, len);
  174. return -2;
  175. }
  176. static const OSSL_PARAM *krb5kdf_gettable_ctx_params(ossl_unused void *ctx,
  177. ossl_unused void *provctx)
  178. {
  179. static const OSSL_PARAM known_gettable_ctx_params[] = {
  180. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  181. OSSL_PARAM_END
  182. };
  183. return known_gettable_ctx_params;
  184. }
  185. const OSSL_DISPATCH ossl_kdf_krb5kdf_functions[] = {
  186. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))krb5kdf_new },
  187. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))krb5kdf_dup },
  188. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))krb5kdf_free },
  189. { OSSL_FUNC_KDF_RESET, (void(*)(void))krb5kdf_reset },
  190. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))krb5kdf_derive },
  191. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  192. (void(*)(void))krb5kdf_settable_ctx_params },
  193. { OSSL_FUNC_KDF_SET_CTX_PARAMS,
  194. (void(*)(void))krb5kdf_set_ctx_params },
  195. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  196. (void(*)(void))krb5kdf_gettable_ctx_params },
  197. { OSSL_FUNC_KDF_GET_CTX_PARAMS,
  198. (void(*)(void))krb5kdf_get_ctx_params },
  199. OSSL_DISPATCH_END
  200. };
  201. #ifndef OPENSSL_NO_DES
  202. /*
  203. * DES3 is a special case, it requires a random-to-key function and its
  204. * input truncated to 21 bytes of the 24 produced by the cipher.
  205. * See RFC3961 6.3.1
  206. */
  207. static int fixup_des3_key(unsigned char *key)
  208. {
  209. unsigned char *cblock;
  210. int i, j;
  211. for (i = 2; i >= 0; i--) {
  212. cblock = &key[i * 8];
  213. memmove(cblock, &key[i * 7], 7);
  214. cblock[7] = 0;
  215. for (j = 0; j < 7; j++)
  216. cblock[7] |= (cblock[j] & 1) << (j + 1);
  217. DES_set_odd_parity((DES_cblock *)cblock);
  218. }
  219. /* fail if keys are such that triple des degrades to single des */
  220. if (CRYPTO_memcmp(&key[0], &key[8], 8) == 0 ||
  221. CRYPTO_memcmp(&key[8], &key[16], 8) == 0) {
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. #endif
  227. /*
  228. * N-fold(K) where blocksize is N, and constant_len is K
  229. * Note: Here |= denotes concatenation
  230. *
  231. * L = lcm(N,K)
  232. * R = L/K
  233. *
  234. * for r: 1 -> R
  235. * s |= constant rot 13*(r-1))
  236. *
  237. * block = 0
  238. * for k: 1 -> K
  239. * block += s[N(k-1)..(N-1)k] (one's complement addition)
  240. *
  241. * Optimizing for space we compute:
  242. * for each l in L-1 -> 0:
  243. * s[l] = (constant rot 13*(l/K))[l%k]
  244. * block[l % N] += s[l] (with carry)
  245. * finally add carry if any
  246. */
  247. static void n_fold(unsigned char *block, unsigned int blocksize,
  248. const unsigned char *constant, size_t constant_len)
  249. {
  250. unsigned int tmp, gcd, remainder, lcm, carry;
  251. int b, l;
  252. if (constant_len == blocksize) {
  253. memcpy(block, constant, constant_len);
  254. return;
  255. }
  256. /* Least Common Multiple of lengths: LCM(a,b)*/
  257. gcd = blocksize;
  258. remainder = constant_len;
  259. /* Calculate Great Common Divisor first GCD(a,b) */
  260. while (remainder != 0) {
  261. tmp = gcd % remainder;
  262. gcd = remainder;
  263. remainder = tmp;
  264. }
  265. /* resulting a is the GCD, LCM(a,b) = |a*b|/GCD(a,b) */
  266. lcm = blocksize * constant_len / gcd;
  267. /* now spread out the bits */
  268. memset(block, 0, blocksize);
  269. /* last to first to be able to bring carry forward */
  270. carry = 0;
  271. for (l = lcm - 1; l >= 0; l--) {
  272. unsigned int rotbits, rshift, rbyte;
  273. /* destination byte in block is l % N */
  274. b = l % blocksize;
  275. /* Our virtual s buffer is R = L/K long (K = constant_len) */
  276. /* So we rotate backwards from R-1 to 0 (none) rotations */
  277. rotbits = 13 * (l / constant_len);
  278. /* find the byte on s where rotbits falls onto */
  279. rbyte = l - (rotbits / 8);
  280. /* calculate how much shift on that byte */
  281. rshift = rotbits & 0x07;
  282. /* rbyte % constant_len gives us the unrotated byte in the
  283. * constant buffer, get also the previous byte then
  284. * appropriately shift them to get the rotated byte we need */
  285. tmp = (constant[(rbyte-1) % constant_len] << (8 - rshift)
  286. | constant[rbyte % constant_len] >> rshift)
  287. & 0xff;
  288. /* add with carry to any value placed by previous passes */
  289. tmp += carry + block[b];
  290. block[b] = tmp & 0xff;
  291. /* save any carry that may be left */
  292. carry = tmp >> 8;
  293. }
  294. /* if any carry is left at the end, add it through the number */
  295. for (b = blocksize - 1; b >= 0 && carry != 0; b--) {
  296. carry += block[b];
  297. block[b] = carry & 0xff;
  298. carry >>= 8;
  299. }
  300. }
  301. static int cipher_init(EVP_CIPHER_CTX *ctx,
  302. const EVP_CIPHER *cipher, ENGINE *engine,
  303. const unsigned char *key, size_t key_len)
  304. {
  305. int klen, ret;
  306. ret = EVP_EncryptInit_ex(ctx, cipher, engine, key, NULL);
  307. if (!ret)
  308. goto out;
  309. /* set the key len for the odd variable key len cipher */
  310. klen = EVP_CIPHER_CTX_get_key_length(ctx);
  311. if (key_len != (size_t)klen) {
  312. ret = EVP_CIPHER_CTX_set_key_length(ctx, key_len);
  313. if (ret <= 0) {
  314. ret = 0;
  315. goto out;
  316. }
  317. }
  318. /* we never want padding, either the length requested is a multiple of
  319. * the cipher block size or we are passed a cipher that can cope with
  320. * partial blocks via techniques like cipher text stealing */
  321. ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
  322. if (!ret)
  323. goto out;
  324. out:
  325. return ret;
  326. }
  327. static int KRB5KDF(const EVP_CIPHER *cipher, ENGINE *engine,
  328. const unsigned char *key, size_t key_len,
  329. const unsigned char *constant, size_t constant_len,
  330. unsigned char *okey, size_t okey_len)
  331. {
  332. EVP_CIPHER_CTX *ctx = NULL;
  333. unsigned char block[EVP_MAX_BLOCK_LENGTH * 2];
  334. unsigned char *plainblock, *cipherblock;
  335. size_t blocksize;
  336. size_t cipherlen;
  337. size_t osize;
  338. #ifndef OPENSSL_NO_DES
  339. int des3_no_fixup = 0;
  340. #endif
  341. int ret;
  342. if (key_len != okey_len) {
  343. #ifndef OPENSSL_NO_DES
  344. /* special case for 3des, where the caller may be requesting
  345. * the random raw key, instead of the fixed up key */
  346. if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc &&
  347. key_len == 24 && okey_len == 21) {
  348. des3_no_fixup = 1;
  349. } else {
  350. #endif
  351. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
  352. return 0;
  353. #ifndef OPENSSL_NO_DES
  354. }
  355. #endif
  356. }
  357. ctx = EVP_CIPHER_CTX_new();
  358. if (ctx == NULL)
  359. return 0;
  360. ret = cipher_init(ctx, cipher, engine, key, key_len);
  361. if (!ret)
  362. goto out;
  363. /* Initialize input block */
  364. blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  365. if (blocksize == 0) {
  366. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
  367. ret = 0;
  368. goto out;
  369. }
  370. if (constant_len > blocksize) {
  371. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONSTANT_LENGTH);
  372. ret = 0;
  373. goto out;
  374. }
  375. n_fold(block, blocksize, constant, constant_len);
  376. plainblock = block;
  377. cipherblock = block + EVP_MAX_BLOCK_LENGTH;
  378. for (osize = 0; osize < okey_len; osize += cipherlen) {
  379. int olen;
  380. ret = EVP_EncryptUpdate(ctx, cipherblock, &olen,
  381. plainblock, blocksize);
  382. if (!ret)
  383. goto out;
  384. cipherlen = olen;
  385. ret = EVP_EncryptFinal_ex(ctx, cipherblock, &olen);
  386. if (!ret)
  387. goto out;
  388. if (olen != 0) {
  389. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
  390. ret = 0;
  391. goto out;
  392. }
  393. /* write cipherblock out */
  394. if (cipherlen > okey_len - osize)
  395. cipherlen = okey_len - osize;
  396. memcpy(okey + osize, cipherblock, cipherlen);
  397. if (okey_len > osize + cipherlen) {
  398. /* we need to reinitialize cipher context per spec */
  399. ret = EVP_CIPHER_CTX_reset(ctx);
  400. if (!ret)
  401. goto out;
  402. ret = cipher_init(ctx, cipher, engine, key, key_len);
  403. if (!ret)
  404. goto out;
  405. /* also swap block offsets so last ciphertext becomes new
  406. * plaintext */
  407. plainblock = cipherblock;
  408. if (cipherblock == block) {
  409. cipherblock += EVP_MAX_BLOCK_LENGTH;
  410. } else {
  411. cipherblock = block;
  412. }
  413. }
  414. }
  415. #ifndef OPENSSL_NO_DES
  416. if (EVP_CIPHER_get_nid(cipher) == NID_des_ede3_cbc && !des3_no_fixup) {
  417. ret = fixup_des3_key(okey);
  418. if (!ret) {
  419. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
  420. goto out;
  421. }
  422. }
  423. #endif
  424. ret = 1;
  425. out:
  426. EVP_CIPHER_CTX_free(ctx);
  427. OPENSSL_cleanse(block, EVP_MAX_BLOCK_LENGTH * 2);
  428. return ret;
  429. }