#include <sodium.h>
#include "utils.h"
static int
box_detached(void)
{
unsigned char bob_pk[crypto_box_PUBLICKEYBYTES];
unsigned char bob_sk[crypto_box_SECRETKEYBYTES];
unsigned char alice_pk[crypto_box_PUBLICKEYBYTES];
unsigned char alice_sk[crypto_box_SECRETKEYBYTES];
unsigned char nonce[crypto_box_NONCEBYTES];
unsigned char message[MAX_INPUT_LEN];
unsigned char mac[crypto_box_MACBYTES];
unsigned char ciphertext[MAX_INPUT_LEN];
size_t message_len;
int ret;
puts("Example: crypto_box_detached\n");
puts("Generating keypairs...\n");
crypto_box_keypair(bob_pk, bob_sk);
crypto_box_keypair(alice_pk, alice_sk);
puts("Bob");
printf("Public key: ");
print_hex(bob_pk, sizeof bob_pk);
printf("Secret key: ");
print_hex(bob_sk, sizeof bob_sk);
puts("Alice");
printf("Public key: ");
print_hex(alice_pk, sizeof alice_pk);
printf("Secret key: ");
print_hex(alice_sk, sizeof alice_sk);
puts("Generating nonce...");
randombytes_buf(nonce, sizeof nonce);
printf("Nonce: ");
print_hex(nonce, sizeof nonce);
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
print_hex(message, message_len);
printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive());
crypto_box_detached(ciphertext, mac, message, message_len, nonce,
alice_pk, bob_sk);
puts("Bob sends the nonce, the MAC and the ciphertext...\n");
printf("Nonce: ");
print_hex(nonce, sizeof nonce);
printf("MAC: ");
print_hex(mac, sizeof mac);
printf("Ciphertext: ");
print_hex(ciphertext, message_len);
puts("Alice verifies the MAC and decrypts the ciphertext...");
ret = crypto_box_open_detached(message, ciphertext, mac, message_len, nonce,
bob_pk, alice_sk);
print_hex(message, message_len);
print_verification(ret);
if (ret == 0) {
printf("Plaintext: ");
fwrite(message, 1U, message_len, stdout);
putchar('\n');
}
sodium_memzero(bob_sk, sizeof bob_sk);
sodium_memzero(alice_sk, sizeof alice_sk);
sodium_memzero(message, sizeof message);
sodium_memzero(ciphertext, sizeof ciphertext);
return ret;
}
int
main(void)
{
init();
return box_detached() != 0;
}