evp_lib.c 32 KB

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