Refactor TLS-PRF's kdf_tls1_prf_mkmacctx() to a provider utility
[openssl.git] / providers / common / include / internal / provider_util.h
1 /*
2  * Copyright 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 #include <openssl/provider.h>
11 #include <openssl/engine.h>
12
13 typedef struct {
14     /*
15      * References to the underlying cipher implementation.  |cipher| caches
16      * the cipher, always.  |alloc_cipher| only holds a reference to an
17      * explicitly fetched cipher.
18      */
19     const EVP_CIPHER *cipher;   /* cipher */
20     EVP_CIPHER *alloc_cipher;   /* fetched cipher */
21
22     /* Conditions for legacy EVP_CIPHER uses */
23     ENGINE *engine;             /* cipher engine */
24
25     /* Name this was fetched by */
26     char name[51];               /* A longer name would be unexpected */
27 } PROV_CIPHER;
28
29 typedef struct {
30     /*
31      * References to the underlying digest implementation.  |md| caches
32      * the digest, always.  |alloc_md| only holds a reference to an explicitly
33      * fetched digest.
34      */
35     const EVP_MD *md;           /* digest */
36     EVP_MD *alloc_md;           /* fetched digest */
37
38     /* Conditions for legacy EVP_MD uses */
39     ENGINE *engine;             /* digest engine */
40
41     /* Name this was fetched by */
42     char name[51];               /* A longer name would be unexpected */
43 } PROV_DIGEST;
44
45 /* Cipher functions */
46 /*
47  * Load a cipher from the specified parameters with the specified context.
48  * The params "properties", "engine" and "cipher" are used to determine the
49  * implementation used.  If a provider cannot be found, it falls back to trying
50  * non-provider based implementations.
51  */
52 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
53                                       const OSSL_PARAM params[],
54                                       OPENSSL_CTX *ctx);
55
56 /* Reset the PROV_CIPHER fields and free any allocated cipher reference */
57 void ossl_prov_cipher_reset(PROV_CIPHER *pc);
58
59 /* Clone a PROV_CIPHER structure into a second */
60 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src);
61
62 /* Query the cipher and associated engine (if any) */
63 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc);
64 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc);
65 const char *ossl_prov_cipher_name(const PROV_CIPHER *pc);
66
67 /* Digest functions */
68 /*
69  * Load a digest from the specified parameters with the specified context.
70  * The params "properties", "engine" and "digest" are used to determine the
71  * implementation used.  If a provider cannot be found, it falls back to trying
72  * non-provider based implementations.
73  */
74 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
75                                       const OSSL_PARAM params[],
76                                       OPENSSL_CTX *ctx);
77
78 /* Reset the PROV_DIGEST fields and free any allocated digest reference */
79 void ossl_prov_digest_reset(PROV_DIGEST *pd);
80
81 /* Clone a PROV_DIGEST structure into a second */
82 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src);
83
84 /* Query the digest and associated engine (if any) */
85 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd);
86 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd);
87 const char *ossl_prov_digest_name(const PROV_DIGEST *pd);
88
89 /* MAC functions */
90 /*
91  * Load an EVP_MAC_CTX* from the specified parameters with the specified
92  * library context.
93  * The params "mac" and "properties" are used to determine the implementation
94  * used, and the parameters "digest", "cipher", "engine" and "properties" are
95  * passed to the MAC via the created MAC context if they are given.
96  * If there is already a created MAC context, it will be replaced if the "mac"
97  * parameter is found, otherwise it will simply be used as is, and passed the
98  * parameters to pilfer as it sees fit.
99  *
100  * As an option, a MAC name may be explicitly given, and if it is, the "mac"
101  * parameter will be ignored.
102  * Similarly, as an option, a cipher name or a digest name may be explicitly
103  * given, and if any of them is, the "digest" and "cipher" parameters are
104  * ignored.
105  */
106 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
107                                       const OSSL_PARAM params[],
108                                       const char *macname,
109                                       const char *ciphername,
110                                       const char *mdname,
111                                       OPENSSL_CTX *ctx);