xref: /netbsd-src/external/mpl/bind/dist/lib/isc/openssl_shim.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /*	$NetBSD: openssl_shim.c,v 1.3 2020/05/24 19:46:26 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #include "openssl_shim.h"
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include <openssl/crypto.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include <openssl/hmac.h>
22 #include <openssl/opensslv.h>
23 
24 #if !HAVE_CRYPTO_ZALLOC
25 void *
26 CRYPTO_zalloc(size_t size) {
27 	void *ret = OPENSSL_malloc(size);
28 	if (ret != NULL) {
29 		memset(ret, 0, size);
30 	}
31 	return (ret);
32 }
33 #endif /* if !HAVE_CRYPTO_ZALLOC */
34 
35 #if !HAVE_EVP_CIPHER_CTX_NEW
36 EVP_CIPHER_CTX *
37 EVP_CIPHER_CTX_new(void) {
38 	EVP_CIPHER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
39 	return (ctx);
40 }
41 #endif /* if !HAVE_EVP_CIPHER_CTX_NEW */
42 
43 #if !HAVE_EVP_CIPHER_CTX_FREE
44 void
45 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) {
46 	if (ctx != NULL) {
47 		EVP_CIPHER_CTX_cleanup(ctx);
48 		OPENSSL_free(ctx);
49 	}
50 }
51 #endif /* if !HAVE_EVP_CIPHER_CTX_FREE */
52 
53 #if !HAVE_EVP_MD_CTX_NEW
54 EVP_MD_CTX *
55 EVP_MD_CTX_new(void) {
56 	EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
57 	if (ctx != NULL) {
58 		memset(ctx, 0, sizeof(*ctx));
59 	}
60 	return (ctx);
61 }
62 #endif /* if !HAVE_EVP_MD_CTX_NEW */
63 
64 #if !HAVE_EVP_MD_CTX_FREE
65 void
66 EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
67 	if (ctx != NULL) {
68 		EVP_MD_CTX_cleanup(ctx);
69 		OPENSSL_free(ctx);
70 	}
71 }
72 #endif /* if !HAVE_EVP_MD_CTX_FREE */
73 
74 #if !HAVE_EVP_MD_CTX_RESET
75 int
76 EVP_MD_CTX_reset(EVP_MD_CTX *ctx) {
77 	return (EVP_MD_CTX_cleanup(ctx));
78 }
79 #endif /* if !HAVE_EVP_MD_CTX_RESET */
80 
81 #if !HAVE_HMAC_CTX_NEW
82 HMAC_CTX *
83 HMAC_CTX_new(void) {
84 	HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
85 	if (ctx != NULL) {
86 		if (!HMAC_CTX_reset(ctx)) {
87 			HMAC_CTX_free(ctx);
88 			return (NULL);
89 		}
90 	}
91 	return (ctx);
92 }
93 #endif /* if !HAVE_HMAC_CTX_NEW */
94 
95 #if !HAVE_HMAC_CTX_FREE
96 void
97 HMAC_CTX_free(HMAC_CTX *ctx) {
98 	if (ctx != NULL) {
99 		HMAC_CTX_cleanup(ctx);
100 		OPENSSL_free(ctx);
101 	}
102 }
103 #endif /* if !HAVE_HMAC_CTX_FREE */
104 
105 #if !HAVE_HMAC_CTX_RESET
106 int
107 HMAC_CTX_reset(HMAC_CTX *ctx) {
108 	HMAC_CTX_cleanup(ctx);
109 	return (1);
110 }
111 #endif /* if !HAVE_HMAC_CTX_RESET */
112 
113 #if !HAVE_HMAC_CTX_GET_MD
114 const EVP_MD *
115 HMAC_CTX_get_md(const HMAC_CTX *ctx) {
116 	return (ctx->md);
117 }
118 #endif /* if !HAVE_HMAC_CTX_GET_MD */
119