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