xref: /openbsd/lib/libcrypto/evp/evp_local.h (revision 9e6efb0a)
1 /* $OpenBSD: evp_local.h,v 1.22 2024/04/12 09:41:39 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 	} pkey;
177 	int save_parameters;
178 	STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
179 } /* EVP_PKEY */;
180 
181 struct evp_md_st {
182 	int type;
183 	int pkey_type;
184 	int md_size;
185 	unsigned long flags;
186 	int (*init)(EVP_MD_CTX *ctx);
187 	int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
188 	int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
189 	int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
190 	int (*cleanup)(EVP_MD_CTX *ctx);
191 
192 	int block_size;
193 	int ctx_size; /* how big does the ctx->md_data need to be */
194 	/* control function */
195 	int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
196 } /* EVP_MD */;
197 
198 struct evp_md_ctx_st {
199 	const EVP_MD *digest;
200 	unsigned long flags;
201 	void *md_data;
202 	/* Public key context for sign/verify */
203 	EVP_PKEY_CTX *pctx;
204 	/* Update function: usually copied from EVP_MD */
205 	int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
206 } /* EVP_MD_CTX */;
207 
208 struct evp_cipher_st {
209 	int nid;
210 	int block_size;
211 	int key_len;		/* Default value for variable length ciphers */
212 	int iv_len;
213 	unsigned long flags;	/* Various flags */
214 	int (*init)(EVP_CIPHER_CTX *ctx, const unsigned char *key,
215 	    const unsigned char *iv, int enc);	/* init key */
216 	int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
217 	    const unsigned char *in, size_t inl);/* encrypt/decrypt data */
218 	int (*cleanup)(EVP_CIPHER_CTX *); /* cleanup ctx */
219 	int ctx_size;		/* how big ctx->cipher_data needs to be */
220 	int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Populate a ASN1_TYPE with parameters */
221 	int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *); /* Get parameters from a ASN1_TYPE */
222 	int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr); /* Miscellaneous operations */
223 } /* EVP_CIPHER */;
224 
225 struct evp_cipher_ctx_st {
226 	const EVP_CIPHER *cipher;
227 	int encrypt;		/* encrypt or decrypt */
228 	int partial_len;	/* number of bytes written to buf */
229 
230 	unsigned char oiv[EVP_MAX_IV_LENGTH];	/* original iv */
231 	unsigned char iv[EVP_MAX_IV_LENGTH];	/* working iv */
232 	unsigned char buf[EVP_MAX_BLOCK_LENGTH];/* saved partial block */
233 	int num;				/* used by cfb/ofb/ctr mode */
234 
235 	void *app_data;		/* application stuff */
236 	int key_len;		/* May change for variable length cipher */
237 	unsigned long flags;	/* Various flags */
238 	void *cipher_data; /* per EVP data */
239 	int final_used;
240 	unsigned char final[EVP_MAX_BLOCK_LENGTH];/* possible final block */
241 } /* EVP_CIPHER_CTX */;
242 
243 struct evp_Encode_Ctx_st {
244 
245 	int num;	/* number saved in a partial encode/decode */
246 	int length;	/* The length is either the output line length
247 			 * (in input bytes) or the shortest input line
248 			 * length that is ok.  Once decoding begins,
249 			 * the length is adjusted up each time a longer
250 			 * line is decoded */
251 	unsigned char enc_data[80];	/* data to encode */
252 	int line_num;	/* number read on current line */
253 	int expect_nl;
254 } /* EVP_ENCODE_CTX */;
255 
256 #define EVP_MAXCHUNK ((size_t)1<<(sizeof(long)*8-2))
257 
258 struct evp_pkey_ctx_st {
259 	/* Method associated with this operation */
260 	const EVP_PKEY_METHOD *pmeth;
261 	/* Key: may be NULL */
262 	EVP_PKEY *pkey;
263 	/* Peer key for key agreement, may be NULL */
264 	EVP_PKEY *peerkey;
265 	/* Actual operation */
266 	int operation;
267 	/* Algorithm specific data */
268 	void *data;
269 	/* Application specific data */
270 	void *app_data;
271 	/* Keygen callback */
272 	EVP_PKEY_gen_cb *pkey_gencb;
273 	/* implementation specific keygen data */
274 	int *keygen_info;
275 	int keygen_info_count;
276 } /* EVP_PKEY_CTX */;
277 
278 struct evp_pkey_method_st {
279 	int pkey_id;
280 	int flags;
281 
282 	int (*init)(EVP_PKEY_CTX *ctx);
283 	int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src);
284 	void (*cleanup)(EVP_PKEY_CTX *ctx);
285 
286 	int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
287 
288 	int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
289 
290 	int (*sign_init)(EVP_PKEY_CTX *ctx);
291 	int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
292 	    const unsigned char *tbs, size_t tbslen);
293 
294 	int (*verify_init)(EVP_PKEY_CTX *ctx);
295 	int (*verify)(EVP_PKEY_CTX *ctx,
296 	    const unsigned char *sig, size_t siglen,
297 	    const unsigned char *tbs, size_t tbslen);
298 
299 	int (*verify_recover)(EVP_PKEY_CTX *ctx,
300 	    unsigned char *rout, size_t *routlen,
301 	    const unsigned char *sig, size_t siglen);
302 
303 	int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
304 	int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
305 	    EVP_MD_CTX *mctx);
306 
307 	int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
308 	    const unsigned char *in, size_t inlen);
309 
310 	int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
311 	    const unsigned char *in, size_t inlen);
312 
313 	int (*derive_init)(EVP_PKEY_CTX *ctx);
314 	int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
315 
316 	int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
317 	int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
318 
319 	int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
320 	    const unsigned char *tbs, size_t tbslen);
321 	int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
322 	    size_t siglen, const unsigned char *tbs, size_t tbslen);
323 
324 	int (*check)(EVP_PKEY *pkey);
325 	int (*public_check)(EVP_PKEY *pkey);
326 	int (*param_check)(EVP_PKEY *pkey);
327 } /* EVP_PKEY_METHOD */;
328 
329 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
330 
331 /* EVP_AEAD represents a specific AEAD algorithm. */
332 struct evp_aead_st {
333 	unsigned char key_len;
334 	unsigned char nonce_len;
335 	unsigned char overhead;
336 	unsigned char max_tag_len;
337 
338 	int (*init)(struct evp_aead_ctx_st*, const unsigned char *key,
339 	    size_t key_len, size_t tag_len);
340 	void (*cleanup)(struct evp_aead_ctx_st*);
341 
342 	int (*seal)(const struct evp_aead_ctx_st *ctx, unsigned char *out,
343 	    size_t *out_len, size_t max_out_len, const unsigned char *nonce,
344 	    size_t nonce_len, const unsigned char *in, size_t in_len,
345 	    const unsigned char *ad, size_t ad_len);
346 
347 	int (*open)(const struct evp_aead_ctx_st *ctx, unsigned char *out,
348 	    size_t *out_len, size_t max_out_len, const unsigned char *nonce,
349 	    size_t nonce_len, const unsigned char *in, size_t in_len,
350 	    const unsigned char *ad, size_t ad_len);
351 };
352 
353 /* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key
354  * and message-independent IV. */
355 struct evp_aead_ctx_st {
356 	const EVP_AEAD *aead;
357 	/* aead_state is an opaque pointer to the AEAD specific state. */
358 	void *aead_state;
359 };
360 
361 /* Legacy EVP_CIPHER methods used by CMS and its predecessors. */
362 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type);
363 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type);
364 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type);
365 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *cipher, ASN1_TYPE *type);
366 
367 int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
368     ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de);
369 
370 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str);
371 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex);
372 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md_name);
373 
374 void EVP_CIPHER_CTX_legacy_clear(EVP_CIPHER_CTX *ctx);
375 void EVP_MD_CTX_legacy_clear(EVP_MD_CTX *ctx);
376 
377 __END_HIDDEN_DECLS
378 
379 #endif /* !HEADER_EVP_LOCAL_H */
380