1 /* $OpenBSD: evp_local.h,v 1.25 2024/08/29 16:58:19 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #ifndef HEADER_EVP_LOCAL_H 60 #define HEADER_EVP_LOCAL_H 61 62 __BEGIN_HIDDEN_DECLS 63 64 /* XXX - move these to evp.h after unlock. */ 65 #define EVP_CTRL_GET_IVLEN 0x25 66 #define EVP_CIPH_FLAG_CUSTOM_IV_LENGTH 0x400000 67 68 #define EVP_CTRL_AEAD_GET_IVLEN EVP_CTRL_GET_IVLEN 69 70 /* 71 * Don't free md_ctx->pctx in EVP_MD_CTX_cleanup(). Needed for ownership 72 * handling in EVP_MD_CTX_set_pkey_ctx(). 73 */ 74 #define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400 75 76 typedef int evp_sign_method(int type, const unsigned char *m, 77 unsigned int m_length, unsigned char *sigret, unsigned int *siglen, 78 void *key); 79 typedef int evp_verify_method(int type, const unsigned char *m, 80 unsigned int m_length, const unsigned char *sigbuf, unsigned int siglen, 81 void *key); 82 83 struct ecx_key_st { 84 int nid; 85 int key_len; 86 uint8_t *priv_key; 87 size_t priv_key_len; 88 uint8_t *pub_key; 89 size_t pub_key_len; 90 }; 91 92 struct evp_pkey_asn1_method_st { 93 const EVP_PKEY_ASN1_METHOD *base_method; 94 int pkey_id; 95 unsigned long pkey_flags; 96 97 char *pem_str; 98 char *info; 99 100 int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub); 101 int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk); 102 int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); 103 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent, 104 ASN1_PCTX *pctx); 105 106 int (*priv_decode)(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf); 107 int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk); 108 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent, 109 ASN1_PCTX *pctx); 110 111 int (*pkey_size)(const EVP_PKEY *pk); 112 int (*pkey_bits)(const EVP_PKEY *pk); 113 int (*pkey_security_bits)(const EVP_PKEY *pk); 114 115 int (*signature_info)(const X509_ALGOR *sig_alg, int *out_md_nid, 116 int *out_pkey_nid, int *out_security_bits, uint32_t *out_flags); 117 118 int (*param_decode)(EVP_PKEY *pkey, const unsigned char **pder, 119 int derlen); 120 int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder); 121 int (*param_missing)(const EVP_PKEY *pk); 122 int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from); 123 int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b); 124 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent, 125 ASN1_PCTX *pctx); 126 int (*sig_print)(BIO *out, const X509_ALGOR *sigalg, 127 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx); 128 129 void (*pkey_free)(EVP_PKEY *pkey); 130 int (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2); 131 132 /* Legacy functions for old PEM */ 133 134 int (*old_priv_decode)(EVP_PKEY *pkey, const unsigned char **pder, 135 int derlen); 136 int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder); 137 /* Custom ASN1 signature verification */ 138 int (*item_verify)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, 139 X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey); 140 int (*item_sign)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, 141 X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig); 142 143 int (*set_priv_key)(EVP_PKEY *pk, const unsigned char *private_key, 144 size_t len); 145 int (*set_pub_key)(EVP_PKEY *pk, const unsigned char *public_key, 146 size_t len); 147 int (*get_priv_key)(const EVP_PKEY *pk, unsigned char *out_private_key, 148 size_t *out_len); 149 int (*get_pub_key)(const EVP_PKEY *pk, unsigned char *out_public_key, 150 size_t *out_len); 151 } /* EVP_PKEY_ASN1_METHOD */; 152 153 /* Type needs to be a bit field 154 * Sub-type needs to be for variations on the method, as in, can it do 155 * arbitrary encryption.... */ 156 struct evp_pkey_st { 157 int type; 158 int references; 159 const EVP_PKEY_ASN1_METHOD *ameth; 160 union { 161 void *ptr; 162 #ifndef OPENSSL_NO_RSA 163 struct rsa_st *rsa; /* RSA */ 164 #endif 165 #ifndef OPENSSL_NO_DSA 166 struct dsa_st *dsa; /* DSA */ 167 #endif 168 #ifndef OPENSSL_NO_DH 169 struct dh_st *dh; /* DH */ 170 #endif 171 #ifndef OPENSSL_NO_EC 172 struct ec_key_st *ec; /* ECC */ 173 struct ecx_key_st *ecx; /* ECX */ 174 #endif 175 } pkey; 176 int save_parameters; 177 } /* EVP_PKEY */; 178 179 struct evp_md_st { 180 int type; 181 int pkey_type; 182 int md_size; 183 unsigned long flags; 184 int (*init)(EVP_MD_CTX *ctx); 185 int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count); 186 int (*final)(EVP_MD_CTX *ctx, unsigned char *md); 187 int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from); 188 int (*cleanup)(EVP_MD_CTX *ctx); 189 190 int block_size; 191 int ctx_size; /* how big does the ctx->md_data need to be */ 192 /* control function */ 193 int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2); 194 } /* EVP_MD */; 195 196 struct evp_md_ctx_st { 197 const EVP_MD *digest; 198 unsigned long flags; 199 void *md_data; 200 /* Public key context for sign/verify */ 201 EVP_PKEY_CTX *pctx; 202 /* Update function: usually copied from EVP_MD */ 203 int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count); 204 } /* EVP_MD_CTX */; 205 206 struct evp_cipher_st { 207 int nid; 208 int block_size; 209 int key_len; /* Default value for variable length ciphers */ 210 int iv_len; 211 unsigned long flags; /* Various flags */ 212 int (*init)(EVP_CIPHER_CTX *ctx, const unsigned char *key, 213 const unsigned char *iv, int enc); /* init key */ 214 int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out, 215 const unsigned char *in, size_t inl);/* encrypt/decrypt data */ 216 int (*cleanup)(EVP_CIPHER_CTX *); /* cleanup ctx */ 217 int ctx_size; /* how big ctx->cipher_data needs to be */ 218 int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Populate a ASN1_TYPE with parameters */ 219 int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Get parameters from a ASN1_TYPE */ 220 int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr); /* Miscellaneous operations */ 221 } /* EVP_CIPHER */; 222 223 struct evp_cipher_ctx_st { 224 const EVP_CIPHER *cipher; 225 int encrypt; /* encrypt or decrypt */ 226 int partial_len; /* number of bytes written to buf */ 227 228 unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */ 229 unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */ 230 unsigned char buf[EVP_MAX_BLOCK_LENGTH];/* saved partial block */ 231 int num; /* used by cfb/ofb/ctr mode */ 232 233 void *app_data; /* application stuff */ 234 int key_len; /* May change for variable length cipher */ 235 unsigned long flags; /* Various flags */ 236 void *cipher_data; /* per EVP data */ 237 int final_used; 238 unsigned char final[EVP_MAX_BLOCK_LENGTH];/* possible final block */ 239 } /* EVP_CIPHER_CTX */; 240 241 struct evp_Encode_Ctx_st { 242 243 int num; /* number saved in a partial encode/decode */ 244 int length; /* The length is either the output line length 245 * (in input bytes) or the shortest input line 246 * length that is ok. Once decoding begins, 247 * the length is adjusted up each time a longer 248 * line is decoded */ 249 unsigned char enc_data[80]; /* data to encode */ 250 int line_num; /* number read on current line */ 251 int expect_nl; 252 } /* EVP_ENCODE_CTX */; 253 254 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2)) 255 256 struct evp_pkey_ctx_st { 257 /* Method associated with this operation */ 258 const EVP_PKEY_METHOD *pmeth; 259 /* Key: may be NULL */ 260 EVP_PKEY *pkey; 261 /* Peer key for key agreement, may be NULL */ 262 EVP_PKEY *peerkey; 263 /* Actual operation */ 264 int operation; 265 /* Algorithm specific data */ 266 void *data; 267 /* Application specific data */ 268 void *app_data; 269 /* Keygen callback */ 270 EVP_PKEY_gen_cb *pkey_gencb; 271 /* implementation specific keygen data */ 272 int *keygen_info; 273 int keygen_info_count; 274 } /* EVP_PKEY_CTX */; 275 276 struct evp_pkey_method_st { 277 int pkey_id; 278 int flags; 279 280 int (*init)(EVP_PKEY_CTX *ctx); 281 int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src); 282 void (*cleanup)(EVP_PKEY_CTX *ctx); 283 284 int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); 285 286 int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey); 287 288 int (*sign_init)(EVP_PKEY_CTX *ctx); 289 int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 290 const unsigned char *tbs, size_t tbslen); 291 292 int (*verify_init)(EVP_PKEY_CTX *ctx); 293 int (*verify)(EVP_PKEY_CTX *ctx, 294 const unsigned char *sig, size_t siglen, 295 const unsigned char *tbs, size_t tbslen); 296 297 int (*verify_recover)(EVP_PKEY_CTX *ctx, 298 unsigned char *rout, size_t *routlen, 299 const unsigned char *sig, size_t siglen); 300 301 int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx); 302 int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 303 EVP_MD_CTX *mctx); 304 305 int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, 306 const unsigned char *in, size_t inlen); 307 308 int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, 309 const unsigned char *in, size_t inlen); 310 311 int (*derive_init)(EVP_PKEY_CTX *ctx); 312 int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); 313 314 int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2); 315 int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value); 316 317 int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, 318 const unsigned char *tbs, size_t tbslen); 319 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, 320 size_t siglen, const unsigned char *tbs, size_t tbslen); 321 } /* EVP_PKEY_METHOD */; 322 323 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx); 324 325 /* EVP_AEAD represents a specific AEAD algorithm. */ 326 struct evp_aead_st { 327 unsigned char key_len; 328 unsigned char nonce_len; 329 unsigned char overhead; 330 unsigned char max_tag_len; 331 332 int (*init)(struct evp_aead_ctx_st*, const unsigned char *key, 333 size_t key_len, size_t tag_len); 334 void (*cleanup)(struct evp_aead_ctx_st*); 335 336 int (*seal)(const struct evp_aead_ctx_st *ctx, unsigned char *out, 337 size_t *out_len, size_t max_out_len, const unsigned char *nonce, 338 size_t nonce_len, const unsigned char *in, size_t in_len, 339 const unsigned char *ad, size_t ad_len); 340 341 int (*open)(const struct evp_aead_ctx_st *ctx, unsigned char *out, 342 size_t *out_len, size_t max_out_len, const unsigned char *nonce, 343 size_t nonce_len, const unsigned char *in, size_t in_len, 344 const unsigned char *ad, size_t ad_len); 345 }; 346 347 /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key 348 * and message-independent IV. */ 349 struct evp_aead_ctx_st { 350 const EVP_AEAD *aead; 351 /* aead_state is an opaque pointer to the AEAD specific state. */ 352 void *aead_state; 353 }; 354 355 /* Legacy EVP_CIPHER methods used by CMS and its predecessors. */ 356 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type); 357 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type); 358 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type); 359 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type); 360 361 int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen, 362 ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de); 363 364 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str); 365 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex); 366 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name); 367 368 void EVP_CIPHER_CTX_legacy_clear(EVP_CIPHER_CTX *ctx); 369 void EVP_MD_CTX_legacy_clear(EVP_MD_CTX *ctx); 370 371 __END_HIDDEN_DECLS 372 373 #endif /* !HEADER_EVP_LOCAL_H */ 374