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_2001DH = NULL,
43 * pmeth_GostR3410_2012_256 = NULL,
44 * pmeth_GostR3410_2012_512 = NULL,
45 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL,
46 * pmeth_magma_mac = NULL, * pmeth_grasshopper_mac = NULL,
47 * pmeth_magma_mac_acpkm = NULL, * pmeth_grasshopper_mac_acpkm = NULL;
49 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
50 * ameth_GostR3410_2001DH = NULL,
51 * ameth_GostR3410_2012_256 = NULL,
52 * ameth_GostR3410_2012_512 = NULL,
53 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL,
54 * ameth_magma_mac = NULL, * ameth_grasshopper_mac = NULL,
55 * ameth_magma_mac_acpkm = NULL, * ameth_grasshopper_mac_acpkm = NULL;
57 GOST_digest *gost_digest_array[] = {
59 &Gost28147_89_MAC_digest,
60 &GostR3411_2012_256_digest,
61 &GostR3411_2012_512_digest,
62 &Gost28147_89_mac_12_digest,
64 &grasshopper_mac_digest,
65 &kuznyechik_ctracpkm_omac_digest,
68 GOST_cipher *gost_cipher_array[] = {
70 &Gost28147_89_cnt_cipher,
71 &Gost28147_89_cnt_12_cipher,
72 &Gost28147_89_cbc_cipher,
73 &grasshopper_ecb_cipher,
74 &grasshopper_cbc_cipher,
75 &grasshopper_cfb_cipher,
76 &grasshopper_ofb_cipher,
77 &grasshopper_ctr_cipher,
78 &grasshopper_mgm_cipher,
81 &magma_ctr_acpkm_cipher,
82 &magma_ctr_acpkm_omac_cipher,
84 &grasshopper_ctr_acpkm_cipher,
85 &grasshopper_ctr_acpkm_omac_cipher,
87 &kuznyechik_kexp15_cipher,
90 static struct gost_meth_minfo {
92 EVP_PKEY_METHOD **pmeth;
93 EVP_PKEY_ASN1_METHOD **ameth;
96 } gost_meth_array[] = {
98 NID_id_GostR3410_2001,
99 &pmeth_GostR3410_2001,
100 &ameth_GostR3410_2001,
105 NID_id_GostR3410_2001DH,
106 &pmeth_GostR3410_2001DH,
107 &ameth_GostR3410_2001DH,
109 "GOST R 34.10-2001 DH",
112 NID_id_Gost28147_89_MAC,
113 &pmeth_Gost28147_MAC,
114 &ameth_Gost28147_MAC,
119 NID_id_GostR3410_2012_256,
120 &pmeth_GostR3410_2012_256,
121 &ameth_GostR3410_2012_256,
123 "GOST R 34.10-2012 with 256 bit key",
126 NID_id_GostR3410_2012_512,
127 &pmeth_GostR3410_2012_512,
128 &ameth_GostR3410_2012_512,
130 "GOST R 34.10-2012 with 512 bit key",
134 &pmeth_Gost28147_MAC_12,
135 &ameth_Gost28147_MAC_12,
137 "GOST 28147-89 MAC with 2012 params",
144 "GOST R 34.13-2015 Magma MAC",
148 &pmeth_grasshopper_mac,
149 &ameth_grasshopper_mac,
151 "GOST R 34.13-2015 Grasshopper MAC",
154 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
155 &pmeth_magma_mac_acpkm,
156 &ameth_magma_mac_acpkm,
157 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
158 "GOST R 34.13-2015 Magma MAC ACPKM",
161 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
162 &pmeth_grasshopper_mac_acpkm,
163 &ameth_grasshopper_mac_acpkm,
164 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
165 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
171 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
174 static int known_digest_nids[OSSL_NELEM(gost_digest_array)];
175 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
176 /* `- 1' because of terminating zero element */
177 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
179 static int gost_engine_init(ENGINE* e) {
183 static int gost_engine_finish(ENGINE* e) {
187 static int gost_engine_destroy(ENGINE* e) {
190 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
191 GOST_deinit_digest(gost_digest_array[i]);
192 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
193 GOST_deinit_cipher(gost_cipher_array[i]);
197 struct gost_meth_minfo *minfo = gost_meth_array;
198 for (; minfo->nid; minfo++) {
199 *minfo->pmeth = NULL;
200 *minfo->ameth = NULL;
203 ERR_unload_GOST_strings();
208 static int bind_gost(ENGINE* e, const char* id) {
210 if (id != NULL && strcmp(id, engine_gost_id) != 0)
212 if (ameth_GostR3410_2001) {
213 printf("GOST engine already loaded\n");
216 if (!ENGINE_set_id(e, engine_gost_id)) {
217 printf("ENGINE_set_id failed\n");
220 if (!ENGINE_set_name(e, engine_gost_name)) {
221 printf("ENGINE_set_name failed\n");
224 if (!ENGINE_set_digests(e, gost_digests)) {
225 printf("ENGINE_set_digests failed\n");
228 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
229 printf("ENGINE_set_ciphers failed\n");
232 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
233 printf("ENGINE_set_pkey_meths failed\n");
236 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
237 printf("ENGINE_set_pkey_asn1_meths failed\n");
240 /* Control function and commands */
241 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
242 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
245 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
246 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
249 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
250 || !ENGINE_set_init_function(e, gost_engine_init)
251 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
255 struct gost_meth_minfo *minfo = gost_meth_array;
256 for (; minfo->nid; minfo++) {
258 /* This skip looks temporary. */
259 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
262 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
265 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
269 if (!ENGINE_register_ciphers(e)
270 || !ENGINE_register_digests(e)
271 || !ENGINE_register_pkey_meths(e))
275 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
276 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
280 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
281 if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
285 ENGINE_register_all_complete();
287 ERR_load_GOST_strings();
293 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
294 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
295 IMPLEMENT_DYNAMIC_CHECK_FN()
296 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
298 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
299 static int gost_digests(ENGINE *e, const EVP_MD **digest,
300 const int **nids, int nid)
305 int *n = known_digest_nids;
308 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
309 *n++ = gost_digest_array[i]->nid;
313 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
314 if (nid == gost_digest_array[i]->nid) {
315 *digest = GOST_init_digest(gost_digest_array[i]);
322 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
323 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
324 const int **nids, int nid)
329 int *n = known_cipher_nids;
332 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
333 *n++ = gost_cipher_array[i]->nid;
337 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
338 if (nid == gost_cipher_array[i]->nid) {
339 *cipher = GOST_init_cipher(gost_cipher_array[i]);
346 static int gost_meth_nids(const int **nids)
348 struct gost_meth_minfo *info = gost_meth_array;
349 int *n = known_meths_nids;
352 for (; info->nid; info++)
354 return OSSL_NELEM(known_meths_nids);
357 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
358 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
359 const int **nids, int nid)
361 struct gost_meth_minfo *info;
364 return gost_meth_nids(nids);
366 for (info = gost_meth_array; info->nid; info++)
367 if (nid == info->nid) {
368 *pmeth = *info->pmeth;
375 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
376 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
377 const int **nids, int nid)
379 struct gost_meth_minfo *info;
382 return gost_meth_nids(nids);
384 for (info = gost_meth_array; info->nid; info++)
385 if (nid == info->nid) {
386 *ameth = *info->ameth;
393 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
395 static ENGINE* engine_gost(void) {
396 ENGINE* ret = ENGINE_new();
399 if (!bind_gost(ret, engine_gost_id)) {
406 void ENGINE_load_gost(void) {
408 if (pmeth_GostR3410_2001)
410 toadd = engine_gost();
419 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */