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