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