2 * Shim to provide small subset of openssl-1.1.0 API by openssl-1.0.2
4 * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
6 * Contents licensed under the terms of the OpenSSL license
7 * See https://www.openssl.org/source/license.html for details
10 #ifndef _GOST_COMPAT_H
11 #define _GOST_COMPAT_H
13 # include <openssl/opensslv.h>
14 # if (OPENSSL_VERSION_NUMBER <= 0x10002100L)
18 # include <openssl/crypto.h>
19 # include <openssl/dsa.h>
20 # include <openssl/evp.h>
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__)
30 static inline void *CRYPTO_zalloc(size_t num, const char *file, int line)
32 void *ret = CRYPTO_malloc(num, file, line);
39 static inline void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
44 OPENSSL_cleanse(str, num);
52 static inline void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
60 static inline int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
62 if (r == NULL || s == NULL)
64 BN_clear_free(sig->r);
65 BN_clear_free(sig->s);
76 # ifdef OPENSSL_NO_FILENAMES
77 # define OPENSSL_FILE ""
78 # define OPENSSL_LINE 0
80 # define OPENSSL_FILE __FILE__
81 # define OPENSSL_LINE __LINE__
85 static inline void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
87 return ctx->cipher_data;
90 static inline const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
95 static inline unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
100 static inline int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
105 static inline unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
110 static inline int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
115 static inline void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
120 static inline EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
122 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
124 if (cipher != NULL) {
125 cipher->nid = cipher_type;
126 cipher->block_size = block_size;
127 cipher->key_len = key_len;
132 static inline int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
134 cipher->iv_len = iv_len;
138 static inline int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
140 cipher->flags = flags;
144 static inline int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
145 int (*cleanup) (EVP_CIPHER_CTX *))
147 cipher->cleanup = cleanup;
151 static inline int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
152 int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
155 cipher->set_asn1_parameters = set_asn1_parameters;
159 static inline int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
160 int (*ctrl) (EVP_CIPHER_CTX *, int type,
167 static inline int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
168 int (*do_cipher) (EVP_CIPHER_CTX *ctx,
170 const unsigned char *in,
173 cipher->do_cipher = do_cipher;
177 static inline int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
178 int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
181 cipher->get_asn1_parameters = get_asn1_parameters;
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,
195 static inline int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
197 cipher->ctx_size = ctx_size;
201 static inline void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
203 OPENSSL_free(cipher);
206 static inline EVP_MD_CTX *EVP_MD_CTX_new(void)
208 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
211 int ENGINE_finish(ENGINE *e);
212 static inline int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
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);
223 EVP_PKEY_CTX_free(ctx->pctx);
224 #ifndef OPENSSL_NO_ENGINE
225 ENGINE_finish(ctx->engine);
227 OPENSSL_cleanse(ctx, sizeof(*ctx));
232 static inline void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
234 EVP_MD_CTX_reset(ctx);
238 static inline EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
240 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
244 md->pkey_type = pkey_type;
249 static inline int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
251 md->md_size = resultsize;
255 static int EVP_MD_meth_get_result_size(const EVP_MD *md)
260 static inline int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
262 md->block_size = blocksize;
266 static inline int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
268 md->ctx_size = datasize;
272 static inline int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
278 static inline int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
284 static inline int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
292 static inline int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
299 static inline int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
300 const EVP_MD_CTX *from))
306 static inline int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
308 md->cleanup = cleanup;
312 static inline int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
319 static inline void EVP_MD_meth_free(EVP_MD *md)
324 static inline const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
329 static inline void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
334 static inline int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
339 static inline int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
345 # endif /* (OPENSSL_VERSION_NUMBER <= 0x10002100L) */
347 #endif /* !_GOST_COMPAT_H */