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