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,
56 static int gost_digest_nids(const int** nids) {
57 static int digest_nids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
63 if ((md = digest_gost()) != NULL)
64 digest_nids[pos++] = EVP_MD_type(md);
65 if ((md = imit_gost_cpa()) != NULL)
66 digest_nids[pos++] = EVP_MD_type(md);
67 if ((md = digest_gost2012_256()) != NULL)
68 digest_nids[pos++] = EVP_MD_type(md);
69 if ((md = digest_gost2012_512()) != NULL)
70 digest_nids[pos++] = EVP_MD_type(md);
71 if ((md = imit_gost_cp_12()) != NULL)
72 digest_nids[pos++] = EVP_MD_type(md);
73 if ((md = magma_omac()) != NULL)
74 digest_nids[pos++] = EVP_MD_type(md);
75 if ((md = grasshopper_omac()) != NULL)
76 digest_nids[pos++] = EVP_MD_type(md);
77 /* if ((md = magma_omac_acpkm()) != NULL)
78 digest_nids[pos++] = EVP_MD_type(md);*/
79 if ((md = grasshopper_omac_acpkm()) != NULL)
80 digest_nids[pos++] = EVP_MD_type(md);
89 static int gost_pkey_meth_nids[] = {
90 NID_id_GostR3410_2001,
91 NID_id_Gost28147_89_MAC,
92 NID_id_GostR3410_2012_256,
93 NID_id_GostR3410_2012_512,
97 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
98 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
102 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
103 * pmeth_GostR3410_2012_256 = NULL,
104 * pmeth_GostR3410_2012_512 = NULL,
105 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
106 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL,
107 * pmeth_magma_mac_acpkm = NULL, * pmeth_grasshopper_mac_acpkm = NULL;
109 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
110 * ameth_GostR3410_2012_256 = NULL,
111 * ameth_GostR3410_2012_512 = NULL,
112 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
113 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL,
114 * ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;
116 static int gost_engine_init(ENGINE* e) {
120 static int gost_engine_finish(ENGINE* e) {
124 static int gost_engine_destroy(ENGINE* e) {
125 EVP_delete_digest_alias("streebog256");
126 EVP_delete_digest_alias("streebog512");
127 digest_gost_destroy();
128 digest_gost2012_256_destroy();
129 digest_gost2012_512_destroy();
131 imit_gost_cpa_destroy();
132 imit_gost_cp_12_destroy();
133 magma_omac_destroy();
134 grasshopper_omac_destroy();
135 grasshopper_omac_acpkm_destroy();
137 cipher_gost_destroy();
138 cipher_gost_grasshopper_destroy();
142 pmeth_GostR3410_2001 = NULL;
143 pmeth_Gost28147_MAC = NULL;
144 pmeth_GostR3410_2012_256 = NULL;
145 pmeth_GostR3410_2012_512 = NULL;
146 pmeth_Gost28147_MAC_12 = NULL;
147 pmeth_magma_mac = NULL;
148 pmeth_grasshopper_mac = NULL;
149 pmeth_magma_mac_acpkm = NULL;
150 pmeth_grasshopper_mac_acpkm = NULL;
152 ameth_GostR3410_2001 = NULL;
153 ameth_Gost28147_MAC = NULL;
154 ameth_GostR3410_2012_256 = NULL;
155 ameth_GostR3410_2012_512 = NULL;
156 ameth_Gost28147_MAC_12 = NULL;
157 ameth_magma_mac = NULL;
158 ameth_grasshopper_mac = NULL;
159 ameth_magma_mac_acpkm = NULL;
160 ameth_grasshopper_mac_acpkm = NULL;
162 ERR_unload_GOST_strings();
167 static int bind_gost(ENGINE* e, const char* id) {
169 if (id != NULL && strcmp(id, engine_gost_id) != 0)
171 if (ameth_GostR3410_2001) {
172 printf("GOST engine already loaded\n");
175 if (!ENGINE_set_id(e, engine_gost_id)) {
176 printf("ENGINE_set_id failed\n");
179 if (!ENGINE_set_name(e, engine_gost_name)) {
180 printf("ENGINE_set_name failed\n");
183 if (!ENGINE_set_digests(e, gost_digests)) {
184 printf("ENGINE_set_digests failed\n");
187 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
188 printf("ENGINE_set_ciphers failed\n");
191 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
192 printf("ENGINE_set_pkey_meths failed\n");
195 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
196 printf("ENGINE_set_pkey_asn1_meths failed\n");
199 /* Control function and commands */
200 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
201 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
204 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
205 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
208 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
209 || !ENGINE_set_init_function(e, gost_engine_init)
210 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
214 if (!register_ameth_gost
215 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
216 "GOST R 34.10-2001"))
218 if (!register_ameth_gost
219 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
220 "GOST R 34.10-2012 with 256 bit key"))
222 if (!register_ameth_gost
223 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
224 "GOST R 34.10-2012 with 512 bit key"))
226 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
227 "GOST-MAC", "GOST 28147-89 MAC"))
229 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
231 "GOST 28147-89 MAC with 2012 params"))
233 if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac,
234 "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC"))
236 if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac,
237 "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC"))
239 /* if (!register_ameth_gost(NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac, &ameth_magma_mac_acpkm,
240 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC", "GOST R 34.13-2015 Magma MAC ACPKM"))
242 if (!register_ameth_gost(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, &ameth_grasshopper_mac_acpkm,
243 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC", "GOST R 34.13-2015 Grasshopper MAC ACPKM"))
247 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
250 if (!register_pmeth_gost
251 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
253 if (!register_pmeth_gost
254 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
256 if (!register_pmeth_gost
257 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
259 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
261 if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0))
263 if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0))
265 /* if (!register_pmeth_gost(NID_magma_mac_acpkm, &pmeth_magma_mac_acpkm, 0))
267 if (!register_pmeth_gost(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, &pmeth_grasshopper_mac_acpkm, 0))
269 if (!ENGINE_register_ciphers(e)
270 || !ENGINE_register_digests(e)
271 || !ENGINE_register_pkey_meths(e)
272 /* These two actually should go in LIST_ADD command */
273 || !EVP_add_cipher(cipher_gost())
274 || !EVP_add_cipher(cipher_gost_cbc())
275 || !EVP_add_cipher(cipher_gost_cpacnt())
276 || !EVP_add_cipher(cipher_gost_cpcnt_12())
277 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
278 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
279 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
280 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
281 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
282 || !EVP_add_cipher(cipher_gost_grasshopper_ctracpkm())
283 || !EVP_add_cipher(cipher_magma_cbc())
284 || !EVP_add_cipher(cipher_magma_ctr())
285 || !EVP_add_digest(digest_gost())
286 || !EVP_add_digest(digest_gost2012_512())
287 || !EVP_add_digest(digest_gost2012_256())
288 || !EVP_add_digest(imit_gost_cpa())
289 || !EVP_add_digest(imit_gost_cp_12())
290 || !EVP_add_digest(magma_omac())
291 || !EVP_add_digest(grasshopper_omac())
292 /* || !EVP_add_digest(magma_omac_acpkm()) */
293 || !EVP_add_digest(grasshopper_omac_acpkm())
298 if(!EVP_add_digest_alias(SN_id_GostR3411_2012_256, "streebog256")
299 || !EVP_add_digest_alias(SN_id_GostR3411_2012_512, "streebog512")) {
303 ENGINE_register_all_complete();
305 ERR_load_GOST_strings();
311 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
312 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
313 IMPLEMENT_DYNAMIC_CHECK_FN()
314 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
316 static int gost_digests(ENGINE* e, const EVP_MD** digest,
317 const int** nids, int nid) {
319 if (digest == NULL) {
320 return gost_digest_nids(nids);
322 if (nid == NID_id_GostR3411_94) {
323 *digest = digest_gost();
324 } else if (nid == NID_id_Gost28147_89_MAC) {
325 *digest = imit_gost_cpa();
326 } else if (nid == NID_id_GostR3411_2012_256) {
327 *digest = digest_gost2012_256();
328 } else if (nid == NID_id_GostR3411_2012_512) {
329 *digest = digest_gost2012_512();
330 } else if (nid == NID_gost_mac_12) {
331 *digest = imit_gost_cp_12();
332 } else if (nid == NID_magma_mac) {
333 *digest = magma_omac();
334 } else if (nid == NID_grasshopper_mac) {
335 *digest = grasshopper_omac();
336 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac) {
337 *digest = grasshopper_omac_acpkm();
345 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
346 const int** nids, int nid) {
348 if (cipher == NULL) {
349 *nids = gost_cipher_nids;
350 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
353 if (nid == NID_id_Gost28147_89) {
354 *cipher = cipher_gost();
355 } else if (nid == NID_gost89_cnt) {
356 *cipher = cipher_gost_cpacnt();
357 } else if (nid == NID_gost89_cnt_12) {
358 *cipher = cipher_gost_cpcnt_12();
359 } else if (nid == NID_gost89_cbc) {
360 *cipher = cipher_gost_cbc();
361 } else if (nid == NID_grasshopper_ecb) {
362 *cipher = cipher_gost_grasshopper_ecb();
363 } else if (nid == NID_grasshopper_cbc) {
364 *cipher = cipher_gost_grasshopper_cbc();
365 } else if (nid == NID_grasshopper_cfb) {
366 *cipher = cipher_gost_grasshopper_cfb();
367 } else if (nid == NID_grasshopper_ofb) {
368 *cipher = cipher_gost_grasshopper_ofb();
369 } else if (nid == NID_grasshopper_ctr) {
370 *cipher = cipher_gost_grasshopper_ctr();
371 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm) {
372 *cipher = cipher_gost_grasshopper_ctracpkm();
373 } else if (nid == NID_magma_cbc) {
374 *cipher = cipher_magma_cbc();
375 } else if (nid == NID_magma_ctr) {
376 *cipher = cipher_magma_ctr();
384 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
385 const int** nids, int nid) {
387 *nids = gost_pkey_meth_nids;
388 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
392 case NID_id_GostR3410_2001:
393 *pmeth = pmeth_GostR3410_2001;
395 case NID_id_GostR3410_2012_256:
396 *pmeth = pmeth_GostR3410_2012_256;
398 case NID_id_GostR3410_2012_512:
399 *pmeth = pmeth_GostR3410_2012_512;
401 case NID_id_Gost28147_89_MAC:
402 *pmeth = pmeth_Gost28147_MAC;
404 case NID_gost_mac_12:
405 *pmeth = pmeth_Gost28147_MAC_12;
408 *pmeth = pmeth_magma_mac;
410 case NID_grasshopper_mac:
411 *pmeth = pmeth_grasshopper_mac;
413 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
414 *pmeth = pmeth_magma_mac_acpkm;
416 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
417 *pmeth = pmeth_grasshopper_mac_acpkm;
427 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
428 const int** nids, int nid) {
430 *nids = gost_pkey_meth_nids;
431 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
435 case NID_id_GostR3410_2001:
436 *ameth = ameth_GostR3410_2001;
438 case NID_id_GostR3410_2012_256:
439 *ameth = ameth_GostR3410_2012_256;
441 case NID_id_GostR3410_2012_512:
442 *ameth = ameth_GostR3410_2012_512;
444 case NID_id_Gost28147_89_MAC:
445 *ameth = ameth_Gost28147_MAC;
447 case NID_gost_mac_12:
448 *ameth = ameth_Gost28147_MAC_12;
451 *ameth = ameth_magma_mac;
453 case NID_grasshopper_mac:
454 *ameth = ameth_grasshopper_mac;
456 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
457 *ameth = ameth_magma_mac_acpkm;
459 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
460 *ameth = ameth_grasshopper_mac_acpkm;
471 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
473 static ENGINE* engine_gost(void) {
474 ENGINE* ret = ENGINE_new();
477 if (!bind_gost(ret, engine_gost_id)) {
484 void ENGINE_load_gost(void) {
486 if (pmeth_GostR3410_2001)
488 toadd = engine_gost();