evp_local.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core_dispatch.h>
  10. #include "internal/refcount.h"
  11. #define EVP_CTRL_RET_UNSUPPORTED -1
  12. struct evp_md_ctx_st {
  13. const EVP_MD *reqdigest; /* The original requested digest */
  14. const EVP_MD *digest;
  15. ENGINE *engine; /* functional reference if 'digest' is
  16. * ENGINE-provided */
  17. unsigned long flags;
  18. void *md_data;
  19. /* Public key context for sign/verify */
  20. EVP_PKEY_CTX *pctx;
  21. /* Update function: usually copied from EVP_MD */
  22. int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
  23. /*
  24. * Opaque ctx returned from a providers digest algorithm implementation
  25. * OSSL_FUNC_digest_newctx()
  26. */
  27. void *algctx;
  28. EVP_MD *fetched_digest;
  29. } /* EVP_MD_CTX */ ;
  30. struct evp_cipher_ctx_st {
  31. const EVP_CIPHER *cipher;
  32. ENGINE *engine; /* functional reference if 'cipher' is
  33. * ENGINE-provided */
  34. int encrypt; /* encrypt or decrypt */
  35. int buf_len; /* number we have left */
  36. unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
  37. unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
  38. unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
  39. int num; /* used by cfb/ofb/ctr mode */
  40. /* FIXME: Should this even exist? It appears unused */
  41. void *app_data; /* application stuff */
  42. int key_len; /* May change for variable length cipher */
  43. int iv_len; /* IV length */
  44. unsigned long flags; /* Various flags */
  45. void *cipher_data; /* per EVP data */
  46. int final_used;
  47. int block_mask;
  48. unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
  49. /*
  50. * Opaque ctx returned from a providers cipher algorithm implementation
  51. * OSSL_FUNC_cipher_newctx()
  52. */
  53. void *algctx;
  54. EVP_CIPHER *fetched_cipher;
  55. } /* EVP_CIPHER_CTX */ ;
  56. struct evp_mac_ctx_st {
  57. EVP_MAC *meth; /* Method structure */
  58. /*
  59. * Opaque ctx returned from a providers MAC algorithm implementation
  60. * OSSL_FUNC_mac_newctx()
  61. */
  62. void *algctx;
  63. } /* EVP_MAC_CTX */;
  64. struct evp_kdf_ctx_st {
  65. EVP_KDF *meth; /* Method structure */
  66. /*
  67. * Opaque ctx returned from a providers KDF algorithm implementation
  68. * OSSL_FUNC_kdf_newctx()
  69. */
  70. void *algctx;
  71. } /* EVP_KDF_CTX */ ;
  72. struct evp_rand_ctx_st {
  73. EVP_RAND *meth; /* Method structure */
  74. /*
  75. * Opaque ctx returned from a providers rand algorithm implementation
  76. * OSSL_FUNC_rand_newctx()
  77. */
  78. void *algctx;
  79. EVP_RAND_CTX *parent; /* Parent EVP_RAND or NULL if none */
  80. CRYPTO_REF_COUNT refcnt; /* Context reference count */
  81. CRYPTO_RWLOCK *refcnt_lock;
  82. } /* EVP_RAND_CTX */ ;
  83. struct evp_keymgmt_st {
  84. int id; /* libcrypto internal */
  85. int name_id;
  86. char *type_name;
  87. const char *description;
  88. OSSL_PROVIDER *prov;
  89. CRYPTO_REF_COUNT refcnt;
  90. CRYPTO_RWLOCK *lock;
  91. /* Constructor(s), destructor, information */
  92. OSSL_FUNC_keymgmt_new_fn *new;
  93. OSSL_FUNC_keymgmt_free_fn *free;
  94. OSSL_FUNC_keymgmt_get_params_fn *get_params;
  95. OSSL_FUNC_keymgmt_gettable_params_fn *gettable_params;
  96. OSSL_FUNC_keymgmt_set_params_fn *set_params;
  97. OSSL_FUNC_keymgmt_settable_params_fn *settable_params;
  98. /* Generation, a complex constructor */
  99. OSSL_FUNC_keymgmt_gen_init_fn *gen_init;
  100. OSSL_FUNC_keymgmt_gen_set_template_fn *gen_set_template;
  101. OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params;
  102. OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params;
  103. OSSL_FUNC_keymgmt_gen_fn *gen;
  104. OSSL_FUNC_keymgmt_gen_cleanup_fn *gen_cleanup;
  105. OSSL_FUNC_keymgmt_load_fn *load;
  106. /* Key object checking */
  107. OSSL_FUNC_keymgmt_query_operation_name_fn *query_operation_name;
  108. OSSL_FUNC_keymgmt_has_fn *has;
  109. OSSL_FUNC_keymgmt_validate_fn *validate;
  110. OSSL_FUNC_keymgmt_match_fn *match;
  111. /* Import and export routines */
  112. OSSL_FUNC_keymgmt_import_fn *import;
  113. OSSL_FUNC_keymgmt_import_types_fn *import_types;
  114. OSSL_FUNC_keymgmt_import_types_ex_fn *import_types_ex;
  115. OSSL_FUNC_keymgmt_export_fn *export;
  116. OSSL_FUNC_keymgmt_export_types_fn *export_types;
  117. OSSL_FUNC_keymgmt_export_types_ex_fn *export_types_ex;
  118. OSSL_FUNC_keymgmt_dup_fn *dup;
  119. } /* EVP_KEYMGMT */ ;
  120. struct evp_keyexch_st {
  121. int name_id;
  122. char *type_name;
  123. const char *description;
  124. OSSL_PROVIDER *prov;
  125. CRYPTO_REF_COUNT refcnt;
  126. CRYPTO_RWLOCK *lock;
  127. OSSL_FUNC_keyexch_newctx_fn *newctx;
  128. OSSL_FUNC_keyexch_init_fn *init;
  129. OSSL_FUNC_keyexch_set_peer_fn *set_peer;
  130. OSSL_FUNC_keyexch_derive_fn *derive;
  131. OSSL_FUNC_keyexch_freectx_fn *freectx;
  132. OSSL_FUNC_keyexch_dupctx_fn *dupctx;
  133. OSSL_FUNC_keyexch_set_ctx_params_fn *set_ctx_params;
  134. OSSL_FUNC_keyexch_settable_ctx_params_fn *settable_ctx_params;
  135. OSSL_FUNC_keyexch_get_ctx_params_fn *get_ctx_params;
  136. OSSL_FUNC_keyexch_gettable_ctx_params_fn *gettable_ctx_params;
  137. } /* EVP_KEYEXCH */;
  138. struct evp_signature_st {
  139. int name_id;
  140. char *type_name;
  141. const char *description;
  142. OSSL_PROVIDER *prov;
  143. CRYPTO_REF_COUNT refcnt;
  144. CRYPTO_RWLOCK *lock;
  145. OSSL_FUNC_signature_newctx_fn *newctx;
  146. OSSL_FUNC_signature_sign_init_fn *sign_init;
  147. OSSL_FUNC_signature_sign_fn *sign;
  148. OSSL_FUNC_signature_verify_init_fn *verify_init;
  149. OSSL_FUNC_signature_verify_fn *verify;
  150. OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
  151. OSSL_FUNC_signature_verify_recover_fn *verify_recover;
  152. OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
  153. OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
  154. OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
  155. OSSL_FUNC_signature_digest_sign_fn *digest_sign;
  156. OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
  157. OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
  158. OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
  159. OSSL_FUNC_signature_digest_verify_fn *digest_verify;
  160. OSSL_FUNC_signature_freectx_fn *freectx;
  161. OSSL_FUNC_signature_dupctx_fn *dupctx;
  162. OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
  163. OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
  164. OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
  165. OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
  166. OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
  167. OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
  168. OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
  169. OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
  170. } /* EVP_SIGNATURE */;
  171. struct evp_asym_cipher_st {
  172. int name_id;
  173. char *type_name;
  174. const char *description;
  175. OSSL_PROVIDER *prov;
  176. CRYPTO_REF_COUNT refcnt;
  177. CRYPTO_RWLOCK *lock;
  178. OSSL_FUNC_asym_cipher_newctx_fn *newctx;
  179. OSSL_FUNC_asym_cipher_encrypt_init_fn *encrypt_init;
  180. OSSL_FUNC_asym_cipher_encrypt_fn *encrypt;
  181. OSSL_FUNC_asym_cipher_decrypt_init_fn *decrypt_init;
  182. OSSL_FUNC_asym_cipher_decrypt_fn *decrypt;
  183. OSSL_FUNC_asym_cipher_freectx_fn *freectx;
  184. OSSL_FUNC_asym_cipher_dupctx_fn *dupctx;
  185. OSSL_FUNC_asym_cipher_get_ctx_params_fn *get_ctx_params;
  186. OSSL_FUNC_asym_cipher_gettable_ctx_params_fn *gettable_ctx_params;
  187. OSSL_FUNC_asym_cipher_set_ctx_params_fn *set_ctx_params;
  188. OSSL_FUNC_asym_cipher_settable_ctx_params_fn *settable_ctx_params;
  189. } /* EVP_ASYM_CIPHER */;
  190. struct evp_kem_st {
  191. int name_id;
  192. char *type_name;
  193. const char *description;
  194. OSSL_PROVIDER *prov;
  195. CRYPTO_REF_COUNT refcnt;
  196. CRYPTO_RWLOCK *lock;
  197. OSSL_FUNC_kem_newctx_fn *newctx;
  198. OSSL_FUNC_kem_encapsulate_init_fn *encapsulate_init;
  199. OSSL_FUNC_kem_encapsulate_fn *encapsulate;
  200. OSSL_FUNC_kem_decapsulate_init_fn *decapsulate_init;
  201. OSSL_FUNC_kem_decapsulate_fn *decapsulate;
  202. OSSL_FUNC_kem_freectx_fn *freectx;
  203. OSSL_FUNC_kem_dupctx_fn *dupctx;
  204. OSSL_FUNC_kem_get_ctx_params_fn *get_ctx_params;
  205. OSSL_FUNC_kem_gettable_ctx_params_fn *gettable_ctx_params;
  206. OSSL_FUNC_kem_set_ctx_params_fn *set_ctx_params;
  207. OSSL_FUNC_kem_settable_ctx_params_fn *settable_ctx_params;
  208. OSSL_FUNC_kem_auth_encapsulate_init_fn *auth_encapsulate_init;
  209. OSSL_FUNC_kem_auth_decapsulate_init_fn *auth_decapsulate_init;
  210. } /* EVP_KEM */;
  211. int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  212. int passlen, ASN1_TYPE *param,
  213. const EVP_CIPHER *c, const EVP_MD *md,
  214. int en_de);
  215. int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
  216. int passlen, ASN1_TYPE *param,
  217. const EVP_CIPHER *c, const EVP_MD *md,
  218. int en_de, OSSL_LIB_CTX *libctx, const char *propq);
  219. struct evp_Encode_Ctx_st {
  220. /* number saved in a partial encode/decode */
  221. int num;
  222. /*
  223. * The length is either the output line length (in input bytes) or the
  224. * shortest input line length that is ok. Once decoding begins, the
  225. * length is adjusted up each time a longer line is decoded
  226. */
  227. int length;
  228. /* data to encode */
  229. unsigned char enc_data[80];
  230. /* number read on current line */
  231. int line_num;
  232. unsigned int flags;
  233. };
  234. typedef struct evp_pbe_st EVP_PBE_CTL;
  235. DEFINE_STACK_OF(EVP_PBE_CTL)
  236. int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
  237. #include <openssl/types.h>
  238. #include <openssl/core.h>
  239. void *evp_generic_fetch(OSSL_LIB_CTX *ctx, int operation_id,
  240. const char *name, const char *properties,
  241. void *(*new_method)(int name_id,
  242. const OSSL_ALGORITHM *algodef,
  243. OSSL_PROVIDER *prov),
  244. int (*up_ref_method)(void *),
  245. void (*free_method)(void *));
  246. void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
  247. const char *name, const char *properties,
  248. void *(*new_method)(int name_id,
  249. const OSSL_ALGORITHM *algodef,
  250. OSSL_PROVIDER *prov),
  251. int (*up_ref_method)(void *),
  252. void (*free_method)(void *));
  253. void evp_generic_do_all_prefetched(OSSL_LIB_CTX *libctx, int operation_id,
  254. void (*user_fn)(void *method, void *arg),
  255. void *user_arg);
  256. void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
  257. void (*user_fn)(void *method, void *arg),
  258. void *user_arg,
  259. void *(*new_method)(int name_id,
  260. const OSSL_ALGORITHM *algodef,
  261. OSSL_PROVIDER *prov),
  262. int (*up_ref_method)(void *),
  263. void (*free_method)(void *));
  264. /* Internal fetchers for method types that are to be combined with others */
  265. EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OSSL_LIB_CTX *ctx, int name_id,
  266. const char *properties);
  267. EVP_SIGNATURE *evp_signature_fetch_from_prov(OSSL_PROVIDER *prov,
  268. const char *name,
  269. const char *properties);
  270. EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
  271. const char *name,
  272. const char *properties);
  273. EVP_KEYEXCH *evp_keyexch_fetch_from_prov(OSSL_PROVIDER *prov,
  274. const char *name,
  275. const char *properties);
  276. EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov,
  277. const char *name,
  278. const char *properties);
  279. /* Internal structure constructors for fetched methods */
  280. EVP_MD *evp_md_new(void);
  281. EVP_CIPHER *evp_cipher_new(void);
  282. int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  283. evp_cipher_aead_asn1_params *asn1_params);
  284. int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
  285. evp_cipher_aead_asn1_params *asn1_params);
  286. /* Helper functions to avoid duplicating code */
  287. /*
  288. * These methods implement different ways to pass a params array to the
  289. * provider. They will return one of these values:
  290. *
  291. * -2 if the method doesn't come from a provider
  292. * (evp_do_param will return this to the called)
  293. * -1 if the provider doesn't offer the desired function
  294. * (evp_do_param will raise an error and return 0)
  295. * or the return value from the desired function
  296. * (evp_do_param will return it to the caller)
  297. */
  298. int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
  299. int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
  300. OSSL_PARAM params[]);
  301. int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
  302. OSSL_PARAM params[]);
  303. int evp_do_md_getparams(const EVP_MD *md, OSSL_PARAM params[]);
  304. int evp_do_md_ctx_getparams(const EVP_MD *md, void *provctx,
  305. OSSL_PARAM params[]);
  306. int evp_do_md_ctx_setparams(const EVP_MD *md, void *provctx,
  307. OSSL_PARAM params[]);
  308. OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz);
  309. #define M_check_autoarg(ctx, arg, arglen, err) \
  310. if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) { \
  311. size_t pksize = (size_t)EVP_PKEY_get_size(ctx->pkey); \
  312. \
  313. if (pksize == 0) { \
  314. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); /*ckerr_ignore*/ \
  315. return 0; \
  316. } \
  317. if (arg == NULL) { \
  318. *arglen = pksize; \
  319. return 1; \
  320. } \
  321. if (*arglen < pksize) { \
  322. ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/ \
  323. return 0; \
  324. } \
  325. }
  326. void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx);
  327. void evp_cipher_free_int(EVP_CIPHER *md);
  328. void evp_md_free_int(EVP_MD *md);
  329. /* OSSL_PROVIDER * is only used to get the library context */
  330. int evp_is_a(OSSL_PROVIDER *prov, int number,
  331. const char *legacy_name, const char *name);
  332. int evp_names_do_all(OSSL_PROVIDER *prov, int number,
  333. void (*fn)(const char *name, void *data),
  334. void *data);
  335. int evp_cipher_cache_constants(EVP_CIPHER *cipher);