]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Add magma-ecb mode. Fixes #410
[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 #include "gost-engine.h"
20
21 #include "gost_grasshopper_cipher.h"
22
23 static const char* engine_gost_id = "gost";
24
25 static const char* engine_gost_name =
26         "Reference implementation of GOST engine";
27
28 const ENGINE_CMD_DEFN gost_cmds[] = {
29     {GOST_CTRL_CRYPT_PARAMS,
30      "CRYPT_PARAMS",
31      "OID of default GOST 28147-89 parameters",
32      ENGINE_CMD_FLAG_STRING},
33     {GOST_CTRL_PBE_PARAMS,
34      "PBE_PARAMS",
35      "Shortname of default digest alg for PBE",
36      ENGINE_CMD_FLAG_STRING},
37     {GOST_CTRL_PK_FORMAT,
38      "GOST_PK_FORMAT",
39      "Private key format params",
40      ENGINE_CMD_FLAG_STRING},
41     {0, NULL, NULL, 0}
42 };
43
44 /* Symmetric cipher and digest function registrar */
45
46 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
47                         const int** nids, int nid);
48
49 static int gost_digests(ENGINE* e, const EVP_MD** digest,
50                         const int** nids, int nid);
51
52 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
53                            const int** nids, int nid);
54
55 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
56                                 const int** nids, int nid);
57
58 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
59         * pmeth_GostR3410_2001DH = NULL,
60         * pmeth_GostR3410_2012_256 = NULL,
61         * pmeth_GostR3410_2012_512 = NULL,
62         * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
63         * pmeth_magma_mac = NULL,  * pmeth_grasshopper_mac = NULL,
64         * pmeth_magma_mac_acpkm = NULL,  * pmeth_grasshopper_mac_acpkm = NULL;
65
66 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
67         * ameth_GostR3410_2001DH = NULL,
68         * ameth_GostR3410_2012_256 = NULL,
69         * ameth_GostR3410_2012_512 = NULL,
70         * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
71         * ameth_magma_mac = NULL,  * ameth_grasshopper_mac = NULL,
72         * ameth_magma_mac_acpkm = NULL,  * ameth_grasshopper_mac_acpkm = NULL;
73
74 GOST_digest *gost_digest_array[] = {
75     &GostR3411_94_digest,
76     &Gost28147_89_MAC_digest,
77     &GostR3411_2012_256_digest,
78     &GostR3411_2012_512_digest,
79     &Gost28147_89_mac_12_digest,
80     &magma_mac_digest,
81     &grasshopper_mac_digest,
82     &kuznyechik_ctracpkm_omac_digest,
83 };
84
85 GOST_cipher *gost_cipher_array[] = {
86     &Gost28147_89_cipher,
87     &Gost28147_89_cnt_cipher,
88     &Gost28147_89_cnt_12_cipher,
89     &Gost28147_89_cbc_cipher,
90     &grasshopper_ecb_cipher,
91     &grasshopper_cbc_cipher,
92     &grasshopper_cfb_cipher,
93     &grasshopper_ofb_cipher,
94     &grasshopper_ctr_cipher,
95     &magma_ecb_cipher,
96     &magma_cbc_cipher,
97     &magma_ctr_cipher,
98     &magma_ctr_acpkm_cipher,
99     &magma_ctr_acpkm_omac_cipher,
100     &grasshopper_ctr_acpkm_cipher,
101     &grasshopper_ctr_acpkm_omac_cipher,
102     &magma_kexp15_cipher,
103     &kuznyechik_kexp15_cipher,
104 };
105
106 static struct gost_meth_minfo {
107     int nid;
108     EVP_PKEY_METHOD **pmeth;
109     EVP_PKEY_ASN1_METHOD **ameth;
110     const char *pemstr;
111     const char *info;
112 } gost_meth_array[] = {
113     {
114         NID_id_GostR3410_2001,
115         &pmeth_GostR3410_2001,
116         &ameth_GostR3410_2001,
117         "GOST2001",
118         "GOST R 34.10-2001",
119     },
120     {
121         NID_id_GostR3410_2001DH,
122         &pmeth_GostR3410_2001DH,
123         &ameth_GostR3410_2001DH,
124         "GOST2001 DH",
125         "GOST R 34.10-2001 DH",
126     },
127     {
128         NID_id_Gost28147_89_MAC,
129         &pmeth_Gost28147_MAC,
130         &ameth_Gost28147_MAC,
131         "GOST-MAC",
132         "GOST 28147-89 MAC",
133     },
134     {
135         NID_id_GostR3410_2012_256,
136         &pmeth_GostR3410_2012_256,
137         &ameth_GostR3410_2012_256,
138         "GOST2012_256",
139         "GOST R 34.10-2012 with 256 bit key",
140     },
141     {
142         NID_id_GostR3410_2012_512,
143         &pmeth_GostR3410_2012_512,
144         &ameth_GostR3410_2012_512,
145         "GOST2012_512",
146         "GOST R 34.10-2012 with 512 bit key",
147     },
148     {
149         NID_gost_mac_12,
150         &pmeth_Gost28147_MAC_12,
151         &ameth_Gost28147_MAC_12,
152         "GOST-MAC-12",
153         "GOST 28147-89 MAC with 2012 params",
154     },
155     {
156         NID_magma_mac,
157         &pmeth_magma_mac,
158         &ameth_magma_mac,
159         "MAGMA-MAC",
160         "GOST R 34.13-2015 Magma MAC",
161     },
162     {
163         NID_grasshopper_mac,
164         &pmeth_grasshopper_mac,
165         &ameth_grasshopper_mac,
166         "KUZNYECHIK-MAC",
167         "GOST R 34.13-2015 Grasshopper MAC",
168     },
169     {
170         NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
171         &pmeth_magma_mac_acpkm,
172         &ameth_magma_mac_acpkm,
173         "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
174         "GOST R 34.13-2015 Magma MAC ACPKM",
175     },
176     {
177         NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
178         &pmeth_grasshopper_mac_acpkm,
179         &ameth_grasshopper_mac_acpkm,
180         "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
181         "GOST R 34.13-2015 Grasshopper MAC ACPKM",
182     },
183     { 0 },
184 };
185
186 #ifndef OSSL_NELEM
187 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
188 #endif
189
190 static int known_digest_nids[OSSL_NELEM(gost_digest_array)];
191 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
192 /* `- 1' because of terminating zero element */
193 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
194
195 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
196 static int gost_digests(ENGINE *e, const EVP_MD **digest,
197                         const int **nids, int nid)
198 {
199     int i;
200
201     if (!digest) {
202         int *n = known_digest_nids;
203
204         *nids = n;
205         for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
206             *n++ = gost_digest_array[i]->nid;
207         return i;
208     }
209
210     for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
211         if (nid == gost_digest_array[i]->nid) {
212             *digest = GOST_init_digest(gost_digest_array[i]);
213             return 1;
214         }
215     *digest = NULL;
216     return 0;
217 }
218
219 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
220 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
221                         const int **nids, int nid)
222 {
223     int i;
224
225     if (!cipher) {
226         int *n = known_cipher_nids;
227
228         *nids = n;
229         for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
230             *n++ = gost_cipher_array[i]->nid;
231         return i;
232     }
233
234     for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
235         if (nid == gost_cipher_array[i]->nid) {
236             *cipher = GOST_init_cipher(gost_cipher_array[i]);
237             return 1;
238         }
239     *cipher = NULL;
240     return 0;
241 }
242
243 static int gost_meth_nids(const int **nids)
244 {
245     struct gost_meth_minfo *info = gost_meth_array;
246     int *n = known_meths_nids;
247
248     *nids = n;
249     for (; info->nid; info++)
250         *n++ = info->nid;
251     return OSSL_NELEM(known_meths_nids);
252 }
253
254 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
255 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
256                            const int **nids, int nid)
257 {
258     struct gost_meth_minfo *info;
259
260     if (!pmeth)
261         return gost_meth_nids(nids);
262
263     for (info = gost_meth_array; info->nid; info++)
264         if (nid == info->nid) {
265             *pmeth = *info->pmeth;
266             return 1;
267         }
268     *pmeth = NULL;
269     return 0;
270 }
271
272 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
273 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
274                                 const int **nids, int nid)
275 {
276     struct gost_meth_minfo *info;
277
278     if (!ameth)
279         return gost_meth_nids(nids);
280
281     for (info = gost_meth_array; info->nid; info++)
282         if (nid == info->nid) {
283             *ameth = *info->ameth;
284             return 1;
285         }
286     *ameth = NULL;
287     return 0;
288 }
289
290 static int gost_engine_init(ENGINE* e) {
291     return 1;
292 }
293
294 static int gost_engine_finish(ENGINE* e) {
295     return 1;
296 }
297
298 static int gost_engine_destroy(ENGINE* e) {
299     int i;
300
301     for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
302         GOST_deinit_digest(gost_digest_array[i]);
303     for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
304         GOST_deinit_cipher(gost_cipher_array[i]);
305
306     gost_param_free();
307
308     struct gost_meth_minfo *minfo = gost_meth_array;
309     for (; minfo->nid; minfo++) {
310         *minfo->pmeth = NULL;
311         *minfo->ameth = NULL;
312     }
313
314     free_cached_groups();
315
316 # ifndef BUILDING_GOST_PROVIDER
317     ERR_unload_GOST_strings();
318 # endif
319
320     return 1;
321 }
322
323 /*
324  * Following is the glue that populates the ENGINE structure and that
325  * binds it to OpenSSL libraries
326  */
327
328 # ifndef BUILDING_GOST_PROVIDER
329 static
330 # endif
331 int populate_gost_engine(ENGINE* e) {
332     int ret = 0;
333
334     if (e == NULL)
335         goto end;
336     if (!ENGINE_set_id(e, engine_gost_id)) {
337         fprintf(stderr, "ENGINE_set_id failed\n");
338         goto end;
339     }
340     if (!ENGINE_set_name(e, engine_gost_name)) {
341         fprintf(stderr, "ENGINE_set_name failed\n");
342         goto end;
343     }
344     if (!ENGINE_set_digests(e, gost_digests)) {
345         fprintf(stderr, "ENGINE_set_digests failed\n");
346         goto end;
347     }
348     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
349         fprintf(stderr, "ENGINE_set_ciphers failed\n");
350         goto end;
351     }
352     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
353         fprintf(stderr, "ENGINE_set_pkey_meths failed\n");
354         goto end;
355     }
356     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
357         fprintf(stderr, "ENGINE_set_pkey_asn1_meths failed\n");
358         goto end;
359     }
360     /* Control function and commands */
361     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
362         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
363         goto end;
364     }
365     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
366         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
367         goto end;
368     }
369     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
370         || !ENGINE_set_init_function(e, gost_engine_init)
371         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
372         goto end;
373     }
374
375     /*
376      * "register" in "register_ameth_gost" and "register_pmeth_gost" is
377      * not registering in an ENGINE sense, where things are hooked into
378      * OpenSSL's library.  "register_ameth_gost" and "register_pmeth_gost"
379      * merely allocate and populate the method structures of this engine.
380      */
381     struct gost_meth_minfo *minfo = gost_meth_array;
382     for (; minfo->nid; minfo++) {
383
384         /* This skip looks temporary. */
385         if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
386             continue;
387
388         if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
389                 minfo->info))
390             goto end;
391         if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
392             goto end;
393     }
394
395     ret = 1;
396   end:
397     return ret;
398 }
399
400 #ifndef BUILDING_GOST_PROVIDER
401 static int bind_gost_engine(ENGINE* e) {
402     int ret = 0;
403
404     if (!ENGINE_register_ciphers(e)
405         || !ENGINE_register_digests(e)
406         || !ENGINE_register_pkey_meths(e))
407         goto end;
408
409     int i;
410     for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
411         if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
412             goto end;
413     }
414
415     for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
416         if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
417             goto end;
418     }
419
420     ENGINE_register_all_complete();
421
422     ERR_load_GOST_strings();
423     ret = 1;
424   end:
425     return ret;
426 }
427
428 static int check_gost_engine(ENGINE* e, const char* id)
429 {
430     if (id != NULL && strcmp(id, engine_gost_id) != 0)
431         return 0;
432     if (ameth_GostR3410_2001) {
433         printf("GOST engine already loaded\n");
434         return 0;
435     }
436     return 1;
437 }
438
439 static int make_gost_engine(ENGINE* e, const char* id)
440 {
441     return check_gost_engine(e, id)
442         && populate_gost_engine(e)
443         && bind_gost_engine(e);
444 }
445
446 #ifndef BUILDING_ENGINE_AS_LIBRARY
447
448 /*
449  * When building gost-engine as a dynamically loadable module, these two
450  * lines do everything that's needed, and OpenSSL's libcrypto will be able
451  * to call its entry points, v_check and bind_engine.
452  */
453
454 IMPLEMENT_DYNAMIC_BIND_FN(make_gost_engine)
455 IMPLEMENT_DYNAMIC_CHECK_FN()
456
457 #else
458
459 /*
460  * When building gost-engine as a shared library, the application that uses
461  * it must manually call ENGINE_load_gost() for it to bind itself into the
462  * libcrypto libraries.
463  */
464 void ENGINE_load_gost(void) {
465     ENGINE* toadd;
466     int ret = 0;
467
468     if ((toadd = ENGINE_new()) != NULL
469         && (ret = make_gost_engine(toadd, engine_gost_id)) > 0)
470         ENGINE_add(toadd);
471     ENGINE_free(toadd);
472     if (ret > 0)
473         ERR_clear_error();
474 }
475 #endif
476 #endif
477 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */