From cca3511503d0a23d9ca9e69f4c2747a441293b05 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 14 Jul 2026 09:07:03 +0300 Subject: [PATCH] fix: replace deprecated SHA256 API with EVP, conditional dl linkage for Windows --- tests/test_member_sync.c | 27 +++++++++++-------- tools/chatgui/CMakeLists.txt | 6 ++++- tools/chatgui/transport/member_sync.c | 37 +++++++++++++++------------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/tests/test_member_sync.c b/tests/test_member_sync.c index f7280a89..c6f0eb2e 100644 --- a/tests/test_member_sync.c +++ b/tests/test_member_sync.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #define HASH_SZ 32 #define BUCKETS 32 @@ -39,12 +39,14 @@ static void _create_peers(sqlite3* db, const char* ch_id) { static void _member_hash(uint64_t nid, const uint8_t* x25, const uint8_t* ed, const uint8_t* sig, uint8_t out[HASH_SZ]) { - SHA256_CTX ctx; SHA256_Init(&ctx); - SHA256_Update(&ctx, &nid, 8); - SHA256_Update(&ctx, x25, 32); - SHA256_Update(&ctx, ed, 32); - SHA256_Update(&ctx, sig, 64); - SHA256_Final(out, &ctx); + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); + EVP_DigestUpdate(ctx, &nid, 8); + EVP_DigestUpdate(ctx, x25, 32); + EVP_DigestUpdate(ctx, ed, 32); + EVP_DigestUpdate(ctx, sig, 64); + EVP_DigestFinal_ex(ctx, out, NULL); + EVP_MD_CTX_free(ctx); } static uint64_t _mask(int lvl) { int s=63-lvl*5; return s>=0 ? ~0ULL< #include -#include +#include #include #define MS_ID "member_sync" @@ -89,14 +89,14 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519, const uint8_t* ed25519, const uint8_t* join_sig, const uint8_t* addrs_data, int addr_count, int online, uint8_t hash_out[MS_HASH_SIZE]) { - SHA256_CTX ctx; - SHA256_Init(&ctx); - SHA256_Update(&ctx, &node_id, 8); - SHA256_Update(&ctx, x25519, 32); - SHA256_Update(&ctx, ed25519, 32); - SHA256_Update(&ctx, join_sig, 64); + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); + EVP_DigestUpdate(ctx, &node_id, 8); + EVP_DigestUpdate(ctx, x25519, 32); + EVP_DigestUpdate(ctx, ed25519, 32); + EVP_DigestUpdate(ctx, join_sig, 64); uint8_t ac = (uint8_t)addr_count; - SHA256_Update(&ctx, &ac, 1); + EVP_DigestUpdate(ctx, &ac, 1); if (addr_count > 0 && addrs_data) { struct addr_item items[256]; int n = 0; @@ -111,16 +111,17 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519, } qsort(items, (size_t)n, sizeof(struct addr_item), _addr_cmp); for (int i = 0; i < n; i++) { - SHA256_Update(&ctx, &items[i].family, 1); - SHA256_Update(&ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16)); + EVP_DigestUpdate(ctx, &items[i].family, 1); + EVP_DigestUpdate(ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16)); uint8_t port_be[2] = { (uint8_t)(items[i].port >> 8), (uint8_t)(items[i].port & 0xFF) }; - SHA256_Update(&ctx, port_be, 2); + EVP_DigestUpdate(ctx, port_be, 2); } } uint8_t ol = (uint8_t)(online ? 1 : 0); - SHA256_Update(&ctx, &ol, 1); - SHA256_Final(hash_out, &ctx); + EVP_DigestUpdate(ctx, &ol, 1); + EVP_DigestFinal_ex(ctx, hash_out, NULL); + EVP_MD_CTX_free(ctx); } static void _peers_table(const char* ch_id, char* buf, size_t sz) { @@ -153,8 +154,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id, sqlite3_stmt* stmt = NULL; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1; - SHA256_CTX ctx; - SHA256_Init(&ctx); + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); int count = 0; while (sqlite3_step(stmt) == SQLITE_ROW) { @@ -167,12 +168,13 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id, uint8_t mh[MS_HASH_SIZE]; _compute_member_hash(nid, x25, ed, sig, NULL, 0, online, mh); - SHA256_Update(&ctx, mh, MS_HASH_SIZE); + EVP_DigestUpdate(ctx, mh, MS_HASH_SIZE); count++; } sqlite3_finalize(stmt); if (count == 0) { + EVP_MD_CTX_free(ctx); snprintf(sql, sizeof(sql), "DELETE FROM member_tree_hash WHERE channel_id=? AND level=? AND prefix64=?"); sqlite3_stmt* ds = NULL; @@ -186,7 +188,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id, } uint8_t hash[MS_HASH_SIZE]; - SHA256_Final(hash, &ctx); + EVP_DigestFinal_ex(ctx, hash, NULL); + EVP_MD_CTX_free(ctx); snprintf(sql, sizeof(sql), "INSERT OR REPLACE INTO member_tree_hash(channel_id, level, prefix64, hash, member_count)"