Libsodium - use crypto_box_easy where receiver and sender are the same

93 Views Asked by At

I would like to use libsodium to encrypt little messages/secrets and share them among different users. The API is straightforward in case the receiver and sender are different. But what happens, when I want to allow as well, that the user encrypts for himself things and stores them inside of a cloud for example?

#define MESSAGE (const unsigned char *) "test"
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_box_MACBYTES + MESSAGE_LEN)

unsigned char alice_publickey[crypto_box_PUBLICKEYBYTES];
unsigned char alice_secretkey[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(alice_publickey, alice_secretkey);

unsigned char nonce[crypto_box_NONCEBYTES];
unsigned char ciphertext[CIPHERTEXT_LEN];
randombytes_buf(nonce, sizeof nonce);
if (crypto_box_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce,
                    alice_publickey, alice_secretkey) != 0) {
    /* error */
}

unsigned char decrypted[MESSAGE_LEN];
if (crypto_box_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce,
                         alice_publickey, alice_secretkey) != 0) {
    /* message for Bob pretending to be from Alice has been forged! */
}

Is that compromising the algorithm under the hood? Because Diffie-Hellman is used in that case and from my perspective, at least Diffie-Hellman was not designed for this kind of use case, I am concerned. And I can't find any hint if it is forbidden or allowed.

1

There are 1 best solutions below

0
Trafo On

It seems to be possible because mathematically it is not weakening the algorithm. Source: https://crypto.stackexchange.com/questions/103925/ecdh-between-identical-public-keys