1 /**********************************************************************
3 * Main file of GOST engine *
5 * Copyright (c) 2005-2006 Cryptocom LTD *
6 * Copyright (c) 2020 Chikunov Vitaly <vt@altlinux.org> *
8 * This file is distributed under the same license as OpenSSL *
10 **********************************************************************/
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"
19 #include "gost-engine.h"
21 #include "gost_grasshopper_cipher.h"
23 static const char* engine_gost_id = "gost";
25 static const char* engine_gost_name =
26 "Reference implementation of GOST engine";
28 const ENGINE_CMD_DEFN gost_cmds[] = {
29 {GOST_CTRL_CRYPT_PARAMS,
31 "OID of default GOST 28147-89 parameters",
32 ENGINE_CMD_FLAG_STRING},
33 {GOST_CTRL_PBE_PARAMS,
35 "Shortname of default digest alg for PBE",
36 ENGINE_CMD_FLAG_STRING},
39 "Private key format params",
40 ENGINE_CMD_FLAG_STRING},
44 /* Symmetric cipher and digest function registrar */
46 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
47 const int** nids, int nid);
49 static int gost_digests(ENGINE* e, const EVP_MD** digest,
50 const int** nids, int nid);
52 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
53 const int** nids, int nid);
55 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
56 const int** nids, int nid);
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;
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;
74 GOST_digest *gost_digest_array[] = {
76 &Gost28147_89_MAC_digest,
77 &GostR3411_2012_256_digest,
78 &GostR3411_2012_512_digest,
79 &Gost28147_89_mac_12_digest,
81 &grasshopper_mac_digest,
82 &kuznyechik_ctracpkm_omac_digest,
85 GOST_cipher *gost_cipher_array[] = {
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,
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,
106 static struct gost_meth_minfo {
108 EVP_PKEY_METHOD **pmeth;
109 EVP_PKEY_ASN1_METHOD **ameth;
112 } gost_meth_array[] = {
114 NID_id_GostR3410_2001,
115 &pmeth_GostR3410_2001,
116 &ameth_GostR3410_2001,
121 NID_id_GostR3410_2001DH,
122 &pmeth_GostR3410_2001DH,
123 &ameth_GostR3410_2001DH,
125 "GOST R 34.10-2001 DH",
128 NID_id_Gost28147_89_MAC,
129 &pmeth_Gost28147_MAC,
130 &ameth_Gost28147_MAC,
135 NID_id_GostR3410_2012_256,
136 &pmeth_GostR3410_2012_256,
137 &ameth_GostR3410_2012_256,
139 "GOST R 34.10-2012 with 256 bit key",
142 NID_id_GostR3410_2012_512,
143 &pmeth_GostR3410_2012_512,
144 &ameth_GostR3410_2012_512,
146 "GOST R 34.10-2012 with 512 bit key",
150 &pmeth_Gost28147_MAC_12,
151 &ameth_Gost28147_MAC_12,
153 "GOST 28147-89 MAC with 2012 params",
160 "GOST R 34.13-2015 Magma MAC",
164 &pmeth_grasshopper_mac,
165 &ameth_grasshopper_mac,
167 "GOST R 34.13-2015 Grasshopper MAC",
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",
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",
187 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
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];
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)
202 int *n = known_digest_nids;
205 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
206 *n++ = gost_digest_array[i]->nid;
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]);
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)
226 int *n = known_cipher_nids;
229 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
230 *n++ = gost_cipher_array[i]->nid;
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]);
243 static int gost_meth_nids(const int **nids)
245 struct gost_meth_minfo *info = gost_meth_array;
246 int *n = known_meths_nids;
249 for (; info->nid; info++)
251 return OSSL_NELEM(known_meths_nids);
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)
258 struct gost_meth_minfo *info;
261 return gost_meth_nids(nids);
263 for (info = gost_meth_array; info->nid; info++)
264 if (nid == info->nid) {
265 *pmeth = *info->pmeth;
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)
276 struct gost_meth_minfo *info;
279 return gost_meth_nids(nids);
281 for (info = gost_meth_array; info->nid; info++)
282 if (nid == info->nid) {
283 *ameth = *info->ameth;
290 static int gost_engine_init(ENGINE* e) {
294 static int gost_engine_finish(ENGINE* e) {
298 static int gost_engine_destroy(ENGINE* e) {
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]);
308 struct gost_meth_minfo *minfo = gost_meth_array;
309 for (; minfo->nid; minfo++) {
310 *minfo->pmeth = NULL;
311 *minfo->ameth = NULL;
314 free_cached_groups();
316 # ifndef BUILDING_GOST_PROVIDER
317 ERR_unload_GOST_strings();
324 * Following is the glue that populates the ENGINE structure and that
325 * binds it to OpenSSL libraries
328 # ifndef BUILDING_GOST_PROVIDER
331 int populate_gost_engine(ENGINE* e) {
336 if (!ENGINE_set_id(e, engine_gost_id)) {
337 fprintf(stderr, "ENGINE_set_id failed\n");
340 if (!ENGINE_set_name(e, engine_gost_name)) {
341 fprintf(stderr, "ENGINE_set_name failed\n");
344 if (!ENGINE_set_digests(e, gost_digests)) {
345 fprintf(stderr, "ENGINE_set_digests failed\n");
348 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
349 fprintf(stderr, "ENGINE_set_ciphers failed\n");
352 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
353 fprintf(stderr, "ENGINE_set_pkey_meths failed\n");
356 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
357 fprintf(stderr, "ENGINE_set_pkey_asn1_meths failed\n");
360 /* Control function and commands */
361 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
362 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
365 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
366 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
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)) {
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.
381 struct gost_meth_minfo *minfo = gost_meth_array;
382 for (; minfo->nid; minfo++) {
384 /* This skip looks temporary. */
385 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
388 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
391 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
400 #ifndef BUILDING_GOST_PROVIDER
401 static int bind_gost_engine(ENGINE* e) {
404 if (!ENGINE_register_ciphers(e)
405 || !ENGINE_register_digests(e)
406 || !ENGINE_register_pkey_meths(e))
410 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
411 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
415 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
416 if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
420 ENGINE_register_all_complete();
422 ERR_load_GOST_strings();
428 static int check_gost_engine(ENGINE* e, const char* id)
430 if (id != NULL && strcmp(id, engine_gost_id) != 0)
432 if (ameth_GostR3410_2001) {
433 printf("GOST engine already loaded\n");
439 static int make_gost_engine(ENGINE* e, const char* id)
441 return check_gost_engine(e, id)
442 && populate_gost_engine(e)
443 && bind_gost_engine(e);
446 #ifndef BUILDING_ENGINE_AS_LIBRARY
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.
454 IMPLEMENT_DYNAMIC_BIND_FN(make_gost_engine)
455 IMPLEMENT_DYNAMIC_CHECK_FN()
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.
464 void ENGINE_load_gost(void) {
468 if ((toadd = ENGINE_new()) != NULL
469 && (ret = make_gost_engine(toadd, engine_gost_id)) > 0)
477 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */