evp_lib.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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->key_len <= 0 && ctx->cipher->prov != NULL) {
  569. int ok;
  570. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  571. size_t len;
  572. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
  573. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
  574. if (ok <= 0)
  575. return EVP_CTRL_RET_UNSUPPORTED;
  576. /*-
  577. * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
  578. * less than INT_MAX but best to be safe.
  579. *
  580. * Casting away the const is annoying but required here. We need to
  581. * cache the result for performance reasons.
  582. */
  583. if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
  584. return -1;
  585. ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
  586. }
  587. return ctx->key_len;
  588. }
  589. int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
  590. {
  591. return (cipher == NULL) ? NID_undef : cipher->nid;
  592. }
  593. int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
  594. {
  595. return EVP_CIPHER_get_nid(ctx->cipher);
  596. }
  597. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  598. {
  599. if (cipher == NULL)
  600. return 0;
  601. if (cipher->prov != NULL)
  602. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  603. return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
  604. }
  605. int evp_cipher_get_number(const EVP_CIPHER *cipher)
  606. {
  607. return cipher->name_id;
  608. }
  609. const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
  610. {
  611. if (cipher->type_name != NULL)
  612. return cipher->type_name;
  613. #ifndef FIPS_MODULE
  614. return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
  615. #else
  616. return NULL;
  617. #endif
  618. }
  619. const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
  620. {
  621. if (cipher->description != NULL)
  622. return cipher->description;
  623. #ifndef FIPS_MODULE
  624. return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
  625. #else
  626. return NULL;
  627. #endif
  628. }
  629. int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  630. void (*fn)(const char *name, void *data),
  631. void *data)
  632. {
  633. if (cipher->prov != NULL)
  634. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  635. return 1;
  636. }
  637. const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
  638. {
  639. return cipher->prov;
  640. }
  641. int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
  642. {
  643. return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
  644. }
  645. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  646. {
  647. if (md == NULL)
  648. return 0;
  649. if (md->prov != NULL)
  650. return evp_is_a(md->prov, md->name_id, NULL, name);
  651. return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
  652. }
  653. int evp_md_get_number(const EVP_MD *md)
  654. {
  655. return md->name_id;
  656. }
  657. const char *EVP_MD_get0_description(const EVP_MD *md)
  658. {
  659. if (md->description != NULL)
  660. return md->description;
  661. #ifndef FIPS_MODULE
  662. return OBJ_nid2ln(EVP_MD_nid(md));
  663. #else
  664. return NULL;
  665. #endif
  666. }
  667. const char *EVP_MD_get0_name(const EVP_MD *md)
  668. {
  669. if (md == NULL)
  670. return NULL;
  671. if (md->type_name != NULL)
  672. return md->type_name;
  673. #ifndef FIPS_MODULE
  674. return OBJ_nid2sn(EVP_MD_nid(md));
  675. #else
  676. return NULL;
  677. #endif
  678. }
  679. int EVP_MD_names_do_all(const EVP_MD *md,
  680. void (*fn)(const char *name, void *data),
  681. void *data)
  682. {
  683. if (md->prov != NULL)
  684. return evp_names_do_all(md->prov, md->name_id, fn, data);
  685. return 1;
  686. }
  687. const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
  688. {
  689. return md->prov;
  690. }
  691. int EVP_MD_get_type(const EVP_MD *md)
  692. {
  693. return md->type;
  694. }
  695. int EVP_MD_get_pkey_type(const EVP_MD *md)
  696. {
  697. return md->pkey_type;
  698. }
  699. int EVP_MD_get_block_size(const EVP_MD *md)
  700. {
  701. if (md == NULL) {
  702. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  703. return -1;
  704. }
  705. return md->block_size;
  706. }
  707. int EVP_MD_get_size(const EVP_MD *md)
  708. {
  709. if (md == NULL) {
  710. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  711. return -1;
  712. }
  713. return md->md_size;
  714. }
  715. unsigned long EVP_MD_get_flags(const EVP_MD *md)
  716. {
  717. return md->flags;
  718. }
  719. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  720. {
  721. EVP_MD *md = evp_md_new();
  722. if (md != NULL) {
  723. md->type = md_type;
  724. md->pkey_type = pkey_type;
  725. md->origin = EVP_ORIG_METH;
  726. }
  727. return md;
  728. }
  729. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  730. {
  731. EVP_MD *to = NULL;
  732. /*
  733. * Non-legacy EVP_MDs can't be duplicated like this.
  734. * Use EVP_MD_up_ref() instead.
  735. */
  736. if (md->prov != NULL)
  737. return NULL;
  738. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  739. CRYPTO_REF_COUNT refcnt = to->refcnt;
  740. memcpy(to, md, sizeof(*to));
  741. to->refcnt = refcnt;
  742. to->origin = EVP_ORIG_METH;
  743. }
  744. return to;
  745. }
  746. void evp_md_free_int(EVP_MD *md)
  747. {
  748. OPENSSL_free(md->type_name);
  749. ossl_provider_free(md->prov);
  750. CRYPTO_FREE_REF(&md->refcnt);
  751. OPENSSL_free(md);
  752. }
  753. void EVP_MD_meth_free(EVP_MD *md)
  754. {
  755. if (md == NULL || md->origin != EVP_ORIG_METH)
  756. return;
  757. evp_md_free_int(md);
  758. }
  759. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  760. {
  761. if (md->block_size != 0)
  762. return 0;
  763. md->block_size = blocksize;
  764. return 1;
  765. }
  766. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  767. {
  768. if (md->md_size != 0)
  769. return 0;
  770. md->md_size = resultsize;
  771. return 1;
  772. }
  773. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  774. {
  775. if (md->ctx_size != 0)
  776. return 0;
  777. md->ctx_size = datasize;
  778. return 1;
  779. }
  780. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  781. {
  782. if (md->flags != 0)
  783. return 0;
  784. md->flags = flags;
  785. return 1;
  786. }
  787. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  788. {
  789. if (md->init != NULL)
  790. return 0;
  791. md->init = init;
  792. return 1;
  793. }
  794. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
  795. const void *data,
  796. size_t count))
  797. {
  798. if (md->update != NULL)
  799. return 0;
  800. md->update = update;
  801. return 1;
  802. }
  803. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
  804. unsigned char *md))
  805. {
  806. if (md->final != NULL)
  807. return 0;
  808. md->final = final;
  809. return 1;
  810. }
  811. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
  812. const EVP_MD_CTX *from))
  813. {
  814. if (md->copy != NULL)
  815. return 0;
  816. md->copy = copy;
  817. return 1;
  818. }
  819. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  820. {
  821. if (md->cleanup != NULL)
  822. return 0;
  823. md->cleanup = cleanup;
  824. return 1;
  825. }
  826. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
  827. int p1, void *p2))
  828. {
  829. if (md->md_ctrl != NULL)
  830. return 0;
  831. md->md_ctrl = ctrl;
  832. return 1;
  833. }
  834. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  835. {
  836. return md->block_size;
  837. }
  838. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  839. {
  840. return md->md_size;
  841. }
  842. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  843. {
  844. return md->ctx_size;
  845. }
  846. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  847. {
  848. return md->flags;
  849. }
  850. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  851. {
  852. return md->init;
  853. }
  854. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  855. const void *data,
  856. size_t count)
  857. {
  858. return md->update;
  859. }
  860. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  861. unsigned char *md)
  862. {
  863. return md->final;
  864. }
  865. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  866. const EVP_MD_CTX *from)
  867. {
  868. return md->copy;
  869. }
  870. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  871. {
  872. return md->cleanup;
  873. }
  874. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  875. int p1, void *p2)
  876. {
  877. return md->md_ctrl;
  878. }
  879. #ifndef OPENSSL_NO_DEPRECATED_3_0
  880. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  881. {
  882. if (ctx == NULL)
  883. return NULL;
  884. return ctx->reqdigest;
  885. }
  886. #endif
  887. const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
  888. {
  889. if (ctx == NULL)
  890. return NULL;
  891. return ctx->reqdigest;
  892. }
  893. EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
  894. {
  895. EVP_MD *md;
  896. if (ctx == NULL)
  897. return NULL;
  898. md = (EVP_MD *)ctx->reqdigest;
  899. if (md == NULL || !EVP_MD_up_ref(md))
  900. return NULL;
  901. return md;
  902. }
  903. EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
  904. {
  905. return ctx->pctx;
  906. }
  907. #if !defined(FIPS_MODULE)
  908. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  909. {
  910. /*
  911. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  912. * we have to deal with the cleanup job here.
  913. */
  914. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  915. EVP_PKEY_CTX_free(ctx->pctx);
  916. ctx->pctx = pctx;
  917. if (pctx != NULL) {
  918. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  919. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  920. } else {
  921. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  922. }
  923. }
  924. #endif /* !defined(FIPS_MODULE) */
  925. void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
  926. {
  927. return ctx->md_data;
  928. }
  929. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  930. const void *data, size_t count)
  931. {
  932. return ctx->update;
  933. }
  934. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  935. int (*update) (EVP_MD_CTX *ctx,
  936. const void *data, size_t count))
  937. {
  938. ctx->update = update;
  939. }
  940. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  941. {
  942. ctx->flags |= flags;
  943. }
  944. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  945. {
  946. ctx->flags &= ~flags;
  947. }
  948. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  949. {
  950. return (ctx->flags & flags);
  951. }
  952. static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
  953. unsigned int enable)
  954. {
  955. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  956. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
  957. return EVP_CIPHER_CTX_set_params(ctx, params);
  958. }
  959. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  960. {
  961. int oldflags = ctx->flags;
  962. ctx->flags |= flags;
  963. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  964. evp_cipher_ctx_enable_use_bits(ctx, 1);
  965. }
  966. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  967. {
  968. int oldflags = ctx->flags;
  969. ctx->flags &= ~flags;
  970. if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
  971. evp_cipher_ctx_enable_use_bits(ctx, 0);
  972. }
  973. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  974. {
  975. return (ctx->flags & flags);
  976. }
  977. #if !defined(FIPS_MODULE)
  978. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  979. {
  980. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  981. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  982. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  983. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  984. return -2;
  985. }
  986. if (name == NULL)
  987. return -1;
  988. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  989. (char *)name, 0);
  990. return EVP_PKEY_CTX_set_params(ctx, params);
  991. }
  992. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  993. {
  994. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  995. OSSL_PARAM *p = params;
  996. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  997. /* There is no legacy support for this */
  998. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  999. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1000. return -2;
  1001. }
  1002. if (name == NULL)
  1003. return -1;
  1004. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1005. name, namelen);
  1006. if (!EVP_PKEY_CTX_get_params(ctx, params))
  1007. return -1;
  1008. return 1;
  1009. }
  1010. /*
  1011. * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
  1012. * while providing a generic way of generating a new asymmetric key pair
  1013. * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
  1014. * The library context I<libctx> and property query I<propq>
  1015. * are used when fetching algorithms from providers.
  1016. * The I<params> specify algorithm-specific parameters
  1017. * such as the RSA modulus size or the name of an EC curve.
  1018. */
  1019. static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
  1020. const char *propq, const OSSL_PARAM *params)
  1021. {
  1022. EVP_PKEY *pkey = NULL;
  1023. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
  1024. if (ctx != NULL
  1025. && EVP_PKEY_keygen_init(ctx) > 0
  1026. && EVP_PKEY_CTX_set_params(ctx, params))
  1027. (void)EVP_PKEY_generate(ctx, &pkey);
  1028. EVP_PKEY_CTX_free(ctx);
  1029. return pkey;
  1030. }
  1031. EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
  1032. const char *type, ...)
  1033. {
  1034. va_list args;
  1035. size_t bits;
  1036. char *name;
  1037. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1038. EVP_PKEY *ret = NULL;
  1039. va_start(args, type);
  1040. if (OPENSSL_strcasecmp(type, "RSA") == 0) {
  1041. bits = va_arg(args, size_t);
  1042. params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
  1043. } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
  1044. name = va_arg(args, char *);
  1045. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1046. name, 0);
  1047. } else if (OPENSSL_strcasecmp(type, "ED25519") != 0
  1048. && OPENSSL_strcasecmp(type, "X25519") != 0
  1049. && OPENSSL_strcasecmp(type, "ED448") != 0
  1050. && OPENSSL_strcasecmp(type, "X448") != 0
  1051. && OPENSSL_strcasecmp(type, "SM2") != 0) {
  1052. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
  1053. goto end;
  1054. }
  1055. ret = evp_pkey_keygen(libctx, type, propq, params);
  1056. end:
  1057. va_end(args);
  1058. return ret;
  1059. }
  1060. #endif /* !defined(FIPS_MODULE) */