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