xref: /openbsd/lib/libcrypto/evp/evp_local.h (revision 261a77c2)
1 /* $OpenBSD: evp_local.h,v 1.16 2024/02/18 15:43:21 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 (*param_decode)(EVP_PKEY *pkey, const unsigned char **pder,
116 	    int derlen);
117 	int (*param_encode)(const EVP_PKEY *pkey, unsigned char **pder);
118 	int (*param_missing)(const EVP_PKEY *pk);
119 	int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
120 	int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
121 	int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
122 	    ASN1_PCTX *pctx);
123 	int (*sig_print)(BIO *out, const X509_ALGOR *sigalg,
124 	    const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx);
125 
126 	void (*pkey_free)(EVP_PKEY *pkey);
127 	int (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2);
128 
129 	/* Legacy functions for old PEM */
130 
131 	int (*old_priv_decode)(EVP_PKEY *pkey, const unsigned char **pder,
132 	    int derlen);
133 	int (*old_priv_encode)(const EVP_PKEY *pkey, unsigned char **pder);
134 	/* Custom ASN1 signature verification */
135 	int (*item_verify)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
136 	    X509_ALGOR *a, ASN1_BIT_STRING *sig, EVP_PKEY *pkey);
137 	int (*item_sign)(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
138 	    X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig);
139 
140 	int (*pkey_check)(const EVP_PKEY *pk);
141 	int (*pkey_public_check)(const EVP_PKEY *pk);
142 	int (*pkey_param_check)(const EVP_PKEY *pk);
143 
144 	int (*set_priv_key)(EVP_PKEY *pk, const unsigned char *private_key,
145 	    size_t len);
146 	int (*set_pub_key)(EVP_PKEY *pk, const unsigned char *public_key,
147 	    size_t len);
148 	int (*get_priv_key)(const EVP_PKEY *pk, unsigned char *out_private_key,
149 	    size_t *out_len);
150 	int (*get_pub_key)(const EVP_PKEY *pk, unsigned char *out_public_key,
151 	    size_t *out_len);
152 } /* EVP_PKEY_ASN1_METHOD */;
153 
154 /* Type needs to be a bit field
155  * Sub-type needs to be for variations on the method, as in, can it do
156  * arbitrary encryption.... */
157 struct evp_pkey_st {
158 	int type;
159 	int references;
160 	const EVP_PKEY_ASN1_METHOD *ameth;
161 	union	{
162 		void *ptr;
163 #ifndef OPENSSL_NO_RSA
164 		struct rsa_st *rsa;	/* RSA */
165 #endif
166 #ifndef OPENSSL_NO_DSA
167 		struct dsa_st *dsa;	/* DSA */
168 #endif
169 #ifndef OPENSSL_NO_DH
170 		struct dh_st *dh;	/* DH */
171 #endif
172 #ifndef OPENSSL_NO_EC
173 		struct ec_key_st *ec;	/* ECC */
174 		struct ecx_key_st *ecx;	/* ECX */
175 #endif
176 #ifndef OPENSSL_NO_GOST
177 		struct gost_key_st *gost; /* GOST */
178 #endif
179 	} pkey;
180 	int save_parameters;
181 	STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
182 } /* EVP_PKEY */;
183 
184 struct evp_md_st {
185 	int type;
186 	int pkey_type;
187 	int md_size;
188 	unsigned long flags;
189 	int (*init)(EVP_MD_CTX *ctx);
190 	int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
191 	int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
192 	int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
193 	int (*cleanup)(EVP_MD_CTX *ctx);
194 
195 	int block_size;
196 	int ctx_size; /* how big does the ctx->md_data need to be */
197 	/* control function */
198 	int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
199 } /* EVP_MD */;
200 
201 struct evp_md_ctx_st {
202 	const EVP_MD *digest;
203 	unsigned long flags;
204 	void *md_data;
205 	/* Public key context for sign/verify */
206 	EVP_PKEY_CTX *pctx;
207 	/* Update function: usually copied from EVP_MD */
208 	int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
209 } /* EVP_MD_CTX */;
210 
211 struct evp_cipher_st {
212 	int nid;
213 	int block_size;
214 	int key_len;		/* Default value for variable length ciphers */
215 	int iv_len;
216 	unsigned long flags;	/* Various flags */
217 	int (*init)(EVP_CIPHER_CTX *ctx, const unsigned char *key,
218 	    const unsigned char *iv, int enc);	/* init key */
219 	int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
220 	    const unsigned char *in, size_t inl);/* encrypt/decrypt data */
221 	int (*cleanup)(EVP_CIPHER_CTX *); /* cleanup ctx */
222 	int ctx_size;		/* how big ctx->cipher_data needs to be */
223 	int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Populate a ASN1_TYPE with parameters */
224 	int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Get parameters from a ASN1_TYPE */
225 	int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr); /* Miscellaneous operations */
226 } /* EVP_CIPHER */;
227 
228 struct evp_cipher_ctx_st {
229 	const EVP_CIPHER *cipher;
230 	int encrypt;		/* encrypt or decrypt */
231 	int partial_len;	/* number of bytes written to buf */
232 
233 	unsigned char oiv[EVP_MAX_IV_LENGTH];	/* original iv */
234 	unsigned char iv[EVP_MAX_IV_LENGTH];	/* working iv */
235 	unsigned char buf[EVP_MAX_BLOCK_LENGTH];/* saved partial block */
236 	int num;				/* used by cfb/ofb/ctr mode */
237 
238 	void *app_data;		/* application stuff */
239 	int key_len;		/* May change for variable length cipher */
240 	unsigned long flags;	/* Various flags */
241 	void *cipher_data; /* per EVP data */
242 	int final_used;
243 	unsigned char final[EVP_MAX_BLOCK_LENGTH];/* possible final block */
244 } /* EVP_CIPHER_CTX */;
245 
246 struct evp_Encode_Ctx_st {
247 
248 	int num;	/* number saved in a partial encode/decode */
249 	int length;	/* The length is either the output line length
250 			 * (in input bytes) or the shortest input line
251 			 * length that is ok.  Once decoding begins,
252 			 * the length is adjusted up each time a longer
253 			 * line is decoded */
254 	unsigned char enc_data[80];	/* data to encode */
255 	int line_num;	/* number read on current line */
256 	int expect_nl;
257 } /* EVP_ENCODE_CTX */;
258 
259 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
260 
261 struct evp_pkey_ctx_st {
262 	/* Method associated with this operation */
263 	const EVP_PKEY_METHOD *pmeth;
264 	/* Key: may be NULL */
265 	EVP_PKEY *pkey;
266 	/* Peer key for key agreement, may be NULL */
267 	EVP_PKEY *peerkey;
268 	/* Actual operation */
269 	int operation;
270 	/* Algorithm specific data */
271 	void *data;
272 	/* Application specific data */
273 	void *app_data;
274 	/* Keygen callback */
275 	EVP_PKEY_gen_cb *pkey_gencb;
276 	/* implementation specific keygen data */
277 	int *keygen_info;
278 	int keygen_info_count;
279 } /* EVP_PKEY_CTX */;
280 
281 struct evp_pkey_method_st {
282 	int pkey_id;
283 	int flags;
284 
285 	int (*init)(EVP_PKEY_CTX *ctx);
286 	int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
287 	void (*cleanup)(EVP_PKEY_CTX *ctx);
288 
289 	int (*paramgen_init)(EVP_PKEY_CTX *ctx);
290 	int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
291 
292 	int (*keygen_init)(EVP_PKEY_CTX *ctx);
293 	int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
294 
295 	int (*sign_init)(EVP_PKEY_CTX *ctx);
296 	int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
297 	    const unsigned char *tbs, size_t tbslen);
298 
299 	int (*verify_init)(EVP_PKEY_CTX *ctx);
300 	int (*verify)(EVP_PKEY_CTX *ctx,
301 	    const unsigned char *sig, size_t siglen,
302 	    const unsigned char *tbs, size_t tbslen);
303 
304 	int (*verify_recover_init)(EVP_PKEY_CTX *ctx);
305 	int (*verify_recover)(EVP_PKEY_CTX *ctx,
306 	    unsigned char *rout, size_t *routlen,
307 	    const unsigned char *sig, size_t siglen);
308 
309 	int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
310 	int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
311 	    EVP_MD_CTX *mctx);
312 
313 	int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
314 	int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig,
315 	    int siglen, EVP_MD_CTX *mctx);
316 
317 	int (*encrypt_init)(EVP_PKEY_CTX *ctx);
318 	int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
319 	    const unsigned char *in, size_t inlen);
320 
321 	int (*decrypt_init)(EVP_PKEY_CTX *ctx);
322 	int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
323 	    const unsigned char *in, size_t inlen);
324 
325 	int (*derive_init)(EVP_PKEY_CTX *ctx);
326 	int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
327 
328 	int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
329 	int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
330 
331 	int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
332 	    const unsigned char *tbs, size_t tbslen);
333 	int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
334 	    size_t siglen, const unsigned char *tbs, size_t tbslen);
335 
336 	int (*check)(EVP_PKEY *pkey);
337 	int (*public_check)(EVP_PKEY *pkey);
338 	int (*param_check)(EVP_PKEY *pkey);
339 } /* EVP_PKEY_METHOD */;
340 
341 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
342 
343 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
344     ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de);
345 
346 /* EVP_AEAD represents a specific AEAD algorithm. */
347 struct evp_aead_st {
348 	unsigned char key_len;
349 	unsigned char nonce_len;
350 	unsigned char overhead;
351 	unsigned char max_tag_len;
352 
353 	int (*init)(struct evp_aead_ctx_st*, const unsigned char *key,
354 	    size_t key_len, size_t tag_len);
355 	void (*cleanup)(struct evp_aead_ctx_st*);
356 
357 	int (*seal)(const struct evp_aead_ctx_st *ctx, unsigned char *out,
358 	    size_t *out_len, size_t max_out_len, const unsigned char *nonce,
359 	    size_t nonce_len, const unsigned char *in, size_t in_len,
360 	    const unsigned char *ad, size_t ad_len);
361 
362 	int (*open)(const struct evp_aead_ctx_st *ctx, unsigned char *out,
363 	    size_t *out_len, size_t max_out_len, const unsigned char *nonce,
364 	    size_t nonce_len, const unsigned char *in, size_t in_len,
365 	    const unsigned char *ad, size_t ad_len);
366 };
367 
368 /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key
369  * and message-independent IV. */
370 struct evp_aead_ctx_st {
371 	const EVP_AEAD *aead;
372 	/* aead_state is an opaque pointer to the AEAD specific state. */
373 	void *aead_state;
374 };
375 
376 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str);
377 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex);
378 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name);
379 
380 void EVP_CIPHER_CTX_legacy_clear(EVP_CIPHER_CTX *ctx);
381 void EVP_MD_CTX_legacy_clear(EVP_MD_CTX *ctx);
382 
383 __END_HIDDEN_DECLS
384 
385 #endif /* !HEADER_EVP_LOCAL_H */
386