1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <openssl/provider.h>
11e1051a39Sopenharmony_ci#include <openssl/types.h>
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_citypedef struct {
14e1051a39Sopenharmony_ci    /*
15e1051a39Sopenharmony_ci     * References to the underlying cipher implementation.  |cipher| caches
16e1051a39Sopenharmony_ci     * the cipher, always.  |alloc_cipher| only holds a reference to an
17e1051a39Sopenharmony_ci     * explicitly fetched cipher.
18e1051a39Sopenharmony_ci     */
19e1051a39Sopenharmony_ci    const EVP_CIPHER *cipher;   /* cipher */
20e1051a39Sopenharmony_ci    EVP_CIPHER *alloc_cipher;   /* fetched cipher */
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci    /* Conditions for legacy EVP_CIPHER uses */
23e1051a39Sopenharmony_ci    ENGINE *engine;             /* cipher engine */
24e1051a39Sopenharmony_ci} PROV_CIPHER;
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_citypedef struct {
27e1051a39Sopenharmony_ci    /*
28e1051a39Sopenharmony_ci     * References to the underlying digest implementation.  |md| caches
29e1051a39Sopenharmony_ci     * the digest, always.  |alloc_md| only holds a reference to an explicitly
30e1051a39Sopenharmony_ci     * fetched digest.
31e1051a39Sopenharmony_ci     */
32e1051a39Sopenharmony_ci    const EVP_MD *md;           /* digest */
33e1051a39Sopenharmony_ci    EVP_MD *alloc_md;           /* fetched digest */
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci    /* Conditions for legacy EVP_MD uses */
36e1051a39Sopenharmony_ci    ENGINE *engine;             /* digest engine */
37e1051a39Sopenharmony_ci} PROV_DIGEST;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci/* Cipher functions */
40e1051a39Sopenharmony_ci/*
41e1051a39Sopenharmony_ci * Load a cipher from the specified parameters with the specified context.
42e1051a39Sopenharmony_ci * The params "properties", "engine" and "cipher" are used to determine the
43e1051a39Sopenharmony_ci * implementation used.  If a provider cannot be found, it falls back to trying
44e1051a39Sopenharmony_ci * non-provider based implementations.
45e1051a39Sopenharmony_ci */
46e1051a39Sopenharmony_ciint ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
47e1051a39Sopenharmony_ci                                      const OSSL_PARAM params[],
48e1051a39Sopenharmony_ci                                      OSSL_LIB_CTX *ctx);
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci/* Reset the PROV_CIPHER fields and free any allocated cipher reference */
51e1051a39Sopenharmony_civoid ossl_prov_cipher_reset(PROV_CIPHER *pc);
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci/* Clone a PROV_CIPHER structure into a second */
54e1051a39Sopenharmony_ciint ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src);
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci/* Query the cipher and associated engine (if any) */
57e1051a39Sopenharmony_ciconst EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc);
58e1051a39Sopenharmony_ciENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc);
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci/* Digest functions */
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci/*
63e1051a39Sopenharmony_ci * Fetch a digest from the specified libctx using the provided mdname and
64e1051a39Sopenharmony_ci * propquery. Store the result in the PROV_DIGEST and return the fetched md.
65e1051a39Sopenharmony_ci */
66e1051a39Sopenharmony_ciconst EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
67e1051a39Sopenharmony_ci                                     const char *mdname, const char *propquery);
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci/*
70e1051a39Sopenharmony_ci * Load a digest from the specified parameters with the specified context.
71e1051a39Sopenharmony_ci * The params "properties", "engine" and "digest" are used to determine the
72e1051a39Sopenharmony_ci * implementation used.  If a provider cannot be found, it falls back to trying
73e1051a39Sopenharmony_ci * non-provider based implementations.
74e1051a39Sopenharmony_ci */
75e1051a39Sopenharmony_ciint ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
76e1051a39Sopenharmony_ci                                      const OSSL_PARAM params[],
77e1051a39Sopenharmony_ci                                      OSSL_LIB_CTX *ctx);
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci/* Reset the PROV_DIGEST fields and free any allocated digest reference */
80e1051a39Sopenharmony_civoid ossl_prov_digest_reset(PROV_DIGEST *pd);
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci/* Clone a PROV_DIGEST structure into a second */
83e1051a39Sopenharmony_ciint ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src);
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci/* Query the digest and associated engine (if any) */
86e1051a39Sopenharmony_ciconst EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd);
87e1051a39Sopenharmony_ciENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd);
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci
90e1051a39Sopenharmony_ci/*
91e1051a39Sopenharmony_ci * Set the various parameters on an EVP_MAC_CTX from the supplied arguments.
92e1051a39Sopenharmony_ci * If any of the supplied ciphername/mdname etc are NULL then the values
93e1051a39Sopenharmony_ci * from the supplied params (if non NULL) are used instead.
94e1051a39Sopenharmony_ci */
95e1051a39Sopenharmony_ciint ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
96e1051a39Sopenharmony_ci                         const OSSL_PARAM params[],
97e1051a39Sopenharmony_ci                         const char *ciphername,
98e1051a39Sopenharmony_ci                         const char *mdname,
99e1051a39Sopenharmony_ci                         const char *engine,
100e1051a39Sopenharmony_ci                         const char *properties,
101e1051a39Sopenharmony_ci                         const unsigned char *key,
102e1051a39Sopenharmony_ci                         size_t keylen);
103e1051a39Sopenharmony_ci
104e1051a39Sopenharmony_ci/* MAC functions */
105e1051a39Sopenharmony_ci/*
106e1051a39Sopenharmony_ci * Load an EVP_MAC_CTX* from the specified parameters with the specified
107e1051a39Sopenharmony_ci * library context.
108e1051a39Sopenharmony_ci * The params "mac" and "properties" are used to determine the implementation
109e1051a39Sopenharmony_ci * used, and the parameters "digest", "cipher", "engine" and "properties" are
110e1051a39Sopenharmony_ci * passed to the MAC via the created MAC context if they are given.
111e1051a39Sopenharmony_ci * If there is already a created MAC context, it will be replaced if the "mac"
112e1051a39Sopenharmony_ci * parameter is found, otherwise it will simply be used as is, and passed the
113e1051a39Sopenharmony_ci * parameters to pilfer as it sees fit.
114e1051a39Sopenharmony_ci *
115e1051a39Sopenharmony_ci * As an option, a MAC name may be explicitly given, and if it is, the "mac"
116e1051a39Sopenharmony_ci * parameter will be ignored.
117e1051a39Sopenharmony_ci * Similarly, as an option, a cipher name or a digest name may be explicitly
118e1051a39Sopenharmony_ci * given, and if any of them is, the "digest" and "cipher" parameters are
119e1051a39Sopenharmony_ci * ignored.
120e1051a39Sopenharmony_ci */
121e1051a39Sopenharmony_ciint ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
122e1051a39Sopenharmony_ci                                      const OSSL_PARAM params[],
123e1051a39Sopenharmony_ci                                      const char *macname,
124e1051a39Sopenharmony_ci                                      const char *ciphername,
125e1051a39Sopenharmony_ci                                      const char *mdname,
126e1051a39Sopenharmony_ci                                      OSSL_LIB_CTX *ctx);
127e1051a39Sopenharmony_ci
128e1051a39Sopenharmony_citypedef struct ag_capable_st {
129e1051a39Sopenharmony_ci    OSSL_ALGORITHM alg;
130e1051a39Sopenharmony_ci    int (*capable)(void);
131e1051a39Sopenharmony_ci} OSSL_ALGORITHM_CAPABLE;
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_ci/*
134e1051a39Sopenharmony_ci * Dynamically select algorithms by calling a capable() method.
135e1051a39Sopenharmony_ci * If this method is NULL or the method returns 1 then the algorithm is added.
136e1051a39Sopenharmony_ci */
137e1051a39Sopenharmony_civoid ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
138e1051a39Sopenharmony_ci                                         OSSL_ALGORITHM *out);
139