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[] = {
39 NID_undef /*NID_gost89_cnt_12*/,
40 NID_undef /* NID_gost89_cbc */,
44 static int gost_digest_nids[] = {
46 NID_id_Gost28147_89_MAC,
47 NID_undef /*NID_md_gost12_256*/,
48 NID_undef /*NID_md_gost12_512*/,
49 NID_undef /*NID_gost_mac_12*/,
53 static int gost_pkey_meth_nids[] = {
54 NID_id_GostR3410_2001,
55 NID_id_Gost28147_89_MAC,
56 NID_undef /*NID_gost2012_256*/,
57 NID_undef /*NID_gost2012_512*/,
58 NID_undef /*NID_gost_mac_12*/,
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;
102 extern int gost_define_nids(void);
103 static int bind_gost(ENGINE *e, const char *id)
106 if (id && strcmp(id, engine_gost_id))
108 if (ameth_GostR3410_2001) {
109 /* Engine already loaded */
112 if (!gost_define_nids()) {
115 /* Set up nids which might be undefined in the core object database */
116 /* Arrays of algoritmhs */
117 gost_cipher_nids[1]=NID_gost89_cnt;
118 gost_cipher_nids[2]=NID_gost89_cnt_12;
119 gost_cipher_nids[3]=NID_gost89_cbc;
120 gost_digest_nids[2]=NID_md_gost12_256;
121 gost_digest_nids[3]=NID_md_gost12_512;
122 gost_digest_nids[4]=NID_gost_mac_12;
123 gost_pkey_meth_nids[2]=NID_gost2012_256;
124 gost_pkey_meth_nids[3]=NID_gost2012_512;
126 cipher_gost_cbc.nid = NID_gost89_cbc;
127 cipher_gost_cpcnt_12.nid = NID_gost89_cnt_12;
129 digest_gost2012_512.type = NID_md_gost12_512;
130 digest_gost2012_256.type = NID_md_gost12_256;
131 imit_gost_cp_12.type = NID_gost_mac_12;
132 /* Algorithm parameters */
133 R3410_2012_512_paramset[0].nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
134 R3410_2012_512_paramset[1].nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
136 if (!ENGINE_set_id(e, engine_gost_id)) {
137 fprintf(stderr,"ENGINE_set_id failed\n");
140 if (!ENGINE_set_name(e, engine_gost_name)) {
141 fprintf(stderr,"ENGINE_set_name failed\n");
144 if (!ENGINE_set_digests(e, gost_digests)) {
145 fprintf(stderr,"ENGINE_set_digests failed\n");
148 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
149 fprintf(stderr,"ENGINE_set_ciphers failed\n");
152 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
153 fprintf(stderr,"ENGINE_set_pkey_meths failed\n");
156 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
157 fprintf(stderr,"ENGINE_set_pkey_asn1_meths failed\n");
160 /* Control function and commands */
161 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
162 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
165 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
166 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
169 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
170 || !ENGINE_set_init_function(e, gost_engine_init)
171 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
175 if (!register_ameth_gost
176 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
177 "GOST R 34.10-2001"))
179 if (!register_ameth_gost
180 (NID_gost2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
181 "GOST R 34.10-2012 with 256 bit key"))
183 if (!register_ameth_gost
184 (NID_gost2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
185 "GOST R 34.10-2012 with 512 bit key"))
187 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
188 "GOST-MAC", "GOST 28147-89 MAC"))
190 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
192 "GOST 28147-89 MAC with 2012 params"))
195 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
198 if (!register_pmeth_gost
199 (NID_gost2012_256, &pmeth_GostR3410_2012_256, 0))
201 if (!register_pmeth_gost
202 (NID_gost2012_512, &pmeth_GostR3410_2012_512, 0))
204 if (!register_pmeth_gost
205 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
207 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
209 if (!ENGINE_register_ciphers(e)
210 || !ENGINE_register_digests(e)
211 || !ENGINE_register_pkey_meths(e)
212 /* These two actually should go in LIST_ADD command */
213 || !EVP_add_cipher(&cipher_gost)
214 || !EVP_add_cipher(&cipher_gost_cbc)
215 || !EVP_add_cipher(&cipher_gost_cpacnt)
216 || !EVP_add_cipher(&cipher_gost_cpcnt_12)
217 || !EVP_add_digest(&digest_gost)
218 || !EVP_add_digest(&digest_gost2012_512)
219 || !EVP_add_digest(&digest_gost2012_256)
220 || !EVP_add_digest(&imit_gost_cpa)
221 || !EVP_add_digest(&imit_gost_cp_12)
226 ERR_load_GOST_strings();
232 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
233 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
234 IMPLEMENT_DYNAMIC_CHECK_FN()
235 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
236 static int gost_digests(ENGINE *e, const EVP_MD **digest,
237 const int **nids, int nid)
241 *nids = gost_digest_nids;
244 if (nid == NID_id_GostR3411_94) {
245 *digest = &digest_gost;
246 } else if (nid == NID_md_gost12_256) {
247 *digest = &digest_gost2012_256;
248 } else if (nid == NID_md_gost12_512) {
249 *digest = &digest_gost2012_512;
250 } else if (nid == NID_id_Gost28147_89_MAC) {
251 *digest = &imit_gost_cpa;
252 } else if (nid == NID_gost_mac_12) {
253 *digest = &imit_gost_cp_12;
261 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
262 const int **nids, int nid)
266 *nids = gost_cipher_nids;
267 return 4; /* three ciphers are supported */
270 if (nid == NID_id_Gost28147_89) {
271 *cipher = &cipher_gost;
272 } else if (nid == NID_gost89_cnt) {
273 *cipher = &cipher_gost_cpacnt;
274 } else if (nid == NID_gost89_cnt_12) {
275 *cipher = &cipher_gost_cpcnt_12;
276 } else if (nid == NID_gost89_cbc) {
277 *cipher = &cipher_gost_cbc;
285 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
286 const int **nids, int nid)
289 *nids = gost_pkey_meth_nids;
290 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
293 if (nid == NID_id_GostR3410_2001) {
294 *pmeth = pmeth_GostR3410_2001;
297 if (nid == NID_gost2012_256) {
298 *pmeth = pmeth_GostR3410_2012_256;
301 if (nid == NID_gost2012_512) {
302 *pmeth = pmeth_GostR3410_2012_512;
305 if (nid == NID_id_Gost28147_89_MAC) {
306 *pmeth = pmeth_Gost28147_MAC;
309 if (nid == NID_gost_mac_12) {
310 *pmeth = pmeth_Gost28147_MAC_12;
317 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
318 const int **nids, int nid)
321 *nids = gost_pkey_meth_nids;
322 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
324 if (nid == NID_id_GostR3410_2001) {
325 *ameth = ameth_GostR3410_2001;
328 if (nid == NID_gost2012_256) {
329 *ameth = ameth_GostR3410_2012_256;
332 if (nid == NID_gost2012_512) {
333 *ameth = ameth_GostR3410_2012_512;
336 if (nid == NID_id_Gost28147_89_MAC) {
337 *ameth = ameth_Gost28147_MAC;
340 if (nid == NID_gost_mac_12) {
341 *ameth = ameth_Gost28147_MAC_12;
349 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
350 static ENGINE *engine_gost(void)
352 ENGINE *ret = ENGINE_new();
355 if (!bind_gost(ret, engine_gost_id)) {
362 void ENGINE_load_gost(void)
365 if (pmeth_GostR3410_2001)
367 toadd = engine_gost();