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_2012_256 = NULL,
43 * pmeth_GostR3410_2012_512 = NULL,
44 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
45 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL,
46 * pmeth_magma_mac_acpkm = NULL, * pmeth_grasshopper_mac_acpkm = NULL;
48 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
49 * ameth_GostR3410_2012_256 = NULL,
50 * ameth_GostR3410_2012_512 = NULL,
51 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
52 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL,
53 * ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;
55 static struct gost_digest_minfo {
57 EVP_MD *(*digest)(void);
58 void (*destroy)(void);
61 } gost_digest_array[] = {
68 NID_id_Gost28147_89_MAC,
70 imit_gost_cpa_destroy,
73 NID_id_GostR3411_2012_256,
75 digest_gost2012_256_destroy,
76 SN_id_GostR3411_2012_256,
80 NID_id_GostR3411_2012_512,
82 digest_gost2012_512_destroy,
83 SN_id_GostR3411_2012_512,
89 imit_gost_cp_12_destroy,
99 grasshopper_omac_destroy,
102 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
103 grasshopper_omac_acpkm,
104 grasshopper_omac_acpkm_destroy,
109 static struct gost_cipher_minfo {
111 const EVP_CIPHER *(*cipher)(void);
113 } gost_cipher_array[] = {
117 &Gost28147_89_cipher,
122 &Gost28147_89_cnt_cipher,
127 &Gost28147_89_cnt_12_cipher,
132 &Gost28147_89_cbc_cipher,
136 cipher_gost_grasshopper_ecb,
140 cipher_gost_grasshopper_cbc,
144 cipher_gost_grasshopper_cfb,
148 cipher_gost_grasshopper_ofb,
152 cipher_gost_grasshopper_ctr,
167 &magma_ctr_acpkm_cipher,
170 NID_magma_ctr_acpkm_omac,
172 &magma_ctr_acpkm_omac_cipher,
175 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
176 cipher_gost_grasshopper_ctracpkm,
179 NID_kuznyechik_ctr_acpkm_omac,
180 cipher_gost_grasshopper_ctracpkm_omac,
187 NID_kuznyechik_kexp15,
188 cipher_kuznyechik_wrap,
193 static struct gost_meth_minfo {
195 EVP_PKEY_METHOD **pmeth;
196 EVP_PKEY_ASN1_METHOD **ameth;
199 } gost_meth_array[] = {
201 NID_id_GostR3410_2001,
202 &pmeth_GostR3410_2001,
203 &ameth_GostR3410_2001,
208 NID_id_Gost28147_89_MAC,
209 &pmeth_Gost28147_MAC,
210 &ameth_Gost28147_MAC,
215 NID_id_GostR3410_2012_256,
216 &pmeth_GostR3410_2012_256,
217 &ameth_GostR3410_2012_256,
219 "GOST R 34.10-2012 with 256 bit key",
222 NID_id_GostR3410_2012_512,
223 &pmeth_GostR3410_2012_512,
224 &ameth_GostR3410_2012_512,
226 "GOST R 34.10-2012 with 512 bit key",
230 &pmeth_Gost28147_MAC_12,
231 &ameth_Gost28147_MAC_12,
233 "GOST 28147-89 MAC with 2012 params",
240 "GOST R 34.13-2015 Magma MAC",
244 &pmeth_grasshopper_mac,
245 &ameth_grasshopper_mac,
247 "GOST R 34.13-2015 Grasshopper MAC",
250 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
251 &pmeth_magma_mac_acpkm,
252 &ameth_magma_mac_acpkm,
253 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
254 "GOST R 34.13-2015 Magma MAC ACPKM",
257 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
258 &pmeth_grasshopper_mac_acpkm,
259 &ameth_grasshopper_mac_acpkm,
260 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
261 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
267 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
270 /* `- 1' because of terminating zero element */
271 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
272 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
273 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
275 static int gost_engine_init(ENGINE* e) {
279 static int gost_engine_finish(ENGINE* e) {
283 static int gost_engine_destroy(ENGINE* e) {
284 struct gost_digest_minfo *dinfo = gost_digest_array;
285 for (; dinfo->nid; dinfo++) {
287 EVP_delete_digest_alias(dinfo->alias);
291 struct gost_cipher_minfo *cinfo = gost_cipher_array;
292 for (; cinfo->nid; cinfo++) {
294 GOST_deinit_cipher(cinfo->reg);
296 EVP_CIPHER_meth_free((EVP_CIPHER *)cinfo->cipher());
299 //cipher_gost_grasshopper_destroy();
300 //wrap_ciphers_destroy();
304 struct gost_meth_minfo *minfo = gost_meth_array;
305 for (; minfo->nid; minfo++) {
306 *minfo->pmeth = NULL;
307 *minfo->ameth = NULL;
310 ERR_unload_GOST_strings();
315 static int bind_gost(ENGINE* e, const char* id) {
317 if (id != NULL && strcmp(id, engine_gost_id) != 0)
319 if (ameth_GostR3410_2001) {
320 printf("GOST engine already loaded\n");
323 if (!ENGINE_set_id(e, engine_gost_id)) {
324 printf("ENGINE_set_id failed\n");
327 if (!ENGINE_set_name(e, engine_gost_name)) {
328 printf("ENGINE_set_name failed\n");
331 if (!ENGINE_set_digests(e, gost_digests)) {
332 printf("ENGINE_set_digests failed\n");
335 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
336 printf("ENGINE_set_ciphers failed\n");
339 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
340 printf("ENGINE_set_pkey_meths failed\n");
343 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
344 printf("ENGINE_set_pkey_asn1_meths failed\n");
347 /* Control function and commands */
348 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
349 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
352 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
353 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
356 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
357 || !ENGINE_set_init_function(e, gost_engine_init)
358 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
362 struct gost_meth_minfo *minfo = gost_meth_array;
363 for (; minfo->nid; minfo++) {
365 /* This skip looks temporary. */
366 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
369 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
372 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
376 if (!ENGINE_register_ciphers(e)
377 || !ENGINE_register_digests(e)
378 || !ENGINE_register_pkey_meths(e))
381 struct gost_cipher_minfo *cinfo = gost_cipher_array;
382 for (; cinfo->nid; cinfo++) {
383 const EVP_CIPHER *cipher;
386 cipher = GOST_init_cipher(cinfo->reg);
388 cipher = cinfo->cipher();
389 if (!EVP_add_cipher(cipher))
393 struct gost_digest_minfo *dinfo = gost_digest_array;
394 for (; dinfo->nid; dinfo++) {
395 if (!EVP_add_digest(dinfo->digest()))
398 !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
402 ENGINE_register_all_complete();
404 ERR_load_GOST_strings();
410 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
411 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
412 IMPLEMENT_DYNAMIC_CHECK_FN()
413 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
415 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
416 static int gost_digests(ENGINE *e, const EVP_MD **digest,
417 const int **nids, int nid)
419 struct gost_digest_minfo *info = gost_digest_array;
422 int *n = known_digest_nids;
425 for (; info->nid; info++)
427 return OSSL_NELEM(known_digest_nids);
430 for (; info->nid; info++)
431 if (nid == info->nid) {
432 *digest = info->digest();
439 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
440 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
441 const int **nids, int nid)
443 struct gost_cipher_minfo *info = gost_cipher_array;
446 int *n = known_cipher_nids;
449 for (; info->nid; info++)
451 return OSSL_NELEM(known_cipher_nids);
454 for (; info->nid; info++)
455 if (nid == info->nid) {
457 *cipher = GOST_init_cipher(info->reg);
459 *cipher = info->cipher();
466 static int gost_meth_nids(const int **nids)
468 struct gost_meth_minfo *info = gost_meth_array;
469 int *n = known_meths_nids;
472 for (; info->nid; info++)
474 return OSSL_NELEM(known_meths_nids);
477 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
478 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
479 const int **nids, int nid)
481 struct gost_meth_minfo *info;
484 return gost_meth_nids(nids);
486 for (info = gost_meth_array; info->nid; info++)
487 if (nid == info->nid) {
488 *pmeth = *info->pmeth;
495 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
496 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
497 const int **nids, int nid)
499 struct gost_meth_minfo *info;
502 return gost_meth_nids(nids);
504 for (info = gost_meth_array; info->nid; info++)
505 if (nid == info->nid) {
506 *ameth = *info->ameth;
513 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
515 static ENGINE* engine_gost(void) {
516 ENGINE* ret = ENGINE_new();
519 if (!bind_gost(ret, engine_gost_id)) {
526 void ENGINE_load_gost(void) {
528 if (pmeth_GostR3410_2001)
530 toadd = engine_gost();