简体   繁体   中英

Libsodium - use crypto_box_easy where receiver and sender are the same

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 Hellmann is used in that case and from my perspective, at least Diffie Hellmann 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM