1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-30 04:39:03 +01:00

Support for custom crypto

Moved TGVOIP_USE_CUSTOM_CRYPTO checks to TgVoip.h/cpp
Add new source files to automake config
This commit is contained in:
Igor Zhukov 2019-12-27 15:19:21 +04:00
parent 50a91963a7
commit 651396bdec
4 changed files with 109 additions and 81 deletions

View File

@ -4,7 +4,8 @@ CFLAGS = -Wall -DHAVE_CONFIG_H -Wno-unknown-pragmas
lib_LTLIBRARIES = libtgvoip.la lib_LTLIBRARIES = libtgvoip.la
SRC = VoIPController.cpp \ SRC = TgVoip.cpp \
VoIPController.cpp \
Buffers.cpp \ Buffers.cpp \
CongestionControl.cpp \ CongestionControl.cpp \
EchoCanceller.cpp \ EchoCanceller.cpp \
@ -29,6 +30,7 @@ video/ScreamCongestionController.cpp \
json11.cpp json11.cpp
TGVOIP_HDRS = \ TGVOIP_HDRS = \
TgVoip.h \
VoIPController.h \ VoIPController.h \
Buffers.h \ Buffers.h \
BlockingQueue.h \ BlockingQueue.h \

View File

@ -1,7 +1,85 @@
#import "TgVoip.h" #include "TgVoip.h"
#include "VoIPController.h"
#include "VoIPServerConfig.h"
#include <stdarg.h>
#ifndef TGVOIP_USE_CUSTOM_CRYPTO
extern "C" {
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/modes.h>
#include <openssl/rand.h>
}
void tgvoip_openssl_aes_ige_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_encrypt_key(key, 32*8, &akey);
AES_ige_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
}
void tgvoip_openssl_aes_ige_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_decrypt_key(key, 32*8, &akey);
AES_ige_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
}
void tgvoip_openssl_rand_bytes(uint8_t* buffer, size_t len){
RAND_bytes(buffer, len);
}
void tgvoip_openssl_sha1(uint8_t* msg, size_t len, uint8_t* output){
SHA1(msg, len, output);
}
void tgvoip_openssl_sha256(uint8_t* msg, size_t len, uint8_t* output){
SHA256(msg, len, output);
}
void tgvoip_openssl_aes_ctr_encrypt(uint8_t* inout, size_t length, uint8_t* key, uint8_t* iv, uint8_t* ecount, uint32_t* num){
AES_KEY akey;
AES_set_encrypt_key(key, 32*8, &akey);
CRYPTO_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num, (block128_f) AES_encrypt);
}
void tgvoip_openssl_aes_cbc_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_encrypt_key(key, 256, &akey);
AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
}
void tgvoip_openssl_aes_cbc_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_decrypt_key(key, 256, &akey);
AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
}
tgvoip::CryptoFunctions tgvoip::VoIPController::crypto={
tgvoip_openssl_rand_bytes,
tgvoip_openssl_sha1,
tgvoip_openssl_sha256,
tgvoip_openssl_aes_ige_encrypt,
tgvoip_openssl_aes_ige_decrypt,
tgvoip_openssl_aes_ctr_encrypt,
tgvoip_openssl_aes_cbc_encrypt,
tgvoip_openssl_aes_cbc_decrypt
};
#else
struct TgVoipCrypto {
void (*rand_bytes)(uint8_t* buffer, size_t length);
void (*sha1)(uint8_t* msg, size_t length, uint8_t* output);
void (*sha256)(uint8_t* msg, size_t length, uint8_t* output);
void (*aes_ige_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
void (*aes_ige_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
void (*aes_ctr_encrypt)(uint8_t* inout, size_t length, uint8_t* key, uint8_t* iv, uint8_t* ecount, uint32_t* num);
void (*aes_cbc_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
void (*aes_cbc_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
};
CryptoFunctions tgvoip::VoIPController::crypto; // set it yourself upon initialization
#endif
#import "VoIPController.h"
#import "VoIPServerConfig.h"
class TgVoipImpl : public TgVoip { class TgVoipImpl : public TgVoip {
public: public:
@ -10,29 +88,34 @@ public:
std::function<void(int)> onSignalBarsUpdated_; std::function<void(int)> onSignalBarsUpdated_;
TgVoipImpl( TgVoipImpl(
TgVoipCrypto const &crypto,
std::vector<TgVoipEndpoint> const &endpoints, std::vector<TgVoipEndpoint> const &endpoints,
TgVoipPersistentState const &persistentState, TgVoipPersistentState const &persistentState,
std::unique_ptr<TgVoipProxy> const &proxy, std::unique_ptr<TgVoipProxy> const &proxy,
TgVoipConfig const &config, TgVoipConfig const &config,
TgVoipEncryptionKey const &encryptionKey, TgVoipEncryptionKey const &encryptionKey,
TgVoipNetworkType initialNetworkType TgVoipNetworkType initialNetworkType
#if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) #ifdef TGVOIP_USE_CUSTOM_CRYPTO
,
TgVoipCrypto const &crypto
#endif
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
, ,
TgVoipAudioDataCallbacks const &audioDataCallbacks TgVoipAudioDataCallbacks const &audioDataCallbacks
#endif #endif
) { ) {
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
tgvoip::VoIPController::crypto.sha1 = crypto.sha1; tgvoip::VoIPController::crypto.sha1 = crypto.sha1;
tgvoip::VoIPController::crypto.sha256 = crypto.sha256; tgvoip::VoIPController::crypto.sha256 = crypto.sha256;
tgvoip::VoIPController::crypto.rand_bytes = crypto.rand_bytes; tgvoip::VoIPController::crypto.rand_bytes = crypto.rand_bytes;
tgvoip::VoIPController::crypto.aes_ige_encrypt = crypto.aes_ige_encrypt; tgvoip::VoIPController::crypto.aes_ige_encrypt = crypto.aes_ige_encrypt;
tgvoip::VoIPController::crypto.aes_ige_decrypt = crypto.aes_ige_decrypt; tgvoip::VoIPController::crypto.aes_ige_decrypt = crypto.aes_ige_decrypt;
tgvoip::VoIPController::crypto.aes_ctr_encrypt = crypto.aes_ctr_encrypt; tgvoip::VoIPController::crypto.aes_ctr_encrypt = crypto.aes_ctr_encrypt;
#endif
controller_ = new tgvoip::VoIPController(); controller_ = new tgvoip::VoIPController();
controller_->implData = this; controller_->implData = this;
#if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
controller_->SetAudioDataCallbacks(audioDataCallbacks.input, audioDataCallbacks.output, audioDataCallbacks.preprocessed); controller_->SetAudioDataCallbacks(audioDataCallbacks.input, audioDataCallbacks.output, audioDataCallbacks.preprocessed);
#endif #endif
@ -301,27 +384,33 @@ void TgVoip::setGlobalServerConfig(const std::string &serverConfig) {
} }
TgVoip *TgVoip::makeInstance( TgVoip *TgVoip::makeInstance(
TgVoipCrypto const &crypto,
TgVoipConfig const &config, TgVoipConfig const &config,
TgVoipPersistentState const &persistentState, TgVoipPersistentState const &persistentState,
std::vector<TgVoipEndpoint> const &endpoints, std::vector<TgVoipEndpoint> const &endpoints,
std::unique_ptr<TgVoipProxy> const &proxy, std::unique_ptr<TgVoipProxy> const &proxy,
TgVoipNetworkType initialNetworkType, TgVoipNetworkType initialNetworkType,
TgVoipEncryptionKey const &encryptionKey TgVoipEncryptionKey const &encryptionKey
#if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) #ifdef TGVOIP_USE_CUSTOM_CRYPTO
,
TgVoipCrypto const &crypto
#endif
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
, ,
TgVoipAudioDataCallbacks const &audioDataCallbacks TgVoipAudioDataCallbacks const &audioDataCallbacks
#endif #endif
) { ) {
return new TgVoipImpl( return new TgVoipImpl(
crypto,
endpoints, endpoints,
persistentState, persistentState,
proxy, proxy,
config, config,
encryptionKey, encryptionKey,
initialNetworkType initialNetworkType
#if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) #ifdef TGVOIP_USE_CUSTOM_CRYPTO
,
crypto
#endif
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
, ,
audioDataCallbacks audioDataCallbacks
#endif #endif

View File

@ -53,6 +53,7 @@ struct TgVoipPersistentState {
std::vector<uint8_t> value; std::vector<uint8_t> value;
}; };
#ifdef TGVOIP_USE_CUSTOM_CRYPTO
struct TgVoipCrypto { struct TgVoipCrypto {
void (*rand_bytes)(uint8_t* buffer, size_t length); void (*rand_bytes)(uint8_t* buffer, size_t length);
void (*sha1)(uint8_t* msg, size_t length, uint8_t* output); void (*sha1)(uint8_t* msg, size_t length, uint8_t* output);
@ -63,6 +64,7 @@ struct TgVoipCrypto {
void (*aes_cbc_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv); void (*aes_cbc_encrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
void (*aes_cbc_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv); void (*aes_cbc_decrypt)(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv);
}; };
#endif
struct TgVoipConfig { struct TgVoipConfig {
double initializationTimeout; double initializationTimeout;
@ -118,14 +120,17 @@ public:
static void setLoggingFunction(std::function<void(std::string const &)> loggingFunction); static void setLoggingFunction(std::function<void(std::string const &)> loggingFunction);
static void setGlobalServerConfig(std::string const &serverConfig); static void setGlobalServerConfig(std::string const &serverConfig);
static TgVoip *makeInstance( static TgVoip *makeInstance(
TgVoipCrypto const &crypto,
TgVoipConfig const &config, TgVoipConfig const &config,
TgVoipPersistentState const &persistentState, TgVoipPersistentState const &persistentState,
std::vector<TgVoipEndpoint> const &endpoints, std::vector<TgVoipEndpoint> const &endpoints,
std::unique_ptr<TgVoipProxy> const &proxy, std::unique_ptr<TgVoipProxy> const &proxy,
TgVoipNetworkType initialNetworkType, TgVoipNetworkType initialNetworkType,
TgVoipEncryptionKey const &encryptionKey TgVoipEncryptionKey const &encryptionKey
#if defined(TGVOIP_USE_CALLBACK_AUDIO_IO) #ifdef TGVOIP_USE_CUSTOM_CRYPTO
,
TgVoipCrypto const &crypto
#endif
#ifdef TGVOIP_USE_CALLBACK_AUDIO_IO
, ,
TgVoipAudioDataCallbacks const &audioDataCallbacks TgVoipAudioDataCallbacks const &audioDataCallbacks
#endif #endif

View File

@ -69,74 +69,6 @@ extern jclass jniUtilitiesClass;
#include "audio/AudioIOCallback.h" #include "audio/AudioIOCallback.h"
#endif #endif
#pragma mark - OpenSSL wrappers
#ifndef TGVOIP_USE_CUSTOM_CRYPTO
extern "C" {
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/modes.h>
#include <openssl/rand.h>
}
void tgvoip_openssl_aes_ige_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_encrypt_key(key, 32*8, &akey);
AES_ige_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
}
void tgvoip_openssl_aes_ige_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_decrypt_key(key, 32*8, &akey);
AES_ige_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
}
void tgvoip_openssl_rand_bytes(uint8_t* buffer, size_t len){
RAND_bytes(buffer, len);
}
void tgvoip_openssl_sha1(uint8_t* msg, size_t len, uint8_t* output){
SHA1(msg, len, output);
}
void tgvoip_openssl_sha256(uint8_t* msg, size_t len, uint8_t* output){
SHA256(msg, len, output);
}
void tgvoip_openssl_aes_ctr_encrypt(uint8_t* inout, size_t length, uint8_t* key, uint8_t* iv, uint8_t* ecount, uint32_t* num){
AES_KEY akey;
AES_set_encrypt_key(key, 32*8, &akey);
CRYPTO_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num, (block128_f) AES_encrypt);
}
void tgvoip_openssl_aes_cbc_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_encrypt_key(key, 256, &akey);
AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT);
}
void tgvoip_openssl_aes_cbc_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
AES_KEY akey;
AES_set_decrypt_key(key, 256, &akey);
AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
}
CryptoFunctions VoIPController::crypto={
tgvoip_openssl_rand_bytes,
tgvoip_openssl_sha1,
tgvoip_openssl_sha256,
tgvoip_openssl_aes_ige_encrypt,
tgvoip_openssl_aes_ige_decrypt,
tgvoip_openssl_aes_ctr_encrypt,
tgvoip_openssl_aes_cbc_encrypt,
tgvoip_openssl_aes_cbc_decrypt
};
#else
CryptoFunctions VoIPController::crypto; // set it yourself upon initialization
#endif
extern FILE* tgvoipLogFile; extern FILE* tgvoipLogFile;
#pragma mark - Public API #pragma mark - Public API