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