2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2016-03-29 19:02:26 +00:00
|
|
|
#ifndef TR_CRYPTO_UTILS_H
|
|
|
|
#define TR_CRYPTO_UTILS_H
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2022-07-28 15:01:59 +00:00
|
|
|
#include <array>
|
2021-12-17 04:51:53 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint>
|
2022-12-19 23:31:24 +00:00
|
|
|
#include <limits>
|
2022-07-31 20:58:14 +00:00
|
|
|
#include <memory>
|
2021-11-04 00:55:04 +00:00
|
|
|
#include <optional>
|
2022-12-29 16:30:03 +00:00
|
|
|
#include <random> // for std::uniform_int_distribution<T>
|
2021-11-14 02:03:01 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
#include "transmission.h" // tr_sha1_digest_t
|
2014-12-04 19:58:34 +00:00
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
/**
|
2023-01-22 19:21:30 +00:00
|
|
|
* @addtogroup utils Utilities
|
|
|
|
* @{
|
|
|
|
*/
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class tr_sha1
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
|
|
|
static std::unique_ptr<tr_sha1> create();
|
|
|
|
virtual ~tr_sha1() = default;
|
|
|
|
|
|
|
|
virtual void clear() = 0;
|
|
|
|
virtual void add(void const* data, size_t data_length) = 0;
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] virtual tr_sha1_digest_t finish() = 0;
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
template<typename... T>
|
2022-10-03 00:45:38 +00:00
|
|
|
[[nodiscard]] static tr_sha1_digest_t digest(T const&... args)
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
auto context = tr_sha1::create();
|
|
|
|
(context->add(std::data(args), std::size(args)), ...);
|
2022-08-20 20:57:32 +00:00
|
|
|
return context->finish();
|
2021-12-21 22:14:15 +00:00
|
|
|
}
|
2022-07-31 20:58:14 +00:00
|
|
|
};
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class tr_sha256
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
|
|
|
static std::unique_ptr<tr_sha256> create();
|
|
|
|
virtual ~tr_sha256() = default;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
virtual void clear() = 0;
|
|
|
|
virtual void add(void const* data, size_t data_length) = 0;
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] virtual tr_sha256_digest_t finish() = 0;
|
2022-07-31 20:58:14 +00:00
|
|
|
|
|
|
|
template<typename... T>
|
2022-10-03 00:45:38 +00:00
|
|
|
[[nodiscard]] static tr_sha256_digest_t digest(T const&... args)
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
auto context = tr_sha256::create();
|
|
|
|
(context->add(std::data(args), std::size(args)), ...);
|
2022-08-20 20:57:32 +00:00
|
|
|
return context->finish();
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
2022-07-31 20:58:14 +00:00
|
|
|
};
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
/** @brief Opaque SSL context type. */
|
|
|
|
using tr_ssl_ctx_t = void*;
|
|
|
|
/** @brief Opaque X509 certificate store type. */
|
|
|
|
using tr_x509_store_t = void*;
|
|
|
|
/** @brief Opaque X509 certificate type. */
|
|
|
|
using tr_x509_cert_t = void*;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2019-06-22 13:02:17 +00:00
|
|
|
/**
|
|
|
|
* @brief Get X509 certificate store from SSL context.
|
|
|
|
*/
|
|
|
|
tr_x509_store_t tr_ssl_get_x509_store(tr_ssl_ctx_t handle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add certificate to X509 certificate store.
|
|
|
|
*/
|
|
|
|
bool tr_x509_store_add(tr_x509_store_t handle, tr_x509_cert_t cert);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocate and initialize new X509 certificate from DER-encoded buffer.
|
|
|
|
*/
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_x509_cert_t tr_x509_cert_new(void const* der, size_t der_length);
|
2019-06-22 13:02:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Free X509 certificate returned by @ref tr_x509_cert_new.
|
|
|
|
*/
|
|
|
|
void tr_x509_cert_free(tr_x509_cert_t handle);
|
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
/**
|
|
|
|
* @brief Fill a buffer with random bytes.
|
|
|
|
*/
|
2022-12-21 15:58:32 +00:00
|
|
|
void tr_rand_buffer(void* buffer, size_t length);
|
|
|
|
|
|
|
|
// Client code should use `tr_rand_buffer()`.
|
|
|
|
// These helpers are only exposed here to permit open-box tests.
|
|
|
|
bool tr_rand_buffer_crypto(void* buffer, size_t length);
|
|
|
|
void tr_rand_buffer_std(void* buffer, size_t length);
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2022-11-25 21:04:37 +00:00
|
|
|
template<typename T>
|
|
|
|
T tr_rand_obj()
|
|
|
|
{
|
|
|
|
auto t = T{};
|
|
|
|
tr_rand_buffer(&t, sizeof(T));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:45:18 +00:00
|
|
|
/**
|
|
|
|
* @brief Generate a SSHA password from its plaintext source.
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string tr_ssha1(std::string_view plaintext);
|
2014-12-04 20:45:18 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
/**
|
|
|
|
* @brief Return true if this is salted text, false otherwise
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] bool tr_ssha1_test(std::string_view text);
|
2021-12-21 22:14:15 +00:00
|
|
|
|
2014-12-04 20:45:18 +00:00
|
|
|
/**
|
|
|
|
* @brief Validate a test password against the a ssha1 password.
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] bool tr_ssha1_matches(std::string_view ssha1, std::string_view plaintext);
|
2014-12-04 20:45:18 +00:00
|
|
|
|
2014-12-04 19:58:34 +00:00
|
|
|
/**
|
|
|
|
* @brief Translate null-terminated string into base64.
|
2022-01-08 12:46:25 +00:00
|
|
|
* @return a new std::string with the encoded contents
|
2014-12-04 19:58:34 +00:00
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string tr_base64_encode(std::string_view input);
|
2014-12-04 19:58:34 +00:00
|
|
|
|
2021-11-15 03:54:48 +00:00
|
|
|
/**
|
|
|
|
* @brief Translate a character range from base64 into raw form.
|
|
|
|
* @return a new std::string with the decoded contents.
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string tr_base64_decode(std::string_view input);
|
2021-11-15 03:54:48 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
/**
|
|
|
|
* @brief Generate an ascii hex string for a sha1 digest.
|
2014-12-04 20:53:56 +00:00
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string tr_sha1_to_string(tr_sha1_digest_t const&);
|
2014-12-04 20:53:56 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-24 21:03:30 +00:00
|
|
|
* @brief Generate a sha1 digest from a hex string.
|
2014-12-04 20:53:56 +00:00
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::optional<tr_sha1_digest_t> tr_sha1_from_string(std::string_view hex);
|
2014-12-04 20:53:56 +00:00
|
|
|
|
2022-07-01 14:49:33 +00:00
|
|
|
/**
|
|
|
|
* @brief Generate an ascii hex string for a sha256 digest.
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string tr_sha256_to_string(tr_sha256_digest_t const&);
|
2022-07-01 14:49:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generate a sha256 digest from a hex string.
|
|
|
|
*/
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::optional<tr_sha256_digest_t> tr_sha256_from_string(std::string_view hex);
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-28 15:01:59 +00:00
|
|
|
// Convenience utility to efficiently get many random small values.
|
|
|
|
// Use this instead of making a lot of calls to tr_rand_int().
|
2022-09-09 02:49:51 +00:00
|
|
|
template<typename T = uint8_t, size_t N = 1024U>
|
2022-07-28 15:01:59 +00:00
|
|
|
class tr_salt_shaker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
[[nodiscard]] auto operator()() noexcept
|
|
|
|
{
|
|
|
|
if (pos == std::size(buf))
|
|
|
|
{
|
|
|
|
pos = 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == 0U)
|
|
|
|
{
|
2022-09-09 02:49:51 +00:00
|
|
|
tr_rand_buffer(std::data(buf), std::size(buf) * sizeof(T));
|
2022-07-28 15:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf[pos++];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t pos = 0;
|
2022-09-09 02:49:51 +00:00
|
|
|
std::array<T, N> buf;
|
2022-07-28 15:01:59 +00:00
|
|
|
};
|
|
|
|
|
2022-12-19 23:31:24 +00:00
|
|
|
// UniformRandomBitGenerator impl that uses `tr_rand_buffer()`.
|
|
|
|
// See https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator
|
|
|
|
template<typename T, size_t N = 1024U>
|
|
|
|
class tr_urbg
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using result_type = T;
|
|
|
|
static_assert(!std::numeric_limits<T>::is_signed);
|
|
|
|
|
|
|
|
[[nodiscard]] static constexpr T min() noexcept
|
|
|
|
{
|
|
|
|
return std::numeric_limits<T>::min();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] static constexpr T max() noexcept
|
|
|
|
{
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] T operator()() noexcept
|
|
|
|
{
|
|
|
|
return buf_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_salt_shaker<T, N> buf_;
|
|
|
|
};
|
|
|
|
|
2022-12-29 16:30:03 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns a random number in the range of [0...upper_bound).
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
[[nodiscard]] T tr_rand_int(T upper_bound)
|
|
|
|
{
|
|
|
|
static_assert(!std::is_signed<T>());
|
|
|
|
using dist_type = std::uniform_int_distribution<T>;
|
|
|
|
|
|
|
|
thread_local auto rng = tr_urbg<T>{};
|
|
|
|
thread_local auto dist = dist_type{};
|
|
|
|
return dist(rng, typename dist_type::param_type(0, upper_bound - 1));
|
|
|
|
}
|
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
/** @} */
|
|
|
|
|
2016-03-29 19:02:26 +00:00
|
|
|
#endif /* TR_CRYPTO_UTILS_H */
|