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