1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Main file of GOST engine *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include <openssl/obj_mac.h>
16 #include "e_gost_err.h"
19 #include "gost_grasshopper_cipher.h"
21 static const char* engine_gost_id = "gost";
23 static const char* engine_gost_name =
24 "Reference implementation of GOST engine";
26 /* Symmetric cipher and digest function registrar */
28 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
29 const int** nids, int nid);
31 static int gost_digests(ENGINE* e, const EVP_MD** digest,
32 const int** nids, int ind);
34 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
35 const int** nids, int nid);
37 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
38 const int** nids, int nid);
40 static int gost_cipher_nids[] = {
52 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
57 static int gost_digest_nids(const int** nids) {
58 static int digest_nids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
64 if ((md = digest_gost()) != NULL)
65 digest_nids[pos++] = EVP_MD_type(md);
66 if ((md = imit_gost_cpa()) != NULL)
67 digest_nids[pos++] = EVP_MD_type(md);
68 if ((md = digest_gost2012_256()) != NULL)
69 digest_nids[pos++] = EVP_MD_type(md);
70 if ((md = digest_gost2012_512()) != NULL)
71 digest_nids[pos++] = EVP_MD_type(md);
72 if ((md = imit_gost_cp_12()) != NULL)
73 digest_nids[pos++] = EVP_MD_type(md);
74 if ((md = magma_omac()) != NULL)
75 digest_nids[pos++] = EVP_MD_type(md);
76 if ((md = grasshopper_omac()) != NULL)
77 digest_nids[pos++] = EVP_MD_type(md);
78 /* if ((md = magma_omac_acpkm()) != NULL)
79 digest_nids[pos++] = EVP_MD_type(md);*/
80 if ((md = grasshopper_omac_acpkm()) != NULL)
81 digest_nids[pos++] = EVP_MD_type(md);
90 static int gost_pkey_meth_nids[] = {
91 NID_id_GostR3410_2001,
92 NID_id_Gost28147_89_MAC,
93 NID_id_GostR3410_2012_256,
94 NID_id_GostR3410_2012_512,
98 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
99 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
103 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
104 * pmeth_GostR3410_2012_256 = NULL,
105 * pmeth_GostR3410_2012_512 = NULL,
106 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
107 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL,
108 * pmeth_magma_mac_acpkm = NULL, * pmeth_grasshopper_mac_acpkm = NULL;
110 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
111 * ameth_GostR3410_2012_256 = NULL,
112 * ameth_GostR3410_2012_512 = NULL,
113 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
114 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL,
115 * ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;
117 static int gost_engine_init(ENGINE* e) {
121 static int gost_engine_finish(ENGINE* e) {
125 static int gost_engine_destroy(ENGINE* e) {
126 EVP_delete_digest_alias("streebog256");
127 EVP_delete_digest_alias("streebog512");
128 digest_gost_destroy();
129 digest_gost2012_256_destroy();
130 digest_gost2012_512_destroy();
132 imit_gost_cpa_destroy();
133 imit_gost_cp_12_destroy();
134 magma_omac_destroy();
135 grasshopper_omac_destroy();
136 grasshopper_omac_acpkm_destroy();
138 cipher_gost_destroy();
139 cipher_gost_grasshopper_destroy();
143 pmeth_GostR3410_2001 = NULL;
144 pmeth_Gost28147_MAC = NULL;
145 pmeth_GostR3410_2012_256 = NULL;
146 pmeth_GostR3410_2012_512 = NULL;
147 pmeth_Gost28147_MAC_12 = NULL;
148 pmeth_magma_mac = NULL;
149 pmeth_grasshopper_mac = NULL;
150 pmeth_magma_mac_acpkm = NULL;
151 pmeth_grasshopper_mac_acpkm = NULL;
153 ameth_GostR3410_2001 = NULL;
154 ameth_Gost28147_MAC = NULL;
155 ameth_GostR3410_2012_256 = NULL;
156 ameth_GostR3410_2012_512 = NULL;
157 ameth_Gost28147_MAC_12 = NULL;
158 ameth_magma_mac = NULL;
159 ameth_grasshopper_mac = NULL;
160 ameth_magma_mac_acpkm = NULL;
161 ameth_grasshopper_mac_acpkm = NULL;
163 ERR_unload_GOST_strings();
168 static int bind_gost(ENGINE* e, const char* id) {
170 if (id != NULL && strcmp(id, engine_gost_id) != 0)
172 if (ameth_GostR3410_2001) {
173 printf("GOST engine already loaded\n");
176 if (!ENGINE_set_id(e, engine_gost_id)) {
177 printf("ENGINE_set_id failed\n");
180 if (!ENGINE_set_name(e, engine_gost_name)) {
181 printf("ENGINE_set_name failed\n");
184 if (!ENGINE_set_digests(e, gost_digests)) {
185 printf("ENGINE_set_digests failed\n");
188 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
189 printf("ENGINE_set_ciphers failed\n");
192 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
193 printf("ENGINE_set_pkey_meths failed\n");
196 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
197 printf("ENGINE_set_pkey_asn1_meths failed\n");
200 /* Control function and commands */
201 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
202 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
205 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
206 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
209 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
210 || !ENGINE_set_init_function(e, gost_engine_init)
211 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
215 if (!register_ameth_gost
216 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
217 "GOST R 34.10-2001"))
219 if (!register_ameth_gost
220 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
221 "GOST R 34.10-2012 with 256 bit key"))
223 if (!register_ameth_gost
224 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
225 "GOST R 34.10-2012 with 512 bit key"))
227 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
228 "GOST-MAC", "GOST 28147-89 MAC"))
230 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
232 "GOST 28147-89 MAC with 2012 params"))
234 if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac,
235 "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC"))
237 if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac,
238 "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC"))
240 /* if (!register_ameth_gost(NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac, &ameth_magma_mac_acpkm,
241 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC", "GOST R 34.13-2015 Magma MAC ACPKM"))
243 if (!register_ameth_gost(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, &ameth_grasshopper_mac_acpkm,
244 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC", "GOST R 34.13-2015 Grasshopper MAC ACPKM"))
248 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
251 if (!register_pmeth_gost
252 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
254 if (!register_pmeth_gost
255 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
257 if (!register_pmeth_gost
258 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
260 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
262 if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0))
264 if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0))
266 /* if (!register_pmeth_gost(NID_magma_mac_acpkm, &pmeth_magma_mac_acpkm, 0))
268 if (!register_pmeth_gost(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, &pmeth_grasshopper_mac_acpkm, 0))
270 if (!ENGINE_register_ciphers(e)
271 || !ENGINE_register_digests(e)
272 || !ENGINE_register_pkey_meths(e)
273 /* These two actually should go in LIST_ADD command */
274 || !EVP_add_cipher(cipher_gost())
275 || !EVP_add_cipher(cipher_gost_cbc())
276 || !EVP_add_cipher(cipher_gost_cpacnt())
277 || !EVP_add_cipher(cipher_gost_cpcnt_12())
278 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
279 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
280 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
281 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
282 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
283 || !EVP_add_cipher(cipher_gost_grasshopper_ctracpkm())
284 #ifdef NID_kuznyechik_mgm
285 || !EVP_add_cipher(cipher_gost_grasshopper_mgm())
287 || !EVP_add_cipher(cipher_magma_cbc())
288 || !EVP_add_cipher(cipher_magma_ctr())
289 || !EVP_add_digest(digest_gost())
290 || !EVP_add_digest(digest_gost2012_512())
291 || !EVP_add_digest(digest_gost2012_256())
292 || !EVP_add_digest(imit_gost_cpa())
293 || !EVP_add_digest(imit_gost_cp_12())
294 || !EVP_add_digest(magma_omac())
295 || !EVP_add_digest(grasshopper_omac())
296 /* || !EVP_add_digest(magma_omac_acpkm()) */
297 || !EVP_add_digest(grasshopper_omac_acpkm())
302 if(!EVP_add_digest_alias(SN_id_GostR3411_2012_256, "streebog256")
303 || !EVP_add_digest_alias(SN_id_GostR3411_2012_512, "streebog512")) {
307 ENGINE_register_all_complete();
309 ERR_load_GOST_strings();
315 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
316 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
317 IMPLEMENT_DYNAMIC_CHECK_FN()
318 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
320 static int gost_digests(ENGINE* e, const EVP_MD** digest,
321 const int** nids, int nid) {
323 if (digest == NULL) {
324 return gost_digest_nids(nids);
326 if (nid == NID_id_GostR3411_94) {
327 *digest = digest_gost();
328 } else if (nid == NID_id_Gost28147_89_MAC) {
329 *digest = imit_gost_cpa();
330 } else if (nid == NID_id_GostR3411_2012_256) {
331 *digest = digest_gost2012_256();
332 } else if (nid == NID_id_GostR3411_2012_512) {
333 *digest = digest_gost2012_512();
334 } else if (nid == NID_gost_mac_12) {
335 *digest = imit_gost_cp_12();
336 } else if (nid == NID_magma_mac) {
337 *digest = magma_omac();
338 } else if (nid == NID_grasshopper_mac) {
339 *digest = grasshopper_omac();
340 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac) {
341 *digest = grasshopper_omac_acpkm();
349 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
350 const int** nids, int nid) {
352 if (cipher == NULL) {
353 *nids = gost_cipher_nids;
354 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
357 if (nid == NID_id_Gost28147_89) {
358 *cipher = cipher_gost();
359 } else if (nid == NID_gost89_cnt) {
360 *cipher = cipher_gost_cpacnt();
361 } else if (nid == NID_gost89_cnt_12) {
362 *cipher = cipher_gost_cpcnt_12();
363 } else if (nid == NID_gost89_cbc) {
364 *cipher = cipher_gost_cbc();
365 } else if (nid == NID_grasshopper_ecb) {
366 *cipher = cipher_gost_grasshopper_ecb();
367 } else if (nid == NID_grasshopper_cbc) {
368 *cipher = cipher_gost_grasshopper_cbc();
369 } else if (nid == NID_grasshopper_cfb) {
370 *cipher = cipher_gost_grasshopper_cfb();
371 } else if (nid == NID_grasshopper_ofb) {
372 *cipher = cipher_gost_grasshopper_ofb();
373 } else if (nid == NID_grasshopper_ctr) {
374 *cipher = cipher_gost_grasshopper_ctr();
375 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm) {
376 *cipher = cipher_gost_grasshopper_ctracpkm();
377 #ifdef NID_kuznyechik_mgm
378 } else if (nid == NID_kuznyechik_mgm) {
379 *cipher = cipher_gost_grasshopper_mgm();
381 } else if (nid == NID_magma_cbc) {
382 *cipher = cipher_magma_cbc();
383 } else if (nid == NID_magma_ctr) {
384 *cipher = cipher_magma_ctr();
392 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
393 const int** nids, int nid) {
395 *nids = gost_pkey_meth_nids;
396 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
400 case NID_id_GostR3410_2001:
401 *pmeth = pmeth_GostR3410_2001;
403 case NID_id_GostR3410_2012_256:
404 *pmeth = pmeth_GostR3410_2012_256;
406 case NID_id_GostR3410_2012_512:
407 *pmeth = pmeth_GostR3410_2012_512;
409 case NID_id_Gost28147_89_MAC:
410 *pmeth = pmeth_Gost28147_MAC;
412 case NID_gost_mac_12:
413 *pmeth = pmeth_Gost28147_MAC_12;
416 *pmeth = pmeth_magma_mac;
418 case NID_grasshopper_mac:
419 *pmeth = pmeth_grasshopper_mac;
421 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
422 *pmeth = pmeth_magma_mac_acpkm;
424 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
425 *pmeth = pmeth_grasshopper_mac_acpkm;
435 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
436 const int** nids, int nid) {
438 *nids = gost_pkey_meth_nids;
439 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
443 case NID_id_GostR3410_2001:
444 *ameth = ameth_GostR3410_2001;
446 case NID_id_GostR3410_2012_256:
447 *ameth = ameth_GostR3410_2012_256;
449 case NID_id_GostR3410_2012_512:
450 *ameth = ameth_GostR3410_2012_512;
452 case NID_id_Gost28147_89_MAC:
453 *ameth = ameth_Gost28147_MAC;
455 case NID_gost_mac_12:
456 *ameth = ameth_Gost28147_MAC_12;
459 *ameth = ameth_magma_mac;
461 case NID_grasshopper_mac:
462 *ameth = ameth_grasshopper_mac;
464 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
465 *ameth = ameth_magma_mac_acpkm;
467 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
468 *ameth = ameth_grasshopper_mac_acpkm;
479 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
481 static ENGINE* engine_gost(void) {
482 ENGINE* ret = ENGINE_new();
485 if (!bind_gost(ret, engine_gost_id)) {
492 void ENGINE_load_gost(void) {
494 if (pmeth_GostR3410_2001)
496 toadd = engine_gost();