]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Merge branch 'no_gost94_sig' into gost12_algs
[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     0
41 };
42
43 static int gost_digest_nids[] = {
44     NID_id_GostR3411_94,
45     NID_id_Gost28147_89_MAC,
46     NID_id_GostR3411_2012_256,
47     NID_id_GostR3411_2012_512,
48     NID_gost_mac_12,
49     0
50 };
51
52 static int gost_pkey_meth_nids[] = {
53     NID_id_GostR3410_2001,
54     NID_id_Gost28147_89_MAC,
55     NID_id_GostR3410_2012_256,
56     NID_id_GostR3410_2012_512,
57     NID_gost_mac_12,
58     0
59 };
60
61 static EVP_PKEY_METHOD *pmeth_GostR3410_2001 = NULL,
62     *pmeth_GostR3410_2012_256 = NULL,
63     *pmeth_GostR3410_2012_512 = NULL,
64     *pmeth_Gost28147_MAC = NULL,
65     *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,
71     *ameth_Gost28147_MAC_12 = NULL;
72
73 static int gost_engine_init(ENGINE *e)
74 {
75     return 1;
76 }
77
78 static int gost_engine_finish(ENGINE *e)
79 {
80     return 1;
81 }
82
83 static int gost_engine_destroy(ENGINE *e)
84 {
85     gost_param_free();
86
87     pmeth_GostR3410_2001 = NULL;
88     pmeth_Gost28147_MAC = NULL;
89     pmeth_GostR3410_2012_256 = NULL;
90     pmeth_GostR3410_2012_512 = NULL;
91     pmeth_Gost28147_MAC_12 = NULL;
92
93     ameth_GostR3410_2001 = NULL;
94     ameth_Gost28147_MAC = NULL;
95     ameth_GostR3410_2012_256 = NULL;
96     ameth_GostR3410_2012_512 = NULL;
97     ameth_Gost28147_MAC_12 = NULL;
98
99     return 1;
100 }
101
102 static int bind_gost(ENGINE *e, const char *id)
103 {
104     int ret = 0;
105     if (id && strcmp(id, engine_gost_id))
106         return 0;
107     if (ameth_GostR3410_2001) {
108         printf("GOST engine already loaded\n");
109         goto end;
110     }
111     if (!ENGINE_set_id(e, engine_gost_id)) {
112         printf("ENGINE_set_id failed\n");
113         goto end;
114     }
115     if (!ENGINE_set_name(e, engine_gost_name)) {
116         printf("ENGINE_set_name failed\n");
117         goto end;
118     }
119     if (!ENGINE_set_digests(e, gost_digests)) {
120         printf("ENGINE_set_digests failed\n");
121         goto end;
122     }
123     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
124         printf("ENGINE_set_ciphers failed\n");
125         goto end;
126     }
127     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
128         printf("ENGINE_set_pkey_meths failed\n");
129         goto end;
130     }
131     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
132         printf("ENGINE_set_pkey_asn1_meths failed\n");
133         goto end;
134     }
135     /* Control function and commands */
136     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
137         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
138         goto end;
139     }
140     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
141         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
142         goto end;
143     }
144     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
145         || !ENGINE_set_init_function(e, gost_engine_init)
146         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
147         goto end;
148     }
149
150     if (!register_ameth_gost
151         (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
152          "GOST R 34.10-2001"))
153         goto end;
154     if (!register_ameth_gost
155         (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
156          "GOST R 34.10-2012 with 256 bit key"))
157         goto end;
158     if (!register_ameth_gost
159         (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
160          "GOST R 34.10-2012 with 512 bit key"))
161         goto end;
162     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
163                              "GOST-MAC", "GOST 28147-89 MAC"))
164         goto end;
165     if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
166                              "GOST-MAC-12",
167                              "GOST 28147-89 MAC with 2012 params"))
168         goto end;
169
170     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
171         goto end;
172
173     if (!register_pmeth_gost
174         (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
175         goto end;
176     if (!register_pmeth_gost
177         (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
178         goto end;
179     if (!register_pmeth_gost
180         (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
181         goto end;
182     if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
183         goto end;
184     if (!ENGINE_register_ciphers(e)
185         || !ENGINE_register_digests(e)
186         || !ENGINE_register_pkey_meths(e)
187         /* These two actually should go in LIST_ADD command */
188         || !EVP_add_cipher(&cipher_gost)
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 5;
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 3;               /* three ciphers are supported */
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 {
251         ok = 0;
252         *cipher = NULL;
253     }
254     return ok;
255 }
256
257 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
258                            const int **nids, int nid)
259 {
260     if (!pmeth) {
261         *nids = gost_pkey_meth_nids;
262         return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
263     }
264
265     switch (nid) {
266     case NID_id_GostR3410_2001:
267         *pmeth = pmeth_GostR3410_2001;
268         return 1;
269     case NID_id_GostR3410_2012_256:
270         *pmeth = pmeth_GostR3410_2012_256;
271         return 1;
272     case NID_id_GostR3410_2012_512:
273         *pmeth = pmeth_GostR3410_2012_512;
274         return 1;
275     case NID_id_Gost28147_89_MAC:
276         *pmeth = pmeth_Gost28147_MAC;
277         return 1;
278     case NID_gost_mac_12:
279         *pmeth = pmeth_Gost28147_MAC_12;
280         return 1;
281
282     default:;
283     }
284
285     *pmeth = NULL;
286     return 0;
287 }
288
289 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
290                                 const int **nids, int nid)
291 {
292     if (!ameth) {
293         *nids = gost_pkey_meth_nids;
294         return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
295     }
296     switch (nid) {
297     case NID_id_GostR3410_2001:
298         *ameth = ameth_GostR3410_2001;
299         return 1;
300     case NID_id_GostR3410_2012_256:
301         *ameth = ameth_GostR3410_2012_256;
302         return 1;
303     case NID_id_GostR3410_2012_512:
304         *ameth = ameth_GostR3410_2012_512;
305         return 1;
306     case NID_id_Gost28147_89_MAC:
307         *ameth = ameth_Gost28147_MAC;
308         return 1;
309     case NID_gost_mac_12:
310         *ameth = ameth_Gost28147_MAC_12;
311         return 1;
312
313     default:;
314     }
315
316     *ameth = NULL;
317     return 0;
318 }
319
320 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
321 static ENGINE *engine_gost(void)
322 {
323     ENGINE *ret = ENGINE_new();
324     if (!ret)
325         return NULL;
326     if (!bind_gost(ret, engine_gost_id)) {
327         ENGINE_free(ret);
328         return NULL;
329     }
330     return ret;
331 }
332
333 void ENGINE_load_gost(void)
334 {
335     ENGINE *toadd;
336     if (pmeth_GostR3410_2001)
337         return;
338     toadd = engine_gost();
339     if (!toadd)
340         return;
341     ENGINE_add(toadd);
342     ENGINE_free(toadd);
343     ERR_clear_error();
344 }
345 #endif