1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-27 04:34:42 +01:00

Merge pull request #19 from UnigramDev/public

Crypto is now working on WinRT
This commit is contained in:
Gregory K 2017-05-19 14:17:25 +03:00 committed by GitHub
commit 454914a4b6
3 changed files with 133 additions and 43 deletions

View File

@ -17,6 +17,10 @@
<Configuration>Release</Configuration>
<Platform>ARM</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
@ -220,7 +224,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -237,7 +241,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -254,7 +258,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -271,7 +275,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preview|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -288,7 +292,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preview|ARM'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -305,7 +309,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preview|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;TGVOIP_USE_CUSTOM_CRYPTO;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NOMINMAX;WEBRTC_APM_DEBUG_DUMP=0;NDEBUG;TGVOIP_USE_CUSTOM_CRYPTO;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
@ -594,6 +598,5 @@
<ClCompile Include="webrtc_dsp\webrtc\system_wrappers\source\cpu_features.cc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<ImportGroup Label="ExtensionTargets" />
</Project>

View File

@ -5,7 +5,11 @@
#include <string>
#include <collection.h>
#include "CXWrapper.h"
#include <wrl.h>
#include <robuffer.h>
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;
using namespace libtgvoip;
using namespace Platform;
using namespace tgvoip;
@ -171,19 +175,27 @@ void VoIPControllerWrapper::UpdateServerConfig(Platform::String^ json){
}
void MicrosoftCryptoImpl::AesIgeEncrypt(uint8_t* in, uint8_t* out, size_t len, uint8_t* key, uint8_t* iv){
Platform::Array<uint8>^ keybuf=ref new Platform::Array<uint8>(16);
memcpy(keybuf->Data, key, 16);
CryptographicKey^ _key=aesKeyProvider->CreateSymmetricKey(CryptographicBuffer::CreateFromByteArray(keybuf));
Platform::Array<uint8>^ tmpIn=ref new Platform::Array<uint8>(16);
Platform::Array<uint8>^ tmpOut=ref new Platform::Array<uint8>(16);
IBuffer^ keybuf=IBufferFromPtr(key, 32);
CryptographicKey^ _key=aesKeyProvider->CreateSymmetricKey(keybuf);
uint8_t tmpOut[16];
uint8_t* xPrev=iv+16;
uint8_t* yPrev=iv;
uint8_t x[16];
uint8_t y[16];
for(size_t offset=0;offset<len;offset+=16){
uint8_t* y=tmpIn->Data;
uint8_t* x=in+offset;
for (size_t i=0;i<16;i++){
if (offset+i < len){
x[i] = in[offset+i];
}
else{
x[i]=0;
}
}
XorInt128(x, yPrev, y);
CryptographicBuffer::CopyToByteArray(CryptographicEngine::Encrypt(_key, CryptographicBuffer::CreateFromByteArray(tmpIn), nullptr), &tmpOut);
XorInt128(tmpOut->Data, xPrev, y);
IBuffer^ inbuf=IBufferFromPtr(y, 16);
IBuffer^ outbuf=CryptographicEngine::Encrypt(_key, inbuf, nullptr);
IBufferToPtr(outbuf, 16, tmpOut);
XorInt128(tmpOut, xPrev, y);
memcpy(xPrev, x, 16);
memcpy(yPrev, y, 16);
memcpy(out+offset, y, 16);
@ -191,19 +203,27 @@ void MicrosoftCryptoImpl::AesIgeEncrypt(uint8_t* in, uint8_t* out, size_t len, u
}
void MicrosoftCryptoImpl::AesIgeDecrypt(uint8_t* in, uint8_t* out, size_t len, uint8_t* key, uint8_t* iv){
Platform::Array<uint8>^ keybuf=ref new Platform::Array<uint8>(16);
memcpy(keybuf->Data, key, 16);
CryptographicKey^ _key=aesKeyProvider->CreateSymmetricKey(CryptographicBuffer::CreateFromByteArray(keybuf));
Platform::Array<uint8>^ tmpIn=ref new Platform::Array<uint8>(16);
Platform::Array<uint8>^ tmpOut=ref new Platform::Array<uint8>(16);
IBuffer^ keybuf=IBufferFromPtr(key, 32);
CryptographicKey^ _key=aesKeyProvider->CreateSymmetricKey(keybuf);
uint8_t tmpOut[16];
uint8_t* xPrev=iv;
uint8_t* yPrev=iv+16;
uint8_t x[16];
uint8_t y[16];
for(size_t offset=0;offset<len;offset+=16){
uint8_t* y=tmpIn->Data;
uint8_t* x=in+offset;
for (size_t i=0;i<16;i++){
if (offset+i < len){
x[i] = in[offset+i];
}
else{
x[i]=0;
}
}
XorInt128(x, yPrev, y);
CryptographicBuffer::CopyToByteArray(CryptographicEngine::Decrypt(_key, CryptographicBuffer::CreateFromByteArray(tmpIn), nullptr), &tmpOut);
XorInt128(tmpOut->Data, xPrev, y);
IBuffer^ inbuf=IBufferFromPtr(y, 16);
IBuffer^ outbuf=CryptographicEngine::Decrypt(_key, inbuf, nullptr);
IBufferToPtr(outbuf, 16, tmpOut);
XorInt128(tmpOut, xPrev, y);
memcpy(xPrev, x, 16);
memcpy(yPrev, y, 16);
memcpy(out+offset, y, 16);
@ -213,14 +233,11 @@ void MicrosoftCryptoImpl::AesIgeDecrypt(uint8_t* in, uint8_t* out, size_t len, u
void MicrosoftCryptoImpl::SHA1(uint8_t* msg, size_t len, uint8_t* out){
//EnterCriticalSection(&hashMutex);
Platform::Array<uint8>^ arr=ref new Platform::Array<uint8>(len);
memcpy(arr->Data, msg, len);
IBuffer^ arr=IBufferFromPtr(msg, len);
CryptographicHash^ hash=sha1Provider->CreateHash();
hash->Append(CryptographicBuffer::CreateFromByteArray(arr));
hash->Append(arr);
IBuffer^ res=hash->GetValueAndReset();
Platform::Array<uint8>^ _out=ref new Platform::Array<uint8>(20);
CryptographicBuffer::CopyToByteArray(res, &_out);
memcpy(out, _out->Data, 20);
IBufferToPtr(res, 20, out);
//LeaveCriticalSection(&hashMutex);
}
@ -228,22 +245,17 @@ void MicrosoftCryptoImpl::SHA1(uint8_t* msg, size_t len, uint8_t* out){
void MicrosoftCryptoImpl::SHA256(uint8_t* msg, size_t len, uint8_t* out){
//EnterCriticalSection(&hashMutex);
Platform::Array<uint8>^ arr=ref new Platform::Array<uint8>(len);
memcpy(arr->Data, msg, len);
IBuffer^ arr=IBufferFromPtr(msg, len);
CryptographicHash^ hash=sha256Provider->CreateHash();
hash->Append(CryptographicBuffer::CreateFromByteArray(arr));
hash->Append(arr);
IBuffer^ res=hash->GetValueAndReset();
Platform::Array<uint8>^ _out=ref new Platform::Array<uint8>(32);
CryptographicBuffer::CopyToByteArray(res, &_out);
memcpy(out, _out->Data, 32);
IBufferToPtr(res, 32, out);
//LeaveCriticalSection(&hashMutex);
}
void MicrosoftCryptoImpl::RandBytes(uint8_t* buffer, size_t len){
Platform::Array<uint8>^ a=ref new Platform::Array<uint8>(len);
CryptographicBuffer::CopyToByteArray(CryptographicBuffer::GenerateRandom(len), &a);
memcpy(buffer, a->Data, len);
IBuffer^ res=CryptographicBuffer::GenerateRandom(len);
IBufferToPtr(res, len, buffer);
}
void MicrosoftCryptoImpl::Init(){
@ -262,6 +274,22 @@ void MicrosoftCryptoImpl::XorInt128(uint8_t* a, uint8_t* b, uint8_t* out){
_out[1]=_a[1]^_b[1];
}
void MicrosoftCryptoImpl::IBufferToPtr(IBuffer^ buffer, size_t len, uint8_t* out)
{
ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(buffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
byte* hashBuffer;
bufferByteAccess->Buffer(&hashBuffer);
CopyMemory(out, hashBuffer, len);
}
IBuffer^ MicrosoftCryptoImpl::IBufferFromPtr(uint8_t* msg, size_t len)
{
ComPtr<NativeBuffer> nativeBuffer=Make<NativeBuffer>((byte *)msg, len);
return reinterpret_cast<IBuffer^>(nativeBuffer.Get());
}
/*Platform::String^ VoIPControllerWrapper::TestAesIge(){
MicrosoftCryptoImpl::Init();
Platform::String^ res="";

View File

@ -1,5 +1,10 @@
#pragma once
#include <wrl.h>
#include <wrl/implements.h>
#include <windows.storage.streams.h>
#include <robuffer.h>
#include <vector>
#include "../../VoIPController.h"
#include "../../VoIPServerConfig.h"
@ -55,6 +60,7 @@ namespace libtgvoip{
public ref class VoIPControllerWrapper sealed{
public:
VoIPControllerWrapper();
virtual ~VoIPControllerWrapper();
void Start();
void Connect();
void SetPublicEndpoints(Windows::Foundation::Collections::IIterable<Endpoint^>^ endpoints, bool allowP2P);
@ -70,7 +76,6 @@ namespace libtgvoip{
static void UpdateServerConfig(Platform::String^ json);
//static Platform::String^ TestAesIge();
private:
~VoIPControllerWrapper();
static void OnStateChanged(tgvoip::VoIPController* c, int state);
void OnStateChangedInternal(int state);
tgvoip::VoIPController* controller;
@ -88,10 +93,64 @@ namespace libtgvoip{
static void Init();
private:
static inline void XorInt128(uint8_t* a, uint8_t* b, uint8_t* out);
static void IBufferToPtr(Windows::Storage::Streams::IBuffer^ buffer, size_t len, uint8_t* out);
static Windows::Storage::Streams::IBuffer^ IBufferFromPtr(uint8_t* msg, size_t len);
/*static Windows::Security::Cryptography::Core::CryptographicHash^ sha1Hash;
static Windows::Security::Cryptography::Core::CryptographicHash^ sha256Hash;*/
static Windows::Security::Cryptography::Core::HashAlgorithmProvider^ sha1Provider;
static Windows::Security::Cryptography::Core::HashAlgorithmProvider^ sha256Provider;
static Windows::Security::Cryptography::Core::SymmetricKeyAlgorithmProvider^ aesKeyProvider;
};
class NativeBuffer :
public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>,
ABI::Windows::Storage::Streams::IBuffer,
Windows::Storage::Streams::IBufferByteAccess>
{
public:
NativeBuffer(byte *buffer, UINT totalSize)
{
m_length=totalSize;
m_buffer=buffer;
}
virtual ~NativeBuffer()
{
}
STDMETHODIMP RuntimeClassInitialize(byte *buffer, UINT totalSize)
{
m_length=totalSize;
m_buffer=buffer;
return S_OK;
}
STDMETHODIMP Buffer(byte **value)
{
*value=m_buffer;
return S_OK;
}
STDMETHODIMP get_Capacity(UINT32 *value)
{
*value=m_length;
return S_OK;
}
STDMETHODIMP get_Length(UINT32 *value)
{
*value=m_length;
return S_OK;
}
STDMETHODIMP put_Length(UINT32 value)
{
m_length=value;
return S_OK;
}
private:
UINT32 m_length;
byte *m_buffer;
};
}