From 8192496116f5f7b71238b6e7d33240fa0de1a96d Mon Sep 17 00:00:00 2001 From: Simon Maddocks Date: Tue, 25 Jan 2022 23:01:41 +0000 Subject: [PATCH] Add initial encrypt and dummy decrypt --- .gitignore | 5 ++ Romulus-M/CMakeLists.txt | 14 ++---- Romulus-M/encrypt_wrapper.c | 97 +++++++++++++++++++++++++++++++++++++ Romulus-M/encrypt_wrapper.h | 20 ++++++++ Romulus-M/export.h | 5 +- 5 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 Romulus-M/encrypt_wrapper.c create mode 100644 Romulus-M/encrypt_wrapper.h diff --git a/.gitignore b/.gitignore index cd531cf..5e2259c 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,8 @@ Module.symvers Mkfile.old dkms.conf + +CMakeFiles +cmake_install.cmake +CMakeCache.txt +Makefile diff --git a/Romulus-M/CMakeLists.txt b/Romulus-M/CMakeLists.txt index 2f3c033..8a7555f 100644 --- a/Romulus-M/CMakeLists.txt +++ b/Romulus-M/CMakeLists.txt @@ -3,16 +3,8 @@ set(SRC encrypt.c romulus_m_reference.c skinny_reference.c + encrypt_wrapper.c ) -if(EMSCRIPTEN) - # WASM requires an executable, so we use a dummy target that doesn't do anything - # and link our library into it. - add_library(romulus-m STATIC ${SRC}) - add_executable(romulus-m-wasm "main.c") - target_link_libraries(romulus-m-wasm PRIVATE romulus-m) - target_link_options(romulus-m-wasm PRIVATE -s EXPORTED_FUNCTIONS=['_main', '_crypto_aead_encrypt', '_crypto_aead_decrypt']) -else() - add_library(romulus-m SHARED ${SRC}) - install(TARGETS romulus-m) -endif() +add_library(romulus-m STATIC ${SRC}) +install(TARGETS romulus-m) \ No newline at end of file diff --git a/Romulus-M/encrypt_wrapper.c b/Romulus-M/encrypt_wrapper.c new file mode 100644 index 0000000..d8fa9c1 --- /dev/null +++ b/Romulus-M/encrypt_wrapper.c @@ -0,0 +1,97 @@ +#include "encrypt_wrapper.h" +#include +#include +#include +#include +#include +#include "romulus_m.h" +#include "api.h" + + +int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){ + if(sizeof(u_int16_t) + sizeof(u_int16_t) + packet->Size > *len){ + return 1; + } + + int packetSize = packet->Size; + + packet->DataType = __bswap_16(packet->DataType); + + // enc + // ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES + unsigned long long clen = packetSize + CRYPTO_ABYTES; + unsigned char* c = (unsigned char*) calloc(clen, 1); + + // plaintext - in + int mlen = packetSize; + unsigned char* m = packet->Data; + + + // additional text + int adlen = sizeof(packet->DataType); + unsigned char* ad = (unsigned char*)&packet->DataType; + + // unused + unsigned char* nsec; + + // n = nonce, CRYPTO_NPUBBYTES size 16 + unsigned char npub[CRYPTO_NPUBBYTES]; + getrandom(&npub[0], sizeof(npub), 0); + + // key, CRYPTO_KEYBYTES size 16 + unsigned char k[CRYPTO_KEYBYTES] = {0}; + romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], &k[0]); + + if(clen > *len + CRYPTO_NPUBBYTES){ + free(c); + return 1; + } + + // type 0 1 + int loc = 0; + memcpy(&outBuf[loc], &packet->DataType,sizeof(u_int16_t)); + loc += sizeof(u_int16_t); + + // data size 2 3 + u_int16_t clen16 = (u_int16_t)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], npub, CRYPTO_NPUBBYTES); + loc += CRYPTO_NPUBBYTES; + + memcpy(&outBuf[loc], c, clen); + loc += clen; + + *len = loc; + + free(c); + + return 0; +} + +int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len){ + int loc = 0; + 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); + + packet->DataType = __bswap_16(packet->DataType); + packet->Size = __bswap_16(packet->Size); + packet->Id = __bswap_32(packet->Id); + + if(packet->Size <= 1000){ + memcpy(packet->Data, &inBuf[loc], packet->Size); + } + else{ + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/Romulus-M/encrypt_wrapper.h b/Romulus-M/encrypt_wrapper.h new file mode 100644 index 0000000..6f0f8ac --- /dev/null +++ b/Romulus-M/encrypt_wrapper.h @@ -0,0 +1,20 @@ +#include +#include "export.h" + +typedef struct { + __uint16_t DataType; + __uint16_t Size; + unsigned char* Data; +}packet; + +typedef struct { + __uint16_t DataType; + __uint32_t Id; + __uint16_t Size; + unsigned char* Data; +}packetEx; + +EXPORT int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len); + + +EXPORT int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len); \ No newline at end of file diff --git a/Romulus-M/export.h b/Romulus-M/export.h index 3776f2f..92fa7ab 100644 --- a/Romulus-M/export.h +++ b/Romulus-M/export.h @@ -8,9 +8,6 @@ #ifdef _WIN32 #define EXPORT __declspec(dllexport) -#elif __linux__ +#else #define EXPORT __attribute__((visibility("default"))) -#elif __EMSCRIPTEN__ -#include -#define EXPORT EMSCRIPTEN_KEEPALIVE #endif