diff --git a/Romulus-M/encrypt_wrapper.c b/Romulus-M/encrypt_wrapper.c index d8fa9c1..9674022 100644 --- a/Romulus-M/encrypt_wrapper.c +++ b/Romulus-M/encrypt_wrapper.c @@ -3,7 +3,10 @@ #include #include #include +#ifdef _WIN32 +#else #include +#endif #include "romulus_m.h" #include "api.h" @@ -13,9 +16,7 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){ return 1; } - int packetSize = packet->Size; - - packet->DataType = __bswap_16(packet->DataType); + int packetSize = packet->Size; // enc // ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES @@ -26,28 +27,26 @@ int encrypt(packet *packet, unsigned char* outBuf, __uint16_t *len){ 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]); + int ret = romulus_m_encrypt(c, &clen, m, mlen, ad, adlen, 0, &npub[0], &k[0]); - if(clen > *len + CRYPTO_NPUBBYTES){ + if(clen > *len + CRYPTO_NPUBBYTES || ret != 0){ free(c); return 1; } // type 0 1 + // 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); @@ -86,12 +85,37 @@ int decrypt(packetEx *packet, unsigned char* inBuf, __uint16_t len){ packet->Size = __bswap_16(packet->Size); packet->Id = __bswap_32(packet->Id); - if(packet->Size <= 1000){ - memcpy(packet->Data, &inBuf[loc], packet->Size); + // Data + // Nonce|Data + // dec + // n = nonce, CRYPTO_NPUBBYTES size 16 + unsigned char* npub = &inBuf[loc]; + loc += CRYPTO_NPUBBYTES; + + // ciphertext - out, MAX 16 bytes larger than plaintext - defined as CRYPTO_ABYTES + int clen = packet->Size - CRYPTO_NPUBBYTES; + unsigned char* c = &inBuf[loc]; + + // plaintext - in + unsigned long long mlen = clen; + unsigned char* m = (unsigned char*)calloc(mlen, 1); + + // additional text + int adlen = sizeof(packet->DataType); + unsigned char* ad = (unsigned char*)&packet->DataType; + + // key, CRYPTO_KEYBYTES size 16 + unsigned char k[CRYPTO_KEYBYTES] = {0}; + + int ret = romulus_m_decrypt(m, &mlen, 0, c, clen, ad, adlen, npub, &k[0]); + if(mlen <= 1000 && ret == 0){ + memcpy(packet->Data, m, mlen); + packet->Size = (u_int16_t)mlen; } else{ + free(m); return 1; } - + free(m); return 0; } \ No newline at end of file