2014-09-24 14:40:21 -07:00
|
|
|
/*
|
|
|
|
* 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 */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 04:02:56 -07:00
|
|
|
#include "demo_utils.h" /* utility functions shared by demos */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
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 */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 06:39:34 -07:00
|
|
|
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 */
|
2014-09-24 14:40:21 -07:00
|
|
|
int r;
|
|
|
|
|
2014-09-24 17:08:31 -07:00
|
|
|
puts("Example: crypto_box_easy\n");
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-24 14:40:21 -07:00
|
|
|
/* read input */
|
2015-05-27 04:02:56 -07:00
|
|
|
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
|
|
|
|
|
2014-09-24 17:08:31 -07:00
|
|
|
puts("Notice there is no padding");
|
|
|
|
print_hex(m, mlen);
|
2014-09-24 14:40:21 -07:00
|
|
|
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
|
|
|
|
2014-09-24 14:40:21 -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);
|
2014-09-24 17:08:31 -07:00
|
|
|
print_hex(c, mlen + crypto_box_MACBYTES);
|
2014-09-24 14:40:21 -07:00
|
|
|
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);
|
2014-09-24 17:08:31 -07:00
|
|
|
|
|
|
|
puts("Notice there is no padding");
|
|
|
|
print_hex(m, mlen);
|
2014-09-24 14:40:21 -07:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
print_verification(r);
|
2015-05-27 04:02:56 -07:00
|
|
|
if (r == 0)
|
|
|
|
printf("Plaintext: %s\n\n", m);
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 04:02:56 -07:00
|
|
|
sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */
|
2014-09-24 14:40:21 -07:00
|
|
|
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)
|
2014-09-24 14:40:21 -07:00
|
|
|
{
|
|
|
|
sodium_init();
|
|
|
|
printf("Using LibSodium %s\n", sodium_version_string());
|
|
|
|
|
2015-05-27 03:41:43 -07:00
|
|
|
return box() != 0;
|
2014-09-24 14:40:21 -07:00
|
|
|
}
|