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