Deal with BUF_MEM_grow ambiguity
[openssl.git] / crypto / evp / evp_utils.c
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 /* Internal EVP utility functions */
11
12 #include <openssl/core.h>
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include <openssl/asn1.h>        /* evp_locl.h needs it */
16 #include <openssl/safestack.h>   /* evp_locl.h needs it */
17 #include "internal/evp_int.h"    /* evp_locl.h needs it */
18 #include "evp_locl.h"
19
20 /*
21  * EVP_CTRL_RET_UNSUPPORTED = -1 is the returned value from any ctrl function
22  * where the control command isn't supported, and an alternative code path
23  * may be chosen.
24  * Since these functions are used to implement ctrl functionality, we
25  * use the same value, and other callers will have to compensate.
26  */
27 #define PARAM_CHECK(obj, func, errfunc)                                        \
28     if (obj->prov == NULL)                                                     \
29         return EVP_CTRL_RET_UNSUPPORTED;                                       \
30     if (obj->func == NULL) {                                                   \
31         errfunc();                                                             \
32         return 0;                                                              \
33     }
34
35 #define PARAM_FUNC(name, func, type, err)                                      \
36 int name (const type *obj, OSSL_PARAM params[])                                \
37 {                                                                              \
38     PARAM_CHECK(obj, func, err)                                                \
39     return obj->func(params);                                                  \
40 }
41
42 #define PARAM_CTX_FUNC(name, func, type, err)                                  \
43 int name (const type *obj, void *provctx, OSSL_PARAM params[])                 \
44 {                                                                              \
45     PARAM_CHECK(obj, func, err)                                                \
46     return obj->func(provctx, params);                                         \
47 }
48
49 #define PARAM_FUNCTIONS(type,                                                  \
50                         getname, getfunc,                                      \
51                         getctxname, getctxfunc,                                \
52                         setctxname, setctxfunc)                                \
53     PARAM_FUNC(getname, getfunc, type, geterr)                                 \
54     PARAM_CTX_FUNC(getctxname, getctxfunc, type, geterr)                       \
55     PARAM_CTX_FUNC(setctxname, setctxfunc, type, seterr)
56
57 /*
58  * These error functions are a workaround for the error scripts, which
59  * currently require that XXXerr method appears inside a function (not a macro).
60  */
61 static void geterr(void)
62 {
63     EVPerr(0, EVP_R_CANNOT_GET_PARAMETERS);
64 }
65
66 static void seterr(void)
67 {
68     EVPerr(0, EVP_R_CANNOT_SET_PARAMETERS);
69 }
70
71 PARAM_FUNCTIONS(EVP_CIPHER,
72                 evp_do_ciph_getparams, get_params,
73                 evp_do_ciph_ctx_getparams, get_ctx_params,
74                 evp_do_ciph_ctx_setparams, set_ctx_params)
75
76 PARAM_FUNCTIONS(EVP_MD,
77                 evp_do_md_getparams, get_params,
78                 evp_do_md_ctx_getparams, get_ctx_params,
79                 evp_do_md_ctx_setparams, set_ctx_params)