Fix windows build

This commit is contained in:
Simon 2022-01-27 22:08:40 +00:00
parent 768aa8a4c3
commit 5a1de60e4d
2 changed files with 49 additions and 25 deletions

View File

@ -1,18 +1,24 @@
#include "encrypt_wrapper.h" #include "encrypt_wrapper.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <byteswap.h>
#ifdef _WIN32 #ifdef _MSC_VER
#define _CRT_RAND_S
#define __bswap_16 _byteswap_ushort
#define __bswap_32 _byteswap_ulong
#else #else
#include <byteswap.h>
#include <sys/random.h> #include <sys/random.h>
#endif #endif
#include <stdlib.h>
#include "romulus_m.h" #include "romulus_m.h"
#include "api.h" #include "api.h"
int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned char* key){
if(sizeof(u_int16_t) + sizeof(u_int16_t) + packet->Size > *len){ int encrypt(packet *packet, unsigned char* outBuf, UINT16 *len, unsigned char* key){
if(sizeof(UINT16) + sizeof(UINT16) + packet->Size > *len){
return 1; return 1;
} }
@ -32,7 +38,16 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned cha
// n = nonce, CRYPTO_NPUBBYTES size 16 // n = nonce, CRYPTO_NPUBBYTES size 16
unsigned char npub[CRYPTO_NPUBBYTES]; unsigned char npub[CRYPTO_NPUBBYTES];
#ifdef _MSC_VER
UINT32 secRandom;
for (size_t i = 0; i < CRYPTO_NPUBBYTES; i+=sizeof(UINT32))
{
rand_s(&secRandom);
memcpy(&npub[i], &secRandom, sizeof(UINT32));
}
#else
getrandom(&npub[0], sizeof(npub), 0); getrandom(&npub[0], sizeof(npub), 0);
#endif
int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key); int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key);
@ -45,16 +60,16 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned cha
// Swap after adding it as additional data // Swap after adding it as additional data
packet->DataType = __bswap_16(packet->DataType); packet->DataType = __bswap_16(packet->DataType);
int loc = 0; int loc = 0;
memcpy(&outBuf[loc], &packet->DataType,sizeof(u_int16_t)); memcpy(&outBuf[loc], &packet->DataType,sizeof(UINT16));
loc += sizeof(u_int16_t); loc += sizeof(UINT16);
// data size 2 3 // data size 2 3
u_int16_t clen16 = (u_int16_t)clen; UINT16 clen16 = (UINT16)clen;
// we copy data and nonce // we copy data and nonce
clen16 += CRYPTO_NPUBBYTES; clen16 += CRYPTO_NPUBBYTES;
clen16 = __bswap_16(clen16); clen16 = __bswap_16(clen16);
memcpy(&outBuf[loc], &clen16, sizeof(u_int16_t)); memcpy(&outBuf[loc], &clen16, sizeof(UINT16));
loc += sizeof(u_int16_t); loc += sizeof(UINT16);
memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES); memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES);
loc += CRYPTO_NPUBBYTES; loc += CRYPTO_NPUBBYTES;
@ -69,17 +84,17 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned cha
return 0; return 0;
} }
int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len, unsigned char* key){ int decrypt(packetEx *packet, unsigned char* inBuf, UINT16 len, unsigned char* key){
int loc = 0; int loc = 0;
if(inBuf == 0 ){ if(inBuf == 0 ){
return 1; return 1;
} }
memcpy(&packet->DataType, &inBuf[loc], sizeof(u_int16_t)); memcpy(&packet->DataType, &inBuf[loc], sizeof(UINT16));
loc += sizeof(u_int16_t); loc += sizeof(UINT16);
memcpy(&packet->Id, &inBuf[loc], sizeof(u_int32_t)); memcpy(&packet->Id, &inBuf[loc], sizeof(UINT32));
loc += sizeof(u_int32_t); loc += sizeof(UINT32);
memcpy(&packet->Size, &inBuf[loc], sizeof(u_int16_t)); memcpy(&packet->Size, &inBuf[loc], sizeof(UINT16));
loc += sizeof(u_int16_t); loc += sizeof(UINT16);
packet->DataType = __bswap_16(packet->DataType); packet->DataType = __bswap_16(packet->DataType);
packet->Size = __bswap_16(packet->Size); packet->Size = __bswap_16(packet->Size);
@ -111,7 +126,7 @@ int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len, unsigned cha
int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key); int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key);
if(mlen <= 1000 && ret == 0){ if(mlen <= 1000 && ret == 0){
memcpy(packet->Data, m, mlen); memcpy(packet->Data, m, mlen);
packet->Size = (u_int16_t)mlen; packet->Size = (UINT16)mlen;
} }
else{ else{
free(m); free(m);

View File

@ -1,20 +1,29 @@
#include <stdio.h> #include <stdio.h>
#include "export.h" #include "export.h"
#ifdef _MSC_VER
#include <stdint.h>
#define UINT16 uint16_t
#define UINT32 uint32_t
#else
#define UINT16 __uint16_t
#define UINT32 __uint32_t
#endif
typedef struct { typedef struct {
__uint16_t DataType; UINT16 DataType;
__uint16_t Size; UINT16 Size;
unsigned char* Data; unsigned char* Data;
}packet; }packet;
typedef struct { typedef struct {
__uint16_t DataType; UINT16 DataType;
__uint32_t Id; UINT32 Id;
__uint16_t Size; UINT16 Size;
unsigned char* Data; unsigned char* Data;
}packetEx; }packetEx;
EXPORT int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned char* key); EXPORT int encrypt(packet *packet, unsigned char* outBuf, UINT16 *len, unsigned char* key);
EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len, unsigned char* key); EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, UINT16 len, unsigned char* key);