2015-01-07 02:04:08 +00:00
|
|
|
/*
|
2015-01-07 13:20:56 +00:00
|
|
|
* This file Copyright (C) 2014-2015 Mnemosyne LLC
|
2015-01-07 02:04:08 +00:00
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(CYASSL_IS_WOLFSSL)
|
|
|
|
#define API_HEADER(x) <wolfssl/x>
|
|
|
|
#define API_HEADER_CRYPT(x) API_HEADER(wolfcrypt/x)
|
2021-08-15 09:41:48 +00:00
|
|
|
#define API(x) wc_##x
|
2017-04-19 12:04:45 +00:00
|
|
|
#define API_VERSION_HEX LIBWOLFSSL_VERSION_HEX
|
2016-10-26 19:12:51 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define API_HEADER(x) <cyassl/x>
|
|
|
|
#define API_HEADER_CRYPT(x) API_HEADER(ctaocrypt/x)
|
|
|
|
#define API(x) x
|
|
|
|
#define API_VERSION_HEX LIBCYASSL_VERSION_HEX
|
2016-10-26 19:12:51 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-14 03:48:04 +00:00
|
|
|
#include API_HEADER(options.h)
|
2017-04-19 12:04:45 +00:00
|
|
|
#include API_HEADER_CRYPT(dh.h)
|
|
|
|
#include API_HEADER_CRYPT(error-crypt.h)
|
|
|
|
#include API_HEADER_CRYPT(random.h)
|
|
|
|
#include API_HEADER_CRYPT(sha.h)
|
|
|
|
#include API_HEADER(version.h)
|
2015-01-07 02:04:08 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "log.h"
|
2015-01-07 13:13:19 +00:00
|
|
|
#include "platform.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2015-01-07 02:04:08 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define TR_CRYPTO_DH_SECRET_FALLBACK
|
2019-06-22 13:02:17 +00:00
|
|
|
#define TR_CRYPTO_X509_FALLBACK
|
2021-09-12 17:41:49 +00:00
|
|
|
#include "crypto-utils-fallback.cc"
|
2015-01-07 02:04:08 +00:00
|
|
|
|
|
|
|
struct tr_dh_ctx
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
DhKey dh;
|
|
|
|
word32 key_length;
|
|
|
|
uint8_t* private_key;
|
|
|
|
word32 private_key_length;
|
2015-01-07 02:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define MY_NAME "tr_crypto_utils"
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void log_cyassl_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
|
|
|
{
|
2016-10-26 19:12:51 +00:00
|
|
|
#if API_VERSION_HEX >= 0x03004000
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* error_message = API(GetErrorString)(error_code);
|
2016-10-26 19:12:51 +00:00
|
|
|
#elif API_VERSION_HEX >= 0x03000002
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* error_message = CTaoCryptGetErrorString(error_code);
|
2015-01-07 02:04:08 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
char error_message[CYASSL_MAX_ERROR_SZ];
|
|
|
|
CTaoCryptErrorString(error_code, error_message);
|
2015-01-07 02:04:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddMessage(file, line, TR_LOG_ERROR, MY_NAME, "CyaSSL error: %s", error_message);
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool check_cyassl_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)
|
|
|
|
{
|
|
|
|
log_cyassl_error(result, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define check_result(result) check_cyassl_result((result), __FILE__, __LINE__)
|
2015-01-07 02:04:08 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static RNG* get_rng(void)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static RNG rng;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!check_result(API(InitRng)(&rng)))
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_lock* get_rng_lock(void)
|
2015-01-07 13:13:19 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
static tr_lock* lock = nullptr;
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (lock == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
lock = tr_lockNew();
|
|
|
|
}
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return lock;
|
2015-01-07 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 02:04:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sha1_ctx_t tr_sha1_init(void)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Sha* handle = tr_new(Sha, 1);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (check_result(API(InitSha)(handle)))
|
|
|
|
{
|
|
|
|
return handle;
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
bool tr_sha1_update(tr_sha1_ctx_t raw_handle, void const* data, size_t data_length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<Sha*>(raw_handle);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (data_length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
return check_result(API(ShaUpdate)(handle, static_cast<byte const*>(data), data_length));
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
bool tr_sha1_final(tr_sha1_ctx_t raw_handle, uint8_t* hash)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<Sha*>(raw_handle);
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = true;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (hash != nullptr)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = check_result(API(ShaFinal)(handle, hash));
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle);
|
|
|
|
return ret;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_dh_ctx_t tr_dh_new(
|
|
|
|
uint8_t const* prime_num,
|
|
|
|
size_t prime_num_length,
|
|
|
|
uint8_t const* generator_num,
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t generator_num_length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(prime_num != nullptr);
|
|
|
|
TR_ASSERT(generator_num != nullptr);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_dh_ctx* handle = tr_new0(struct tr_dh_ctx, 1);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
API(InitDhKey)(&handle->dh);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!check_result(API(DhSetKey)(&handle->dh, prime_num, prime_num_length, generator_num, generator_num_length)))
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handle->key_length = prime_num_length;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return handle;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_dh_free(tr_dh_ctx_t raw_handle)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<struct tr_dh_ctx*>(raw_handle);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handle == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
API(FreeDhKey)(&handle->dh);
|
|
|
|
tr_free(handle->private_key);
|
|
|
|
tr_free(handle);
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
bool tr_dh_make_key(
|
|
|
|
tr_dh_ctx_t raw_handle,
|
|
|
|
[[maybe_unused]] size_t private_key_length,
|
|
|
|
uint8_t* public_key,
|
|
|
|
size_t* public_key_length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(raw_handle != nullptr);
|
|
|
|
TR_ASSERT(public_key != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<struct tr_dh_ctx*>(raw_handle);
|
2017-05-01 15:46:41 +00:00
|
|
|
word32 my_private_key_length;
|
|
|
|
word32 my_public_key_length;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lock* rng_lock = get_rng_lock();
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (handle->private_key == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
handle->private_key = static_cast<uint8_t*>(tr_malloc(handle->key_length));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockLock(rng_lock);
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!check_result(API(DhGenerateKeyPair)(
|
|
|
|
&handle->dh,
|
|
|
|
get_rng(),
|
|
|
|
handle->private_key,
|
|
|
|
&my_private_key_length,
|
|
|
|
public_key,
|
|
|
|
&my_public_key_length)))
|
2015-01-07 13:13:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(rng_lock);
|
|
|
|
return false;
|
2015-01-07 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(rng_lock);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dh_align_key(public_key, my_public_key_length, handle->key_length);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handle->private_key_length = my_private_key_length;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (public_key_length != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*public_key_length = handle->key_length;
|
|
|
|
}
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_dh_secret_t tr_dh_agree(tr_dh_ctx_t raw_handle, uint8_t const* other_public_key, size_t other_public_key_length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(raw_handle != nullptr);
|
|
|
|
TR_ASSERT(other_public_key != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* handle = static_cast<struct tr_dh_ctx*>(raw_handle);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_dh_secret* ret;
|
|
|
|
word32 my_secret_key_length;
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_dh_secret_new(handle->key_length);
|
2015-01-07 02:04:08 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (check_result(API(DhAgree)(
|
|
|
|
&handle->dh,
|
|
|
|
ret->key,
|
|
|
|
&my_secret_key_length,
|
|
|
|
handle->private_key,
|
|
|
|
handle->private_key_length,
|
|
|
|
other_public_key,
|
|
|
|
other_public_key_length)))
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dh_secret_align(ret, my_secret_key_length);
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_dh_secret_free(ret);
|
2021-09-15 00:18:09 +00:00
|
|
|
ret = nullptr;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_rand_buffer(void* buffer, size_t length)
|
2015-01-07 02:04:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
|
|
|
tr_lock* rng_lock = get_rng_lock();
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockLock(rng_lock);
|
2021-09-12 17:41:49 +00:00
|
|
|
ret = check_result(API(RNG_GenerateBlock)(get_rng(), static_cast<byte*>(buffer), length));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(rng_lock);
|
2015-01-07 13:13:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2015-01-07 02:04:08 +00:00
|
|
|
}
|