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"
19 #include "gost_grasshopper_cipher.h"
21 static const char* engine_gost_id = "gost";
23 static const char* engine_gost_name =
24 "Reference implementation of GOST engine";
26 /* Symmetric cipher and digest function registrar */
28 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
29 const int** nids, int nid);
31 static int gost_digests(ENGINE* e, const EVP_MD** digest,
32 const int** nids, int ind);
34 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
35 const int** nids, int nid);
37 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
38 const int** nids, int nid);
40 static int gost_cipher_nids[] = {
53 static int gost_digest_nids(const int** nids) {
54 static int digest_nids[6] = {0, 0, 0, 0, 0, 0};
60 if ((md = digest_gost()) != NULL)
61 digest_nids[pos++] = EVP_MD_type(md);
62 if ((md = imit_gost_cpa()) != NULL)
63 digest_nids[pos++] = EVP_MD_type(md);
64 if ((md = digest_gost2012_256()) != NULL)
65 digest_nids[pos++] = EVP_MD_type(md);
66 if ((md = digest_gost2012_512()) != NULL)
67 digest_nids[pos++] = EVP_MD_type(md);
68 if ((md = imit_gost_cp_12()) != NULL)
69 digest_nids[pos++] = EVP_MD_type(md);
77 static int gost_pkey_meth_nids[] = {
78 NID_id_GostR3410_2001,
79 NID_id_Gost28147_89_MAC,
80 NID_id_GostR3410_2012_256,
81 NID_id_GostR3410_2012_512,
86 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
87 * pmeth_GostR3410_2012_256 = NULL,
88 * pmeth_GostR3410_2012_512 = NULL,
89 * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL;
91 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
92 * ameth_GostR3410_2012_256 = NULL,
93 * ameth_GostR3410_2012_512 = NULL,
94 * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL;
96 static int gost_engine_init(ENGINE* e) {
100 static int gost_engine_finish(ENGINE* e) {
104 static int gost_engine_destroy(ENGINE* e) {
105 EVP_delete_digest_alias("streebog256");
106 EVP_delete_digest_alias("streebog512");
107 digest_gost_destroy();
108 digest_gost2012_256_destroy();
109 digest_gost2012_512_destroy();
111 imit_gost_cpa_destroy();
112 imit_gost_cp_12_destroy();
114 cipher_gost_destroy();
118 pmeth_GostR3410_2001 = NULL;
119 pmeth_Gost28147_MAC = NULL;
120 pmeth_GostR3410_2012_256 = NULL;
121 pmeth_GostR3410_2012_512 = NULL;
122 pmeth_Gost28147_MAC_12 = NULL;
124 ameth_GostR3410_2001 = NULL;
125 ameth_Gost28147_MAC = NULL;
126 ameth_GostR3410_2012_256 = NULL;
127 ameth_GostR3410_2012_512 = NULL;
128 ameth_Gost28147_MAC_12 = NULL;
130 ERR_unload_GOST_strings();
135 static int bind_gost(ENGINE* e, const char* id) {
137 if (id != NULL && strcmp(id, engine_gost_id) != 0)
139 if (ameth_GostR3410_2001) {
140 printf("GOST engine already loaded\n");
143 if (!ENGINE_set_id(e, engine_gost_id)) {
144 printf("ENGINE_set_id failed\n");
147 if (!ENGINE_set_name(e, engine_gost_name)) {
148 printf("ENGINE_set_name failed\n");
151 if (!ENGINE_set_digests(e, gost_digests)) {
152 printf("ENGINE_set_digests failed\n");
155 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
156 printf("ENGINE_set_ciphers failed\n");
159 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
160 printf("ENGINE_set_pkey_meths failed\n");
163 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
164 printf("ENGINE_set_pkey_asn1_meths failed\n");
167 /* Control function and commands */
168 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
169 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
172 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
173 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
176 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
177 || !ENGINE_set_init_function(e, gost_engine_init)
178 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
182 if (!register_ameth_gost
183 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
184 "GOST R 34.10-2001"))
186 if (!register_ameth_gost
187 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
188 "GOST R 34.10-2012 with 256 bit key"))
190 if (!register_ameth_gost
191 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
192 "GOST R 34.10-2012 with 512 bit key"))
194 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
195 "GOST-MAC", "GOST 28147-89 MAC"))
197 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
199 "GOST 28147-89 MAC with 2012 params"))
202 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
205 if (!register_pmeth_gost
206 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
208 if (!register_pmeth_gost
209 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
211 if (!register_pmeth_gost
212 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
214 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
216 if (!ENGINE_register_ciphers(e)
217 || !ENGINE_register_digests(e)
218 || !ENGINE_register_pkey_meths(e)
219 /* These two actually should go in LIST_ADD command */
220 || !EVP_add_cipher(cipher_gost())
221 || !EVP_add_cipher(cipher_gost_cbc())
222 || !EVP_add_cipher(cipher_gost_cpacnt())
223 || !EVP_add_cipher(cipher_gost_cpcnt_12())
224 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
225 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
226 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
227 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
228 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
229 || !EVP_add_digest(digest_gost())
230 || !EVP_add_digest(digest_gost2012_512())
231 || !EVP_add_digest(digest_gost2012_256())
232 || !EVP_add_digest(imit_gost_cpa())
233 || !EVP_add_digest(imit_gost_cp_12())
238 if(!EVP_add_digest_alias(SN_id_GostR3411_2012_256, "streebog256")
239 || !EVP_add_digest_alias(SN_id_GostR3411_2012_512, "streebog512")) {
243 ENGINE_register_all_complete();
245 ERR_load_GOST_strings();
251 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
252 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
253 IMPLEMENT_DYNAMIC_CHECK_FN()
254 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
256 static int gost_digests(ENGINE* e, const EVP_MD** digest,
257 const int** nids, int nid) {
259 if (digest == NULL) {
260 return gost_digest_nids(nids);
262 if (nid == NID_id_GostR3411_94) {
263 *digest = digest_gost();
264 } else if (nid == NID_id_Gost28147_89_MAC) {
265 *digest = imit_gost_cpa();
266 } else if (nid == NID_id_GostR3411_2012_256) {
267 *digest = digest_gost2012_256();
268 } else if (nid == NID_id_GostR3411_2012_512) {
269 *digest = digest_gost2012_512();
270 } else if (nid == NID_gost_mac_12) {
271 *digest = imit_gost_cp_12();
279 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
280 const int** nids, int nid) {
282 if (cipher == NULL) {
283 *nids = gost_cipher_nids;
284 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
287 if (nid == NID_id_Gost28147_89) {
288 *cipher = cipher_gost();
289 } else if (nid == NID_gost89_cnt) {
290 *cipher = cipher_gost_cpacnt();
291 } else if (nid == NID_gost89_cnt_12) {
292 *cipher = cipher_gost_cpcnt_12();
293 } else if (nid == NID_gost89_cbc) {
294 *cipher = cipher_gost_cbc();
295 } else if (nid == NID_grasshopper_ecb) {
296 *cipher = cipher_gost_grasshopper_ecb();
297 } else if (nid == NID_grasshopper_cbc) {
298 *cipher = cipher_gost_grasshopper_cbc();
299 } else if (nid == NID_grasshopper_cfb) {
300 *cipher = cipher_gost_grasshopper_cfb();
301 } else if (nid == NID_grasshopper_ofb) {
302 *cipher = cipher_gost_grasshopper_ofb();
303 } else if (nid == NID_grasshopper_ctr) {
304 *cipher = cipher_gost_grasshopper_ctr();
312 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
313 const int** nids, int nid) {
315 *nids = gost_pkey_meth_nids;
316 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
320 case NID_id_GostR3410_2001:
321 *pmeth = pmeth_GostR3410_2001;
323 case NID_id_GostR3410_2012_256:
324 *pmeth = pmeth_GostR3410_2012_256;
326 case NID_id_GostR3410_2012_512:
327 *pmeth = pmeth_GostR3410_2012_512;
329 case NID_id_Gost28147_89_MAC:
330 *pmeth = pmeth_Gost28147_MAC;
332 case NID_gost_mac_12:
333 *pmeth = pmeth_Gost28147_MAC_12;
343 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
344 const int** nids, int nid) {
346 *nids = gost_pkey_meth_nids;
347 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
351 case NID_id_GostR3410_2001:
352 *ameth = ameth_GostR3410_2001;
354 case NID_id_GostR3410_2012_256:
355 *ameth = ameth_GostR3410_2012_256;
357 case NID_id_GostR3410_2012_512:
358 *ameth = ameth_GostR3410_2012_512;
360 case NID_id_Gost28147_89_MAC:
361 *ameth = ameth_Gost28147_MAC;
363 case NID_gost_mac_12:
364 *ameth = ameth_Gost28147_MAC_12;
374 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
376 static ENGINE* engine_gost(void) {
377 ENGINE* ret = ENGINE_new();
380 if (!bind_gost(ret, engine_gost_id)) {
387 void ENGINE_load_gost(void) {
389 if (pmeth_GostR3410_2001)
391 toadd = engine_gost();