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[] = {
44 static int gost_digest_nids[] = {
46 NID_id_Gost28147_89_MAC,
47 NID_id_GostR3411_2012_256,
48 NID_id_GostR3411_2012_512,
53 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_2001 = NULL,
63 *pmeth_GostR3410_2012_256 = NULL,
64 *pmeth_GostR3410_2012_512 = NULL,
65 *pmeth_Gost28147_MAC = NULL, *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, *ameth_Gost28147_MAC_12 = NULL;
72 static int gost_engine_init(ENGINE *e)
77 static int gost_engine_finish(ENGINE *e)
82 static int gost_engine_destroy(ENGINE *e)
86 pmeth_GostR3410_2001 = NULL;
87 pmeth_Gost28147_MAC = NULL;
88 pmeth_GostR3410_2012_256 = NULL;
89 pmeth_GostR3410_2012_512 = NULL;
90 pmeth_Gost28147_MAC_12 = NULL;
92 ameth_GostR3410_2001 = NULL;
93 ameth_Gost28147_MAC = NULL;
94 ameth_GostR3410_2012_256 = NULL;
95 ameth_GostR3410_2012_512 = NULL;
96 ameth_Gost28147_MAC_12 = NULL;
101 static int bind_gost(ENGINE *e, const char *id)
104 if (id && strcmp(id, engine_gost_id))
106 if (ameth_GostR3410_2001) {
107 printf("GOST engine already loaded\n");
110 if (!ENGINE_set_id(e, engine_gost_id)) {
111 printf("ENGINE_set_id failed\n");
114 if (!ENGINE_set_name(e, engine_gost_name)) {
115 printf("ENGINE_set_name failed\n");
118 if (!ENGINE_set_digests(e, gost_digests)) {
119 printf("ENGINE_set_digests failed\n");
122 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
123 printf("ENGINE_set_ciphers failed\n");
126 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
127 printf("ENGINE_set_pkey_meths failed\n");
130 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
131 printf("ENGINE_set_pkey_asn1_meths failed\n");
134 /* Control function and commands */
135 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
136 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
139 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
140 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
143 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
144 || !ENGINE_set_init_function(e, gost_engine_init)
145 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
149 if (!register_ameth_gost
150 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
151 "GOST R 34.10-2001"))
153 if (!register_ameth_gost
154 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
155 "GOST R 34.10-2012 with 256 bit key"))
157 if (!register_ameth_gost
158 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
159 "GOST R 34.10-2012 with 512 bit key"))
161 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
162 "GOST-MAC", "GOST 28147-89 MAC"))
164 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
166 "GOST 28147-89 MAC with 2012 params"))
169 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
172 if (!register_pmeth_gost
173 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
175 if (!register_pmeth_gost
176 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
178 if (!register_pmeth_gost
179 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
181 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
183 if (!ENGINE_register_ciphers(e)
184 || !ENGINE_register_digests(e)
185 || !ENGINE_register_pkey_meths(e)
186 /* These two actually should go in LIST_ADD command */
187 || !EVP_add_cipher(&cipher_gost)
188 || !EVP_add_cipher(&cipher_gost_cbc)
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;
216 return sizeof(gost_digest_nids) / sizeof(gost_digest_nids[0]) - 1;
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 sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
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;
250 } else if (nid == NID_gost89_cbc) {
251 *cipher = &cipher_gost_cbc;
259 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
260 const int **nids, int nid)
263 *nids = gost_pkey_meth_nids;
264 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
269 case NID_id_GostR3410_2001:
270 *pmeth = pmeth_GostR3410_2001;
272 case NID_id_GostR3410_2012_256:
273 *pmeth = pmeth_GostR3410_2012_256;
275 case NID_id_GostR3410_2012_512:
276 *pmeth = pmeth_GostR3410_2012_512;
278 case NID_id_Gost28147_89_MAC:
279 *pmeth = pmeth_Gost28147_MAC;
281 case NID_gost_mac_12:
282 *pmeth = pmeth_Gost28147_MAC_12;
292 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
293 const int **nids, int nid)
296 *nids = gost_pkey_meth_nids;
297 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
301 case NID_id_GostR3410_2001:
302 *ameth = ameth_GostR3410_2001;
304 case NID_id_GostR3410_2012_256:
305 *ameth = ameth_GostR3410_2012_256;
307 case NID_id_GostR3410_2012_512:
308 *ameth = ameth_GostR3410_2012_512;
310 case NID_id_Gost28147_89_MAC:
311 *ameth = ameth_Gost28147_MAC;
313 case NID_gost_mac_12:
314 *ameth = ameth_Gost28147_MAC_12;
324 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
325 static ENGINE *engine_gost(void)
327 ENGINE *ret = ENGINE_new();
330 if (!bind_gost(ret, engine_gost_id)) {
337 void ENGINE_load_gost(void)
340 if (pmeth_GostR3410_2001)
342 toadd = engine_gost();