2022-01-25 23:01:41 +00:00
|
|
|
#include "encrypt_wrapper.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-01-27 22:08:40 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define __bswap_16 _byteswap_ushort
|
|
|
|
#define __bswap_32 _byteswap_ulong
|
2022-01-31 22:08:55 +00:00
|
|
|
#elif __APPLE__
|
|
|
|
#include <libkern/OSByteOrder.h>
|
|
|
|
#define __bswap_16 OSSwapInt16
|
|
|
|
#define __bswap_32 OSSwapInt32
|
|
|
|
#define __bswap_64 OSSwapInt64
|
2022-01-26 00:21:27 +00:00
|
|
|
#else
|
2022-01-27 22:08:40 +00:00
|
|
|
#include <byteswap.h>
|
2022-01-26 00:21:27 +00:00
|
|
|
#endif
|
2022-01-27 22:08:40 +00:00
|
|
|
#include <stdlib.h>
|
2022-01-25 23:01:41 +00:00
|
|
|
#include "romulus_m.h"
|
|
|
|
#include "api.h"
|
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
#include <rand.h>
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define DEBUG_PRINT printf
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
2022-01-25 23:01:41 +00:00
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
randctx rCtx = {0};
|
2022-01-27 22:08:40 +00:00
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
int encrypt(packet *packet, unsigned char *outBuf, UINT16 *len, unsigned char *key)
|
|
|
|
{
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-30 00:53:20 +00:00
|
|
|
if (sizeof(UINT16) + sizeof(UINT16) + packet->Size > *len)
|
|
|
|
{
|
2022-01-25 23:01:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
int packetSize = packet->Size;
|
2022-01-25 23:01:41 +00:00
|
|
|
// enc
|
|
|
|
// ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
|
|
|
|
unsigned long long clen = packetSize + CRYPTO_ABYTES;
|
2022-01-30 00:53:20 +00:00
|
|
|
unsigned char *c = (unsigned char *)calloc(clen, 1);
|
|
|
|
|
2022-01-25 23:01:41 +00:00
|
|
|
// plaintext - in
|
|
|
|
int mlen = packetSize;
|
2022-01-30 00:53:20 +00:00
|
|
|
unsigned char *m = packet->Data;
|
|
|
|
|
2022-01-25 23:01:41 +00:00
|
|
|
// additional text
|
|
|
|
int adlen = sizeof(packet->DataType);
|
2022-01-30 00:53:20 +00:00
|
|
|
unsigned char *ad = (unsigned char *)&packet->DataType;
|
2022-01-25 23:01:41 +00:00
|
|
|
|
|
|
|
// n = nonce, CRYPTO_NPUBBYTES size 16
|
|
|
|
unsigned char npub[CRYPTO_NPUBBYTES];
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-30 00:53:20 +00:00
|
|
|
fill_random(npub, CRYPTO_NPUBBYTES);
|
2022-01-25 23:01:41 +00:00
|
|
|
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-27 00:43:24 +00:00
|
|
|
int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], key);
|
2022-01-30 00:53:20 +00:00
|
|
|
|
|
|
|
if (clen > *len + CRYPTO_NPUBBYTES || ret != 0)
|
|
|
|
{
|
2022-01-25 23:01:41 +00:00
|
|
|
free(c);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-01-30 00:53:20 +00:00
|
|
|
|
2022-01-25 23:01:41 +00:00
|
|
|
// type 0 1
|
2022-01-26 00:21:27 +00:00
|
|
|
// Swap after adding it as additional data
|
2022-01-30 00:53:20 +00:00
|
|
|
packet->DataType = __bswap_16(packet->DataType);
|
2022-01-25 23:01:41 +00:00
|
|
|
int loc = 0;
|
2022-01-30 00:53:20 +00:00
|
|
|
memcpy(&outBuf[loc], &packet->DataType, sizeof(UINT16));
|
2022-01-27 22:08:40 +00:00
|
|
|
loc += sizeof(UINT16);
|
2022-01-25 23:01:41 +00:00
|
|
|
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-25 23:01:41 +00:00
|
|
|
// data size 2 3
|
2022-01-27 22:08:40 +00:00
|
|
|
UINT16 clen16 = (UINT16)clen;
|
2022-01-25 23:01:41 +00:00
|
|
|
// we copy data and nonce
|
|
|
|
clen16 += CRYPTO_NPUBBYTES;
|
|
|
|
clen16 = __bswap_16(clen16);
|
2022-01-27 22:08:40 +00:00
|
|
|
memcpy(&outBuf[loc], &clen16, sizeof(UINT16));
|
|
|
|
loc += sizeof(UINT16);
|
2022-01-25 23:01:41 +00:00
|
|
|
|
|
|
|
memcpy(&outBuf[loc], npub, CRYPTO_NPUBBYTES);
|
|
|
|
loc += CRYPTO_NPUBBYTES;
|
|
|
|
|
|
|
|
memcpy(&outBuf[loc], c, clen);
|
|
|
|
loc += clen;
|
|
|
|
|
|
|
|
*len = loc;
|
|
|
|
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-25 23:01:41 +00:00
|
|
|
free(c);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
int decrypt(packetEx *packet, unsigned char *inBuf, UINT16 len, unsigned char *key)
|
|
|
|
{
|
2022-01-25 23:01:41 +00:00
|
|
|
int loc = 0;
|
2022-01-30 00:53:20 +00:00
|
|
|
if (inBuf == 0)
|
|
|
|
{
|
2022-01-27 00:43:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-27 22:08:40 +00:00
|
|
|
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);
|
2022-01-25 23:01:41 +00:00
|
|
|
|
|
|
|
packet->DataType = __bswap_16(packet->DataType);
|
|
|
|
packet->Size = __bswap_16(packet->Size);
|
|
|
|
packet->Id = __bswap_32(packet->Id);
|
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
if ((len - loc) < 0 || packet->Size < CRYPTO_NPUBBYTES || packet->Size > 1000)
|
|
|
|
{
|
2022-01-27 00:43:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-27 00:43:24 +00:00
|
|
|
|
2022-01-26 00:21:27 +00:00
|
|
|
// Data
|
|
|
|
// Nonce|Data
|
|
|
|
// dec
|
2022-01-30 00:53:20 +00:00
|
|
|
// n = nonce, CRYPTO_NPUBBYTES size 16
|
|
|
|
unsigned char *npub = &inBuf[loc];
|
2022-01-26 00:21:27 +00:00
|
|
|
loc += CRYPTO_NPUBBYTES;
|
|
|
|
|
2022-01-30 00:53:20 +00:00
|
|
|
// ciphertext - in, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES
|
|
|
|
int clen = len - loc;
|
|
|
|
unsigned char *c = &inBuf[loc];
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d len:%d loc:%d\n", __LINE__, len, loc);
|
2022-01-30 00:53:20 +00:00
|
|
|
|
|
|
|
// plaintext - out
|
2022-01-26 00:21:27 +00:00
|
|
|
unsigned long long mlen = clen;
|
2022-01-30 00:53:20 +00:00
|
|
|
unsigned char *m = (unsigned char *)calloc(clen, 1);
|
|
|
|
|
2022-01-26 00:21:27 +00:00
|
|
|
// additional text
|
|
|
|
int adlen = sizeof(packet->DataType);
|
2022-01-30 00:53:20 +00:00
|
|
|
unsigned char *ad = (unsigned char *)&packet->DataType;
|
2022-01-26 00:21:27 +00:00
|
|
|
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d mlen:%d clen:%d\n", __LINE__, mlen, clen);
|
2022-01-30 00:53:20 +00:00
|
|
|
int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, key);
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d packet:%d, mlen %d\n", __LINE__, packet->Size, mlen);
|
2022-01-30 00:53:20 +00:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-26 00:21:27 +00:00
|
|
|
memcpy(packet->Data, m, mlen);
|
2022-01-27 22:08:40 +00:00
|
|
|
packet->Size = (UINT16)mlen;
|
2022-01-25 23:01:41 +00:00
|
|
|
}
|
2022-01-30 00:53:20 +00:00
|
|
|
|
2022-01-26 00:21:27 +00:00
|
|
|
free(m);
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
2022-01-30 00:53:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_random(unsigned char* buffer, int length){
|
2022-01-30 22:25:15 +00:00
|
|
|
DEBUG_PRINT("line %d\n", __LINE__);
|
|
|
|
if(rCtx.randcnt == 0){
|
|
|
|
randinit(&rCtx, RANDSIZL);
|
|
|
|
}
|
2022-01-30 00:53:20 +00:00
|
|
|
UINT32 secRandom;
|
|
|
|
for (size_t i = 0; i < length; i += sizeof(UINT32))
|
|
|
|
{
|
|
|
|
secRandom = rand(&rCtx);
|
|
|
|
memcpy(&buffer[i], &secRandom, MIN(sizeof(UINT32), length-i));
|
|
|
|
}
|
2022-01-31 22:08:55 +00:00
|
|
|
}
|