4129260286b350d563b7a3224d49a0961d40f0ae
[openssl.git] / engines / ccgost / gost_eng.c
1 /**********************************************************************
2  *                          gost_eng.c                                *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *              Main file of GOST engine                              *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <string.h>
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"
17 #include "gost_lcl.h"
18 static const char *engine_gost_id = "gost";
19 static const char *engine_gost_name =
20     "Reference implementation of GOST engine";
21
22 static int gost_pkey_meth_nids[] = {
23     NID_id_GostR3410_2001, NID_id_Gost28147_89_MAC, 0
24 };
25
26 /* Symmetric cipher and digest function registrar */
27
28 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
29                         const int **nids, int nid);
30
31 static int gost_digests(ENGINE *e, const EVP_MD **digest,
32                         const int **nids, int ind);
33
34 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
35                            const int **nids, int nid);
36
37 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
38                                 const int **nids, int nid);
39
40 static int gost_cipher_nids[] = { NID_id_Gost28147_89, NID_gost89_cnt, 0 };
41
42 static int gost_digest_nids[] =
43     { NID_id_GostR3411_94, NID_id_Gost28147_89_MAC, 0 };
44
45 static EVP_PKEY_METHOD *pmeth_GostR3410_2001 = NULL;
46 static EVP_PKEY_METHOD *pmeth_Gost28147_MAC = NULL;
47
48 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_2001 = NULL;
49 static EVP_PKEY_ASN1_METHOD *ameth_Gost28147_MAC = NULL;
50
51 static int gost_engine_init(ENGINE *e)
52 {
53     return 1;
54 }
55
56 static int gost_engine_finish(ENGINE *e)
57 {
58     return 1;
59 }
60
61 static int gost_engine_destroy(ENGINE *e)
62 {
63     gost_param_free();
64
65     pmeth_GostR3410_2001 = NULL;
66     pmeth_Gost28147_MAC = NULL;
67     ameth_GostR3410_2001 = NULL;
68     ameth_Gost28147_MAC = NULL;
69     return 1;
70 }
71
72 static int bind_gost(ENGINE *e, const char *id)
73 {
74     int ret = 0;
75     if (id && strcmp(id, engine_gost_id))
76         return 0;
77     if (ameth_GostR3410_2001) {
78         printf("GOST engine already loaded\n");
79         goto end;
80     }
81
82     if (!ENGINE_set_id(e, engine_gost_id)) {
83         printf("ENGINE_set_id failed\n");
84         goto end;
85     }
86     if (!ENGINE_set_name(e, engine_gost_name)) {
87         printf("ENGINE_set_name failed\n");
88         goto end;
89     }
90     if (!ENGINE_set_digests(e, gost_digests)) {
91         printf("ENGINE_set_digests failed\n");
92         goto end;
93     }
94     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
95         printf("ENGINE_set_ciphers failed\n");
96         goto end;
97     }
98     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
99         printf("ENGINE_set_pkey_meths failed\n");
100         goto end;
101     }
102     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
103         printf("ENGINE_set_pkey_asn1_meths failed\n");
104         goto end;
105     }
106     /* Control function and commands */
107     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
108         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
109         goto end;
110     }
111     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
112         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
113         goto end;
114     }
115     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
116         || !ENGINE_set_init_function(e, gost_engine_init)
117         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
118         goto end;
119     }
120
121     if (!register_ameth_gost
122         (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
123          "GOST R 34.10-2001"))
124         goto end;
125     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
126                              "GOST-MAC", "GOST 28147-89 MAC"))
127         goto end;
128
129     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
130         goto end;
131     if (!register_pmeth_gost(NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
132         goto end;
133     if (!ENGINE_register_ciphers(e)
134         || !ENGINE_register_digests(e)
135         || !ENGINE_register_pkey_meths(e)
136         /* These two actually should go in LIST_ADD command */
137         || !EVP_add_cipher(&cipher_gost)
138         || !EVP_add_cipher(&cipher_gost_cpacnt)
139         || !EVP_add_digest(&digest_gost)
140         || !EVP_add_digest(&imit_gost_cpa)
141         ) {
142         goto end;
143     }
144
145     ERR_load_GOST_strings();
146     ret = 1;
147  end:
148     return ret;
149 }
150
151 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
152 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
153     IMPLEMENT_DYNAMIC_CHECK_FN()
154 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
155 static int gost_digests(ENGINE *e, const EVP_MD **digest,
156                         const int **nids, int nid)
157 {
158     int ok = 1;
159     if (!digest) {
160         *nids = gost_digest_nids;
161         return 2;
162     }
163     /*
164      * printf("Digest no %d requested\n",nid);
165      */
166     if (nid == NID_id_GostR3411_94) {
167         *digest = &digest_gost;
168     } else if (nid == NID_id_Gost28147_89_MAC) {
169         *digest = &imit_gost_cpa;
170     } else {
171         ok = 0;
172         *digest = NULL;
173     }
174     return ok;
175 }
176
177 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
178                         const int **nids, int nid)
179 {
180     int ok = 1;
181     if (!cipher) {
182         *nids = gost_cipher_nids;
183         return 2;               /* two ciphers are supported */
184     }
185
186     if (nid == NID_id_Gost28147_89) {
187         *cipher = &cipher_gost;
188     } else if (nid == NID_gost89_cnt) {
189         *cipher = &cipher_gost_cpacnt;
190     } else {
191         ok = 0;
192         *cipher = NULL;
193     }
194     return ok;
195 }
196
197 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
198                            const int **nids, int nid)
199 {
200     if (!pmeth) {
201         *nids = gost_pkey_meth_nids;
202         return 2;
203     }
204
205     switch (nid) {
206     case NID_id_GostR3410_2001:
207         *pmeth = pmeth_GostR3410_2001;
208         return 1;
209     case NID_id_Gost28147_89_MAC:
210         *pmeth = pmeth_Gost28147_MAC;
211         return 1;
212     default:;
213     }
214
215     *pmeth = NULL;
216     return 0;
217 }
218
219 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
220                                 const int **nids, int nid)
221 {
222     if (!ameth) {
223         *nids = gost_pkey_meth_nids;
224         return 2;
225     }
226     switch (nid) {
227     case NID_id_GostR3410_2001:
228         *ameth = ameth_GostR3410_2001;
229         return 1;
230     case NID_id_Gost28147_89_MAC:
231         *ameth = ameth_Gost28147_MAC;
232         return 1;
233
234     default:;
235     }
236
237     *ameth = NULL;
238     return 0;
239 }
240
241 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
242 static ENGINE *engine_gost(void)
243 {
244     ENGINE *ret = ENGINE_new();
245     if (!ret)
246         return NULL;
247     if (!bind_gost(ret, engine_gost_id)) {
248         ENGINE_free(ret);
249         return NULL;
250     }
251     return ret;
252 }
253
254 void ENGINE_load_gost(void)
255 {
256     ENGINE *toadd;
257     if (pmeth_GostR3410_2001)
258         return;
259     toadd = engine_gost();
260     if (!toadd)
261         return;
262     ENGINE_add(toadd);
263     ENGINE_free(toadd);
264     ERR_clear_error();
265 }
266 #endif