krb5kdf.c 14 KB

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