diff --git a/Romulus-M/encrypt_wrapper.c b/Romulus-M/encrypt_wrapper.c index 8f8580c..f84814c 100644 --- a/Romulus-M/encrypt_wrapper.c +++ b/Romulus-M/encrypt_wrapper.c @@ -1,18 +1,24 @@ #include "encrypt_wrapper.h" #include -#include #include -#include -#ifdef _WIN32 + +#ifdef _MSC_VER +#define _CRT_RAND_S +#define __bswap_16 _byteswap_ushort +#define __bswap_32 _byteswap_ulong #else +#include #include #endif + +#include #include "romulus_m.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; } @@ -32,7 +38,16 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned cha // n = nonce, CRYPTO_NPUBBYTES size 16 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); + #endif 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 packet->DataType = __bswap_16(packet->DataType); int loc = 0; - memcpy(&outBuf[loc], &packet->DataType,sizeof(u_int16_t)); - loc += sizeof(u_int16_t); + memcpy(&outBuf[loc], &packet->DataType,sizeof(UINT16)); + loc += sizeof(UINT16); // data size 2 3 - u_int16_t clen16 = (u_int16_t)clen; + UINT16 clen16 = (UINT16)clen; // we copy data and nonce clen16 += CRYPTO_NPUBBYTES; clen16 = __bswap_16(clen16); - memcpy(&outBuf[loc], &clen16, sizeof(u_int16_t)); - loc += sizeof(u_int16_t); + memcpy(&outBuf[loc], &clen16, sizeof(UINT16)); + loc += sizeof(UINT16); memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES); loc += CRYPTO_NPUBBYTES; @@ -69,17 +84,17 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len, unsigned cha 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; if(inBuf == 0 ){ return 1; } - memcpy(&packet->DataType, &inBuf[loc], sizeof(u_int16_t)); - loc += sizeof(u_int16_t); - memcpy(&packet->Id, &inBuf[loc], sizeof(u_int32_t)); - loc += sizeof(u_int32_t); - memcpy(&packet->Size, &inBuf[loc], sizeof(u_int16_t)); - loc += sizeof(u_int16_t); + memcpy(&packet->DataType, &inBuf[loc], sizeof(UINT16)); + loc += sizeof(UINT16); + memcpy(&packet->Id, &inBuf[loc], sizeof(UINT32)); + loc += sizeof(UINT32); + memcpy(&packet->Size, &inBuf[loc], sizeof(UINT16)); + loc += sizeof(UINT16); packet->DataType = __bswap_16(packet->DataType); 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); if(mlen <= 1000 && ret == 0){ memcpy(packet->Data, m, mlen); - packet->Size = (u_int16_t)mlen; + packet->Size = (UINT16)mlen; } else{ free(m); diff --git a/Romulus-M/encrypt_wrapper.h b/Romulus-M/encrypt_wrapper.h index 6c637d3..ca8ccce 100644 --- a/Romulus-M/encrypt_wrapper.h +++ b/Romulus-M/encrypt_wrapper.h @@ -1,20 +1,29 @@ #include #include "export.h" +#ifdef _MSC_VER +#include +#define UINT16 uint16_t +#define UINT32 uint32_t +#else +#define UINT16 __uint16_t +#define UINT32 __uint32_t +#endif + typedef struct { - __uint16_t DataType; - __uint16_t Size; + UINT16 DataType; + UINT16 Size; unsigned char* Data; }packet; typedef struct { - __uint16_t DataType; - __uint32_t Id; - __uint16_t Size; + UINT16 DataType; + UINT32 Id; + UINT16 Size; unsigned char* Data; }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); \ No newline at end of file +EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, UINT16 len, unsigned char* key); \ No newline at end of file