1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-20 10:37:24 -07:00
libsodium/demos/box.c

125 lines
3.6 KiB
C
Raw Normal View History

/*
* GraxRabble
* Demo programs for libsodium.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2015-05-27 04:02:56 -07:00
#include <sodium.h> /* library header */
2015-05-27 04:02:56 -07:00
#include "demo_utils.h" /* utility functions shared by demos */
/*
* Shows how crypto_box works using Bob and Alice with a simple message.
* Both clients must generate their own key pair and swap public key. The
* library will perform Diffie-Hellman to generate a shared key for
* symmetric encryption.
*
* Encrypted messages will be 16 bytes longer because a 16 byte
* authentication token will be prepended to the message.
*
* Note the same nonce must not be used; it should be safe to use a counter.
*/
static int
box(void)
{
2015-05-27 04:02:56 -07:00
unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */
unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */
2015-05-27 04:02:56 -07:00
unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */
unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */
unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */
unsigned char m[MAX_INPUT_SIZE]; /* plaintext */
unsigned char c[MAX_INPUT_SIZE + crypto_box_MACBYTES]; /* ciphertext */
size_t mlen; /* length */
int r;
puts("Example: crypto_box_easy\n");
puts("Generating keypairs...\n");
crypto_box_keypair(bob_pk, bob_sk); /* generate Bob's keys */
crypto_box_keypair(alice_pk, alice_sk); /* generate Alice's keys */
puts("Bob");
fputs("Public: ", stdout);
print_hex(bob_pk, sizeof bob_pk);
putchar('\n');
fputs("Secret: ", stdout);
print_hex(bob_sk, sizeof bob_sk);
putchar('\n');
putchar('\n');
puts("Alice");
fputs("Public: ", stdout);
print_hex(alice_pk, sizeof alice_pk);
putchar('\n');
fputs("Secret: ", stdout);
print_hex(alice_sk, sizeof alice_sk);
putchar('\n');
putchar('\n');
/* nonce must be generated per message, safe to send with message */
puts("Generating nonce...");
randombytes_buf(n, sizeof n);
fputs("Nonce: ", stdout);
print_hex(n, sizeof n);
putchar('\n');
putchar('\n');
2015-05-27 04:02:56 -07:00
/* read input */
2015-05-27 04:02:56 -07:00
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
puts("Notice there is no padding");
print_hex(m, mlen);
putchar('\n');
putchar('\n');
/* encrypt the message */
printf("Encrypting with %s\n\n", crypto_box_primitive());
2014-09-24 17:11:50 -07:00
crypto_box_easy(c, m, mlen, n, alice_pk, bob_sk);
2015-05-27 04:02:56 -07:00
/* sent message */
puts("Bob sending message...\n");
puts("Notice the prepended 16 byte authentication token");
puts("Format: nonce::message");
fputs("Ciphertext: ", stdout);
print_hex(n, sizeof n);
fputs("::", stdout);
print_hex(c, mlen + crypto_box_MACBYTES);
putchar('\n');
putchar('\n');
/* decrypt the message */
puts("Alice opening message...");
2015-05-27 04:02:56 -07:00
r = crypto_box_open_easy(m, c, mlen + crypto_box_MACBYTES, n, bob_pk,
alice_sk);
puts("Notice there is no padding");
print_hex(m, mlen);
putchar('\n');
print_verification(r);
2015-05-27 04:02:56 -07:00
if (r == 0)
printf("Plaintext: %s\n\n", m);
2015-05-27 04:02:56 -07:00
sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */
sodium_memzero(bob_sk, sizeof bob_sk);
sodium_memzero(alice_pk, sizeof alice_pk);
sodium_memzero(alice_sk, sizeof alice_sk);
sodium_memzero(n, sizeof n);
sodium_memzero(m, sizeof m);
sodium_memzero(c, sizeof c);
return r;
}
int
2015-05-27 03:41:43 -07:00
main(void)
{
sodium_init();
printf("Using LibSodium %s\n", sodium_version_string());
2015-05-27 03:41:43 -07:00
return box() != 0;
}