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