x942kdf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "e_os.h"
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_dispatch.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/params.h>
  16. #include "internal/packet.h"
  17. #include "internal/der.h"
  18. #include "prov/provider_ctx.h"
  19. #include "prov/providercommonerr.h"
  20. #include "prov/implementations.h"
  21. #include "prov/provider_util.h"
  22. #include "prov/der_wrap.h"
  23. #define X942KDF_MAX_INLEN (1 << 30)
  24. static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
  25. static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
  26. static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
  27. static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
  28. static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
  29. static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
  30. static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
  31. static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
  32. typedef struct {
  33. void *provctx;
  34. PROV_DIGEST digest;
  35. unsigned char *secret;
  36. size_t secret_len;
  37. unsigned char *ukm;
  38. size_t ukm_len;
  39. size_t dkm_len;
  40. const unsigned char *cek_oid;
  41. size_t cek_oid_len;
  42. } KDF_X942;
  43. /*
  44. * A table of allowed wrapping algorithms, oids and the associated output
  45. * lengths.
  46. * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
  47. * corresponding ciphers for these operations.
  48. */
  49. static const struct {
  50. const char *name;
  51. const unsigned char *oid;
  52. size_t oid_len;
  53. size_t keklen; /* size in bytes */
  54. } kek_algs[] = {
  55. { "AES-128-WRAP", der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap, 16 },
  56. { "AES-192-WRAP", der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap, 24 },
  57. { "AES-256-WRAP", der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap, 32 },
  58. #ifndef FIPS_MODULE
  59. { "DES3-WRAP", der_oid_id_alg_CMS3DESwrap, DER_OID_SZ_id_alg_CMS3DESwrap,
  60. 24 },
  61. #endif
  62. };
  63. static int find_alg_id(OPENSSL_CTX *libctx, const char *algname, size_t *id)
  64. {
  65. int ret = 1;
  66. size_t i;
  67. EVP_CIPHER *cipher;
  68. cipher = EVP_CIPHER_fetch(libctx, algname, NULL);
  69. if (cipher != NULL) {
  70. for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
  71. if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
  72. *id = i;
  73. goto end;
  74. }
  75. }
  76. }
  77. ret = 0;
  78. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
  79. end:
  80. EVP_CIPHER_free(cipher);
  81. return ret;
  82. }
  83. static int DER_w_keyinfo(WPACKET *pkt,
  84. const unsigned char *der_oid, size_t der_oidlen,
  85. unsigned char **pcounter)
  86. {
  87. return DER_w_begin_sequence(pkt, -1)
  88. /* Store the initial value of 1 into the counter */
  89. && DER_w_octet_string_uint32(pkt, -1, 1)
  90. /* Remember where we stored the counter in the buffer */
  91. && (pcounter == NULL
  92. || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
  93. && DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
  94. && DER_w_end_sequence(pkt, -1);
  95. }
  96. static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
  97. const unsigned char *der_oid, size_t der_oidlen,
  98. const unsigned char *ukm, size_t ukmlen,
  99. uint32_t keylen_bits, unsigned char **pcounter)
  100. {
  101. return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
  102. WPACKET_init_null_der(pkt))
  103. && DER_w_begin_sequence(pkt, -1)
  104. && DER_w_octet_string_uint32(pkt, 2, keylen_bits)
  105. && (ukm == NULL || DER_w_octet_string(pkt, 0, ukm, ukmlen))
  106. && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
  107. && DER_w_end_sequence(pkt, -1)
  108. && WPACKET_finish(pkt);
  109. }
  110. /*
  111. * Encode the other info structure.
  112. *
  113. * RFC2631 Section 2.1.2 Contains the following definition for otherinfo
  114. *
  115. * OtherInfo ::= SEQUENCE {
  116. * keyInfo KeySpecificInfo,
  117. * partyAInfo [0] OCTET STRING OPTIONAL,
  118. * suppPubInfo [2] OCTET STRING
  119. * }
  120. * Note suppPubInfo is the key length (in bits) (stored into 4 bytes)
  121. *
  122. *
  123. * KeySpecificInfo ::= SEQUENCE {
  124. * algorithm OBJECT IDENTIFIER,
  125. * counter OCTET STRING SIZE (4..4)
  126. * }
  127. *
  128. * |keylen| is the length (in bytes) of the generated KEK. It is stored into
  129. * suppPubInfo (in bits).
  130. * |cek_oid| The oid of the key wrapping algorithm.
  131. * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
  132. * |ukm| is the optional user keying material that is stored into partyAInfo. It
  133. * can be NULL.
  134. * |ukmlen| is the user keying material length (in bytes).
  135. * |der| is the returned encoded data. It must be freed by the caller.
  136. * |der_len| is the returned size of the encoded data.
  137. * |out_ctr| returns a pointer to the counter data which is embedded inside the
  138. * encoded data. This allows the counter bytes to be updated without re-encoding.
  139. *
  140. * Returns: 1 if successfully encoded, or 0 otherwise.
  141. * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
  142. */
  143. static int x942_encode_otherinfo(size_t keylen,
  144. const unsigned char *cek_oid, size_t cek_oidlen,
  145. const unsigned char *ukm, size_t ukmlen,
  146. unsigned char **der, size_t *der_len,
  147. unsigned char **out_ctr)
  148. {
  149. int ret = 0;
  150. unsigned char *pcounter = NULL, *der_buf = NULL;
  151. size_t der_buflen = 0;
  152. WPACKET pkt;
  153. uint32_t keylen_bits;
  154. /* keylenbits must fit into 4 bytes */
  155. if (keylen > 0xFFFFFF)
  156. goto err;
  157. keylen_bits = 8 * keylen;
  158. /* Calculate the size of the buffer */
  159. if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oidlen, ukm, ukmlen,
  160. keylen_bits, NULL)
  161. || !WPACKET_get_total_written(&pkt, &der_buflen))
  162. goto err;
  163. WPACKET_cleanup(&pkt);
  164. /* Alloc the buffer */
  165. der_buf = OPENSSL_zalloc(der_buflen);
  166. if (der_buf == NULL)
  167. goto err;
  168. /* Encode into the buffer */
  169. if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oidlen,
  170. ukm, ukmlen, keylen_bits, &pcounter))
  171. goto err;
  172. /*
  173. * Since we allocated the exact size required, the buffer should point to the
  174. * start of the alllocated buffer at this point.
  175. */
  176. if (WPACKET_get_curr(&pkt) != der_buf)
  177. goto err;
  178. /*
  179. * The data for the DER encoded octet string of a 32 bit counter = 1
  180. * should be 04 04 00 00 00 01
  181. * So just check the header is correct and skip over it.
  182. * This counter will be incremented in the kdf update loop.
  183. */
  184. if (pcounter == NULL
  185. || pcounter[0] != 0x04
  186. || pcounter[1] != 0x04)
  187. goto err;
  188. *out_ctr = (pcounter + 2);
  189. *der = der_buf;
  190. *der_len = der_buflen;
  191. ret = 1;
  192. err:
  193. WPACKET_cleanup(&pkt);
  194. return ret;
  195. }
  196. static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
  197. const unsigned char *z, size_t z_len,
  198. const unsigned char *other, size_t other_len,
  199. unsigned char *ctr,
  200. unsigned char *derived_key, size_t derived_key_len)
  201. {
  202. int ret = 0, hlen;
  203. size_t counter, out_len, len = derived_key_len;
  204. unsigned char mac[EVP_MAX_MD_SIZE];
  205. unsigned char *out = derived_key;
  206. EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
  207. if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
  208. || derived_key_len > X942KDF_MAX_INLEN
  209. || derived_key_len == 0) {
  210. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  211. return 0;
  212. }
  213. hlen = EVP_MD_size(kdf_md);
  214. if (hlen <= 0)
  215. return 0;
  216. out_len = (size_t)hlen;
  217. ctx = EVP_MD_CTX_create();
  218. ctx_init = EVP_MD_CTX_create();
  219. if (ctx == NULL || ctx_init == NULL)
  220. goto end;
  221. if (!EVP_DigestInit(ctx_init, kdf_md))
  222. goto end;
  223. for (counter = 1;; counter++) {
  224. /* updating the ctr modifies 4 bytes in the 'other' buffer */
  225. ctr[0] = (unsigned char)((counter >> 24) & 0xff);
  226. ctr[1] = (unsigned char)((counter >> 16) & 0xff);
  227. ctr[2] = (unsigned char)((counter >> 8) & 0xff);
  228. ctr[3] = (unsigned char)(counter & 0xff);
  229. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
  230. || !EVP_DigestUpdate(ctx, z, z_len)
  231. || !EVP_DigestUpdate(ctx, other, other_len))
  232. goto end;
  233. if (len >= out_len) {
  234. if (!EVP_DigestFinal_ex(ctx, out, NULL))
  235. goto end;
  236. out += out_len;
  237. len -= out_len;
  238. if (len == 0)
  239. break;
  240. } else {
  241. if (!EVP_DigestFinal_ex(ctx, mac, NULL))
  242. goto end;
  243. memcpy(out, mac, len);
  244. break;
  245. }
  246. }
  247. ret = 1;
  248. end:
  249. EVP_MD_CTX_free(ctx);
  250. EVP_MD_CTX_free(ctx_init);
  251. OPENSSL_cleanse(mac, sizeof(mac));
  252. return ret;
  253. }
  254. static void *x942kdf_new(void *provctx)
  255. {
  256. KDF_X942 *ctx;
  257. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  258. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  259. ctx->provctx = provctx;
  260. return ctx;
  261. }
  262. static void x942kdf_reset(void *vctx)
  263. {
  264. KDF_X942 *ctx = (KDF_X942 *)vctx;
  265. void *provctx = ctx->provctx;
  266. ossl_prov_digest_reset(&ctx->digest);
  267. OPENSSL_clear_free(ctx->secret, ctx->secret_len);
  268. OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
  269. memset(ctx, 0, sizeof(*ctx));
  270. ctx->provctx = provctx;
  271. }
  272. static void x942kdf_free(void *vctx)
  273. {
  274. KDF_X942 *ctx = (KDF_X942 *)vctx;
  275. if (ctx != NULL) {
  276. x942kdf_reset(ctx);
  277. OPENSSL_free(ctx);
  278. }
  279. }
  280. static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
  281. const OSSL_PARAM *p)
  282. {
  283. if (p->data_size == 0 || p->data == NULL)
  284. return 1;
  285. OPENSSL_free(*out);
  286. *out = NULL;
  287. return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
  288. }
  289. static size_t x942kdf_size(KDF_X942 *ctx)
  290. {
  291. int len;
  292. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  293. if (md == NULL) {
  294. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  295. return 0;
  296. }
  297. len = EVP_MD_size(md);
  298. return (len <= 0) ? 0 : (size_t)len;
  299. }
  300. static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
  301. {
  302. KDF_X942 *ctx = (KDF_X942 *)vctx;
  303. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  304. int ret = 0;
  305. unsigned char *ctr;
  306. unsigned char *der = NULL;
  307. size_t der_len = 0;
  308. if (ctx->secret == NULL) {
  309. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  310. return 0;
  311. }
  312. if (md == NULL) {
  313. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  314. return 0;
  315. }
  316. if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
  317. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
  318. return 0;
  319. }
  320. if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
  321. /*
  322. * Note the ukm length MUST be 512 bits.
  323. * For backwards compatibility the old check is being done.
  324. */
  325. ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
  326. return 0;
  327. }
  328. /* generate the otherinfo der */
  329. if (!x942_encode_otherinfo(ctx->dkm_len,
  330. ctx->cek_oid, ctx->cek_oid_len,
  331. ctx->ukm, ctx->ukm_len,
  332. &der, &der_len, &ctr)) {
  333. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
  334. return 0;
  335. }
  336. ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
  337. der, der_len, ctr, key, keylen);
  338. OPENSSL_free(der);
  339. return ret;
  340. }
  341. static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  342. {
  343. const OSSL_PARAM *p;
  344. KDF_X942 *ctx = vctx;
  345. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  346. size_t id;
  347. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  348. return 0;
  349. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
  350. || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
  351. if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
  352. return 0;
  353. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
  354. if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
  355. return 0;
  356. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
  357. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  358. return 0;
  359. if (find_alg_id(provctx, p->data, &id) == 0)
  360. return 0;
  361. ctx->cek_oid = kek_algs[id].oid;
  362. ctx->cek_oid_len = kek_algs[id].oid_len;
  363. ctx->dkm_len = kek_algs[id].keklen;
  364. }
  365. return 1;
  366. }
  367. static const OSSL_PARAM *x942kdf_settable_ctx_params(void)
  368. {
  369. static const OSSL_PARAM known_settable_ctx_params[] = {
  370. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  371. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  372. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  373. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  374. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
  375. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
  376. OSSL_PARAM_END
  377. };
  378. return known_settable_ctx_params;
  379. }
  380. static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  381. {
  382. KDF_X942 *ctx = (KDF_X942 *)vctx;
  383. OSSL_PARAM *p;
  384. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  385. return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
  386. return -2;
  387. }
  388. static const OSSL_PARAM *x942kdf_gettable_ctx_params(void)
  389. {
  390. static const OSSL_PARAM known_gettable_ctx_params[] = {
  391. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  392. OSSL_PARAM_END
  393. };
  394. return known_gettable_ctx_params;
  395. }
  396. const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
  397. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
  398. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
  399. { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
  400. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
  401. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  402. (void(*)(void))x942kdf_settable_ctx_params },
  403. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
  404. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  405. (void(*)(void))x942kdf_gettable_ctx_params },
  406. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
  407. { 0, NULL }
  408. };