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