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,
125 cipher_gost_cpcnt_12,
133 cipher_gost_grasshopper_ecb,
137 cipher_gost_grasshopper_cbc,
141 cipher_gost_grasshopper_cfb,
145 cipher_gost_grasshopper_ofb,
149 cipher_gost_grasshopper_ctr,
161 cipher_magma_ctr_acpkm,
164 NID_magma_ctr_acpkm_omac,
165 cipher_magma_ctr_acpkm_omac,
168 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
169 cipher_gost_grasshopper_ctracpkm,
172 NID_kuznyechik_ctr_acpkm_omac,
173 cipher_gost_grasshopper_ctracpkm_omac,
180 NID_kuznyechik_kexp15,
181 cipher_kuznyechik_wrap,
186 static struct gost_meth_minfo {
188 EVP_PKEY_METHOD **pmeth;
189 EVP_PKEY_ASN1_METHOD **ameth;
192 } gost_meth_array[] = {
194 NID_id_GostR3410_2001,
195 &pmeth_GostR3410_2001,
196 &ameth_GostR3410_2001,
201 NID_id_Gost28147_89_MAC,
202 &pmeth_Gost28147_MAC,
203 &ameth_Gost28147_MAC,
208 NID_id_GostR3410_2012_256,
209 &pmeth_GostR3410_2012_256,
210 &ameth_GostR3410_2012_256,
212 "GOST R 34.10-2012 with 256 bit key",
215 NID_id_GostR3410_2012_512,
216 &pmeth_GostR3410_2012_512,
217 &ameth_GostR3410_2012_512,
219 "GOST R 34.10-2012 with 512 bit key",
223 &pmeth_Gost28147_MAC_12,
224 &ameth_Gost28147_MAC_12,
226 "GOST 28147-89 MAC with 2012 params",
233 "GOST R 34.13-2015 Magma MAC",
237 &pmeth_grasshopper_mac,
238 &ameth_grasshopper_mac,
240 "GOST R 34.13-2015 Grasshopper MAC",
243 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
244 &pmeth_magma_mac_acpkm,
245 &ameth_magma_mac_acpkm,
246 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
247 "GOST R 34.13-2015 Magma MAC ACPKM",
250 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
251 &pmeth_grasshopper_mac_acpkm,
252 &ameth_grasshopper_mac_acpkm,
253 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
254 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
260 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
263 /* `- 1' because of terminating zero element */
264 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
265 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
266 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
268 static int gost_engine_init(ENGINE* e) {
272 static int gost_engine_finish(ENGINE* e) {
276 static int gost_engine_destroy(ENGINE* e) {
277 struct gost_digest_minfo *dinfo = gost_digest_array;
278 for (; dinfo->nid; dinfo++) {
280 EVP_delete_digest_alias(dinfo->alias);
284 struct gost_cipher_minfo *cinfo = gost_cipher_array;
285 for (; cinfo->nid; cinfo++) {
287 GOST_deinit_cipher(cinfo->reg);
289 EVP_CIPHER_meth_free((EVP_CIPHER *)cinfo->cipher());
292 //cipher_gost_grasshopper_destroy();
293 //wrap_ciphers_destroy();
297 struct gost_meth_minfo *minfo = gost_meth_array;
298 for (; minfo->nid; minfo++) {
299 *minfo->pmeth = NULL;
300 *minfo->ameth = NULL;
303 ERR_unload_GOST_strings();
308 static int bind_gost(ENGINE* e, const char* id) {
310 if (id != NULL && strcmp(id, engine_gost_id) != 0)
312 if (ameth_GostR3410_2001) {
313 printf("GOST engine already loaded\n");
316 if (!ENGINE_set_id(e, engine_gost_id)) {
317 printf("ENGINE_set_id failed\n");
320 if (!ENGINE_set_name(e, engine_gost_name)) {
321 printf("ENGINE_set_name failed\n");
324 if (!ENGINE_set_digests(e, gost_digests)) {
325 printf("ENGINE_set_digests failed\n");
328 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
329 printf("ENGINE_set_ciphers failed\n");
332 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
333 printf("ENGINE_set_pkey_meths failed\n");
336 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
337 printf("ENGINE_set_pkey_asn1_meths failed\n");
340 /* Control function and commands */
341 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
342 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
345 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
346 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
349 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
350 || !ENGINE_set_init_function(e, gost_engine_init)
351 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
355 struct gost_meth_minfo *minfo = gost_meth_array;
356 for (; minfo->nid; minfo++) {
358 /* This skip looks temporary. */
359 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
362 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
365 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
369 if (!ENGINE_register_ciphers(e)
370 || !ENGINE_register_digests(e)
371 || !ENGINE_register_pkey_meths(e))
374 struct gost_cipher_minfo *cinfo = gost_cipher_array;
375 for (; cinfo->nid; cinfo++) {
376 const EVP_CIPHER *cipher;
379 cipher = GOST_init_cipher(cinfo->reg);
381 cipher = cinfo->cipher();
382 if (!EVP_add_cipher(cipher))
386 struct gost_digest_minfo *dinfo = gost_digest_array;
387 for (; dinfo->nid; dinfo++) {
388 if (!EVP_add_digest(dinfo->digest()))
391 !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
395 ENGINE_register_all_complete();
397 ERR_load_GOST_strings();
403 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
404 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
405 IMPLEMENT_DYNAMIC_CHECK_FN()
406 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
408 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
409 static int gost_digests(ENGINE *e, const EVP_MD **digest,
410 const int **nids, int nid)
412 struct gost_digest_minfo *info = gost_digest_array;
415 int *n = known_digest_nids;
418 for (; info->nid; info++)
420 return OSSL_NELEM(known_digest_nids);
423 for (; info->nid; info++)
424 if (nid == info->nid) {
425 *digest = info->digest();
432 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
433 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
434 const int **nids, int nid)
436 struct gost_cipher_minfo *info = gost_cipher_array;
439 int *n = known_cipher_nids;
442 for (; info->nid; info++)
444 return OSSL_NELEM(known_cipher_nids);
447 for (; info->nid; info++)
448 if (nid == info->nid) {
450 *cipher = GOST_init_cipher(info->reg);
452 *cipher = info->cipher();
459 static int gost_meth_nids(const int **nids)
461 struct gost_meth_minfo *info = gost_meth_array;
462 int *n = known_meths_nids;
465 for (; info->nid; info++)
467 return OSSL_NELEM(known_meths_nids);
470 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
471 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
472 const int **nids, int nid)
474 struct gost_meth_minfo *info;
477 return gost_meth_nids(nids);
479 for (info = gost_meth_array; info->nid; info++)
480 if (nid == info->nid) {
481 *pmeth = *info->pmeth;
488 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
489 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
490 const int **nids, int nid)
492 struct gost_meth_minfo *info;
495 return gost_meth_nids(nids);
497 for (info = gost_meth_array; info->nid; info++)
498 if (nid == info->nid) {
499 *ameth = *info->ameth;
506 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
508 static ENGINE* engine_gost(void) {
509 ENGINE* ret = ENGINE_new();
512 if (!bind_gost(ret, engine_gost_id)) {
519 void ENGINE_load_gost(void) {
521 if (pmeth_GostR3410_2001)
523 toadd = engine_gost();