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