]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_prov.c
Start converting the engine to provider
[openssl-gost/engine.git] / gost_prov.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <openssl/core.h>
4 #include <openssl/core_numbers.h>
5 #include <openssl/core_names.h>
6 #include <openssl/params.h>
7
8 #include "gost_prov.h"
9
10 /* Functions provided by the core */
11 static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
12 static OSSL_core_get_params_fn *c_get_params = NULL;
13
14 /* Parameters we provide to the core */
15 static const OSSL_ITEM gost_param_types[] = {
16     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
17     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
18     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
19     { 0, NULL }
20 };
21
22 static const OSSL_ITEM *gost_gettable_params(const OSSL_PROVIDER *prov)
23 {
24     return gost_param_types;
25 }
26
27 static int gost_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
28 {
29     OSSL_PARAM *p;
30
31     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
32     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, GOST_PROV_NAME))
33         return 0;
34     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
35     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, GOST_PROV_VERSION_STR))
36         return 0;
37     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
38     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, GOST_PROV_FULL_VERSION_STR))
39         return 0;
40
41     return 1;
42 }
43
44 static const OSSL_ALGORITHM gost_digests[] = {
45                 { "md_gost2012_256:streebog256", "gost.gost=yes", streebog256_funcs },
46                 { "md_gost2012_512:streebog512", "gost.gost=yes", NULL },
47
48     { NULL, NULL, NULL }
49 };
50
51 static const OSSL_ALGORITHM *gost_query(OSSL_PROVIDER *prov,
52                                           int operation_id,
53                                           int *no_cache)
54 {
55     *no_cache = 0;
56     switch (operation_id) {
57     case OSSL_OP_DIGEST:
58         return gost_digests;
59     }
60     return NULL;
61 }
62
63 /* Functions we provide to the core */
64 static const OSSL_DISPATCH gost_dispatch_table[] = {
65     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))gost_gettable_params },
66     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))gost_get_params },
67     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))gost_query },
68     { 0, NULL }
69 };
70
71 int OSSL_provider_init(const OSSL_PROVIDER *provider,
72                        const OSSL_DISPATCH *in,
73                        const OSSL_DISPATCH **out,
74                        void **provctx)
75 {
76     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
77
78     for (; in->function_id != 0; in++) {
79         switch (in->function_id) {
80         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
81             c_gettable_params = OSSL_get_core_gettable_params(in);
82             break;
83         case OSSL_FUNC_CORE_GET_PARAMS:
84             c_get_params = OSSL_get_core_get_params(in);
85             break;
86         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
87             c_get_libctx = OSSL_get_core_get_library_context(in);
88             break;
89         /* Just ignore anything we don't understand */
90         default:
91             break;
92         }
93     }
94
95     if (c_get_libctx == NULL)
96         return 0;
97
98     *out = gost_dispatch_table;
99
100     /*
101      * We want to make sure that all calls from this provider that requires
102      * a library context use the same context as the one used to call our
103      * functions.  We do that by passing it along as the provider context.
104      */
105     *provctx = c_get_libctx(provider);
106     return 1;
107 }