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