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