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,
97 &magma_ctr_acpkm_cipher,
98 &magma_ctr_acpkm_omac_cipher,
99 &grasshopper_ctr_acpkm_cipher,
100 &grasshopper_ctr_acpkm_omac_cipher,
101 &magma_kexp15_cipher,
102 &kuznyechik_kexp15_cipher,
105 static struct gost_meth_minfo {
107 EVP_PKEY_METHOD **pmeth;
108 EVP_PKEY_ASN1_METHOD **ameth;
111 } gost_meth_array[] = {
113 NID_id_GostR3410_2001,
114 &pmeth_GostR3410_2001,
115 &ameth_GostR3410_2001,
120 NID_id_GostR3410_2001DH,
121 &pmeth_GostR3410_2001DH,
122 &ameth_GostR3410_2001DH,
124 "GOST R 34.10-2001 DH",
127 NID_id_Gost28147_89_MAC,
128 &pmeth_Gost28147_MAC,
129 &ameth_Gost28147_MAC,
134 NID_id_GostR3410_2012_256,
135 &pmeth_GostR3410_2012_256,
136 &ameth_GostR3410_2012_256,
138 "GOST R 34.10-2012 with 256 bit key",
141 NID_id_GostR3410_2012_512,
142 &pmeth_GostR3410_2012_512,
143 &ameth_GostR3410_2012_512,
145 "GOST R 34.10-2012 with 512 bit key",
149 &pmeth_Gost28147_MAC_12,
150 &ameth_Gost28147_MAC_12,
152 "GOST 28147-89 MAC with 2012 params",
159 "GOST R 34.13-2015 Magma MAC",
163 &pmeth_grasshopper_mac,
164 &ameth_grasshopper_mac,
166 "GOST R 34.13-2015 Grasshopper MAC",
169 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
170 &pmeth_magma_mac_acpkm,
171 &ameth_magma_mac_acpkm,
172 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
173 "GOST R 34.13-2015 Magma MAC ACPKM",
176 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
177 &pmeth_grasshopper_mac_acpkm,
178 &ameth_grasshopper_mac_acpkm,
179 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
180 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
186 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
189 static int known_digest_nids[OSSL_NELEM(gost_digest_array)];
190 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
191 /* `- 1' because of terminating zero element */
192 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
194 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
195 static int gost_digests(ENGINE *e, const EVP_MD **digest,
196 const int **nids, int nid)
201 int *n = known_digest_nids;
204 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
205 *n++ = gost_digest_array[i]->nid;
209 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
210 if (nid == gost_digest_array[i]->nid) {
211 *digest = GOST_init_digest(gost_digest_array[i]);
218 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
219 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
220 const int **nids, int nid)
225 int *n = known_cipher_nids;
228 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
229 *n++ = gost_cipher_array[i]->nid;
233 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
234 if (nid == gost_cipher_array[i]->nid) {
235 *cipher = GOST_init_cipher(gost_cipher_array[i]);
242 static int gost_meth_nids(const int **nids)
244 struct gost_meth_minfo *info = gost_meth_array;
245 int *n = known_meths_nids;
248 for (; info->nid; info++)
250 return OSSL_NELEM(known_meths_nids);
253 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
254 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
255 const int **nids, int nid)
257 struct gost_meth_minfo *info;
260 return gost_meth_nids(nids);
262 for (info = gost_meth_array; info->nid; info++)
263 if (nid == info->nid) {
264 *pmeth = *info->pmeth;
271 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
272 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
273 const int **nids, int nid)
275 struct gost_meth_minfo *info;
278 return gost_meth_nids(nids);
280 for (info = gost_meth_array; info->nid; info++)
281 if (nid == info->nid) {
282 *ameth = *info->ameth;
289 static int gost_engine_init(ENGINE* e) {
293 static int gost_engine_finish(ENGINE* e) {
297 static int gost_engine_destroy(ENGINE* e) {
300 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
301 GOST_deinit_digest(gost_digest_array[i]);
302 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
303 GOST_deinit_cipher(gost_cipher_array[i]);
307 struct gost_meth_minfo *minfo = gost_meth_array;
308 for (; minfo->nid; minfo++) {
309 *minfo->pmeth = NULL;
310 *minfo->ameth = NULL;
313 ERR_unload_GOST_strings();
319 * Following is the glue that populates the ENGINE structure and that
320 * binds it to OpenSSL libraries
323 static int populate_gost_engine(ENGINE* e) {
328 if (!ENGINE_set_id(e, engine_gost_id)) {
329 printf("ENGINE_set_id failed\n");
332 if (!ENGINE_set_name(e, engine_gost_name)) {
333 printf("ENGINE_set_name failed\n");
336 if (!ENGINE_set_digests(e, gost_digests)) {
337 printf("ENGINE_set_digests failed\n");
340 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
341 printf("ENGINE_set_ciphers failed\n");
344 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
345 printf("ENGINE_set_pkey_meths failed\n");
348 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
349 printf("ENGINE_set_pkey_asn1_meths failed\n");
352 /* Control function and commands */
353 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
354 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
357 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
358 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
361 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
362 || !ENGINE_set_init_function(e, gost_engine_init)
363 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
368 * "register" in "register_ameth_gost" and "register_pmeth_gost" is
369 * not registering in an ENGINE sense, where things are hooked into
370 * OpenSSL's library. "register_ameth_gost" and "register_pmeth_gost"
371 * merely allocate and populate the method structures of this engine.
373 struct gost_meth_minfo *minfo = gost_meth_array;
374 for (; minfo->nid; minfo++) {
376 /* This skip looks temporary. */
377 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
380 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
383 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
392 static int bind_gost_engine(ENGINE* e) {
395 if (!ENGINE_register_ciphers(e)
396 || !ENGINE_register_digests(e)
397 || !ENGINE_register_pkey_meths(e))
401 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
402 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
406 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
407 if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
411 ENGINE_register_all_complete();
413 ERR_load_GOST_strings();
419 static int check_gost_engine(ENGINE* e, const char* id)
421 if (id != NULL && strcmp(id, engine_gost_id) != 0)
423 if (ameth_GostR3410_2001) {
424 printf("GOST engine already loaded\n");
430 static int make_gost_engine(ENGINE* e, const char* id)
432 return check_gost_engine(e, id)
433 && populate_gost_engine(e)
434 && bind_gost_engine(e);
437 #ifndef BUILDING_ENGINE_AS_LIBRARY
440 * When building gost-engine as a dynamically loadable module, these two
441 * lines do everything that's needed, and OpenSSL's libcrypto will be able
442 * to call its entry points, v_check and bind_engine.
445 IMPLEMENT_DYNAMIC_BIND_FN(make_gost_engine)
446 IMPLEMENT_DYNAMIC_CHECK_FN()
451 * When building gost-engine as a shared library, the application that uses
452 * it must manually call ENGINE_load_gost() for it to bind itself into the
453 * libcrypto libraries.
456 void ENGINE_load_gost(void) {
460 if ((toadd = ENGINE_new()) != NULL
461 && (ret = make_gost_engine(toadd, engine_gost_id)) > 0)
469 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */