2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
|
|
|
* Demo programs for libsodium.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include "demo_utils.h" /* utility functions shared by demos */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Stream utilizes a nonce to generate a sequence of bytes. The library has
|
|
|
|
* an internal function which XOR data and the stream into an encrypted result.
|
2015-05-27 07:02:56 -04:00
|
|
|
*
|
2014-09-24 17:40:21 -04:00
|
|
|
* Note that this method does not supply authentication. Try secretbox instead.
|
|
|
|
*
|
|
|
|
* Note that nonce must be different for each message since it provides
|
|
|
|
* change between each operation. It should be safe to use a counter
|
|
|
|
* instead of purely random data each time.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
stream(void)
|
|
|
|
{
|
2015-05-27 07:02:56 -04:00
|
|
|
unsigned char k[crypto_stream_KEYBYTES]; /* secret key */
|
|
|
|
unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */
|
2015-05-27 09:39:34 -04:00
|
|
|
unsigned char m[MAX_INPUT_SIZE]; /* plain-text */
|
|
|
|
unsigned char c[MAX_INPUT_SIZE]; /* cipher-text */
|
2015-05-27 07:02:56 -04:00
|
|
|
size_t mlen; /* length */
|
2014-09-24 17:40:21 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
puts("Example: crypto_stream\n");
|
|
|
|
|
|
|
|
sodium_memzero(k, sizeof k);
|
2015-05-27 07:02:56 -04:00
|
|
|
prompt_input("Input your key > ", (char*)k, sizeof k);
|
2014-09-24 17:40:21 -04:00
|
|
|
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 07:02:56 -04:00
|
|
|
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
printf("Encrypting with (xor) %s\n", crypto_stream_primitive());
|
|
|
|
crypto_stream_xor(c, m, mlen, n, k);
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
puts("Sending message...");
|
|
|
|
puts("Format: nonce::message");
|
|
|
|
fputs("Ciphertext: ", stdout);
|
|
|
|
print_hex(n, sizeof n);
|
|
|
|
fputs("::", stdout);
|
|
|
|
print_hex(c, mlen);
|
|
|
|
putchar('\n');
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
puts("Opening message...");
|
|
|
|
r = crypto_stream_xor(m, c, mlen, n, k);
|
|
|
|
|
|
|
|
print_verification(r);
|
2015-05-27 07:02:56 -04:00
|
|
|
if (r == 0)
|
|
|
|
printf("Plaintext: %s\n\n", m);
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
sodium_memzero(k, sizeof k); /* wipe sensitive data */
|
2014-09-24 17:40:21 -04:00
|
|
|
sodium_memzero(n, sizeof n);
|
|
|
|
sodium_memzero(m, sizeof m);
|
|
|
|
sodium_memzero(c, sizeof c);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
|
|
|
sodium_init();
|
|
|
|
printf("Using LibSodium %s\n", sodium_version_string());
|
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return stream() != 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|