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.
|
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>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2021-10-24 18:19:57 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
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";
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:23:44 +00:00
|
|
|
void log_ccrypto_error(CCCryptorStatus error_code, char const* file, long line)
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:23:44 +00:00
|
|
|
bool check_ccrypto_result(CCCryptorStatus result, char const* file, long line)
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
// --- sha1
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1::tr_sha1()
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
clear();
|
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1::~tr_sha1()
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha1::clear()
|
|
|
|
{
|
|
|
|
CC_SHA1_Init(&handle_);
|
|
|
|
}
|
2021-10-24 18:19:57 +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)
|
2022-07-31 20:58:14 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
return;
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
CC_SHA1_Update(&handle_, data, data_length);
|
|
|
|
}
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha1_digest_t tr_sha1::finish()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
// --- sha256
|
2021-10-24 18:19:57 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
tr_sha256::tr_sha256()
|
2022-07-01 14:49:33 +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()
|
|
|
|
{
|
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
void tr_sha256::clear()
|
|
|
|
{
|
|
|
|
CC_SHA256_Init(&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-31 20:58:14 +00:00
|
|
|
{
|
2024-03-04 22:59:51 +00:00
|
|
|
return;
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2024-03-04 22:59:51 +00:00
|
|
|
CC_SHA256_Update(&handle_, data, data_length);
|
2022-07-31 20:58:14 +00:00
|
|
|
}
|
2022-07-01 14:49:33 +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{};
|
|
|
|
CC_SHA256_Final(reinterpret_cast<unsigned char*>(std::data(digest)), &handle_);
|
|
|
|
clear();
|
|
|
|
return digest;
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-07-01 14:49:33 +00:00
|
|
|
|
2022-12-21 15:58:32 +00:00
|
|
|
bool tr_rand_buffer_crypto(void* buffer, size_t length)
|
2021-10-24 18:19:57 +00:00
|
|
|
{
|
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));
|
|
|
|
}
|