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