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