Skip to content

Commit 3e440ea

Browse files
Eric Wonggitster
authored andcommitted
sha256: avoid functions deprecated in OpenSSL 3+
OpenSSL 3+ deprecates the SHA256_Init, SHA256_Update, and SHA256_Final functions, leading to errors when building with `DEVELOPER=1'. Use the newer EVP_* API with OpenSSL 3+ despite being more error-prone and less efficient due to heap allocations. Signed-off-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit 3e440ea

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,9 @@ $(SP_OBJ): %.sp: %.c %.o
32153215
sparse: $(SP_OBJ)
32163216

32173217
EXCEPT_HDRS := $(GENERATED_H) unicode-width.h compat/% xdiff/%
3218+
ifndef OPENSSL_SHA256
3219+
EXCEPT_HDRS += sha256/openssl.h
3220+
endif
32183221
ifndef NETTLE_SHA256
32193222
EXCEPT_HDRS += sha256/nettle.h
32203223
endif

hash-ll.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
#define SHA256_NEEDS_CLONE_HELPER
1818
#include "sha256/gcrypt.h"
1919
#elif defined(SHA256_OPENSSL)
20-
#include <openssl/sha.h>
20+
# include <openssl/sha.h>
21+
# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
22+
# define SHA256_NEEDS_CLONE_HELPER
23+
# include "sha256/openssl.h"
24+
# endif
2125
#else
2226
#include "sha256/block/sha256.h"
2327
#endif

sha256/openssl.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* wrappers for the EVP API of OpenSSL 3+ */
2+
#ifndef SHA256_OPENSSL_H
3+
#define SHA256_OPENSSL_H
4+
#include <openssl/evp.h>
5+
6+
struct openssl_SHA256_CTX {
7+
EVP_MD_CTX *ectx;
8+
};
9+
10+
typedef struct openssl_SHA256_CTX openssl_SHA256_CTX;
11+
12+
static inline void openssl_SHA256_Init(struct openssl_SHA256_CTX *ctx)
13+
{
14+
const EVP_MD *type = EVP_sha256();
15+
16+
ctx->ectx = EVP_MD_CTX_new();
17+
if (!ctx->ectx)
18+
die("EVP_MD_CTX_new: out of memory");
19+
20+
EVP_DigestInit_ex(ctx->ectx, type, NULL);
21+
}
22+
23+
static inline void openssl_SHA256_Update(struct openssl_SHA256_CTX *ctx,
24+
const void *data,
25+
size_t len)
26+
{
27+
EVP_DigestUpdate(ctx->ectx, data, len);
28+
}
29+
30+
static inline void openssl_SHA256_Final(unsigned char *digest,
31+
struct openssl_SHA256_CTX *ctx)
32+
{
33+
EVP_DigestFinal_ex(ctx->ectx, digest, NULL);
34+
EVP_MD_CTX_free(ctx->ectx);
35+
}
36+
37+
static inline void openssl_SHA256_Clone(struct openssl_SHA256_CTX *dst,
38+
const struct openssl_SHA256_CTX *src)
39+
{
40+
EVP_MD_CTX_copy_ex(dst->ectx, src->ectx);
41+
}
42+
43+
#define platform_SHA256_CTX openssl_SHA256_CTX
44+
#define platform_SHA256_Init openssl_SHA256_Init
45+
#define platform_SHA256_Clone openssl_SHA256_Clone
46+
#define platform_SHA256_Update openssl_SHA256_Update
47+
#define platform_SHA256_Final openssl_SHA256_Final
48+
49+
#endif /* SHA256_OPENSSL_H */

0 commit comments

Comments
 (0)