]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Added CBC mode for gost and contril command to set size of MAC (from 1 to 8 bytes)
[openssl-gost/engine.git] / 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 /* Symmetric cipher and digest function registrar */
23
24 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
25                         const int **nids, int nid);
26
27 static int gost_digests(ENGINE *e, const EVP_MD **digest,
28                         const int **nids, int ind);
29
30 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
31                            const int **nids, int nid);
32
33 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
34                                 const int **nids, int nid);
35
36 static int gost_cipher_nids[] = {
37     NID_id_Gost28147_89,
38     NID_gost89_cnt,
39     NID_gost89_cnt_12,
40     NID_gost89_cbc,
41     0
42 };
43
44 static int gost_digest_nids[] = {
45     NID_id_GostR3411_94,
46     NID_id_Gost28147_89_MAC,
47     NID_id_GostR3411_2012_256,
48     NID_id_GostR3411_2012_512,
49     NID_gost_mac_12,
50     0
51 };
52
53 static int gost_pkey_meth_nids[] = {
54     NID_id_GostR3410_2001,
55     NID_id_Gost28147_89_MAC,
56     NID_id_GostR3410_2012_256,
57     NID_id_GostR3410_2012_512,
58     NID_gost_mac_12,
59     0
60 };
61
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;
67
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;
73
74 static int gost_engine_init(ENGINE *e)
75 {
76     return 1;
77 }
78
79 static int gost_engine_finish(ENGINE *e)
80 {
81     return 1;
82 }
83
84 static int gost_engine_destroy(ENGINE *e)
85 {
86     gost_param_free();
87
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;
93
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;
99
100     return 1;
101 }
102
103 static int bind_gost(ENGINE *e, const char *id)
104 {
105     int ret = 0;
106     if (id && strcmp(id, engine_gost_id))
107         return 0;
108     if (ameth_GostR3410_2001) {
109         printf("GOST engine already loaded\n");
110         goto end;
111     }
112     if (!ENGINE_set_id(e, engine_gost_id)) {
113         printf("ENGINE_set_id failed\n");
114         goto end;
115     }
116     if (!ENGINE_set_name(e, engine_gost_name)) {
117         printf("ENGINE_set_name failed\n");
118         goto end;
119     }
120     if (!ENGINE_set_digests(e, gost_digests)) {
121         printf("ENGINE_set_digests failed\n");
122         goto end;
123     }
124     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
125         printf("ENGINE_set_ciphers failed\n");
126         goto end;
127     }
128     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
129         printf("ENGINE_set_pkey_meths failed\n");
130         goto end;
131     }
132     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
133         printf("ENGINE_set_pkey_asn1_meths failed\n");
134         goto end;
135     }
136     /* Control function and commands */
137     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
138         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
139         goto end;
140     }
141     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
142         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
143         goto end;
144     }
145     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
146         || !ENGINE_set_init_function(e, gost_engine_init)
147         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
148         goto end;
149     }
150
151     if (!register_ameth_gost
152         (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
153          "GOST R 34.10-2001"))
154         goto end;
155     if (!register_ameth_gost
156         (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
157          "GOST R 34.10-2012 with 256 bit key"))
158         goto end;
159     if (!register_ameth_gost
160         (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
161          "GOST R 34.10-2012 with 512 bit key"))
162         goto end;
163     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
164                              "GOST-MAC", "GOST 28147-89 MAC"))
165         goto end;
166     if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
167                              "GOST-MAC-12",
168                              "GOST 28147-89 MAC with 2012 params"))
169         goto end;
170
171     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
172         goto end;
173
174     if (!register_pmeth_gost
175         (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
176         goto end;
177     if (!register_pmeth_gost
178         (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
179         goto end;
180     if (!register_pmeth_gost
181         (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
182         goto end;
183     if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
184         goto end;
185     if (!ENGINE_register_ciphers(e)
186         || !ENGINE_register_digests(e)
187         || !ENGINE_register_pkey_meths(e)
188         /* These two actually should go in LIST_ADD command */
189         || !EVP_add_cipher(&cipher_gost)
190         || !EVP_add_cipher(&cipher_gost_cbc)
191         || !EVP_add_cipher(&cipher_gost_cpacnt)
192         || !EVP_add_cipher(&cipher_gost_cpcnt_12)
193         || !EVP_add_digest(&digest_gost)
194         || !EVP_add_digest(&digest_gost2012_512)
195         || !EVP_add_digest(&digest_gost2012_256)
196         || !EVP_add_digest(&imit_gost_cpa)
197         || !EVP_add_digest(&imit_gost_cp_12)
198         ) {
199         goto end;
200     }
201
202     ERR_load_GOST_strings();
203     ret = 1;
204  end:
205     return ret;
206 }
207
208 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
209 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
210     IMPLEMENT_DYNAMIC_CHECK_FN()
211 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
212 static int gost_digests(ENGINE *e, const EVP_MD **digest,
213                         const int **nids, int nid)
214 {
215     int ok = 1;
216     if (!digest) {
217         *nids = gost_digest_nids;
218         return 5;
219     }
220     if (nid == NID_id_GostR3411_94) {
221         *digest = &digest_gost;
222     } else if (nid == NID_id_GostR3411_2012_256) {
223         *digest = &digest_gost2012_256;
224     } else if (nid == NID_id_GostR3411_2012_512) {
225         *digest = &digest_gost2012_512;
226     } else if (nid == NID_id_Gost28147_89_MAC) {
227         *digest = &imit_gost_cpa;
228     } else if (nid == NID_gost_mac_12) {
229         *digest = &imit_gost_cp_12;
230     } else {
231         ok = 0;
232         *digest = NULL;
233     }
234     return ok;
235 }
236
237 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
238                         const int **nids, int nid)
239 {
240     int ok = 1;
241     if (!cipher) {
242         *nids = gost_cipher_nids;
243         return 4;               /* three ciphers are supported */
244     }
245
246     if (nid == NID_id_Gost28147_89) {
247         *cipher = &cipher_gost;
248     } else if (nid == NID_gost89_cnt) {
249         *cipher = &cipher_gost_cpacnt;
250     } else if (nid == NID_gost89_cnt_12) {
251         *cipher = &cipher_gost_cpcnt_12;
252     } else if (nid == NID_gost89_cbc) {
253         *cipher = &cipher_gost_cbc;
254     } else {
255         ok = 0;
256         *cipher = NULL;
257     }
258     return ok;
259 }
260
261 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
262                            const int **nids, int nid)
263 {
264     if (!pmeth) {
265         *nids = gost_pkey_meth_nids;
266         return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
267     }
268
269     switch (nid) {
270     case NID_id_GostR3410_2001:
271         *pmeth = pmeth_GostR3410_2001;
272         return 1;
273     case NID_id_GostR3410_2012_256:
274         *pmeth = pmeth_GostR3410_2012_256;
275         return 1;
276     case NID_id_GostR3410_2012_512:
277         *pmeth = pmeth_GostR3410_2012_512;
278         return 1;
279     case NID_id_Gost28147_89_MAC:
280         *pmeth = pmeth_Gost28147_MAC;
281         return 1;
282     case NID_gost_mac_12:
283         *pmeth = pmeth_Gost28147_MAC_12;
284         return 1;
285
286     default:;
287     }
288
289     *pmeth = NULL;
290     return 0;
291 }
292
293 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
294                                 const int **nids, int nid)
295 {
296     if (!ameth) {
297         *nids = gost_pkey_meth_nids;
298         return sizeof(gost_pkey_meth_nids)/sizeof(int) - 1;
299     }
300     switch (nid) {
301     case NID_id_GostR3410_2001:
302         *ameth = ameth_GostR3410_2001;
303         return 1;
304     case NID_id_GostR3410_2012_256:
305         *ameth = ameth_GostR3410_2012_256;
306         return 1;
307     case NID_id_GostR3410_2012_512:
308         *ameth = ameth_GostR3410_2012_512;
309         return 1;
310     case NID_id_Gost28147_89_MAC:
311         *ameth = ameth_Gost28147_MAC;
312         return 1;
313     case NID_gost_mac_12:
314         *ameth = ameth_Gost28147_MAC_12;
315         return 1;
316
317     default:;
318     }
319
320     *ameth = NULL;
321     return 0;
322 }
323
324 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
325 static ENGINE *engine_gost(void)
326 {
327     ENGINE *ret = ENGINE_new();
328     if (!ret)
329         return NULL;
330     if (!bind_gost(ret, engine_gost_id)) {
331         ENGINE_free(ret);
332         return NULL;
333     }
334     return ret;
335 }
336
337 void ENGINE_load_gost(void)
338 {
339     ENGINE *toadd;
340     if (pmeth_GostR3410_2001)
341         return;
342     toadd = engine_gost();
343     if (!toadd)
344         return;
345     ENGINE_add(toadd);
346     ENGINE_free(toadd);
347     ERR_clear_error();
348 }
349 #endif