1 /**********************************************************************
2 * gost_eng.c *
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
5 * *
6 * Main file of GOST engine *
7 * for OpenSSL *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
10 #include <string.h>
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"
17 #include "gost_lcl.h"
18
19 #include "gost_grasshopper_cipher.h"
20
21 static const char* engine_gost_id = "gost";
22
23 static const char* engine_gost_name =
24 "Reference implementation of GOST engine";
25
26 /* Symmetric cipher and digest function registrar */
27
28 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
29 const int** nids, int nid);
30
31 static int gost_digests(ENGINE* e, const EVP_MD** digest,
32 const int** nids, int ind);
33
34 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
35 const int** nids, int nid);
36
37 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
38 const int** nids, int nid);
39
40 static int gost_cipher_nids[] = {
41 NID_id_Gost28147_89,
42 NID_gost89_cnt,
43 NID_gost89_cnt_12,
44 NID_gost89_cbc,
45 NID_grasshopper_ecb,
46 NID_grasshopper_cbc,
47 NID_grasshopper_cfb,
48 NID_grasshopper_ofb,
49 NID_grasshopper_ctr,
50 NID_magma_cbc,
51 NID_magma_ctr,
52 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
53 0
54 };
55
gost_digest_nids(const int ** nids)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};
58 static int pos = 0;
59 static int init = 0;
60
61 if (!init) {
62 const EVP_MD* md;
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);
81
82 digest_nids[pos] = 0;
83 init = 1;
84 }
85 *nids = digest_nids;
86 return pos;
87 }
88
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,
94 NID_gost_mac_12,
95 NID_magma_mac,
96 NID_grasshopper_mac,
97 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
98 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
99 0
100 };
101
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;
108
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;
115
gost_engine_init(ENGINE * e)116 static int gost_engine_init(ENGINE* e) {
117 return 1;
118 }
119
gost_engine_finish(ENGINE * e)120 static int gost_engine_finish(ENGINE* e) {
121 return 1;
122 }
123
gost_engine_destroy(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();
130
131 imit_gost_cpa_destroy();
132 imit_gost_cp_12_destroy();
133 magma_omac_destroy();
134 grasshopper_omac_destroy();
135 grasshopper_omac_acpkm_destroy();
136
137 cipher_gost_destroy();
138 cipher_gost_grasshopper_destroy();
139
140 gost_param_free();
141
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;
151
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;
161
162 ERR_unload_GOST_strings();
163
164 return 1;
165 }
166
bind_gost(ENGINE * e,const char * id)167 static int bind_gost(ENGINE* e, const char* id) {
168 int ret = 0;
169 if (id != NULL && strcmp(id, engine_gost_id) != 0)
170 return 0;
171 if (ameth_GostR3410_2001) {
172 printf("GOST engine already loaded\n");
173 goto end;
174 }
175 if (!ENGINE_set_id(e, engine_gost_id)) {
176 printf("ENGINE_set_id failed\n");
177 goto end;
178 }
179 if (!ENGINE_set_name(e, engine_gost_name)) {
180 printf("ENGINE_set_name failed\n");
181 goto end;
182 }
183 if (!ENGINE_set_digests(e, gost_digests)) {
184 printf("ENGINE_set_digests failed\n");
185 goto end;
186 }
187 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
188 printf("ENGINE_set_ciphers failed\n");
189 goto end;
190 }
191 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
192 printf("ENGINE_set_pkey_meths failed\n");
193 goto end;
194 }
195 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
196 printf("ENGINE_set_pkey_asn1_meths failed\n");
197 goto end;
198 }
199 /* Control function and commands */
200 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
201 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
202 goto end;
203 }
204 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
205 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
206 goto end;
207 }
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)) {
211 goto end;
212 }
213
214 if (!register_ameth_gost
215 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
216 "GOST R 34.10-2001"))
217 goto end;
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"))
221 goto end;
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"))
225 goto end;
226 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
227 "GOST-MAC", "GOST 28147-89 MAC"))
228 goto end;
229 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
230 "GOST-MAC-12",
231 "GOST 28147-89 MAC with 2012 params"))
232 goto end;
233 if (!register_ameth_gost(NID_magma_mac, &ameth_magma_mac,
234 "MAGMA-MAC", "GOST R 34.13-2015 Magma MAC"))
235 goto end;
236 if (!register_ameth_gost(NID_grasshopper_mac, &ameth_grasshopper_mac,
237 "GRASSHOPPER-MAC", "GOST R 34.13-2015 Grasshopper MAC"))
238 goto end;
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"))
241 goto end;*/
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"))
244 goto end;
245
246
247 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
248 goto end;
249
250 if (!register_pmeth_gost
251 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
252 goto end;
253 if (!register_pmeth_gost
254 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
255 goto end;
256 if (!register_pmeth_gost
257 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
258 goto end;
259 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
260 goto end;
261 if (!register_pmeth_gost(NID_magma_mac, &pmeth_magma_mac, 0))
262 goto end;
263 if (!register_pmeth_gost(NID_grasshopper_mac, &pmeth_grasshopper_mac, 0))
264 goto end;
265 /* if (!register_pmeth_gost(NID_magma_mac_acpkm, &pmeth_magma_mac_acpkm, 0))
266 goto end;*/
267 if (!register_pmeth_gost(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, &pmeth_grasshopper_mac_acpkm, 0))
268 goto end;
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())
294 ) {
295 goto end;
296 }
297
298 if(!EVP_add_digest_alias(SN_id_GostR3411_2012_256, "streebog256")
299 || !EVP_add_digest_alias(SN_id_GostR3411_2012_512, "streebog512")) {
300 goto end;
301 }
302
303 ENGINE_register_all_complete();
304
305 ERR_load_GOST_strings();
306 ret = 1;
307 end:
308 return ret;
309 }
310
311 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
312 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
IMPLEMENT_DYNAMIC_CHECK_FN()313 IMPLEMENT_DYNAMIC_CHECK_FN()
314 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
315
316 static int gost_digests(ENGINE* e, const EVP_MD** digest,
317 const int** nids, int nid) {
318 int ok = 1;
319 if (digest == NULL) {
320 return gost_digest_nids(nids);
321 }
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();
338 } else {
339 ok = 0;
340 *digest = NULL;
341 }
342 return ok;
343 }
344
gost_ciphers(ENGINE * e,const EVP_CIPHER ** cipher,const int ** nids,int nid)345 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
346 const int** nids, int nid) {
347 int ok = 1;
348 if (cipher == NULL) {
349 *nids = gost_cipher_nids;
350 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
351 }
352
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();
377 } else {
378 ok = 0;
379 *cipher = NULL;
380 }
381 return ok;
382 }
383
gost_pkey_meths(ENGINE * e,EVP_PKEY_METHOD ** pmeth,const int ** nids,int nid)384 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
385 const int** nids, int nid) {
386 if (pmeth == NULL) {
387 *nids = gost_pkey_meth_nids;
388 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
389 }
390
391 switch (nid) {
392 case NID_id_GostR3410_2001:
393 *pmeth = pmeth_GostR3410_2001;
394 return 1;
395 case NID_id_GostR3410_2012_256:
396 *pmeth = pmeth_GostR3410_2012_256;
397 return 1;
398 case NID_id_GostR3410_2012_512:
399 *pmeth = pmeth_GostR3410_2012_512;
400 return 1;
401 case NID_id_Gost28147_89_MAC:
402 *pmeth = pmeth_Gost28147_MAC;
403 return 1;
404 case NID_gost_mac_12:
405 *pmeth = pmeth_Gost28147_MAC_12;
406 return 1;
407 case NID_magma_mac:
408 *pmeth = pmeth_magma_mac;
409 return 1;
410 case NID_grasshopper_mac:
411 *pmeth = pmeth_grasshopper_mac;
412 return 1;
413 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
414 *pmeth = pmeth_magma_mac_acpkm;
415 return 1;
416 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
417 *pmeth = pmeth_grasshopper_mac_acpkm;
418 return 1;
419
420 default:;
421 }
422
423 *pmeth = NULL;
424 return 0;
425 }
426
gost_pkey_asn1_meths(ENGINE * e,EVP_PKEY_ASN1_METHOD ** ameth,const int ** nids,int nid)427 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
428 const int** nids, int nid) {
429 if (ameth == NULL) {
430 *nids = gost_pkey_meth_nids;
431 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
432 }
433
434 switch (nid) {
435 case NID_id_GostR3410_2001:
436 *ameth = ameth_GostR3410_2001;
437 return 1;
438 case NID_id_GostR3410_2012_256:
439 *ameth = ameth_GostR3410_2012_256;
440 return 1;
441 case NID_id_GostR3410_2012_512:
442 *ameth = ameth_GostR3410_2012_512;
443 return 1;
444 case NID_id_Gost28147_89_MAC:
445 *ameth = ameth_Gost28147_MAC;
446 return 1;
447 case NID_gost_mac_12:
448 *ameth = ameth_Gost28147_MAC_12;
449 return 1;
450 case NID_magma_mac:
451 *ameth = ameth_magma_mac;
452 return 1;
453 case NID_grasshopper_mac:
454 *ameth = ameth_grasshopper_mac;
455 return 1;
456 case NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac:
457 *ameth = ameth_magma_mac_acpkm;
458 return 1;
459 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
460 *ameth = ameth_grasshopper_mac_acpkm;
461 return 1;
462
463
464 default:;
465 }
466
467 *ameth = NULL;
468 return 0;
469 }
470
471 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
472
engine_gost(void)473 static ENGINE* engine_gost(void) {
474 ENGINE* ret = ENGINE_new();
475 if (!ret)
476 return NULL;
477 if (!bind_gost(ret, engine_gost_id)) {
478 ENGINE_free(ret);
479 return NULL;
480 }
481 return ret;
482 }
483
ENGINE_load_gost(void)484 void ENGINE_load_gost(void) {
485 ENGINE* toadd;
486 if (pmeth_GostR3410_2001)
487 return;
488 toadd = engine_gost();
489 if (!toadd)
490 return;
491 ENGINE_add(toadd);
492 ENGINE_free(toadd);
493 ERR_clear_error();
494 }
495
496 #endif
497