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,
66 *pmeth_Gost28147_MAC_12 = NULL;
68 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_2001 = NULL,
69 *ameth_GostR3410_2012_256 = NULL,
70 *ameth_GostR3410_2012_512 = NULL,
71 *ameth_Gost28147_MAC = NULL,
72 *ameth_Gost28147_MAC_12 = NULL;
74 static int gost_engine_init(ENGINE *e)
79 static int gost_engine_finish(ENGINE *e)
84 static int gost_engine_destroy(ENGINE *e)
88 pmeth_GostR3410_2001 = NULL;
89 pmeth_Gost28147_MAC = NULL;
90 pmeth_GostR3410_2012_256 = NULL;
91 pmeth_GostR3410_2012_512 = NULL;
92 pmeth_Gost28147_MAC_12 = NULL;
94 ameth_GostR3410_2001 = NULL;
95 ameth_Gost28147_MAC = NULL;
96 ameth_GostR3410_2012_256 = NULL;
97 ameth_GostR3410_2012_512 = NULL;
98 ameth_Gost28147_MAC_12 = NULL;
103 static int bind_gost(ENGINE *e, const char *id)
106 if (id && strcmp(id, engine_gost_id))
108 if (ameth_GostR3410_2001) {
109 printf("GOST engine already loaded\n");
112 if (!ENGINE_set_id(e, engine_gost_id)) {
113 printf("ENGINE_set_id failed\n");
116 if (!ENGINE_set_name(e, engine_gost_name)) {
117 printf("ENGINE_set_name failed\n");
120 if (!ENGINE_set_digests(e, gost_digests)) {
121 printf("ENGINE_set_digests failed\n");
124 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
125 printf("ENGINE_set_ciphers failed\n");
128 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
129 printf("ENGINE_set_pkey_meths failed\n");
132 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
133 printf("ENGINE_set_pkey_asn1_meths failed\n");
136 /* Control function and commands */
137 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
138 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
141 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
142 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
145 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
146 || !ENGINE_set_init_function(e, gost_engine_init)
147 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
151 if (!register_ameth_gost
152 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
153 "GOST R 34.10-2001"))
155 if (!register_ameth_gost
156 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
157 "GOST R 34.10-2012 with 256 bit key"))
159 if (!register_ameth_gost
160 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
161 "GOST R 34.10-2012 with 512 bit key"))
163 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
164 "GOST-MAC", "GOST 28147-89 MAC"))
166 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
168 "GOST 28147-89 MAC with 2012 params"))
171 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
174 if (!register_pmeth_gost
175 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
177 if (!register_pmeth_gost
178 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
180 if (!register_pmeth_gost
181 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
183 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
185 if (!ENGINE_register_ciphers(e)
186 || !ENGINE_register_digests(e)
187 || !ENGINE_register_pkey_meths(e)
188 /* These two actually should go in LIST_ADD command */
189 || !EVP_add_cipher(&cipher_gost)
190 || !EVP_add_cipher(&cipher_gost_cbc)
191 || !EVP_add_cipher(&cipher_gost_cpacnt)
192 || !EVP_add_cipher(&cipher_gost_cpcnt_12)
193 || !EVP_add_digest(&digest_gost)
194 || !EVP_add_digest(&digest_gost2012_512)
195 || !EVP_add_digest(&digest_gost2012_256)
196 || !EVP_add_digest(&imit_gost_cpa)
197 || !EVP_add_digest(&imit_gost_cp_12)
202 ERR_load_GOST_strings();
208 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
209 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
210 IMPLEMENT_DYNAMIC_CHECK_FN()
211 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
212 static int gost_digests(ENGINE *e, const EVP_MD **digest,
213 const int **nids, int nid)
217 *nids = gost_digest_nids;
220 if (nid == NID_id_GostR3411_94) {
221 *digest = &digest_gost;
222 } else if (nid == NID_id_GostR3411_2012_256) {
223 *digest = &digest_gost2012_256;
224 } else if (nid == NID_id_GostR3411_2012_512) {
225 *digest = &digest_gost2012_512;
226 } else if (nid == NID_id_Gost28147_89_MAC) {
227 *digest = &imit_gost_cpa;
228 } else if (nid == NID_gost_mac_12) {
229 *digest = &imit_gost_cp_12;
237 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
238 const int **nids, int nid)
242 *nids = gost_cipher_nids;
243 return 4; /* three ciphers are supported */
246 if (nid == NID_id_Gost28147_89) {
247 *cipher = &cipher_gost;
248 } else if (nid == NID_gost89_cnt) {
249 *cipher = &cipher_gost_cpacnt;
250 } else if (nid == NID_gost89_cnt_12) {
251 *cipher = &cipher_gost_cpcnt_12;
252 } else if (nid == NID_gost89_cbc) {
253 *cipher = &cipher_gost_cbc;
261 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
262 const int **nids, int nid)
265 *nids = gost_pkey_meth_nids;
266 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
270 case NID_id_GostR3410_2001:
271 *pmeth = pmeth_GostR3410_2001;
273 case NID_id_GostR3410_2012_256:
274 *pmeth = pmeth_GostR3410_2012_256;
276 case NID_id_GostR3410_2012_512:
277 *pmeth = pmeth_GostR3410_2012_512;
279 case NID_id_Gost28147_89_MAC:
280 *pmeth = pmeth_Gost28147_MAC;
282 case NID_gost_mac_12:
283 *pmeth = pmeth_Gost28147_MAC_12;
293 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
294 const int **nids, int nid)
297 *nids = gost_pkey_meth_nids;
298 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
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();