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