evp_lib.c 33 KB

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