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