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 digest_gost_destroy();
106 digest_gost2012_256_destroy();
107 digest_gost2012_512_destroy();
109 imit_gost_cpa_destroy();
110 imit_gost_cp_12_destroy();
112 cipher_gost_destroy();
116 pmeth_GostR3410_2001 = NULL;
117 pmeth_Gost28147_MAC = NULL;
118 pmeth_GostR3410_2012_256 = NULL;
119 pmeth_GostR3410_2012_512 = NULL;
120 pmeth_Gost28147_MAC_12 = NULL;
122 ameth_GostR3410_2001 = NULL;
123 ameth_Gost28147_MAC = NULL;
124 ameth_GostR3410_2012_256 = NULL;
125 ameth_GostR3410_2012_512 = NULL;
126 ameth_Gost28147_MAC_12 = NULL;
128 ERR_unload_GOST_strings();
133 static int bind_gost(ENGINE* e, const char* id) {
135 if (id != NULL && strcmp(id, engine_gost_id) != 0)
137 if (ameth_GostR3410_2001) {
138 printf("GOST engine already loaded\n");
141 if (!ENGINE_set_id(e, engine_gost_id)) {
142 printf("ENGINE_set_id failed\n");
145 if (!ENGINE_set_name(e, engine_gost_name)) {
146 printf("ENGINE_set_name failed\n");
149 if (!ENGINE_set_digests(e, gost_digests)) {
150 printf("ENGINE_set_digests failed\n");
153 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
154 printf("ENGINE_set_ciphers failed\n");
157 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
158 printf("ENGINE_set_pkey_meths failed\n");
161 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
162 printf("ENGINE_set_pkey_asn1_meths failed\n");
165 /* Control function and commands */
166 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
167 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
170 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
171 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
174 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
175 || !ENGINE_set_init_function(e, gost_engine_init)
176 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
180 if (!register_ameth_gost
181 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
182 "GOST R 34.10-2001"))
184 if (!register_ameth_gost
185 (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
186 "GOST R 34.10-2012 with 256 bit key"))
188 if (!register_ameth_gost
189 (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
190 "GOST R 34.10-2012 with 512 bit key"))
192 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
193 "GOST-MAC", "GOST 28147-89 MAC"))
195 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
197 "GOST 28147-89 MAC with 2012 params"))
200 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
203 if (!register_pmeth_gost
204 (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
206 if (!register_pmeth_gost
207 (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
209 if (!register_pmeth_gost
210 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
212 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
214 if (!ENGINE_register_ciphers(e)
215 || !ENGINE_register_digests(e)
216 || !ENGINE_register_pkey_meths(e)
217 /* These two actually should go in LIST_ADD command */
218 || !EVP_add_cipher(cipher_gost())
219 || !EVP_add_cipher(cipher_gost_cbc())
220 || !EVP_add_cipher(cipher_gost_cpacnt())
221 || !EVP_add_cipher(cipher_gost_cpcnt_12())
222 || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
223 || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
224 || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
225 || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
226 || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
227 || !EVP_add_digest(digest_gost())
228 || !EVP_add_digest(digest_gost2012_512())
229 || !EVP_add_digest(digest_gost2012_256())
230 || !EVP_add_digest(imit_gost_cpa())
231 || !EVP_add_digest(imit_gost_cp_12())
236 ENGINE_register_all_complete();
238 ERR_load_GOST_strings();
244 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
245 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
246 IMPLEMENT_DYNAMIC_CHECK_FN()
247 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
249 static int gost_digests(ENGINE* e, const EVP_MD** digest,
250 const int** nids, int nid) {
252 if (digest == NULL) {
253 return gost_digest_nids(nids);
255 if (nid == NID_id_GostR3411_94) {
256 *digest = digest_gost();
257 } else if (nid == NID_id_Gost28147_89_MAC) {
258 *digest = imit_gost_cpa();
259 } else if (nid == NID_id_GostR3411_2012_256) {
260 *digest = digest_gost2012_256();
261 } else if (nid == NID_id_GostR3411_2012_512) {
262 *digest = digest_gost2012_512();
263 } else if (nid == NID_gost_mac_12) {
264 *digest = imit_gost_cp_12();
272 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
273 const int** nids, int nid) {
275 if (cipher == NULL) {
276 *nids = gost_cipher_nids;
277 return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
280 if (nid == NID_id_Gost28147_89) {
281 *cipher = cipher_gost();
282 } else if (nid == NID_gost89_cnt) {
283 *cipher = cipher_gost_cpacnt();
284 } else if (nid == NID_gost89_cnt_12) {
285 *cipher = cipher_gost_cpcnt_12();
286 } else if (nid == NID_gost89_cbc) {
287 *cipher = cipher_gost_cbc();
288 } else if (nid == NID_grasshopper_ecb) {
289 *cipher = cipher_gost_grasshopper_ecb();
290 } else if (nid == NID_grasshopper_cbc) {
291 *cipher = cipher_gost_grasshopper_cbc();
292 } else if (nid == NID_grasshopper_cfb) {
293 *cipher = cipher_gost_grasshopper_cfb();
294 } else if (nid == NID_grasshopper_ofb) {
295 *cipher = cipher_gost_grasshopper_ofb();
296 } else if (nid == NID_grasshopper_ctr) {
297 *cipher = cipher_gost_grasshopper_ctr();
305 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
306 const int** nids, int nid) {
308 *nids = gost_pkey_meth_nids;
309 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
313 case NID_id_GostR3410_2001:
314 *pmeth = pmeth_GostR3410_2001;
316 case NID_id_GostR3410_2012_256:
317 *pmeth = pmeth_GostR3410_2012_256;
319 case NID_id_GostR3410_2012_512:
320 *pmeth = pmeth_GostR3410_2012_512;
322 case NID_id_Gost28147_89_MAC:
323 *pmeth = pmeth_Gost28147_MAC;
325 case NID_gost_mac_12:
326 *pmeth = pmeth_Gost28147_MAC_12;
336 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
337 const int** nids, int nid) {
339 *nids = gost_pkey_meth_nids;
340 return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
344 case NID_id_GostR3410_2001:
345 *ameth = ameth_GostR3410_2001;
347 case NID_id_GostR3410_2012_256:
348 *ameth = ameth_GostR3410_2012_256;
350 case NID_id_GostR3410_2012_512:
351 *ameth = ameth_GostR3410_2012_512;
353 case NID_id_Gost28147_89_MAC:
354 *ameth = ameth_Gost28147_MAC;
356 case NID_gost_mac_12:
357 *ameth = ameth_Gost28147_MAC_12;
367 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
369 static ENGINE* engine_gost(void) {
370 ENGINE* ret = ENGINE_new();
373 if (!bind_gost(ret, engine_gost_id)) {
380 void ENGINE_load_gost(void) {
382 if (pmeth_GostR3410_2001)
384 toadd = engine_gost();