}
SConnect(fd, server);
setTCPNoDelay(fd);
- SodiumNonce theirs, ours;
+ SodiumNonce theirs, ours, readingNonce, writingNonce;
ours.init();
writen2(fd, (const char*)ours.value, sizeof(ours.value));
readn2(fd, (char*)theirs.value, sizeof(theirs.value));
+ readingNonce.merge(ours, theirs);
+ writingNonce.merge(theirs, ours);
if(!command.empty()) {
- string msg=sodEncryptSym(command, g_key, ours);
+ string msg=sodEncryptSym(command, g_key, writingNonce);
putMsgLen32(fd, (uint32_t) msg.length());
if(!msg.empty())
writen2(fd, msg);
boost::scoped_array<char> resp(new char[len]);
readn2(fd, resp.get(), len);
msg.assign(resp.get(), len);
- msg=sodDecryptSym(msg, g_key, theirs);
+ msg=sodDecryptSym(msg, g_key, readingNonce);
cout<<msg;
cout.flush();
}
if(line.empty())
continue;
- string msg=sodEncryptSym(line, g_key, ours);
+ string msg=sodEncryptSym(line, g_key, writingNonce);
putMsgLen32(fd, (uint32_t) msg.length());
writen2(fd, msg);
uint32_t len;
boost::scoped_array<char> resp(new char[len]);
readn2(fd, resp.get(), len);
msg.assign(resp.get(), len);
- msg=sodDecryptSym(msg, g_key, theirs);
+ msg=sodDecryptSym(msg, g_key, readingNonce);
cout<<msg;
cout.flush();
}
try
{
setTCPNoDelay(fd);
- SodiumNonce theirs;
- readn2(fd, (char*)theirs.value, sizeof(theirs.value));
- SodiumNonce ours;
+ SodiumNonce theirs, ours, readingNonce, writingNonce;
ours.init();
+ readn2(fd, (char*)theirs.value, sizeof(theirs.value));
writen2(fd, (char*)ours.value, sizeof(ours.value));
+ readingNonce.merge(ours, theirs);
+ writingNonce.merge(theirs, ours);
for(;;) {
uint32_t len;
readn2(fd, msg.get(), len);
string line(msg.get(), len);
- line = sodDecryptSym(line, g_key, theirs);
+ line = sodDecryptSym(line, g_key, readingNonce);
// cerr<<"Have decrypted line: "<<line<<endl;
string response;
try {
catch(const LuaContext::SyntaxErrorException& e) {
response = "Error: " + string(e.what()) + ": ";
}
- response = sodEncryptSym(response, g_key, ours);
+ response = sodEncryptSym(response, g_key, writingNonce);
putMsgLen32(fd, response.length());
writen2(fd, response.c_str(), response.length());
}
#ifndef HAVE_LIBSODIUM
struct SodiumNonce
{
- void init(){};
- void increment(){};
- unsigned char value[1];
+ void init(){};
+ void merge(const SodiumNonce& lower, const SodiumNonce& higher) {};
+ void increment(){};
+ unsigned char value[1];
};
#else
#include <sodium.h>
{
randombytes_buf(value, sizeof value);
}
-
+
+ void merge(const SodiumNonce& lower, const SodiumNonce& higher)
+ {
+ static const size_t halfSize = (sizeof value) / 2;
+ memcpy(value, lower.value, halfSize);
+ memcpy(value + halfSize, higher.value + halfSize, halfSize);
+ }
+
void increment()
{
uint32_t* p = (uint32_t*)value;
sock.send(ourNonce)
theirNonce = sock.recv(len(ourNonce))
- msg = cls._encryptConsole(command, ourNonce)
+ halfNonceSize = len(ourNonce) / 2
+ readingNonce = ourNonce[0:halfNonceSize] + theirNonce[halfNonceSize:]
+ writingNonce = theirNonce[0:halfNonceSize] + ourNonce[halfNonceSize:]
+
+ msg = cls._encryptConsole(command, writingNonce)
sock.send(struct.pack("!I", len(msg)))
sock.send(msg)
data = sock.recv(4)
(responseLen,) = struct.unpack("!I", data)
data = sock.recv(responseLen)
- response = cls._decryptConsole(data, theirNonce)
+ response = cls._decryptConsole(data, readingNonce)
return response