2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2021-2022 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.
|
2021-10-24 18:19:57 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include <CommonCrypto/CommonDigest.h>
|
|
|
|
#include <CommonCrypto/CommonRandom.h>
|
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2021-10-24 18:19:57 +00:00
|
|
|
#include "transmission.h"
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2021-10-24 18:19:57 +00:00
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "tr-assert.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define TR_CRYPTO_X509_FALLBACK
|
2022-02-07 04:28:36 +00:00
|
|
|
#include "crypto-utils-fallback.cc" // NOLINT(bugprone-suspicious-include)
|
2021-10-24 18:19:57 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
char const* ccrypto_error_to_str(CCCryptorStatus error_code)
|
|
|
|
{
|
|
|
|
switch (error_code)
|
|
|
|
{
|
|
|
|
case kCCSuccess:
|
|
|
|
return "Operation completed normally";
|
|
|
|
|
|
|
|
case kCCParamError:
|
|
|
|
return "Illegal parameter value";
|
|
|
|
|
|
|
|
case kCCBufferTooSmall:
|
2022-04-21 14:28:38 +00:00
|
|
|
return "Insufficient buffer provided for specified operation";
|
2021-10-24 18:19:57 +00:00
|
|
|
|
|
|
|
case kCCMemoryFailure:
|
|
|
|
return "Memory allocation failure";
|
|
|
|
|
|
|
|
case kCCAlignmentError:
|
|
|
|
return "Input size was not aligned properly";
|
|
|
|
|
|
|
|
case kCCDecodeError:
|
|
|
|
return "Input data did not decode or decrypt properly";
|
|
|
|
|
|
|
|
case kCCUnimplemented:
|
|
|
|
return "Function not implemented for the current algorithm";
|
|
|
|
|
|
|
|
case kCCOverflow:
|
|
|
|
return "Buffer overflow";
|
|
|
|
|
|
|
|
case kCCRNGFailure:
|
|
|
|
return "Random number generator failure";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown error";
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_ccrypto_error(CCCryptorStatus error_code, char const* file, int line)
|
|
|
|
{
|
|
|
|
if (tr_logLevelIsActive(TR_LOG_ERROR))
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddMessage(
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
TR_LOG_ERROR,
|
|
|
|
fmt::format(
|
|
|
|
_("{crypto_library} error: {error} ({error_code})"),
|
|
|
|
fmt::arg("crypto_library", "CCrypto"),
|
|
|
|
fmt::arg("error", ccrypto_error_to_str(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2021-10-24 18:19:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_ccrypto_result(CCCryptorStatus result, char const* file, int line)
|
|
|
|
{
|
|
|
|
bool const ret = result == kCCSuccess;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
log_ccrypto_error(result, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define check_result(result) check_ccrypto_result((result), __FILE__, __LINE__)
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
namespace
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class Sha1Impl final : public tr_sha1
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
|
|
|
Sha1Impl()
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
clear();
|
2021-10-24 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
~Sha1Impl() override = default;
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void clear() override
|
|
|
|
{
|
|
|
|
CC_SHA1_Init(&handle_);
|
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void add(void const* data, size_t data_length) override
|
|
|
|
{
|
2022-11-21 16:19:45 +00:00
|
|
|
static auto constexpr Max = static_cast<size_t>(std::numeric_limits<CC_LONG>::max());
|
|
|
|
auto const* sha_data = static_cast<uint8_t const*>(data);
|
|
|
|
while (data_length > 0)
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
2022-11-21 16:19:45 +00:00
|
|
|
auto const n_bytes = static_cast<CC_LONG>(std::min(data_length, Max));
|
|
|
|
CC_SHA1_Update(&handle_, sha_data, n_bytes);
|
|
|
|
data_length -= n_bytes;
|
|
|
|
sha_data += n_bytes;
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] tr_sha1_digest_t finish() override
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
auto digest = tr_sha1_digest_t{};
|
|
|
|
CC_SHA1_Final(reinterpret_cast<unsigned char*>(std::data(digest)), &handle_);
|
|
|
|
clear();
|
|
|
|
return digest;
|
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
CC_SHA1_CTX handle_ = {};
|
|
|
|
};
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
class Sha256Impl final : public tr_sha256
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
public:
|
|
|
|
Sha256Impl()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
~Sha256Impl() override = default;
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void clear() override
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-07-31 20:58:14 +00:00
|
|
|
CC_SHA256_Init(&handle_);
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
void add(void const* data, size_t data_length) override
|
|
|
|
{
|
2022-11-21 16:19:45 +00:00
|
|
|
static auto constexpr Max = static_cast<size_t>(std::numeric_limits<CC_LONG>::max());
|
|
|
|
auto const* sha_data = static_cast<uint8_t const*>(data);
|
|
|
|
while (data_length > 0)
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
2022-11-21 16:19:45 +00:00
|
|
|
auto const n_bytes = static_cast<CC_LONG>(std::min(data_length, Max));
|
|
|
|
CC_SHA256_Update(&handle_, sha_data, n_bytes);
|
|
|
|
data_length -= n_bytes;
|
|
|
|
sha_data += n_bytes;
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-08-20 20:57:32 +00:00
|
|
|
[[nodiscard]] tr_sha256_digest_t finish() override
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
|
|
|
auto digest = tr_sha256_digest_t{};
|
|
|
|
CC_SHA256_Final(reinterpret_cast<unsigned char*>(std::data(digest)), &handle_);
|
|
|
|
clear();
|
|
|
|
return digest;
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
private:
|
|
|
|
CC_SHA256_CTX handle_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::unique_ptr<tr_sha1> tr_sha1::create()
|
|
|
|
{
|
|
|
|
return std::make_unique<Sha1Impl>();
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::unique_ptr<tr_sha256> tr_sha256::create()
|
|
|
|
{
|
|
|
|
return std::make_unique<Sha256Impl>();
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-24 18:19:57 +00:00
|
|
|
bool tr_rand_buffer(void* buffer, size_t length)
|
|
|
|
{
|
2021-11-19 18:37:38 +00:00
|
|
|
if (length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:19:57 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
|
|
|
|
|
|
|
return check_result(CCRandomGenerateBytes(buffer, length));
|
|
|
|
}
|