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