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[] = { NID_id_Gost28147_89, NID_gost89_cnt, 0 };
38 static int gost_digest_nids[] =
39 { NID_id_GostR3411_94, NID_id_Gost28147_89_MAC, 0 };
41 static int gost_pkey_meth_nids[] = { NID_id_GostR3410_94,
42 NID_id_GostR3410_2001, NID_id_Gost28147_89_MAC, 0
45 static EVP_PKEY_METHOD *pmeth_GostR3410_94 = NULL,
46 *pmeth_GostR3410_2001 = NULL, *pmeth_Gost28147_MAC = NULL;
48 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_94 = NULL,
49 *ameth_GostR3410_2001 = NULL, *ameth_Gost28147_MAC = NULL;
51 static int gost_engine_init(ENGINE *e)
56 static int gost_engine_finish(ENGINE *e)
61 static int gost_engine_destroy(ENGINE *e)
65 pmeth_GostR3410_94 = NULL;
66 pmeth_GostR3410_2001 = NULL;
67 pmeth_Gost28147_MAC = NULL;
68 ameth_GostR3410_94 = NULL;
69 ameth_GostR3410_2001 = NULL;
70 ameth_Gost28147_MAC = NULL;
74 static int bind_gost(ENGINE *e, const char *id)
77 if (id && strcmp(id, engine_gost_id))
79 if (ameth_GostR3410_94) {
80 printf("GOST engine already loaded\n");
84 if (!ENGINE_set_id(e, engine_gost_id)) {
85 printf("ENGINE_set_id failed\n");
88 if (!ENGINE_set_name(e, engine_gost_name)) {
89 printf("ENGINE_set_name failed\n");
92 if (!ENGINE_set_digests(e, gost_digests)) {
93 printf("ENGINE_set_digests failed\n");
96 if (!ENGINE_set_ciphers(e, gost_ciphers)) {
97 printf("ENGINE_set_ciphers failed\n");
100 if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
101 printf("ENGINE_set_pkey_meths failed\n");
104 if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
105 printf("ENGINE_set_pkey_asn1_meths failed\n");
108 /* Control function and commands */
109 if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
110 fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
113 if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
114 fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
117 if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
118 || !ENGINE_set_init_function(e, gost_engine_init)
119 || !ENGINE_set_finish_function(e, gost_engine_finish)) {
123 if (!register_ameth_gost
124 (NID_id_GostR3410_94, &ameth_GostR3410_94, "GOST94",
127 if (!register_ameth_gost
128 (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
129 "GOST R 34.10-2001"))
131 if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
132 "GOST-MAC", "GOST 28147-89 MAC"))
135 if (!register_pmeth_gost(NID_id_GostR3410_94, &pmeth_GostR3410_94, 0))
137 if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
139 if (!register_pmeth_gost
140 (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
142 if (!ENGINE_register_ciphers(e)
143 || !ENGINE_register_digests(e)
144 || !ENGINE_register_pkey_meths(e)
145 /* These two actually should go in LIST_ADD command */
146 || !EVP_add_cipher(&cipher_gost)
147 || !EVP_add_cipher(&cipher_gost_cpacnt)
148 || !EVP_add_digest(&digest_gost)
149 || !EVP_add_digest(&imit_gost_cpa)
154 ERR_load_GOST_strings();
160 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
161 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
162 IMPLEMENT_DYNAMIC_CHECK_FN()
163 #endif /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
164 static int gost_digests(ENGINE *e, const EVP_MD **digest,
165 const int **nids, int nid)
169 *nids = gost_digest_nids;
173 * printf("Digest no %d requested\n",nid);
175 if (nid == NID_id_GostR3411_94) {
176 *digest = &digest_gost;
177 } else if (nid == NID_id_Gost28147_89_MAC) {
178 *digest = &imit_gost_cpa;
186 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
187 const int **nids, int nid)
191 *nids = gost_cipher_nids;
192 return 2; /* two ciphers are supported */
195 if (nid == NID_id_Gost28147_89) {
196 *cipher = &cipher_gost;
197 } else if (nid == NID_gost89_cnt) {
198 *cipher = &cipher_gost_cpacnt;
206 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
207 const int **nids, int nid)
210 *nids = gost_pkey_meth_nids;
215 case NID_id_GostR3410_94:
216 *pmeth = pmeth_GostR3410_94;
218 case NID_id_GostR3410_2001:
219 *pmeth = pmeth_GostR3410_2001;
221 case NID_id_Gost28147_89_MAC:
222 *pmeth = pmeth_Gost28147_MAC;
231 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
232 const int **nids, int nid)
235 *nids = gost_pkey_meth_nids;
239 case NID_id_GostR3410_94:
240 *ameth = ameth_GostR3410_94;
242 case NID_id_GostR3410_2001:
243 *ameth = ameth_GostR3410_2001;
245 case NID_id_Gost28147_89_MAC:
246 *ameth = ameth_Gost28147_MAC;
256 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
257 static ENGINE *engine_gost(void)
259 ENGINE *ret = ENGINE_new();
262 if (!bind_gost(ret, engine_gost_id)) {
269 void ENGINE_load_gost(void)
272 if (pmeth_GostR3410_94)
274 toadd = engine_gost();