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,
158 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
159 cipher_gost_grasshopper_ctracpkm,
162 NID_kuznyechik_ctr_acpkm_omac,
163 cipher_gost_grasshopper_ctracpkm_omac,
170 NID_kuznyechik_kexp15,
171 cipher_kuznyechik_wrap,
176 static struct gost_meth_minfo {
178 EVP_PKEY_METHOD **pmeth;
179 EVP_PKEY_ASN1_METHOD **ameth;
182 } gost_meth_array[] = {
184 NID_id_GostR3410_2001,
185 &pmeth_GostR3410_2001,
186 &ameth_GostR3410_2001,
191 NID_id_Gost28147_89_MAC,
192 &pmeth_Gost28147_MAC,
193 &ameth_Gost28147_MAC,
198 NID_id_GostR3410_2012_256,
199 &pmeth_GostR3410_2012_256,
200 &ameth_GostR3410_2012_256,
202 "GOST R 34.10-2012 with 256 bit key",
205 NID_id_GostR3410_2012_512,
206 &pmeth_GostR3410_2012_512,
207 &ameth_GostR3410_2012_512,
209 "GOST R 34.10-2012 with 512 bit key",
213 &pmeth_Gost28147_MAC_12,
214 &ameth_Gost28147_MAC_12,
216 "GOST 28147-89 MAC with 2012 params",
223 "GOST R 34.13-2015 Magma MAC",
227 &pmeth_grasshopper_mac,
228 &ameth_grasshopper_mac,
230 "GOST R 34.13-2015 Grasshopper MAC",
233 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
234 &pmeth_magma_mac_acpkm,
235 &ameth_magma_mac_acpkm,
236 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
237 "GOST R 34.13-2015 Magma MAC ACPKM",
240 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
241 &pmeth_grasshopper_mac_acpkm,
242 &ameth_grasshopper_mac_acpkm,
243 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
244 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
250 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
253 /* `- 1' because of terminating zero element */
254 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
255 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array) - 1];
256 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
258 static int gost_engine_init(ENGINE* e) {
262 static int gost_engine_finish(ENGINE* e) {
266 static int gost_engine_destroy(ENGINE* e) {
267 struct gost_digest_minfo *dinfo = gost_digest_array;
268 for (; dinfo->nid; dinfo++) {
270 EVP_delete_digest_alias(dinfo->alias);
274 cipher_gost_destroy();
275 cipher_gost_grasshopper_destroy();
276 wrap_ciphers_destroy();
280 struct gost_meth_minfo *minfo = gost_meth_array;
281 for (; minfo->nid; minfo++) {
282 *minfo->pmeth = NULL;
283 *minfo->ameth = NULL;
286 ERR_unload_GOST_strings();
291 static int bind_gost(ENGINE* e, const char* id) {
293 if (id != NULL && strcmp(id, engine_gost_id) != 0)
295 if (ameth_GostR3410_2001) {
296 printf("GOST engine already loaded\n");
299 if (!ENGINE_set_id(e, engine_gost_id)) {
300 printf("ENGINE_set_id failed\n");
303 if (!ENGINE_set_name(e, engine_gost_name)) {
304 printf("ENGINE_set_name failed\n");
307 if (!ENGINE_set_digests(e, gost_digests)) {
308 printf("ENGINE_set_digests failed\n");
311 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
312 printf("ENGINE_set_ciphers failed\n");
315 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
316 printf("ENGINE_set_pkey_meths failed\n");
319 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
320 printf("ENGINE_set_pkey_asn1_meths failed\n");
323 /* Control function and commands */
324 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
325 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
328 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
329 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
332 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
333 || !ENGINE_set_init_function(e, gost_engine_init)
334 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
338 struct gost_meth_minfo *minfo = gost_meth_array;
339 for (; minfo->nid; minfo++) {
341 /* This skip looks temporary. */
342 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
345 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
348 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
352 if (!ENGINE_register_ciphers(e)
353 || !ENGINE_register_digests(e)
354 || !ENGINE_register_pkey_meths(e))
357 struct gost_cipher_minfo *cinfo = gost_cipher_array;
358 for (; cinfo->nid; cinfo++)
359 if (!EVP_add_cipher(cinfo->cipher()))
362 struct gost_digest_minfo *dinfo = gost_digest_array;
363 for (; dinfo->nid; dinfo++) {
364 if (!EVP_add_digest(dinfo->digest()))
367 !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
371 ENGINE_register_all_complete();
373 ERR_load_GOST_strings();
379 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
380 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
381 IMPLEMENT_DYNAMIC_CHECK_FN()
382 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
384 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
385 static int gost_digests(ENGINE *e, const EVP_MD **digest,
386 const int **nids, int nid)
388 struct gost_digest_minfo *info = gost_digest_array;
391 int *n = known_digest_nids;
394 for (; info->nid; info++)
396 return OSSL_NELEM(known_digest_nids);
399 for (; info->nid; info++)
400 if (nid == info->nid) {
401 *digest = info->digest();
408 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
409 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
410 const int **nids, int nid)
412 struct gost_cipher_minfo *info = gost_cipher_array;
415 int *n = known_cipher_nids;
418 for (; info->nid; info++)
420 return OSSL_NELEM(known_cipher_nids);
423 for (; info->nid; info++)
424 if (nid == info->nid) {
425 *cipher = info->cipher();
432 static int gost_meth_nids(const int **nids)
434 struct gost_meth_minfo *info = gost_meth_array;
435 int *n = known_meths_nids;
438 for (; info->nid; info++)
440 return OSSL_NELEM(known_meths_nids);
443 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
444 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
445 const int **nids, int nid)
447 struct gost_meth_minfo *info;
450 return gost_meth_nids(nids);
452 for (info = gost_meth_array; info->nid; info++)
453 if (nid == info->nid) {
454 *pmeth = *info->pmeth;
461 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
462 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
463 const int **nids, int nid)
465 struct gost_meth_minfo *info;
468 return gost_meth_nids(nids);
470 for (info = gost_meth_array; info->nid; info++)
471 if (nid == info->nid) {
472 *ameth = *info->ameth;
479 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
481 static ENGINE* engine_gost(void) {
482 ENGINE* ret = ENGINE_new();
485 if (!bind_gost(ret, engine_gost_id)) {
492 void ENGINE_load_gost(void) {
494 if (pmeth_GostR3410_2001)
496 toadd = engine_gost();