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