-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathgost_eng_cipher.c
More file actions
233 lines (202 loc) · 7.69 KB
/
gost_eng_cipher.c
File metadata and controls
233 lines (202 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <string.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "gost_lcl.h"
#include "gost_eng_cipher.h"
struct gost_eng_cipher_st {
GOST_cipher *cipher;
EVP_CIPHER *evp_cipher;
};
/* Engine backend helpers */
int GOST_cipher_init_evp(const GOST_cipher *cipher, EVP_CIPHER_CTX *ctx,
const unsigned char *key, const unsigned char *iv,
int enc);
int GOST_cipher_do_cipher_evp(const GOST_cipher *cipher, EVP_CIPHER_CTX *ctx,
unsigned char *out, const unsigned char *in,
size_t inl);
int GOST_cipher_cleanup_evp(const GOST_cipher *cipher, EVP_CIPHER_CTX *ctx);
int GOST_cipher_ctrl_evp(const GOST_cipher *cipher, EVP_CIPHER_CTX *ctx,
int type, int arg, void *ptr);
int GOST_cipher_set_asn1_parameters_evp(const GOST_cipher *cipher,
EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params);
int GOST_cipher_get_asn1_parameters_evp(const GOST_cipher *cipher,
EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params);
int gost_engine_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc);
int gost_engine_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl);
int gost_engine_cipher_cleanup(EVP_CIPHER_CTX *ctx);
int gost_engine_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
int gost_engine_cipher_set_asn1_parameters(EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params);
int gost_engine_cipher_get_asn1_parameters(EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params);
static EVP_CIPHER *GOST_init_cipher(GOST_cipher *c)
{
/* Some sanity checking. */
int flags = GOST_cipher_flags(c) | EVP_CIPH_CUSTOM_COPY;
int block_size = GOST_cipher_block_size(c);
switch (flags & EVP_CIPH_MODE) {
case EVP_CIPH_CBC_MODE:
case EVP_CIPH_ECB_MODE:
case EVP_CIPH_WRAP_MODE:
OPENSSL_assert(block_size != 1);
OPENSSL_assert(!(flags & EVP_CIPH_NO_PADDING));
break;
default:
OPENSSL_assert(block_size == 1);
OPENSSL_assert(flags & EVP_CIPH_NO_PADDING);
}
if (GOST_cipher_iv_length(c) != 0)
OPENSSL_assert(flags & EVP_CIPH_CUSTOM_IV);
else
OPENSSL_assert(!(flags & EVP_CIPH_CUSTOM_IV));
EVP_CIPHER *cipher = NULL;
if (!(cipher = EVP_CIPHER_meth_new(GOST_cipher_nid(c), block_size,
GOST_cipher_key_length(c)))
|| !EVP_CIPHER_meth_set_iv_length(cipher, GOST_cipher_iv_length(c))
|| !EVP_CIPHER_meth_set_flags(cipher, flags)
|| !EVP_CIPHER_meth_set_init(cipher, gost_engine_cipher_init)
|| !EVP_CIPHER_meth_set_do_cipher(cipher, gost_engine_cipher_do_cipher)
|| !EVP_CIPHER_meth_set_cleanup(cipher, gost_engine_cipher_cleanup)
|| !EVP_CIPHER_meth_set_impl_ctx_size(cipher, GOST_cipher_ctx_size(c))
|| !EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_engine_cipher_set_asn1_parameters)
|| !EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_engine_cipher_get_asn1_parameters)
|| !EVP_CIPHER_meth_set_ctrl(cipher, gost_engine_cipher_ctrl)) {
EVP_CIPHER_meth_free(cipher);
cipher = NULL;
}
return cipher;
}
/* Wrapper functions to expose GOST_cipher descriptors as EVP_CIPHER objects
* cached in GOST_eng_cipher structures. */
EVP_CIPHER *GOST_eng_cipher_init(GOST_eng_cipher *c)
{
if (c->evp_cipher)
return c->evp_cipher;
EVP_CIPHER *m = GOST_init_cipher(c->cipher);
c->evp_cipher = m;
return m;
}
void GOST_eng_cipher_deinit(GOST_eng_cipher *c)
{
EVP_CIPHER_meth_free(c->evp_cipher);
c->evp_cipher = NULL;
}
int GOST_eng_cipher_nid(const GOST_eng_cipher *c)
{
return GOST_cipher_nid(c->cipher);
}
static const GOST_cipher *gost_cipher_from_nid(int nid)
{
GOST_cipher *list[] = {
&Gost28147_89_cipher,
&Gost28147_89_cbc_cipher,
&Gost28147_89_cnt_cipher,
&Gost28147_89_cnt_12_cipher,
&magma_ctr_cipher,
&magma_ctr_acpkm_cipher,
&magma_ctr_acpkm_omac_cipher,
&magma_ecb_cipher,
&magma_cbc_cipher,
&magma_mgm_cipher,
&grasshopper_ecb_cipher,
&grasshopper_cbc_cipher,
&grasshopper_cfb_cipher,
&grasshopper_ofb_cipher,
&grasshopper_ctr_cipher,
&grasshopper_mgm_cipher,
&grasshopper_ctr_acpkm_cipher,
&grasshopper_ctr_acpkm_omac_cipher,
&magma_kexp15_cipher,
&kuznyechik_kexp15_cipher
};
size_t i;
for (i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
if (GOST_cipher_nid(list[i]) == nid)
return list[i];
}
return NULL;
}
static const GOST_cipher *gost_engine_cipher_desc(EVP_CIPHER_CTX *ctx)
{
const EVP_CIPHER *cipher;
if (ctx == NULL)
return NULL;
cipher = EVP_CIPHER_CTX_cipher(ctx);
if (cipher == NULL)
return NULL;
return gost_cipher_from_nid(EVP_CIPHER_nid(cipher));
}
int gost_engine_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
const unsigned char *iv, int enc)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_init_evp(cipher, ctx, key, iv, enc);
}
int gost_engine_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t inl)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_do_cipher_evp(cipher, ctx, out, in, inl);
}
int gost_engine_cipher_cleanup(EVP_CIPHER_CTX *ctx)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_cleanup_evp(cipher, ctx);
}
int gost_engine_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_ctrl_evp(cipher, ctx, type, arg, ptr);
}
int gost_engine_cipher_set_asn1_parameters(EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_set_asn1_parameters_evp(cipher, ctx, params);
}
int gost_engine_cipher_get_asn1_parameters(EVP_CIPHER_CTX *ctx,
ASN1_TYPE *params)
{
const GOST_cipher *cipher = gost_engine_cipher_desc(ctx);
if (cipher == NULL)
return 0;
return GOST_cipher_get_asn1_parameters_evp(cipher, ctx, params);
}
/* Define engine-exposed instances for all GOST ciphers */
#define DEF_CIPHER(name) \
GOST_eng_cipher ENG_CIPHER_NAME(name) = { &name, NULL }
DEF_CIPHER(Gost28147_89_cipher);
DEF_CIPHER(Gost28147_89_cbc_cipher);
DEF_CIPHER(Gost28147_89_cnt_cipher);
DEF_CIPHER(Gost28147_89_cnt_12_cipher);
DEF_CIPHER(magma_ctr_cipher);
DEF_CIPHER(magma_ctr_acpkm_cipher);
DEF_CIPHER(magma_ctr_acpkm_omac_cipher);
DEF_CIPHER(magma_ecb_cipher);
DEF_CIPHER(magma_cbc_cipher);
DEF_CIPHER(magma_mgm_cipher);
DEF_CIPHER(grasshopper_ecb_cipher);
DEF_CIPHER(grasshopper_cbc_cipher);
DEF_CIPHER(grasshopper_cfb_cipher);
DEF_CIPHER(grasshopper_ofb_cipher);
DEF_CIPHER(grasshopper_ctr_cipher);
DEF_CIPHER(grasshopper_mgm_cipher);
DEF_CIPHER(grasshopper_ctr_acpkm_cipher);
DEF_CIPHER(grasshopper_ctr_acpkm_omac_cipher);
DEF_CIPHER(magma_kexp15_cipher);
DEF_CIPHER(kuznyechik_kexp15_cipher);