]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
engine: create missing NIDs
[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 void free_NIDs();
299
300 static int gost_engine_destroy(ENGINE* e) {
301     int i;
302
303     for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
304         GOST_deinit_digest(gost_digest_array[i]);
305     for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
306         GOST_deinit_cipher(gost_cipher_array[i]);
307
308     gost_param_free();
309
310     struct gost_meth_minfo *minfo = gost_meth_array;
311     for (; minfo->nid; minfo++) {
312         *minfo->pmeth = NULL;
313         *minfo->ameth = NULL;
314     }
315
316     free_cached_groups();
317     free_NIDs();
318
319 # ifndef BUILDING_GOST_PROVIDER
320     ERR_unload_GOST_strings();
321 # endif
322
323     return 1;
324 }
325
326 /*
327  * Following is the glue that populates the ENGINE structure and that
328  * binds it to OpenSSL libraries
329  */
330
331 static GOST_NID_JOB *missing_NIDs[] = {
332 };
333
334 static int create_NIDs() {
335     int i;
336     int new_nid = OBJ_new_nid(OSSL_NELEM(missing_NIDs));
337     for (i = 0; i < OSSL_NELEM(missing_NIDs); i++) {
338         GOST_NID_JOB *job = missing_NIDs[i];
339         ASN1_OBJECT *obj =
340             ASN1_OBJECT_create(new_nid + i, NULL, 0, job->sn, job->ln);
341         job->asn1 = obj;
342         if (!obj || OBJ_add_object(obj) == NID_undef) {
343             OPENSSL_free(obj);
344             return 0;
345         }
346         (*missing_NIDs[i]->callback)(new_nid + i);
347     }
348     return 1;
349 }
350
351 static void free_NIDs() {
352     int i;
353     for (i = 0; i < OSSL_NELEM(missing_NIDs); i++) {
354         ASN1_OBJECT_free(missing_NIDs[i]->asn1);
355     }
356 }
357
358 # ifndef BUILDING_GOST_PROVIDER
359 static
360 # endif
361 int populate_gost_engine(ENGINE* e) {
362     int ret = 0;
363
364     if (e == NULL)
365         goto end;
366     if (!ENGINE_set_id(e, engine_gost_id)) {
367         fprintf(stderr, "ENGINE_set_id failed\n");
368         goto end;
369     }
370     if (!ENGINE_set_name(e, engine_gost_name)) {
371         fprintf(stderr, "ENGINE_set_name failed\n");
372         goto end;
373     }
374     if (!create_NIDs()) {
375         fprintf(stderr, "NID creation failed\n");
376         goto end;
377     }
378     if (!ENGINE_set_digests(e, gost_digests)) {
379         fprintf(stderr, "ENGINE_set_digests failed\n");
380         goto end;
381     }
382     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
383         fprintf(stderr, "ENGINE_set_ciphers failed\n");
384         goto end;
385     }
386     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
387         fprintf(stderr, "ENGINE_set_pkey_meths failed\n");
388         goto end;
389     }
390     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
391         fprintf(stderr, "ENGINE_set_pkey_asn1_meths failed\n");
392         goto end;
393     }
394     /* Control function and commands */
395     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
396         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
397         goto end;
398     }
399     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
400         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
401         goto end;
402     }
403     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
404         || !ENGINE_set_init_function(e, gost_engine_init)
405         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
406         goto end;
407     }
408
409     /*
410      * "register" in "register_ameth_gost" and "register_pmeth_gost" is
411      * not registering in an ENGINE sense, where things are hooked into
412      * OpenSSL's library.  "register_ameth_gost" and "register_pmeth_gost"
413      * merely allocate and populate the method structures of this engine.
414      */
415     struct gost_meth_minfo *minfo = gost_meth_array;
416     for (; minfo->nid; minfo++) {
417
418         /* This skip looks temporary. */
419         if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
420             continue;
421
422         if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
423                 minfo->info))
424             goto end;
425         if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
426             goto end;
427     }
428
429     ret = 1;
430   end:
431     return ret;
432 }
433
434 #ifndef BUILDING_GOST_PROVIDER
435 static int bind_gost_engine(ENGINE* e) {
436     int ret = 0;
437
438     if (!ENGINE_register_ciphers(e)
439         || !ENGINE_register_digests(e)
440         || !ENGINE_register_pkey_meths(e))
441         goto end;
442
443     int i;
444     for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
445         if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
446             goto end;
447     }
448
449     for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
450         if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
451             goto end;
452     }
453
454     ENGINE_register_all_complete();
455
456     ERR_load_GOST_strings();
457     ret = 1;
458   end:
459     return ret;
460 }
461
462 static int check_gost_engine(ENGINE* e, const char* id)
463 {
464     if (id != NULL && strcmp(id, engine_gost_id) != 0)
465         return 0;
466     if (ameth_GostR3410_2001) {
467         printf("GOST engine already loaded\n");
468         return 0;
469     }
470     return 1;
471 }
472
473 static int make_gost_engine(ENGINE* e, const char* id)
474 {
475     return check_gost_engine(e, id)
476         && populate_gost_engine(e)
477         && bind_gost_engine(e);
478 }
479
480 #ifndef BUILDING_ENGINE_AS_LIBRARY
481
482 /*
483  * When building gost-engine as a dynamically loadable module, these two
484  * lines do everything that's needed, and OpenSSL's libcrypto will be able
485  * to call its entry points, v_check and bind_engine.
486  */
487
488 IMPLEMENT_DYNAMIC_BIND_FN(make_gost_engine)
489 IMPLEMENT_DYNAMIC_CHECK_FN()
490
491 #else
492
493 /*
494  * When building gost-engine as a shared library, the application that uses
495  * it must manually call ENGINE_load_gost() for it to bind itself into the
496  * libcrypto libraries.
497  */
498 void ENGINE_load_gost(void) {
499     ENGINE* toadd;
500     int ret = 0;
501
502     if ((toadd = ENGINE_new()) != NULL
503         && (ret = make_gost_engine(toadd, engine_gost_id)) > 0)
504         ENGINE_add(toadd);
505     ENGINE_free(toadd);
506     if (ret > 0)
507         ERR_clear_error();
508 }
509 #endif
510 #endif
511 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */