evp_lib.c 33 KB

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