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();
115 cipher_gost_grasshopper_destroy();
119 pmeth_GostR3410_2001 = NULL;
120 pmeth_Gost28147_MAC = NULL;
121 pmeth_GostR3410_2012_256 = NULL;
122 pmeth_GostR3410_2012_512 = NULL;
123 pmeth_Gost28147_MAC_12 = NULL;
125 ameth_GostR3410_2001 = NULL;
126 ameth_Gost28147_MAC = NULL;
127 ameth_GostR3410_2012_256 = NULL;
128 ameth_GostR3410_2012_512 = NULL;
129 ameth_Gost28147_MAC_12 = NULL;
131 ERR_unload_GOST_strings();
136 static int bind_gost(ENGINE* e, const char* id) {
138 if (id != NULL && strcmp(id, engine_gost_id) != 0)
140 if (ameth_GostR3410_2001) {
141 printf("GOST engine already loaded\n");
144 if (!ENGINE_set_id(e, engine_gost_id)) {
145 printf("ENGINE_set_id failed\n");
148 if (!ENGINE_set_name(e, engine_gost_name)) {
149 printf("ENGINE_set_name failed\n");
152 if (!ENGINE_set_digests(e, gost_digests)) {
153 printf("ENGINE_set_digests failed\n");
156 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
157 printf("ENGINE_set_ciphers failed\n");
160 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
161 printf("ENGINE_set_pkey_meths failed\n");
164 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
165 printf("ENGINE_set_pkey_asn1_meths failed\n");
168 /* Control function and commands */
169 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
170 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
173 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
174 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
177 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
178 || !ENGINE_set_init_function(e, gost_engine_init)
179 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
183 if (!register_ameth_gost
184 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
185 "GOST R 34.10-2001"))
187 if (!register_ameth_gost
188 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
189 "GOST R 34.10-2012 with 256 bit key"))
191 if (!register_ameth_gost
192 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
193 "GOST R 34.10-2012 with 512 bit key"))
195 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
196 "GOST-MAC", "GOST 28147-89 MAC"))
198 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
200 "GOST 28147-89 MAC with 2012 params"))
203 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
206 if (!register_pmeth_gost
207 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
209 if (!register_pmeth_gost
210 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
212 if (!register_pmeth_gost
213 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
215 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
217 if (!ENGINE_register_ciphers(e)
218 || !ENGINE_register_digests(e)
219 || !ENGINE_register_pkey_meths(e)
220 /* These two actually should go in LIST_ADD command */
221 || !EVP_add_cipher(cipher_gost())
222 || !EVP_add_cipher(cipher_gost_cbc())
223 || !EVP_add_cipher(cipher_gost_cpacnt())
224 || !EVP_add_cipher(cipher_gost_cpcnt_12())
225 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
226 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
227 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
228 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
229 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
230 || !EVP_add_digest(digest_gost())
231 || !EVP_add_digest(digest_gost2012_512())
232 || !EVP_add_digest(digest_gost2012_256())
233 || !EVP_add_digest(imit_gost_cpa())
234 || !EVP_add_digest(imit_gost_cp_12())
239 if(!EVP_add_digest_alias(SN_id_GostR3411_2012_256, "streebog256")
240 || !EVP_add_digest_alias(SN_id_GostR3411_2012_512, "streebog512")) {
244 ENGINE_register_all_complete();
246 ERR_load_GOST_strings();
252 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
253 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
254 IMPLEMENT_DYNAMIC_CHECK_FN()
255 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
257 static int gost_digests(ENGINE* e, const EVP_MD** digest,
258 const int** nids, int nid) {
260 if (digest == NULL) {
261 return gost_digest_nids(nids);
263 if (nid == NID_id_GostR3411_94) {
264 *digest = digest_gost();
265 } else if (nid == NID_id_Gost28147_89_MAC) {
266 *digest = imit_gost_cpa();
267 } else if (nid == NID_id_GostR3411_2012_256) {
268 *digest = digest_gost2012_256();
269 } else if (nid == NID_id_GostR3411_2012_512) {
270 *digest = digest_gost2012_512();
271 } else if (nid == NID_gost_mac_12) {
272 *digest = imit_gost_cp_12();
280 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
281 const int** nids, int nid) {
283 if (cipher == NULL) {
284 *nids = gost_cipher_nids;
285 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
288 if (nid == NID_id_Gost28147_89) {
289 *cipher = cipher_gost();
290 } else if (nid == NID_gost89_cnt) {
291 *cipher = cipher_gost_cpacnt();
292 } else if (nid == NID_gost89_cnt_12) {
293 *cipher = cipher_gost_cpcnt_12();
294 } else if (nid == NID_gost89_cbc) {
295 *cipher = cipher_gost_cbc();
296 } else if (nid == NID_grasshopper_ecb) {
297 *cipher = cipher_gost_grasshopper_ecb();
298 } else if (nid == NID_grasshopper_cbc) {
299 *cipher = cipher_gost_grasshopper_cbc();
300 } else if (nid == NID_grasshopper_cfb) {
301 *cipher = cipher_gost_grasshopper_cfb();
302 } else if (nid == NID_grasshopper_ofb) {
303 *cipher = cipher_gost_grasshopper_ofb();
304 } else if (nid == NID_grasshopper_ctr) {
305 *cipher = cipher_gost_grasshopper_ctr();
313 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
314 const int** nids, int nid) {
316 *nids = gost_pkey_meth_nids;
317 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
321 case NID_id_GostR3410_2001:
322 *pmeth = pmeth_GostR3410_2001;
324 case NID_id_GostR3410_2012_256:
325 *pmeth = pmeth_GostR3410_2012_256;
327 case NID_id_GostR3410_2012_512:
328 *pmeth = pmeth_GostR3410_2012_512;
330 case NID_id_Gost28147_89_MAC:
331 *pmeth = pmeth_Gost28147_MAC;
333 case NID_gost_mac_12:
334 *pmeth = pmeth_Gost28147_MAC_12;
344 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
345 const int** nids, int nid) {
347 *nids = gost_pkey_meth_nids;
348 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
352 case NID_id_GostR3410_2001:
353 *ameth = ameth_GostR3410_2001;
355 case NID_id_GostR3410_2012_256:
356 *ameth = ameth_GostR3410_2012_256;
358 case NID_id_GostR3410_2012_512:
359 *ameth = ameth_GostR3410_2012_512;
361 case NID_id_Gost28147_89_MAC:
362 *ameth = ameth_Gost28147_MAC;
364 case NID_gost_mac_12:
365 *ameth = ameth_Gost28147_MAC_12;
375 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
377 static ENGINE* engine_gost(void) {
378 ENGINE* ret = ENGINE_new();
381 if (!bind_gost(ret, engine_gost_id)) {
388 void ENGINE_load_gost(void) {
390 if (pmeth_GostR3410_2001)
392 toadd = engine_gost();