evp_lib.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * Copyright 1995-2020 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. EVPerr(0, EVP_R_UNSUPPORTED_CIPHER);
  140. else if (ret <= 0)
  141. EVPerr(0, 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. EVPerr(0, EVP_R_UNSUPPORTED_CIPHER);
  203. else if (ret <= 0)
  204. EVPerr(0, 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 = asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
  218. if (i <= 0)
  219. return -1;
  220. 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 asn1_type_set_octetstring_int(type, asn1_params->tag_len,
  231. asn1_params->iv, asn1_params->iv_len);
  232. }
  233. #endif /* !defined(FIPS_MODULE) */
  234. /* Convert the various cipher NIDs and dummies to a proper OID NID */
  235. int EVP_CIPHER_type(const EVP_CIPHER *ctx)
  236. {
  237. int nid;
  238. nid = EVP_CIPHER_nid(ctx);
  239. switch (nid) {
  240. case NID_rc2_cbc:
  241. case NID_rc2_64_cbc:
  242. case NID_rc2_40_cbc:
  243. return NID_rc2_cbc;
  244. case NID_rc4:
  245. case NID_rc4_40:
  246. return NID_rc4;
  247. case NID_aes_128_cfb128:
  248. case NID_aes_128_cfb8:
  249. case NID_aes_128_cfb1:
  250. return NID_aes_128_cfb128;
  251. case NID_aes_192_cfb128:
  252. case NID_aes_192_cfb8:
  253. case NID_aes_192_cfb1:
  254. return NID_aes_192_cfb128;
  255. case NID_aes_256_cfb128:
  256. case NID_aes_256_cfb8:
  257. case NID_aes_256_cfb1:
  258. return NID_aes_256_cfb128;
  259. case NID_des_cfb64:
  260. case NID_des_cfb8:
  261. case NID_des_cfb1:
  262. return NID_des_cfb64;
  263. case NID_des_ede3_cfb64:
  264. case NID_des_ede3_cfb8:
  265. case NID_des_ede3_cfb1:
  266. return NID_des_cfb64;
  267. default:
  268. #ifdef FIPS_MODULE
  269. return NID_undef;
  270. #else
  271. {
  272. /* Check it has an OID and it is valid */
  273. ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
  274. if (OBJ_get0_data(otmp) == NULL)
  275. nid = NID_undef;
  276. ASN1_OBJECT_free(otmp);
  277. return nid;
  278. }
  279. #endif
  280. }
  281. }
  282. int evp_cipher_cache_constants(EVP_CIPHER *cipher)
  283. {
  284. int ok;
  285. size_t ivlen = 0;
  286. size_t blksz = 0;
  287. size_t keylen = 0;
  288. unsigned int mode = 0;
  289. unsigned long flags = 0;
  290. OSSL_PARAM params[6];
  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_ulong(OSSL_CIPHER_PARAM_FLAGS, &flags);
  296. params[5] = OSSL_PARAM_construct_end();
  297. ok = evp_do_ciph_getparams(cipher, params);
  298. if (ok) {
  299. /* Provided implementations may have a custom cipher_cipher */
  300. if (cipher->prov != NULL && cipher->ccipher != NULL)
  301. flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
  302. cipher->block_size = blksz;
  303. cipher->iv_len = ivlen;
  304. cipher->key_len = keylen;
  305. cipher->flags = flags | mode;
  306. }
  307. return ok;
  308. }
  309. int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
  310. {
  311. return cipher->block_size;
  312. }
  313. int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
  314. {
  315. return EVP_CIPHER_block_size(ctx->cipher);
  316. }
  317. int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
  318. {
  319. return e->ctx_size;
  320. }
  321. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  322. const unsigned char *in, unsigned int inl)
  323. {
  324. if (ctx->cipher->prov != NULL) {
  325. /*
  326. * If the provided implementation has a ccipher function, we use it,
  327. * and translate its return value like this: 0 => -1, 1 => outlen
  328. *
  329. * Otherwise, we call the cupdate function if in != NULL, or cfinal
  330. * if in == NULL. Regardless of which, we return what we got.
  331. */
  332. int ret = -1;
  333. size_t outl = 0;
  334. size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
  335. if (ctx->cipher->ccipher != NULL)
  336. ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
  337. inl + (blocksize == 1 ? 0 : blocksize),
  338. in, (size_t)inl)
  339. ? (int)outl : -1;
  340. else if (in != NULL)
  341. ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
  342. inl + (blocksize == 1 ? 0 : blocksize),
  343. in, (size_t)inl);
  344. else
  345. ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
  346. blocksize == 1 ? 0 : blocksize);
  347. return ret;
  348. }
  349. return ctx->cipher->do_cipher(ctx, out, in, inl);
  350. }
  351. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
  352. {
  353. return ctx->cipher;
  354. }
  355. int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
  356. {
  357. return ctx->encrypt;
  358. }
  359. unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
  360. {
  361. return cipher->flags;
  362. }
  363. void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
  364. {
  365. return ctx->app_data;
  366. }
  367. void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
  368. {
  369. ctx->app_data = data;
  370. }
  371. void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
  372. {
  373. return ctx->cipher_data;
  374. }
  375. void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
  376. {
  377. void *old_cipher_data;
  378. old_cipher_data = ctx->cipher_data;
  379. ctx->cipher_data = cipher_data;
  380. return old_cipher_data;
  381. }
  382. int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
  383. {
  384. return cipher->iv_len;
  385. }
  386. int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
  387. {
  388. int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
  389. size_t v = len;
  390. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  391. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
  392. rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  393. if (rv == EVP_CTRL_RET_UNSUPPORTED)
  394. goto legacy;
  395. return rv != 0 ? (int)v : -1;
  396. /* TODO (3.0) Remove legacy support */
  397. legacy:
  398. if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
  399. rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
  400. 0, &len);
  401. return (rv == 1) ? len : -1;
  402. }
  403. return len;
  404. }
  405. int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
  406. {
  407. int ret;
  408. size_t v = 0;
  409. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  410. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
  411. ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  412. return ret == 1 ? (int)v : 0;
  413. }
  414. #ifndef OPENSSL_NO_DEPRECATED_3_0
  415. const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
  416. {
  417. int ok;
  418. const unsigned char *v = ctx->oiv;
  419. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  420. params[0] =
  421. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
  422. (void **)&v, sizeof(ctx->oiv));
  423. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  424. return ok != 0 ? v : NULL;
  425. }
  426. /*
  427. * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
  428. */
  429. const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
  430. {
  431. int ok;
  432. const unsigned char *v = ctx->iv;
  433. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  434. params[0] =
  435. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE, (void **)&v,
  436. sizeof(ctx->iv));
  437. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  438. return ok != 0 ? v : NULL;
  439. }
  440. unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
  441. {
  442. int ok;
  443. unsigned char *v = ctx->iv;
  444. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  445. params[0] =
  446. OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE, (void **)&v,
  447. sizeof(ctx->iv));
  448. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  449. return ok != 0 ? v : NULL;
  450. }
  451. #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
  452. int EVP_CIPHER_CTX_get_iv_state(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  453. {
  454. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  455. params[0] =
  456. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV_STATE, buf, len);
  457. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  458. }
  459. int EVP_CIPHER_CTX_get_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
  460. {
  461. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  462. params[0] =
  463. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
  464. return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  465. }
  466. unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
  467. {
  468. return ctx->buf;
  469. }
  470. int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
  471. {
  472. int ok;
  473. unsigned int v = (unsigned int)ctx->num;
  474. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  475. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
  476. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  477. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  478. }
  479. int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
  480. {
  481. int ok;
  482. unsigned int n = (unsigned int)num;
  483. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  484. params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
  485. ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
  486. if (ok != 0)
  487. ctx->num = (int)n;
  488. return ok != 0;
  489. }
  490. int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
  491. {
  492. return cipher->key_len;
  493. }
  494. int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
  495. {
  496. int ok;
  497. size_t v = ctx->key_len;
  498. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  499. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
  500. ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
  501. return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
  502. }
  503. int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
  504. {
  505. return cipher->nid;
  506. }
  507. int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
  508. {
  509. return ctx->cipher->nid;
  510. }
  511. int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
  512. {
  513. if (cipher->prov != NULL)
  514. return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
  515. return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
  516. }
  517. int EVP_CIPHER_number(const EVP_CIPHER *cipher)
  518. {
  519. return cipher->name_id;
  520. }
  521. const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
  522. {
  523. if (cipher->prov != NULL)
  524. return evp_first_name(cipher->prov, cipher->name_id);
  525. #ifndef FIPS_MODULE
  526. return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
  527. #else
  528. return NULL;
  529. #endif
  530. }
  531. void EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
  532. void (*fn)(const char *name, void *data),
  533. void *data)
  534. {
  535. if (cipher->prov != NULL)
  536. evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
  537. }
  538. const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
  539. {
  540. return cipher->prov;
  541. }
  542. int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
  543. {
  544. return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
  545. }
  546. int EVP_MD_is_a(const EVP_MD *md, const char *name)
  547. {
  548. if (md->prov != NULL)
  549. return evp_is_a(md->prov, md->name_id, NULL, name);
  550. return evp_is_a(NULL, 0, EVP_MD_name(md), name);
  551. }
  552. int EVP_MD_number(const EVP_MD *md)
  553. {
  554. return md->name_id;
  555. }
  556. const char *EVP_MD_name(const EVP_MD *md)
  557. {
  558. if (md->prov != NULL)
  559. return evp_first_name(md->prov, md->name_id);
  560. #ifndef FIPS_MODULE
  561. return OBJ_nid2sn(EVP_MD_nid(md));
  562. #else
  563. return NULL;
  564. #endif
  565. }
  566. void EVP_MD_names_do_all(const EVP_MD *md,
  567. void (*fn)(const char *name, void *data),
  568. void *data)
  569. {
  570. if (md->prov != NULL)
  571. evp_names_do_all(md->prov, md->name_id, fn, data);
  572. }
  573. const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
  574. {
  575. return md->prov;
  576. }
  577. int EVP_MD_block_size(const EVP_MD *md)
  578. {
  579. int ok;
  580. size_t v;
  581. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  582. if (md == NULL) {
  583. EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
  584. return -1;
  585. }
  586. v = md->block_size;
  587. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
  588. ok = evp_do_md_getparams(md, params);
  589. return ok != 0 ? (int)v : -1;
  590. }
  591. int EVP_MD_type(const EVP_MD *md)
  592. {
  593. return md->type;
  594. }
  595. int EVP_MD_pkey_type(const EVP_MD *md)
  596. {
  597. return md->pkey_type;
  598. }
  599. int EVP_MD_size(const EVP_MD *md)
  600. {
  601. int ok;
  602. size_t v;
  603. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  604. if (md == NULL) {
  605. EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
  606. return -1;
  607. }
  608. v = md->md_size;
  609. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &v);
  610. ok = evp_do_md_getparams(md, params);
  611. return ok != 0 ? (int)v : -1;
  612. }
  613. unsigned long EVP_MD_flags(const EVP_MD *md)
  614. {
  615. int ok;
  616. unsigned long v = md->flags;
  617. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  618. params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
  619. ok = evp_do_md_getparams(md, params);
  620. return ok != 0 ? v : 0;
  621. }
  622. EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
  623. {
  624. EVP_MD *md = evp_md_new();
  625. if (md != NULL) {
  626. md->type = md_type;
  627. md->pkey_type = pkey_type;
  628. }
  629. return md;
  630. }
  631. EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
  632. {
  633. EVP_MD *to = NULL;
  634. /*
  635. * Non-legacy EVP_MDs can't be duplicated like this.
  636. * Use EVP_MD_up_ref() instead.
  637. */
  638. if (md->prov != NULL)
  639. return NULL;
  640. if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
  641. CRYPTO_RWLOCK *lock = to->lock;
  642. memcpy(to, md, sizeof(*to));
  643. to->lock = lock;
  644. }
  645. return to;
  646. }
  647. void EVP_MD_meth_free(EVP_MD *md)
  648. {
  649. EVP_MD_free(md);
  650. }
  651. int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
  652. {
  653. if (md->block_size != 0)
  654. return 0;
  655. md->block_size = blocksize;
  656. return 1;
  657. }
  658. int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
  659. {
  660. if (md->md_size != 0)
  661. return 0;
  662. md->md_size = resultsize;
  663. return 1;
  664. }
  665. int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
  666. {
  667. if (md->ctx_size != 0)
  668. return 0;
  669. md->ctx_size = datasize;
  670. return 1;
  671. }
  672. int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
  673. {
  674. if (md->flags != 0)
  675. return 0;
  676. md->flags = flags;
  677. return 1;
  678. }
  679. int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
  680. {
  681. if (md->init != NULL)
  682. return 0;
  683. md->init = init;
  684. return 1;
  685. }
  686. int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
  687. const void *data,
  688. size_t count))
  689. {
  690. if (md->update != NULL)
  691. return 0;
  692. md->update = update;
  693. return 1;
  694. }
  695. int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
  696. unsigned char *md))
  697. {
  698. if (md->final != NULL)
  699. return 0;
  700. md->final = final;
  701. return 1;
  702. }
  703. int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
  704. const EVP_MD_CTX *from))
  705. {
  706. if (md->copy != NULL)
  707. return 0;
  708. md->copy = copy;
  709. return 1;
  710. }
  711. int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
  712. {
  713. if (md->cleanup != NULL)
  714. return 0;
  715. md->cleanup = cleanup;
  716. return 1;
  717. }
  718. int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
  719. int p1, void *p2))
  720. {
  721. if (md->md_ctrl != NULL)
  722. return 0;
  723. md->md_ctrl = ctrl;
  724. return 1;
  725. }
  726. int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
  727. {
  728. return md->block_size;
  729. }
  730. int EVP_MD_meth_get_result_size(const EVP_MD *md)
  731. {
  732. return md->md_size;
  733. }
  734. int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
  735. {
  736. return md->ctx_size;
  737. }
  738. unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
  739. {
  740. return md->flags;
  741. }
  742. int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
  743. {
  744. return md->init;
  745. }
  746. int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
  747. const void *data,
  748. size_t count)
  749. {
  750. return md->update;
  751. }
  752. int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
  753. unsigned char *md)
  754. {
  755. return md->final;
  756. }
  757. int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
  758. const EVP_MD_CTX *from)
  759. {
  760. return md->copy;
  761. }
  762. int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
  763. {
  764. return md->cleanup;
  765. }
  766. int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
  767. int p1, void *p2)
  768. {
  769. return md->md_ctrl;
  770. }
  771. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
  772. {
  773. if (ctx == NULL)
  774. return NULL;
  775. return ctx->reqdigest;
  776. }
  777. EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
  778. {
  779. return ctx->pctx;
  780. }
  781. #if !defined(FIPS_MODULE)
  782. /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
  783. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  784. {
  785. /*
  786. * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
  787. * we have to deal with the cleanup job here.
  788. */
  789. if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
  790. EVP_PKEY_CTX_free(ctx->pctx);
  791. ctx->pctx = pctx;
  792. if (pctx != NULL) {
  793. /* make sure pctx is not freed when destroying EVP_MD_CTX */
  794. EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  795. } else {
  796. EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
  797. }
  798. }
  799. #endif /* !defined(FIPS_MODULE) */
  800. void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
  801. {
  802. return ctx->md_data;
  803. }
  804. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  805. const void *data, size_t count)
  806. {
  807. return ctx->update;
  808. }
  809. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  810. int (*update) (EVP_MD_CTX *ctx,
  811. const void *data, size_t count))
  812. {
  813. ctx->update = update;
  814. }
  815. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
  816. {
  817. ctx->flags |= flags;
  818. }
  819. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
  820. {
  821. ctx->flags &= ~flags;
  822. }
  823. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
  824. {
  825. return (ctx->flags & flags);
  826. }
  827. void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
  828. {
  829. ctx->flags |= flags;
  830. }
  831. void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
  832. {
  833. ctx->flags &= ~flags;
  834. }
  835. int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
  836. {
  837. return (ctx->flags & flags);
  838. }
  839. int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
  840. void *ctx, int cmd, const char *value)
  841. {
  842. size_t len;
  843. len = strlen(value);
  844. if (len > INT_MAX)
  845. return -1;
  846. return cb(ctx, cmd, (void *)value, len);
  847. }
  848. int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
  849. void *ctx, int cmd, const char *hex)
  850. {
  851. unsigned char *bin;
  852. long binlen;
  853. int rv = -1;
  854. bin = OPENSSL_hexstr2buf(hex, &binlen);
  855. if (bin == NULL)
  856. return 0;
  857. if (binlen <= INT_MAX)
  858. rv = cb(ctx, cmd, bin, binlen);
  859. OPENSSL_free(bin);
  860. return rv;
  861. }
  862. int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
  863. {
  864. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  865. OSSL_PARAM *p = params;
  866. if (ctx == NULL) {
  867. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  868. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  869. return -2;
  870. }
  871. if (!EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  872. #ifndef FIPS_MODULE
  873. int nid;
  874. /* Could be a legacy key, try and convert to a ctrl */
  875. if (ctx->pmeth != NULL && (nid = OBJ_txt2nid(name)) != NID_undef) {
  876. # ifndef OPENSSL_NO_DH
  877. if (ctx->pmeth->pkey_id == EVP_PKEY_DH)
  878. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH,
  879. EVP_PKEY_OP_PARAMGEN
  880. | EVP_PKEY_OP_KEYGEN,
  881. EVP_PKEY_CTRL_DH_NID, nid, NULL);
  882. # endif
  883. # ifndef OPENSSL_NO_EC
  884. if (ctx->pmeth->pkey_id == EVP_PKEY_EC)
  885. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
  886. EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
  887. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID,
  888. nid, NULL);
  889. # endif
  890. }
  891. #endif
  892. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  893. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  894. return -2;
  895. }
  896. if (name == NULL)
  897. return -1;
  898. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  899. (char *)name, 0);
  900. return EVP_PKEY_CTX_set_params(ctx, params);
  901. }
  902. int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
  903. {
  904. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  905. OSSL_PARAM *p = params;
  906. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  907. /* There is no legacy support for this */
  908. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  909. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  910. return -2;
  911. }
  912. if (name == NULL)
  913. return -1;
  914. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  915. name, namelen);
  916. if (!EVP_PKEY_CTX_get_params(ctx, params))
  917. return -1;
  918. return 1;
  919. }