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"
20 #include "gost_grasshopper_cipher.h"
22 static const char* engine_gost_id = "gost";
24 static const char* engine_gost_name =
25 "Reference implementation of GOST engine";
27 /* Symmetric cipher and digest function registrar */
29 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
30 const int** nids, int nid);
32 static int gost_digests(ENGINE* e, const EVP_MD** digest,
33 const int** nids, int nid);
35 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
36 const int** nids, int nid);
38 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
39 const int** nids, int nid);
41 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
42 * pmeth_GostR3410_2001DH = NULL,
43 * pmeth_GostR3410_2012_256 = NULL,
44 * pmeth_GostR3410_2012_512 = NULL,
45 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
46 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL,
47 * pmeth_magma_mac_acpkm = NULL, * pmeth_grasshopper_mac_acpkm = NULL;
49 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
50 * ameth_GostR3410_2001DH = NULL,
51 * ameth_GostR3410_2012_256 = NULL,
52 * ameth_GostR3410_2012_512 = NULL,
53 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
54 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL,
55 * ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;
57 GOST_digest *gost_digest_array[] = {
59 &Gost28147_89_MAC_digest,
60 &GostR3411_2012_256_digest,
61 &GostR3411_2012_512_digest,
62 &Gost28147_89_mac_12_digest,
64 &grasshopper_mac_digest,
65 &kuznyechik_ctracpkm_omac_digest,
68 GOST_cipher *gost_cipher_array[] = {
70 &Gost28147_89_cnt_cipher,
71 &Gost28147_89_cnt_12_cipher,
72 &Gost28147_89_cbc_cipher,
73 &grasshopper_ecb_cipher,
74 &grasshopper_cbc_cipher,
75 &grasshopper_cfb_cipher,
76 &grasshopper_ofb_cipher,
77 &grasshopper_ctr_cipher,
80 &magma_ctr_acpkm_cipher,
81 &magma_ctr_acpkm_omac_cipher,
82 &grasshopper_ctr_acpkm_cipher,
83 &grasshopper_ctr_acpkm_omac_cipher,
85 &kuznyechik_kexp15_cipher,
88 static struct gost_meth_minfo {
90 EVP_PKEY_METHOD **pmeth;
91 EVP_PKEY_ASN1_METHOD **ameth;
94 } gost_meth_array[] = {
96 NID_id_GostR3410_2001,
97 &pmeth_GostR3410_2001,
98 &ameth_GostR3410_2001,
103 NID_id_GostR3410_2001DH,
104 &pmeth_GostR3410_2001DH,
105 &ameth_GostR3410_2001DH,
107 "GOST R 34.10-2001 DH",
110 NID_id_Gost28147_89_MAC,
111 &pmeth_Gost28147_MAC,
112 &ameth_Gost28147_MAC,
117 NID_id_GostR3410_2012_256,
118 &pmeth_GostR3410_2012_256,
119 &ameth_GostR3410_2012_256,
121 "GOST R 34.10-2012 with 256 bit key",
124 NID_id_GostR3410_2012_512,
125 &pmeth_GostR3410_2012_512,
126 &ameth_GostR3410_2012_512,
128 "GOST R 34.10-2012 with 512 bit key",
132 &pmeth_Gost28147_MAC_12,
133 &ameth_Gost28147_MAC_12,
135 "GOST 28147-89 MAC with 2012 params",
142 "GOST R 34.13-2015 Magma MAC",
146 &pmeth_grasshopper_mac,
147 &ameth_grasshopper_mac,
149 "GOST R 34.13-2015 Grasshopper MAC",
152 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
153 &pmeth_magma_mac_acpkm,
154 &ameth_magma_mac_acpkm,
155 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
156 "GOST R 34.13-2015 Magma MAC ACPKM",
159 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
160 &pmeth_grasshopper_mac_acpkm,
161 &ameth_grasshopper_mac_acpkm,
162 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
163 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
169 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
172 static int known_digest_nids[OSSL_NELEM(gost_digest_array)];
173 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
174 /* `- 1' because of terminating zero element */
175 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
177 static int gost_engine_init(ENGINE* e) {
181 static int gost_engine_finish(ENGINE* e) {
185 static int gost_engine_destroy(ENGINE* e) {
188 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
189 GOST_deinit_digest(gost_digest_array[i]);
190 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
191 GOST_deinit_cipher(gost_cipher_array[i]);
195 struct gost_meth_minfo *minfo = gost_meth_array;
196 for (; minfo->nid; minfo++) {
197 *minfo->pmeth = NULL;
198 *minfo->ameth = NULL;
201 ERR_unload_GOST_strings();
206 static int bind_gost(ENGINE* e, const char* id) {
208 if (id != NULL && strcmp(id, engine_gost_id) != 0)
210 if (ameth_GostR3410_2001) {
211 printf("GOST engine already loaded\n");
214 if (!ENGINE_set_id(e, engine_gost_id)) {
215 printf("ENGINE_set_id failed\n");
218 if (!ENGINE_set_name(e, engine_gost_name)) {
219 printf("ENGINE_set_name failed\n");
222 if (!ENGINE_set_digests(e, gost_digests)) {
223 printf("ENGINE_set_digests failed\n");
226 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
227 printf("ENGINE_set_ciphers failed\n");
230 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
231 printf("ENGINE_set_pkey_meths failed\n");
234 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
235 printf("ENGINE_set_pkey_asn1_meths failed\n");
238 /* Control function and commands */
239 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
240 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
243 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
244 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
247 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
248 || !ENGINE_set_init_function(e, gost_engine_init)
249 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
253 struct gost_meth_minfo *minfo = gost_meth_array;
254 for (; minfo->nid; minfo++) {
256 /* This skip looks temporary. */
257 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
260 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
263 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
267 if (!ENGINE_register_ciphers(e)
268 || !ENGINE_register_digests(e)
269 || !ENGINE_register_pkey_meths(e))
273 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
274 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
278 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
279 if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
283 ENGINE_register_all_complete();
285 ERR_load_GOST_strings();
291 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
292 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
293 IMPLEMENT_DYNAMIC_CHECK_FN()
294 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
296 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
297 static int gost_digests(ENGINE *e, const EVP_MD **digest,
298 const int **nids, int nid)
303 int *n = known_digest_nids;
306 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
307 *n++ = gost_digest_array[i]->nid;
311 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
312 if (nid == gost_digest_array[i]->nid) {
313 *digest = GOST_init_digest(gost_digest_array[i]);
320 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
321 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
322 const int **nids, int nid)
327 int *n = known_cipher_nids;
330 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
331 *n++ = gost_cipher_array[i]->nid;
335 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
336 if (nid == gost_cipher_array[i]->nid) {
337 *cipher = GOST_init_cipher(gost_cipher_array[i]);
344 static int gost_meth_nids(const int **nids)
346 struct gost_meth_minfo *info = gost_meth_array;
347 int *n = known_meths_nids;
350 for (; info->nid; info++)
352 return OSSL_NELEM(known_meths_nids);
355 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
356 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
357 const int **nids, int nid)
359 struct gost_meth_minfo *info;
362 return gost_meth_nids(nids);
364 for (info = gost_meth_array; info->nid; info++)
365 if (nid == info->nid) {
366 *pmeth = *info->pmeth;
373 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
374 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
375 const int **nids, int nid)
377 struct gost_meth_minfo *info;
380 return gost_meth_nids(nids);
382 for (info = gost_meth_array; info->nid; info++)
383 if (nid == info->nid) {
384 *ameth = *info->ameth;
391 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
393 static ENGINE* engine_gost(void) {
394 ENGINE* ret = ENGINE_new();
397 if (!bind_gost(ret, engine_gost_id)) {
404 void ENGINE_load_gost(void) {
406 if (pmeth_GostR3410_2001)
408 toadd = engine_gost();
417 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */