Skip to content

Commit 0917c07

Browse files
FozzTexxtschak909
andauthored
Check mbedtls version to determine which functions to use. (#979)
Co-authored-by: Thomas Cherryhomes <[email protected]>
1 parent dcd7e14 commit 0917c07

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

lib/FileSystem/fnFileCache.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <algorithm>
1111

1212
// TODO: Replace MD5 with some simple non-crypto hash function producing 128+ bit hash ...
13+
#include <mbedtls/version.h>
1314
#include <mbedtls/md5.h>
1415

1516
#include "../../include/debug.h"
@@ -61,24 +62,24 @@ static std::string encode_host_path(const char *host, const char *path)
6162
unsigned char md5_result[16];
6263
std::string result;
6364
// host part
64-
#ifdef mbedtls_md5_ret
65+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
6566
int err = mbedtls_md5_ret((const unsigned char *)host, strlen(host), md5_result);
6667
if (err != 0) {
67-
Debug_printf("mbedtls_md5_ret failed with error code %d\n", ret);
68+
Debug_printf("mbedtls_md5_ret failed with error code %d\n", err);
6869
}
69-
#else
70+
#else
7071
mbedtls_md5((const unsigned char *)host, strlen(host), md5_result);
71-
#endif
72+
#endif
7273
result = encode_base32(std::string((char *)md5_result, 5)) + '-';
7374
// path part
74-
#ifdef mbedtls_md5_ret
75-
int err = mbedtls_md5_ret((const unsigned char *)path, strlen(path), md5_result);
75+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
76+
err = mbedtls_md5_ret((const unsigned char *)path, strlen(path), md5_result);
7677
if (err != 0) {
77-
Debug_printf("mbedtls_md5_ret failed with error code %d\n", ret);
78+
Debug_printf("mbedtls_md5_ret failed with error code %d\n", err);
7879
}
79-
#else
80+
#else
8081
mbedtls_md5((const unsigned char *)path, strlen(path), md5_result);
81-
#endif
82+
#endif
8283
result += encode_base32(std::string((char *)md5_result, 15));
8384
return result;
8485
}

lib/encoding/hash.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <iomanip>
33

44
#include "hash.h"
5+
#include <mbedtls/version.h>
56

67
Hash hasher;
78

@@ -108,7 +109,7 @@ void Hash::compute_sha1() {
108109

109110
hash_output.resize(20);
110111

111-
#if defined(mbedtls_sha1_starts_ret) && defined(mbedtls_sha1_update_ret) && defined(mbedtls_sha1_finish_ret)
112+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
112113
int err = 0;
113114

114115
// Use newer API that returns status code
@@ -142,7 +143,7 @@ void Hash::compute_sha256() {
142143

143144
hash_output.resize(32);
144145

145-
#if defined(mbedtls_sha256_starts_ret) && defined(mbedtls_sha256_update_ret) && defined(mbedtls_sha256_finish_ret)
146+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
146147
int err = 0;
147148

148149
// Use newer API that returns status code
@@ -176,7 +177,7 @@ void Hash::compute_sha512() {
176177

177178
hash_output.resize(64);
178179

179-
#if defined(mbedtls_sha512_starts_ret) && defined(mbedtls_sha512_update_ret) && defined(mbedtls_sha512_finish_ret)
180+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
180181
int err = 0;
181182

182183
// Use newer API that returns status code

lib/utils/string_utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cmath>
2424
#include <sstream>
2525
#include <iomanip>
26+
#include <mbedtls/version.h>
2627
#include <mbedtls/sha1.h>
2728
#include <mbedtls/base64.h>
2829

@@ -555,7 +556,7 @@ namespace mstr {
555556
{
556557
unsigned char hash[21] = { 0x00 };
557558

558-
#if defined(mbedtls_sha1_ret)
559+
#if MBEDTLS_VERSION_NUMBER >= 0x02070000 && MBEDTLS_VERSION_NUMBER < 0x03000000
559560
// Use the newer mbedtls API
560561
int ret = mbedtls_sha1_ret((const unsigned char *)s.c_str(), s.length(), hash);
561562
if (ret != 0) {

0 commit comments

Comments
 (0)