]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
openssl 1.1.0 compatibility
[openssl-gost/engine.git] / gost_eng.c
1 /**********************************************************************
2  *                          gost_eng.c                                *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *              Main file of GOST engine                              *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include <openssl/obj_mac.h>
16 #include "e_gost_err.h"
17 #include "gost_lcl.h"
18 static const char *engine_gost_id = "gost";
19 static const char *engine_gost_name =
20     "Reference implementation of GOST engine";
21
22 /* Symmetric cipher and digest function registrar */
23
24 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
25                         const int **nids, int nid);
26
27 static int gost_digests(ENGINE *e, const EVP_MD **digest,
28                         const int **nids, int ind);
29
30 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
31                            const int **nids, int nid);
32
33 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
34                                 const int **nids, int nid);
35
36 static int gost_cipher_nids[] = {
37     NID_id_Gost28147_89,
38     NID_gost89_cnt,
39     NID_gost89_cnt_12,
40     NID_gost89_cbc,
41     0
42 };
43
44 static int gost_digest_nids(const int **nids)
45 {
46     static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
47     static int pos = 0;
48     static int init = 0;
49
50     if (!init) {
51         const EVP_MD *md;
52         if ((md = digest_gost()) != NULL)
53             digest_nids[pos++] = EVP_MD_type(md);
54         if ((md = imit_gost_cpa()) != NULL)
55             digest_nids[pos++] = EVP_MD_type(md);
56         if ((md = digest_gost2012_256()) != NULL)
57             digest_nids[pos++] = EVP_MD_type(md);
58         if ((md = digest_gost2012_512()) != NULL)
59             digest_nids[pos++] = EVP_MD_type(md);
60         if ((md = imit_gost_cp_12()) != NULL)
61             digest_nids[pos++] = EVP_MD_type(md);
62         digest_nids[pos] = 0;
63         init = 1;
64     }
65     *nids = digest_nids;
66     return pos;
67 }
68
69 static int gost_pkey_meth_nids[] = {
70     NID_id_GostR3410_2001,
71     NID_id_Gost28147_89_MAC,
72     NID_id_GostR3410_2012_256,
73     NID_id_GostR3410_2012_512,
74     NID_gost_mac_12,
75     0
76 };
77
78 static EVP_PKEY_METHOD *pmeth_GostR3410_2001 = NULL,
79     *pmeth_GostR3410_2012_256 = NULL,
80     *pmeth_GostR3410_2012_512 = NULL,
81     *pmeth_Gost28147_MAC = NULL, *pmeth_Gost28147_MAC_12 = NULL;
82
83 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_2001 = NULL,
84     *ameth_GostR3410_2012_256 = NULL,
85     *ameth_GostR3410_2012_512 = NULL,
86     *ameth_Gost28147_MAC = NULL, *ameth_Gost28147_MAC_12 = NULL;
87
88 static int gost_engine_init(ENGINE *e)
89 {
90     return 1;
91 }
92
93 static int gost_engine_finish(ENGINE *e)
94 {
95     return 1;
96 }
97
98 static int gost_engine_destroy(ENGINE *e)
99 {
100     digest_gost_destroy();
101     digest_gost2012_256_destroy();
102     digest_gost2012_512_destroy();
103     imit_gost_cpa_destroy();
104     imit_gost_cp_12_destroy();
105
106     gost_param_free();
107
108     pmeth_GostR3410_2001 = NULL;
109     pmeth_Gost28147_MAC = NULL;
110     pmeth_GostR3410_2012_256 = NULL;
111     pmeth_GostR3410_2012_512 = NULL;
112     pmeth_Gost28147_MAC_12 = NULL;
113
114     ameth_GostR3410_2001 = NULL;
115     ameth_Gost28147_MAC = NULL;
116     ameth_GostR3410_2012_256 = NULL;
117     ameth_GostR3410_2012_512 = NULL;
118     ameth_Gost28147_MAC_12 = NULL;
119
120     return 1;
121 }
122
123 static int bind_gost(ENGINE *e, const char *id)
124 {
125     int ret = 0;
126     if (id && strcmp(id, engine_gost_id))
127         return 0;
128     if (ameth_GostR3410_2001) {
129         printf("GOST engine already loaded\n");
130         goto end;
131     }
132     if (!ENGINE_set_id(e, engine_gost_id)) {
133         printf("ENGINE_set_id failed\n");
134         goto end;
135     }
136     if (!ENGINE_set_name(e, engine_gost_name)) {
137         printf("ENGINE_set_name failed\n");
138         goto end;
139     }
140     if (!ENGINE_set_digests(e, gost_digests)) {
141         printf("ENGINE_set_digests failed\n");
142         goto end;
143     }
144     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
145         printf("ENGINE_set_ciphers failed\n");
146         goto end;
147     }
148     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
149         printf("ENGINE_set_pkey_meths failed\n");
150         goto end;
151     }
152     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
153         printf("ENGINE_set_pkey_asn1_meths failed\n");
154         goto end;
155     }
156     /* Control function and commands */
157     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
158         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
159         goto end;
160     }
161     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
162         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
163         goto end;
164     }
165     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
166         || !ENGINE_set_init_function(e, gost_engine_init)
167         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
168         goto end;
169     }
170
171     if (!register_ameth_gost
172         (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
173          "GOST R 34.10-2001"))
174         goto end;
175     if (!register_ameth_gost
176         (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
177          "GOST R 34.10-2012 with 256 bit key"))
178         goto end;
179     if (!register_ameth_gost
180         (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
181          "GOST R 34.10-2012 with 512 bit key"))
182         goto end;
183     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
184                              "GOST-MAC", "GOST 28147-89 MAC"))
185         goto end;
186     if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
187                              "GOST-MAC-12",
188                              "GOST 28147-89 MAC with 2012 params"))
189         goto end;
190
191     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
192         goto end;
193
194     if (!register_pmeth_gost
195         (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
196         goto end;
197     if (!register_pmeth_gost
198         (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
199         goto end;
200     if (!register_pmeth_gost
201         (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
202         goto end;
203     if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
204         goto end;
205     if (!ENGINE_register_ciphers(e)
206         || !ENGINE_register_digests(e)
207         || !ENGINE_register_pkey_meths(e)
208         /* These two actually should go in LIST_ADD command */
209         || !EVP_add_cipher(&cipher_gost)
210         || !EVP_add_cipher(&cipher_gost_cbc)
211         || !EVP_add_cipher(&cipher_gost_cpacnt)
212         || !EVP_add_cipher(&cipher_gost_cpcnt_12)
213         || !EVP_add_digest(digest_gost())
214         || !EVP_add_digest(digest_gost2012_512())
215         || !EVP_add_digest(digest_gost2012_256())
216         || !EVP_add_digest(imit_gost_cpa())
217         || !EVP_add_digest(imit_gost_cp_12())
218         ) {
219         goto end;
220     }
221
222     ERR_load_GOST_strings();
223     ret = 1;
224  end:
225     return ret;
226 }
227
228 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
229 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
230     IMPLEMENT_DYNAMIC_CHECK_FN()
231 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
232 static int gost_digests(ENGINE *e, const EVP_MD **digest,
233                         const int **nids, int nid)
234 {
235     int ok = 1;
236     if (!digest) {
237         return gost_digest_nids(nids);
238     }
239     if (nid == NID_id_GostR3411_94) {
240         *digest = digest_gost();
241     } else if (nid == NID_id_Gost28147_89_MAC) {
242         *digest = imit_gost_cpa();
243     } else {
244         ok = 0;
245         *digest = NULL;
246     }
247     return ok;
248 }
249
250 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
251                         const int **nids, int nid)
252 {
253     int ok = 1;
254     if (!cipher) {
255         *nids = gost_cipher_nids;
256         return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
257     }
258
259     if (nid == NID_id_Gost28147_89) {
260         *cipher = &cipher_gost;
261     } else if (nid == NID_gost89_cnt) {
262         *cipher = &cipher_gost_cpacnt;
263     } else if (nid == NID_gost89_cnt_12) {
264         *cipher = &cipher_gost_cpcnt_12;
265     } else if (nid == NID_gost89_cbc) {
266         *cipher = &cipher_gost_cbc;
267     } else {
268         ok = 0;
269         *cipher = NULL;
270     }
271     return ok;
272 }
273
274 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
275                            const int **nids, int nid)
276 {
277     if (!pmeth) {
278         *nids = gost_pkey_meth_nids;
279         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
280             1;
281     }
282
283     switch (nid) {
284     case NID_id_GostR3410_2001:
285         *pmeth = pmeth_GostR3410_2001;
286         return 1;
287     case NID_id_GostR3410_2012_256:
288         *pmeth = pmeth_GostR3410_2012_256;
289         return 1;
290     case NID_id_GostR3410_2012_512:
291         *pmeth = pmeth_GostR3410_2012_512;
292         return 1;
293     case NID_id_Gost28147_89_MAC:
294         *pmeth = pmeth_Gost28147_MAC;
295         return 1;
296     case NID_gost_mac_12:
297         *pmeth = pmeth_Gost28147_MAC_12;
298         return 1;
299
300     default:;
301     }
302
303     *pmeth = NULL;
304     return 0;
305 }
306
307 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
308                                 const int **nids, int nid)
309 {
310     if (!ameth) {
311         *nids = gost_pkey_meth_nids;
312         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
313             1;
314     }
315     switch (nid) {
316     case NID_id_GostR3410_2001:
317         *ameth = ameth_GostR3410_2001;
318         return 1;
319     case NID_id_GostR3410_2012_256:
320         *ameth = ameth_GostR3410_2012_256;
321         return 1;
322     case NID_id_GostR3410_2012_512:
323         *ameth = ameth_GostR3410_2012_512;
324         return 1;
325     case NID_id_Gost28147_89_MAC:
326         *ameth = ameth_Gost28147_MAC;
327         return 1;
328     case NID_gost_mac_12:
329         *ameth = ameth_Gost28147_MAC_12;
330         return 1;
331
332     default:;
333     }
334
335     *ameth = NULL;
336     return 0;
337 }
338
339 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
340 static ENGINE *engine_gost(void)
341 {
342     ENGINE *ret = ENGINE_new();
343     if (!ret)
344         return NULL;
345     if (!bind_gost(ret, engine_gost_id)) {
346         ENGINE_free(ret);
347         return NULL;
348     }
349     return ret;
350 }
351
352 void ENGINE_load_gost(void)
353 {
354     ENGINE *toadd;
355     if (pmeth_GostR3410_2001)
356         return;
357     toadd = engine_gost();
358     if (!toadd)
359         return;
360     ENGINE_add(toadd);
361     ENGINE_free(toadd);
362     ERR_clear_error();
363 }
364 #endif