2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#include <wolfssl/options.h>
|
|
|
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
|
|
|
#include <wolfssl/wolfcrypt/random.h>
|
|
|
|
#include <wolfssl/wolfcrypt/sha.h>
|
|
|
|
#include <wolfssl/wolfcrypt/sha256.h>
|
|
|
|
#include <wolfssl/version.h>
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
#ifndef WITH_WOLFSSL
|
|
|
|
#error wolfssl module
|
|
|
|
#endif
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
#if LIBWOLFSSL_VERSION_HEX >= 0x04000000 // 4.0.0
|
|
|
|
using TR_WC_RNG = WC_RNG;
|
|
|
|
#else
|
|
|
|
using TR_WC_RNG = RNG;
|
|
|
|
#endif
|
|
|
|
|
2019-06-22 13:02:17 +00:00
|
|
|
#define TR_CRYPTO_X509_FALLBACK
|
2022-02-07 04:28:36 +00:00
|
|
|
#include "crypto-utils-fallback.cc" // NOLINT(bugprone-suspicious-include)
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-01-05 05:12:51 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void log_wolfssl_error(int error_code, char const* file, int line)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logLevelIsActive(TR_LOG_ERROR))
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddMessage(
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
TR_LOG_ERROR,
|
|
|
|
fmt::format(
|
|
|
|
_("{crypto_library} error: {error} ({error_code})"),
|
2022-12-29 22:38:04 +00:00
|
|
|
fmt::arg("crypto_library", "WolfSSL"),
|
|
|
|
fmt::arg("error", wc_GetErrorString(error_code)),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error_code", error_code)));
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 05:12:51 +00:00
|
|
|
bool check_wolfssl_result(int result, char const* file, int line)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const ret = result == 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
log_wolfssl_error(result, file, line);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 22:38:04 +00:00
|
|
|
#define check_result(result) check_wolfssl_result((result), __FILE__, __LINE__)
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-01-05 05:12:51 +00:00
|
|
|
TR_WC_RNG* get_rng()
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
static TR_WC_RNG rng;
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool rng_initialized = false;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!rng_initialized)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2022-12-29 22:38:04 +00:00
|
|
|
if (!check_result(wc_InitRng(&rng)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rng_initialized = true;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return &rng;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 05:12:51 +00:00
|
|
|
std::mutex rng_mutex_;
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// --- sha1
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1::tr_sha1()
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
clear();
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1::~tr_sha1() = default;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha1::clear()
|
|
|
|
{
|
|
|
|
wc_InitSha(&handle_);
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha1::add(void const* data, size_t data_length)
|
|
|
|
{
|
|
|
|
if (data_length > 0U)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
wc_ShaUpdate(&handle_, static_cast<byte const*>(data), data_length);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2024-03-04 22:59:51 +00:00
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1_digest_t tr_sha1::finish()
|
|
|
|
{
|
|
|
|
auto digest = tr_sha1_digest_t{};
|
|
|
|
wc_ShaFinal(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
|
|
|
clear();
|
|
|
|
return digest;
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
// --- sha256
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha256::tr_sha256()
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
clear();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha256::~tr_sha256() = default;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha256::clear()
|
|
|
|
{
|
|
|
|
wc_InitSha256(&handle_);
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha256::add(void const* data, size_t data_length)
|
|
|
|
{
|
|
|
|
if (data_length > 0U)
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
wc_Sha256Update(&handle_, static_cast<byte const*>(data), data_length);
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha256_digest_t tr_sha256::finish()
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
auto digest = tr_sha256_digest_t{};
|
|
|
|
wc_Sha256Final(&handle_, reinterpret_cast<byte*>(std::data(digest)));
|
|
|
|
clear();
|
|
|
|
return digest;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2022-12-21 15:58:32 +00:00
|
|
|
bool tr_rand_buffer_crypto(void* buffer, size_t length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-11-19 18:37:38 +00:00
|
|
|
if (length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2024-02-18 04:43:24 +00:00
|
|
|
auto const lock = std::lock_guard{ rng_mutex_ };
|
2022-12-29 22:38:04 +00:00
|
|
|
return check_result(wc_RNG_GenerateBlock(get_rng(), static_cast<byte*>(buffer), length));
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|