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