Add initial encrypt and dummy decrypt
This commit is contained in:
parent
936fe8c0ad
commit
8192496116
|
@ -52,3 +52,8 @@ Module.symvers
|
|||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
|
||||
CMakeFiles
|
||||
cmake_install.cmake
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
|
|
|
@ -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)
|
|
@ -0,0 +1,97 @@
|
|||
#include "encrypt_wrapper.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <byteswap.h>
|
||||
#include <sys/random.h>
|
||||
#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;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#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);
|
|
@ -8,9 +8,6 @@
|
|||
|
||||
#ifdef _WIN32
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#elif __linux__
|
||||
#else
|
||||
#define EXPORT __attribute__((visibility("default")))
|
||||
#elif __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#define EXPORT EMSCRIPTEN_KEEPALIVE
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue