]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
gost_crypt: Rework cipher registration, add Gost28147_89_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         cipher_gost_cpacnt,
122     },
123     {
124         NID_gost89_cnt_12,
125         cipher_gost_cpcnt_12,
126     },
127     {
128         NID_gost89_cbc,
129         cipher_gost_cbc,
130     },
131     {
132         NID_grasshopper_ecb,
133         cipher_gost_grasshopper_ecb,
134     },
135     {
136         NID_grasshopper_cbc,
137         cipher_gost_grasshopper_cbc,
138     },
139     {
140         NID_grasshopper_cfb,
141         cipher_gost_grasshopper_cfb,
142     },
143     {
144         NID_grasshopper_ofb,
145         cipher_gost_grasshopper_ofb,
146     },
147     {
148         NID_grasshopper_ctr,
149         cipher_gost_grasshopper_ctr,
150     },
151     {
152         NID_magma_cbc,
153         cipher_magma_cbc,
154     },
155     {
156         NID_magma_ctr,
157         cipher_magma_ctr,
158     },
159     {
160         NID_magma_ctr_acpkm,
161         cipher_magma_ctr_acpkm,
162     },
163     {
164         NID_magma_ctr_acpkm_omac,
165         cipher_magma_ctr_acpkm_omac,
166     },
167     {
168         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
169         cipher_gost_grasshopper_ctracpkm,
170     },
171     {
172         NID_kuznyechik_ctr_acpkm_omac,
173         cipher_gost_grasshopper_ctracpkm_omac,
174     },
175     {
176         NID_magma_kexp15,
177         cipher_magma_wrap,
178     },
179     {
180         NID_kuznyechik_kexp15,
181         cipher_kuznyechik_wrap,
182     },
183     { 0 },
184 };
185
186 static struct gost_meth_minfo {
187     int nid;
188     EVP_PKEY_METHOD **pmeth;
189     EVP_PKEY_ASN1_METHOD **ameth;
190     const char *pemstr;
191     const char *info;
192 } gost_meth_array[] = {
193     {
194         NID_id_GostR3410_2001,
195         &pmeth_GostR3410_2001,
196         &ameth_GostR3410_2001,
197         "GOST2001",
198         "GOST R 34.10-2001",
199     },
200     {
201         NID_id_Gost28147_89_MAC,
202         &pmeth_Gost28147_MAC,
203         &ameth_Gost28147_MAC,
204         "GOST-MAC",
205         "GOST 28147-89 MAC",
206     },
207     {
208         NID_id_GostR3410_2012_256,
209         &pmeth_GostR3410_2012_256,
210         &ameth_GostR3410_2012_256,
211         "GOST2012_256",
212         "GOST R 34.10-2012 with 256 bit key",
213     },
214     {
215         NID_id_GostR3410_2012_512,
216         &pmeth_GostR3410_2012_512,
217         &ameth_GostR3410_2012_512,
218         "GOST2012_512",
219         "GOST R 34.10-2012 with 512 bit key",
220     },
221     {
222         NID_gost_mac_12,
223         &pmeth_Gost28147_MAC_12,
224         &ameth_Gost28147_MAC_12,
225         "GOST-MAC-12",
226         "GOST 28147-89 MAC with 2012 params",
227     },
228     {
229         NID_magma_mac,
230         &pmeth_magma_mac,
231         &ameth_magma_mac,
232         "MAGMA-MAC",
233         "GOST R 34.13-2015 Magma MAC",
234     },
235     {
236         NID_grasshopper_mac,
237         &pmeth_grasshopper_mac,
238         &ameth_grasshopper_mac,
239         "KUZNYECHIK-MAC",
240         "GOST R 34.13-2015 Grasshopper MAC",
241     },
242     {
243         NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
244         &pmeth_magma_mac_acpkm,
245         &ameth_magma_mac_acpkm,
246         "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
247         "GOST R 34.13-2015 Magma MAC ACPKM",
248     },
249     {
250         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
251         &pmeth_grasshopper_mac_acpkm,
252         &ameth_grasshopper_mac_acpkm,
253         "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
254         "GOST R 34.13-2015 Grasshopper MAC ACPKM",
255     },
256     { 0 },
257 };
258
259 #ifndef OSSL_NELEM
260 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
261 #endif
262
263 /* `- 1' because of terminating zero element */
264 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
265 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
266 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
267
268 static int gost_engine_init(ENGINE* e) {
269     return 1;
270 }
271
272 static int gost_engine_finish(ENGINE* e) {
273     return 1;
274 }
275
276 static int gost_engine_destroy(ENGINE* e) {
277     struct gost_digest_minfo *dinfo = gost_digest_array;
278     for (; dinfo->nid; dinfo++) {
279         if (dinfo->alias)
280             EVP_delete_digest_alias(dinfo->alias);
281         dinfo->destroy();
282     }
283
284     struct gost_cipher_minfo *cinfo = gost_cipher_array;
285     for (; cinfo->nid; cinfo++) {
286         if (cinfo->reg)
287             GOST_deinit_cipher(cinfo->reg);
288         else
289             EVP_CIPHER_meth_free((EVP_CIPHER *)cinfo->cipher());
290     }
291
292     //cipher_gost_grasshopper_destroy();
293     //wrap_ciphers_destroy();
294
295     gost_param_free();
296
297     struct gost_meth_minfo *minfo = gost_meth_array;
298     for (; minfo->nid; minfo++) {
299         *minfo->pmeth = NULL;
300         *minfo->ameth = NULL;
301     }
302
303     ERR_unload_GOST_strings();
304
305     return 1;
306 }
307
308 static int bind_gost(ENGINE* e, const char* id) {
309     int ret = 0;
310     if (id != NULL && strcmp(id, engine_gost_id) != 0)
311         return 0;
312     if (ameth_GostR3410_2001) {
313         printf("GOST engine already loaded\n");
314         goto end;
315     }
316     if (!ENGINE_set_id(e, engine_gost_id)) {
317         printf("ENGINE_set_id failed\n");
318         goto end;
319     }
320     if (!ENGINE_set_name(e, engine_gost_name)) {
321         printf("ENGINE_set_name failed\n");
322         goto end;
323     }
324     if (!ENGINE_set_digests(e, gost_digests)) {
325         printf("ENGINE_set_digests failed\n");
326         goto end;
327     }
328     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
329         printf("ENGINE_set_ciphers failed\n");
330         goto end;
331     }
332     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
333         printf("ENGINE_set_pkey_meths failed\n");
334         goto end;
335     }
336     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
337         printf("ENGINE_set_pkey_asn1_meths failed\n");
338         goto end;
339     }
340     /* Control function and commands */
341     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
342         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
343         goto end;
344     }
345     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
346         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
347         goto end;
348     }
349     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
350         || !ENGINE_set_init_function(e, gost_engine_init)
351         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
352         goto end;
353     }
354
355     struct gost_meth_minfo *minfo = gost_meth_array;
356     for (; minfo->nid; minfo++) {
357
358         /* This skip looks temporary. */
359         if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
360             continue;
361
362         if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
363                 minfo->info))
364             goto end;
365         if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
366             goto end;
367     }
368
369     if (!ENGINE_register_ciphers(e)
370         || !ENGINE_register_digests(e)
371         || !ENGINE_register_pkey_meths(e))
372         goto end;
373
374     struct gost_cipher_minfo *cinfo = gost_cipher_array;
375     for (; cinfo->nid; cinfo++) {
376         const EVP_CIPHER *cipher;
377
378         if (cinfo->reg)
379             cipher = GOST_init_cipher(cinfo->reg);
380         else
381             cipher = cinfo->cipher();
382         if (!EVP_add_cipher(cipher))
383             goto end;
384     }
385
386     struct gost_digest_minfo *dinfo = gost_digest_array;
387     for (; dinfo->nid; dinfo++) {
388         if (!EVP_add_digest(dinfo->digest()))
389             goto end;
390         if (dinfo->alias &&
391             !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
392             goto end;
393     }
394
395     ENGINE_register_all_complete();
396
397     ERR_load_GOST_strings();
398     ret = 1;
399     end:
400     return ret;
401 }
402
403 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
404 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
405     IMPLEMENT_DYNAMIC_CHECK_FN()
406 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
407
408 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
409 static int gost_digests(ENGINE *e, const EVP_MD **digest,
410                         const int **nids, int nid)
411 {
412     struct gost_digest_minfo *info = gost_digest_array;
413
414     if (!digest) {
415         int *n = known_digest_nids;
416
417         *nids = n;
418         for (; info->nid; info++)
419             *n++ = info->nid;
420         return OSSL_NELEM(known_digest_nids);
421     }
422
423     for (; info->nid; info++)
424         if (nid == info->nid) {
425             *digest = info->digest();
426             return 1;
427         }
428     *digest = NULL;
429     return 0;
430 }
431
432 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
433 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
434                         const int **nids, int nid)
435 {
436     struct gost_cipher_minfo *info = gost_cipher_array;
437
438     if (!cipher) {
439         int *n = known_cipher_nids;
440
441         *nids = n;
442         for (; info->nid; info++)
443             *n++ = info->nid;
444         return OSSL_NELEM(known_cipher_nids);
445     }
446
447     for (; info->nid; info++)
448         if (nid == info->nid) {
449             if (info->reg)
450                 *cipher = GOST_init_cipher(info->reg);
451             else
452                 *cipher = info->cipher();
453             return 1;
454         }
455     *cipher = NULL;
456     return 0;
457 }
458
459 static int gost_meth_nids(const int **nids)
460 {
461     struct gost_meth_minfo *info = gost_meth_array;
462     int *n = known_meths_nids;
463
464     *nids = n;
465     for (; info->nid; info++)
466         *n++ = info->nid;
467     return OSSL_NELEM(known_meths_nids);
468 }
469
470 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
471 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
472                            const int **nids, int nid)
473 {
474     struct gost_meth_minfo *info;
475
476     if (!pmeth)
477         return gost_meth_nids(nids);
478
479     for (info = gost_meth_array; info->nid; info++)
480         if (nid == info->nid) {
481             *pmeth = *info->pmeth;
482             return 1;
483         }
484     *pmeth = NULL;
485     return 0;
486 }
487
488 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
489 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
490                                 const int **nids, int nid)
491 {
492     struct gost_meth_minfo *info;
493
494     if (!ameth)
495         return gost_meth_nids(nids);
496
497     for (info = gost_meth_array; info->nid; info++)
498         if (nid == info->nid) {
499             *ameth = *info->ameth;
500             return 1;
501         }
502     *ameth = NULL;
503     return 0;
504 }
505
506 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
507
508 static ENGINE* engine_gost(void) {
509     ENGINE* ret = ENGINE_new();
510     if (!ret)
511         return NULL;
512     if (!bind_gost(ret, engine_gost_id)) {
513         ENGINE_free(ret);
514         return NULL;
515     }
516     return ret;
517 }
518
519 void ENGINE_load_gost(void) {
520     ENGINE* toadd;
521     if (pmeth_GostR3410_2001)
522         return;
523     toadd = engine_gost();
524     if (!toadd)
525         return;
526     ENGINE_add(toadd);
527     ENGINE_free(toadd);
528     ERR_clear_error();
529 }
530
531 #endif