evp_lib.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * Copyright 1995-2024 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. * EVP _meth_ APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/evp.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/params.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/dh.h>
  23. #include <openssl/ec.h>
  24. #include "crypto/evp.h"
  25. #include "crypto/cryptlib.h"
  26. #include "internal/provider.h"
  27. #include "evp_local.h"
  28. #if !defined(FIPS_MODULE)
  29. # include "crypto/asn1.h"
  30. int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  31. {
  32. return evp_cipher_param_to_asn1_ex(c, type, NULL);
  33. }
  34. int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  35. {
  36. return evp_cipher_asn1_to_param_ex(c, type, NULL);
  37. }
  38. int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
  39. {
  40. int i = 0;
  41. unsigned int l;
  42. if (type != NULL) {
  43. unsigned char iv[EVP_MAX_IV_LENGTH];
  44. l = EVP_CIPHER_CTX_get_iv_length(ctx);
  45. if (!ossl_assert(l <= sizeof(iv)))
  46. return -1;
  47. i = ASN1_TYPE_get_octetstring(type, iv, l);
  48. if (i != (int)l)
  49. return -1;
  50. if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
  51. return -1;
  52. }
  53. return i;
  54. }
  55. int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  56. {
  57. int i = 0;
  58. unsigned int j;
  59. unsigned char *oiv = NULL;
  60. if (type != NULL) {
  61. oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
  62. j = EVP_CIPHER_CTX_get_iv_length(c);
  63. OPENSSL_assert(j <= sizeof(c->iv));
  64. i = ASN1_TYPE_set_octetstring(type, oiv, j);
  65. }
  66. return i;
  67. }
  68. int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  69. evp_cipher_aead_asn1_params *asn1_params)
  70. {
  71. int ret = -1; /* Assume the worst */
  72. const EVP_CIPHER *cipher;
  73. if (c == NULL || c->cipher == NULL)
  74. goto err;
  75. cipher = c->cipher;
  76. /*
  77. * For legacy implementations, we detect custom AlgorithmIdentifier
  78. * parameter handling by checking if the function pointer
  79. * cipher->set_asn1_parameters is set. We know that this pointer
  80. * is NULL for provided implementations.
  81. *
  82. * Otherwise, for any implementation, we check the flag
  83. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  84. * default AI parameter extraction.
  85. *
  86. * Otherwise, for provided implementations, we convert |type| to
  87. * a DER encoded blob and pass to the implementation in OSSL_PARAM
  88. * form.
  89. *
  90. * If none of the above applies, this operation is unsupported.
  91. */
  92. if (cipher->set_asn1_parameters != NULL) {
  93. ret = cipher->set_asn1_parameters(c, type);
  94. } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  95. switch (EVP_CIPHER_get_mode(cipher)) {
  96. case EVP_CIPH_WRAP_MODE:
  97. if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
  98. ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
  99. ret = 1;
  100. break;
  101. case EVP_CIPH_GCM_MODE:
  102. ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
  103. break;
  104. case EVP_CIPH_CCM_MODE:
  105. case EVP_CIPH_XTS_MODE:
  106. case EVP_CIPH_OCB_MODE:
  107. ret = -2;
  108. break;
  109. default:
  110. ret = EVP_CIPHER_set_asn1_iv(c, type);
  111. }
  112. } else if (cipher->prov != NULL) {
  113. OSSL_PARAM params[3], *p = params;
  114. unsigned char *der = NULL, *derp;
  115. /*
  116. * We make two passes, the first to get the appropriate buffer size,
  117. * and the second to get the actual value.
  118. */
  119. *p++ = OSSL_PARAM_construct_octet_string(
  120. OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
  121. NULL, 0);
  122. *p = OSSL_PARAM_construct_end();
  123. if (!EVP_CIPHER_CTX_get_params(c, params))
  124. goto err;
  125. /* ... but, we should get a return size too! */
  126. if (OSSL_PARAM_modified(params)
  127. && params[0].return_size != 0
  128. && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
  129. params[0].data = der;
  130. params[0].data_size = params[0].return_size;
  131. OSSL_PARAM_set_all_unmodified(params);
  132. derp = der;
  133. if (EVP_CIPHER_CTX_get_params(c, params)
  134. && OSSL_PARAM_modified(params)
  135. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  136. params[0].return_size) != NULL) {
  137. ret = 1;
  138. }
  139. OPENSSL_free(der);
  140. }
  141. } else {
  142. ret = -2;
  143. }
  144. err:
  145. if (ret == -2)
  146. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  147. else if (ret <= 0)
  148. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  149. if (ret < -1)
  150. ret = -1;
  151. return ret;
  152. }
  153. int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  154. evp_cipher_aead_asn1_params *asn1_params)
  155. {
  156. int ret = -1; /* Assume the worst */
  157. const EVP_CIPHER *cipher;
  158. if (c == NULL || c->cipher == NULL)
  159. goto err;
  160. cipher = c->cipher;
  161. /*
  162. * For legacy implementations, we detect custom AlgorithmIdentifier
  163. * parameter handling by checking if there the function pointer
  164. * cipher->get_asn1_parameters is set. We know that this pointer
  165. * is NULL for provided implementations.
  166. *
  167. * Otherwise, for any implementation, we check the flag
  168. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  169. * default AI parameter creation.
  170. *
  171. * Otherwise, for provided implementations, we get the AI parameter
  172. * in DER encoded form from the implementation by requesting the
  173. * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
  174. *
  175. * If none of the above applies, this operation is unsupported.
  176. */
  177. if (cipher->get_asn1_parameters != NULL) {
  178. ret = cipher->get_asn1_parameters(c, type);
  179. } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  180. switch (EVP_CIPHER_get_mode(cipher)) {
  181. case EVP_CIPH_WRAP_MODE:
  182. ret = 1;
  183. break;
  184. case EVP_CIPH_GCM_MODE:
  185. ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
  186. break;
  187. case EVP_CIPH_CCM_MODE:
  188. case EVP_CIPH_XTS_MODE:
  189. case EVP_CIPH_OCB_MODE:
  190. ret = -2;
  191. break;
  192. default:
  193. ret = EVP_CIPHER_get_asn1_iv(c, type) >= 0 ? 1 : -1;
  194. }
  195. } else if (cipher->prov != NULL) {
  196. OSSL_PARAM params[3], *p = params;
  197. unsigned char *der = NULL;
  198. int derl = -1;
  199. if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
  200. *p++ =
  201. OSSL_PARAM_construct_octet_string(
  202. OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
  203. der, (size_t)derl);
  204. *p = OSSL_PARAM_construct_end();
  205. if (EVP_CIPHER_CTX_set_params(c, params))
  206. ret = 1;
  207. OPENSSL_free(der);
  208. }
  209. } else {
  210. ret = -2;
  211. }
  212. err:
  213. if (ret == -2)
  214. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  215. else if (ret <= 0)
  216. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  217. if (ret < -1)
  218. ret = -1;
  219. return ret;
  220. }
  221. int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  222. evp_cipher_aead_asn1_params *asn1_params)
  223. {
  224. int i = 0;
  225. long tl;
  226. unsigned char iv[EVP_MAX_IV_LENGTH];
  227. if (type == NULL || asn1_params == NULL)
  228. return 0;
  229. i = ossl_asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
  230. if (i <= 0)
  231. return -1;
  232. ossl_asn1_type_get_octetstring_int(type, &tl, iv, i);
  233. memcpy(asn1_params->iv, iv, i);
  234. asn1_params->iv_len = i;
  235. return i;
  236. }
  237. int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  238. evp_cipher_aead_asn1_params *asn1_params)
  239. {
  240. if (type == NULL || asn1_params == NULL)
  241. return 0;
  242. return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
  243. asn1_params->iv,
  244. asn1_params->iv_len);
  245. }
  246. #endif /* !defined(FIPS_MODULE) */
  247. /* Convert the various cipher NIDs and dummies to a proper OID NID */
  248. int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
  249. {
  250. int nid;
  251. nid = EVP_CIPHER_get_nid(cipher);
  252. switch (nid) {
  253. case NID_rc2_cbc:
  254. case NID_rc2_64_cbc:
  255. case NID_rc2_40_cbc:
  256. return NID_rc2_cbc;
  257. case NID_rc4:
  258. case NID_rc4_40:
  259. return NID_rc4;
  260. case NID_aes_128_cfb128:
  261. case NID_aes_128_cfb8:
  262. case NID_aes_128_cfb1:
  263. return NID_aes_128_cfb128;
  264. case NID_aes_192_cfb128:
  265. case NID_aes_192_cfb8:
  266. case NID_aes_192_cfb1:
  267. return NID_aes_192_cfb128;
  268. case NID_aes_256_cfb128:
  269. case NID_aes_256_cfb8:
  270. case NID_aes_256_cfb1:
  271. return NID_aes_256_cfb128;
  272. case NID_des_cfb64:
  273. case NID_des_cfb8:
  274. case NID_des_cfb1:
  275. return NID_des_cfb64;
  276. case NID_des_ede3_cfb64:
  277. case NID_des_ede3_cfb8:
  278. case NID_des_ede3_cfb1:
  279. return NID_des_cfb64;
  280. default:
  281. #ifdef FIPS_MODULE
  282. return NID_undef;
  283. #else
  284. {
  285. /* Check it has an OID and it is valid */
  286. ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
  287. if (OBJ_get0_data(otmp) == NULL)
  288. nid = NID_undef;
  289. ASN1_OBJECT_free(otmp);
  290. return nid;
  291. }
  292. #endif
  293. }
  294. }
  295. int evp_cipher_cache_constants(EVP_CIPHER *cipher)
  296. {
  297. int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
  298. size_t ivlen = 0;
  299. size_t blksz = 0;
  300. size_t keylen = 0;
  301. unsigned int mode = 0;
  302. OSSL_PARAM params[10];
  303. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
  304. params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
  305. params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
  306. params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
  307. params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
  308. params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
  309. &custom_iv);
  310. params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
  311. params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
  312. &multiblock);
  313. params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
  314. &randkey);
  315. params[9] = OSSL_PARAM_construct_end();
  316. ok = evp_do_ciph_getparams(cipher, params) > 0;
  317. if (ok) {
  318. cipher->block_size = blksz;
  319. cipher->iv_len = ivlen;
  320. cipher->key_len = keylen;
  321. cipher->flags = mode;
  322. if (aead)
  323. cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
  324. if (custom_iv)
  325. cipher->flags |= EVP_CIPH_CUSTOM_IV;
  326. if (cts)
  327. cipher->flags |= EVP_CIPH_FLAG_CTS;
  328. if (multiblock)
  329. cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
  330. if (cipher->ccipher != NULL)
  331. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
  332. if (randkey)
  333. cipher->flags |= EVP_CIPH_RAND_KEY;
  334. if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
  335. OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
  336. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
  337. }
  338. return ok;
  339. }
  340. int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
  341. {
  342. return (cipher == NULL) ? 0 : cipher->block_size;
  343. }
  344. int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
  345. {
  346. return EVP_CIPHER_get_block_size(ctx->cipher);
  347. }
  348. int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
  349. {
  350. return e->ctx_size;
  351. }
  352. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  353. const unsigned char *in, unsigned int inl)
  354. {
  355. if (ctx == NULL || ctx->cipher == NULL)
  356. return 0;
  357. if (ctx->cipher->prov != NULL) {
  358. /*
  359. * If the provided implementation has a ccipher function, we use it,
  360. * and translate its return value like this: 0 => -1, 1 => outlen
  361. *
  362. * Otherwise, we call the cupdate function if in != NULL, or cfinal
  363. * if in == NULL. Regardless of which, we return what we got.
  364. */
  365. int ret = -1;
  366. size_t outl = 0;
  367. size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
  368. if (blocksize == 0)
  369. return 0;
  370. if (ctx->cipher->ccipher != NULL)
  371. ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
  372. inl + (blocksize == 1 ? 0 : blocksize),
  373. in, (size_t)inl)
  374. ? (int)outl : -1;
  375. else if (in != NULL)
  376. ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
  377. inl + (blocksize == 1 ? 0 : blocksize),
  378. in, (size_t)inl);
  379. else
  380. ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
  381. blocksize == 1 ? 0 : blocksize);
  382. return ret;
  383. }
  384. return ctx->cipher->do_cipher(ctx, out, in, inl);
  385. }
  386. #ifndef OPENSSL_NO_DEPRECATED_3_0
  387. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
  388. {
  389. if (ctx == NULL)
  390. return NULL;
  391. return ctx->cipher;
  392. }
  393. #endif
  394. const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
  395. {
  396. if (ctx == NULL)
  397. return NULL;
  398. return ctx->cipher;
  399. }
  400. EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
  401. {
  402. EVP_CIPHER *cipher;
  403. if (ctx == NULL || ctx->cipher == NULL)
  404. return NULL;
  405. cipher = (EVP_CIPHER *)ctx->cipher;
  406. if (!EVP_CIPHER_up_ref(cipher))
  407. return NULL;
  408. return cipher;
  409. }
  410. int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
  411. {
  412. return ctx->encrypt;
  413. }
  414. unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
  415. {
  416. return cipher == NULL ? 0 : cipher->flags;
  417. }
  418. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
  419. {
  420. return ctx->app_data;
  421. }
  422. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
  423. {
  424. ctx->app_data = data;
  425. }
  426. void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
  427. {
  428. return ctx->cipher_data;
  429. }
  430. void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
  431. {
  432. void *old_cipher_data;
  433. old_cipher_data = ctx->cipher_data;
  434. ctx->cipher_data = cipher_data;
  435. return old_cipher_data;
  436. }
  437. int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
  438. {
  439. return (cipher == NULL) ? 0 : cipher->iv_len;
  440. }
  441. int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
  442. {
  443. if (ctx->cipher == NULL)
  444. return 0;
  445. if (ctx->iv_len < 0) {
  446. int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
  447. size_t v = len;
  448. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  449. if (ctx->cipher->get_ctx_params != NULL) {
  450. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
  451. &v);
  452. rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  453. if (rv > 0) {
  454. if (OSSL_PARAM_modified(params)
  455. && !OSSL_PARAM_get_int(params, &len))
  456. return -1;
  457. } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
  458. return -1;
  459. }
  460. }
  461. /* Code below to be removed when legacy support is dropped. */
  462. else if ((EVP_CIPHER_get_flags(ctx->cipher)
  463. & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
  464. rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
  465. 0, &len);
  466. if (rv <= 0)
  467. return -1;
  468. }
  469. /*-
  470. * Casting away the const is annoying but required here. We need to
  471. * cache the result for performance reasons.
  472. */
  473. ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
  474. }
  475. return ctx->iv_len;
  476. }
  477. int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
  478. {
  479. int ret;
  480. size_t v = 0;
  481. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  482. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
  483. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  484. return ret == 1 ? (int)v : 0;
  485. }
  486. #ifndef OPENSSL_NO_DEPRECATED_3_0
  487. const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
  488. {
  489. int ok;
  490. const unsigned char *v = ctx->oiv;
  491. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  492. params[0] =
  493. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
  494. (void **)&v, sizeof(ctx->oiv));
  495. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  496. return ok != 0 ? v : NULL;
  497. }
  498. /*
  499. * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
  500. */
  501. const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
  502. {
  503. int ok;
  504. const unsigned char *v = ctx->iv;
  505. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  506. params[0] =
  507. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  508. (void **)&v, sizeof(ctx->iv));
  509. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  510. return ok != 0 ? v : NULL;
  511. }
  512. unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
  513. {
  514. int ok;
  515. unsigned char *v = ctx->iv;
  516. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  517. params[0] =
  518. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  519. (void **)&v, sizeof(ctx->iv));
  520. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  521. return ok != 0 ? v : NULL;
  522. }
  523. #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
  524. int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  525. {
  526. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  527. params[0] =
  528. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
  529. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  530. }
  531. int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  532. {
  533. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  534. params[0] =
  535. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
  536. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
  537. }
  538. unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
  539. {
  540. return ctx->buf;
  541. }
  542. int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
  543. {
  544. int ok;
  545. unsigned int v = (unsigned int)ctx->num;
  546. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  547. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
  548. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  549. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  550. }
  551. int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
  552. {
  553. int ok;
  554. unsigned int n = (unsigned int)num;
  555. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  556. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
  557. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
  558. if (ok != 0)
  559. ctx->num = (int)n;
  560. return ok != 0;
  561. }
  562. int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
  563. {
  564. return cipher->key_len;
  565. }
  566. int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
  567. {
  568. if (ctx->cipher == NULL)
  569. return 0;
  570. if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
  571. int ok;
  572. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  573. size_t len;
  574. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
  575. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  576. if (ok <= 0)
  577. return EVP_CTRL_RET_UNSUPPORTED;
  578. /*-
  579. * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
  580. * less than INT_MAX but best to be safe.
  581. *
  582. * Casting away the const is annoying but required here. We need to
  583. * cache the result for performance reasons.
  584. */
  585. if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
  586. return -1;
  587. ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
  588. }
  589. return ctx->key_len;
  590. }
  591. int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
  592. {
  593. return (cipher == NULL) ? NID_undef : cipher->nid;
  594. }
  595. int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
  596. {
  597. return EVP_CIPHER_get_nid(ctx->cipher);
  598. }
  599. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  600. {
  601. if (cipher == NULL)
  602. return 0;
  603. if (cipher->prov != NULL)
  604. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  605. return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
  606. }
  607. int evp_cipher_get_number(const EVP_CIPHER *cipher)
  608. {
  609. return cipher->name_id;
  610. }
  611. const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
  612. {
  613. if (cipher->type_name != NULL)
  614. return cipher->type_name;
  615. #ifndef FIPS_MODULE
  616. return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
  617. #else
  618. return NULL;
  619. #endif
  620. }
  621. const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
  622. {
  623. if (cipher->description != NULL)
  624. return cipher->description;
  625. #ifndef FIPS_MODULE
  626. return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
  627. #else
  628. return NULL;
  629. #endif
  630. }
  631. int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  632. void (*fn)(const char *name, void *data),
  633. void *data)
  634. {
  635. if (cipher->prov != NULL)
  636. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  637. return 1;
  638. }
  639. const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
  640. {
  641. return cipher->prov;
  642. }
  643. int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
  644. {
  645. return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
  646. }
  647. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  648. {
  649. if (md == NULL)
  650. return 0;
  651. if (md->prov != NULL)
  652. return evp_is_a(md->prov, md->name_id, NULL, name);
  653. return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
  654. }
  655. int evp_md_get_number(const EVP_MD *md)
  656. {
  657. return md->name_id;
  658. }
  659. const char *EVP_MD_get0_description(const EVP_MD *md)
  660. {
  661. if (md->description != NULL)
  662. return md->description;
  663. #ifndef FIPS_MODULE
  664. return OBJ_nid2ln(EVP_MD_nid(md));
  665. #else
  666. return NULL;
  667. #endif
  668. }
  669. const char *EVP_MD_get0_name(const EVP_MD *md)
  670. {
  671. if (md == NULL)
  672. return NULL;
  673. if (md->type_name != NULL)
  674. return md->type_name;
  675. #ifndef FIPS_MODULE
  676. return OBJ_nid2sn(EVP_MD_nid(md));
  677. #else
  678. return NULL;
  679. #endif
  680. }
  681. int EVP_MD_names_do_all(const EVP_MD *md,
  682. void (*fn)(const char *name, void *data),
  683. void *data)
  684. {
  685. if (md->prov != NULL)
  686. return evp_names_do_all(md->prov, md->name_id, fn, data);
  687. return 1;
  688. }
  689. const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
  690. {
  691. return md->prov;
  692. }
  693. int EVP_MD_get_type(const EVP_MD *md)
  694. {
  695. return md->type;
  696. }
  697. int EVP_MD_get_pkey_type(const EVP_MD *md)
  698. {
  699. return md->pkey_type;
  700. }
  701. int EVP_MD_get_block_size(const EVP_MD *md)
  702. {
  703. if (md == NULL) {
  704. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  705. return -1;
  706. }
  707. return md->block_size;
  708. }
  709. int EVP_MD_get_size(const EVP_MD *md)
  710. {
  711. if (md == NULL) {
  712. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  713. return -1;
  714. }
  715. return md->md_size;
  716. }
  717. unsigned long EVP_MD_get_flags(const EVP_MD *md)
  718. {
  719. return md->flags;
  720. }
  721. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  722. {
  723. EVP_MD *md = evp_md_new();
  724. if (md != NULL) {
  725. md->type = md_type;
  726. md->pkey_type = pkey_type;
  727. md->origin = EVP_ORIG_METH;
  728. }
  729. return md;
  730. }
  731. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  732. {
  733. EVP_MD *to = NULL;
  734. /*
  735. * Non-legacy EVP_MDs can't be duplicated like this.
  736. * Use EVP_MD_up_ref() instead.
  737. */
  738. if (md->prov != NULL)
  739. return NULL;
  740. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  741. CRYPTO_REF_COUNT refcnt = to->refcnt;
  742. memcpy(to, md, sizeof(*to));
  743. to->refcnt = refcnt;
  744. to->origin = EVP_ORIG_METH;
  745. }
  746. return to;
  747. }
  748. void evp_md_free_int(EVP_MD *md)
  749. {
  750. OPENSSL_free(md->type_name);
  751. ossl_provider_free(md->prov);
  752. CRYPTO_FREE_REF(&md->refcnt);
  753. OPENSSL_free(md);
  754. }
  755. void EVP_MD_meth_free(EVP_MD *md)
  756. {
  757. if (md == NULL || md->origin != EVP_ORIG_METH)
  758. return;
  759. evp_md_free_int(md);
  760. }
  761. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  762. {
  763. if (md->block_size != 0)
  764. return 0;
  765. md->block_size = blocksize;
  766. return 1;
  767. }
  768. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  769. {
  770. if (md->md_size != 0)
  771. return 0;
  772. md->md_size = resultsize;
  773. return 1;
  774. }
  775. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  776. {
  777. if (md->ctx_size != 0)
  778. return 0;
  779. md->ctx_size = datasize;
  780. return 1;
  781. }
  782. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  783. {
  784. if (md->flags != 0)
  785. return 0;
  786. md->flags = flags;
  787. return 1;
  788. }
  789. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  790. {
  791. if (md->init != NULL)
  792. return 0;
  793. md->init = init;
  794. return 1;
  795. }
  796. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
  797. const void *data,
  798. size_t count))
  799. {
  800. if (md->update != NULL)
  801. return 0;
  802. md->update = update;
  803. return 1;
  804. }
  805. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
  806. unsigned char *md))
  807. {
  808. if (md->final != NULL)
  809. return 0;
  810. md->final = final;
  811. return 1;
  812. }
  813. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
  814. const EVP_MD_CTX *from))
  815. {
  816. if (md->copy != NULL)
  817. return 0;
  818. md->copy = copy;
  819. return 1;
  820. }
  821. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  822. {
  823. if (md->cleanup != NULL)
  824. return 0;
  825. md->cleanup = cleanup;
  826. return 1;
  827. }
  828. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
  829. int p1, void *p2))
  830. {
  831. if (md->md_ctrl != NULL)
  832. return 0;
  833. md->md_ctrl = ctrl;
  834. return 1;
  835. }
  836. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  837. {
  838. return md->block_size;
  839. }
  840. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  841. {
  842. return md->md_size;
  843. }
  844. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  845. {
  846. return md->ctx_size;
  847. }
  848. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  849. {
  850. return md->flags;
  851. }
  852. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  853. {
  854. return md->init;
  855. }
  856. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  857. const void *data,
  858. size_t count)
  859. {
  860. return md->update;
  861. }
  862. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  863. unsigned char *md)
  864. {
  865. return md->final;
  866. }
  867. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  868. const EVP_MD_CTX *from)
  869. {
  870. return md->copy;
  871. }
  872. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  873. {
  874. return md->cleanup;
  875. }
  876. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  877. int p1, void *p2)
  878. {
  879. return md->md_ctrl;
  880. }
  881. #ifndef OPENSSL_NO_DEPRECATED_3_0
  882. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  883. {
  884. if (ctx == NULL)
  885. return NULL;
  886. return ctx->reqdigest;
  887. }
  888. #endif
  889. const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
  890. {
  891. if (ctx == NULL)
  892. return NULL;
  893. return ctx->reqdigest;
  894. }
  895. EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
  896. {
  897. EVP_MD *md;
  898. if (ctx == NULL)
  899. return NULL;
  900. md = (EVP_MD *)ctx->reqdigest;
  901. if (md == NULL || !EVP_MD_up_ref(md))
  902. return NULL;
  903. return md;
  904. }
  905. EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
  906. {
  907. return ctx->pctx;
  908. }
  909. #if !defined(FIPS_MODULE)
  910. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  911. {
  912. /*
  913. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  914. * we have to deal with the cleanup job here.
  915. */
  916. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  917. EVP_PKEY_CTX_free(ctx->pctx);
  918. ctx->pctx = pctx;
  919. if (pctx != NULL) {
  920. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  921. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  922. } else {
  923. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  924. }
  925. }
  926. #endif /* !defined(FIPS_MODULE) */
  927. void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
  928. {
  929. return ctx->md_data;
  930. }
  931. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  932. const void *data, size_t count)
  933. {
  934. return ctx->update;
  935. }
  936. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  937. int (*update) (EVP_MD_CTX *ctx,
  938. const void *data, size_t count))
  939. {
  940. ctx->update = update;
  941. }
  942. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  943. {
  944. ctx->flags |= flags;
  945. }
  946. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  947. {
  948. ctx->flags &= ~flags;
  949. }
  950. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  951. {
  952. return (ctx->flags & flags);
  953. }
  954. static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
  955. unsigned int enable)
  956. {
  957. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  958. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
  959. return EVP_CIPHER_CTX_set_params(ctx, params);
  960. }
  961. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  962. {
  963. int oldflags = ctx->flags;
  964. ctx->flags |= flags;
  965. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  966. evp_cipher_ctx_enable_use_bits(ctx, 1);
  967. }
  968. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  969. {
  970. int oldflags = ctx->flags;
  971. ctx->flags &= ~flags;
  972. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  973. evp_cipher_ctx_enable_use_bits(ctx, 0);
  974. }
  975. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  976. {
  977. return (ctx->flags & flags);
  978. }
  979. #if !defined(FIPS_MODULE)
  980. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  981. {
  982. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  983. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  984. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  985. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  986. return -2;
  987. }
  988. if (name == NULL)
  989. return -1;
  990. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  991. (char *)name, 0);
  992. return EVP_PKEY_CTX_set_params(ctx, params);
  993. }
  994. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  995. {
  996. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  997. OSSL_PARAM *p = params;
  998. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  999. /* There is no legacy support for this */
  1000. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1001. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1002. return -2;
  1003. }
  1004. if (name == NULL)
  1005. return -1;
  1006. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1007. name, namelen);
  1008. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1009. return -1;
  1010. return 1;
  1011. }
  1012. /*
  1013. * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
  1014. * while providing a generic way of generating a new asymmetric key pair
  1015. * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
  1016. * The library context I<libctx> and property query I<propq>
  1017. * are used when fetching algorithms from providers.
  1018. * The I<params> specify algorithm-specific parameters
  1019. * such as the RSA modulus size or the name of an EC curve.
  1020. */
  1021. static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
  1022. const char *propq, const OSSL_PARAM *params)
  1023. {
  1024. EVP_PKEY *pkey = NULL;
  1025. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
  1026. if (ctx != NULL
  1027. && EVP_PKEY_keygen_init(ctx) > 0
  1028. && EVP_PKEY_CTX_set_params(ctx, params))
  1029. (void)EVP_PKEY_generate(ctx, &pkey);
  1030. EVP_PKEY_CTX_free(ctx);
  1031. return pkey;
  1032. }
  1033. EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
  1034. const char *type, ...)
  1035. {
  1036. va_list args;
  1037. size_t bits;
  1038. char *name;
  1039. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1040. EVP_PKEY *ret = NULL;
  1041. va_start(args, type);
  1042. if (OPENSSL_strcasecmp(type, "RSA") == 0) {
  1043. bits = va_arg(args, size_t);
  1044. params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
  1045. } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
  1046. name = va_arg(args, char *);
  1047. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1048. name, 0);
  1049. } else if (OPENSSL_strcasecmp(type, "ED25519") != 0
  1050. && OPENSSL_strcasecmp(type, "X25519") != 0
  1051. && OPENSSL_strcasecmp(type, "ED448") != 0
  1052. && OPENSSL_strcasecmp(type, "X448") != 0
  1053. && OPENSSL_strcasecmp(type, "SM2") != 0) {
  1054. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
  1055. goto end;
  1056. }
  1057. ret = evp_pkey_keygen(libctx, type, propq, params);
  1058. end:
  1059. va_end(args);
  1060. return ret;
  1061. }
  1062. #endif /* !defined(FIPS_MODULE) */