Before we begin here, I have a server and a client. I wish to send an encrypted string to the client that contains the server's Diffie-Hellman public static key and public ephemeral key. To do so, I use the server's private RSA key to send the encrypted string and the client decrypts with the server's public RSA key.
Now the reason why I am needing to do it this way is because the server is the only one with a public/private key pair. This is fine since encrypting with one key pair still cuts off one side of the MITM attack against Diffie-Hellman, and for my requirements, it is ok.
Converting the static and ephemeral keys into a hex encoded string and sending it over the socket is giving me a problem with the private key encryption stage.
my server is doing:
DH2 dhA(dh);
SecByteBlock sprivA(dhA.StaticPrivateKeyLength()), spubA(
dhA.StaticPublicKeyLength());
SecByteBlock eprivA(dhA.EphemeralPrivateKeyLength()), epubA(
dhA.EphemeralPublicKeyLength());
dhA.GenerateStaticKeyPair(rnd, sprivA, spubA);
dhA.GenerateEphemeralKeyPair(rnd, eprivA, epubA);
string sendBuf, recvBuf;
string saEncoded, eaEncoded, encoding;
cout << "spubA: " << (char*) spubA.data() << endl << "epubA: "
<< (char*) epubA.data() << endl;
SecByteBlock nil;
nil.CleanNew(HMAC< SHA256 >::DEFAULT_KEYLENGTH);
HMAC< SHA256 > hmac;
hmac.SetKey(nil.data(), nil.size());
HashFilter filter(hmac, new HexEncoder(new StringSink(encoding)));
filter.Put(spubA.data(), spubA.size());
filter.MessageEnd();
saEncoded = encoding;
encoding = "";
filter.Put(epubA.data(), epubA.size());
filter.MessageEnd();
eaEncoded = encoding;
encoding = "";
// StringSource saSource(spubA, sizeof(spubA), true,
// new HexEncoder(new StringSink(saEncoded)));
//
// StringSource eaSource(epubA, sizeof(epubA), true,
// new HexEncoder(new StringSink(eaEncoded)));
//
sendBuf = saEncoded + " " + eaEncoded;
cout << "Send Buffer: " << sendBuf << endl;
SendMsg(sendBuf, tdata);
where SendMsg()
contains the encryption process.
It fails at this point:
void SendMsg( string sendBuf, struct ThreadData * tdata )
{
AutoSeededRandomPool rng;
Integer m, c, r;
stringstream ss;
try
{
// Encode the message as an Integer
m = Integer((const byte *) sendBuf.c_str(), sendBuf.size());
//Encrypt
c = tdata->privateKey.CalculateInverse(rng, m); //HERE!
With error message:
InvertibleRSAFunction: computational error during private key operation
The code that is currently not commented out in the Diffie-Hellman secition was obtained from HERE. The problem with the commented code is that when the client receives the hex encoded string, it has lost data and cannot agree on a shared secret. But it does get through the socket.
An example of this can be shown:
Server:
spubA: &a�|՜D2�tu�cJ����B�R�8�*i�x?N���p��Q�����K��+O �"��P:k�d|3�����6Z
epubA: 4v������M�E�`l�K��[dN�|Q^r�-ż�����A~D�>4$�9���"v�*:Y��s�O���J��ow�M�߬�C�9n�;���Z�D�6lp�V��oowZ��WSv��",��A3��XL��8��
Send Buffer: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
Client:
Recovered: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
SA: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738
EA: 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
Decoded SA: &a�|՜D2�tu�cJ����B�R�8
Decoded EA: 4v������M�E�`l�K��[dN
With respect to this instance, I tried on the client side to do the following:
// Get spubA and epubA from server
recovered = recoverMsg(serverKey, sockServer);
//Calculate shared secret.
string sa, ea;
ss.str(recovered);
ss >> sa >> ea;
ss.str("");
ss.clear();
cout << "SA: " << sa << endl << "EA: " << ea << endl;
string decodedSA, decodedEA;
StringSource decodeSA(sa, true,
new HexDecoder(new StringSink(decodedSA)));
StringSource decodeEA(ea, true,
new HexDecoder(new StringSink(decodedEA)));
cout << "Decoded SA: " << decodedSA << endl;
cout << "Decoded EA: " << decodedEA << endl;
SecByteBlock spubA((const byte*) decodedSA.data(), decodedSA.size());
if ( spubA.size() < dhB.StaticPublicKeyLength() ) spubA.CleanGrow(
dhB.StaticPublicKeyLength());
else spubA.resize(dhB.StaticPublicKeyLength());
SecByteBlock epubA((const byte*) decodedEA.data(), decodedEA.size());
if ( epubA.size() < dhB.EphemeralPublicKeyLength() ) epubA.CleanGrow(
dhB.EphemeralPublicKeyLength());
else epubA.resize(dhB.EphemeralPublicKeyLength());
But I still get the same result.
Does anybody know how I can encrypt this with the server's private key and send it across the socket correctly?