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