evp_lib.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 "internal/cryptlib.h"
  16. #include <openssl/evp.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/params.h>
  19. #include <openssl/core_names.h>
  20. #include <openssl/dh.h>
  21. #include <openssl/ec.h>
  22. #include "crypto/evp.h"
  23. #include "crypto/asn1.h"
  24. #include "internal/provider.h"
  25. #include "evp_local.h"
  26. #if !defined(FIPS_MODULE)
  27. int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  28. {
  29. return evp_cipher_param_to_asn1_ex(c, type, NULL);
  30. }
  31. int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  32. {
  33. return evp_cipher_asn1_to_param_ex(c, type, NULL);
  34. }
  35. int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
  36. {
  37. int i = 0;
  38. unsigned int l;
  39. if (type != NULL) {
  40. unsigned char iv[EVP_MAX_IV_LENGTH];
  41. l = EVP_CIPHER_CTX_iv_length(ctx);
  42. if (!ossl_assert(l <= sizeof(iv)))
  43. return -1;
  44. i = ASN1_TYPE_get_octetstring(type, iv, l);
  45. if (i != (int)l)
  46. return -1;
  47. if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
  48. return -1;
  49. }
  50. return i;
  51. }
  52. int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
  53. {
  54. int i = 0;
  55. unsigned int j;
  56. unsigned char *oiv = NULL;
  57. if (type != NULL) {
  58. oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
  59. j = EVP_CIPHER_CTX_iv_length(c);
  60. OPENSSL_assert(j <= sizeof(c->iv));
  61. i = ASN1_TYPE_set_octetstring(type, oiv, j);
  62. }
  63. return i;
  64. }
  65. int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  66. evp_cipher_aead_asn1_params *asn1_params)
  67. {
  68. int ret = -1; /* Assume the worst */
  69. const EVP_CIPHER *cipher = c->cipher;
  70. /*
  71. * For legacy implementations, we detect custom AlgorithmIdentifier
  72. * parameter handling by checking if the function pointer
  73. * cipher->set_asn1_parameters is set. We know that this pointer
  74. * is NULL for provided implementations.
  75. *
  76. * Otherwise, for any implementation, we check the flag
  77. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  78. * default AI parameter extraction.
  79. *
  80. * Otherwise, for provided implementations, we convert |type| to
  81. * a DER encoded blob and pass to the implementation in OSSL_PARAM
  82. * form.
  83. *
  84. * If none of the above applies, this operation is unsupported.
  85. */
  86. if (cipher->set_asn1_parameters != NULL) {
  87. ret = cipher->set_asn1_parameters(c, type);
  88. } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  89. switch (EVP_CIPHER_mode(cipher)) {
  90. case EVP_CIPH_WRAP_MODE:
  91. if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
  92. ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
  93. ret = 1;
  94. break;
  95. case EVP_CIPH_GCM_MODE:
  96. ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
  97. break;
  98. case EVP_CIPH_CCM_MODE:
  99. case EVP_CIPH_XTS_MODE:
  100. case EVP_CIPH_OCB_MODE:
  101. ret = -2;
  102. break;
  103. default:
  104. ret = EVP_CIPHER_set_asn1_iv(c, type);
  105. }
  106. } else if (cipher->prov != NULL) {
  107. OSSL_PARAM params[3], *p = params;
  108. unsigned char *der = NULL, *derp;
  109. /*
  110. * We make two passes, the first to get the appropriate buffer size,
  111. * and the second to get the actual value.
  112. */
  113. *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
  114. NULL, 0);
  115. *p = OSSL_PARAM_construct_end();
  116. if (!EVP_CIPHER_CTX_get_params(c, params))
  117. goto err;
  118. /* ... but, we should get a return size too! */
  119. if (OSSL_PARAM_modified(params)
  120. && params[0].return_size != 0
  121. && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
  122. params[0].data = der;
  123. params[0].data_size = params[0].return_size;
  124. OSSL_PARAM_set_all_unmodified(params);
  125. derp = der;
  126. if (EVP_CIPHER_CTX_get_params(c, params)
  127. && OSSL_PARAM_modified(params)
  128. && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
  129. params[0].return_size) != NULL) {
  130. ret = 1;
  131. }
  132. OPENSSL_free(der);
  133. }
  134. } else {
  135. ret = -2;
  136. }
  137. err:
  138. if (ret == -2)
  139. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  140. else if (ret <= 0)
  141. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  142. if (ret < -1)
  143. ret = -1;
  144. return ret;
  145. }
  146. int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  147. evp_cipher_aead_asn1_params *asn1_params)
  148. {
  149. int ret = -1; /* Assume the worst */
  150. const EVP_CIPHER *cipher = c->cipher;
  151. /*
  152. * For legacy implementations, we detect custom AlgorithmIdentifier
  153. * parameter handling by checking if there the function pointer
  154. * cipher->get_asn1_parameters is set. We know that this pointer
  155. * is NULL for provided implementations.
  156. *
  157. * Otherwise, for any implementation, we check the flag
  158. * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
  159. * default AI parameter creation.
  160. *
  161. * Otherwise, for provided implementations, we get the AI parameter
  162. * in DER encoded form from the implementation by requesting the
  163. * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
  164. *
  165. * If none of the above applies, this operation is unsupported.
  166. */
  167. if (cipher->get_asn1_parameters != NULL) {
  168. ret = cipher->get_asn1_parameters(c, type);
  169. } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
  170. switch (EVP_CIPHER_mode(cipher)) {
  171. case EVP_CIPH_WRAP_MODE:
  172. ret = 1;
  173. break;
  174. case EVP_CIPH_GCM_MODE:
  175. ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
  176. break;
  177. case EVP_CIPH_CCM_MODE:
  178. case EVP_CIPH_XTS_MODE:
  179. case EVP_CIPH_OCB_MODE:
  180. ret = -2;
  181. break;
  182. default:
  183. ret = EVP_CIPHER_get_asn1_iv(c, type);
  184. }
  185. } else if (cipher->prov != NULL) {
  186. OSSL_PARAM params[3], *p = params;
  187. unsigned char *der = NULL;
  188. int derl = -1;
  189. if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
  190. *p++ =
  191. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
  192. der, (size_t)derl);
  193. *p = OSSL_PARAM_construct_end();
  194. if (EVP_CIPHER_CTX_set_params(c, params))
  195. ret = 1;
  196. OPENSSL_free(der);
  197. }
  198. } else {
  199. ret = -2;
  200. }
  201. if (ret == -2)
  202. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  203. else if (ret <= 0)
  204. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  205. if (ret < -1)
  206. ret = -1;
  207. return ret;
  208. }
  209. int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  210. evp_cipher_aead_asn1_params *asn1_params)
  211. {
  212. int i = 0;
  213. long tl;
  214. unsigned char iv[EVP_MAX_IV_LENGTH];
  215. if (type == NULL || asn1_params == NULL)
  216. return 0;
  217. i = ossl_asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
  218. if (i <= 0)
  219. return -1;
  220. ossl_asn1_type_get_octetstring_int(type, &tl, iv, i);
  221. memcpy(asn1_params->iv, iv, i);
  222. asn1_params->iv_len = i;
  223. return i;
  224. }
  225. int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  226. evp_cipher_aead_asn1_params *asn1_params)
  227. {
  228. if (type == NULL || asn1_params == NULL)
  229. return 0;
  230. return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
  231. asn1_params->iv,
  232. asn1_params->iv_len);
  233. }
  234. #endif /* !defined(FIPS_MODULE) */
  235. /* Convert the various cipher NIDs and dummies to a proper OID NID */
  236. int EVP_CIPHER_type(const EVP_CIPHER *cipher)
  237. {
  238. int nid;
  239. nid = EVP_CIPHER_nid(cipher);
  240. switch (nid) {
  241. case NID_rc2_cbc:
  242. case NID_rc2_64_cbc:
  243. case NID_rc2_40_cbc:
  244. return NID_rc2_cbc;
  245. case NID_rc4:
  246. case NID_rc4_40:
  247. return NID_rc4;
  248. case NID_aes_128_cfb128:
  249. case NID_aes_128_cfb8:
  250. case NID_aes_128_cfb1:
  251. return NID_aes_128_cfb128;
  252. case NID_aes_192_cfb128:
  253. case NID_aes_192_cfb8:
  254. case NID_aes_192_cfb1:
  255. return NID_aes_192_cfb128;
  256. case NID_aes_256_cfb128:
  257. case NID_aes_256_cfb8:
  258. case NID_aes_256_cfb1:
  259. return NID_aes_256_cfb128;
  260. case NID_des_cfb64:
  261. case NID_des_cfb8:
  262. case NID_des_cfb1:
  263. return NID_des_cfb64;
  264. case NID_des_ede3_cfb64:
  265. case NID_des_ede3_cfb8:
  266. case NID_des_ede3_cfb1:
  267. return NID_des_cfb64;
  268. default:
  269. #ifdef FIPS_MODULE
  270. return NID_undef;
  271. #else
  272. {
  273. /* Check it has an OID and it is valid */
  274. ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
  275. if (OBJ_get0_data(otmp) == NULL)
  276. nid = NID_undef;
  277. ASN1_OBJECT_free(otmp);
  278. return nid;
  279. }
  280. #endif
  281. }
  282. }
  283. int evp_cipher_cache_constants(EVP_CIPHER *cipher)
  284. {
  285. int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0;
  286. size_t ivlen = 0;
  287. size_t blksz = 0;
  288. size_t keylen = 0;
  289. unsigned int mode = 0;
  290. OSSL_PARAM params[9];
  291. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
  292. params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
  293. params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
  294. params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
  295. params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
  296. params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
  297. &custom_iv);
  298. params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
  299. params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
  300. &multiblock);
  301. params[8] = OSSL_PARAM_construct_end();
  302. ok = evp_do_ciph_getparams(cipher, params);
  303. if (ok) {
  304. cipher->block_size = blksz;
  305. cipher->iv_len = ivlen;
  306. cipher->key_len = keylen;
  307. cipher->flags = mode;
  308. if (aead)
  309. cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
  310. if (custom_iv)
  311. cipher->flags |= EVP_CIPH_CUSTOM_IV;
  312. if (cts)
  313. cipher->flags |= EVP_CIPH_FLAG_CTS;
  314. if (multiblock)
  315. cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
  316. /* Provided implementations may have a custom cipher_cipher */
  317. if (cipher->prov != NULL && cipher->ccipher != NULL)
  318. cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
  319. }
  320. return ok;
  321. }
  322. int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
  323. {
  324. return cipher->block_size;
  325. }
  326. int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
  327. {
  328. return EVP_CIPHER_block_size(ctx->cipher);
  329. }
  330. int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
  331. {
  332. return e->ctx_size;
  333. }
  334. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  335. const unsigned char *in, unsigned int inl)
  336. {
  337. if (ctx->cipher->prov != NULL) {
  338. /*
  339. * If the provided implementation has a ccipher function, we use it,
  340. * and translate its return value like this: 0 => -1, 1 => outlen
  341. *
  342. * Otherwise, we call the cupdate function if in != NULL, or cfinal
  343. * if in == NULL. Regardless of which, we return what we got.
  344. */
  345. int ret = -1;
  346. size_t outl = 0;
  347. size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
  348. if (ctx->cipher->ccipher != NULL)
  349. ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
  350. inl + (blocksize == 1 ? 0 : blocksize),
  351. in, (size_t)inl)
  352. ? (int)outl : -1;
  353. else if (in != NULL)
  354. ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
  355. inl + (blocksize == 1 ? 0 : blocksize),
  356. in, (size_t)inl);
  357. else
  358. ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
  359. blocksize == 1 ? 0 : blocksize);
  360. return ret;
  361. }
  362. return ctx->cipher->do_cipher(ctx, out, in, inl);
  363. }
  364. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
  365. {
  366. return ctx->cipher;
  367. }
  368. int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
  369. {
  370. return ctx->encrypt;
  371. }
  372. unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
  373. {
  374. return cipher->flags;
  375. }
  376. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
  377. {
  378. return ctx->app_data;
  379. }
  380. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
  381. {
  382. ctx->app_data = data;
  383. }
  384. void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
  385. {
  386. return ctx->cipher_data;
  387. }
  388. void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
  389. {
  390. void *old_cipher_data;
  391. old_cipher_data = ctx->cipher_data;
  392. ctx->cipher_data = cipher_data;
  393. return old_cipher_data;
  394. }
  395. int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
  396. {
  397. return cipher->iv_len;
  398. }
  399. int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
  400. {
  401. int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
  402. size_t v = len;
  403. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  404. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
  405. rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  406. if (rv == EVP_CTRL_RET_UNSUPPORTED)
  407. goto legacy;
  408. return rv != 0 ? (int)v : -1;
  409. /* Code below to be removed when legacy support is dropped. */
  410. legacy:
  411. if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
  412. rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
  413. 0, &len);
  414. return (rv == 1) ? len : -1;
  415. }
  416. return len;
  417. }
  418. int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
  419. {
  420. int ret;
  421. size_t v = 0;
  422. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  423. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
  424. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  425. return ret == 1 ? (int)v : 0;
  426. }
  427. #ifndef OPENSSL_NO_DEPRECATED_3_0
  428. const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
  429. {
  430. int ok;
  431. const unsigned char *v = ctx->oiv;
  432. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  433. params[0] =
  434. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
  435. (void **)&v, sizeof(ctx->oiv));
  436. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  437. return ok != 0 ? v : NULL;
  438. }
  439. /*
  440. * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
  441. */
  442. const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
  443. {
  444. int ok;
  445. const unsigned char *v = ctx->iv;
  446. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  447. params[0] =
  448. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  449. (void **)&v, sizeof(ctx->iv));
  450. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  451. return ok != 0 ? v : NULL;
  452. }
  453. unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
  454. {
  455. int ok;
  456. unsigned char *v = ctx->iv;
  457. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  458. params[0] =
  459. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
  460. (void **)&v, sizeof(ctx->iv));
  461. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  462. return ok != 0 ? v : NULL;
  463. }
  464. #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
  465. int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  466. {
  467. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  468. params[0] =
  469. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
  470. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  471. }
  472. int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  473. {
  474. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  475. params[0] =
  476. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
  477. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  478. }
  479. unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
  480. {
  481. return ctx->buf;
  482. }
  483. int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
  484. {
  485. int ok;
  486. unsigned int v = (unsigned int)ctx->num;
  487. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  488. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
  489. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  490. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  491. }
  492. int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
  493. {
  494. int ok;
  495. unsigned int n = (unsigned int)num;
  496. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  497. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
  498. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
  499. if (ok != 0)
  500. ctx->num = (int)n;
  501. return ok != 0;
  502. }
  503. int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
  504. {
  505. return cipher->key_len;
  506. }
  507. int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
  508. {
  509. int ok;
  510. size_t v = ctx->key_len;
  511. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  512. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
  513. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  514. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  515. }
  516. int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
  517. {
  518. return cipher->nid;
  519. }
  520. int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
  521. {
  522. return ctx->cipher->nid;
  523. }
  524. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  525. {
  526. if (cipher->prov != NULL)
  527. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  528. return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
  529. }
  530. int EVP_CIPHER_number(const EVP_CIPHER *cipher)
  531. {
  532. return cipher->name_id;
  533. }
  534. const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
  535. {
  536. if (cipher->prov != NULL)
  537. return evp_first_name(cipher->prov, cipher->name_id);
  538. #ifndef FIPS_MODULE
  539. return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
  540. #else
  541. return NULL;
  542. #endif
  543. }
  544. const char *EVP_CIPHER_description(const EVP_CIPHER *cipher)
  545. {
  546. if (cipher->description != NULL)
  547. return cipher->description;
  548. #ifndef FIPS_MODULE
  549. return OBJ_nid2ln(EVP_CIPHER_nid(cipher));
  550. #else
  551. return NULL;
  552. #endif
  553. }
  554. int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  555. void (*fn)(const char *name, void *data),
  556. void *data)
  557. {
  558. if (cipher->prov != NULL)
  559. return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  560. return 1;
  561. }
  562. const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
  563. {
  564. return cipher->prov;
  565. }
  566. int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
  567. {
  568. return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
  569. }
  570. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  571. {
  572. if (md->prov != NULL)
  573. return evp_is_a(md->prov, md->name_id, NULL, name);
  574. return evp_is_a(NULL, 0, EVP_MD_name(md), name);
  575. }
  576. int EVP_MD_number(const EVP_MD *md)
  577. {
  578. return md->name_id;
  579. }
  580. const char *EVP_MD_description(const EVP_MD *md)
  581. {
  582. if (md->description != NULL)
  583. return md->description;
  584. #ifndef FIPS_MODULE
  585. return OBJ_nid2ln(EVP_MD_nid(md));
  586. #else
  587. return NULL;
  588. #endif
  589. }
  590. const char *EVP_MD_name(const EVP_MD *md)
  591. {
  592. if (md->prov != NULL)
  593. return evp_first_name(md->prov, md->name_id);
  594. #ifndef FIPS_MODULE
  595. return OBJ_nid2sn(EVP_MD_nid(md));
  596. #else
  597. return NULL;
  598. #endif
  599. }
  600. int EVP_MD_names_do_all(const EVP_MD *md,
  601. void (*fn)(const char *name, void *data),
  602. void *data)
  603. {
  604. if (md->prov != NULL)
  605. return evp_names_do_all(md->prov, md->name_id, fn, data);
  606. return 1;
  607. }
  608. const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
  609. {
  610. return md->prov;
  611. }
  612. int EVP_MD_type(const EVP_MD *md)
  613. {
  614. return md->type;
  615. }
  616. int EVP_MD_pkey_type(const EVP_MD *md)
  617. {
  618. return md->pkey_type;
  619. }
  620. int EVP_MD_block_size(const EVP_MD *md)
  621. {
  622. if (md == NULL) {
  623. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  624. return -1;
  625. }
  626. return md->block_size;
  627. }
  628. int EVP_MD_size(const EVP_MD *md)
  629. {
  630. if (md == NULL) {
  631. ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
  632. return -1;
  633. }
  634. return md->md_size;
  635. }
  636. unsigned long EVP_MD_flags(const EVP_MD *md)
  637. {
  638. return md->flags;
  639. }
  640. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  641. {
  642. EVP_MD *md = evp_md_new();
  643. if (md != NULL) {
  644. md->type = md_type;
  645. md->pkey_type = pkey_type;
  646. }
  647. return md;
  648. }
  649. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  650. {
  651. EVP_MD *to = NULL;
  652. /*
  653. * Non-legacy EVP_MDs can't be duplicated like this.
  654. * Use EVP_MD_up_ref() instead.
  655. */
  656. if (md->prov != NULL)
  657. return NULL;
  658. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  659. CRYPTO_RWLOCK *lock = to->lock;
  660. memcpy(to, md, sizeof(*to));
  661. to->lock = lock;
  662. }
  663. return to;
  664. }
  665. void EVP_MD_meth_free(EVP_MD *md)
  666. {
  667. EVP_MD_free(md);
  668. }
  669. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  670. {
  671. if (md->block_size != 0)
  672. return 0;
  673. md->block_size = blocksize;
  674. return 1;
  675. }
  676. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  677. {
  678. if (md->md_size != 0)
  679. return 0;
  680. md->md_size = resultsize;
  681. return 1;
  682. }
  683. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  684. {
  685. if (md->ctx_size != 0)
  686. return 0;
  687. md->ctx_size = datasize;
  688. return 1;
  689. }
  690. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  691. {
  692. if (md->flags != 0)
  693. return 0;
  694. md->flags = flags;
  695. return 1;
  696. }
  697. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  698. {
  699. if (md->init != NULL)
  700. return 0;
  701. md->init = init;
  702. return 1;
  703. }
  704. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
  705. const void *data,
  706. size_t count))
  707. {
  708. if (md->update != NULL)
  709. return 0;
  710. md->update = update;
  711. return 1;
  712. }
  713. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
  714. unsigned char *md))
  715. {
  716. if (md->final != NULL)
  717. return 0;
  718. md->final = final;
  719. return 1;
  720. }
  721. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
  722. const EVP_MD_CTX *from))
  723. {
  724. if (md->copy != NULL)
  725. return 0;
  726. md->copy = copy;
  727. return 1;
  728. }
  729. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  730. {
  731. if (md->cleanup != NULL)
  732. return 0;
  733. md->cleanup = cleanup;
  734. return 1;
  735. }
  736. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
  737. int p1, void *p2))
  738. {
  739. if (md->md_ctrl != NULL)
  740. return 0;
  741. md->md_ctrl = ctrl;
  742. return 1;
  743. }
  744. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  745. {
  746. return md->block_size;
  747. }
  748. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  749. {
  750. return md->md_size;
  751. }
  752. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  753. {
  754. return md->ctx_size;
  755. }
  756. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  757. {
  758. return md->flags;
  759. }
  760. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  761. {
  762. return md->init;
  763. }
  764. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  765. const void *data,
  766. size_t count)
  767. {
  768. return md->update;
  769. }
  770. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  771. unsigned char *md)
  772. {
  773. return md->final;
  774. }
  775. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  776. const EVP_MD_CTX *from)
  777. {
  778. return md->copy;
  779. }
  780. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  781. {
  782. return md->cleanup;
  783. }
  784. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  785. int p1, void *p2)
  786. {
  787. return md->md_ctrl;
  788. }
  789. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  790. {
  791. if (ctx == NULL)
  792. return NULL;
  793. return ctx->reqdigest;
  794. }
  795. EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
  796. {
  797. return ctx->pctx;
  798. }
  799. #if !defined(FIPS_MODULE)
  800. /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
  801. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  802. {
  803. /*
  804. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  805. * we have to deal with the cleanup job here.
  806. */
  807. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  808. EVP_PKEY_CTX_free(ctx->pctx);
  809. ctx->pctx = pctx;
  810. if (pctx != NULL) {
  811. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  812. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  813. } else {
  814. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  815. }
  816. }
  817. #endif /* !defined(FIPS_MODULE) */
  818. void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
  819. {
  820. return ctx->md_data;
  821. }
  822. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  823. const void *data, size_t count)
  824. {
  825. return ctx->update;
  826. }
  827. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  828. int (*update) (EVP_MD_CTX *ctx,
  829. const void *data, size_t count))
  830. {
  831. ctx->update = update;
  832. }
  833. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  834. {
  835. ctx->flags |= flags;
  836. }
  837. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  838. {
  839. ctx->flags &= ~flags;
  840. }
  841. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  842. {
  843. return (ctx->flags & flags);
  844. }
  845. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  846. {
  847. ctx->flags |= flags;
  848. }
  849. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  850. {
  851. ctx->flags &= ~flags;
  852. }
  853. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  854. {
  855. return (ctx->flags & flags);
  856. }
  857. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  858. {
  859. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  860. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  861. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  862. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  863. return -2;
  864. }
  865. if (name == NULL)
  866. return -1;
  867. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  868. (char *)name, 0);
  869. return EVP_PKEY_CTX_set_params(ctx, params);
  870. }
  871. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  872. {
  873. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  874. OSSL_PARAM *p = params;
  875. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  876. /* There is no legacy support for this */
  877. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  878. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  879. return -2;
  880. }
  881. if (name == NULL)
  882. return -1;
  883. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  884. name, namelen);
  885. if (!EVP_PKEY_CTX_get_params(ctx, params))
  886. return -1;
  887. return 1;
  888. }