]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
gost_crypt: Add magma_cbc_cipher
[openssl-gost/engine.git] / gost_eng.c
1 /**********************************************************************
2  *                          gost_eng.c                                *
3  *              Main file of GOST engine                              *
4  *                                                                    *
5  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
6  *             Copyright (c) 2020 Chikunov Vitaly <vt@altlinux.org>   *
7  *                                                                    *
8  *       This file is distributed under the same license as OpenSSL   *
9  *                                                                    *
10  **********************************************************************/
11 #include <string.h>
12 #include <openssl/crypto.h>
13 #include <openssl/err.h>
14 #include <openssl/evp.h>
15 #include <openssl/engine.h>
16 #include <openssl/obj_mac.h>
17 #include "e_gost_err.h"
18 #include "gost_lcl.h"
19
20 #include "gost_grasshopper_cipher.h"
21
22 static const char* engine_gost_id = "gost";
23
24 static const char* engine_gost_name =
25         "Reference implementation of GOST engine";
26
27 /* Symmetric cipher and digest function registrar */
28
29 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
30                         const int** nids, int nid);
31
32 static int gost_digests(ENGINE* e, const EVP_MD** digest,
33                         const int** nids, int nid);
34
35 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
36                            const int** nids, int nid);
37
38 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
39                                 const int** nids, int nid);
40
41 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
42         * pmeth_GostR3410_2012_256 = NULL,
43         * pmeth_GostR3410_2012_512 = NULL,
44         * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
45         * pmeth_magma_mac = NULL,  * pmeth_grasshopper_mac = NULL,
46         * pmeth_magma_mac_acpkm = NULL,  * pmeth_grasshopper_mac_acpkm = NULL;
47
48 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
49         * ameth_GostR3410_2012_256 = NULL,
50         * ameth_GostR3410_2012_512 = NULL,
51         * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
52         * ameth_magma_mac = NULL,  * ameth_grasshopper_mac = NULL,
53         * ameth_magma_mac_acpkm = NULL,  * ameth_grasshopper_mac_acpkm = NULL;
54
55 static struct gost_digest_minfo {
56     int nid;
57     EVP_MD *(*digest)(void);
58     void (*destroy)(void);
59     const char *sn;
60     const char *alias;
61 } gost_digest_array[] = {
62     {
63         NID_id_GostR3411_94,
64         digest_gost,
65         digest_gost_destroy,
66     },
67     {
68         NID_id_Gost28147_89_MAC,
69         imit_gost_cpa,
70         imit_gost_cpa_destroy,
71     },
72     {
73         NID_id_GostR3411_2012_256,
74         digest_gost2012_256,
75         digest_gost2012_256_destroy,
76         SN_id_GostR3411_2012_256,
77         "streebog256",
78     },
79     {
80         NID_id_GostR3411_2012_512,
81         digest_gost2012_512,
82         digest_gost2012_512_destroy,
83         SN_id_GostR3411_2012_512,
84         "streebog512",
85     },
86     {
87         NID_gost_mac_12,
88         imit_gost_cp_12,
89         imit_gost_cp_12_destroy,
90     },
91     {
92         NID_magma_mac,
93         magma_omac,
94         magma_omac_destroy,
95     },
96     {
97         NID_grasshopper_mac,
98         grasshopper_omac,
99         grasshopper_omac_destroy,
100     },
101     {
102         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
103         grasshopper_omac_acpkm,
104         grasshopper_omac_acpkm_destroy,
105     },
106     { 0 },
107 };
108
109 static struct gost_cipher_minfo {
110     int nid;
111     const EVP_CIPHER *(*cipher)(void);
112     GOST_cipher *reg;
113 } gost_cipher_array[] = {
114     {
115         NID_id_Gost28147_89,
116         NULL,
117         &Gost28147_89_cipher,
118     },
119     {
120         NID_gost89_cnt,
121         NULL,
122         &Gost28147_89_cnt_cipher,
123     },
124     {
125         NID_gost89_cnt_12,
126         NULL,
127         &Gost28147_89_cnt_12_cipher,
128     },
129     {
130         NID_gost89_cbc,
131         NULL,
132         &Gost28147_89_cbc_cipher,
133     },
134     {
135         NID_grasshopper_ecb,
136         cipher_gost_grasshopper_ecb,
137     },
138     {
139         NID_grasshopper_cbc,
140         cipher_gost_grasshopper_cbc,
141     },
142     {
143         NID_grasshopper_cfb,
144         cipher_gost_grasshopper_cfb,
145     },
146     {
147         NID_grasshopper_ofb,
148         cipher_gost_grasshopper_ofb,
149     },
150     {
151         NID_grasshopper_ctr,
152         cipher_gost_grasshopper_ctr,
153     },
154     {
155         NID_magma_cbc,
156         NULL,
157         &magma_cbc_cipher,
158     },
159     {
160         NID_magma_ctr,
161         NULL,
162         &magma_ctr_cipher,
163     },
164     {
165         NID_magma_ctr_acpkm,
166         NULL,
167         &magma_ctr_acpkm_cipher,
168     },
169     {
170         NID_magma_ctr_acpkm_omac,
171         NULL,
172         &magma_ctr_acpkm_omac_cipher,
173     },
174     {
175         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
176         cipher_gost_grasshopper_ctracpkm,
177     },
178     {
179         NID_kuznyechik_ctr_acpkm_omac,
180         cipher_gost_grasshopper_ctracpkm_omac,
181     },
182     {
183         NID_magma_kexp15,
184         cipher_magma_wrap,
185     },
186     {
187         NID_kuznyechik_kexp15,
188         cipher_kuznyechik_wrap,
189     },
190     { 0 },
191 };
192
193 static struct gost_meth_minfo {
194     int nid;
195     EVP_PKEY_METHOD **pmeth;
196     EVP_PKEY_ASN1_METHOD **ameth;
197     const char *pemstr;
198     const char *info;
199 } gost_meth_array[] = {
200     {
201         NID_id_GostR3410_2001,
202         &pmeth_GostR3410_2001,
203         &ameth_GostR3410_2001,
204         "GOST2001",
205         "GOST R 34.10-2001",
206     },
207     {
208         NID_id_Gost28147_89_MAC,
209         &pmeth_Gost28147_MAC,
210         &ameth_Gost28147_MAC,
211         "GOST-MAC",
212         "GOST 28147-89 MAC",
213     },
214     {
215         NID_id_GostR3410_2012_256,
216         &pmeth_GostR3410_2012_256,
217         &ameth_GostR3410_2012_256,
218         "GOST2012_256",
219         "GOST R 34.10-2012 with 256 bit key",
220     },
221     {
222         NID_id_GostR3410_2012_512,
223         &pmeth_GostR3410_2012_512,
224         &ameth_GostR3410_2012_512,
225         "GOST2012_512",
226         "GOST R 34.10-2012 with 512 bit key",
227     },
228     {
229         NID_gost_mac_12,
230         &pmeth_Gost28147_MAC_12,
231         &ameth_Gost28147_MAC_12,
232         "GOST-MAC-12",
233         "GOST 28147-89 MAC with 2012 params",
234     },
235     {
236         NID_magma_mac,
237         &pmeth_magma_mac,
238         &ameth_magma_mac,
239         "MAGMA-MAC",
240         "GOST R 34.13-2015 Magma MAC",
241     },
242     {
243         NID_grasshopper_mac,
244         &pmeth_grasshopper_mac,
245         &ameth_grasshopper_mac,
246         "KUZNYECHIK-MAC",
247         "GOST R 34.13-2015 Grasshopper MAC",
248     },
249     {
250         NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
251         &pmeth_magma_mac_acpkm,
252         &ameth_magma_mac_acpkm,
253         "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
254         "GOST R 34.13-2015 Magma MAC ACPKM",
255     },
256     {
257         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
258         &pmeth_grasshopper_mac_acpkm,
259         &ameth_grasshopper_mac_acpkm,
260         "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
261         "GOST R 34.13-2015 Grasshopper MAC ACPKM",
262     },
263     { 0 },
264 };
265
266 #ifndef OSSL_NELEM
267 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
268 #endif
269
270 /* `- 1' because of terminating zero element */
271 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
272 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
273 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
274
275 static int gost_engine_init(ENGINE* e) {
276     return 1;
277 }
278
279 static int gost_engine_finish(ENGINE* e) {
280     return 1;
281 }
282
283 static int gost_engine_destroy(ENGINE* e) {
284     struct gost_digest_minfo *dinfo = gost_digest_array;
285     for (; dinfo->nid; dinfo++) {
286         if (dinfo->alias)
287             EVP_delete_digest_alias(dinfo->alias);
288         dinfo->destroy();
289     }
290
291     struct gost_cipher_minfo *cinfo = gost_cipher_array;
292     for (; cinfo->nid; cinfo++) {
293         if (cinfo->reg)
294             GOST_deinit_cipher(cinfo->reg);
295         else
296             EVP_CIPHER_meth_free((EVP_CIPHER *)cinfo->cipher());
297     }
298
299     //cipher_gost_grasshopper_destroy();
300     //wrap_ciphers_destroy();
301
302     gost_param_free();
303
304     struct gost_meth_minfo *minfo = gost_meth_array;
305     for (; minfo->nid; minfo++) {
306         *minfo->pmeth = NULL;
307         *minfo->ameth = NULL;
308     }
309
310     ERR_unload_GOST_strings();
311
312     return 1;
313 }
314
315 static int bind_gost(ENGINE* e, const char* id) {
316     int ret = 0;
317     if (id != NULL && strcmp(id, engine_gost_id) != 0)
318         return 0;
319     if (ameth_GostR3410_2001) {
320         printf("GOST engine already loaded\n");
321         goto end;
322     }
323     if (!ENGINE_set_id(e, engine_gost_id)) {
324         printf("ENGINE_set_id failed\n");
325         goto end;
326     }
327     if (!ENGINE_set_name(e, engine_gost_name)) {
328         printf("ENGINE_set_name failed\n");
329         goto end;
330     }
331     if (!ENGINE_set_digests(e, gost_digests)) {
332         printf("ENGINE_set_digests failed\n");
333         goto end;
334     }
335     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
336         printf("ENGINE_set_ciphers failed\n");
337         goto end;
338     }
339     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
340         printf("ENGINE_set_pkey_meths failed\n");
341         goto end;
342     }
343     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
344         printf("ENGINE_set_pkey_asn1_meths failed\n");
345         goto end;
346     }
347     /* Control function and commands */
348     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
349         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
350         goto end;
351     }
352     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
353         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
354         goto end;
355     }
356     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
357         || !ENGINE_set_init_function(e, gost_engine_init)
358         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
359         goto end;
360     }
361
362     struct gost_meth_minfo *minfo = gost_meth_array;
363     for (; minfo->nid; minfo++) {
364
365         /* This skip looks temporary. */
366         if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
367             continue;
368
369         if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
370                 minfo->info))
371             goto end;
372         if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
373             goto end;
374     }
375
376     if (!ENGINE_register_ciphers(e)
377         || !ENGINE_register_digests(e)
378         || !ENGINE_register_pkey_meths(e))
379         goto end;
380
381     struct gost_cipher_minfo *cinfo = gost_cipher_array;
382     for (; cinfo->nid; cinfo++) {
383         const EVP_CIPHER *cipher;
384
385         if (cinfo->reg)
386             cipher = GOST_init_cipher(cinfo->reg);
387         else
388             cipher = cinfo->cipher();
389         if (!EVP_add_cipher(cipher))
390             goto end;
391     }
392
393     struct gost_digest_minfo *dinfo = gost_digest_array;
394     for (; dinfo->nid; dinfo++) {
395         if (!EVP_add_digest(dinfo->digest()))
396             goto end;
397         if (dinfo->alias &&
398             !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
399             goto end;
400     }
401
402     ENGINE_register_all_complete();
403
404     ERR_load_GOST_strings();
405     ret = 1;
406     end:
407     return ret;
408 }
409
410 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
411 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
412     IMPLEMENT_DYNAMIC_CHECK_FN()
413 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
414
415 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
416 static int gost_digests(ENGINE *e, const EVP_MD **digest,
417                         const int **nids, int nid)
418 {
419     struct gost_digest_minfo *info = gost_digest_array;
420
421     if (!digest) {
422         int *n = known_digest_nids;
423
424         *nids = n;
425         for (; info->nid; info++)
426             *n++ = info->nid;
427         return OSSL_NELEM(known_digest_nids);
428     }
429
430     for (; info->nid; info++)
431         if (nid == info->nid) {
432             *digest = info->digest();
433             return 1;
434         }
435     *digest = NULL;
436     return 0;
437 }
438
439 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
440 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
441                         const int **nids, int nid)
442 {
443     struct gost_cipher_minfo *info = gost_cipher_array;
444
445     if (!cipher) {
446         int *n = known_cipher_nids;
447
448         *nids = n;
449         for (; info->nid; info++)
450             *n++ = info->nid;
451         return OSSL_NELEM(known_cipher_nids);
452     }
453
454     for (; info->nid; info++)
455         if (nid == info->nid) {
456             if (info->reg)
457                 *cipher = GOST_init_cipher(info->reg);
458             else
459                 *cipher = info->cipher();
460             return 1;
461         }
462     *cipher = NULL;
463     return 0;
464 }
465
466 static int gost_meth_nids(const int **nids)
467 {
468     struct gost_meth_minfo *info = gost_meth_array;
469     int *n = known_meths_nids;
470
471     *nids = n;
472     for (; info->nid; info++)
473         *n++ = info->nid;
474     return OSSL_NELEM(known_meths_nids);
475 }
476
477 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
478 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
479                            const int **nids, int nid)
480 {
481     struct gost_meth_minfo *info;
482
483     if (!pmeth)
484         return gost_meth_nids(nids);
485
486     for (info = gost_meth_array; info->nid; info++)
487         if (nid == info->nid) {
488             *pmeth = *info->pmeth;
489             return 1;
490         }
491     *pmeth = NULL;
492     return 0;
493 }
494
495 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
496 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
497                                 const int **nids, int nid)
498 {
499     struct gost_meth_minfo *info;
500
501     if (!ameth)
502         return gost_meth_nids(nids);
503
504     for (info = gost_meth_array; info->nid; info++)
505         if (nid == info->nid) {
506             *ameth = *info->ameth;
507             return 1;
508         }
509     *ameth = NULL;
510     return 0;
511 }
512
513 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
514
515 static ENGINE* engine_gost(void) {
516     ENGINE* ret = ENGINE_new();
517     if (!ret)
518         return NULL;
519     if (!bind_gost(ret, engine_gost_id)) {
520         ENGINE_free(ret);
521         return NULL;
522     }
523     return ret;
524 }
525
526 void ENGINE_load_gost(void) {
527     ENGINE* toadd;
528     if (pmeth_GostR3410_2001)
529         return;
530     toadd = engine_gost();
531     if (!toadd)
532         return;
533     ENGINE_add(toadd);
534     ENGINE_free(toadd);
535     ERR_clear_error();
536 }
537
538 #endif