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 GOST_cipher *gost_cipher_array[] = {
110 &Gost28147_89_cipher,
111 &Gost28147_89_cnt_cipher,
112 &Gost28147_89_cnt_12_cipher,
113 &Gost28147_89_cbc_cipher,
114 &grasshopper_ecb_cipher,
115 &grasshopper_cbc_cipher,
116 &grasshopper_cfb_cipher,
117 &grasshopper_ofb_cipher,
118 &grasshopper_ctr_cipher,
121 &magma_ctr_acpkm_cipher,
122 &magma_ctr_acpkm_omac_cipher,
123 &grasshopper_ctr_acpkm_cipher,
124 &grasshopper_ctr_acpkm_omac_cipher,
125 &magma_kexp15_cipher,
126 &kuznyechik_kexp15_cipher,
129 static struct gost_meth_minfo {
131 EVP_PKEY_METHOD **pmeth;
132 EVP_PKEY_ASN1_METHOD **ameth;
135 } gost_meth_array[] = {
137 NID_id_GostR3410_2001,
138 &pmeth_GostR3410_2001,
139 &ameth_GostR3410_2001,
144 NID_id_Gost28147_89_MAC,
145 &pmeth_Gost28147_MAC,
146 &ameth_Gost28147_MAC,
151 NID_id_GostR3410_2012_256,
152 &pmeth_GostR3410_2012_256,
153 &ameth_GostR3410_2012_256,
155 "GOST R 34.10-2012 with 256 bit key",
158 NID_id_GostR3410_2012_512,
159 &pmeth_GostR3410_2012_512,
160 &ameth_GostR3410_2012_512,
162 "GOST R 34.10-2012 with 512 bit key",
166 &pmeth_Gost28147_MAC_12,
167 &ameth_Gost28147_MAC_12,
169 "GOST 28147-89 MAC with 2012 params",
176 "GOST R 34.13-2015 Magma MAC",
180 &pmeth_grasshopper_mac,
181 &ameth_grasshopper_mac,
183 "GOST R 34.13-2015 Grasshopper MAC",
186 NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac,
187 &pmeth_magma_mac_acpkm,
188 &ameth_magma_mac_acpkm,
189 "ID-TC26-CIPHER-GOSTR3412-2015-MAGMA-CTRACPKM-OMAC",
190 "GOST R 34.13-2015 Magma MAC ACPKM",
193 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
194 &pmeth_grasshopper_mac_acpkm,
195 &ameth_grasshopper_mac_acpkm,
196 "ID-TC26-CIPHER-GOSTR3412-2015-KUZNYECHIK-CTRACPKM-OMAC",
197 "GOST R 34.13-2015 Grasshopper MAC ACPKM",
203 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
206 /* `- 1' because of terminating zero element */
207 static int known_digest_nids[OSSL_NELEM(gost_digest_array) - 1];
208 static int known_cipher_nids[OSSL_NELEM(gost_cipher_array)];
209 static int known_meths_nids[OSSL_NELEM(gost_meth_array) - 1];
211 static int gost_engine_init(ENGINE* e) {
215 static int gost_engine_finish(ENGINE* e) {
219 static int gost_engine_destroy(ENGINE* e) {
220 struct gost_digest_minfo *dinfo = gost_digest_array;
221 for (; dinfo->nid; dinfo++) {
223 EVP_delete_digest_alias(dinfo->alias);
228 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
229 GOST_deinit_cipher(gost_cipher_array[i]);
233 struct gost_meth_minfo *minfo = gost_meth_array;
234 for (; minfo->nid; minfo++) {
235 *minfo->pmeth = NULL;
236 *minfo->ameth = NULL;
239 ERR_unload_GOST_strings();
244 static int bind_gost(ENGINE* e, const char* id) {
246 if (id != NULL && strcmp(id, engine_gost_id) != 0)
248 if (ameth_GostR3410_2001) {
249 printf("GOST engine already loaded\n");
252 if (!ENGINE_set_id(e, engine_gost_id)) {
253 printf("ENGINE_set_id failed\n");
256 if (!ENGINE_set_name(e, engine_gost_name)) {
257 printf("ENGINE_set_name failed\n");
260 if (!ENGINE_set_digests(e, gost_digests)) {
261 printf("ENGINE_set_digests failed\n");
264 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
265 printf("ENGINE_set_ciphers failed\n");
268 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
269 printf("ENGINE_set_pkey_meths failed\n");
272 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
273 printf("ENGINE_set_pkey_asn1_meths failed\n");
276 /* Control function and commands */
277 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
278 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
281 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
282 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
285 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
286 || !ENGINE_set_init_function(e, gost_engine_init)
287 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
291 struct gost_meth_minfo *minfo = gost_meth_array;
292 for (; minfo->nid; minfo++) {
294 /* This skip looks temporary. */
295 if (minfo->nid == NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac)
298 if (!register_ameth_gost(minfo->nid, minfo->ameth, minfo->pemstr,
301 if (!register_pmeth_gost(minfo->nid, minfo->pmeth, 0))
305 if (!ENGINE_register_ciphers(e)
306 || !ENGINE_register_digests(e)
307 || !ENGINE_register_pkey_meths(e))
311 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++) {
312 if (!EVP_add_cipher(GOST_init_cipher(gost_cipher_array[i])))
316 struct gost_digest_minfo *dinfo = gost_digest_array;
317 for (; dinfo->nid; dinfo++) {
318 if (!EVP_add_digest(dinfo->digest()))
321 !EVP_add_digest_alias(dinfo->sn, dinfo->alias))
325 ENGINE_register_all_complete();
327 ERR_load_GOST_strings();
333 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
334 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
335 IMPLEMENT_DYNAMIC_CHECK_FN()
336 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
338 /* ENGINE_DIGESTS_PTR callback installed by ENGINE_set_digests */
339 static int gost_digests(ENGINE *e, const EVP_MD **digest,
340 const int **nids, int nid)
342 struct gost_digest_minfo *info = gost_digest_array;
345 int *n = known_digest_nids;
348 for (; info->nid; info++)
350 return OSSL_NELEM(known_digest_nids);
353 for (; info->nid; info++)
354 if (nid == info->nid) {
355 *digest = info->digest();
362 /* ENGINE_CIPHERS_PTR callback installed by ENGINE_set_ciphers */
363 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
364 const int **nids, int nid)
369 int *n = known_cipher_nids;
372 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
373 *n++ = gost_cipher_array[i]->nid;
377 for (i = 0; i < OSSL_NELEM(gost_cipher_array); i++)
378 if (nid == gost_cipher_array[i]->nid) {
379 *cipher = GOST_init_cipher(gost_cipher_array[i]);
386 static int gost_meth_nids(const int **nids)
388 struct gost_meth_minfo *info = gost_meth_array;
389 int *n = known_meths_nids;
392 for (; info->nid; info++)
394 return OSSL_NELEM(known_meths_nids);
397 /* ENGINE_PKEY_METHS_PTR installed by ENGINE_set_pkey_meths */
398 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
399 const int **nids, int nid)
401 struct gost_meth_minfo *info;
404 return gost_meth_nids(nids);
406 for (info = gost_meth_array; info->nid; info++)
407 if (nid == info->nid) {
408 *pmeth = *info->pmeth;
415 /* ENGINE_PKEY_ASN1_METHS_PTR installed by ENGINE_set_pkey_asn1_meths */
416 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
417 const int **nids, int nid)
419 struct gost_meth_minfo *info;
422 return gost_meth_nids(nids);
424 for (info = gost_meth_array; info->nid; info++)
425 if (nid == info->nid) {
426 *ameth = *info->ameth;
433 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
435 static ENGINE* engine_gost(void) {
436 ENGINE* ret = ENGINE_new();
439 if (!bind_gost(ret, engine_gost_id)) {
446 void ENGINE_load_gost(void) {
448 if (pmeth_GostR3410_2001)
450 toadd = engine_gost();
459 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */