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[] = {
53 NID_id_GostR3410_2001,
54 NID_id_Gost28147_89_MAC,
55 NID_id_GostR3410_2012_256,
56 NID_id_GostR3410_2012_512,
61 static EVP_PKEY_METHOD *pmeth_GostR3410_2001 = NULL,
62 *pmeth_GostR3410_2012_256 = NULL,
63 *pmeth_GostR3410_2012_512 = NULL,
64 *pmeth_Gost28147_MAC = NULL,
65 *pmeth_Gost28147_MAC_12 = NULL;
67 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_2001 = NULL,
68 *ameth_GostR3410_2012_256 = NULL,
69 *ameth_GostR3410_2012_512 = NULL,
70 *ameth_Gost28147_MAC = NULL,
71 *ameth_Gost28147_MAC_12 = NULL;
73 static int gost_engine_init(ENGINE *e)
78 static int gost_engine_finish(ENGINE *e)
83 static int gost_engine_destroy(ENGINE *e)
87 pmeth_GostR3410_2001 = NULL;
88 pmeth_Gost28147_MAC = NULL;
89 pmeth_GostR3410_2012_256 = NULL;
90 pmeth_GostR3410_2012_512 = NULL;
91 pmeth_Gost28147_MAC_12 = NULL;
93 ameth_GostR3410_2001 = NULL;
94 ameth_Gost28147_MAC = NULL;
95 ameth_GostR3410_2012_256 = NULL;
96 ameth_GostR3410_2012_512 = NULL;
97 ameth_Gost28147_MAC_12 = NULL;
102 static int bind_gost(ENGINE *e, const char *id)
105 if (id && strcmp(id, engine_gost_id))
107 if (ameth_GostR3410_2001) {
108 printf("GOST engine already loaded\n");
111 if (!ENGINE_set_id(e, engine_gost_id)) {
112 printf("ENGINE_set_id failed\n");
115 if (!ENGINE_set_name(e, engine_gost_name)) {
116 printf("ENGINE_set_name failed\n");
119 if (!ENGINE_set_digests(e, gost_digests)) {
120 printf("ENGINE_set_digests failed\n");
123 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
124 printf("ENGINE_set_ciphers failed\n");
127 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
128 printf("ENGINE_set_pkey_meths failed\n");
131 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
132 printf("ENGINE_set_pkey_asn1_meths failed\n");
135 /* Control function and commands */
136 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
137 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
140 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
141 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
144 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
145 || !ENGINE_set_init_function(e, gost_engine_init)
146 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
150 if (!register_ameth_gost
151 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
152 "GOST R 34.10-2001"))
154 if (!register_ameth_gost
155 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
156 "GOST R 34.10-2012 with 256 bit key"))
158 if (!register_ameth_gost
159 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
160 "GOST R 34.10-2012 with 512 bit key"))
162 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
163 "GOST-MAC", "GOST 28147-89 MAC"))
165 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
167 "GOST 28147-89 MAC with 2012 params"))
170 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
173 if (!register_pmeth_gost
174 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
176 if (!register_pmeth_gost
177 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
179 if (!register_pmeth_gost
180 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
182 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
184 if (!ENGINE_register_ciphers(e)
185 || !ENGINE_register_digests(e)
186 || !ENGINE_register_pkey_meths(e)
187 /* These two actually should go in LIST_ADD command */
188 || !EVP_add_cipher(&cipher_gost)
189 || !EVP_add_cipher(&cipher_gost_cpacnt)
190 || !EVP_add_cipher(&cipher_gost_cpcnt_12)
191 || !EVP_add_digest(&digest_gost)
192 || !EVP_add_digest(&digest_gost2012_512)
193 || !EVP_add_digest(&digest_gost2012_256)
194 || !EVP_add_digest(&imit_gost_cpa)
195 || !EVP_add_digest(&imit_gost_cp_12)
200 ERR_load_GOST_strings();
206 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
207 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
208 IMPLEMENT_DYNAMIC_CHECK_FN()
209 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
210 static int gost_digests(ENGINE *e, const EVP_MD **digest,
211 const int **nids, int nid)
215 *nids = gost_digest_nids;
218 if (nid == NID_id_GostR3411_94) {
219 *digest = &digest_gost;
220 } else if (nid == NID_id_GostR3411_2012_256) {
221 *digest = &digest_gost2012_256;
222 } else if (nid == NID_id_GostR3411_2012_512) {
223 *digest = &digest_gost2012_512;
224 } else if (nid == NID_id_Gost28147_89_MAC) {
225 *digest = &imit_gost_cpa;
226 } else if (nid == NID_gost_mac_12) {
227 *digest = &imit_gost_cp_12;
235 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
236 const int **nids, int nid)
240 *nids = gost_cipher_nids;
241 return 3; /* three ciphers are supported */
244 if (nid == NID_id_Gost28147_89) {
245 *cipher = &cipher_gost;
246 } else if (nid == NID_gost89_cnt) {
247 *cipher = &cipher_gost_cpacnt;
248 } else if (nid == NID_gost89_cnt_12) {
249 *cipher = &cipher_gost_cpcnt_12;
257 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
258 const int **nids, int nid)
261 *nids = gost_pkey_meth_nids;
262 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
266 case NID_id_GostR3410_2001:
267 *pmeth = pmeth_GostR3410_2001;
269 case NID_id_GostR3410_2012_256:
270 *pmeth = pmeth_GostR3410_2012_256;
272 case NID_id_GostR3410_2012_512:
273 *pmeth = pmeth_GostR3410_2012_512;
275 case NID_id_Gost28147_89_MAC:
276 *pmeth = pmeth_Gost28147_MAC;
278 case NID_gost_mac_12:
279 *pmeth = pmeth_Gost28147_MAC_12;
289 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
290 const int **nids, int nid)
293 *nids = gost_pkey_meth_nids;
294 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
297 case NID_id_GostR3410_2001:
298 *ameth = ameth_GostR3410_2001;
300 case NID_id_GostR3410_2012_256:
301 *ameth = ameth_GostR3410_2012_256;
303 case NID_id_GostR3410_2012_512:
304 *ameth = ameth_GostR3410_2012_512;
306 case NID_id_Gost28147_89_MAC:
307 *ameth = ameth_Gost28147_MAC;
309 case NID_gost_mac_12:
310 *ameth = ameth_Gost28147_MAC_12;
320 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
321 static ENGINE *engine_gost(void)
323 ENGINE *ret = ENGINE_new();
326 if (!bind_gost(ret, engine_gost_id)) {
333 void ENGINE_load_gost(void)
336 if (pmeth_GostR3410_2001)
338 toadd = engine_gost();