Coverage Report

Created: 2022-11-30 06:20

/src/openssl/engines/ccgost/gost_eng.c
Line
Count
Source (jump to first uncovered line)
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
static const char *engine_gost_id = "gost";
19
static const char *engine_gost_name =
20
    "Reference implementation of GOST engine";
21
22
/* Symmetric cipher and digest function registrar */
23
24
static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
25
                        const int **nids, int nid);
26
27
static int gost_digests(ENGINE *e, const EVP_MD **digest,
28
                        const int **nids, int ind);
29
30
static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
31
                           const int **nids, int nid);
32
33
static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
34
                                const int **nids, int nid);
35
36
static int gost_cipher_nids[] = { NID_id_Gost28147_89, NID_gost89_cnt, 0 };
37
38
static int gost_digest_nids[] =
39
    { NID_id_GostR3411_94, NID_id_Gost28147_89_MAC, 0 };
40
41
static int gost_pkey_meth_nids[] = { NID_id_GostR3410_94,
42
    NID_id_GostR3410_2001, NID_id_Gost28147_89_MAC, 0
43
};
44
45
static EVP_PKEY_METHOD *pmeth_GostR3410_94 = NULL,
46
    *pmeth_GostR3410_2001 = NULL, *pmeth_Gost28147_MAC = NULL;
47
48
static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_94 = NULL,
49
    *ameth_GostR3410_2001 = NULL, *ameth_Gost28147_MAC = NULL;
50
51
static int gost_engine_init(ENGINE *e)
52
0
{
53
0
    return 1;
54
0
}
55
56
static int gost_engine_finish(ENGINE *e)
57
0
{
58
0
    return 1;
59
0
}
60
61
static int gost_engine_destroy(ENGINE *e)
62
0
{
63
0
    gost_param_free();
64
65
0
    pmeth_GostR3410_94 = NULL;
66
0
    pmeth_GostR3410_2001 = NULL;
67
0
    pmeth_Gost28147_MAC = NULL;
68
0
    ameth_GostR3410_94 = NULL;
69
0
    ameth_GostR3410_2001 = NULL;
70
0
    ameth_Gost28147_MAC = NULL;
71
0
    return 1;
72
0
}
73
74
static int bind_gost(ENGINE *e, const char *id)
75
19
{
76
19
    int ret = 0;
77
19
    if (id && strcmp(id, engine_gost_id))
78
0
        return 0;
79
19
    if (ameth_GostR3410_94) {
80
0
        printf("GOST engine already loaded\n");
81
0
        goto end;
82
0
    }
83
84
19
    if (!ENGINE_set_id(e, engine_gost_id)) {
85
0
        printf("ENGINE_set_id failed\n");
86
0
        goto end;
87
0
    }
88
19
    if (!ENGINE_set_name(e, engine_gost_name)) {
89
0
        printf("ENGINE_set_name failed\n");
90
0
        goto end;
91
0
    }
92
19
    if (!ENGINE_set_digests(e, gost_digests)) {
93
0
        printf("ENGINE_set_digests failed\n");
94
0
        goto end;
95
0
    }
96
19
    if (!ENGINE_set_ciphers(e, gost_ciphers)) {
97
0
        printf("ENGINE_set_ciphers failed\n");
98
0
        goto end;
99
0
    }
100
19
    if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
101
0
        printf("ENGINE_set_pkey_meths failed\n");
102
0
        goto end;
103
0
    }
104
19
    if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
105
0
        printf("ENGINE_set_pkey_asn1_meths failed\n");
106
0
        goto end;
107
0
    }
108
    /* Control function and commands */
109
19
    if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
110
0
        fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
111
0
        goto end;
112
0
    }
113
19
    if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
114
0
        fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
115
0
        goto end;
116
0
    }
117
19
    if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
118
19
        || !ENGINE_set_init_function(e, gost_engine_init)
119
19
        || !ENGINE_set_finish_function(e, gost_engine_finish)) {
120
0
        goto end;
121
0
    }
122
123
19
    if (!register_ameth_gost
124
19
        (NID_id_GostR3410_94, &ameth_GostR3410_94, "GOST94",
125
19
         "GOST R 34.10-94"))
126
0
        goto end;
127
19
    if (!register_ameth_gost
128
19
        (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
129
19
         "GOST R 34.10-2001"))
130
0
        goto end;
131
19
    if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
132
19
                             "GOST-MAC", "GOST 28147-89 MAC"))
133
0
        goto end;
134
135
19
    if (!register_pmeth_gost(NID_id_GostR3410_94, &pmeth_GostR3410_94, 0))
136
0
        goto end;
137
19
    if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
138
0
        goto end;
