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 GOST_digest *gost_digest_array[] = {
57 &Gost28147_89_MAC_digest,
58 &GostR3411_2012_256_digest,
59 &GostR3411_2012_512_digest,
60 &Gost28147_89_mac_12_digest,
62 &grasshopper_mac_digest,
63 &kuznyechik_ctracpkm_omac_digest,
66 GOST_cipher *gost_cipher_array[] = {
68 &Gost28147_89_cnt_cipher,
69 &Gost28147_89_cnt_12_cipher,
70 &Gost28147_89_cbc_cipher,
71 &grasshopper_ecb_cipher,
72 &grasshopper_cbc_cipher,
73 &grasshopper_cfb_cipher,
74 &grasshopper_ofb_cipher,
75 &grasshopper_ctr_cipher,
78 &magma_ctr_acpkm_cipher,
79 &magma_ctr_acpkm_omac_cipher,
80 &grasshopper_ctr_acpkm_cipher,
81 &grasshopper_ctr_acpkm_omac_cipher,
83 &kuznyechik_kexp15_cipher,
86 static struct gost_meth_minfo {
88 EVP_PKEY_METHOD **pmeth;
89 EVP_PKEY_ASN1_METHOD **ameth;
92 } gost_meth_array[] = {
94 NID_id_GostR3410_2001,
95 &pmeth_GostR3410_2001,
96 &ameth_GostR3410_2001,
101 NID_id_Gost28147_89_MAC,
102 &pmeth_Gost28147_MAC,
103 &ameth_Gost28147_MAC,
108 NID_id_GostR3410_2012_256,
109 &pmeth_GostR3410_2012_256,
110 &ameth_GostR3410_2012_256,
112 "GOST R 34.10-2012 with 256 bit key",
115 NID_id_GostR3410_2012_512,
116 &pmeth_GostR3410_2012_512,
117 &ameth_GostR3410_2012_512,
119 "GOST R 34.10-2012 with 512 bit key",
123 &pmeth_Gost28147_MAC_12,
124 &ameth_Gost28147_MAC_12,
126 "GOST 28147-89 MAC with 2012 params",
133 "GOST R 34.13-2015 Magma MAC",
137 &pmeth_grasshopper_mac,
138 &ameth_grasshopper_mac,
140 "GOST R 34.13-2015 Grasshopper MAC",
143 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
144 &pmeth_magma_mac_acpkm,
145 &ameth_magma_mac_acpkm,
146 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
147 "GOST R 34.13-2015 Magma MAC ACPKM",
150 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
151 &pmeth_grasshopper_mac_acpkm,
152 &ameth_grasshopper_mac_acpkm,
153 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
154 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
160 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
163 static int known_digest_nids[OSSL_NELEM(gost_digest_array)];
164 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
165 /* `- 1' because of terminating zero element */
166 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
168 static int gost_engine_init(ENGINE* e) {
172 static int gost_engine_finish(ENGINE* e) {
176 static int gost_engine_destroy(ENGINE* e) {
179 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
180 GOST_deinit_digest(gost_digest_array[i]);
181 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
182 GOST_deinit_cipher(gost_cipher_array[i]);
186 struct gost_meth_minfo *minfo = gost_meth_array;
187 for (; minfo->nid; minfo++) {
188 *minfo->pmeth = NULL;
189 *minfo->ameth = NULL;
192 ERR_unload_GOST_strings();
197 static int bind_gost(ENGINE* e, const char* id) {
199 if (id != NULL && strcmp(id, engine_gost_id) != 0)
201 if (ameth_GostR3410_2001) {
202 printf("GOST engine already loaded\n");
205 if (!ENGINE_set_id(e, engine_gost_id)) {
206 printf("ENGINE_set_id failed\n");
209 if (!ENGINE_set_name(e, engine_gost_name)) {
210 printf("ENGINE_set_name failed\n");
213 if (!ENGINE_set_digests(e, gost_digests)) {
214 printf("ENGINE_set_digests failed\n");
217 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
218 printf("ENGINE_set_ciphers failed\n");
221 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
222 printf("ENGINE_set_pkey_meths failed\n");
225 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
226 printf("ENGINE_set_pkey_asn1_meths failed\n");
229 /* Control function and commands */
230 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
231 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
234 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
235 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
238 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
239 || !ENGINE_set_init_function(e, gost_engine_init)
240 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
244 struct gost_meth_minfo *minfo = gost_meth_array;
245 for (; minfo->nid; minfo++) {
247 /* This skip looks temporary. */
248 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
251 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
254 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
258 if (!ENGINE_register_ciphers(e)
259 || !ENGINE_register_digests(e)
260 || !ENGINE_register_pkey_meths(e))
264 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
265 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
269 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++) {
270 if (!EVP_add_digest(GOST_init_digest(gost_digest_array[i])))
274 ENGINE_register_all_complete();
276 ERR_load_GOST_strings();
282 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
283 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
284 IMPLEMENT_DYNAMIC_CHECK_FN()
285 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
287 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
288 static int gost_digests(ENGINE *e, const EVP_MD **digest,
289 const int **nids, int nid)
294 int *n = known_digest_nids;
297 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
298 *n++ = gost_digest_array[i]->nid;
302 for (i = 0; i < OSSL_NELEM(gost_digest_array); i++)
303 if (nid == gost_digest_array[i]->nid) {
304 *digest = GOST_init_digest(gost_digest_array[i]);
311 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
312 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
313 const int **nids, int nid)
318 int *n = known_cipher_nids;
321 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
322 *n++ = gost_cipher_array[i]->nid;
326 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
327 if (nid == gost_cipher_array[i]->nid) {
328 *cipher = GOST_init_cipher(gost_cipher_array[i]);
335 static int gost_meth_nids(const int **nids)
337 struct gost_meth_minfo *info = gost_meth_array;
338 int *n = known_meths_nids;
341 for (; info->nid; info++)
343 return OSSL_NELEM(known_meths_nids);
346 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
347 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
348 const int **nids, int nid)
350 struct gost_meth_minfo *info;
353 return gost_meth_nids(nids);
355 for (info = gost_meth_array; info->nid; info++)
356 if (nid == info->nid) {
357 *pmeth = *info->pmeth;
364 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
365 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
366 const int **nids, int nid)
368 struct gost_meth_minfo *info;
371 return gost_meth_nids(nids);
373 for (info = gost_meth_array; info->nid; info++)
374 if (nid == info->nid) {
375 *ameth = *info->ameth;
382 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
384 static ENGINE* engine_gost(void) {
385 ENGINE* ret = ENGINE_new();
388 if (!bind_gost(ret, engine_gost_id)) {
395 void ENGINE_load_gost(void) {
397 if (pmeth_GostR3410_2001)
399 toadd = engine_gost();
408 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */