]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Start implementing MAGMA algorithm
[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
19 #include "gost_grasshopper_cipher.h"
20
21 static const char* engine_gost_id = "gost";
22
23 static const char* engine_gost_name =
24         "Reference implementation of GOST engine";
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[] = {
41         NID_id_Gost28147_89,
42         NID_gost89_cnt,
43         NID_gost89_cnt_12,
44         NID_gost89_cbc,
45         NID_grasshopper_ecb,
46         NID_grasshopper_cbc,
47         NID_grasshopper_cfb,
48         NID_grasshopper_ofb,
49         NID_grasshopper_ctr,
50         0
51 };
52
53 static int gost_digest_nids(const int** nids) {
54     static int digest_nids[6] = {0, 0, 0, 0, 0, 0};
55     static int pos = 0;
56     static int init = 0;
57
58     if (!init) {
59         const EVP_MD* md;
60         if ((md = digest_gost()) != NULL)
61             digest_nids[pos++] = EVP_MD_type(md);
62         if ((md = imit_gost_cpa()) != NULL)
63             digest_nids[pos++] = EVP_MD_type(md);
64         if ((md = digest_gost2012_256()) != NULL)
65             digest_nids[pos++] = EVP_MD_type(md);
66         if ((md = digest_gost2012_512()) != NULL)
67             digest_nids[pos++] = EVP_MD_type(md);
68         if ((md = imit_gost_cp_12()) != NULL)
69             digest_nids[pos++] = EVP_MD_type(md);
70         digest_nids[pos] = 0;
71         init = 1;
72     }
73     *nids = digest_nids;
74     return pos;
75 }
76
77 static int gost_pkey_meth_nids[] = {
78         NID_id_GostR3410_2001,
79         NID_id_Gost28147_89_MAC,
80         NID_id_GostR3410_2012_256,
81         NID_id_GostR3410_2012_512,
82         NID_gost_mac_12,
83         0
84 };
85
86 static EVP_PKEY_METHOD* pmeth_GostR3410_2001 = NULL,
87         * pmeth_GostR3410_2012_256 = NULL,
88         * pmeth_GostR3410_2012_512 = NULL,
89         * pmeth_Gost28147_MAC = NULL, * pmeth_Gost28147_MAC_12 = NULL;
90
91 static EVP_PKEY_ASN1_METHOD* ameth_GostR3410_2001 = NULL,
92         * ameth_GostR3410_2012_256 = NULL,
93         * ameth_GostR3410_2012_512 = NULL,
94         * ameth_Gost28147_MAC = NULL, * ameth_Gost28147_MAC_12 = NULL;
95
96 static int gost_engine_init(ENGINE* e) {
97     return 1;
98 }
99
100 static int gost_engine_finish(ENGINE* e) {
101     return 1;
102 }
103
104 static int gost_engine_destroy(ENGINE* e) {
105     digest_gost_destroy();
106     digest_gost2012_256_destroy();
107     digest_gost2012_512_destroy();
108
109     imit_gost_cpa_destroy();
110     imit_gost_cp_12_destroy();
111
112     cipher_gost_destroy();
113
114     gost_param_free();
115
116     pmeth_GostR3410_2001 = NULL;
117     pmeth_Gost28147_MAC = NULL;
118     pmeth_GostR3410_2012_256 = NULL;
119     pmeth_GostR3410_2012_512 = NULL;
120     pmeth_Gost28147_MAC_12 = NULL;
121
122     ameth_GostR3410_2001 = NULL;
123     ameth_Gost28147_MAC = NULL;
124     ameth_GostR3410_2012_256 = NULL;
125     ameth_GostR3410_2012_512 = NULL;
126     ameth_Gost28147_MAC_12 = NULL;
127
128         ERR_unload_GOST_strings();
129         
130     return 1;
131 }
132
133 static int bind_gost(ENGINE* e, const char* id) {
134     int ret = 0;
135     if (id != NULL && strcmp(id, engine_gost_id) != 0)
136         return 0;
137     if (ameth_GostR3410_2001) {
138         printf("GOST engine already loaded\n");
139         goto end;
140     }
141     if (!ENGINE_set_id(e, engine_gost_id)) {
142         printf("ENGINE_set_id failed\n");
143         goto end;
144     }
145     if (!ENGINE_set_name(e, engine_gost_name)) {
146         printf("ENGINE_set_name failed\n");
147         goto end;
148     }
149     if (!ENGINE_set_digests(e, gost_digests)) {
150         printf("ENGINE_set_digests failed\n");
151         goto end;
152     }
153     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
154         printf("ENGINE_set_ciphers failed\n");
155         goto end;
156     }
157     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
158         printf("ENGINE_set_pkey_meths failed\n");
159         goto end;
160     }
161     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
162         printf("ENGINE_set_pkey_asn1_meths failed\n");
163         goto end;
164     }
165     /* Control function and commands */
166     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
167         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
168         goto end;
169     }
170     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
171         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
172         goto end;
173     }
174     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
175         || !ENGINE_set_init_function(e, gost_engine_init)
176         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
177         goto end;
178     }
179
180     if (!register_ameth_gost
181             (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
182              "GOST R 34.10-2001"))
183         goto end;
184     if (!register_ameth_gost
185             (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
186              "GOST R 34.10-2012 with 256 bit key"))
187         goto end;
188     if (!register_ameth_gost
189             (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
190              "GOST R 34.10-2012 with 512 bit key"))
191         goto end;
192     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
193                              "GOST-MAC", "GOST 28147-89 MAC"))
194         goto end;
195     if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
196                              "GOST-MAC-12",
197                              "GOST 28147-89 MAC with 2012 params"))
198         goto end;
199
200     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
201         goto end;
202
203     if (!register_pmeth_gost
204             (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
205         goto end;
206     if (!register_pmeth_gost
207             (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
208         goto end;
209     if (!register_pmeth_gost
210             (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
211         goto end;
212     if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
213         goto end;
214     if (!ENGINE_register_ciphers(e)
215         || !ENGINE_register_digests(e)
216         || !ENGINE_register_pkey_meths(e)
217         /* These two actually should go in LIST_ADD command */
218         || !EVP_add_cipher(cipher_gost())
219         || !EVP_add_cipher(cipher_gost_cbc())
220         || !EVP_add_cipher(cipher_gost_cpacnt())
221         || !EVP_add_cipher(cipher_gost_cpcnt_12())
222         || !EVP_add_cipher(cipher_gost_grasshopper_ecb())
223         || !EVP_add_cipher(cipher_gost_grasshopper_cbc())
224         || !EVP_add_cipher(cipher_gost_grasshopper_cfb())
225         || !EVP_add_cipher(cipher_gost_grasshopper_ofb())
226         || !EVP_add_cipher(cipher_gost_grasshopper_ctr())
227         || !EVP_add_cipher(cipher_magma_cbc())
228         || !EVP_add_digest(digest_gost())
229         || !EVP_add_digest(digest_gost2012_512())
230         || !EVP_add_digest(digest_gost2012_256())
231         || !EVP_add_digest(imit_gost_cpa())
232         || !EVP_add_digest(imit_gost_cp_12())
233             ) {
234         goto end;
235     }
236
237     ENGINE_register_all_complete();
238
239     ERR_load_GOST_strings();
240     ret = 1;
241     end:
242     return ret;
243 }
244
245 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
246 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
247     IMPLEMENT_DYNAMIC_CHECK_FN()
248 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
249
250 static int gost_digests(ENGINE* e, const EVP_MD** digest,
251                         const int** nids, int nid) {
252     int ok = 1;
253     if (digest == NULL) {
254         return gost_digest_nids(nids);
255     }
256     if (nid == NID_id_GostR3411_94) {
257         *digest = digest_gost();
258     } else if (nid == NID_id_Gost28147_89_MAC) {
259         *digest = imit_gost_cpa();
260     } else if (nid == NID_id_GostR3411_2012_256) {
261         *digest = digest_gost2012_256();
262     } else if (nid == NID_id_GostR3411_2012_512) {
263         *digest = digest_gost2012_512();
264     } else if (nid == NID_gost_mac_12) {
265         *digest = imit_gost_cp_12();
266     } else {
267         ok = 0;
268         *digest = NULL;
269     }
270     return ok;
271 }
272
273 static int gost_ciphers(ENGINE* e, const EVP_CIPHER** cipher,
274                         const int** nids, int nid) {
275     int ok = 1;
276     if (cipher == NULL) {
277         *nids = gost_cipher_nids;
278         return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
279     }
280
281     if (nid == NID_id_Gost28147_89) {
282         *cipher = cipher_gost();
283     } else if (nid == NID_gost89_cnt) {
284         *cipher = cipher_gost_cpacnt();
285     } else if (nid == NID_gost89_cnt_12) {
286         *cipher = cipher_gost_cpcnt_12();
287     } else if (nid == NID_gost89_cbc) {
288         *cipher = cipher_gost_cbc();
289     } else if (nid == NID_grasshopper_ecb) {
290         *cipher = cipher_gost_grasshopper_ecb();
291     } else if (nid == NID_grasshopper_cbc) {
292         *cipher = cipher_gost_grasshopper_cbc();
293     } else if (nid == NID_grasshopper_cfb) {
294         *cipher = cipher_gost_grasshopper_cfb();
295     } else if (nid == NID_grasshopper_ofb) {
296         *cipher = cipher_gost_grasshopper_ofb();
297     } else if (nid == NID_grasshopper_ctr) {
298         *cipher = cipher_gost_grasshopper_ctr();
299     } else {
300         ok = 0;
301         *cipher = NULL;
302     }
303     return ok;
304 }
305
306 static int gost_pkey_meths(ENGINE* e, EVP_PKEY_METHOD** pmeth,
307                            const int** nids, int nid) {
308     if (pmeth == NULL) {
309         *nids = gost_pkey_meth_nids;
310         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
311     }
312
313     switch (nid) {
314         case NID_id_GostR3410_2001:
315             *pmeth = pmeth_GostR3410_2001;
316             return 1;
317         case NID_id_GostR3410_2012_256:
318             *pmeth = pmeth_GostR3410_2012_256;
319             return 1;
320         case NID_id_GostR3410_2012_512:
321             *pmeth = pmeth_GostR3410_2012_512;
322             return 1;
323         case NID_id_Gost28147_89_MAC:
324             *pmeth = pmeth_Gost28147_MAC;
325             return 1;
326         case NID_gost_mac_12:
327             *pmeth = pmeth_Gost28147_MAC_12;
328             return 1;
329
330         default:;
331     }
332
333     *pmeth = NULL;
334     return 0;
335 }
336
337 static int gost_pkey_asn1_meths(ENGINE* e, EVP_PKEY_ASN1_METHOD** ameth,
338                                 const int** nids, int nid) {
339     if (ameth == NULL) {
340         *nids = gost_pkey_meth_nids;
341         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) - 1;
342     }
343
344     switch (nid) {
345         case NID_id_GostR3410_2001:
346             *ameth = ameth_GostR3410_2001;
347             return 1;
348         case NID_id_GostR3410_2012_256:
349             *ameth = ameth_GostR3410_2012_256;
350             return 1;
351         case NID_id_GostR3410_2012_512:
352             *ameth = ameth_GostR3410_2012_512;
353             return 1;
354         case NID_id_Gost28147_89_MAC:
355             *ameth = ameth_Gost28147_MAC;
356             return 1;
357         case NID_gost_mac_12:
358             *ameth = ameth_Gost28147_MAC_12;
359             return 1;
360
361         default:;
362     }
363
364     *ameth = NULL;
365     return 0;
366 }
367
368 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
369
370 static ENGINE* engine_gost(void) {
371     ENGINE* ret = ENGINE_new();
372     if (!ret)
373         return NULL;
374     if (!bind_gost(ret, engine_gost_id)) {
375         ENGINE_free(ret);
376         return NULL;
377     }
378     return ret;
379 }
380
381 void ENGINE_load_gost(void) {
382     ENGINE* toadd;
383     if (pmeth_GostR3410_2001)
384         return;
385     toadd = engine_gost();
386     if (!toadd)
387         return;
388     ENGINE_add(toadd);
389     ENGINE_free(toadd);
390     ERR_clear_error();
391 }
392
393 #endif