2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstring> /* memcpy(), memmove(), memset() */
|
2007-10-25 13:59:46 +00:00
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
#include <arc4.h>
|
|
|
|
|
2010-04-23 16:36:16 +00:00
|
|
|
#include "transmission.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static auto constexpr PrimeLen = size_t{ 96 };
|
|
|
|
static auto constexpr DhPrivkeyLen = size_t{ 20 };
|
2009-12-12 03:51:36 +00:00
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static uint8_t constexpr dh_P[PrimeLen] = {
|
2021-08-15 09:41:48 +00:00
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, //
|
|
|
|
0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, //
|
|
|
|
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, //
|
|
|
|
0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD, //
|
|
|
|
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, //
|
|
|
|
0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, //
|
|
|
|
0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9, //
|
|
|
|
0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63, //
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static uint8_t constexpr dh_G[] = { 2 };
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-13 23:15:43 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void ensureKeyExists(tr_crypto* crypto)
|
2007-10-11 02:22:17 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (crypto->dh == nullptr)
|
2007-10-11 02:22:17 +00:00
|
|
|
{
|
2021-10-27 00:16:56 +00:00
|
|
|
size_t public_key_length = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
crypto->dh = tr_dh_new(dh_P, sizeof(dh_P), dh_G, sizeof(dh_G));
|
2022-01-17 18:39:50 +00:00
|
|
|
tr_dh_make_key(crypto->dh, DhPrivkeyLen, crypto->myPublicKey, &public_key_length);
|
#4400, #5462: Move DH helpers to crypto-utils
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing DH key exchange to crypto-utils.{c,h}. OpenSSL-related
functionality (DH context management) is moved to crypto-utils-openssl.c.
Since we know in advance that DH secret key management code will be the
same for most of backends, implement common functionality in separate
crypto-utils-fallback.c.
Add new tr_dh_ctx_t and tr_dh_secret_t types and functions to be
implemented by crypto backends:
* tr_dh_new - allocate DH context,
* tr_dh_free - free the context,
* tr_dh_make_key - generate private/public keypair,
* tr_dh_agree - perform DH key exchange and generate secret key,
* tr_dh_secret_derive - calculate secret key hash,
* tr_dh_secret_free - free the secret key,
* tr_dh_align_key - align some DH key in the buffer allocated for it.
Make DH secret key not accessible in plain form outside the crypto
backend. This allows for implementations where the key is managed by
the underlying library and is not even exposed to our backend.
2014-12-04 19:18:08 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(public_key_length == KEY_LEN);
|
2009-12-12 03:51:36 +00:00
|
|
|
}
|
2007-10-13 23:15:43 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-12-25 17:22:12 +00:00
|
|
|
tr_crypto::tr_crypto(tr_sha1_digest_t const* torrent_hash_in, bool is_incoming_in)
|
|
|
|
: is_incoming{ is_incoming_in }
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
if (torrent_hash_in != nullptr)
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
this->torrent_hash = *torrent_hash_in;
|
2021-12-21 22:14:15 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 17:22:12 +00:00
|
|
|
tr_crypto::~tr_crypto()
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
tr_dh_secret_free(this->mySecret);
|
|
|
|
tr_dh_free(this->dh);
|
|
|
|
tr_free(this->enc_key);
|
|
|
|
tr_free(this->dec_key);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_cryptoComputeSecret(tr_crypto* crypto, uint8_t const* peerPublicKey)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ensureKeyExists(crypto);
|
|
|
|
crypto->mySecret = tr_dh_agree(crypto->dh, peerPublicKey, KEY_LEN);
|
2021-09-15 00:18:09 +00:00
|
|
|
return crypto->mySecret != nullptr;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* tr_cryptoGetMyPublicKey(tr_crypto const* crypto, int* setme_len)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
ensureKeyExists(const_cast<tr_crypto*>(crypto));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_len = KEY_LEN;
|
|
|
|
return crypto->myPublicKey;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
static void init_rc4(tr_crypto const* crypto, struct arc4_context** setme, char const* key)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
TR_ASSERT(crypto->torrent_hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (*setme == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 03:47:29 +00:00
|
|
|
*setme = tr_new0(struct arc4_context, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-04 12:37:08 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
auto const buf = tr_cryptoSecretKeySha1(crypto, key, 4, std::data(*crypto->torrent_hash), std::size(*crypto->torrent_hash));
|
|
|
|
if (buf)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
arc4_init(*setme, std::data(*buf), std::size(*buf));
|
2021-09-12 03:47:29 +00:00
|
|
|
arc4_discard(*setme, 1024);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
static void crypt_rc4(struct arc4_context* key, size_t buf_len, void const* buf_in, void* buf_out)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (key == nullptr)
|
2014-12-04 12:37:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (buf_in != buf_out)
|
|
|
|
{
|
|
|
|
memmove(buf_out, buf_in, buf_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2014-12-04 12:37:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
arc4_process(key, buf_in, buf_out, buf_len);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
void tr_cryptoDecryptInit(tr_crypto* crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
init_rc4(crypto, &crypto->dec_key, crypto->is_incoming ? "keyA" : "keyB"); // lgtm[cpp/weak-cryptographic-algorithm]
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
void tr_cryptoDecrypt(tr_crypto* crypto, size_t buf_len, void const* buf_in, void* buf_out)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 03:47:29 +00:00
|
|
|
crypt_rc4(crypto->dec_key, buf_len, buf_in, buf_out); // lgtm[cpp/weak-cryptographic-algorithm]
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
void tr_cryptoEncryptInit(tr_crypto* crypto)
|
|
|
|
{
|
2021-12-25 17:22:12 +00:00
|
|
|
init_rc4(crypto, &crypto->enc_key, crypto->is_incoming ? "keyB" : "keyA"); // lgtm[cpp/weak-cryptographic-algorithm]
|
2021-09-12 03:47:29 +00:00
|
|
|
}
|
2014-12-04 12:37:08 +00:00
|
|
|
|
2021-09-12 03:47:29 +00:00
|
|
|
void tr_cryptoEncrypt(tr_crypto* crypto, size_t buf_len, void const* buf_in, void* buf_out)
|
|
|
|
{
|
|
|
|
crypt_rc4(crypto->enc_key, buf_len, buf_in, buf_out); // lgtm[cpp/weak-cryptographic-algorithm]
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::optional<tr_sha1_digest_t> tr_cryptoSecretKeySha1(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_crypto const* crypto,
|
|
|
|
void const* prepend_data,
|
|
|
|
size_t prepend_data_size,
|
|
|
|
void const* append_data,
|
2021-12-21 22:14:15 +00:00
|
|
|
size_t append_data_size)
|
#4400, #5462: Move DH helpers to crypto-utils
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing DH key exchange to crypto-utils.{c,h}. OpenSSL-related
functionality (DH context management) is moved to crypto-utils-openssl.c.
Since we know in advance that DH secret key management code will be the
same for most of backends, implement common functionality in separate
crypto-utils-fallback.c.
Add new tr_dh_ctx_t and tr_dh_secret_t types and functions to be
implemented by crypto backends:
* tr_dh_new - allocate DH context,
* tr_dh_free - free the context,
* tr_dh_make_key - generate private/public keypair,
* tr_dh_agree - perform DH key exchange and generate secret key,
* tr_dh_secret_derive - calculate secret key hash,
* tr_dh_secret_free - free the secret key,
* tr_dh_align_key - align some DH key in the buffer allocated for it.
Make DH secret key not accessible in plain form outside the crypto
backend. This allows for implementations where the key is managed by
the underlying library and is not even exposed to our backend.
2014-12-04 19:18:08 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(crypto != nullptr);
|
|
|
|
TR_ASSERT(crypto->mySecret != nullptr);
|
#4400, #5462: Move DH helpers to crypto-utils
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing DH key exchange to crypto-utils.{c,h}. OpenSSL-related
functionality (DH context management) is moved to crypto-utils-openssl.c.
Since we know in advance that DH secret key management code will be the
same for most of backends, implement common functionality in separate
crypto-utils-fallback.c.
Add new tr_dh_ctx_t and tr_dh_secret_t types and functions to be
implemented by crypto backends:
* tr_dh_new - allocate DH context,
* tr_dh_free - free the context,
* tr_dh_make_key - generate private/public keypair,
* tr_dh_agree - perform DH key exchange and generate secret key,
* tr_dh_secret_derive - calculate secret key hash,
* tr_dh_secret_free - free the secret key,
* tr_dh_align_key - align some DH key in the buffer allocated for it.
Make DH secret key not accessible in plain form outside the crypto
backend. This allows for implementations where the key is managed by
the underlying library and is not even exposed to our backend.
2014-12-04 19:18:08 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
return tr_dh_secret_derive(crypto->mySecret, prepend_data, prepend_data_size, append_data, append_data_size);
|
#4400, #5462: Move DH helpers to crypto-utils
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing DH key exchange to crypto-utils.{c,h}. OpenSSL-related
functionality (DH context management) is moved to crypto-utils-openssl.c.
Since we know in advance that DH secret key management code will be the
same for most of backends, implement common functionality in separate
crypto-utils-fallback.c.
Add new tr_dh_ctx_t and tr_dh_secret_t types and functions to be
implemented by crypto backends:
* tr_dh_new - allocate DH context,
* tr_dh_free - free the context,
* tr_dh_make_key - generate private/public keypair,
* tr_dh_agree - perform DH key exchange and generate secret key,
* tr_dh_secret_derive - calculate secret key hash,
* tr_dh_secret_free - free the secret key,
* tr_dh_align_key - align some DH key in the buffer allocated for it.
Make DH secret key not accessible in plain form outside the crypto
backend. This allows for implementations where the key is managed by
the underlying library and is not even exposed to our backend.
2014-12-04 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
void tr_cryptoSetTorrentHash(tr_crypto* crypto, tr_sha1_digest_t const& hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
crypto->torrent_hash = hash;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::optional<tr_sha1_digest_t> tr_cryptoGetTorrentHash(tr_crypto const* crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(crypto != nullptr);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
return crypto->torrent_hash;
|
2021-11-04 00:55:04 +00:00
|
|
|
}
|