139
19
    if (!register_pmeth_gost
140
19
        (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
141
0
        goto end;
142
19
    if (!ENGINE_register_ciphers(e)
143
19
        || !ENGINE_register_digests(e)
144
19
        || !ENGINE_register_pkey_meths(e)
145
        /* These two actually should go in LIST_ADD command */
146
19
        || !EVP_add_cipher(&cipher_gost)
147
19
        || !EVP_add_cipher(&cipher_gost_cpacnt)
148
19
        || !EVP_add_digest(&digest_gost)
149
19
        || !EVP_add_digest(&imit_gost_cpa)
150
19
        ) {
151
0
        goto end;
152
0
    }
153
154
19
    ERR_load_GOST_strings();
155
19
    ret = 1;
156
19
 end:
157
19
    return ret;
158
19
}
159
160
#ifndef OPENSSL_NO_DYNAMIC_ENGINE
161
IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
162
    IMPLEMENT_DYNAMIC_CHECK_FN()
163
#endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
164
static int gost_digests(ENGINE *e, const EVP_MD **digest,
165
                        const int **nids, int nid)
166
38
{
167
38
    int ok = 1;
168
38
    if (!digest) {
169
38
        *nids = gost_digest_nids;
170
38
        return 2;
171
38
    }
172
    /*
173
     * printf("Digest no %d requested\n",nid);
174
     */
175
0
    if (nid == NID_id_GostR3411_94) {
176
0
        *digest = &digest_gost;
177
0
    } else if (nid == NID_id_Gost28147_89_MAC) {
178
0
        *digest = &imit_gost_cpa;
179
0
    } else {
180
0
        ok = 0;
181
0
        *digest = NULL;
182
0
    }
183
0
    return ok;
184
38
}
185
186
static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
187
                        const int **nids, int nid)
188
38
{
189
38
    int ok = 1;
190
38
    if (!cipher) {
191
38
        *nids = gost_cipher_nids;
192
38
        return 2;               /* two ciphers are supported */
193
38
    }
194
195
0
    if (nid == NID_id_Gost28147_89) {
196
0
        *cipher = &cipher_gost;
197
0
    } else if (nid == NID_gost89_cnt) {
198
0
        *cipher = &cipher_gost_cpacnt;
199
0
    } else {
200
0
        ok = 0;
201
0
        *cipher = NULL;
202
0
    }
203
0
    return ok;
204
38
}
205
206
static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
207
                           const int **nids, int nid)
208
38
{
209
38
    if (!pmeth) {
210
38
        *nids = gost_pkey_meth_nids;
211
38
        return 3;
212
38
    }
213
214
0
    switch (nid) {
215
0
    case NID_id_GostR3410_94:
216
0
        *pmeth = pmeth_GostR3410_94;
217
0
        return 1;
218
0
    case NID_id_GostR3410_2001:
219
0
        *pmeth = pmeth_GostR3410_2001;
220
0
        return 1;
221
0
    case NID_id_Gost28147_89_MAC:
222
0
        *pmeth = pmeth_Gost28147_MAC;
223
0
        return 1;
224
0
    default:;
225
0
    }
226
227
0
    *pmeth = NULL;
228
0
    return 0;
229
0
}
230
231
static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
232
                                const int **nids, int nid)
233
0
{
234
0
    if (!ameth) {
235
0
        *nids = gost_pkey_meth_nids;
236
0
        return 3;
237
0
    }
238
0
    switch (nid) {
239
0
    case NID_id_GostR3410_94:
240
0
        *ameth = ameth_GostR3410_94;
241
0
        return 1;
242
0
    case NID_id_GostR3410_2001:
243
0
        *ameth = ameth_GostR3410_2001;
244
0
        return 1;
245
0
    case NID_id_Gost28147_89_MAC:
246
0
        *ameth = ameth_Gost28147_MAC;
247
0
        return 1;
248
249
0
    default:;
250
0
    }
251
252
0
    *ameth = NULL;
253
0
    return 0;
254
0
}
255
256
#ifdef OPENSSL_NO_DYNAMIC_ENGINE
257
static ENGINE *engine_gost(void)
258
19
{
259
19
    ENGINE *ret = ENGINE_new();
260
19
    if (!ret)
261
0
        return NULL;
262
19
    if (!bind_gost(ret, engine_gost_id)) {
263
0
        ENGINE_free(ret);
264
0
        return NULL;
265
0
    }
266
19
    return ret;
267
19
}
268
269
void ENGINE_load_gost(void)
270
19
{
271
19
    ENGINE *toadd;
272
19
    if (pmeth_GostR3410_94)
273
0
        return;
274
19
    toadd = engine_gost();
275
19
    if (!toadd)
276
0
        return;
277
19
    ENGINE_add(toadd);
278
19
    ENGINE_free(toadd);
279
19
    ERR_clear_error();
280
19
}
281
#endif