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);
112 } gost_cipher_array[] = {
123 cipher_gost_cpcnt_12,
131 cipher_gost_grasshopper_ecb,
135 cipher_gost_grasshopper_cbc,
139 cipher_gost_grasshopper_cfb,
143 cipher_gost_grasshopper_ofb,
147 cipher_gost_grasshopper_ctr,
159 cipher_magma_ctr_acpkm,
162 NID_magma_ctr_acpkm_omac,
163 cipher_magma_ctr_acpkm_omac,
166 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
167 cipher_gost_grasshopper_ctracpkm,
170 NID_kuznyechik_ctr_acpkm_omac,
171 cipher_gost_grasshopper_ctracpkm_omac,
178 NID_kuznyechik_kexp15,
179 cipher_kuznyechik_wrap,
184 static struct gost_meth_minfo {
186 EVP_PKEY_METHOD **pmeth;
187 EVP_PKEY_ASN1_METHOD **ameth;
190 } gost_meth_array[] = {
192 NID_id_GostR3410_2001,
193 &pmeth_GostR3410_2001,
194 &ameth_GostR3410_2001,
199 NID_id_Gost28147_89_MAC,
200 &pmeth_Gost28147_MAC,
201 &ameth_Gost28147_MAC,
206 NID_id_GostR3410_2012_256,
207 &pmeth_GostR3410_2012_256,
208 &ameth_GostR3410_2012_256,
210 "GOST R 34.10-2012 with 256 bit key",
213 NID_id_GostR3410_2012_512,
214 &pmeth_GostR3410_2012_512,
215 &ameth_GostR3410_2012_512,
217 "GOST R 34.10-2012 with 512 bit key",
221 &pmeth_Gost28147_MAC_12,
222 &ameth_Gost28147_MAC_12,
224 "GOST 28147-89 MAC with 2012 params",
231 "GOST R 34.13-2015 Magma MAC",
235 &pmeth_grasshopper_mac,
236 &ameth_grasshopper_mac,
238 "GOST R 34.13-2015 Grasshopper MAC",
241 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
242 &pmeth_magma_mac_acpkm,
243 &ameth_magma_mac_acpkm,
244 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
245 "GOST R 34.13-2015 Magma MAC ACPKM",
248 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
249 &pmeth_grasshopper_mac_acpkm,
250 &ameth_grasshopper_mac_acpkm,
251 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
252 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
258 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
261 /* `- 1' because of terminating zero element */
262 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
263 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
264 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
266 static int gost_engine_init(ENGINE* e) {
270 static int gost_engine_finish(ENGINE* e) {
274 static int gost_engine_destroy(ENGINE* e) {
275 struct gost_digest_minfo *dinfo = gost_digest_array;
276 for (; dinfo->nid; dinfo++) {
278 EVP_delete_digest_alias(dinfo->alias);
282 cipher_gost_destroy();
283 cipher_gost_grasshopper_destroy();
284 wrap_ciphers_destroy();
288 struct gost_meth_minfo *minfo = gost_meth_array;
289 for (; minfo->nid; minfo++) {
290 *minfo->pmeth = NULL;
291 *minfo->ameth = NULL;
294 ERR_unload_GOST_strings();
299 static int bind_gost(ENGINE* e, const char* id) {
301 if (id != NULL && strcmp(id, engine_gost_id) != 0)
303 if (ameth_GostR3410_2001) {
304 printf("GOST engine already loaded\n");
307 if (!ENGINE_set_id(e, engine_gost_id)) {
308 printf("ENGINE_set_id failed\n");
311 if (!ENGINE_set_name(e, engine_gost_name)) {
312 printf("ENGINE_set_name failed\n");
315 if (!ENGINE_set_digests(e, gost_digests)) {
316 printf("ENGINE_set_digests failed\n");
319 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
320 printf("ENGINE_set_ciphers failed\n");
323 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
324 printf("ENGINE_set_pkey_meths failed\n");
327 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
328 printf("ENGINE_set_pkey_asn1_meths failed\n");
331 /* Control function and commands */
332 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
333 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
336 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
337 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
340 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
341 || !ENGINE_set_init_function(e, gost_engine_init)
342 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
346 struct gost_meth_minfo *minfo = gost_meth_array;
347 for (; minfo->nid; minfo++) {
349 /* This skip looks temporary. */
350 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
353 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
356 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
360 if (!ENGINE_register_ciphers(e)
361 || !ENGINE_register_digests(e)
362 || !ENGINE_register_pkey_meths(e))
365 struct gost_cipher_minfo *cinfo = gost_cipher_array;
366 for (; cinfo->nid; cinfo++)
367 if (!EVP_add_cipher(cinfo->cipher()))
370 struct gost_digest_minfo *dinfo = gost_digest_array;
371 for (; dinfo->nid; dinfo++) {
372 if (!EVP_add_digest(dinfo->digest()))
375 !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
379 ENGINE_register_all_complete();
381 ERR_load_GOST_strings();
387 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
388 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
389 IMPLEMENT_DYNAMIC_CHECK_FN()
390 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
392 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
393 static int gost_digests(ENGINE *e, const EVP_MD **digest,
394 const int **nids, int nid)
396 struct gost_digest_minfo *info = gost_digest_array;
399 int *n = known_digest_nids;
402 for (; info->nid; info++)
404 return OSSL_NELEM(known_digest_nids);
407 for (; info->nid; info++)
408 if (nid == info->nid) {
409 *digest = info->digest();
416 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
417 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
418 const int **nids, int nid)
420 struct gost_cipher_minfo *info = gost_cipher_array;
423 int *n = known_cipher_nids;
426 for (; info->nid; info++)
428 return OSSL_NELEM(known_cipher_nids);
431 for (; info->nid; info++)
432 if (nid == info->nid) {
433 *cipher = info->cipher();
440 static int gost_meth_nids(const int **nids)
442 struct gost_meth_minfo *info = gost_meth_array;
443 int *n = known_meths_nids;
446 for (; info->nid; info++)
448 return OSSL_NELEM(known_meths_nids);
451 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
452 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
453 const int **nids, int nid)
455 struct gost_meth_minfo *info;
458 return gost_meth_nids(nids);
460 for (info = gost_meth_array; info->nid; info++)
461 if (nid == info->nid) {
462 *pmeth = *info->pmeth;
469 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
470 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
471 const int **nids, int nid)
473 struct gost_meth_minfo *info;
476 return gost_meth_nids(nids);
478 for (info = gost_meth_array; info->nid; info++)
479 if (nid == info->nid) {
480 *ameth = *info->ameth;
487 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
489 static ENGINE* engine_gost(void) {
490 ENGINE* ret = ENGINE_new();
493 if (!bind_gost(ret, engine_gost_id)) {
500 void ENGINE_load_gost(void) {
502 if (pmeth_GostR3410_2001)
504 toadd = engine_gost();