Add Common shared code needed to move aes ciphers to providers
[openssl.git] / crypto / evp / evp_locl.h
1 /*
2  * Copyright 2000-2019 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
10 /* EVP_MD_CTX related stuff */
11
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     /* Provider ctx */
25     void *provctx;
26     EVP_MD *fetched_digest;
27 } /* EVP_MD_CTX */ ;
28
29 struct evp_cipher_ctx_st {
30     const EVP_CIPHER *cipher;
31     ENGINE *engine;             /* functional reference if 'cipher' is
32                                  * ENGINE-provided */
33     int encrypt;                /* encrypt or decrypt */
34     int buf_len;                /* number we have left */
35     unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
36     unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
37     unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
38     int num;                    /* used by cfb/ofb/ctr mode */
39     /* FIXME: Should this even exist? It appears unused */
40     void *app_data;             /* application stuff */
41     int key_len;                /* May change for variable length cipher */
42     unsigned long flags;        /* Various flags */
43     void *cipher_data;          /* per EVP data */
44     int final_used;
45     int block_mask;
46     unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
47
48     /* Provider ctx */
49     void *provctx;
50     EVP_CIPHER *fetched_cipher;
51 } /* EVP_CIPHER_CTX */ ;
52
53 struct evp_mac_ctx_st {
54     const EVP_MAC *meth;         /* Method structure */
55     void *data;                  /* Individual method data */
56 } /* EVP_MAC_CTX */;
57
58 struct evp_kdf_ctx_st {
59     const EVP_KDF *meth;         /* Method structure */
60     EVP_KDF_IMPL *impl;          /* Algorithm-specific data */
61 } /* EVP_KDF_CTX */ ;
62
63 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
64                              int passlen, ASN1_TYPE *param,
65                              const EVP_CIPHER *c, const EVP_MD *md,
66                              int en_de);
67
68 struct evp_Encode_Ctx_st {
69     /* number saved in a partial encode/decode */
70     int num;
71     /*
72      * The length is either the output line length (in input bytes) or the
73      * shortest input line length that is ok.  Once decoding begins, the
74      * length is adjusted up each time a longer line is decoded
75      */
76     int length;
77     /* data to encode */
78     unsigned char enc_data[80];
79     /* number read on current line */
80     int line_num;
81     unsigned int flags;
82 };
83
84 typedef struct evp_pbe_st EVP_PBE_CTL;
85 DEFINE_STACK_OF(EVP_PBE_CTL)
86
87 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len);
88
89 #include <openssl/ossl_typ.h>
90 #include <openssl/core.h>
91
92 void *evp_generic_fetch(OPENSSL_CTX *ctx, int operation_id,
93                         const char *algorithm, const char *properties,
94                         void *(*new_method)(const OSSL_DISPATCH *fns,
95                                             OSSL_PROVIDER *prov),
96                         int (*up_ref_method)(void *),
97                         void (*free_method)(void *));
98
99 /* Helper functions to avoid duplicating code */
100
101 /*
102  * These methods implement different ways to pass a params array to the
103  * provider.  They will return one of these values:
104  *
105  * -2 if the method doesn't come from a provider
106  *    (evp_do_param will return this to the called)
107  * -1 if the provider doesn't offer the desired function
108  *    (evp_do_param will raise an error and return 0)
109  * or the return value from the desired function
110  *    (evp_do_param will return it to the caller)
111  */
112 int evp_do_ciph_getparams(const EVP_CIPHER *ciph, OSSL_PARAM params[]);
113 int evp_do_ciph_ctx_getparams(const EVP_CIPHER *ciph, void *provctx,
114                               OSSL_PARAM params[]);
115 int evp_do_ciph_ctx_setparams(const EVP_CIPHER *ciph, void *provctx,
116                               OSSL_PARAM params[]);