evp_local.h 13 KB

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