1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Main file of GOST engine *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include <openssl/obj_mac.h>
16 #include "e_gost_err.h"
18 static const char *engine_gost_id = "gost";
19 static const char *engine_gost_name =
20 "Reference implementation of GOST engine";
22 /* Symmetric cipher and digest function registrar */
24 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
25 const int **nids, int nid);
27 static int gost_digests(ENGINE *e, const EVP_MD **digest,
28 const int **nids, int ind);
30 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
31 const int **nids, int nid);
33 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
34 const int **nids, int nid);
36 static int gost_cipher_nids[] = {
43 static int gost_digest_nids[] = {
45 NID_id_Gost28147_89_MAC,
46 NID_id_GostR3411_2012_256,
47 NID_id_GostR3411_2012_512,
52 static int gost_pkey_meth_nids[] = {
54 NID_id_GostR3410_2001,
55 NID_id_Gost28147_89_MAC,
56 NID_id_GostR3410_2012_256,
57 NID_id_GostR3410_2012_512,
62 static EVP_PKEY_METHOD *pmeth_GostR3410_94 = NULL,
63 *pmeth_GostR3410_2001 = NULL,
64 *pmeth_GostR3410_2012_256 = NULL,
65 *pmeth_GostR3410_2012_512 = NULL,
66 *pmeth_Gost28147_MAC = NULL,
67 *pmeth_Gost28147_MAC_12 = NULL;
69 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_94 = NULL,
70 *ameth_GostR3410_2001 = NULL,
71 *ameth_GostR3410_2012_256 = NULL,
72 *ameth_GostR3410_2012_512 = NULL,
73 *ameth_Gost28147_MAC = NULL,
74 *ameth_Gost28147_MAC_12 = NULL;
76 static int gost_engine_init(ENGINE *e)
81 static int gost_engine_finish(ENGINE *e)
86 static int gost_engine_destroy(ENGINE *e)
90 pmeth_GostR3410_94 = NULL;
91 pmeth_GostR3410_2001 = NULL;
92 pmeth_Gost28147_MAC = NULL;
93 pmeth_GostR3410_2012_256 = NULL;
94 pmeth_GostR3410_2012_512 = NULL;
95 pmeth_Gost28147_MAC_12 = NULL;
97 ameth_GostR3410_94 = NULL;
98 ameth_GostR3410_2001 = NULL;
99 ameth_Gost28147_MAC = NULL;
100 ameth_GostR3410_2012_256 = NULL;
101 ameth_GostR3410_2012_512 = NULL;
102 ameth_Gost28147_MAC_12 = NULL;
107 static int bind_gost(ENGINE *e, const char *id)
110 if (id && strcmp(id, engine_gost_id))
113 if (ameth_GostR3410_94) {
114 printf("GOST engine already loaded\n");
117 if (!ENGINE_set_id(e, engine_gost_id)) {
118 printf("ENGINE_set_id failed\n");
121 if (!ENGINE_set_name(e, engine_gost_name)) {
122 printf("ENGINE_set_name failed\n");
125 if (!ENGINE_set_digests(e, gost_digests)) {
126 printf("ENGINE_set_digests failed\n");
129 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
130 printf("ENGINE_set_ciphers failed\n");
133 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
134 printf("ENGINE_set_pkey_meths failed\n");
137 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
138 printf("ENGINE_set_pkey_asn1_meths failed\n");
141 /* Control function and commands */
142 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
143 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
146 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
147 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
150 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
151 || !ENGINE_set_init_function(e, gost_engine_init)
152 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
156 if (!register_ameth_gost
157 (NID_id_GostR3410_94, &ameth_GostR3410_94, "GOST94",
160 if (!register_ameth_gost
161 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
162 "GOST R 34.10-2001"))
164 if (!register_ameth_gost
165 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
166 "GOST R 34.10-2012 with 256 bit key"))
168 if (!register_ameth_gost
169 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
170 "GOST R 34.10-2012 with 512 bit key"))
172 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
173 "GOST-MAC", "GOST 28147-89 MAC"))
175 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
177 "GOST 28147-89 MAC with 2012 params"))
180 if (!register_pmeth_gost(NID_id_GostR3410_94, &pmeth_GostR3410_94, 0))
182 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
184 if (!register_pmeth_gost
185 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
187 if (!register_pmeth_gost
188 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
190 if (!register_pmeth_gost
191 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
193 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
195 if (!ENGINE_register_ciphers(e)
196 || !ENGINE_register_digests(e)
197 || !ENGINE_register_pkey_meths(e)
198 /* These two actually should go in LIST_ADD command */
199 || !EVP_add_cipher(&cipher_gost)
200 || !EVP_add_cipher(&cipher_gost_cpacnt)
201 || !EVP_add_cipher(&cipher_gost_cpcnt_12)
202 || !EVP_add_digest(&digest_gost)
203 || !EVP_add_digest(&digest_gost2012_512)
204 || !EVP_add_digest(&digest_gost2012_256)
205 || !EVP_add_digest(&imit_gost_cpa)
206 || !EVP_add_digest(&imit_gost_cp_12)
211 ERR_load_GOST_strings();
217 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
218 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
219 IMPLEMENT_DYNAMIC_CHECK_FN()
220 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
221 static int gost_digests(ENGINE *e, const EVP_MD **digest,
222 const int **nids, int nid)
226 *nids = gost_digest_nids;
229 if (nid == NID_id_GostR3411_94) {
230 *digest = &digest_gost;
231 } else if (nid == NID_id_GostR3411_2012_256) {
232 *digest = &digest_gost2012_256;
233 } else if (nid == NID_id_GostR3411_2012_512) {
234 *digest = &digest_gost2012_512;
235 } else if (nid == NID_id_Gost28147_89_MAC) {
236 *digest = &imit_gost_cpa;
237 } else if (nid == NID_gost_mac_12) {
238 *digest = &imit_gost_cp_12;
246 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
247 const int **nids, int nid)
251 *nids = gost_cipher_nids;
252 return 3; /* three ciphers are supported */
255 if (nid == NID_id_Gost28147_89) {
256 *cipher = &cipher_gost;
257 } else if (nid == NID_gost89_cnt) {
258 *cipher = &cipher_gost_cpacnt;
259 } else if (nid == NID_gost89_cnt_12) {
260 *cipher = &cipher_gost_cpcnt_12;
268 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
269 const int **nids, int nid)
272 *nids = gost_pkey_meth_nids;
277 case NID_id_GostR3410_94:
278 *pmeth = pmeth_GostR3410_94;
280 case NID_id_GostR3410_2001:
281 *pmeth = pmeth_GostR3410_2001;
283 case NID_id_GostR3410_2012_256:
284 *pmeth = pmeth_GostR3410_2012_256;
286 case NID_id_GostR3410_2012_512:
287 *pmeth = pmeth_GostR3410_2012_512;
289 case NID_id_Gost28147_89_MAC:
290 *pmeth = pmeth_Gost28147_MAC;
292 case NID_gost_mac_12:
293 *pmeth = pmeth_Gost28147_MAC_12;
303 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
304 const int **nids, int nid)
307 *nids = gost_pkey_meth_nids;
311 case NID_id_GostR3410_94:
312 *ameth = ameth_GostR3410_94;
314 case NID_id_GostR3410_2001:
315 *ameth = ameth_GostR3410_2001;
317 case NID_id_GostR3410_2012_256:
318 *ameth = ameth_GostR3410_2012_256;
320 case NID_id_GostR3410_2012_512:
321 *ameth = ameth_GostR3410_2012_512;
323 case NID_id_Gost28147_89_MAC:
324 *ameth = ameth_Gost28147_MAC;
326 case NID_gost_mac_12:
327 *ameth = ameth_Gost28147_MAC_12;
337 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
338 static ENGINE *engine_gost(void)
340 ENGINE *ret = ENGINE_new();
343 if (!bind_gost(ret, engine_gost_id)) {
350 void ENGINE_load_gost(void)
353 if (pmeth_GostR3410_94)
355 toadd = engine_gost();