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[8] = {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);
86 static int gost_pkey_meth_nids[] = {
87 NID_id_GostR3410_2001,
88 NID_id_Gost28147_89_MAC,
89 NID_id_GostR3410_2012_256,
90 NID_id_GostR3410_2012_512,
97 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
98 * pmeth_GostR3410_2012_256 = NULL,
99 * pmeth_GostR3410_2012_512 = NULL,
100 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
101 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL;
103 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
104 * ameth_GostR3410_2012_256 = NULL,
105 * ameth_GostR3410_2012_512 = NULL,
106 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
107 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL;
109 static int gost_engine_init(ENGINE* e) {
113 static int gost_engine_finish(ENGINE* e) {
117 static int gost_engine_destroy(ENGINE* e) {
118 digest_gost_destroy();
119 digest_gost2012_256_destroy();
120 digest_gost2012_512_destroy();
122 imit_gost_cpa_destroy();
123 imit_gost_cp_12_destroy();
124 magma_omac_destroy();
125 grasshopper_omac_destroy();
126 grasshopper_omac_acpkm_destroy();
128 cipher_gost_destroy();
129 cipher_gost_grasshopper_destroy();
133 pmeth_GostR3410_2001 = NULL;
134 pmeth_Gost28147_MAC = NULL;
135 pmeth_GostR3410_2012_256 = NULL;
136 pmeth_GostR3410_2012_512 = NULL;
137 pmeth_Gost28147_MAC_12 = NULL;
138 pmeth_magma_mac = NULL;
139 pmeth_grasshopper_mac = NULL;
141 ameth_GostR3410_2001 = NULL;
142 ameth_Gost28147_MAC = NULL;
143 ameth_GostR3410_2012_256 = NULL;
144 ameth_GostR3410_2012_512 = NULL;
145 ameth_Gost28147_MAC_12 = NULL;
146 ameth_magma_mac = NULL;
147 ameth_grasshopper_mac = NULL;
149 ERR_unload_GOST_strings();
154 static int bind_gost(ENGINE* e, const char* id) {
156 if (id != NULL && strcmp(id, engine_gost_id) != 0)
158 if (ameth_GostR3410_2001) {
159 printf("GOST engine already loaded\n");
162 if (!ENGINE_set_id(e, engine_gost_id)) {
163 printf("ENGINE_set_id failed\n");
166 if (!ENGINE_set_name(e, engine_gost_name)) {
167 printf("ENGINE_set_name failed\n");
170 if (!ENGINE_set_digests(e, gost_digests)) {
171 printf("ENGINE_set_digests failed\n");
174 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
175 printf("ENGINE_set_ciphers failed\n");
178 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
179 printf("ENGINE_set_pkey_meths failed\n");
182 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
183 printf("ENGINE_set_pkey_asn1_meths failed\n");
186 /* Control function and commands */
187 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
188 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
191 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
192 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
195 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
196 || !ENGINE_set_init_function(e, gost_engine_init)
197 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
201 if (!register_ameth_gost
202 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
203 "GOST R 34.10-2001"))
205 if (!register_ameth_gost
206 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
207 "GOST R 34.10-2012 with 256 bit key"))
209 if (!register_ameth_gost
210 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
211 "GOST R 34.10-2012 with 512 bit key"))
213 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
214 "GOST-MAC", "GOST 28147-89 MAC"))
216 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
218 "GOST 28147-89 MAC with 2012 params"))
220 if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac,
221 "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC"))
223 if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac,
224 "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC"))
227 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
230 if (!register_pmeth_gost
231 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
233 if (!register_pmeth_gost
234 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
236 if (!register_pmeth_gost
237 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
239 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
241 if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0))
243 if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0))
245 if (!ENGINE_register_ciphers(e)
246 || !ENGINE_register_digests(e)
247 || !ENGINE_register_pkey_meths(e)
248 /* These two actually should go in LIST_ADD command */
249 || !EVP_add_cipher(cipher_gost())
250 || !EVP_add_cipher(cipher_gost_cbc())
251 || !EVP_add_cipher(cipher_gost_cpacnt())
252 || !EVP_add_cipher(cipher_gost_cpcnt_12())
253 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
254 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
255 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
256 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
257 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
258 || !EVP_add_cipher(cipher_gost_grasshopper_ctracpkm())
259 || !EVP_add_cipher(cipher_magma_cbc())
260 || !EVP_add_cipher(cipher_magma_ctr())
261 || !EVP_add_digest(digest_gost())
262 || !EVP_add_digest(digest_gost2012_512())
263 || !EVP_add_digest(digest_gost2012_256())
264 || !EVP_add_digest(imit_gost_cpa())
265 || !EVP_add_digest(imit_gost_cp_12())
266 || !EVP_add_digest(magma_omac())
267 || !EVP_add_digest(grasshopper_omac())
272 ENGINE_register_all_complete();
274 ERR_load_GOST_strings();
280 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
281 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
282 IMPLEMENT_DYNAMIC_CHECK_FN()
283 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
285 static int gost_digests(ENGINE* e, const EVP_MD** digest,
286 const int** nids, int nid) {
288 if (digest == NULL) {
289 return gost_digest_nids(nids);
291 if (nid == NID_id_GostR3411_94) {
292 *digest = digest_gost();
293 } else if (nid == NID_id_Gost28147_89_MAC) {
294 *digest = imit_gost_cpa();
295 } else if (nid == NID_id_GostR3411_2012_256) {
296 *digest = digest_gost2012_256();
297 } else if (nid == NID_id_GostR3411_2012_512) {
298 *digest = digest_gost2012_512();
299 } else if (nid == NID_gost_mac_12) {
300 *digest = imit_gost_cp_12();
301 } else if (nid == NID_magma_mac) {
302 *digest = magma_omac();
303 } else if (nid == NID_grasshopper_mac) {
304 *digest = grasshopper_omac();
305 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac) {
306 *digest = grasshopper_omac_acpkm();
314 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
315 const int** nids, int nid) {
317 if (cipher == NULL) {
318 *nids = gost_cipher_nids;
319 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
322 if (nid == NID_id_Gost28147_89) {
323 *cipher = cipher_gost();
324 } else if (nid == NID_gost89_cnt) {
325 *cipher = cipher_gost_cpacnt();
326 } else if (nid == NID_gost89_cnt_12) {
327 *cipher = cipher_gost_cpcnt_12();
328 } else if (nid == NID_gost89_cbc) {
329 *cipher = cipher_gost_cbc();
330 } else if (nid == NID_grasshopper_ecb) {
331 *cipher = cipher_gost_grasshopper_ecb();
332 } else if (nid == NID_grasshopper_cbc) {
333 *cipher = cipher_gost_grasshopper_cbc();
334 } else if (nid == NID_grasshopper_cfb) {
335 *cipher = cipher_gost_grasshopper_cfb();
336 } else if (nid == NID_grasshopper_ofb) {
337 *cipher = cipher_gost_grasshopper_ofb();
338 } else if (nid == NID_grasshopper_ctr) {
339 *cipher = cipher_gost_grasshopper_ctr();
340 } else if (nid == NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm) {
341 *cipher = cipher_gost_grasshopper_ctracpkm();
342 } else if (nid == NID_magma_cbc) {
343 *cipher = cipher_magma_cbc();
344 } else if (nid == NID_magma_ctr) {
345 *cipher = cipher_magma_ctr();
353 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
354 const int** nids, int nid) {
356 *nids = gost_pkey_meth_nids;
357 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
361 case NID_id_GostR3410_2001:
362 *pmeth = pmeth_GostR3410_2001;
364 case NID_id_GostR3410_2012_256:
365 *pmeth = pmeth_GostR3410_2012_256;
367 case NID_id_GostR3410_2012_512:
368 *pmeth = pmeth_GostR3410_2012_512;
370 case NID_id_Gost28147_89_MAC:
371 *pmeth = pmeth_Gost28147_MAC;
373 case NID_gost_mac_12:
374 *pmeth = pmeth_Gost28147_MAC_12;
377 *pmeth = pmeth_magma_mac;
379 case NID_grasshopper_mac:
380 *pmeth = pmeth_grasshopper_mac;
390 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
391 const int** nids, int nid) {
393 *nids = gost_pkey_meth_nids;
394 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
398 case NID_id_GostR3410_2001:
399 *ameth = ameth_GostR3410_2001;
401 case NID_id_GostR3410_2012_256:
402 *ameth = ameth_GostR3410_2012_256;
404 case NID_id_GostR3410_2012_512:
405 *ameth = ameth_GostR3410_2012_512;
407 case NID_id_Gost28147_89_MAC:
408 *ameth = ameth_Gost28147_MAC;
410 case NID_gost_mac_12:
411 *ameth = ameth_Gost28147_MAC_12;
414 *ameth = ameth_magma_mac;
416 case NID_grasshopper_mac:
417 *ameth = ameth_grasshopper_mac;
427 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
429 static ENGINE* engine_gost(void) {
430 ENGINE* ret = ENGINE_new();
433 if (!bind_gost(ret, engine_gost_id)) {
440 void ENGINE_load_gost(void) {
442 if (pmeth_GostR3410_2001)
444 toadd = engine_gost();