]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - compat.h
Make it possible to re-define OPENSSL_ENGINES_INSTALL_DIR
[openssl-gost/engine.git] / compat.h
1 /*
2  * Shim to provide small subset of openssl-1.1.0 API by openssl-1.0.2
3  *
4  * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
5  *
6  * Contents licensed under the terms of the OpenSSL license
7  * See https://www.openssl.org/source/license.html for details
8  */
9
10 #ifndef _GOST_COMPAT_H
11 #define _GOST_COMPAT_H
12
13 # include <openssl/opensslv.h>
14 # if (OPENSSL_VERSION_NUMBER <= 0x10002100L)
15
16 # include <string.h>
17
18 # include <openssl/crypto.h>
19 # include <openssl/dsa.h>
20 # include <openssl/evp.h>
21
22 /*
23  * for crypto.h
24  */
25
26 # define OPENSSL_zalloc(num)     CRYPTO_zalloc(num, __FILE__, __LINE__)
27 # define OPENSSL_clear_free(addr, num) \
28     CRYPTO_clear_free(addr, num, __FILE__, __LINE__)
29
30 static inline void *CRYPTO_zalloc(size_t num, const char *file, int line)
31 {
32     void *ret = CRYPTO_malloc(num, file, line);
33
34     if (ret != NULL)
35         memset(ret, 0, num);
36     return ret;
37 }
38
39 static inline void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
40 {
41     if (str == NULL)
42         return;
43     if (num)
44         OPENSSL_cleanse(str, num);
45     CRYPTO_free(str);
46 }
47
48 /*
49  * for dsa.h
50  */
51
52 static inline void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
53 {
54     if (pr != NULL)
55         *pr = sig->r;
56     if (ps != NULL)
57         *ps = sig->s;
58 }
59
60 static inline int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
61 {
62     if (r == NULL || s == NULL)
63         return 0;
64     BN_clear_free(sig->r);
65     BN_clear_free(sig->s);
66     sig->r = r;
67     sig->s = s;
68     return 1;
69 }
70
71 /*
72  * for evp.h
73  */
74
75 #ifndef OPENSSL_FILE
76 # ifdef OPENSSL_NO_FILENAMES
77 #  define OPENSSL_FILE ""
78 #  define OPENSSL_LINE 0
79 # else
80 #  define OPENSSL_FILE __FILE__
81 #  define OPENSSL_LINE __LINE__
82 # endif
83 #endif
84
85 static inline void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
86 {
87     return ctx->cipher_data;
88 }
89
90 static inline const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
91 {
92     return ctx->oiv;
93 }
94
95 static inline unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
96 {
97     return ctx->iv;
98 }
99
100 static inline int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
101 {
102     return ctx->encrypt;
103 }
104
105 static inline unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
106 {
107     return ctx->buf;
108 }
109
110 static inline int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
111 {
112     return ctx->num;
113 }
114
115 static inline void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
116 {
117     ctx->num = num;
118 }
119
120 static inline EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
121 {
122     EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
123
124     if (cipher != NULL) {
125         cipher->nid = cipher_type;
126         cipher->block_size = block_size;
127         cipher->key_len = key_len;
128     }
129     return cipher;
130 }
131
132 static inline int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
133 {
134     cipher->iv_len = iv_len;
135     return 1;
136 }
137
138 static inline int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
139 {
140     cipher->flags = flags;
141     return 1;
142 }
143
144 static inline int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
145     int (*cleanup) (EVP_CIPHER_CTX *))
146 {
147     cipher->cleanup = cleanup;
148     return 1;
149 }
150
151 static inline int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
152     int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
153         ASN1_TYPE *))
154 {
155     cipher->set_asn1_parameters = set_asn1_parameters;
156     return 1;
157 }
158
159 static inline int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
160     int (*ctrl) (EVP_CIPHER_CTX *, int type,
161         int arg, void *ptr))
162 {
163     cipher->ctrl = ctrl;
164     return 1;
165 }
166
167 static inline int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
168     int (*do_cipher) (EVP_CIPHER_CTX *ctx,
169         unsigned char *out,
170         const unsigned char *in,
171         size_t inl))
172 {
173     cipher->do_cipher = do_cipher;
174     return 1;
175 }
176
177 static inline int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
178     int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
179         ASN1_TYPE *))
180 {
181     cipher->get_asn1_parameters = get_asn1_parameters;
182     return 1;
183 }
184
185 static inline int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
186     int (*init) (EVP_CIPHER_CTX *ctx,
187         const unsigned char *key,
188         const unsigned char *iv,
189         int enc))
190 {
191     cipher->init = init;
192     return 1;
193 }
194
195 static inline int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
196 {
197     cipher->ctx_size = ctx_size;
198     return 1;
199 }
200
201 static inline void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
202 {
203     OPENSSL_free(cipher);
204 }
205
206 static inline EVP_MD_CTX *EVP_MD_CTX_new(void)
207 {
208     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
209 }
210
211 int ENGINE_finish(ENGINE *e);
212 static inline int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
213 {
214     if (ctx == NULL)
215         return 1;
216     if (ctx->digest && ctx->digest->cleanup
217         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
218         ctx->digest->cleanup(ctx);
219     if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
220         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
221         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
222     }
223     EVP_PKEY_CTX_free(ctx->pctx);
224 #ifndef OPENSSL_NO_ENGINE
225     ENGINE_finish(ctx->engine);
226 #endif
227     OPENSSL_cleanse(ctx, sizeof(*ctx));
228
229     return 1;
230 }
231
232 static inline void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
233 {
234     EVP_MD_CTX_reset(ctx);
235     OPENSSL_free(ctx);
236 }
237
238 static inline EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
239 {
240     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
241
242     if (md != NULL) {
243         md->type = md_type;
244         md->pkey_type = pkey_type;
245     }
246     return md;
247 }
248
249 static inline int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
250 {
251     md->md_size = resultsize;
252     return 1;
253 }
254
255 static int EVP_MD_meth_get_result_size(const EVP_MD *md)
256 {
257     return md->md_size;
258 }
259
260 static inline int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
261 {
262     md->block_size = blocksize;
263     return 1;
264 }
265
266 static inline int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
267 {
268     md->ctx_size = datasize;
269     return 1;
270 }
271
272 static inline int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
273 {
274     md->flags = flags;
275     return 1;
276 }
277
278 static inline int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
279 {
280     md->init = init;
281     return 1;
282 }
283
284 static inline int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
285         const void *data,
286         size_t count))
287 {
288     md->update = update;
289     return 1;
290 }
291
292 static inline int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
293         unsigned char *md))
294 {
295     md->final = final;
296     return 1;
297 }
298
299 static inline int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
300         const EVP_MD_CTX *from))
301 {
302     md->copy = copy;
303     return 1;
304 }
305
306 static inline int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
307 {
308     md->cleanup = cleanup;
309     return 1;
310 }
311
312 static inline int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
313         int p1, void *p2))
314 {
315     md->md_ctrl = ctrl;
316     return 1;
317 }
318
319 static inline void EVP_MD_meth_free(EVP_MD *md)
320 {
321     OPENSSL_free(md);
322 }
323
324 static inline const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
325 {
326     return ctx->iv;
327 }
328
329 static inline void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
330 {
331     return ctx->md_data;
332 }
333
334 static inline int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
335 {
336     return md->init;
337 }
338
339 static inline int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
340     int p1, void *p2)
341 {
342     return md->md_ctrl;
343 }
344
345 # endif /* (OPENSSL_VERSION_NUMBER <= 0x10002100L) */
346
347 # ifndef NID_id_tc26_cipher_gostr3412_2015_kuznyechik
348 #  define NID_id_tc26_cipher_gostr3412_2015_kuznyechik                  1176
349 #  define NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm         1177
350 #  define NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac    1178
351 #  define NID_magma_ecb                                                 1187
352 #  define NID_magma_ctr                                                 1188
353 #  define NID_magma_ofb                                                 1189
354 #  define NID_magma_cbc                                                 1190
355 #  define NID_magma_cfb                                                 1191
356 #  define NID_magma_mac                                                 1192
357 # endif
358
359 #endif /* !_GOST_COMPAT_H */