]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Missed digests
[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 if (nid == NID_id_GostR3411_2012_256) {
244         *digest = digest_gost2012_256();
245     } else if (nid == NID_id_GostR3411_2012_512) {
246         *digest = digest_gost2012_512();
247     } else if (nid == NID_id_Gost28147_89_MAC) {
248         *digest = imit_gost_cp_12();
249     } else {
250         ok = 0;
251         *digest = NULL;
252     }
253     return ok;
254 }
255
256 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
257                         const int **nids, int nid)
258 {
259     int ok = 1;
260     if (!cipher) {
261         *nids = gost_cipher_nids;
262         return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
263     }
264
265     if (nid == NID_id_Gost28147_89) {
266         *cipher = &cipher_gost;
267     } else if (nid == NID_gost89_cnt) {
268         *cipher = &cipher_gost_cpacnt;
269     } else if (nid == NID_gost89_cnt_12) {
270         *cipher = &cipher_gost_cpcnt_12;
271     } else if (nid == NID_gost89_cbc) {
272         *cipher = &cipher_gost_cbc;
273     } else {
274         ok = 0;
275         *cipher = NULL;
276     }
277     return ok;
278 }
279
280 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
281                            const int **nids, int nid)
282 {
283     if (!pmeth) {
284         *nids = gost_pkey_meth_nids;
285         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
286             1;
287     }
288
289     switch (nid) {
290     case NID_id_GostR3410_2001:
291         *pmeth = pmeth_GostR3410_2001;
292         return 1;
293     case NID_id_GostR3410_2012_256:
294         *pmeth = pmeth_GostR3410_2012_256;
295         return 1;
296     case NID_id_GostR3410_2012_512:
297         *pmeth = pmeth_GostR3410_2012_512;
298         return 1;
299     case NID_id_Gost28147_89_MAC:
300         *pmeth = pmeth_Gost28147_MAC;
301         return 1;
302     case NID_gost_mac_12:
303         *pmeth = pmeth_Gost28147_MAC_12;
304         return 1;
305
306     default:;
307     }
308
309     *pmeth = NULL;
310     return 0;
311 }
312
313 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
314                                 const int **nids, int nid)
315 {
316     if (!ameth) {
317         *nids = gost_pkey_meth_nids;
318         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
319             1;
320     }
321     switch (nid) {
322     case NID_id_GostR3410_2001:
323         *ameth = ameth_GostR3410_2001;
324         return 1;
325     case NID_id_GostR3410_2012_256:
326         *ameth = ameth_GostR3410_2012_256;
327         return 1;
328     case NID_id_GostR3410_2012_512:
329         *ameth = ameth_GostR3410_2012_512;
330         return 1;
331     case NID_id_Gost28147_89_MAC:
332         *ameth = ameth_Gost28147_MAC;
333         return 1;
334     case NID_gost_mac_12:
335         *ameth = ameth_Gost28147_MAC_12;
336         return 1;
337
338     default:;
339     }
340
341     *ameth = NULL;
342     return 0;
343 }
344
345 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
346 static ENGINE *engine_gost(void)
347 {
348     ENGINE *ret = ENGINE_new();
349     if (!ret)
350         return NULL;
351     if (!bind_gost(ret, engine_gost_id)) {
352         ENGINE_free(ret);
353         return NULL;
354     }
355     return ret;
356 }
357
358 void ENGINE_load_gost(void)
359 {
360     ENGINE *toadd;
361     if (pmeth_GostR3410_2001)
362         return;
363     toadd = engine_gost();
364     if (!toadd)
365         return;
366     ENGINE_add(toadd);
367     ENGINE_free(toadd);
368     ERR_clear_error();
369 }
370 #endif