2014-09-24 14:40:21 -07:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
2015-05-27 12:36:54 -07:00
|
|
|
* example programs for libsodium.
|
2014-09-24 14:40:21 -07:00
|
|
|
*/
|
|
|
|
|
2015-05-27 04:02:56 -07:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 12:36:54 -07:00
|
|
|
#include "utils.h" /* utility functions shared by examples */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
/*
|
2015-05-27 07:54:42 -07:00
|
|
|
* Using public-key authenticated encryption, Bob can encrypt a
|
|
|
|
* confidential message specifically for Alice, using Alice's public
|
|
|
|
* key.
|
2014-09-24 14:40:21 -07:00
|
|
|
*
|
2015-05-27 07:54:42 -07:00
|
|
|
* Using Bob's public key, Alice can verify that the encrypted
|
|
|
|
* message was actually created by Bob and was not tampered with,
|
|
|
|
* before eventually decrypting it.
|
2014-09-24 14:40:21 -07:00
|
|
|
*
|
2015-05-27 07:54:42 -07:00
|
|
|
* Alice only needs Bob's public key, the nonce and the ciphertext.
|
|
|
|
* Bob should never ever share his secret key, even with Alice.
|
|
|
|
*
|
|
|
|
* And in order to send messages to Alice, Bob only needs Alice's
|
|
|
|
* public key. Alice should never ever share her secret key either,
|
|
|
|
* even with Bob.
|
|
|
|
*
|
|
|
|
* Alice can reply to Bob using the same system, without having to
|
|
|
|
* generate a distinct key pair.
|
|
|
|
*
|
|
|
|
* The nonce doesn't have to be confidential, but it should be used
|
|
|
|
* with just one invokation of crypto_box_open_easy() for a
|
|
|
|
* particular pair of public and secret keys.
|
|
|
|
*
|
|
|
|
* One easy way to generate a nonce is to use randombytes_buf(),
|
|
|
|
* considering the size of nonces the risk of any random collisions
|
|
|
|
* is negligible. For some applications, if you wish to use nonces to
|
|
|
|
* detect missing messages or to ignore replayed messages, it is also
|
|
|
|
* ok to use a simple incrementing counter as a nonce.
|
|
|
|
*
|
|
|
|
* When doing so you must ensure that the same value can never be
|
|
|
|
* re-used (for example you may have multiple threads or even hosts
|
|
|
|
* generating messages using the same key pairs).
|
|
|
|
*
|
|
|
|
* This system provides mutual authentication. However, a typical use
|
|
|
|
* case is to secure communications between a server, whose public
|
|
|
|
* key is known in advance, and clients connecting anonymously.
|
2014-09-24 14:40:21 -07:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
box(void)
|
|
|
|
{
|
2015-05-27 07:54:42 -07:00
|
|
|
unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob's public key */
|
|
|
|
unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob's secret key */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice's public key */
|
|
|
|
unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice's secret key */
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
unsigned char nonce[crypto_box_NONCEBYTES];
|
2015-05-27 12:19:17 -07:00
|
|
|
unsigned char message[MAX_INPUT_LEN];
|
|
|
|
unsigned char ciphertext[crypto_box_MACBYTES + MAX_INPUT_LEN];
|
2015-05-27 07:54:42 -07:00
|
|
|
size_t message_len;
|
|
|
|
size_t ciphertext_len;
|
|
|
|
int ret;
|
2014-09-24 14:40:21 -07:00
|
|
|
|
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");
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Public key: ");
|
2014-09-24 14:40:21 -07:00
|
|
|
print_hex(bob_pk, sizeof bob_pk);
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Secret key: ");
|
2014-09-24 14:40:21 -07:00
|
|
|
print_hex(bob_sk, sizeof bob_sk);
|
|
|
|
|
|
|
|
puts("Alice");
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Public key: ");
|
2014-09-24 14:40:21 -07:00
|
|
|
print_hex(alice_pk, sizeof alice_pk);
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Secret key: ");
|
2014-09-24 14:40:21 -07:00
|
|
|
print_hex(alice_sk, sizeof alice_sk);
|
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
/* nonce must be unique per (key, message) - it can be public and deterministic */
|
2014-09-24 14:40:21 -07:00
|
|
|
puts("Generating nonce...");
|
2015-05-27 07:54:42 -07:00
|
|
|
randombytes_buf(nonce, sizeof nonce);
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Nonce: ");
|
2015-05-27 07:54:42 -07:00
|
|
|
print_hex(nonce, sizeof nonce);
|
2015-05-27 04:02:56 -07:00
|
|
|
|
2014-09-24 14:40:21 -07:00
|
|
|
/* read input */
|
2015-05-27 09:10:28 -07:00
|
|
|
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
2015-05-27 04:02:56 -07:00
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
print_hex(message, message_len);
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
/* encrypt and authenticate the message */
|
|
|
|
printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive());
|
|
|
|
crypto_box_easy(ciphertext, message, message_len, nonce, alice_pk, bob_sk);
|
|
|
|
ciphertext_len = crypto_box_MACBYTES + message_len;
|
2015-05-27 04:02:56 -07:00
|
|
|
|
2015-05-27 08:24:20 -07:00
|
|
|
/* send the nonce and the ciphertext */
|
|
|
|
puts("Bob sends the nonce and the ciphertext...\n");
|
2015-05-27 07:54:42 -07:00
|
|
|
printf("Ciphertext len: %zu bytes - Original message length: %zu bytes\n",
|
|
|
|
ciphertext_len, message_len);
|
2015-05-27 07:57:51 -07:00
|
|
|
puts("Notice the prepended 16 byte authentication token\n");
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Nonce: ");
|
2015-05-27 12:35:10 -07:00
|
|
|
print_hex(nonce, sizeof nonce);
|
2015-05-27 12:09:32 -07:00
|
|
|
printf("Ciphertext: ");
|
2015-05-27 07:54:42 -07:00
|
|
|
print_hex(ciphertext, ciphertext_len);
|
2014-09-24 14:40:21 -07:00
|
|
|
|
|
|
|
/* decrypt the message */
|
2015-05-27 07:54:42 -07:00
|
|
|
puts("Alice verifies and decrypts the ciphertext...");
|
|
|
|
ret = crypto_box_open_easy(message, ciphertext, ciphertext_len, nonce, bob_pk,
|
|
|
|
alice_sk);
|
|
|
|
print_hex(message, message_len);
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 07:54:42 -07:00
|
|
|
print_verification(ret);
|
2015-05-27 11:05:15 -07:00
|
|
|
if (ret == 0) {
|
|
|
|
printf("Plaintext: ");
|
|
|
|
fwrite(message, 1U, message_len, stdout);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2015-05-27 06:46:17 -07:00
|
|
|
sodium_memzero(bob_sk, sizeof bob_sk); /* wipe sensitive data */
|
2014-09-24 14:40:21 -07:00
|
|
|
sodium_memzero(alice_sk, sizeof alice_sk);
|
2015-05-27 07:54:42 -07:00
|
|
|
sodium_memzero(message, sizeof message);
|
|
|
|
sodium_memzero(ciphertext, sizeof ciphertext);
|
|
|
|
|
|
|
|
return ret;
|
2014-09-24 14:40:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 03:41:43 -07:00
|
|
|
main(void)
|
2014-09-24 14:40:21 -07:00
|
|
|
{
|
2015-05-27 07:10:07 -07:00
|
|
|
init();
|
2014-09-24 14:40:21 -07:00
|
|
|
|
2015-05-27 03:41:43 -07:00
|
|
|
return box() != 0;
|
2014-09-24 14:40:21 -07:00
|
|
|
}
|