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;
125 gost_pkey_meth_nids[4]=NID_gost_mac_12;
127 cipher_gost_cbc.nid = NID_gost89_cbc;
128 cipher_gost_cpcnt_12.nid = NID_gost89_cnt_12;
130 digest_gost2012_512.type = NID_md_gost12_512;
131 digest_gost2012_256.type = NID_md_gost12_256;
132 imit_gost_cp_12.type = NID_gost_mac_12;
133 /* Algorithm parameters */
134 R3410_2012_512_paramset[0].nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
135 R3410_2012_512_paramset[1].nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
137 if (!ENGINE_set_id(e, engine_gost_id)) {
138 fprintf(stderr,"ENGINE_set_id failed\n");
141 if (!ENGINE_set_name(e, engine_gost_name)) {
142 fprintf(stderr,"ENGINE_set_name failed\n");
145 if (!ENGINE_set_digests(e, gost_digests)) {
146 fprintf(stderr,"ENGINE_set_digests failed\n");
149 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
150 fprintf(stderr,"ENGINE_set_ciphers failed\n");
153 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
154 fprintf(stderr,"ENGINE_set_pkey_meths failed\n");
157 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
158 fprintf(stderr,"ENGINE_set_pkey_asn1_meths failed\n");
161 /* Control function and commands */
162 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
163 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
166 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
167 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
170 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
171 || !ENGINE_set_init_function(e, gost_engine_init)
172 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
176 if (!register_ameth_gost
177 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
178 "GOST R 34.10-2001"))
180 if (!register_ameth_gost
181 (NID_gost2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
182 "GOST R 34.10-2012 with 256 bit key"))
184 if (!register_ameth_gost
185 (NID_gost2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
186 "GOST R 34.10-2012 with 512 bit key"))
188 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
189 "GOST-MAC", "GOST 28147-89 MAC"))
191 if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
193 "GOST 28147-89 MAC with 2012 params"))
196 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
199 if (!register_pmeth_gost
200 (NID_gost2012_256, &pmeth_GostR3410_2012_256, 0))
202 if (!register_pmeth_gost
203 (NID_gost2012_512, &pmeth_GostR3410_2012_512, 0))
205 if (!register_pmeth_gost
206 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
208 if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
210 if (!ENGINE_register_ciphers(e)
211 || !ENGINE_register_digests(e)
212 || !ENGINE_register_pkey_meths(e)
213 /* These two actually should go in LIST_ADD command */
214 || !EVP_add_cipher(&cipher_gost)
215 || !EVP_add_cipher(&cipher_gost_cbc)
216 || !EVP_add_cipher(&cipher_gost_cpacnt)
217 || !EVP_add_cipher(&cipher_gost_cpcnt_12)
218 || !EVP_add_digest(&digest_gost)
219 || !EVP_add_digest(&digest_gost2012_512)
220 || !EVP_add_digest(&digest_gost2012_256)
221 || !EVP_add_digest(&imit_gost_cpa)
222 || !EVP_add_digest(&imit_gost_cp_12)
227 ERR_load_GOST_strings();
233 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
234 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
235 IMPLEMENT_DYNAMIC_CHECK_FN()
236 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
237 static int gost_digests(ENGINE *e, const EVP_MD **digest,
238 const int **nids, int nid)
242 *nids = gost_digest_nids;
245 if (nid == NID_id_GostR3411_94) {
246 *digest = &digest_gost;
247 } else if (nid == NID_md_gost12_256) {
248 *digest = &digest_gost2012_256;
249 } else if (nid == NID_md_gost12_512) {
250 *digest = &digest_gost2012_512;
251 } else if (nid == NID_id_Gost28147_89_MAC) {
252 *digest = &imit_gost_cpa;
253 } else if (nid == NID_gost_mac_12) {
254 *digest = &imit_gost_cp_12;
262 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
263 const int **nids, int nid)
267 *nids = gost_cipher_nids;
268 return 4; /* four ciphers are supported */
271 if (nid == NID_id_Gost28147_89) {
272 *cipher = &cipher_gost;
273 } else if (nid == NID_gost89_cnt) {
274 *cipher = &cipher_gost_cpacnt;
275 } else if (nid == NID_gost89_cnt_12) {
276 *cipher = &cipher_gost_cpcnt_12;
277 } else if (nid == NID_gost89_cbc) {
278 *cipher = &cipher_gost_cbc;
286 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
287 const int **nids, int nid)
290 *nids = gost_pkey_meth_nids;
291 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
294 if (nid == NID_id_GostR3410_2001) {
295 *pmeth = pmeth_GostR3410_2001;
298 if (nid == NID_gost2012_256) {
299 *pmeth = pmeth_GostR3410_2012_256;
302 if (nid == NID_gost2012_512) {
303 *pmeth = pmeth_GostR3410_2012_512;
306 if (nid == NID_id_Gost28147_89_MAC) {
307 *pmeth = pmeth_Gost28147_MAC;
310 if (nid == NID_gost_mac_12) {
311 *pmeth = pmeth_Gost28147_MAC_12;
318 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
319 const int **nids, int nid)
322 *nids = gost_pkey_meth_nids;
323 return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
325 if (nid == NID_id_GostR3410_2001) {
326 *ameth = ameth_GostR3410_2001;
329 if (nid == NID_gost2012_256) {
330 *ameth = ameth_GostR3410_2012_256;
333 if (nid == NID_gost2012_512) {
334 *ameth = ameth_GostR3410_2012_512;
337 if (nid == NID_id_Gost28147_89_MAC) {
338 *ameth = ameth_Gost28147_MAC;
341 if (nid == NID_gost_mac_12) {
342 *ameth = ameth_Gost28147_MAC_12;
350 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
351 static ENGINE *engine_gost(void)
353 ENGINE *ret = ENGINE_new();
356 if (!bind_gost(ret, engine_gost_id)) {
363 void ENGINE_load_gost(void)
366 if (pmeth_GostR3410_2001)
368 toadd = engine_gost();