SEAL icon indicating copy to clipboard operation
SEAL copied to clipboard

Transporting SEAL objects over sockets -- Converting SEAL objects to char and vice versa

Open iammrgenie opened this issue 2 years ago • 5 comments

Hi,

I am working on a client-server application using SEAL and sockets. In the application, I generate SEAL objects on the server and transport these objects to the client over sockets. I can successfully convert the EncryptionParameters value to char and reverse the process on the client without any issues. However, when I save a PublicKey object and transport it, I get an error trying to load it back as a PublicKey object. Here are snippets of the code I use. Any better approach or any particular reason why my code is failing?

--- Server ---- EncryptionParamters conversion

stringstream parms_stream;
auto size = parms.save(parms_stream);
string parms_string = parms_stream.str();
send(socketClient, parms_string.data(), size + 1, 0);

PublicKey conversion

stringstream pk_stream;
auto pk_size = he_pk.save(pk_stream);
string pk_string = pk_stream.str();

char key_length[10];
sprintf(key_length, "%ld", [k_size);
//Key size
send(socketClient, key_length, sizeof(key_length) + 1, 0);
//Send the public key
send(socketClient, pk_string.data(), pk_size, 0);

---- Client ---

stringstream parms_stream;
stringstream pk_stream;
//Retrieving the EncryptionParameters
string parms_string;
char parms_char[102];
recv(clientSocket, parms_char, sizeof(parms_char), 0);
for (int i = 0; i < sizeof(parms_char); i++){
       parms_string.push_back(parms_char[i]);
}
//Deserialize from Parms String to SEAL object
parms_stream << parms_string;
EncryptionParameters parms;
parms.load(parms_stream);
print_parameters(parms);
SEALContext context(parms);

//Retrieving Public Key
//Receive size of incoming Key
char key_length[10];
recv(clientSocket, key_length, sizeof(key_length) + 1, 0);

long int pk_size;
sscanf(key_length + 1, "%ld", &pk_size);

//Receive incoming Key
char pk_char[pk_size];
string pk_string;
recv(clientSocket, pk_char, sizeof(pk_char), 0);
for (int j = 0; j < sizeof(pk_char); j ++){
       pk_string.push_back(pk_char[j]);
 }

//Deserialize from String to SEAL object
pk_stream << pk_string;

//Load Key
PublicKey he_pk;
he_pk.load(context, pk_stream);

Parameters work fine, but I get this error for the PublicKey part. Any advice?

terminate called after throwing an instance of 'std::logic_error' what(): incompatible version Aborted (core dumped)

iammrgenie avatar Sep 29 '22 08:09 iammrgenie

"what(): incompatible version"

make sure all your machines are installed with the same version of SEAL.

fionser avatar Sep 30 '22 06:09 fionser

I am running both applications on the same machine. So the version of SEAL is consistent. Is the approach for converting and reloading acceptable? I tried with a SecretKey object as well and I got the same error.

iammrgenie avatar Sep 30 '22 06:09 iammrgenie

I have a hunch that some information is corrupted during stringstream --> string --> char array --> string --> stringstream conversions. Can you try saving and loading a PublicKey or SecretKey without char array as an intermediate step? That's how you got EncryptionParameters to work.

WeiDaiWD avatar Sep 30 '22 18:09 WeiDaiWD

I did the same thing I did for the EncryptionParameters as I did for the PublicKey and SecretKey. I can't seem to be able to send strings across the network without first converting to char.

iammrgenie avatar Oct 03 '22 12:10 iammrgenie

Don't involve networking then since you are debugging.

WeiDaiWD avatar Oct 06 '22 17:10 WeiDaiWD