pkey_kdf.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2018, 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 <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include <openssl/buffer.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/params.h>
  18. #include "internal/numbers.h"
  19. #include "crypto/evp.h"
  20. #define MAX_PARAM 20
  21. typedef struct {
  22. EVP_KDF_CTX *kctx;
  23. /*
  24. * EVP_PKEY implementations collect bits of certain data
  25. */
  26. BUF_MEM *collected_seed;
  27. BUF_MEM *collected_info;
  28. } EVP_PKEY_KDF_CTX;
  29. static void pkey_kdf_free_collected(EVP_PKEY_KDF_CTX *pkctx)
  30. {
  31. BUF_MEM_free(pkctx->collected_seed);
  32. pkctx->collected_seed = NULL;
  33. BUF_MEM_free(pkctx->collected_info);
  34. pkctx->collected_info = NULL;
  35. }
  36. static int pkey_kdf_init(EVP_PKEY_CTX *ctx)
  37. {
  38. EVP_PKEY_KDF_CTX *pkctx;
  39. EVP_KDF_CTX *kctx;
  40. const char *kdf_name = OBJ_nid2sn(ctx->pmeth->pkey_id);
  41. EVP_KDF *kdf;
  42. pkctx = OPENSSL_zalloc(sizeof(*pkctx));
  43. if (pkctx == NULL)
  44. return 0;
  45. kdf = EVP_KDF_fetch(NULL, kdf_name, NULL);
  46. kctx = EVP_KDF_new_ctx(kdf);
  47. EVP_KDF_free(kdf);
  48. if (kctx == NULL) {
  49. OPENSSL_free(pkctx);
  50. return 0;
  51. }
  52. pkctx->kctx = kctx;
  53. ctx->data = pkctx;
  54. return 1;
  55. }
  56. static void pkey_kdf_cleanup(EVP_PKEY_CTX *ctx)
  57. {
  58. EVP_PKEY_KDF_CTX *pkctx = ctx->data;
  59. EVP_KDF_free_ctx(pkctx->kctx);
  60. pkey_kdf_free_collected(pkctx);
  61. OPENSSL_free(pkctx);
  62. }
  63. static int collect(BUF_MEM **collector, void *data, size_t datalen)
  64. {
  65. size_t i;
  66. if (*collector == NULL)
  67. *collector = BUF_MEM_new();
  68. if (*collector == NULL) {
  69. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  70. return 0;
  71. }
  72. if (data != NULL && datalen > 0) {
  73. i = (*collector)->length; /* BUF_MEM_grow() changes it! */
  74. if (!BUF_MEM_grow(*collector, i + datalen))
  75. return 0;
  76. memcpy((*collector)->data + i, data, datalen);
  77. }
  78. return 1;
  79. }
  80. static int pkey_kdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  81. {
  82. EVP_PKEY_KDF_CTX *pkctx = ctx->data;
  83. EVP_KDF_CTX *kctx = pkctx->kctx;
  84. enum { T_OCTET_STRING, T_UINT64, T_DIGEST, T_INT } cmd;
  85. const char *name, *mdname;
  86. BUF_MEM **collector = NULL;
  87. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  88. switch (type) {
  89. case EVP_PKEY_CTRL_PASS:
  90. cmd = T_OCTET_STRING;
  91. name = OSSL_KDF_PARAM_PASSWORD;
  92. break;
  93. case EVP_PKEY_CTRL_HKDF_SALT:
  94. case EVP_PKEY_CTRL_SCRYPT_SALT:
  95. cmd = T_OCTET_STRING;
  96. name = OSSL_KDF_PARAM_SALT;
  97. break;
  98. case EVP_PKEY_CTRL_TLS_MD:
  99. case EVP_PKEY_CTRL_HKDF_MD:
  100. cmd = T_DIGEST;
  101. name = OSSL_KDF_PARAM_DIGEST;
  102. break;
  103. case EVP_PKEY_CTRL_TLS_SECRET:
  104. cmd = T_OCTET_STRING;
  105. name = OSSL_KDF_PARAM_SECRET;
  106. /*
  107. * Perform the semantics described in
  108. * EVP_PKEY_CTX_add1_tls1_prf_seed(3)
  109. */
  110. if (ctx->pmeth->pkey_id == NID_tls1_prf) {
  111. BUF_MEM_free(pkctx->collected_seed);
  112. pkctx->collected_seed = NULL;
  113. }
  114. break;
  115. case EVP_PKEY_CTRL_TLS_SEED:
  116. cmd = T_OCTET_STRING;
  117. name = OSSL_KDF_PARAM_SEED;
  118. collector = &pkctx->collected_seed;
  119. break;
  120. case EVP_PKEY_CTRL_HKDF_KEY:
  121. cmd = T_OCTET_STRING;
  122. name = OSSL_KDF_PARAM_KEY;
  123. break;
  124. case EVP_PKEY_CTRL_HKDF_INFO:
  125. cmd = T_OCTET_STRING;
  126. name = OSSL_KDF_PARAM_INFO;
  127. collector = &pkctx->collected_info;
  128. break;
  129. case EVP_PKEY_CTRL_HKDF_MODE:
  130. cmd = T_INT;
  131. name = OSSL_KDF_PARAM_MODE;
  132. break;
  133. case EVP_PKEY_CTRL_SCRYPT_N:
  134. cmd = T_UINT64;
  135. name = OSSL_KDF_PARAM_SCRYPT_N;
  136. break;
  137. case EVP_PKEY_CTRL_SCRYPT_R:
  138. cmd = T_UINT64; /* Range checking occurs on the provider side */
  139. name = OSSL_KDF_PARAM_SCRYPT_R;
  140. break;
  141. case EVP_PKEY_CTRL_SCRYPT_P:
  142. cmd = T_UINT64; /* Range checking occurs on the provider side */
  143. name = OSSL_KDF_PARAM_SCRYPT_P;
  144. break;
  145. case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
  146. cmd = T_UINT64;
  147. name = OSSL_KDF_PARAM_SCRYPT_MAXMEM;
  148. break;
  149. default:
  150. return -2;
  151. }
  152. if (collector != NULL) {
  153. switch (cmd) {
  154. case T_OCTET_STRING:
  155. return collect(collector, p2, p1);
  156. default:
  157. OPENSSL_assert("You shouldn't be here");
  158. break;
  159. }
  160. return 1;
  161. }
  162. switch (cmd) {
  163. case T_OCTET_STRING:
  164. params[0] =
  165. OSSL_PARAM_construct_octet_string(name, (unsigned char *)p2,
  166. (size_t)p1);
  167. break;
  168. case T_DIGEST:
  169. mdname = EVP_MD_name((const EVP_MD *)p2);
  170. params[0] = OSSL_PARAM_construct_utf8_string(name, (char *)mdname, 0);
  171. break;
  172. /*
  173. * These are special because the helper macros pass a pointer to the
  174. * stack, so a local copy is required.
  175. */
  176. case T_INT:
  177. params[0] = OSSL_PARAM_construct_int(name, &p1);
  178. break;
  179. case T_UINT64:
  180. params[0] = OSSL_PARAM_construct_uint64(name, (uint64_t *)p2);
  181. break;
  182. }
  183. return EVP_KDF_set_ctx_params(kctx, params);
  184. }
  185. static int pkey_kdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
  186. const char *value)
  187. {
  188. EVP_PKEY_KDF_CTX *pkctx = ctx->data;
  189. EVP_KDF_CTX *kctx = pkctx->kctx;
  190. const EVP_KDF *kdf = EVP_KDF_get_ctx_kdf(kctx);
  191. BUF_MEM **collector = NULL;
  192. const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(kdf);
  193. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  194. int ok = 0;
  195. /* Deal with ctrl name aliasing */
  196. if (strcmp(type, "md") == 0)
  197. type = OSSL_KDF_PARAM_DIGEST;
  198. /* scrypt uses 'N', params uses 'n' */
  199. if (strcmp(type, "N") == 0)
  200. type = OSSL_KDF_PARAM_SCRYPT_N;
  201. if (!OSSL_PARAM_allocate_from_text(&params[0], defs, type,
  202. value, strlen(value), NULL))
  203. return 0;
  204. /*
  205. * We do the same special casing of seed and info here as in
  206. * pkey_kdf_ctrl()
  207. */
  208. if (strcmp(params[0].key, OSSL_KDF_PARAM_SEED) == 0)
  209. collector = &pkctx->collected_seed;
  210. else if (strcmp(params[0].key, OSSL_KDF_PARAM_INFO) == 0)
  211. collector = &pkctx->collected_info;
  212. if (collector != NULL)
  213. ok = collect(collector, params[0].data, params[0].data_size);
  214. else
  215. ok = EVP_KDF_set_ctx_params(kctx, params);
  216. OPENSSL_free(params[0].data);
  217. return ok;
  218. }
  219. static int pkey_kdf_derive_init(EVP_PKEY_CTX *ctx)
  220. {
  221. EVP_PKEY_KDF_CTX *pkctx = ctx->data;
  222. pkey_kdf_free_collected(pkctx);
  223. if (pkctx->kctx != NULL)
  224. EVP_KDF_reset(pkctx->kctx);
  225. return 1;
  226. }
  227. /*
  228. * For fixed-output algorithms the keylen parameter is an "out" parameter
  229. * otherwise it is an "in" parameter.
  230. */
  231. static int pkey_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  232. size_t *keylen)
  233. {
  234. EVP_PKEY_KDF_CTX *pkctx = ctx->data;
  235. EVP_KDF_CTX *kctx = pkctx->kctx;
  236. size_t outlen = EVP_KDF_size(kctx);
  237. int r;
  238. if (pkctx->collected_seed != NULL) {
  239. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  240. params[0] =
  241. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  242. pkctx->collected_seed->data,
  243. pkctx->collected_seed->length);
  244. r = EVP_KDF_set_ctx_params(kctx, params);
  245. pkey_kdf_free_collected(pkctx);
  246. if (!r)
  247. return 0;
  248. }
  249. if (pkctx->collected_info != NULL) {
  250. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  251. params[0] =
  252. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
  253. pkctx->collected_info->data,
  254. pkctx->collected_info->length);
  255. r = EVP_KDF_set_ctx_params(kctx, params);
  256. pkey_kdf_free_collected(pkctx);
  257. if (!r)
  258. return 0;
  259. }
  260. if (outlen == 0 || outlen == SIZE_MAX) {
  261. /* Variable-output algorithm */
  262. if (key == NULL)
  263. return 0;
  264. } else {
  265. /* Fixed-output algorithm */
  266. *keylen = outlen;
  267. if (key == NULL)
  268. return 1;
  269. }
  270. return EVP_KDF_derive(kctx, key, *keylen);
  271. }
  272. #ifndef OPENSSL_NO_SCRYPT
  273. static const EVP_PKEY_METHOD scrypt_pkey_meth = {
  274. EVP_PKEY_SCRYPT,
  275. 0,
  276. pkey_kdf_init,
  277. 0,
  278. pkey_kdf_cleanup,
  279. 0, 0,
  280. 0, 0,
  281. 0,
  282. 0,
  283. 0,
  284. 0,
  285. 0, 0,
  286. 0, 0, 0, 0,
  287. 0, 0,
  288. 0, 0,
  289. pkey_kdf_derive_init,
  290. pkey_kdf_derive,
  291. pkey_kdf_ctrl,
  292. pkey_kdf_ctrl_str
  293. };
  294. const EVP_PKEY_METHOD *scrypt_pkey_method(void)
  295. {
  296. return &scrypt_pkey_meth;
  297. }
  298. #endif
  299. static const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
  300. EVP_PKEY_TLS1_PRF,
  301. 0,
  302. pkey_kdf_init,
  303. 0,
  304. pkey_kdf_cleanup,
  305. 0, 0,
  306. 0, 0,
  307. 0,
  308. 0,
  309. 0,
  310. 0,
  311. 0, 0,
  312. 0, 0, 0, 0,
  313. 0, 0,
  314. 0, 0,
  315. pkey_kdf_derive_init,
  316. pkey_kdf_derive,
  317. pkey_kdf_ctrl,
  318. pkey_kdf_ctrl_str
  319. };
  320. const EVP_PKEY_METHOD *tls1_prf_pkey_method(void)
  321. {
  322. return &tls1_prf_pkey_meth;
  323. }
  324. static const EVP_PKEY_METHOD hkdf_pkey_meth = {
  325. EVP_PKEY_HKDF,
  326. 0,
  327. pkey_kdf_init,
  328. 0,
  329. pkey_kdf_cleanup,
  330. 0, 0,
  331. 0, 0,
  332. 0,
  333. 0,
  334. 0,
  335. 0,
  336. 0, 0,
  337. 0, 0, 0, 0,
  338. 0, 0,
  339. 0, 0,
  340. pkey_kdf_derive_init,
  341. pkey_kdf_derive,
  342. pkey_kdf_ctrl,
  343. pkey_kdf_ctrl_str
  344. };
  345. const EVP_PKEY_METHOD *hkdf_pkey_method(void)
  346. {
  347. return &hkdf_pkey_meth;
  348. }