From c78c9785929751e0722d94ded760889927ef429d Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Wed, 24 Sep 2014 17:40:21 -0400 Subject: [PATCH 01/69] added gist examples into demos folder; you can figure out where this belongs --- demos/.gitignore | 11 +++ demos/auth.c | 68 +++++++++++++++++ demos/box.c | 157 ++++++++++++++++++++++++++++++++++++++ demos/demo_utils.c | 66 ++++++++++++++++ demos/demo_utils.h | 22 ++++++ demos/generichash.c | 57 ++++++++++++++ demos/generichashstream.c | 66 ++++++++++++++++ demos/hash.c | 49 ++++++++++++ demos/onetimeauth.c | 71 +++++++++++++++++ demos/secretbox.c | 110 ++++++++++++++++++++++++++ demos/shorthash.c | 54 +++++++++++++ demos/sign.c | 86 +++++++++++++++++++++ demos/stream.c | 90 ++++++++++++++++++++++ 13 files changed, 907 insertions(+) create mode 100644 demos/.gitignore create mode 100644 demos/auth.c create mode 100644 demos/box.c create mode 100644 demos/demo_utils.c create mode 100644 demos/demo_utils.h create mode 100644 demos/generichash.c create mode 100644 demos/generichashstream.c create mode 100644 demos/hash.c create mode 100644 demos/onetimeauth.c create mode 100644 demos/secretbox.c create mode 100644 demos/shorthash.c create mode 100644 demos/sign.c create mode 100644 demos/stream.c diff --git a/demos/.gitignore b/demos/.gitignore new file mode 100644 index 00000000..9b7faddc --- /dev/null +++ b/demos/.gitignore @@ -0,0 +1,11 @@ +auth +box +generhash +generhashstream +hash +Makefile +onetimeauth +sretbox +shorthash +sign +stream diff --git a/demos/auth.c b/demos/auth.c new file mode 100644 index 00000000..d801f08b --- /dev/null +++ b/demos/auth.c @@ -0,0 +1,68 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Full featured authentication which is used to verify that the message + * comes from the expected person. It should be safe to keep the same key + * for multiple messages. + */ +static int +auth(void) +{ + unsigned char k[crypto_auth_KEYBYTES]; /* key */ + unsigned char a[crypto_auth_BYTES]; /* authentication token */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* message length */ + int r; + + sodium_memzero(k, sizeof k); /* must zero the key */ + + puts("Example: crypto_auth\n"); + + prompt_input("Input your key > ", (char*) k, sizeof k); + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + putchar('\n'); + + printf("Generating %s authentication...\n", crypto_auth_primitive()); + crypto_auth(a, m, mlen, k); + + puts("Format: authentication token::message"); + print_hex(a, sizeof a); + fputs("::", stdout); + puts((const char*) m); + putchar('\n'); + + puts("Verifying authentication..."); + r = crypto_auth_verify(a, m, mlen, k); + print_verification(r); + + sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(a, sizeof a); + sodium_memzero(m, sizeof m); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == auth() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + diff --git a/demos/box.c b/demos/box.c new file mode 100644 index 00000000..5ebd2014 --- /dev/null +++ b/demos/box.c @@ -0,0 +1,157 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#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. + * + * Note that crypto_box uses padding at the start of both messages. + * The padding must be zero else the encryption or decryption will fail. + * + * 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) +{ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ + unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ + + unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ + unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ + unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ + + unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ + unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES];/* plaintext */ + unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES];/* ciphertext */ + size_t mlen; /* length */ + int r; + + puts("Example: crypto_box\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'); + + /* perform diffie hellman */ + crypto_box_beforenm(bob_ss, alice_pk, bob_sk); + crypto_box_beforenm(alice_ss, bob_pk, alice_sk); + fputs("Bob shared key: ", stdout); + print_hex(bob_ss, sizeof bob_ss); + putchar('\n'); + fputs("Alice shared key: ", stdout); + print_hex(alice_ss, sizeof alice_ss); + 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'); + + /* read input */ + mlen = prompt_input("Input your message > ", + (char*) m + crypto_box_ZEROBYTES, + sizeof m - crypto_box_ZEROBYTES); + + /* must zero at least the padding */ + sodium_memzero(m, crypto_box_ZEROBYTES); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + /* encrypt the message */ + printf("Encrypting with %s\n\n", crypto_box_primitive()); + crypto_box_afternm(c, m, mlen + crypto_box_ZEROBYTES, n, bob_ss); + + /* 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_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + /* decrypt the message */ + puts("Alice opening message..."); + + /* must zero at least the padding */ + sodium_memzero(c, crypto_box_BOXZEROBYTES); + r = crypto_box_open_afternm( + m, c, mlen + crypto_box_ZEROBYTES, + n, alice_ss); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + + print_verification(r); + if (r == 0) printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); + + sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ + sodium_memzero(bob_sk, sizeof bob_sk); + sodium_memzero(bob_ss, sizeof bob_ss); + sodium_memzero(alice_pk, sizeof alice_pk); + sodium_memzero(alice_sk, sizeof alice_sk); + sodium_memzero(alice_ss, sizeof alice_ss); + sodium_memzero(n, sizeof n); + sodium_memzero(m, sizeof m); + sodium_memzero(c, sizeof c); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == box() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + diff --git a/demos/demo_utils.c b/demos/demo_utils.c new file mode 100644 index 00000000..1387b10d --- /dev/null +++ b/demos/demo_utils.c @@ -0,0 +1,66 @@ +/* + * These are the utility functions shared by all demo programs. + */ +#include +#include +#include + +#include "sodium.h" /* library header */ + +#include "demo_utils.h" /* demo utility header */ + + + +/* ================================================================== * + * utility functions * + * ================================================================== */ + +/* + * Print hex. + */ +void +print_hex(const void *buf, const size_t len) +{ + const unsigned char *b; + char *p; + + b = buf; + p = malloc((len * 2 + 1) * (sizeof *b)); + + /* the library supplies a few utility functions like the one below */ + sodium_bin2hex(p, len * 2 + 1, b, len); + fputs(p, stdout); + free(p); +} + +/* + * Display a prompt for input by user. It will save the input into a buffer + * of a specific size with room for the null terminator while removing + * trailing newline characters. + */ +size_t +prompt_input(char *prompt, char *buf, const size_t len) +{ + size_t n; + + fputs(prompt, stdout); + fgets(buf, len, stdin); /* grab input with room for NULL */ + + n = strlen(buf); + if (buf[n - 1] == '\n') { /* trim excess new line */ + buf[n - 1] = '\0'; + --n; + } + return n; +} + +/* + * Print a message if the function was sucessful or failed. + */ +void +print_verification(int r) +{ + if (r == 0) puts("Success\n"); + else puts("Failure\n"); +} + diff --git a/demos/demo_utils.h b/demos/demo_utils.h new file mode 100644 index 00000000..ae35d8d7 --- /dev/null +++ b/demos/demo_utils.h @@ -0,0 +1,22 @@ +/* + * Utility functions shared by all the demo programs. + */ +#ifndef DEMO_UTILS_H +#define DEMO_UTILS_H + + +#include + + +#define BUFFER_SIZE 128 /* size of all input buffers in the demo */ + + + +void print_hex(const void *buf, const size_t len); +size_t prompt_input(char *prompt, char *buf, const size_t len); +void print_verification(int r); + + + +#endif /* DEMO_UTILS_H */ + diff --git a/demos/generichash.c b/demos/generichash.c new file mode 100644 index 00000000..a8b2d435 --- /dev/null +++ b/demos/generichash.c @@ -0,0 +1,57 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Generic hash is intended as a variable output hash with enough strength + * to ensure data integrity. The hash out put is also able to vary in size. + * Key is optional and is able to vary in size. + * + * Note that it is recommended to stay within the range of MIN and MAX + * output because larger output will produce gaps. + */ +void +generichash(void) +{ + unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ + unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ + + puts("Example: crypto_generichash\n"); + + sodium_memzero(k, sizeof k); + prompt_input("Input your key > ", (char*) k, sizeof k); + + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_generichash_primitive()); + crypto_generichash(h, sizeof h, m, mlen, k, sizeof k); + fputs("Hash: ", stdout); + print_hex(h, sizeof h); + putchar('\n'); + putchar('\n'); +} + +int +main(int argc, char **argv) +{ + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + generichash(); + exit(EXIT_SUCCESS); +} + diff --git a/demos/generichashstream.c b/demos/generichashstream.c new file mode 100644 index 00000000..63d7a2e6 --- /dev/null +++ b/demos/generichashstream.c @@ -0,0 +1,66 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Streaming variant of generic hash. This has the ability to hash + * data in chunks at a time and compute the same result as hashing + * all of the data at once. + */ +void +generichashstream(void) +{ + unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ + unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ + crypto_generichash_state state; /* hash stream */ + unsigned char m[BUFFER_SIZE]; /* input buffer */ + size_t mlen; /* input length */ + + puts("Example: crypto_generichashstream\n"); + + sodium_memzero(k, sizeof k); + prompt_input("Input your key > ", (char*) k, sizeof k); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_generichash_primitive()); + + /* initialize the stream */ + crypto_generichash_init(&state, k, sizeof k, sizeof h); + + while (1) { + mlen = prompt_input("> ", (char*) m, sizeof m); + if (mlen == 0) break; + + /* keep appending data */ + crypto_generichash_update(&state, m, mlen); + } + crypto_generichash_final(&state, h, sizeof h); + putchar('\n'); + + fputs("Hash: ", stdout); + print_hex(h, sizeof h); + putchar('\n'); + putchar('\n'); +} + +int +main(int argc, char **argv) +{ + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + generichashstream(); + exit(EXIT_SUCCESS); +} + diff --git a/demos/hash.c b/demos/hash.c new file mode 100644 index 00000000..167a3b26 --- /dev/null +++ b/demos/hash.c @@ -0,0 +1,49 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * The library ships with a one-shot SHA-512 implementation. Simply allocate + * all desired data into a single continuous buffer. + */ +static void +hash(void) +{ + unsigned char h[crypto_hash_BYTES]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ + + puts("Example: crypto_hash\n"); + + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_hash_primitive()); + crypto_hash(h, m, mlen); + fputs("Hash: ", stdout); + print_hex(h, sizeof h); + putchar('\n'); + putchar('\n'); +} + +int +main(int argc, char **argv) +{ + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + hash(); + exit(EXIT_SUCCESS); +} + diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c new file mode 100644 index 00000000..c9b47189 --- /dev/null +++ b/demos/onetimeauth.c @@ -0,0 +1,71 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * This method is only effective for a single use per key. The benefit is + * the algorithm is quicker and output is half the size of auth. It is easy + * to see how weak the algorithm is when you use a one letter key. + * + * Note that the same key must not be used more than once. + */ +static int +onetimeauth(void) +{ + unsigned char k[crypto_onetimeauth_KEYBYTES];/* key */ + unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* message length */ + int r; + + sodium_memzero(k, sizeof k); /* must zero the key */ + + puts("Example: crypto_onetimeauth\n"); + + prompt_input("Input your key > ", (char*) k, sizeof k); + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + putchar('\n'); + + printf("Generating %s authentication...\n", + crypto_onetimeauth_primitive()); + crypto_onetimeauth(a, m, mlen, k); + + puts("Format: authentication token::message"); + print_hex(a, sizeof a); + fputs("::", stdout); + puts((const char*) m); + putchar('\n'); + + puts("Verifying authentication..."); + r = crypto_onetimeauth_verify(a, m, mlen, k); + print_verification(r); + + sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(a, sizeof a); + sodium_memzero(m, sizeof m); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == onetimeauth() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + diff --git a/demos/secretbox.c b/demos/secretbox.c new file mode 100644 index 00000000..4a043f24 --- /dev/null +++ b/demos/secretbox.c @@ -0,0 +1,110 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * This is a wrapper around stream which does XOR automatically. + * + * Note that the buffer must be padded at the front. The same nonce must + * not be used; it should be safe to use a counter. + * + * Encrypted messages will be 16 bytes longer because a 16 byte + * authentication token will be prepended to the message. + */ +static int +secretbox(void) +{ + unsigned char k[crypto_secretbox_KEYBYTES]; /* secret */ + unsigned char n[crypto_secretbox_NONCEBYTES]; /* nonce */ + unsigned char m[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* plain */ + unsigned char c[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* cipher */ + size_t mlen; /* length */ + int r; + + puts("Example: crypto_secretbox\n"); + + sodium_memzero(k, sizeof k); + prompt_input("Input your key > ", (char*) k, sizeof k); + + /* 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'); + + mlen = prompt_input("Input your message > ", + (char*) m + crypto_secretbox_ZEROBYTES, + sizeof m - crypto_secretbox_ZEROBYTES); + + /* must zero at least the padding */ + sodium_memzero(m, crypto_secretbox_ZEROBYTES); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + + /* encrypting message */ + printf("Encrypting with %s\n", crypto_secretbox_primitive()); + + crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k); + putchar('\n'); + + puts("Notice the prepended 16 byte authentication token"); + puts("Sending message..."); + puts("Format: nonce::message"); + fputs("Ciphertext: ", stdout); + print_hex(n, sizeof n); + fputs("::", stdout); + print_hex(c, mlen + crypto_secretbox_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + /* decrypting message */ + puts("Opening message..."); + + /* must zero at least the padding */ + sodium_memzero(c, crypto_secretbox_BOXZEROBYTES); + r = crypto_secretbox_open( + m, c, mlen + crypto_secretbox_ZEROBYTES, n, k); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + print_verification(r); + if (r == 0) printf("Plaintext: %s\n\n", + m + crypto_secretbox_ZEROBYTES); + + sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(n, sizeof n); + sodium_memzero(m, sizeof m); + sodium_memzero(c, sizeof c); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == secretbox() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + diff --git a/demos/shorthash.c b/demos/shorthash.c new file mode 100644 index 00000000..fb12ce1f --- /dev/null +++ b/demos/shorthash.c @@ -0,0 +1,54 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Short hash is a fast algorithm intended for hash tables and anything + * else that does not require data integrity. There is the added benefit + * of a key which will alter the output of the hash. + */ +void +shorthash(void) +{ + unsigned char k[crypto_shorthash_KEYBYTES]; /* key */ + unsigned char h[crypto_shorthash_BYTES]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ + + puts("Example: crypto_shorthash\n"); + + sodium_memzero(k, sizeof k); + prompt_input("Input your key > ", (char*) k, sizeof k); + + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_shorthash_primitive()); + crypto_shorthash(h, m, mlen, k); + fputs("Hash: ", stdout); + print_hex(h, sizeof h); + putchar('\n'); + putchar('\n'); +} + +int +main(int argc, char **argv) +{ + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + shorthash(); + exit(EXIT_SUCCESS); +} + diff --git a/demos/sign.c b/demos/sign.c new file mode 100644 index 00000000..08120982 --- /dev/null +++ b/demos/sign.c @@ -0,0 +1,86 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Signs a message with secret key which will authenticate a message. + * Everybody else can use the public key to ensure that the message is both + * valid and untampered. + * + * Note that both message and signed message must be padded for signature. + * The padding does not have to be set to zero. + */ +static int +sign(void) +{ + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ + unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ + unsigned char m[BUFFER_SIZE + crypto_sign_BYTES]; /* message */ + unsigned char sm[BUFFER_SIZE + crypto_sign_BYTES]; /* signed message */ + unsigned long long int mlen; /* message length */ + unsigned long long int smlen; /* signed length */ + int r; + + puts("Example: crypto_sign\n"); + + puts("Generating keypair..."); + crypto_sign_keypair(pk, sk); /* generate Bob's keys */ + + fputs("Public: ", stdout); + print_hex(pk, sizeof pk); + putc('\n', stdout); + fputs("Secret: ", stdout); + print_hex(sk, sizeof sk); + puts("\n"); + + /* read input */ + mlen = prompt_input("Input your message > ", + (char*) m, sizeof m - crypto_sign_BYTES); + putc('\n', stdout); + + printf("Signing message with %s...\n", crypto_sign_primitive()); + crypto_sign(sm, &smlen, m, mlen, sk); + + puts("Format: signature::message"); + fputs("Signed: ", stdout); + print_hex(sm, crypto_sign_BYTES); + fputs("::", stdout); + puts((const char*) sm + crypto_sign_BYTES); + putc('\n', stdout); + + puts("Validating message..."); + r = crypto_sign_open(m, &mlen, sm, smlen, pk); + + print_verification(r); + if (r == 0) printf("Message: %s\n\n", m); + + sodium_memzero(pk, sizeof pk); /* wipe sensitive data */ + sodium_memzero(sk, sizeof sk); + sodium_memzero(m, sizeof m); + sodium_memzero(sm, sizeof sm); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == sign() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + diff --git a/demos/stream.c b/demos/stream.c new file mode 100644 index 00000000..459748e7 --- /dev/null +++ b/demos/stream.c @@ -0,0 +1,90 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * 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. + * + * 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) +{ + unsigned char k[crypto_stream_KEYBYTES]; /* secret key */ + unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */ + unsigned char m[BUFFER_SIZE]; /* plain-text */ + unsigned char c[BUFFER_SIZE]; /* cipher-text */ + size_t mlen; /* length */ + int r; + + puts("Example: crypto_stream\n"); + + sodium_memzero(k, sizeof k); + prompt_input("Input your key > ", (char*) k, sizeof k); + 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'); + + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + 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); + if (r == 0) printf("Plaintext: %s\n\n", m); + + sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(n, sizeof n); + sodium_memzero(m, sizeof m); + sodium_memzero(c, sizeof c); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == stream() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + From 7ae583d19a89d1fcfdbd8b3f357bef258784d028 Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Wed, 24 Sep 2014 19:39:35 -0400 Subject: [PATCH 02/69] patched all notied issues #192 except for crypto_box_afternm --- demos/.gitignore | 6 +++--- demos/auth.c | 14 ++++++++++++-- demos/demo_utils.c | 5 +++-- demos/onetimeauth.c | 11 +++++++++++ demos/sign.c | 12 +++++++++++- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/demos/.gitignore b/demos/.gitignore index 9b7faddc..a2cb8b4a 100644 --- a/demos/.gitignore +++ b/demos/.gitignore @@ -1,11 +1,11 @@ auth box -generhash -generhashstream +generichash +generichashstream hash Makefile onetimeauth -sretbox +secretbox shorthash sign stream diff --git a/demos/auth.c b/demos/auth.c index d801f08b..77dd3df4 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -27,11 +27,21 @@ auth(void) size_t mlen; /* message length */ int r; - sodium_memzero(k, sizeof k); /* must zero the key */ puts("Example: crypto_auth\n"); - + + /* + * Keys are entered as ascii values. The key is zeroed to + * maintain consistency. Input is read through a special + * function which reads exactly n bytes into a buffer to + * prevent buffer overflows. + */ + sodium_memzero(k, sizeof k); prompt_input("Input your key > ", (char*) k, sizeof k); + puts("Your key that you entered"); + print_hex(k, sizeof k); + putchar('\n'); + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); putchar('\n'); diff --git a/demos/demo_utils.c b/demos/demo_utils.c index 1387b10d..f0c4b917 100644 --- a/demos/demo_utils.c +++ b/demos/demo_utils.c @@ -16,7 +16,8 @@ * ================================================================== */ /* - * Print hex. + * Print hex is a wrapper around sodium_bin2hex which uses calloc + * to allocate temporary memory then immediately printing the result. */ void print_hex(const void *buf, const size_t len) @@ -25,7 +26,7 @@ print_hex(const void *buf, const size_t len) char *p; b = buf; - p = malloc((len * 2 + 1) * (sizeof *b)); + p = calloc(len * 2 + 1, sizeof *b); /* the library supplies a few utility functions like the one below */ sodium_bin2hex(p, len * 2 + 1, b, len); diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index c9b47189..fcacdbb8 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -33,7 +33,18 @@ onetimeauth(void) puts("Example: crypto_onetimeauth\n"); + /* + * Keys are entered as ascii values. The key is zeroed to + * maintain consistency. Input is read through a special + * function which reads exactly n bytes into a buffer to + * prevent buffer overflows. + */ + sodium_memzero(k, sizeof k); prompt_input("Input your key > ", (char*) k, sizeof k); + puts("Your key that you entered"); + print_hex(k, sizeof k); + putchar('\n'); + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); putchar('\n'); diff --git a/demos/sign.c b/demos/sign.c index 08120982..3ca8f06c 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -26,7 +26,7 @@ sign(void) { unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ - unsigned char m[BUFFER_SIZE + crypto_sign_BYTES]; /* message */ + unsigned char m[BUFFER_SIZE]; /* message */ unsigned char sm[BUFFER_SIZE + crypto_sign_BYTES]; /* signed message */ unsigned long long int mlen; /* message length */ unsigned long long int smlen; /* signed length */ @@ -48,10 +48,20 @@ sign(void) mlen = prompt_input("Input your message > ", (char*) m, sizeof m - crypto_sign_BYTES); putc('\n', stdout); + + puts("Notice the message has no prepended padding"); + print_hex(m, mlen); + putchar('\n'); + putchar('\n'); printf("Signing message with %s...\n", crypto_sign_primitive()); crypto_sign(sm, &smlen, m, mlen, sk); + puts("Notice the signed message has prepended signature"); + print_hex(sm, smlen); + putchar('\n'); + putchar('\n'); + puts("Format: signature::message"); fputs("Signed: ", stdout); print_hex(sm, crypto_sign_BYTES); From 15ab5f6bf20efa0258b906d7bf770fd9829410cd Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Wed, 24 Sep 2014 20:08:31 -0400 Subject: [PATCH 03/69] moved box.c to box_old.c and made a new box.c with --- demos/.gitignore | 1 + demos/box.c | 50 ++++----------- demos/box_old.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+), 37 deletions(-) create mode 100644 demos/box_old.c diff --git a/demos/.gitignore b/demos/.gitignore index a2cb8b4a..9e4e2cf2 100644 --- a/demos/.gitignore +++ b/demos/.gitignore @@ -1,5 +1,6 @@ auth box +box_old generichash generichashstream hash diff --git a/demos/box.c b/demos/box.c index 5ebd2014..022b8ab8 100644 --- a/demos/box.c +++ b/demos/box.c @@ -32,19 +32,17 @@ box(void) { unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ - unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ - unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ - unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES];/* plaintext */ - unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES];/* ciphertext */ + unsigned char m[BUFFER_SIZE]; /* plaintext */ + unsigned char c[BUFFER_SIZE + crypto_box_MACBYTES]; /* ciphertext */ size_t mlen; /* length */ int r; - puts("Example: crypto_box\n"); + puts("Example: crypto_box_easy\n"); puts("Generating keypairs...\n"); crypto_box_keypair(bob_pk, bob_sk); /* generate Bob's keys */ @@ -67,17 +65,6 @@ box(void) print_hex(alice_sk, sizeof alice_sk); putchar('\n'); putchar('\n'); - - /* perform diffie hellman */ - crypto_box_beforenm(bob_ss, alice_pk, bob_sk); - crypto_box_beforenm(alice_ss, bob_pk, alice_sk); - fputs("Bob shared key: ", stdout); - print_hex(bob_ss, sizeof bob_ss); - putchar('\n'); - fputs("Alice shared key: ", stdout); - print_hex(alice_ss, sizeof alice_ss); - putchar('\n'); - putchar('\n'); /* nonce must be generated per message, safe to send with message */ puts("Generating nonce..."); @@ -88,21 +75,16 @@ box(void) putchar('\n'); /* read input */ - mlen = prompt_input("Input your message > ", - (char*) m + crypto_box_ZEROBYTES, - sizeof m - crypto_box_ZEROBYTES); + mlen = prompt_input("Input your message > ", (char*) m, sizeof m); - /* must zero at least the padding */ - sodium_memzero(m, crypto_box_ZEROBYTES); - - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); + 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()); - crypto_box_afternm(c, m, mlen + crypto_box_ZEROBYTES, n, bob_ss); + crypto_box_easy(c, m, mlen, n, bob_pk, alice_sk); /* sent message */ puts("Bob sending message...\n"); @@ -111,32 +93,26 @@ box(void) fputs("Ciphertext: ", stdout); print_hex(n, sizeof n); fputs("::", stdout); - print_hex(c, mlen + crypto_box_ZEROBYTES); + print_hex(c, mlen + crypto_box_MACBYTES); putchar('\n'); putchar('\n'); /* decrypt the message */ puts("Alice opening message..."); - - /* must zero at least the padding */ - sodium_memzero(c, crypto_box_BOXZEROBYTES); - r = crypto_box_open_afternm( - m, c, mlen + crypto_box_ZEROBYTES, - n, alice_ss); + r = crypto_box_open_easy(m, c, mlen + crypto_box_MACBYTES, + n, bob_pk, alice_sk); - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); + puts("Notice there is no padding"); + print_hex(m, mlen); putchar('\n'); print_verification(r); - if (r == 0) printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); + if (r == 0) printf("Plaintext: %s\n\n", m); sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ sodium_memzero(bob_sk, sizeof bob_sk); - sodium_memzero(bob_ss, sizeof bob_ss); sodium_memzero(alice_pk, sizeof alice_pk); sodium_memzero(alice_sk, sizeof alice_sk); - sodium_memzero(alice_ss, sizeof alice_ss); sodium_memzero(n, sizeof n); sodium_memzero(m, sizeof m); sodium_memzero(c, sizeof c); diff --git a/demos/box_old.c b/demos/box_old.c new file mode 100644 index 00000000..7cc1f945 --- /dev/null +++ b/demos/box_old.c @@ -0,0 +1,157 @@ +/* + * GraxRabble + * 05 May 2014 + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "demo_utils.h" /* utility functions shared by demos */ + + + +/* + * Shows how crypto_box_afternm 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. + * + * Note that crypto_box uses padding at the start of both messages. + * The padding must be zero else the encryption or decryption will fail. + * + * 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) +{ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ + unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ + + unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ + unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ + unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ + + unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ + unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES];/* plaintext */ + unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES];/* ciphertext */ + size_t mlen; /* length */ + int r; + + puts("Example: crypto_box_afternm (archaic)\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'); + + /* perform diffie hellman */ + crypto_box_beforenm(bob_ss, alice_pk, bob_sk); + crypto_box_beforenm(alice_ss, bob_pk, alice_sk); + fputs("Bob shared key: ", stdout); + print_hex(bob_ss, sizeof bob_ss); + putchar('\n'); + fputs("Alice shared key: ", stdout); + print_hex(alice_ss, sizeof alice_ss); + 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'); + + /* read input */ + mlen = prompt_input("Input your message > ", + (char*) m + crypto_box_ZEROBYTES, + sizeof m - crypto_box_ZEROBYTES); + + /* must zero at least the padding */ + sodium_memzero(m, crypto_box_ZEROBYTES); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + /* encrypt the message */ + printf("Encrypting with %s\n\n", crypto_box_primitive()); + crypto_box_afternm(c, m, mlen + crypto_box_ZEROBYTES, n, bob_ss); + + /* 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_ZEROBYTES); + putchar('\n'); + putchar('\n'); + + /* decrypt the message */ + puts("Alice opening message..."); + + /* must zero at least the padding */ + sodium_memzero(c, crypto_box_BOXZEROBYTES); + r = crypto_box_open_afternm( + m, c, mlen + crypto_box_ZEROBYTES, + n, alice_ss); + + puts("Notice the 32 bytes of zero"); + print_hex(m, mlen + crypto_box_ZEROBYTES); + putchar('\n'); + + print_verification(r); + if (r == 0) printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); + + sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ + sodium_memzero(bob_sk, sizeof bob_sk); + sodium_memzero(bob_ss, sizeof bob_ss); + sodium_memzero(alice_pk, sizeof alice_pk); + sodium_memzero(alice_sk, sizeof alice_sk); + sodium_memzero(alice_ss, sizeof alice_ss); + sodium_memzero(n, sizeof n); + sodium_memzero(m, sizeof m); + sodium_memzero(c, sizeof c); + return r; +} + +int +main(int argc, char **argv) +{ + int r; + + sodium_init(); + printf("Using LibSodium %s\n", sodium_version_string()); + + r = (0 == box() ? EXIT_SUCCESS : EXIT_FAILURE); + exit(r); +} + From 2d8c08b01beb1ed23a48ed7d116be3de0d91fb04 Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Wed, 24 Sep 2014 20:11:50 -0400 Subject: [PATCH 04/69] Got bob and alice mixed up on box.c:87 --- demos/box.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demos/box.c b/demos/box.c index 022b8ab8..a29d49bd 100644 --- a/demos/box.c +++ b/demos/box.c @@ -84,7 +84,7 @@ box(void) /* encrypt the message */ printf("Encrypting with %s\n\n", crypto_box_primitive()); - crypto_box_easy(c, m, mlen, n, bob_pk, alice_sk); + crypto_box_easy(c, m, mlen, n, alice_pk, bob_sk); /* sent message */ puts("Bob sending message...\n"); From e99748b1c07efe97a5ee77fab53836cd9d76d369 Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Wed, 24 Sep 2014 20:13:01 -0400 Subject: [PATCH 05/69] removed comment about box_easy having to use padding --- demos/box.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/demos/box.c b/demos/box.c index a29d49bd..c2c93706 100644 --- a/demos/box.c +++ b/demos/box.c @@ -19,9 +19,6 @@ * library will perform Diffie-Hellman to generate a shared key for * symmetric encryption. * - * Note that crypto_box uses padding at the start of both messages. - * The padding must be zero else the encryption or decryption will fail. - * * Encrypted messages will be 16 bytes longer because a 16 byte * authentication token will be prepended to the message. * From 44fed3dd0ed28cd8a4132e3162e2a600c37dc7f2 Mon Sep 17 00:00:00 2001 From: GraxRabble Date: Thu, 25 Sep 2014 13:21:44 -0400 Subject: [PATCH 06/69] fixed the comment about both message and signed message being padded --- demos/sign.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/demos/sign.c b/demos/sign.c index 3ca8f06c..345b436b 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -18,8 +18,9 @@ * Everybody else can use the public key to ensure that the message is both * valid and untampered. * - * Note that both message and signed message must be padded for signature. - * The padding does not have to be set to zero. + * Note that the signed message will have 16 bytes of signature prepended. + * Ensure that the signed buffer is at least crypto_sign_BYTES longer then + * the actual message. */ static int sign(void) From 2f9920c71f48fdf2ea84d8a5d03c4ab9fb01cfcd Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 12:36:44 +0200 Subject: [PATCH 07/69] There are no Makefiles in the demos folder --- demos/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/demos/.gitignore b/demos/.gitignore index 9e4e2cf2..f20c5fbb 100644 --- a/demos/.gitignore +++ b/demos/.gitignore @@ -4,7 +4,6 @@ box_old generichash generichashstream hash -Makefile onetimeauth secretbox shorthash From 9c613c2e0cfd1b08c4682566eda0e5fde34e83a3 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 12:41:43 +0200 Subject: [PATCH 08/69] Have main() return a value --- demos/auth.c | 7 ++----- demos/box.c | 7 ++----- demos/box_old.c | 7 ++----- demos/generichash.c | 4 ++-- demos/generichashstream.c | 4 ++-- demos/hash.c | 4 ++-- demos/onetimeauth.c | 7 ++----- demos/secretbox.c | 7 ++----- demos/shorthash.c | 4 ++-- demos/sign.c | 7 ++----- demos/stream.c | 7 ++----- 11 files changed, 22 insertions(+), 43 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index 77dd3df4..b63da698 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -65,14 +65,11 @@ auth(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == auth() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return auth() != 0; } diff --git a/demos/box.c b/demos/box.c index c2c93706..d076f118 100644 --- a/demos/box.c +++ b/demos/box.c @@ -117,14 +117,11 @@ box(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == box() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return box() != 0; } diff --git a/demos/box_old.c b/demos/box_old.c index 7cc1f945..1597dbf3 100644 --- a/demos/box_old.c +++ b/demos/box_old.c @@ -144,14 +144,11 @@ box(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == box() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return box() != 0; } diff --git a/demos/generichash.c b/demos/generichash.c index a8b2d435..eeef43bd 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -46,12 +46,12 @@ generichash(void) } int -main(int argc, char **argv) +main(void) { sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); generichash(); - exit(EXIT_SUCCESS); + return 0; } diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 63d7a2e6..37ced3a4 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -55,12 +55,12 @@ generichashstream(void) } int -main(int argc, char **argv) +main(void) { sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); generichashstream(); - exit(EXIT_SUCCESS); + return 0; } diff --git a/demos/hash.c b/demos/hash.c index 167a3b26..6cf34215 100644 --- a/demos/hash.c +++ b/demos/hash.c @@ -38,12 +38,12 @@ hash(void) } int -main(int argc, char **argv) +main(void) { sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); hash(); - exit(EXIT_SUCCESS); + return 0; } diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index fcacdbb8..4944d230 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -69,14 +69,11 @@ onetimeauth(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == onetimeauth() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return onetimeauth() != 0; } diff --git a/demos/secretbox.c b/demos/secretbox.c index 4a043f24..2823f592 100644 --- a/demos/secretbox.c +++ b/demos/secretbox.c @@ -97,14 +97,11 @@ secretbox(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == secretbox() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return secretbox() != 0; } diff --git a/demos/shorthash.c b/demos/shorthash.c index fb12ce1f..b16f96c0 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -43,12 +43,12 @@ shorthash(void) } int -main(int argc, char **argv) +main(void) { sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); shorthash(); - exit(EXIT_SUCCESS); + return 0; } diff --git a/demos/sign.c b/demos/sign.c index 345b436b..181b3fec 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -84,14 +84,11 @@ sign(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == sign() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return sign() != 0; } diff --git a/demos/stream.c b/demos/stream.c index 459748e7..aca4510c 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -77,14 +77,11 @@ stream(void) } int -main(int argc, char **argv) +main(void) { - int r; - sodium_init(); printf("Using LibSodium %s\n", sodium_version_string()); - r = (0 == stream() ? EXIT_SUCCESS : EXIT_FAILURE); - exit(r); + return stream() != 0; } From fb6bb61a72c4afd689e4dc3fa1f2ce6dfbaf6fc8 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 12:43:49 +0200 Subject: [PATCH 09/69] Dates don't age well --- demos/auth.c | 1 - demos/box.c | 1 - demos/box_old.c | 1 - demos/generichash.c | 1 - demos/generichashstream.c | 1 - demos/hash.c | 1 - demos/onetimeauth.c | 1 - demos/secretbox.c | 1 - demos/shorthash.c | 1 - demos/sign.c | 1 - demos/stream.c | 1 - 11 files changed, 11 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index b63da698..b1a36621 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/box.c b/demos/box.c index d076f118..d6ea0076 100644 --- a/demos/box.c +++ b/demos/box.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/box_old.c b/demos/box_old.c index 1597dbf3..928b7228 100644 --- a/demos/box_old.c +++ b/demos/box_old.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/generichash.c b/demos/generichash.c index eeef43bd..f5834abc 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 37ced3a4..ec62d190 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/hash.c b/demos/hash.c index 6cf34215..cd1214bb 100644 --- a/demos/hash.c +++ b/demos/hash.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index 4944d230..acf00937 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/secretbox.c b/demos/secretbox.c index 2823f592..6ab4cb58 100644 --- a/demos/secretbox.c +++ b/demos/secretbox.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/shorthash.c b/demos/shorthash.c index b16f96c0..c0790f81 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/sign.c b/demos/sign.c index 181b3fec..35b47ce9 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include diff --git a/demos/stream.c b/demos/stream.c index aca4510c..a06c4cc5 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -1,6 +1,5 @@ /* * GraxRabble - * 05 May 2014 * Demo programs for libsodium. */ #include From ba3fe15b757d8a4b0c64d5f56a6df0e3c47ad38c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 13:02:56 +0200 Subject: [PATCH 10/69] Indent --- demos/auth.c | 26 +++++++++----------- demos/box.c | 32 ++++++++++++------------- demos/box_old.c | 50 ++++++++++++++++++--------------------- demos/demo_utils.c | 23 +++++++++--------- demos/demo_utils.h | 11 ++------- demos/generichash.c | 19 +++++++-------- demos/generichashstream.c | 34 +++++++++++++------------- demos/hash.c | 15 +++++------- demos/onetimeauth.c | 30 ++++++++++------------- demos/secretbox.c | 48 +++++++++++++++++-------------------- demos/shorthash.c | 19 +++++++-------- demos/sign.c | 40 +++++++++++++++---------------- demos/stream.c | 28 ++++++++++------------ 13 files changed, 167 insertions(+), 208 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index b1a36621..ef9d1e0e 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Full featured authentication which is used to verify that the message @@ -20,15 +18,14 @@ static int auth(void) { - unsigned char k[crypto_auth_KEYBYTES]; /* key */ - unsigned char a[crypto_auth_BYTES]; /* authentication token */ - unsigned char m[BUFFER_SIZE]; /* message */ - size_t mlen; /* message length */ + unsigned char k[crypto_auth_KEYBYTES]; /* key */ + unsigned char a[crypto_auth_BYTES]; /* authentication token */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* message length */ int r; - puts("Example: crypto_auth\n"); - + /* * Keys are entered as ascii values. The key is zeroed to * maintain consistency. Input is read through a special @@ -36,12 +33,12 @@ auth(void) * prevent buffer overflows. */ sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); puts("Your key that you entered"); print_hex(k, sizeof k); putchar('\n'); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); printf("Generating %s authentication...\n", crypto_auth_primitive()); @@ -50,14 +47,14 @@ auth(void) puts("Format: authentication token::message"); print_hex(a, sizeof a); fputs("::", stdout); - puts((const char*) m); + puts((const char*)m); putchar('\n'); puts("Verifying authentication..."); r = crypto_auth_verify(a, m, mlen, k); print_verification(r); - sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(k, sizeof k); /* wipe sensitive data */ sodium_memzero(a, sizeof a); sodium_memzero(m, sizeof m); return r; @@ -71,4 +68,3 @@ main(void) return auth() != 0; } - diff --git a/demos/box.c b/demos/box.c index d6ea0076..da69075e 100644 --- a/demos/box.c +++ b/demos/box.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Shows how crypto_box works using Bob and Alice with a simple message. @@ -26,11 +24,11 @@ static int box(void) { - unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ - unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ - unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ - unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ + 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[BUFFER_SIZE]; /* plaintext */ @@ -69,10 +67,10 @@ box(void) print_hex(n, sizeof n); putchar('\n'); putchar('\n'); - + /* read input */ - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); - + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); + puts("Notice there is no padding"); print_hex(m, mlen); putchar('\n'); @@ -81,7 +79,7 @@ box(void) /* encrypt the message */ printf("Encrypting with %s\n\n", crypto_box_primitive()); crypto_box_easy(c, m, mlen, n, alice_pk, bob_sk); - + /* sent message */ puts("Bob sending message...\n"); puts("Notice the prepended 16 byte authentication token"); @@ -95,17 +93,18 @@ box(void) /* decrypt the message */ puts("Alice opening message..."); - r = crypto_box_open_easy(m, c, mlen + crypto_box_MACBYTES, - n, bob_pk, alice_sk); + 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); - if (r == 0) printf("Plaintext: %s\n\n", m); + if (r == 0) + printf("Plaintext: %s\n\n", m); - sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ + 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); @@ -123,4 +122,3 @@ main(void) return box() != 0; } - diff --git a/demos/box_old.c b/demos/box_old.c index 928b7228..a81eee84 100644 --- a/demos/box_old.c +++ b/demos/box_old.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Shows how crypto_box_afternm works using Bob and Alice with a simple @@ -29,18 +27,18 @@ static int box(void) { - unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ - unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ - unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ + unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ - unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ - unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ - unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ + unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ + unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ + unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ - unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ - unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES];/* plaintext */ - unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES];/* ciphertext */ - size_t mlen; /* length */ + unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ + unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES]; /* plaintext */ + unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES]; /* ciphertext */ + size_t mlen; /* length */ int r; puts("Example: crypto_box_afternm (archaic)\n"); @@ -66,7 +64,7 @@ box(void) print_hex(alice_sk, sizeof alice_sk); putchar('\n'); putchar('\n'); - + /* perform diffie hellman */ crypto_box_beforenm(bob_ss, alice_pk, bob_sk); crypto_box_beforenm(alice_ss, bob_pk, alice_sk); @@ -85,12 +83,12 @@ box(void) print_hex(n, sizeof n); putchar('\n'); putchar('\n'); - + /* read input */ mlen = prompt_input("Input your message > ", - (char*) m + crypto_box_ZEROBYTES, - sizeof m - crypto_box_ZEROBYTES); - + (char*)m + crypto_box_ZEROBYTES, + sizeof m - crypto_box_ZEROBYTES); + /* must zero at least the padding */ sodium_memzero(m, crypto_box_ZEROBYTES); @@ -102,7 +100,7 @@ box(void) /* encrypt the message */ printf("Encrypting with %s\n\n", crypto_box_primitive()); crypto_box_afternm(c, m, mlen + crypto_box_ZEROBYTES, n, bob_ss); - + /* sent message */ puts("Bob sending message...\n"); puts("Notice the prepended 16 byte authentication token"); @@ -116,21 +114,20 @@ box(void) /* decrypt the message */ puts("Alice opening message..."); - + /* must zero at least the padding */ sodium_memzero(c, crypto_box_BOXZEROBYTES); - r = crypto_box_open_afternm( - m, c, mlen + crypto_box_ZEROBYTES, - n, alice_ss); + r = crypto_box_open_afternm(m, c, mlen + crypto_box_ZEROBYTES, n, alice_ss); puts("Notice the 32 bytes of zero"); print_hex(m, mlen + crypto_box_ZEROBYTES); putchar('\n'); print_verification(r); - if (r == 0) printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); + if (r == 0) + printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); - sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ + sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ sodium_memzero(bob_sk, sizeof bob_sk); sodium_memzero(bob_ss, sizeof bob_ss); sodium_memzero(alice_pk, sizeof alice_pk); @@ -150,4 +147,3 @@ main(void) return box() != 0; } - diff --git a/demos/demo_utils.c b/demos/demo_utils.c index f0c4b917..aa9f7af3 100644 --- a/demos/demo_utils.c +++ b/demos/demo_utils.c @@ -5,11 +5,9 @@ #include #include -#include "sodium.h" /* library header */ - -#include "demo_utils.h" /* demo utility header */ - +#include "sodium.h" /* library header */ +#include "demo_utils.h" /* demo utility header */ /* ================================================================== * * utility functions * @@ -24,10 +22,10 @@ print_hex(const void *buf, const size_t len) { const unsigned char *b; char *p; - + b = buf; p = calloc(len * 2 + 1, sizeof *b); - + /* the library supplies a few utility functions like the one below */ sodium_bin2hex(p, len * 2 + 1, b, len); fputs(p, stdout); @@ -45,10 +43,10 @@ prompt_input(char *prompt, char *buf, const size_t len) size_t n; fputs(prompt, stdout); - fgets(buf, len, stdin); /* grab input with room for NULL */ - + fgets(buf, len, stdin); /* grab input with room for NULL */ + n = strlen(buf); - if (buf[n - 1] == '\n') { /* trim excess new line */ + if (buf[n - 1] == '\n') { /* trim excess new line */ buf[n - 1] = '\0'; --n; } @@ -61,7 +59,8 @@ prompt_input(char *prompt, char *buf, const size_t len) void print_verification(int r) { - if (r == 0) puts("Success\n"); - else puts("Failure\n"); + if (r == 0) + puts("Success\n"); + else + puts("Failure\n"); } - diff --git a/demos/demo_utils.h b/demos/demo_utils.h index ae35d8d7..0bf8b2db 100644 --- a/demos/demo_utils.h +++ b/demos/demo_utils.h @@ -4,19 +4,12 @@ #ifndef DEMO_UTILS_H #define DEMO_UTILS_H - #include - -#define BUFFER_SIZE 128 /* size of all input buffers in the demo */ - - +#define BUFFER_SIZE 128 /* size of all input buffers in the demo */ void print_hex(const void *buf, const size_t len); size_t prompt_input(char *prompt, char *buf, const size_t len); void print_verification(int r); - - -#endif /* DEMO_UTILS_H */ - +#endif /* DEMO_UTILS_H */ diff --git a/demos/generichash.c b/demos/generichash.c index f5834abc..71342c69 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Generic hash is intended as a variable output hash with enough strength @@ -23,17 +21,17 @@ void generichash(void) { - unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ - unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ - size_t mlen; /* length */ + unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ + unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ puts("Example: crypto_generichash\n"); sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); printf("Hashing message with %s\n", crypto_generichash_primitive()); @@ -53,4 +51,3 @@ main(void) generichash(); return 0; } - diff --git a/demos/generichashstream.c b/demos/generichashstream.c index ec62d190..48445997 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Streaming variant of generic hash. This has the ability to hash @@ -20,33 +18,34 @@ void generichashstream(void) { - unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ - unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - crypto_generichash_state state; /* hash stream */ - unsigned char m[BUFFER_SIZE]; /* input buffer */ - size_t mlen; /* input length */ + unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ + unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ + crypto_generichash_state state; /* hash stream */ + unsigned char m[BUFFER_SIZE]; /* input buffer */ + size_t mlen; /* input length */ puts("Example: crypto_generichashstream\n"); - + sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); putchar('\n'); - + printf("Hashing message with %s\n", crypto_generichash_primitive()); - + /* initialize the stream */ crypto_generichash_init(&state, k, sizeof k, sizeof h); while (1) { - mlen = prompt_input("> ", (char*) m, sizeof m); - if (mlen == 0) break; - + mlen = prompt_input("> ", (char*)m, sizeof m); + if (mlen == 0) + break; + /* keep appending data */ crypto_generichash_update(&state, m, mlen); } crypto_generichash_final(&state, h, sizeof h); putchar('\n'); - + fputs("Hash: ", stdout); print_hex(h, sizeof h); putchar('\n'); @@ -62,4 +61,3 @@ main(void) generichashstream(); return 0; } - diff --git a/demos/hash.c b/demos/hash.c index cd1214bb..1765413d 100644 --- a/demos/hash.c +++ b/demos/hash.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * The library ships with a one-shot SHA-512 implementation. Simply allocate @@ -19,13 +17,13 @@ static void hash(void) { - unsigned char h[crypto_hash_BYTES]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ - size_t mlen; /* length */ + unsigned char h[crypto_hash_BYTES]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ puts("Example: crypto_hash\n"); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); printf("Hashing message with %s\n", crypto_hash_primitive()); @@ -45,4 +43,3 @@ main(void) hash(); return 0; } - diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index acf00937..fdbcfbb5 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * This method is only effective for a single use per key. The benefit is @@ -22,13 +20,13 @@ static int onetimeauth(void) { - unsigned char k[crypto_onetimeauth_KEYBYTES];/* key */ - unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */ - unsigned char m[BUFFER_SIZE]; /* message */ - size_t mlen; /* message length */ + unsigned char k[crypto_onetimeauth_KEYBYTES]; /* key */ + unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* message length */ int r; - sodium_memzero(k, sizeof k); /* must zero the key */ + sodium_memzero(k, sizeof k); /* must zero the key */ puts("Example: crypto_onetimeauth\n"); @@ -39,29 +37,28 @@ onetimeauth(void) * prevent buffer overflows. */ sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); puts("Your key that you entered"); print_hex(k, sizeof k); putchar('\n'); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); - printf("Generating %s authentication...\n", - crypto_onetimeauth_primitive()); + printf("Generating %s authentication...\n", crypto_onetimeauth_primitive()); crypto_onetimeauth(a, m, mlen, k); puts("Format: authentication token::message"); print_hex(a, sizeof a); fputs("::", stdout); - puts((const char*) m); + puts((const char*)m); putchar('\n'); puts("Verifying authentication..."); r = crypto_onetimeauth_verify(a, m, mlen, k); print_verification(r); - - sodium_memzero(k, sizeof k); /* wipe sensitive data */ + + sodium_memzero(k, sizeof k); /* wipe sensitive data */ sodium_memzero(a, sizeof a); sodium_memzero(m, sizeof m); return r; @@ -75,4 +72,3 @@ main(void) return onetimeauth() != 0; } - diff --git a/demos/secretbox.c b/demos/secretbox.c index 6ab4cb58..12757a08 100644 --- a/demos/secretbox.c +++ b/demos/secretbox.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * This is a wrapper around stream which does XOR automatically. @@ -24,17 +22,17 @@ static int secretbox(void) { - unsigned char k[crypto_secretbox_KEYBYTES]; /* secret */ - unsigned char n[crypto_secretbox_NONCEBYTES]; /* nonce */ - unsigned char m[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* plain */ - unsigned char c[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* cipher */ - size_t mlen; /* length */ + unsigned char k[crypto_secretbox_KEYBYTES]; /* secret */ + unsigned char n[crypto_secretbox_NONCEBYTES]; /* nonce */ + unsigned char m[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* plain */ + unsigned char c[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* cipher */ + size_t mlen; /* length */ int r; puts("Example: crypto_secretbox\n"); sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); /* nonce must be generated per message, safe to send with message */ puts("Generating nonce..."); @@ -43,21 +41,21 @@ secretbox(void) print_hex(n, sizeof n); putchar('\n'); putchar('\n'); - + mlen = prompt_input("Input your message > ", - (char*) m + crypto_secretbox_ZEROBYTES, - sizeof m - crypto_secretbox_ZEROBYTES); - + (char*)m + crypto_secretbox_ZEROBYTES, + sizeof m - crypto_secretbox_ZEROBYTES); + /* must zero at least the padding */ sodium_memzero(m, crypto_secretbox_ZEROBYTES); - + puts("Notice the 32 bytes of zero"); print_hex(m, mlen + crypto_box_ZEROBYTES); putchar('\n'); - + /* encrypting message */ printf("Encrypting with %s\n", crypto_secretbox_primitive()); - + crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k); putchar('\n'); @@ -70,25 +68,24 @@ secretbox(void) print_hex(c, mlen + crypto_secretbox_ZEROBYTES); putchar('\n'); putchar('\n'); - + /* decrypting message */ puts("Opening message..."); - + /* must zero at least the padding */ sodium_memzero(c, crypto_secretbox_BOXZEROBYTES); - r = crypto_secretbox_open( - m, c, mlen + crypto_secretbox_ZEROBYTES, n, k); - + r = crypto_secretbox_open(m, c, mlen + crypto_secretbox_ZEROBYTES, n, k); + puts("Notice the 32 bytes of zero"); print_hex(m, mlen + crypto_box_ZEROBYTES); putchar('\n'); putchar('\n'); print_verification(r); - if (r == 0) printf("Plaintext: %s\n\n", - m + crypto_secretbox_ZEROBYTES); + if (r == 0) + printf("Plaintext: %s\n\n", m + crypto_secretbox_ZEROBYTES); - sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(k, sizeof k); /* wipe sensitive data */ sodium_memzero(n, sizeof n); sodium_memzero(m, sizeof m); sodium_memzero(c, sizeof c); @@ -103,4 +100,3 @@ main(void) return secretbox() != 0; } - diff --git a/demos/shorthash.c b/demos/shorthash.c index c0790f81..a1ee3141 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Short hash is a fast algorithm intended for hash tables and anything @@ -20,17 +18,17 @@ void shorthash(void) { - unsigned char k[crypto_shorthash_KEYBYTES]; /* key */ - unsigned char h[crypto_shorthash_BYTES]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ - size_t mlen; /* length */ + unsigned char k[crypto_shorthash_KEYBYTES]; /* key */ + unsigned char h[crypto_shorthash_BYTES]; /* hash output */ + unsigned char m[BUFFER_SIZE]; /* message */ + size_t mlen; /* length */ puts("Example: crypto_shorthash\n"); sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); printf("Hashing message with %s\n", crypto_shorthash_primitive()); @@ -50,4 +48,3 @@ main(void) shorthash(); return 0; } - diff --git a/demos/sign.c b/demos/sign.c index 35b47ce9..6e7acb89 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -6,11 +6,9 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * Signs a message with secret key which will authenticate a message. @@ -24,18 +22,18 @@ static int sign(void) { - unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ - unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ - unsigned char m[BUFFER_SIZE]; /* message */ - unsigned char sm[BUFFER_SIZE + crypto_sign_BYTES]; /* signed message */ - unsigned long long int mlen; /* message length */ - unsigned long long int smlen; /* signed length */ + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ + unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ + unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char sm[BUFFER_SIZE + crypto_sign_BYTES]; /* signed message */ + unsigned long long int mlen; /* message length */ + unsigned long long int smlen; /* signed length */ int r; puts("Example: crypto_sign\n"); puts("Generating keypair..."); - crypto_sign_keypair(pk, sk); /* generate Bob's keys */ + crypto_sign_keypair(pk, sk); /* generate Bob's keys */ fputs("Public: ", stdout); print_hex(pk, sizeof pk); @@ -45,10 +43,10 @@ sign(void) puts("\n"); /* read input */ - mlen = prompt_input("Input your message > ", - (char*) m, sizeof m - crypto_sign_BYTES); + mlen = prompt_input("Input your message > ", (char*)m, + sizeof m - crypto_sign_BYTES); putc('\n', stdout); - + puts("Notice the message has no prepended padding"); print_hex(m, mlen); putchar('\n'); @@ -56,26 +54,27 @@ sign(void) printf("Signing message with %s...\n", crypto_sign_primitive()); crypto_sign(sm, &smlen, m, mlen, sk); - + puts("Notice the signed message has prepended signature"); print_hex(sm, smlen); putchar('\n'); putchar('\n'); - + puts("Format: signature::message"); fputs("Signed: ", stdout); print_hex(sm, crypto_sign_BYTES); fputs("::", stdout); - puts((const char*) sm + crypto_sign_BYTES); + puts((const char*)sm + crypto_sign_BYTES); putc('\n', stdout); puts("Validating message..."); r = crypto_sign_open(m, &mlen, sm, smlen, pk); print_verification(r); - if (r == 0) printf("Message: %s\n\n", m); - - sodium_memzero(pk, sizeof pk); /* wipe sensitive data */ + if (r == 0) + printf("Message: %s\n\n", m); + + sodium_memzero(pk, sizeof pk); /* wipe sensitive data */ sodium_memzero(sk, sizeof sk); sodium_memzero(m, sizeof m); sodium_memzero(sm, sizeof sm); @@ -90,4 +89,3 @@ main(void) return sign() != 0; } - diff --git a/demos/stream.c b/demos/stream.c index a06c4cc5..f5a89434 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -6,16 +6,14 @@ #include #include -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - +#include /* library header */ +#include "demo_utils.h" /* utility functions shared by demos */ /* * 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. - * + * * Note that this method does not supply authentication. Try secretbox instead. * * Note that nonce must be different for each message since it provides @@ -25,17 +23,17 @@ static int stream(void) { - unsigned char k[crypto_stream_KEYBYTES]; /* secret key */ - unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */ - unsigned char m[BUFFER_SIZE]; /* plain-text */ - unsigned char c[BUFFER_SIZE]; /* cipher-text */ - size_t mlen; /* length */ + unsigned char k[crypto_stream_KEYBYTES]; /* secret key */ + unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */ + unsigned char m[BUFFER_SIZE]; /* plain-text */ + unsigned char c[BUFFER_SIZE]; /* cipher-text */ + size_t mlen; /* length */ int r; puts("Example: crypto_stream\n"); sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*) k, sizeof k); + prompt_input("Input your key > ", (char*)k, sizeof k); putchar('\n'); /* nonce must be generated per message, safe to send with message */ @@ -46,7 +44,7 @@ stream(void) putchar('\n'); putchar('\n'); - mlen = prompt_input("Input your message > ", (char*) m, sizeof m); + mlen = prompt_input("Input your message > ", (char*)m, sizeof m); putchar('\n'); printf("Encrypting with (xor) %s\n", crypto_stream_primitive()); @@ -66,9 +64,10 @@ stream(void) r = crypto_stream_xor(m, c, mlen, n, k); print_verification(r); - if (r == 0) printf("Plaintext: %s\n\n", m); + if (r == 0) + printf("Plaintext: %s\n\n", m); - sodium_memzero(k, sizeof k); /* wipe sensitive data */ + sodium_memzero(k, sizeof k); /* wipe sensitive data */ sodium_memzero(n, sizeof n); sodium_memzero(m, sizeof m); sodium_memzero(c, sizeof c); @@ -83,4 +82,3 @@ main(void) return stream() != 0; } - From a46076fa5b20cf548ae5bda63b3018fee001ec09 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 13:06:42 +0200 Subject: [PATCH 11/69] Remove box_old --- demos/box_old.c | 149 ------------------------------------------------ 1 file changed, 149 deletions(-) delete mode 100644 demos/box_old.c diff --git a/demos/box_old.c b/demos/box_old.c deleted file mode 100644 index a81eee84..00000000 --- a/demos/box_old.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - -/* - * Shows how crypto_box_afternm 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. - * - * Note that crypto_box uses padding at the start of both messages. - * The padding must be zero else the encryption or decryption will fail. - * - * 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) -{ - unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ - unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ - unsigned char bob_ss[crypto_box_BEFORENMBYTES]; /* Bob session */ - - unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ - unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ - unsigned char alice_ss[crypto_box_BEFORENMBYTES]; /* Alice session */ - - unsigned char n[crypto_box_NONCEBYTES]; /* message nonce */ - unsigned char m[BUFFER_SIZE + crypto_box_ZEROBYTES]; /* plaintext */ - unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES]; /* ciphertext */ - size_t mlen; /* length */ - int r; - - puts("Example: crypto_box_afternm (archaic)\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'); - - /* perform diffie hellman */ - crypto_box_beforenm(bob_ss, alice_pk, bob_sk); - crypto_box_beforenm(alice_ss, bob_pk, alice_sk); - fputs("Bob shared key: ", stdout); - print_hex(bob_ss, sizeof bob_ss); - putchar('\n'); - fputs("Alice shared key: ", stdout); - print_hex(alice_ss, sizeof alice_ss); - 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'); - - /* read input */ - mlen = prompt_input("Input your message > ", - (char*)m + crypto_box_ZEROBYTES, - sizeof m - crypto_box_ZEROBYTES); - - /* must zero at least the padding */ - sodium_memzero(m, crypto_box_ZEROBYTES); - - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); - putchar('\n'); - putchar('\n'); - - /* encrypt the message */ - printf("Encrypting with %s\n\n", crypto_box_primitive()); - crypto_box_afternm(c, m, mlen + crypto_box_ZEROBYTES, n, bob_ss); - - /* 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_ZEROBYTES); - putchar('\n'); - putchar('\n'); - - /* decrypt the message */ - puts("Alice opening message..."); - - /* must zero at least the padding */ - sodium_memzero(c, crypto_box_BOXZEROBYTES); - r = crypto_box_open_afternm(m, c, mlen + crypto_box_ZEROBYTES, n, alice_ss); - - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); - putchar('\n'); - - print_verification(r); - if (r == 0) - printf("Plaintext: %s\n\n", m + crypto_box_ZEROBYTES); - - sodium_memzero(bob_pk, sizeof bob_pk); /* wipe sensitive data */ - sodium_memzero(bob_sk, sizeof bob_sk); - sodium_memzero(bob_ss, sizeof bob_ss); - sodium_memzero(alice_pk, sizeof alice_pk); - sodium_memzero(alice_sk, sizeof alice_sk); - sodium_memzero(alice_ss, sizeof alice_ss); - sodium_memzero(n, sizeof n); - sodium_memzero(m, sizeof m); - sodium_memzero(c, sizeof c); - return r; -} - -int -main(void) -{ - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - - return box() != 0; -} From f1943d346ce1bfefb546a029ceff3a924b287163 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 13:07:16 +0200 Subject: [PATCH 12/69] Remove misleading secretbox example --- demos/secretbox.c | 102 ---------------------------------------------- 1 file changed, 102 deletions(-) delete mode 100644 demos/secretbox.c diff --git a/demos/secretbox.c b/demos/secretbox.c deleted file mode 100644 index 12757a08..00000000 --- a/demos/secretbox.c +++ /dev/null @@ -1,102 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "demo_utils.h" /* utility functions shared by demos */ - -/* - * This is a wrapper around stream which does XOR automatically. - * - * Note that the buffer must be padded at the front. The same nonce must - * not be used; it should be safe to use a counter. - * - * Encrypted messages will be 16 bytes longer because a 16 byte - * authentication token will be prepended to the message. - */ -static int -secretbox(void) -{ - unsigned char k[crypto_secretbox_KEYBYTES]; /* secret */ - unsigned char n[crypto_secretbox_NONCEBYTES]; /* nonce */ - unsigned char m[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* plain */ - unsigned char c[BUFFER_SIZE + crypto_secretbox_ZEROBYTES]; /* cipher */ - size_t mlen; /* length */ - int r; - - puts("Example: crypto_secretbox\n"); - - sodium_memzero(k, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); - - /* 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'); - - mlen = prompt_input("Input your message > ", - (char*)m + crypto_secretbox_ZEROBYTES, - sizeof m - crypto_secretbox_ZEROBYTES); - - /* must zero at least the padding */ - sodium_memzero(m, crypto_secretbox_ZEROBYTES); - - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); - putchar('\n'); - - /* encrypting message */ - printf("Encrypting with %s\n", crypto_secretbox_primitive()); - - crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k); - putchar('\n'); - - puts("Notice the prepended 16 byte authentication token"); - puts("Sending message..."); - puts("Format: nonce::message"); - fputs("Ciphertext: ", stdout); - print_hex(n, sizeof n); - fputs("::", stdout); - print_hex(c, mlen + crypto_secretbox_ZEROBYTES); - putchar('\n'); - putchar('\n'); - - /* decrypting message */ - puts("Opening message..."); - - /* must zero at least the padding */ - sodium_memzero(c, crypto_secretbox_BOXZEROBYTES); - r = crypto_secretbox_open(m, c, mlen + crypto_secretbox_ZEROBYTES, n, k); - - puts("Notice the 32 bytes of zero"); - print_hex(m, mlen + crypto_box_ZEROBYTES); - putchar('\n'); - putchar('\n'); - - print_verification(r); - if (r == 0) - printf("Plaintext: %s\n\n", m + crypto_secretbox_ZEROBYTES); - - sodium_memzero(k, sizeof k); /* wipe sensitive data */ - sodium_memzero(n, sizeof n); - sodium_memzero(m, sizeof m); - sodium_memzero(c, sizeof c); - return r; -} - -int -main(void) -{ - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - - return secretbox() != 0; -} From 93499566f4e1511d913ee6f7dbbb0b7c4879974a Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 13:07:58 +0200 Subject: [PATCH 13/69] Remove .gitignore --- demos/.gitignore | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 demos/.gitignore diff --git a/demos/.gitignore b/demos/.gitignore deleted file mode 100644 index f20c5fbb..00000000 --- a/demos/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -auth -box -box_old -generichash -generichashstream -hash -onetimeauth -secretbox -shorthash -sign -stream From 50ffeecb95d46d26f36b49f5e1adfba2e53690af Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:27:15 +0200 Subject: [PATCH 14/69] Minor changes to demo_utils for clarity --- demos/demo_utils.c | 48 ++++++++++++++++++++++++++++------------------ demos/demo_utils.h | 4 ++-- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/demos/demo_utils.c b/demos/demo_utils.c index aa9f7af3..9f6ba8fb 100644 --- a/demos/demo_utils.c +++ b/demos/demo_utils.c @@ -1,6 +1,7 @@ /* * These are the utility functions shared by all demo programs. */ +#include #include #include #include @@ -14,22 +15,28 @@ * ================================================================== */ /* - * Print hex is a wrapper around sodium_bin2hex which uses calloc - * to allocate temporary memory then immediately printing the result. + * Print hex is a wrapper around sodium_bin2hex which allocates + * temporary memory then immediately prints the result. */ void -print_hex(const void *buf, const size_t len) +print_hex(const void *bin, const size_t bin_len) { - const unsigned char *b; - char *p; - - b = buf; - p = calloc(len * 2 + 1, sizeof *b); + char *hex; + size_t hex_size; + if (bin_len >= SIZE_MAX / 2) { + abort(); + } + hex_size = bin_len * 2 + 1; + if ((hex = malloc(hex_size)) == NULL) { + abort(); + } /* the library supplies a few utility functions like the one below */ - sodium_bin2hex(p, len * 2 + 1, b, len); - fputs(p, stdout); - free(p); + if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) { + abort(); + } + fputs(hex, stdout); + free(hex); } /* @@ -38,19 +45,22 @@ print_hex(const void *buf, const size_t len) * trailing newline characters. */ size_t -prompt_input(char *prompt, char *buf, const size_t len) +prompt_input(char *prompt, char *input, const size_t max_input_len) { - size_t n; + size_t actual_input_len; fputs(prompt, stdout); - fgets(buf, len, stdin); /* grab input with room for NULL */ + fflush(stdout); + fgets(input, max_input_len, stdin); /* grab input with room for \0 */ - n = strlen(buf); - if (buf[n - 1] == '\n') { /* trim excess new line */ - buf[n - 1] = '\0'; - --n; + actual_input_len = strlen(input); + + /* trim excess new line */ + if (actual_input_len > 0 && input[actual_input_len - 1] == '\n') { + input[actual_input_len - 1] = '\0'; + --actual_input_len; } - return n; + return actual_input_len; } /* diff --git a/demos/demo_utils.h b/demos/demo_utils.h index 0bf8b2db..29d3aa0a 100644 --- a/demos/demo_utils.h +++ b/demos/demo_utils.h @@ -8,8 +8,8 @@ #define BUFFER_SIZE 128 /* size of all input buffers in the demo */ -void print_hex(const void *buf, const size_t len); -size_t prompt_input(char *prompt, char *buf, const size_t len); +void print_hex(const void *bin, const size_t bin_len); +size_t prompt_input(char *prompt, char *input, const size_t max_input_len); void print_verification(int r); #endif /* DEMO_UTILS_H */ From d97aa46d8a7dfab798e7ecc52a70d3a815e2d567 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:32:22 +0200 Subject: [PATCH 15/69] or "sodium.h", pick one --- demos/demo_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/demo_utils.c b/demos/demo_utils.c index 9f6ba8fb..689928a8 100644 --- a/demos/demo_utils.c +++ b/demos/demo_utils.c @@ -6,9 +6,9 @@ #include #include -#include "sodium.h" /* library header */ +#include -#include "demo_utils.h" /* demo utility header */ +#include "demo_utils.h" /* ================================================================== * * utility functions * From 81a7abea7e5c2a1bf26481aebfa45a5ef74afc05 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:39:34 +0200 Subject: [PATCH 16/69] "buffer" and "buffer size" are way too generic to be useful --- demos/auth.c | 2 +- demos/box.c | 8 ++++---- demos/demo_utils.c | 4 ++-- demos/demo_utils.h | 2 +- demos/generichash.c | 2 +- demos/generichashstream.c | 2 +- demos/hash.c | 2 +- demos/onetimeauth.c | 2 +- demos/shorthash.c | 2 +- demos/sign.c | 12 ++++++------ demos/stream.c | 4 ++-- 11 files changed, 21 insertions(+), 21 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index ef9d1e0e..c25e95c9 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -20,7 +20,7 @@ auth(void) { unsigned char k[crypto_auth_KEYBYTES]; /* key */ unsigned char a[crypto_auth_BYTES]; /* authentication token */ - unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ size_t mlen; /* message length */ int r; diff --git a/demos/box.c b/demos/box.c index da69075e..796993e2 100644 --- a/demos/box.c +++ b/demos/box.c @@ -30,10 +30,10 @@ box(void) 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[BUFFER_SIZE]; /* plaintext */ - unsigned char c[BUFFER_SIZE + crypto_box_MACBYTES]; /* ciphertext */ - size_t mlen; /* length */ + 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"); diff --git a/demos/demo_utils.c b/demos/demo_utils.c index 689928a8..e56abba0 100644 --- a/demos/demo_utils.c +++ b/demos/demo_utils.c @@ -15,7 +15,7 @@ * ================================================================== */ /* - * Print hex is a wrapper around sodium_bin2hex which allocates + * print_hex() is a wrapper around sodium_bin2hex() which allocates * temporary memory then immediately prints the result. */ void @@ -64,7 +64,7 @@ prompt_input(char *prompt, char *input, const size_t max_input_len) } /* - * Print a message if the function was sucessful or failed. + * Display whether the function was sucessful or failed. */ void print_verification(int r) diff --git a/demos/demo_utils.h b/demos/demo_utils.h index 29d3aa0a..619bb1fa 100644 --- a/demos/demo_utils.h +++ b/demos/demo_utils.h @@ -6,7 +6,7 @@ #include -#define BUFFER_SIZE 128 /* size of all input buffers in the demo */ +#define MAX_INPUT_SIZE 128 /* size of all input buffers in the demo */ void print_hex(const void *bin, const size_t bin_len); size_t prompt_input(char *prompt, char *input, const size_t max_input_len); diff --git a/demos/generichash.c b/demos/generichash.c index 71342c69..05527cf3 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -23,7 +23,7 @@ generichash(void) { unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ size_t mlen; /* length */ puts("Example: crypto_generichash\n"); diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 48445997..3da5d1bd 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -21,7 +21,7 @@ generichashstream(void) unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ crypto_generichash_state state; /* hash stream */ - unsigned char m[BUFFER_SIZE]; /* input buffer */ + unsigned char m[MAX_INPUT_SIZE]; /* input buffer */ size_t mlen; /* input length */ puts("Example: crypto_generichashstream\n"); diff --git a/demos/hash.c b/demos/hash.c index 1765413d..dbdcb278 100644 --- a/demos/hash.c +++ b/demos/hash.c @@ -18,7 +18,7 @@ static void hash(void) { unsigned char h[crypto_hash_BYTES]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ size_t mlen; /* length */ puts("Example: crypto_hash\n"); diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index fdbcfbb5..7a6dd9d6 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -22,7 +22,7 @@ onetimeauth(void) { unsigned char k[crypto_onetimeauth_KEYBYTES]; /* key */ unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */ - unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ size_t mlen; /* message length */ int r; diff --git a/demos/shorthash.c b/demos/shorthash.c index a1ee3141..26326ad3 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -20,7 +20,7 @@ shorthash(void) { unsigned char k[crypto_shorthash_KEYBYTES]; /* key */ unsigned char h[crypto_shorthash_BYTES]; /* hash output */ - unsigned char m[BUFFER_SIZE]; /* message */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ size_t mlen; /* length */ puts("Example: crypto_shorthash\n"); diff --git a/demos/sign.c b/demos/sign.c index 6e7acb89..13d66995 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -22,12 +22,12 @@ static int sign(void) { - unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ - unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ - unsigned char m[BUFFER_SIZE]; /* message */ - unsigned char sm[BUFFER_SIZE + crypto_sign_BYTES]; /* signed message */ - unsigned long long int mlen; /* message length */ - unsigned long long int smlen; /* signed length */ + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ + unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ + unsigned char m[MAX_INPUT_SIZE]; /* message */ + unsigned char sm[MAX_INPUT_SIZE + crypto_sign_BYTES]; /* signed message */ + unsigned long long int mlen; /* message length */ + unsigned long long int smlen; /* signed length */ int r; puts("Example: crypto_sign\n"); diff --git a/demos/stream.c b/demos/stream.c index f5a89434..fa697e55 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -25,8 +25,8 @@ stream(void) { unsigned char k[crypto_stream_KEYBYTES]; /* secret key */ unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */ - unsigned char m[BUFFER_SIZE]; /* plain-text */ - unsigned char c[BUFFER_SIZE]; /* cipher-text */ + unsigned char m[MAX_INPUT_SIZE]; /* plain-text */ + unsigned char c[MAX_INPUT_SIZE]; /* cipher-text */ size_t mlen; /* length */ int r; From b92cc46432f41801114ad3576defd8f92944d067 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:40:37 +0200 Subject: [PATCH 17/69] Crank up the max input size; use stddef.h instead of stdlib.h for size_t --- demos/demo_utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/demo_utils.h b/demos/demo_utils.h index 619bb1fa..bd915cdd 100644 --- a/demos/demo_utils.h +++ b/demos/demo_utils.h @@ -4,9 +4,9 @@ #ifndef DEMO_UTILS_H #define DEMO_UTILS_H -#include +#include -#define MAX_INPUT_SIZE 128 /* size of all input buffers in the demo */ +#define MAX_INPUT_SIZE 4096 /* max size of all input buffers in the demo */ void print_hex(const void *bin, const size_t bin_len); size_t prompt_input(char *prompt, char *input, const size_t max_input_len); From efbd347cbb579ad1ee1dd8e80058fbc822b5ad29 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:46:17 +0200 Subject: [PATCH 18/69] Remove inappropriate sodium_memzero() calls --- demos/auth.c | 2 +- demos/box.c | 5 +---- demos/generichash.c | 2 +- demos/generichashstream.c | 2 +- demos/onetimeauth.c | 4 +--- demos/shorthash.c | 2 +- demos/sign.c | 3 +-- demos/stream.c | 3 +-- 8 files changed, 8 insertions(+), 15 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index c25e95c9..154a0e18 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -32,7 +32,7 @@ auth(void) * function which reads exactly n bytes into a buffer to * prevent buffer overflows. */ - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); puts("Your key that you entered"); print_hex(k, sizeof k); diff --git a/demos/box.c b/demos/box.c index 796993e2..a3355196 100644 --- a/demos/box.c +++ b/demos/box.c @@ -104,11 +104,8 @@ box(void) if (r == 0) printf("Plaintext: %s\n\n", m); - 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(bob_sk, sizeof bob_sk); /* wipe sensitive data */ sodium_memzero(alice_sk, sizeof alice_sk); - sodium_memzero(n, sizeof n); sodium_memzero(m, sizeof m); sodium_memzero(c, sizeof c); return r; diff --git a/demos/generichash.c b/demos/generichash.c index 05527cf3..9b284ff0 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -28,7 +28,7 @@ generichash(void) puts("Example: crypto_generichash\n"); - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); mlen = prompt_input("Input your message > ", (char*)m, sizeof m); diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 3da5d1bd..30ef6133 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -26,7 +26,7 @@ generichashstream(void) puts("Example: crypto_generichashstream\n"); - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); putchar('\n'); diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index 7a6dd9d6..e08261e8 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -26,8 +26,6 @@ onetimeauth(void) size_t mlen; /* message length */ int r; - sodium_memzero(k, sizeof k); /* must zero the key */ - puts("Example: crypto_onetimeauth\n"); /* @@ -36,7 +34,7 @@ onetimeauth(void) * function which reads exactly n bytes into a buffer to * prevent buffer overflows. */ - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); puts("Your key that you entered"); print_hex(k, sizeof k); diff --git a/demos/shorthash.c b/demos/shorthash.c index 26326ad3..52b534ab 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -25,7 +25,7 @@ shorthash(void) puts("Example: crypto_shorthash\n"); - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); mlen = prompt_input("Input your message > ", (char*)m, sizeof m); diff --git a/demos/sign.c b/demos/sign.c index 13d66995..57b13966 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -74,8 +74,7 @@ sign(void) if (r == 0) printf("Message: %s\n\n", m); - sodium_memzero(pk, sizeof pk); /* wipe sensitive data */ - sodium_memzero(sk, sizeof sk); + sodium_memzero(sk, sizeof sk); /* wipe sensitive data */ sodium_memzero(m, sizeof m); sodium_memzero(sm, sizeof sm); return r; diff --git a/demos/stream.c b/demos/stream.c index fa697e55..d9588116 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -32,7 +32,7 @@ stream(void) puts("Example: crypto_stream\n"); - sodium_memzero(k, sizeof k); + memset(k, 0, sizeof k); prompt_input("Input your key > ", (char*)k, sizeof k); putchar('\n'); @@ -68,7 +68,6 @@ stream(void) printf("Plaintext: %s\n\n", m); sodium_memzero(k, sizeof k); /* wipe sensitive data */ - sodium_memzero(n, sizeof n); sodium_memzero(m, sizeof m); sodium_memzero(c, sizeof c); return r; From a748029632365ed8b24dea446e8e8c6e8a8af386 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:47:49 +0200 Subject: [PATCH 19/69] demo_utils -> utils for consistency with other demo files --- demos/auth.c | 2 +- demos/box.c | 2 +- demos/generichash.c | 2 +- demos/generichashstream.c | 2 +- demos/hash.c | 2 +- demos/onetimeauth.c | 2 +- demos/shorthash.c | 2 +- demos/sign.c | 2 +- demos/stream.c | 2 +- demos/{demo_utils.c => utils.c} | 2 +- demos/{demo_utils.h => utils.h} | 0 11 files changed, 10 insertions(+), 10 deletions(-) rename demos/{demo_utils.c => utils.c} (98%) rename demos/{demo_utils.h => utils.h} (100%) diff --git a/demos/auth.c b/demos/auth.c index 154a0e18..eed4fde9 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Full featured authentication which is used to verify that the message diff --git a/demos/box.c b/demos/box.c index a3355196..e69460b6 100644 --- a/demos/box.c +++ b/demos/box.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Shows how crypto_box works using Bob and Alice with a simple message. diff --git a/demos/generichash.c b/demos/generichash.c index 9b284ff0..6c0508f5 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Generic hash is intended as a variable output hash with enough strength diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 30ef6133..08d840ce 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Streaming variant of generic hash. This has the ability to hash diff --git a/demos/hash.c b/demos/hash.c index dbdcb278..2c765819 100644 --- a/demos/hash.c +++ b/demos/hash.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * The library ships with a one-shot SHA-512 implementation. Simply allocate diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index e08261e8..7e8f193a 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * This method is only effective for a single use per key. The benefit is diff --git a/demos/shorthash.c b/demos/shorthash.c index 52b534ab..a0317edc 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Short hash is a fast algorithm intended for hash tables and anything diff --git a/demos/sign.c b/demos/sign.c index 57b13966..a7132790 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Signs a message with secret key which will authenticate a message. diff --git a/demos/stream.c b/demos/stream.c index d9588116..4d9fe1ca 100644 --- a/demos/stream.c +++ b/demos/stream.c @@ -8,7 +8,7 @@ #include /* library header */ -#include "demo_utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by demos */ /* * Stream utilizes a nonce to generate a sequence of bytes. The library has diff --git a/demos/demo_utils.c b/demos/utils.c similarity index 98% rename from demos/demo_utils.c rename to demos/utils.c index e56abba0..1a1fe429 100644 --- a/demos/demo_utils.c +++ b/demos/utils.c @@ -8,7 +8,7 @@ #include -#include "demo_utils.h" +#include "utils.h" /* ================================================================== * * utility functions * diff --git a/demos/demo_utils.h b/demos/utils.h similarity index 100% rename from demos/demo_utils.h rename to demos/utils.h From 4ea111bcb58cd903ea40d94893a0d08fd6aa68e5 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 15:49:57 +0200 Subject: [PATCH 20/69] Remove hash.c and stream.c for now. --- demos/hash.c | 45 --------------------------- demos/stream.c | 83 -------------------------------------------------- 2 files changed, 128 deletions(-) delete mode 100644 demos/hash.c delete mode 100644 demos/stream.c diff --git a/demos/hash.c b/demos/hash.c deleted file mode 100644 index 2c765819..00000000 --- a/demos/hash.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "utils.h" /* utility functions shared by demos */ - -/* - * The library ships with a one-shot SHA-512 implementation. Simply allocate - * all desired data into a single continuous buffer. - */ -static void -hash(void) -{ - unsigned char h[crypto_hash_BYTES]; /* hash output */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - size_t mlen; /* length */ - - puts("Example: crypto_hash\n"); - - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); - putchar('\n'); - - printf("Hashing message with %s\n", crypto_hash_primitive()); - crypto_hash(h, m, mlen); - fputs("Hash: ", stdout); - print_hex(h, sizeof h); - putchar('\n'); - putchar('\n'); -} - -int -main(void) -{ - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - - hash(); - return 0; -} diff --git a/demos/stream.c b/demos/stream.c deleted file mode 100644 index 4d9fe1ca..00000000 --- a/demos/stream.c +++ /dev/null @@ -1,83 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "utils.h" /* utility functions shared by demos */ - -/* - * 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. - * - * 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) -{ - unsigned char k[crypto_stream_KEYBYTES]; /* secret key */ - unsigned char n[crypto_stream_NONCEBYTES]; /* message nonce */ - unsigned char m[MAX_INPUT_SIZE]; /* plain-text */ - unsigned char c[MAX_INPUT_SIZE]; /* cipher-text */ - size_t mlen; /* length */ - int r; - - puts("Example: crypto_stream\n"); - - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); - 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'); - - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); - 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); - if (r == 0) - printf("Plaintext: %s\n\n", m); - - sodium_memzero(k, sizeof k); /* wipe sensitive data */ - sodium_memzero(m, sizeof m); - sodium_memzero(c, sizeof c); - return r; -} - -int -main(void) -{ - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - - return stream() != 0; -} From 67305902eefeafeff7e59efed41d32bb1eaf554f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 16:10:07 +0200 Subject: [PATCH 21/69] DRY --- demos/auth.c | 3 +-- demos/box.c | 3 +-- demos/generichash.c | 5 ++--- demos/generichashstream.c | 5 ++--- demos/onetimeauth.c | 3 +-- demos/shorthash.c | 5 ++--- demos/sign.c | 3 +-- demos/utils.c | 7 +++++++ demos/utils.h | 10 +++++++--- 9 files changed, 24 insertions(+), 20 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index eed4fde9..fabc58f4 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -63,8 +63,7 @@ auth(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); + init(); return auth() != 0; } diff --git a/demos/box.c b/demos/box.c index e69460b6..a2629140 100644 --- a/demos/box.c +++ b/demos/box.c @@ -114,8 +114,7 @@ box(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); + init(); return box() != 0; } diff --git a/demos/generichash.c b/demos/generichash.c index 6c0508f5..ecb26d17 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -45,9 +45,8 @@ generichash(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - + init(); generichash(); + return 0; } diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 08d840ce..6f8e2119 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -55,9 +55,8 @@ generichashstream(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - + init(); generichashstream(); + return 0; } diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c index 7e8f193a..e344dc64 100644 --- a/demos/onetimeauth.c +++ b/demos/onetimeauth.c @@ -65,8 +65,7 @@ onetimeauth(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); + init(); return onetimeauth() != 0; } diff --git a/demos/shorthash.c b/demos/shorthash.c index a0317edc..d567b6c8 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -42,9 +42,8 @@ shorthash(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); - + init(); shorthash(); + return 0; } diff --git a/demos/sign.c b/demos/sign.c index a7132790..6107c642 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -83,8 +83,7 @@ sign(void) int main(void) { - sodium_init(); - printf("Using LibSodium %s\n", sodium_version_string()); + init(); return sign() != 0; } diff --git a/demos/utils.c b/demos/utils.c index 1a1fe429..a24b68e4 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -74,3 +74,10 @@ print_verification(int r) else puts("Failure\n"); } + +void +init(void) +{ + sodium_init(); + printf("Using libsodium %s\n", sodium_version_string()); +} diff --git a/demos/utils.h b/demos/utils.h index bd915cdd..d6890d16 100644 --- a/demos/utils.h +++ b/demos/utils.h @@ -1,15 +1,19 @@ /* * Utility functions shared by all the demo programs. */ -#ifndef DEMO_UTILS_H -#define DEMO_UTILS_H +#ifndef UTILS_H +#define UTILS_H #include #define MAX_INPUT_SIZE 4096 /* max size of all input buffers in the demo */ void print_hex(const void *bin, const size_t bin_len); + size_t prompt_input(char *prompt, char *input, const size_t max_input_len); + void print_verification(int r); -#endif /* DEMO_UTILS_H */ +void init(void); + +#endif /* UTILS_H */ From 8920cde3a3654d0b1e42938e5ce9de7bf6785404 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 16:29:57 +0200 Subject: [PATCH 22/69] Use meaningful variable names instead of having to comment them --- demos/auth.c | 50 +++++++++++++++++++++++--------------------------- demos/utils.c | 4 ++-- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index fabc58f4..b56a161f 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -18,46 +18,42 @@ static int auth(void) { - unsigned char k[crypto_auth_KEYBYTES]; /* key */ - unsigned char a[crypto_auth_BYTES]; /* authentication token */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - size_t mlen; /* message length */ - int r; + unsigned char key[crypto_auth_KEYBYTES]; + unsigned char mac[crypto_auth_BYTES]; + unsigned char message[MAX_INPUT_SIZE]; + size_t message_len; + int ret; puts("Example: crypto_auth\n"); - /* - * Keys are entered as ascii values. The key is zeroed to - * maintain consistency. Input is read through a special - * function which reads exactly n bytes into a buffer to - * prevent buffer overflows. - */ - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); - puts("Your key that you entered"); - print_hex(k, sizeof k); + memset(key, 0, sizeof key); + prompt_input("Enter a key > ", (char*)key, sizeof key); + puts("Complete key:"); + print_hex(key, sizeof key); putchar('\n'); - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); + message_len = prompt_input("Enter a message > ", + (char*)message, sizeof message); putchar('\n'); printf("Generating %s authentication...\n", crypto_auth_primitive()); - crypto_auth(a, m, mlen, k); + crypto_auth(mac, message, message_len, key); - puts("Format: authentication token::message"); - print_hex(a, sizeof a); + puts("Format: authentication tag::message"); + print_hex(mac, sizeof mac); fputs("::", stdout); - puts((const char*)m); + puts((const char*)message); putchar('\n'); - puts("Verifying authentication..."); - r = crypto_auth_verify(a, m, mlen, k); - print_verification(r); + puts("Verifying authentication tag..."); + ret = crypto_auth_verify(mac, message, message_len, key); + print_verification(ret); - sodium_memzero(k, sizeof k); /* wipe sensitive data */ - sodium_memzero(a, sizeof a); - sodium_memzero(m, sizeof m); - return r; + sodium_memzero(key, sizeof key); /* wipe sensitive data */ + sodium_memzero(mac, sizeof mac); + sodium_memzero(message, sizeof message); + + return ret; } int diff --git a/demos/utils.c b/demos/utils.c index a24b68e4..ee7f6fee 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -70,9 +70,9 @@ void print_verification(int r) { if (r == 0) - puts("Success\n"); + puts("Success!\n"); else - puts("Failure\n"); + puts("Failure.\n"); } void From 5b4a40e1f6f8a3bc6dcea108a37f57e4c7435b8f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 16:34:06 +0200 Subject: [PATCH 23/69] Update auth.c demo description. --- demos/auth.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index b56a161f..e1ae9b85 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -11,9 +11,27 @@ #include "utils.h" /* utility functions shared by demos */ /* - * Full featured authentication which is used to verify that the message - * comes from the expected person. It should be safe to keep the same key - * for multiple messages. + * This operation computes an authentication tag for a message and a + * secret key, and provides a way to verify that a given tag is valid + * for a given message and a key. + * + * The function computing the tag deterministic: the same (message, + * key) tuple will always produce the same output. + * + * However, even if the message is public, knowing the key is + * required in order to be able to compute a valid tag. Therefore, + * the key should remain confidential. The tag, however, can be + * public. + * + * A typical use case is: + * + * - A prepares a message, add an authentication tag, sends it to B + * - A doesn't store the message + * - Later on, B sends the message and the authentication tag to A + * - A uses the authentication tag to verify that it created this message. + * + * This operation does not encrypt the message. It only computes and + * verifies an authentication tag. */ static int auth(void) @@ -50,8 +68,6 @@ auth(void) print_verification(ret); sodium_memzero(key, sizeof key); /* wipe sensitive data */ - sodium_memzero(mac, sizeof mac); - sodium_memzero(message, sizeof message); return ret; } From 57fb685157bdf7c3cc733a16aae416da63e53851 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 16:54:42 +0200 Subject: [PATCH 24/69] Use meaningful variable names in box.c --- demos/box.c | 121 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 46 deletions(-) diff --git a/demos/box.c b/demos/box.c index a2629140..4ae41649 100644 --- a/demos/box.c +++ b/demos/box.c @@ -11,30 +11,57 @@ #include "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. + * Using public-key authenticated encryption, Bob can encrypt a + * confidential message specifically for Alice, using Alice's public + * key. * - * Encrypted messages will be 16 bytes longer because a 16 byte - * authentication token will be prepended to the message. + * 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. * - * Note the same nonce must not be used; it should be safe to use a counter. + * 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. */ static int box(void) { - unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob public */ - unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob secret */ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob's public key */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob's secret key */ - unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice public */ - unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice secret */ + unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice's public key */ + unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice's secret key */ - 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; + unsigned char nonce[crypto_box_NONCEBYTES]; + unsigned char message[MAX_INPUT_SIZE]; + unsigned char ciphertext[crypto_box_MACBYTES + MAX_INPUT_SIZE]; + size_t message_len; + size_t ciphertext_len; + int ret; puts("Example: crypto_box_easy\n"); @@ -43,72 +70,74 @@ box(void) crypto_box_keypair(alice_pk, alice_sk); /* generate Alice's keys */ puts("Bob"); - fputs("Public: ", stdout); + fputs("Public key: ", stdout); print_hex(bob_pk, sizeof bob_pk); putchar('\n'); - fputs("Secret: ", stdout); + fputs("Secret key: ", stdout); print_hex(bob_sk, sizeof bob_sk); putchar('\n'); putchar('\n'); puts("Alice"); - fputs("Public: ", stdout); + fputs("Public key: ", stdout); print_hex(alice_pk, sizeof alice_pk); putchar('\n'); - fputs("Secret: ", stdout); + fputs("Secret key: ", stdout); print_hex(alice_sk, sizeof alice_sk); putchar('\n'); putchar('\n'); - /* nonce must be generated per message, safe to send with message */ + /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); - randombytes_buf(n, sizeof n); + randombytes_buf(nonce, sizeof nonce); fputs("Nonce: ", stdout); - print_hex(n, sizeof n); + print_hex(nonce, sizeof nonce); putchar('\n'); putchar('\n'); /* read input */ - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); + message_len = prompt_input("Enter a message > ", + (char*)message, sizeof message); - puts("Notice there is no padding"); - print_hex(m, mlen); + print_hex(message, message_len); putchar('\n'); putchar('\n'); - /* encrypt the message */ - printf("Encrypting with %s\n\n", crypto_box_primitive()); - crypto_box_easy(c, m, mlen, n, alice_pk, bob_sk); + /* 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; - /* sent message */ - puts("Bob sending message...\n"); + /* send the ciphertext */ + puts("Bob sends the ciphertext...\n"); + printf("Ciphertext len: %zu bytes - Original message length: %zu bytes\n", + ciphertext_len, message_len); puts("Notice the prepended 16 byte authentication token"); - puts("Format: nonce::message"); + puts("Format: nonce::encrypted_message"); fputs("Ciphertext: ", stdout); - print_hex(n, sizeof n); + print_hex(nonce, sizeof nonce); fputs("::", stdout); - print_hex(c, mlen + crypto_box_MACBYTES); + print_hex(ciphertext, ciphertext_len); putchar('\n'); putchar('\n'); /* decrypt the message */ - puts("Alice opening message..."); - 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); + 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); putchar('\n'); - print_verification(r); - if (r == 0) - printf("Plaintext: %s\n\n", m); + print_verification(ret); + if (ret == 0) + printf("Plaintext: %s\n\n", message); sodium_memzero(bob_sk, sizeof bob_sk); /* wipe sensitive data */ sodium_memzero(alice_sk, sizeof alice_sk); - sodium_memzero(m, sizeof m); - sodium_memzero(c, sizeof c); - return r; + sodium_memzero(message, sizeof message); + sodium_memzero(ciphertext, sizeof ciphertext); + + return ret; } int From a549360201f2cbca27e285b33dff817ad1e6dff3 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 16:57:51 +0200 Subject: [PATCH 25/69] Display actual data, do not suggest that the nonce is part of the ciphertext --- demos/auth.c | 4 +--- demos/box.c | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index e1ae9b85..bcabbb69 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -57,10 +57,8 @@ auth(void) printf("Generating %s authentication...\n", crypto_auth_primitive()); crypto_auth(mac, message, message_len, key); - puts("Format: authentication tag::message"); + fputs("Authentication tag: ", stdout); print_hex(mac, sizeof mac); - fputs("::", stdout); - puts((const char*)message); putchar('\n'); puts("Verifying authentication tag..."); diff --git a/demos/box.c b/demos/box.c index 4ae41649..7e33d9e4 100644 --- a/demos/box.c +++ b/demos/box.c @@ -112,11 +112,11 @@ box(void) puts("Bob sends the ciphertext...\n"); printf("Ciphertext len: %zu bytes - Original message length: %zu bytes\n", ciphertext_len, message_len); - puts("Notice the prepended 16 byte authentication token"); - puts("Format: nonce::encrypted_message"); + puts("Notice the prepended 16 byte authentication token\n"); + fputs("Nonce: ", stdout); + print_hex(ciphertext, ciphertext_len); + putchar('\n'); fputs("Ciphertext: ", stdout); - print_hex(nonce, sizeof nonce); - fputs("::", stdout); print_hex(ciphertext, ciphertext_len); putchar('\n'); putchar('\n'); From 3eef43eeadc44efe95c1ff5c131e6422fe4f9866 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 17:12:16 +0200 Subject: [PATCH 26/69] Remove onetimeauth demo --- demos/onetimeauth.c | 71 --------------------------------------------- 1 file changed, 71 deletions(-) delete mode 100644 demos/onetimeauth.c diff --git a/demos/onetimeauth.c b/demos/onetimeauth.c deleted file mode 100644 index e344dc64..00000000 --- a/demos/onetimeauth.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "utils.h" /* utility functions shared by demos */ - -/* - * This method is only effective for a single use per key. The benefit is - * the algorithm is quicker and output is half the size of auth. It is easy - * to see how weak the algorithm is when you use a one letter key. - * - * Note that the same key must not be used more than once. - */ -static int -onetimeauth(void) -{ - unsigned char k[crypto_onetimeauth_KEYBYTES]; /* key */ - unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - size_t mlen; /* message length */ - int r; - - puts("Example: crypto_onetimeauth\n"); - - /* - * Keys are entered as ascii values. The key is zeroed to - * maintain consistency. Input is read through a special - * function which reads exactly n bytes into a buffer to - * prevent buffer overflows. - */ - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); - puts("Your key that you entered"); - print_hex(k, sizeof k); - putchar('\n'); - - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); - putchar('\n'); - - printf("Generating %s authentication...\n", crypto_onetimeauth_primitive()); - crypto_onetimeauth(a, m, mlen, k); - - puts("Format: authentication token::message"); - print_hex(a, sizeof a); - fputs("::", stdout); - puts((const char*)m); - putchar('\n'); - - puts("Verifying authentication..."); - r = crypto_onetimeauth_verify(a, m, mlen, k); - print_verification(r); - - sodium_memzero(k, sizeof k); /* wipe sensitive data */ - sodium_memzero(a, sizeof a); - sodium_memzero(m, sizeof m); - return r; -} - -int -main(void) -{ - init(); - - return onetimeauth() != 0; -} From a14dc377bf26b88885267c3239eea7100277aa24 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 17:17:36 +0200 Subject: [PATCH 27/69] Use meaningful variable names in shorthash demo --- demos/shorthash.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/demos/shorthash.c b/demos/shorthash.c index d567b6c8..11d54c16 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -11,30 +11,48 @@ #include "utils.h" /* utility functions shared by demos */ /* - * Short hash is a fast algorithm intended for hash tables and anything - * else that does not require data integrity. There is the added benefit - * of a key which will alter the output of the hash. + * Many applications and programming language implementations were + * recently found to be vulnerable to denial-of-service attacks when + * a hash function with weak security guarantees, such as Murmurhash + * 3, was used to construct a hash table. + * + * In order to address this, Sodium provides the crypto_shorthash() + * function, which outputs short but unpredictable (without knowing + * the secret key) values suitable for picking a list in a hash table + * for a given key. + * + * This function is optimized for short inputs. + * + * The output of this function is only 64 bits. Therefore, it should + * not be considered collision-resistant. + * + * Use cases: + * + * - Hash tables + * - Probabilistic data structures such as Bloom filters + * - Integrity checking in interactive protocols */ void shorthash(void) { - unsigned char k[crypto_shorthash_KEYBYTES]; /* key */ - unsigned char h[crypto_shorthash_BYTES]; /* hash output */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - size_t mlen; /* length */ + unsigned char key[crypto_shorthash_KEYBYTES]; + unsigned char hash[crypto_shorthash_BYTES]; + unsigned char message[MAX_INPUT_SIZE]; + size_t message_len; puts("Example: crypto_shorthash\n"); - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); + memset(key, 0, sizeof key); + prompt_input("Enter a key > ", (char*)key, sizeof key); - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); + message_len = prompt_input("Enter a message > ", + (char*)message, sizeof message); putchar('\n'); - printf("Hashing message with %s\n", crypto_shorthash_primitive()); - crypto_shorthash(h, m, mlen, k); + printf("Hashing the message with %s\n", crypto_shorthash_primitive()); + crypto_shorthash(hash, message, message_len, key); fputs("Hash: ", stdout); - print_hex(h, sizeof h); + print_hex(hash, sizeof hash); putchar('\n'); putchar('\n'); } From cf6106e022afa0cfbf7df459749d4c4a3bf5e08f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 17:24:20 +0200 Subject: [PATCH 28/69] + box_detached demo --- demos/box.c | 6 +- demos/box_detached.c | 149 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 demos/box_detached.c diff --git a/demos/box.c b/demos/box.c index 7e33d9e4..09693f42 100644 --- a/demos/box.c +++ b/demos/box.c @@ -108,13 +108,13 @@ box(void) crypto_box_easy(ciphertext, message, message_len, nonce, alice_pk, bob_sk); ciphertext_len = crypto_box_MACBYTES + message_len; - /* send the ciphertext */ - puts("Bob sends the ciphertext...\n"); + /* send the nonce and the ciphertext */ + puts("Bob sends the nonce and the ciphertext...\n"); printf("Ciphertext len: %zu bytes - Original message length: %zu bytes\n", ciphertext_len, message_len); puts("Notice the prepended 16 byte authentication token\n"); fputs("Nonce: ", stdout); - print_hex(ciphertext, ciphertext_len); + print_hex(nonce, nonce_len); putchar('\n'); fputs("Ciphertext: ", stdout); print_hex(ciphertext, ciphertext_len); diff --git a/demos/box_detached.c b/demos/box_detached.c new file mode 100644 index 00000000..c37307cb --- /dev/null +++ b/demos/box_detached.c @@ -0,0 +1,149 @@ +/* + * GraxRabble + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "utils.h" /* utility functions shared by demos */ + +/* + * Using public-key authenticated encryption, Bob can encrypt a + * confidential message specifically for Alice, using Alice's public + * key. + * + * 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. + * + * 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. + */ +static int +box_detached(void) +{ + unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; /* Bob's public key */ + unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; /* Bob's secret key */ + + unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; /* Alice's public key */ + unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice's secret key */ + + unsigned char nonce[crypto_box_NONCEBYTES]; + unsigned char message[MAX_INPUT_SIZE]; + unsigned char mac[crypto_box_MACBYTES]; + unsigned char ciphertext[MAX_INPUT_SIZE]; + size_t message_len; + int ret; + + puts("Example: crypto_box_detached\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 key: ", stdout); + print_hex(bob_pk, sizeof bob_pk); + putchar('\n'); + fputs("Secret key: ", stdout); + print_hex(bob_sk, sizeof bob_sk); + putchar('\n'); + putchar('\n'); + + puts("Alice"); + fputs("Public key: ", stdout); + print_hex(alice_pk, sizeof alice_pk); + putchar('\n'); + fputs("Secret key: ", stdout); + print_hex(alice_sk, sizeof alice_sk); + putchar('\n'); + putchar('\n'); + + /* nonce must be unique per (key, message) - it can be public and deterministic */ + puts("Generating nonce..."); + randombytes_buf(nonce, sizeof nonce); + fputs("Nonce: ", stdout); + print_hex(nonce, sizeof nonce); + putchar('\n'); + putchar('\n'); + + /* read input */ + message_len = prompt_input("Enter a message > ", + (char*)message, sizeof message); + + print_hex(message, message_len); + putchar('\n'); + putchar('\n'); + + /* encrypt and authenticate the message */ + printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive()); + crypto_box_detached(ciphertext, mac, message, message_len, nonce, + alice_pk, bob_sk); + + /* send the nonce, the MAC and the ciphertext */ + puts("Bob sends the nonce, the MAC and the ciphertext...\n"); + fputs("Nonce: ", stdout); + print_hex(nonce, sizeof nonce); + putchar('\n'); + fputs("MAC: ", stdout); + print_hex(mac, sizeof mac); + putchar('\n'); + fputs("Ciphertext: ", stdout); + print_hex(ciphertext, message_len); + putchar('\n'); + putchar('\n'); + + /* decrypt the message */ + 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); + putchar('\n'); + + print_verification(ret); + if (ret == 0) + printf("Plaintext: %s\n\n", message); + + sodium_memzero(bob_sk, sizeof bob_sk); /* wipe sensitive data */ + 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; +} From 9060457fac6e9b4e949a9187495a8eab50406a84 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 18:10:28 +0200 Subject: [PATCH 29/69] Make prompt_input accept fixed-length and variable-length input. Now that strings have the correct size, but no trailing \0, puts() cannot safely be used any more. --- demos/auth.c | 9 ++---- demos/box.c | 3 +- demos/box_detached.c | 3 +- demos/generichash.c | 66 +++++++++++++++++++++++++++++---------- demos/generichashstream.c | 7 ++--- demos/shorthash.c | 6 ++-- demos/sign.c | 4 +-- demos/utils.c | 40 ++++++++++++++++++------ demos/utils.h | 3 +- 9 files changed, 94 insertions(+), 47 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index bcabbb69..f7179e1d 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -44,14 +44,9 @@ auth(void) puts("Example: crypto_auth\n"); - memset(key, 0, sizeof key); - prompt_input("Enter a key > ", (char*)key, sizeof key); - puts("Complete key:"); - print_hex(key, sizeof key); - putchar('\n'); + prompt_input("a key", (char*)key, sizeof key, 0); - message_len = prompt_input("Enter a message > ", - (char*)message, sizeof message); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); putchar('\n'); printf("Generating %s authentication...\n", crypto_auth_primitive()); diff --git a/demos/box.c b/demos/box.c index 09693f42..d81f4543 100644 --- a/demos/box.c +++ b/demos/box.c @@ -96,8 +96,7 @@ box(void) putchar('\n'); /* read input */ - message_len = prompt_input("Enter a message > ", - (char*)message, sizeof message); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); putchar('\n'); diff --git a/demos/box_detached.c b/demos/box_detached.c index c37307cb..ec3c834a 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -96,8 +96,7 @@ box_detached(void) putchar('\n'); /* read input */ - message_len = prompt_input("Enter a message > ", - (char*)message, sizeof message); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); putchar('\n'); diff --git a/demos/generichash.c b/demos/generichash.c index ecb26d17..1e4d15fc 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -11,34 +11,68 @@ #include "utils.h" /* utility functions shared by demos */ /* - * Generic hash is intended as a variable output hash with enough strength - * to ensure data integrity. The hash out put is also able to vary in size. - * Key is optional and is able to vary in size. + * This function computes a fixed-length fingerprint for an arbitrary long message. * - * Note that it is recommended to stay within the range of MIN and MAX - * output because larger output will produce gaps. + * Sample use cases: + * + * File integrity checking + * Creating unique identifiers to index arbitrary long data + * + * The crypto_generichash() function puts a fingerprint of the + * message in whose length is inlen bytes into out. The output size + * can be chosen by the application. + * + * The minimum recommended output size is crypto_generichash_BYTES. + * This size makes it practically impossible for two messages to + * produce the same fingerprint. + * + * But for specific use cases, the size can be any value between + * crypto_generichash_BYTES_MIN (included) and + * crypto_generichash_BYTES_MAX (included). + * + * key can be NULL and keylen can be 0. In this case, a message will + * always have the same fingerprint, similar to the MD5 or SHA-1 + * functions for which crypto_generichash() is a faster and more + * secure alternative. + * + * But a key can also be specified. A message will always have the + * same fingerprint for a given key, but different keys used to hash + * the same message are very likely to produce distinct fingerprints. + * + * In particular, the key can be used to make sure that different + * applications generate different fingerprints even if they process + * the same data. + * + * The recommended key size is crypto_generichash_KEYBYTES bytes. + * + * However, the key size can by any value between + * crypto_generichash_KEYBYTES_MIN (included) and + * crypto_generichash_KEYBYTES_MAX (included). */ void generichash(void) { - unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ - unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - size_t mlen; /* length */ + unsigned char key[crypto_generichash_KEYBYTES_MAX]; + unsigned char hash[crypto_generichash_BYTES_MIN]; + unsigned char message[MAX_INPUT_SIZE]; + size_t message_len; + size_t key_len; puts("Example: crypto_generichash\n"); - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); + key_len = prompt_input("a key", (char*)key, sizeof key, 1); - mlen = prompt_input("Input your message > ", (char*)m, sizeof m); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); putchar('\n'); printf("Hashing message with %s\n", crypto_generichash_primitive()); - crypto_generichash(h, sizeof h, m, mlen, k, sizeof k); - fputs("Hash: ", stdout); - print_hex(h, sizeof h); - putchar('\n'); + if (crypto_generichash(hash, sizeof hash, message, message_len, + key, key_len) != 0) { + puts("Couldn't hash the message, probably due to the key length"); + } else { + fputs("Hash: ", stdout); + print_hex(hash, sizeof hash); + } putchar('\n'); } diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 6f8e2119..25cb2b88 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -26,8 +26,7 @@ generichashstream(void) puts("Example: crypto_generichashstream\n"); - memset(k, 0, sizeof k); - prompt_input("Input your key > ", (char*)k, sizeof k); + prompt_input("a key", (char*)k, sizeof k, 0); putchar('\n'); printf("Hashing message with %s\n", crypto_generichash_primitive()); @@ -35,8 +34,8 @@ generichashstream(void) /* initialize the stream */ crypto_generichash_init(&state, k, sizeof k, sizeof h); - while (1) { - mlen = prompt_input("> ", (char*)m, sizeof m); + for(;;) { + mlen = prompt_input("the next part of the message", (char*)m, sizeof m); if (mlen == 0) break; diff --git a/demos/shorthash.c b/demos/shorthash.c index 11d54c16..b526397d 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -42,11 +42,9 @@ shorthash(void) puts("Example: crypto_shorthash\n"); - memset(key, 0, sizeof key); - prompt_input("Enter a key > ", (char*)key, sizeof key); + prompt_input("a key", (char*)key, sizeof key, 0); - message_len = prompt_input("Enter a message > ", - (char*)message, sizeof message); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); putchar('\n'); printf("Hashing the message with %s\n", crypto_shorthash_primitive()); diff --git a/demos/sign.c b/demos/sign.c index 6107c642..49b6568e 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -43,8 +43,8 @@ sign(void) puts("\n"); /* read input */ - mlen = prompt_input("Input your message > ", (char*)m, - sizeof m - crypto_sign_BYTES); + mlen = prompt_input("a message", (char*)m, + sizeof m - crypto_sign_BYTES, 1); putc('\n', stdout); puts("Notice the message has no prepended padding"); diff --git a/demos/utils.c b/demos/utils.c index ee7f6fee..428955f0 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -45,22 +45,44 @@ print_hex(const void *bin, const size_t bin_len) * trailing newline characters. */ size_t -prompt_input(char *prompt, char *input, const size_t max_input_len) +prompt_input(const char *prompt, char *input, const size_t max_input_len, + int variable_length) { + char input_tmp[MAX_INPUT_SIZE + 1U]; size_t actual_input_len; - fputs(prompt, stdout); + if (variable_length != 0) { + printf("Enter %s (%zu bytes max) > ", prompt, max_input_len); + } else { + printf("Enter %s (%zu bytes) > ", prompt, max_input_len); + } fflush(stdout); - fgets(input, max_input_len, stdin); /* grab input with room for \0 */ + fgets(input_tmp, sizeof input_tmp, stdin); + actual_input_len = strlen(input_tmp); - actual_input_len = strlen(input); - - /* trim excess new line */ - if (actual_input_len > 0 && input[actual_input_len - 1] == '\n') { - input[actual_input_len - 1] = '\0'; + /* trim \n */ + if (actual_input_len > 0 && input_tmp[actual_input_len - 1] == '\n') { + input_tmp[actual_input_len - 1] = '\0'; --actual_input_len; } - return actual_input_len; + + if (actual_input_len > max_input_len) { + printf("Warning: truncating input to %zu bytes\n", max_input_len); + actual_input_len = max_input_len; + } else if (actual_input_len < max_input_len && variable_length == 0) { + printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n", + max_input_len, actual_input_len); + memset(input, 0, max_input_len); + } else { + printf("Length: %zu bytes\n", actual_input_len); + } + + memcpy(input, input_tmp, actual_input_len); + if (variable_length == 0) { + return max_input_len; + } else { + return actual_input_len; + } } /* diff --git a/demos/utils.h b/demos/utils.h index d6890d16..b88347fd 100644 --- a/demos/utils.h +++ b/demos/utils.h @@ -10,7 +10,8 @@ void print_hex(const void *bin, const size_t bin_len); -size_t prompt_input(char *prompt, char *input, const size_t max_input_len); +size_t prompt_input(const char *prompt, char *input, const size_t max_input_len, + int variable_length); void print_verification(int r); From a67f42e015d658ab6985fd7543872e6fe41c94ff Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:00:05 +0200 Subject: [PATCH 30/69] sign demo: print signature and message separately --- demos/sign.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/demos/sign.c b/demos/sign.c index 49b6568e..dafbdb28 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -60,12 +60,12 @@ sign(void) putchar('\n'); putchar('\n'); - puts("Format: signature::message"); - fputs("Signed: ", stdout); + fputs("Signature: ", stdout); print_hex(sm, crypto_sign_BYTES); - fputs("::", stdout); - puts((const char*)sm + crypto_sign_BYTES); - putc('\n', stdout); + putchar('\n'); + fputs("Message: ", stdout); + fwrite(sm + crypto_sign_BYTES, 1U, smlen - crypto_sign_BYTES, stdout); + putchar('\n'); puts("Validating message..."); r = crypto_sign_open(m, &mlen, sm, smlen, pk); From 385b44aee5d7a4e6c0429be8d701c3e6eb1fb8e0 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:01:03 +0200 Subject: [PATCH 31/69] sign demo: fix weird max message length --- demos/sign.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/demos/sign.c b/demos/sign.c index dafbdb28..e728694a 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -43,9 +43,8 @@ sign(void) puts("\n"); /* read input */ - mlen = prompt_input("a message", (char*)m, - sizeof m - crypto_sign_BYTES, 1); - putc('\n', stdout); + mlen = prompt_input("a message", (char*)m, sizeof m, 1); + putchar('\n'); puts("Notice the message has no prepended padding"); print_hex(m, mlen); From c28886dc423ac42c5640ff0ef38dd88b8fb3b13e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:02:20 +0200 Subject: [PATCH 32/69] Demos: remove excessive \n --- demos/generichashstream.c | 1 - demos/shorthash.c | 1 - 2 files changed, 2 deletions(-) diff --git a/demos/generichashstream.c b/demos/generichashstream.c index 25cb2b88..1e3b84c8 100644 --- a/demos/generichashstream.c +++ b/demos/generichashstream.c @@ -48,7 +48,6 @@ generichashstream(void) fputs("Hash: ", stdout); print_hex(h, sizeof h); putchar('\n'); - putchar('\n'); } int diff --git a/demos/shorthash.c b/demos/shorthash.c index b526397d..14272192 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -52,7 +52,6 @@ shorthash(void) fputs("Hash: ", stdout); print_hex(hash, sizeof hash); putchar('\n'); - putchar('\n'); } int From 071c467ff331bdebbdc5fca96d3abe72c1355c69 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:05:15 +0200 Subject: [PATCH 33/69] box demos: do not attempt to print anything past the end of the plaintext --- demos/box.c | 8 +++++--- demos/box_detached.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/demos/box.c b/demos/box.c index d81f4543..f9b624f8 100644 --- a/demos/box.c +++ b/demos/box.c @@ -128,9 +128,11 @@ box(void) putchar('\n'); print_verification(ret); - if (ret == 0) - printf("Plaintext: %s\n\n", message); - + if (ret == 0) { + printf("Plaintext: "); + fwrite(message, 1U, message_len, stdout); + putchar('\n'); + } sodium_memzero(bob_sk, sizeof bob_sk); /* wipe sensitive data */ sodium_memzero(alice_sk, sizeof alice_sk); sodium_memzero(message, sizeof message); diff --git a/demos/box_detached.c b/demos/box_detached.c index ec3c834a..7cf8e171 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -128,9 +128,11 @@ box_detached(void) putchar('\n'); print_verification(ret); - if (ret == 0) - printf("Plaintext: %s\n\n", message); - + if (ret == 0) { + printf("Plaintext: "); + fwrite(message, 1U, message_len, stdout); + putchar('\n'); + } sodium_memzero(bob_sk, sizeof bob_sk); /* wipe sensitive data */ sodium_memzero(alice_sk, sizeof alice_sk); sodium_memzero(message, sizeof message); From b1bcecf086ce5bbbc12bd6edddaf39c01e3dd59b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:27:07 +0200 Subject: [PATCH 34/69] putc('\n', stdout) or putchar('\n') - pick one And remove duplicate putchar('\n') to improve code clarity --- demos/box.c | 5 ----- demos/box_detached.c | 5 ----- demos/sign.c | 10 ++++------ 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/demos/box.c b/demos/box.c index f9b624f8..7f44deeb 100644 --- a/demos/box.c +++ b/demos/box.c @@ -76,7 +76,6 @@ box(void) fputs("Secret key: ", stdout); print_hex(bob_sk, sizeof bob_sk); putchar('\n'); - putchar('\n'); puts("Alice"); fputs("Public key: ", stdout); @@ -85,7 +84,6 @@ box(void) fputs("Secret key: ", stdout); print_hex(alice_sk, sizeof alice_sk); putchar('\n'); - putchar('\n'); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); @@ -93,14 +91,12 @@ box(void) fputs("Nonce: ", stdout); print_hex(nonce, sizeof nonce); putchar('\n'); - putchar('\n'); /* read input */ message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); putchar('\n'); - putchar('\n'); /* encrypt and authenticate the message */ printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive()); @@ -118,7 +114,6 @@ box(void) fputs("Ciphertext: ", stdout); print_hex(ciphertext, ciphertext_len); putchar('\n'); - putchar('\n'); /* decrypt the message */ puts("Alice verifies and decrypts the ciphertext..."); diff --git a/demos/box_detached.c b/demos/box_detached.c index 7cf8e171..714ce1e8 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -76,7 +76,6 @@ box_detached(void) fputs("Secret key: ", stdout); print_hex(bob_sk, sizeof bob_sk); putchar('\n'); - putchar('\n'); puts("Alice"); fputs("Public key: ", stdout); @@ -85,7 +84,6 @@ box_detached(void) fputs("Secret key: ", stdout); print_hex(alice_sk, sizeof alice_sk); putchar('\n'); - putchar('\n'); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); @@ -93,14 +91,12 @@ box_detached(void) fputs("Nonce: ", stdout); print_hex(nonce, sizeof nonce); putchar('\n'); - putchar('\n'); /* read input */ message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); putchar('\n'); - putchar('\n'); /* encrypt and authenticate the message */ printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive()); @@ -118,7 +114,6 @@ box_detached(void) fputs("Ciphertext: ", stdout); print_hex(ciphertext, message_len); putchar('\n'); - putchar('\n'); /* decrypt the message */ puts("Alice verifies the MAC and decrypts the ciphertext..."); diff --git a/demos/sign.c b/demos/sign.c index e728694a..8bced0f7 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -37,19 +37,17 @@ sign(void) fputs("Public: ", stdout); print_hex(pk, sizeof pk); - putc('\n', stdout); + putchar('\n'); fputs("Secret: ", stdout); print_hex(sk, sizeof sk); - puts("\n"); + putchar('\n'); - /* read input */ mlen = prompt_input("a message", (char*)m, sizeof m, 1); putchar('\n'); puts("Notice the message has no prepended padding"); print_hex(m, mlen); putchar('\n'); - putchar('\n'); printf("Signing message with %s...\n", crypto_sign_primitive()); crypto_sign(sm, &smlen, m, mlen, sk); @@ -57,7 +55,6 @@ sign(void) puts("Notice the signed message has prepended signature"); print_hex(sm, smlen); putchar('\n'); - putchar('\n'); fputs("Signature: ", stdout); print_hex(sm, crypto_sign_BYTES); @@ -71,11 +68,12 @@ sign(void) print_verification(r); if (r == 0) - printf("Message: %s\n\n", m); + printf("Message: %s\n", m); sodium_memzero(sk, sizeof sk); /* wipe sensitive data */ sodium_memzero(m, sizeof m); sodium_memzero(sm, sizeof sm); + return r; } From 9b209f0078daa9361fc1c373a4fef59983587611 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:35:40 +0200 Subject: [PATCH 35/69] generichash_stream example --- demos/generichash.c | 2 +- demos/generichash_stream.c | 63 ++++++++++++++++++++++++++++++++++++++ demos/generichashstream.c | 60 ------------------------------------ 3 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 demos/generichash_stream.c delete mode 100644 demos/generichashstream.c diff --git a/demos/generichash.c b/demos/generichash.c index 1e4d15fc..ea2df02f 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -53,7 +53,7 @@ void generichash(void) { unsigned char key[crypto_generichash_KEYBYTES_MAX]; - unsigned char hash[crypto_generichash_BYTES_MIN]; + unsigned char hash[crypto_generichash_BYTES]; unsigned char message[MAX_INPUT_SIZE]; size_t message_len; size_t key_len; diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c new file mode 100644 index 00000000..5e666f77 --- /dev/null +++ b/demos/generichash_stream.c @@ -0,0 +1,63 @@ +/* + * GraxRabble + * Demo programs for libsodium. + */ +#include +#include +#include + +#include /* library header */ + +#include "utils.h" /* utility functions shared by demos */ + +/* + * Streaming variant of generic hash. This has the ability to hash + * data in chunks at a time and compute the same result as hashing + * all of the data at once. + */ +void +generichash_stream(void) +{ + unsigned char key[crypto_generichash_KEYBYTES_MAX]; + unsigned char hash[crypto_generichash_BYTES]; + unsigned char message_part[MAX_INPUT_SIZE]; + crypto_generichash_state state; + size_t message_part_len; + + puts("Example: crypto_generichashstream\n"); + + prompt_input("a key", (char*)key, sizeof key, 1); + putchar('\n'); + + printf("Hashing message with %s\n", crypto_generichash_primitive()); + + /* initialize the stream */ + if (crypto_generichash_init(&state, key, sizeof key, sizeof hash) != 0) { + puts("Couldn't hash the message, probably due to the key length"); + exit(EXIT_FAILURE); + } + + for(;;) { + message_part_len = prompt_input("the next part of the message", + (char*)message_part, sizeof message_part, 1); + if (message_part_len == 0) + break; + + /* keep appending data */ + crypto_generichash_update(&state, message_part, message_part_len); + } + crypto_generichash_final(&state, hash, sizeof hash); + + fputs("Hash: ", stdout); + print_hex(hash, sizeof hash); + putchar('\n'); +} + +int +main(void) +{ + init(); + generichash_stream(); + + return 0; +} diff --git a/demos/generichashstream.c b/demos/generichashstream.c deleted file mode 100644 index 1e3b84c8..00000000 --- a/demos/generichashstream.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * GraxRabble - * Demo programs for libsodium. - */ -#include -#include -#include - -#include /* library header */ - -#include "utils.h" /* utility functions shared by demos */ - -/* - * Streaming variant of generic hash. This has the ability to hash - * data in chunks at a time and compute the same result as hashing - * all of the data at once. - */ -void -generichashstream(void) -{ - unsigned char k[crypto_generichash_KEYBYTES_MAX]; /* key */ - unsigned char h[crypto_generichash_BYTES_MIN]; /* hash output */ - crypto_generichash_state state; /* hash stream */ - unsigned char m[MAX_INPUT_SIZE]; /* input buffer */ - size_t mlen; /* input length */ - - puts("Example: crypto_generichashstream\n"); - - prompt_input("a key", (char*)k, sizeof k, 0); - putchar('\n'); - - printf("Hashing message with %s\n", crypto_generichash_primitive()); - - /* initialize the stream */ - crypto_generichash_init(&state, k, sizeof k, sizeof h); - - for(;;) { - mlen = prompt_input("the next part of the message", (char*)m, sizeof m); - if (mlen == 0) - break; - - /* keep appending data */ - crypto_generichash_update(&state, m, mlen); - } - crypto_generichash_final(&state, h, sizeof h); - putchar('\n'); - - fputs("Hash: ", stdout); - print_hex(h, sizeof h); - putchar('\n'); -} - -int -main(void) -{ - init(); - generichashstream(); - - return 0; -} From a313e0be660dadddf09a002a04f35202a529c6a5 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:53:54 +0200 Subject: [PATCH 36/69] Demos: let prompt_input() add extra \n itself --- demos/auth.c | 2 -- demos/generichash.c | 2 -- demos/generichash_stream.c | 1 - demos/shorthash.c | 2 -- demos/sign.c | 57 +++++++++++++++++++------------------- demos/utils.c | 10 +++---- 6 files changed, 34 insertions(+), 40 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index f7179e1d..6b1dd438 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -45,9 +45,7 @@ auth(void) puts("Example: crypto_auth\n"); prompt_input("a key", (char*)key, sizeof key, 0); - message_len = prompt_input("a message", (char*)message, sizeof message, 1); - putchar('\n'); printf("Generating %s authentication...\n", crypto_auth_primitive()); crypto_auth(mac, message, message_len, key); diff --git a/demos/generichash.c b/demos/generichash.c index ea2df02f..d994e5bb 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -61,9 +61,7 @@ generichash(void) puts("Example: crypto_generichash\n"); key_len = prompt_input("a key", (char*)key, sizeof key, 1); - message_len = prompt_input("a message", (char*)message, sizeof message, 1); - putchar('\n'); printf("Hashing message with %s\n", crypto_generichash_primitive()); if (crypto_generichash(hash, sizeof hash, message, message_len, diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c index 5e666f77..5b1a3635 100644 --- a/demos/generichash_stream.c +++ b/demos/generichash_stream.c @@ -27,7 +27,6 @@ generichash_stream(void) puts("Example: crypto_generichashstream\n"); prompt_input("a key", (char*)key, sizeof key, 1); - putchar('\n'); printf("Hashing message with %s\n", crypto_generichash_primitive()); diff --git a/demos/shorthash.c b/demos/shorthash.c index 14272192..44a9f05c 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -43,9 +43,7 @@ shorthash(void) puts("Example: crypto_shorthash\n"); prompt_input("a key", (char*)key, sizeof key, 0); - message_len = prompt_input("a message", (char*)message, sizeof message, 1); - putchar('\n'); printf("Hashing the message with %s\n", crypto_shorthash_primitive()); crypto_shorthash(hash, message, message_len, key); diff --git a/demos/sign.c b/demos/sign.c index 8bced0f7..f0aa8996 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -22,59 +22,60 @@ static int sign(void) { - unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */ - unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */ - unsigned char m[MAX_INPUT_SIZE]; /* message */ - unsigned char sm[MAX_INPUT_SIZE + crypto_sign_BYTES]; /* signed message */ - unsigned long long int mlen; /* message length */ - unsigned long long int smlen; /* signed length */ - int r; + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob's public key */ + unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob's secret key */ + unsigned char message[MAX_INPUT_SIZE]; + unsigned char message_signed[crypto_sign_BYTES + MAX_INPUT_SIZE]; + unsigned long long message_len; + unsigned long long message_signed_len; + int ret; puts("Example: crypto_sign\n"); puts("Generating keypair..."); crypto_sign_keypair(pk, sk); /* generate Bob's keys */ - fputs("Public: ", stdout); + fputs("Public key: ", stdout); print_hex(pk, sizeof pk); putchar('\n'); - fputs("Secret: ", stdout); + fputs("Secret key: ", stdout); print_hex(sk, sizeof sk); putchar('\n'); + puts("The secret key, as returned by crypto_sign_keypair(), actually includes " + "a copy of the public key, in order to avoid a scalar multiplication " + "when signing messages."); - mlen = prompt_input("a message", (char*)m, sizeof m, 1); - putchar('\n'); - - puts("Notice the message has no prepended padding"); - print_hex(m, mlen); - putchar('\n'); + message_len = prompt_input("a message", (char*)message, sizeof message, 1); printf("Signing message with %s...\n", crypto_sign_primitive()); - crypto_sign(sm, &smlen, m, mlen, sk); + crypto_sign(message_signed, &message_signed_len, message, message_len, sk); - puts("Notice the signed message has prepended signature"); - print_hex(sm, smlen); + printf("Signed message:"); + print_hex(message_signed, message_signed_len); putchar('\n'); + printf("A %u bytes signature was prepended to the message\n", + crypto_sign_BYTES); fputs("Signature: ", stdout); - print_hex(sm, crypto_sign_BYTES); + print_hex(message_signed, crypto_sign_BYTES); putchar('\n'); fputs("Message: ", stdout); - fwrite(sm + crypto_sign_BYTES, 1U, smlen - crypto_sign_BYTES, stdout); + fwrite(message_signed + crypto_sign_BYTES, 1U, + message_signed_len - crypto_sign_BYTES, stdout); putchar('\n'); puts("Validating message..."); - r = crypto_sign_open(m, &mlen, sm, smlen, pk); - - print_verification(r); - if (r == 0) - printf("Message: %s\n", m); + ret = crypto_sign_open(message, &message_len, message_signed, + message_signed_len, pk); + print_verification(ret); + if (ret == 0) + printf("Message: %s\n", message); sodium_memzero(sk, sizeof sk); /* wipe sensitive data */ - sodium_memzero(m, sizeof m); - sodium_memzero(sm, sizeof sm); + sodium_memzero(message, sizeof message); + sodium_memzero(message_signed, sizeof message_signed); - return r; + return ret; } int diff --git a/demos/utils.c b/demos/utils.c index 428955f0..dc3adc1b 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -52,9 +52,9 @@ prompt_input(const char *prompt, char *input, const size_t max_input_len, size_t actual_input_len; if (variable_length != 0) { - printf("Enter %s (%zu bytes max) > ", prompt, max_input_len); + printf("\nEnter %s (%zu bytes max) > ", prompt, max_input_len); } else { - printf("Enter %s (%zu bytes) > ", prompt, max_input_len); + printf("\nEnter %s (%zu bytes) > ", prompt, max_input_len); } fflush(stdout); fgets(input_tmp, sizeof input_tmp, stdin); @@ -67,14 +67,14 @@ prompt_input(const char *prompt, char *input, const size_t max_input_len, } if (actual_input_len > max_input_len) { - printf("Warning: truncating input to %zu bytes\n", max_input_len); + printf("Warning: truncating input to %zu bytes\n\n", max_input_len); actual_input_len = max_input_len; } else if (actual_input_len < max_input_len && variable_length == 0) { - printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n", + printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n\n", max_input_len, actual_input_len); memset(input, 0, max_input_len); } else { - printf("Length: %zu bytes\n", actual_input_len); + printf("Length: %zu bytes\n\n", actual_input_len); } memcpy(input, input_tmp, actual_input_len); From 596c65c745900be00fbda8149e4ec70a915f19a6 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:01:49 +0200 Subject: [PATCH 37/69] r -> ret --- demos/utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/utils.c b/demos/utils.c index dc3adc1b..3db69ebe 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -89,9 +89,9 @@ prompt_input(const char *prompt, char *input, const size_t max_input_len, * Display whether the function was sucessful or failed. */ void -print_verification(int r) +print_verification(int ret) { - if (r == 0) + if (ret == 0) puts("Success!\n"); else puts("Failure.\n"); From 944857bbf59f239aacf4ed2c75134c26be432565 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:01:57 +0200 Subject: [PATCH 38/69] The first line already said these were utility functions, just like the file name --- demos/utils.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/demos/utils.c b/demos/utils.c index 3db69ebe..413a74f6 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -10,10 +10,6 @@ #include "utils.h" -/* ================================================================== * - * utility functions * - * ================================================================== */ - /* * print_hex() is a wrapper around sodium_bin2hex() which allocates * temporary memory then immediately prints the result. From 5a57615f442a8d013d07e9420ad98b278340f914 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:04:32 +0200 Subject: [PATCH 39/69] print_hex() is always followed by putc('\n'), so let it do it directly --- demos/auth.c | 1 - demos/box.c | 9 --------- demos/box_detached.c | 10 ---------- demos/generichash.c | 1 - demos/generichash_stream.c | 1 - demos/shorthash.c | 1 - demos/sign.c | 4 ---- demos/utils.c | 4 ++-- 8 files changed, 2 insertions(+), 29 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index 6b1dd438..6accf7d6 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -52,7 +52,6 @@ auth(void) fputs("Authentication tag: ", stdout); print_hex(mac, sizeof mac); - putchar('\n'); puts("Verifying authentication tag..."); ret = crypto_auth_verify(mac, message, message_len, key); diff --git a/demos/box.c b/demos/box.c index 7f44deeb..79dc3144 100644 --- a/demos/box.c +++ b/demos/box.c @@ -72,31 +72,25 @@ box(void) puts("Bob"); fputs("Public key: ", stdout); print_hex(bob_pk, sizeof bob_pk); - putchar('\n'); fputs("Secret key: ", stdout); print_hex(bob_sk, sizeof bob_sk); - putchar('\n'); puts("Alice"); fputs("Public key: ", stdout); print_hex(alice_pk, sizeof alice_pk); - putchar('\n'); fputs("Secret key: ", stdout); print_hex(alice_sk, sizeof alice_sk); - putchar('\n'); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); randombytes_buf(nonce, sizeof nonce); fputs("Nonce: ", stdout); print_hex(nonce, sizeof nonce); - putchar('\n'); /* read input */ message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); - putchar('\n'); /* encrypt and authenticate the message */ printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive()); @@ -110,17 +104,14 @@ box(void) puts("Notice the prepended 16 byte authentication token\n"); fputs("Nonce: ", stdout); print_hex(nonce, nonce_len); - putchar('\n'); fputs("Ciphertext: ", stdout); print_hex(ciphertext, ciphertext_len); - putchar('\n'); /* decrypt the message */ 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); - putchar('\n'); print_verification(ret); if (ret == 0) { diff --git a/demos/box_detached.c b/demos/box_detached.c index 714ce1e8..80478b4a 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -72,31 +72,25 @@ box_detached(void) puts("Bob"); fputs("Public key: ", stdout); print_hex(bob_pk, sizeof bob_pk); - putchar('\n'); fputs("Secret key: ", stdout); print_hex(bob_sk, sizeof bob_sk); - putchar('\n'); puts("Alice"); fputs("Public key: ", stdout); print_hex(alice_pk, sizeof alice_pk); - putchar('\n'); fputs("Secret key: ", stdout); print_hex(alice_sk, sizeof alice_sk); - putchar('\n'); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); randombytes_buf(nonce, sizeof nonce); fputs("Nonce: ", stdout); print_hex(nonce, sizeof nonce); - putchar('\n'); /* read input */ message_len = prompt_input("a message", (char*)message, sizeof message, 1); print_hex(message, message_len); - putchar('\n'); /* encrypt and authenticate the message */ printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive()); @@ -107,20 +101,16 @@ box_detached(void) puts("Bob sends the nonce, the MAC and the ciphertext...\n"); fputs("Nonce: ", stdout); print_hex(nonce, sizeof nonce); - putchar('\n'); fputs("MAC: ", stdout); print_hex(mac, sizeof mac); - putchar('\n'); fputs("Ciphertext: ", stdout); print_hex(ciphertext, message_len); - putchar('\n'); /* decrypt the message */ 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); - putchar('\n'); print_verification(ret); if (ret == 0) { diff --git a/demos/generichash.c b/demos/generichash.c index d994e5bb..064a3c53 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -71,7 +71,6 @@ generichash(void) fputs("Hash: ", stdout); print_hex(hash, sizeof hash); } - putchar('\n'); } int diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c index 5b1a3635..67d46df3 100644 --- a/demos/generichash_stream.c +++ b/demos/generichash_stream.c @@ -49,7 +49,6 @@ generichash_stream(void) fputs("Hash: ", stdout); print_hex(hash, sizeof hash); - putchar('\n'); } int diff --git a/demos/shorthash.c b/demos/shorthash.c index 44a9f05c..6aac468c 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -49,7 +49,6 @@ shorthash(void) crypto_shorthash(hash, message, message_len, key); fputs("Hash: ", stdout); print_hex(hash, sizeof hash); - putchar('\n'); } int diff --git a/demos/sign.c b/demos/sign.c index f0aa8996..2cbb97f5 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -37,10 +37,8 @@ sign(void) fputs("Public key: ", stdout); print_hex(pk, sizeof pk); - putchar('\n'); fputs("Secret key: ", stdout); print_hex(sk, sizeof sk); - putchar('\n'); puts("The secret key, as returned by crypto_sign_keypair(), actually includes " "a copy of the public key, in order to avoid a scalar multiplication " "when signing messages."); @@ -52,13 +50,11 @@ sign(void) printf("Signed message:"); print_hex(message_signed, message_signed_len); - putchar('\n'); printf("A %u bytes signature was prepended to the message\n", crypto_sign_BYTES); fputs("Signature: ", stdout); print_hex(message_signed, crypto_sign_BYTES); - putchar('\n'); fputs("Message: ", stdout); fwrite(message_signed + crypto_sign_BYTES, 1U, message_signed_len - crypto_sign_BYTES, stdout); diff --git a/demos/utils.c b/demos/utils.c index 413a74f6..0542aef8 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -12,7 +12,7 @@ /* * print_hex() is a wrapper around sodium_bin2hex() which allocates - * temporary memory then immediately prints the result. + * temporary memory then immediately prints the result followed by \n */ void print_hex(const void *bin, const size_t bin_len) @@ -31,7 +31,7 @@ print_hex(const void *bin, const size_t bin_len) if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) { abort(); } - fputs(hex, stdout); + puts(hex); free(hex); } From 66819cf0e0b90ed6586ffdf2e50a3838f294132f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:05:51 +0200 Subject: [PATCH 40/69] The message and signature aren't secret data --- demos/sign.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/demos/sign.c b/demos/sign.c index 2cbb97f5..778b33e8 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -68,8 +68,6 @@ sign(void) printf("Message: %s\n", message); sodium_memzero(sk, sizeof sk); /* wipe sensitive data */ - sodium_memzero(message, sizeof message); - sodium_memzero(message_signed, sizeof message_signed); return ret; } From 8b8f89d4370dcca6487e4ec648c12d3417003c42 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:09:32 +0200 Subject: [PATCH 41/69] fputs(..., stdout) -> printf() This reduces the number of functions we use to print something from 5 to 4 (!). --- demos/auth.c | 2 +- demos/box.c | 14 +++++++------- demos/box_detached.c | 16 ++++++++-------- demos/generichash.c | 2 +- demos/generichash_stream.c | 2 +- demos/shorthash.c | 2 +- demos/sign.c | 8 ++++---- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index 6accf7d6..905b8287 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -50,7 +50,7 @@ auth(void) printf("Generating %s authentication...\n", crypto_auth_primitive()); crypto_auth(mac, message, message_len, key); - fputs("Authentication tag: ", stdout); + printf("Authentication tag: "); print_hex(mac, sizeof mac); puts("Verifying authentication tag..."); diff --git a/demos/box.c b/demos/box.c index 79dc3144..066ac4e4 100644 --- a/demos/box.c +++ b/demos/box.c @@ -70,21 +70,21 @@ box(void) crypto_box_keypair(alice_pk, alice_sk); /* generate Alice's keys */ puts("Bob"); - fputs("Public key: ", stdout); + printf("Public key: "); print_hex(bob_pk, sizeof bob_pk); - fputs("Secret key: ", stdout); + printf("Secret key: "); print_hex(bob_sk, sizeof bob_sk); puts("Alice"); - fputs("Public key: ", stdout); + printf("Public key: "); print_hex(alice_pk, sizeof alice_pk); - fputs("Secret key: ", stdout); + printf("Secret key: "); print_hex(alice_sk, sizeof alice_sk); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); randombytes_buf(nonce, sizeof nonce); - fputs("Nonce: ", stdout); + printf("Nonce: "); print_hex(nonce, sizeof nonce); /* read input */ @@ -102,9 +102,9 @@ box(void) printf("Ciphertext len: %zu bytes - Original message length: %zu bytes\n", ciphertext_len, message_len); puts("Notice the prepended 16 byte authentication token\n"); - fputs("Nonce: ", stdout); + printf("Nonce: "); print_hex(nonce, nonce_len); - fputs("Ciphertext: ", stdout); + printf("Ciphertext: "); print_hex(ciphertext, ciphertext_len); /* decrypt the message */ diff --git a/demos/box_detached.c b/demos/box_detached.c index 80478b4a..2d8cd3ef 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -70,21 +70,21 @@ box_detached(void) crypto_box_keypair(alice_pk, alice_sk); /* generate Alice's keys */ puts("Bob"); - fputs("Public key: ", stdout); + printf("Public key: "); print_hex(bob_pk, sizeof bob_pk); - fputs("Secret key: ", stdout); + printf("Secret key: "); print_hex(bob_sk, sizeof bob_sk); puts("Alice"); - fputs("Public key: ", stdout); + printf("Public key: "); print_hex(alice_pk, sizeof alice_pk); - fputs("Secret key: ", stdout); + printf("Secret key: "); print_hex(alice_sk, sizeof alice_sk); /* nonce must be unique per (key, message) - it can be public and deterministic */ puts("Generating nonce..."); randombytes_buf(nonce, sizeof nonce); - fputs("Nonce: ", stdout); + printf("Nonce: "); print_hex(nonce, sizeof nonce); /* read input */ @@ -99,11 +99,11 @@ box_detached(void) /* send the nonce, the MAC and the ciphertext */ puts("Bob sends the nonce, the MAC and the ciphertext...\n"); - fputs("Nonce: ", stdout); + printf("Nonce: "); print_hex(nonce, sizeof nonce); - fputs("MAC: ", stdout); + printf("MAC: "); print_hex(mac, sizeof mac); - fputs("Ciphertext: ", stdout); + printf("Ciphertext: "); print_hex(ciphertext, message_len); /* decrypt the message */ diff --git a/demos/generichash.c b/demos/generichash.c index 064a3c53..51f5978f 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -68,7 +68,7 @@ generichash(void) key, key_len) != 0) { puts("Couldn't hash the message, probably due to the key length"); } else { - fputs("Hash: ", stdout); + printf("Hash: "); print_hex(hash, sizeof hash); } } diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c index 67d46df3..1db3fc39 100644 --- a/demos/generichash_stream.c +++ b/demos/generichash_stream.c @@ -47,7 +47,7 @@ generichash_stream(void) } crypto_generichash_final(&state, hash, sizeof hash); - fputs("Hash: ", stdout); + printf("Hash: "); print_hex(hash, sizeof hash); } diff --git a/demos/shorthash.c b/demos/shorthash.c index 6aac468c..cc31d711 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -47,7 +47,7 @@ shorthash(void) printf("Hashing the message with %s\n", crypto_shorthash_primitive()); crypto_shorthash(hash, message, message_len, key); - fputs("Hash: ", stdout); + printf("Hash: "); print_hex(hash, sizeof hash); } diff --git a/demos/sign.c b/demos/sign.c index 778b33e8..e9c8f007 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -35,9 +35,9 @@ sign(void) puts("Generating keypair..."); crypto_sign_keypair(pk, sk); /* generate Bob's keys */ - fputs("Public key: ", stdout); + printf("Public key: "); print_hex(pk, sizeof pk); - fputs("Secret key: ", stdout); + printf("Secret key: "); print_hex(sk, sizeof sk); puts("The secret key, as returned by crypto_sign_keypair(), actually includes " "a copy of the public key, in order to avoid a scalar multiplication " @@ -53,9 +53,9 @@ sign(void) printf("A %u bytes signature was prepended to the message\n", crypto_sign_BYTES); - fputs("Signature: ", stdout); + printf("Signature: "); print_hex(message_signed, crypto_sign_BYTES); - fputs("Message: ", stdout); + printf("Message: "); fwrite(message_signed + crypto_sign_BYTES, 1U, message_signed_len - crypto_sign_BYTES, stdout); putchar('\n'); From a53c566375f5184ec4f20935be5210d8c48e9d1b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:19:17 +0200 Subject: [PATCH 42/69] SIZE -> LEN, for consistency --- demos/auth.c | 2 +- demos/box.c | 4 ++-- demos/box_detached.c | 4 ++-- demos/generichash.c | 2 +- demos/generichash_stream.c | 2 +- demos/shorthash.c | 2 +- demos/sign.c | 4 ++-- demos/utils.c | 2 +- demos/utils.h | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/demos/auth.c b/demos/auth.c index 905b8287..d11ffde7 100644 --- a/demos/auth.c +++ b/demos/auth.c @@ -38,7 +38,7 @@ auth(void) { unsigned char key[crypto_auth_KEYBYTES]; unsigned char mac[crypto_auth_BYTES]; - unsigned char message[MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; size_t message_len; int ret; diff --git a/demos/box.c b/demos/box.c index 066ac4e4..421d4a31 100644 --- a/demos/box.c +++ b/demos/box.c @@ -57,8 +57,8 @@ box(void) unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice's secret key */ unsigned char nonce[crypto_box_NONCEBYTES]; - unsigned char message[MAX_INPUT_SIZE]; - unsigned char ciphertext[crypto_box_MACBYTES + MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; + unsigned char ciphertext[crypto_box_MACBYTES + MAX_INPUT_LEN]; size_t message_len; size_t ciphertext_len; int ret; diff --git a/demos/box_detached.c b/demos/box_detached.c index 2d8cd3ef..f93b7af7 100644 --- a/demos/box_detached.c +++ b/demos/box_detached.c @@ -57,9 +57,9 @@ box_detached(void) unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; /* Alice's secret key */ unsigned char nonce[crypto_box_NONCEBYTES]; - unsigned char message[MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; unsigned char mac[crypto_box_MACBYTES]; - unsigned char ciphertext[MAX_INPUT_SIZE]; + unsigned char ciphertext[MAX_INPUT_LEN]; size_t message_len; int ret; diff --git a/demos/generichash.c b/demos/generichash.c index 51f5978f..2167514b 100644 --- a/demos/generichash.c +++ b/demos/generichash.c @@ -54,7 +54,7 @@ generichash(void) { unsigned char key[crypto_generichash_KEYBYTES_MAX]; unsigned char hash[crypto_generichash_BYTES]; - unsigned char message[MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; size_t message_len; size_t key_len; diff --git a/demos/generichash_stream.c b/demos/generichash_stream.c index 1db3fc39..4550ac10 100644 --- a/demos/generichash_stream.c +++ b/demos/generichash_stream.c @@ -20,7 +20,7 @@ generichash_stream(void) { unsigned char key[crypto_generichash_KEYBYTES_MAX]; unsigned char hash[crypto_generichash_BYTES]; - unsigned char message_part[MAX_INPUT_SIZE]; + unsigned char message_part[MAX_INPUT_LEN]; crypto_generichash_state state; size_t message_part_len; diff --git a/demos/shorthash.c b/demos/shorthash.c index cc31d711..0cd6cdd0 100644 --- a/demos/shorthash.c +++ b/demos/shorthash.c @@ -37,7 +37,7 @@ shorthash(void) { unsigned char key[crypto_shorthash_KEYBYTES]; unsigned char hash[crypto_shorthash_BYTES]; - unsigned char message[MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; size_t message_len; puts("Example: crypto_shorthash\n"); diff --git a/demos/sign.c b/demos/sign.c index e9c8f007..78a9279f 100644 --- a/demos/sign.c +++ b/demos/sign.c @@ -24,8 +24,8 @@ sign(void) { unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob's public key */ unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob's secret key */ - unsigned char message[MAX_INPUT_SIZE]; - unsigned char message_signed[crypto_sign_BYTES + MAX_INPUT_SIZE]; + unsigned char message[MAX_INPUT_LEN]; + unsigned char message_signed[crypto_sign_BYTES + MAX_INPUT_LEN]; unsigned long long message_len; unsigned long long message_signed_len; int ret; diff --git a/demos/utils.c b/demos/utils.c index 0542aef8..f3d8bb90 100644 --- a/demos/utils.c +++ b/demos/utils.c @@ -44,7 +44,7 @@ size_t prompt_input(const char *prompt, char *input, const size_t max_input_len, int variable_length) { - char input_tmp[MAX_INPUT_SIZE + 1U]; + char input_tmp[MAX_INPUT_LEN + 1U]; size_t actual_input_len; if (variable_length != 0) { diff --git a/demos/utils.h b/demos/utils.h index b88347fd..4e6d1b79 100644 --- a/demos/utils.h +++ b/demos/utils.h @@ -6,7 +6,7 @@ #include -#define MAX_INPUT_SIZE 4096 /* max size of all input buffers in the demo */ +#define MAX_INPUT_LEN 4096 /* max size of all input buffers in the demo */ void print_hex(const void *bin, const size_t bin_len); From 0417759d9c807b44fc2af74b42f4d339946db60b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:35:10 +0200 Subject: [PATCH 43/69] Make utils.c a header --- demos/box.c | 2 +- demos/utils.c | 101 -------------------------------------------------- demos/utils.h | 100 +++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 109 deletions(-) delete mode 100644 demos/utils.c diff --git a/demos/box.c b/demos/box.c index 421d4a31..918a4f90 100644 --- a/demos/box.c +++ b/demos/box.c @@ -103,7 +103,7 @@ box(void) ciphertext_len, message_len); puts("Notice the prepended 16 byte authentication token\n"); printf("Nonce: "); - print_hex(nonce, nonce_len); + print_hex(nonce, sizeof nonce); printf("Ciphertext: "); print_hex(ciphertext, ciphertext_len); diff --git a/demos/utils.c b/demos/utils.c deleted file mode 100644 index f3d8bb90..00000000 --- a/demos/utils.c +++ /dev/null @@ -1,101 +0,0 @@ -/* - * These are the utility functions shared by all demo programs. - */ -#include -#include -#include -#include - -#include - -#include "utils.h" - -/* - * print_hex() is a wrapper around sodium_bin2hex() which allocates - * temporary memory then immediately prints the result followed by \n - */ -void -print_hex(const void *bin, const size_t bin_len) -{ - char *hex; - size_t hex_size; - - if (bin_len >= SIZE_MAX / 2) { - abort(); - } - hex_size = bin_len * 2 + 1; - if ((hex = malloc(hex_size)) == NULL) { - abort(); - } - /* the library supplies a few utility functions like the one below */ - if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) { - abort(); - } - puts(hex); - free(hex); -} - -/* - * Display a prompt for input by user. It will save the input into a buffer - * of a specific size with room for the null terminator while removing - * trailing newline characters. - */ -size_t -prompt_input(const char *prompt, char *input, const size_t max_input_len, - int variable_length) -{ - char input_tmp[MAX_INPUT_LEN + 1U]; - size_t actual_input_len; - - if (variable_length != 0) { - printf("\nEnter %s (%zu bytes max) > ", prompt, max_input_len); - } else { - printf("\nEnter %s (%zu bytes) > ", prompt, max_input_len); - } - fflush(stdout); - fgets(input_tmp, sizeof input_tmp, stdin); - actual_input_len = strlen(input_tmp); - - /* trim \n */ - if (actual_input_len > 0 && input_tmp[actual_input_len - 1] == '\n') { - input_tmp[actual_input_len - 1] = '\0'; - --actual_input_len; - } - - if (actual_input_len > max_input_len) { - printf("Warning: truncating input to %zu bytes\n\n", max_input_len); - actual_input_len = max_input_len; - } else if (actual_input_len < max_input_len && variable_length == 0) { - printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n\n", - max_input_len, actual_input_len); - memset(input, 0, max_input_len); - } else { - printf("Length: %zu bytes\n\n", actual_input_len); - } - - memcpy(input, input_tmp, actual_input_len); - if (variable_length == 0) { - return max_input_len; - } else { - return actual_input_len; - } -} - -/* - * Display whether the function was sucessful or failed. - */ -void -print_verification(int ret) -{ - if (ret == 0) - puts("Success!\n"); - else - puts("Failure.\n"); -} - -void -init(void) -{ - sodium_init(); - printf("Using libsodium %s\n", sodium_version_string()); -} diff --git a/demos/utils.h b/demos/utils.h index 4e6d1b79..c9460474 100644 --- a/demos/utils.h +++ b/demos/utils.h @@ -4,17 +4,103 @@ #ifndef UTILS_H #define UTILS_H -#include +#include +#include +#include +#include -#define MAX_INPUT_LEN 4096 /* max size of all input buffers in the demo */ +#include -void print_hex(const void *bin, const size_t bin_len); +#define MAX_INPUT_LEN 4096 -size_t prompt_input(const char *prompt, char *input, const size_t max_input_len, - int variable_length); +/* + * print_hex() is a wrapper around sodium_bin2hex() which allocates + * temporary memory then immediately prints the result followed by \n + */ +static void +print_hex(const void *bin, const size_t bin_len) +{ + char *hex; + size_t hex_size; -void print_verification(int r); + if (bin_len >= SIZE_MAX / 2) { + abort(); + } + hex_size = bin_len * 2 + 1; + if ((hex = malloc(hex_size)) == NULL) { + abort(); + } + /* the library supplies a few utility functions like the one below */ + if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) { + abort(); + } + puts(hex); + free(hex); +} -void init(void); +/* + * Display a prompt for input by user. It will save the input into a buffer + * of a specific size with room for the null terminator while removing + * trailing newline characters. + */ +static size_t +prompt_input(const char *prompt, char *input, const size_t max_input_len, + int variable_length) +{ + char input_tmp[MAX_INPUT_LEN + 1U]; + size_t actual_input_len; + + if (variable_length != 0) { + printf("\nEnter %s (%zu bytes max) > ", prompt, max_input_len); + } else { + printf("\nEnter %s (%zu bytes) > ", prompt, max_input_len); + } + fflush(stdout); + fgets(input_tmp, sizeof input_tmp, stdin); + actual_input_len = strlen(input_tmp); + + /* trim \n */ + if (actual_input_len > 0 && input_tmp[actual_input_len - 1] == '\n') { + input_tmp[actual_input_len - 1] = '\0'; + --actual_input_len; + } + + if (actual_input_len > max_input_len) { + printf("Warning: truncating input to %zu bytes\n\n", max_input_len); + actual_input_len = max_input_len; + } else if (actual_input_len < max_input_len && variable_length == 0) { + printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n\n", + max_input_len, actual_input_len); + memset(input, 0, max_input_len); + } else { + printf("Length: %zu bytes\n\n", actual_input_len); + } + + memcpy(input, input_tmp, actual_input_len); + if (variable_length == 0) { + return max_input_len; + } else { + return actual_input_len; + } +} + +/* + * Display whether the function was sucessful or failed. + */ +static void +print_verification(int ret) +{ + if (ret == 0) + puts("Success!\n"); + else + puts("Failure.\n"); +} + +static void +init(void) +{ + sodium_init(); + printf("Using libsodium %s\n", sodium_version_string()); +} #endif /* UTILS_H */ From e3d71367fcc9e53edcdb7da3b3972a5b0c3efdf1 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:35:37 +0200 Subject: [PATCH 44/69] Add a basic Makefile for the demos --- demos/Makefile | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 demos/Makefile diff --git a/demos/Makefile b/demos/Makefile new file mode 100644 index 00000000..5519a7c6 --- /dev/null +++ b/demos/Makefile @@ -0,0 +1,21 @@ + +TARGETS = \ + auth \ + box \ + box_detached \ + generichash \ + generichash_stream \ + shorthash \ + sign + +SODIUM_CFLAGS != pkg-config --cflags libsodium +SODIUM_LIBS != pkg-config --libs libsodium +CFLAGS += $(SODIUM_CFLAGS) +LDFLAGS += $(SODIUM_LIBS) + +all: $(TARGETS) + +clean: + rm -f $(TARGETS) + +distclean: clean From c1c28b0ee5a49e296402454c4953f4720ddcf420 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:36:54 +0200 Subject: [PATCH 45/69] demo -> example --- {demos => examples}/Makefile | 0 {demos => examples}/auth.c | 4 ++-- {demos => examples}/box.c | 4 ++-- {demos => examples}/box_detached.c | 4 ++-- {demos => examples}/generichash.c | 4 ++-- {demos => examples}/generichash_stream.c | 4 ++-- {demos => examples}/shorthash.c | 4 ++-- {demos => examples}/sign.c | 4 ++-- {demos => examples}/utils.h | 0 9 files changed, 14 insertions(+), 14 deletions(-) rename {demos => examples}/Makefile (100%) rename {demos => examples}/auth.c (95%) rename {demos => examples}/box.c (97%) rename {demos => examples}/box_detached.c (97%) rename {demos => examples}/generichash.c (96%) rename {demos => examples}/generichash_stream.c (94%) rename {demos => examples}/shorthash.c (94%) rename {demos => examples}/sign.c (96%) rename {demos => examples}/utils.h (100%) diff --git a/demos/Makefile b/examples/Makefile similarity index 100% rename from demos/Makefile rename to examples/Makefile diff --git a/demos/auth.c b/examples/auth.c similarity index 95% rename from demos/auth.c rename to examples/auth.c index d11ffde7..0edbd18c 100644 --- a/demos/auth.c +++ b/examples/auth.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * This operation computes an authentication tag for a message and a diff --git a/demos/box.c b/examples/box.c similarity index 97% rename from demos/box.c rename to examples/box.c index 918a4f90..d19dde86 100644 --- a/demos/box.c +++ b/examples/box.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * Using public-key authenticated encryption, Bob can encrypt a diff --git a/demos/box_detached.c b/examples/box_detached.c similarity index 97% rename from demos/box_detached.c rename to examples/box_detached.c index f93b7af7..e7f5de3f 100644 --- a/demos/box_detached.c +++ b/examples/box_detached.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * Using public-key authenticated encryption, Bob can encrypt a diff --git a/demos/generichash.c b/examples/generichash.c similarity index 96% rename from demos/generichash.c rename to examples/generichash.c index 2167514b..e4cd3700 100644 --- a/demos/generichash.c +++ b/examples/generichash.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * This function computes a fixed-length fingerprint for an arbitrary long message. diff --git a/demos/generichash_stream.c b/examples/generichash_stream.c similarity index 94% rename from demos/generichash_stream.c rename to examples/generichash_stream.c index 4550ac10..cb01f13a 100644 --- a/demos/generichash_stream.c +++ b/examples/generichash_stream.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * Streaming variant of generic hash. This has the ability to hash diff --git a/demos/shorthash.c b/examples/shorthash.c similarity index 94% rename from demos/shorthash.c rename to examples/shorthash.c index 0cd6cdd0..9e7f9e4d 100644 --- a/demos/shorthash.c +++ b/examples/shorthash.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * Many applications and programming language implementations were diff --git a/demos/sign.c b/examples/sign.c similarity index 96% rename from demos/sign.c rename to examples/sign.c index 78a9279f..b95e9d81 100644 --- a/demos/sign.c +++ b/examples/sign.c @@ -1,6 +1,6 @@ /* * GraxRabble - * Demo programs for libsodium. + * example programs for libsodium. */ #include #include @@ -8,7 +8,7 @@ #include /* library header */ -#include "utils.h" /* utility functions shared by demos */ +#include "utils.h" /* utility functions shared by examples */ /* * Signs a message with secret key which will authenticate a message. diff --git a/demos/utils.h b/examples/utils.h similarity index 100% rename from demos/utils.h rename to examples/utils.h From 5b851b45206ea0272b90711b46f205b449847338 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 21:38:16 +0200 Subject: [PATCH 46/69] Remove common system headers already included in utils.h --- examples/auth.c | 3 --- examples/box.c | 3 --- examples/box_detached.c | 3 --- examples/generichash.c | 3 --- examples/generichash_stream.c | 3 --- examples/shorthash.c | 3 --- examples/sign.c | 3 --- 7 files changed, 21 deletions(-) diff --git a/examples/auth.c b/examples/auth.c index 0edbd18c..25e3eacf 100644 --- a/examples/auth.c +++ b/examples/auth.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/box.c b/examples/box.c index d19dde86..0f4ec7f5 100644 --- a/examples/box.c +++ b/examples/box.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/box_detached.c b/examples/box_detached.c index e7f5de3f..3ea5a739 100644 --- a/examples/box_detached.c +++ b/examples/box_detached.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/generichash.c b/examples/generichash.c index e4cd3700..fbf00442 100644 --- a/examples/generichash.c +++ b/examples/generichash.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/generichash_stream.c b/examples/generichash_stream.c index cb01f13a..aab8b490 100644 --- a/examples/generichash_stream.c +++ b/examples/generichash_stream.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/shorthash.c b/examples/shorthash.c index 9e7f9e4d..390150f6 100644 --- a/examples/shorthash.c +++ b/examples/shorthash.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ diff --git a/examples/sign.c b/examples/sign.c index b95e9d81..d17d3545 100644 --- a/examples/sign.c +++ b/examples/sign.c @@ -2,9 +2,6 @@ * GraxRabble * example programs for libsodium. */ -#include -#include -#include #include /* library header */ From c7179cea2d55d072d5fcf0fd083e071e8a1279c6 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 28 May 2015 10:43:57 +0200 Subject: [PATCH 47/69] .gitignore --- examples/.gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 examples/.gitignore diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..9ffb5a89 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,7 @@ +auth +box +box_detached +generichash +generichash_stream +shorthash +sign From 7821009bff1c69f6dd20ade6801e61358a659a1a Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 29 May 2015 12:07:05 +0200 Subject: [PATCH 48/69] Do not assume that _MSC_VER being defined implied x86 or x64 --- .../scryptsalsa208sha256/crypto_scrypt-common.c | 6 ++++-- .../sse/pwhash_scryptsalsa208sha256_sse.c | 3 ++- src/libsodium/sodium/runtime.c | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c index 77be6ab9..8e9aceff 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c @@ -153,7 +153,8 @@ escrypt_r(escrypt_local_t * local, const uint8_t * passwd, size_t passwdlen, if (need > buflen || need < saltlen) { return NULL; } -#if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER) +#if defined(HAVE_EMMINTRIN_H) || \ + (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) escrypt_kdf = sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse; #else @@ -234,7 +235,8 @@ crypto_pwhash_scryptsalsa208sha256_ll(const uint8_t * passwd, size_t passwdlen, if (escrypt_init_local(&local)) { return -1; /* LCOV_EXCL_LINE */ } -#if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER) +#if defined(HAVE_EMMINTRIN_H) || \ + (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) escrypt_kdf = sodium_runtime_has_sse2() ? escrypt_kdf_sse : escrypt_kdf_nosse; #else diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c index a5202ed6..faba9f17 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c @@ -28,7 +28,8 @@ * online backup system. */ -#if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER) +#if defined(HAVE_EMMINTRIN_H) || \ + (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))) #if __GNUC__ # pragma GCC target("sse2") #endif diff --git a/src/libsodium/sodium/runtime.c b/src/libsodium/sodium/runtime.c index 3e424a01..93b07932 100644 --- a/src/libsodium/sodium/runtime.c +++ b/src/libsodium/sodium/runtime.c @@ -43,7 +43,8 @@ _sodium_runtime_arm_cpu_features(CPUFeatures * const cpu_features) static void _cpuid(unsigned int cpu_info[4U], const unsigned int cpu_info_type) { -#ifdef _MSC_VER +#if defined(_MSC_VER) && \ + (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86)) __cpuid((int *) cpu_info, cpu_info_type); #elif defined(HAVE_CPUID) cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0; From 305d5e02e2d29c602f1036df53f09c1e177bf3b1 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 29 May 2015 12:25:02 +0200 Subject: [PATCH 49/69] + ARM.props --- builds/msvc/properties/ARM.props | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 builds/msvc/properties/ARM.props diff --git a/builds/msvc/properties/ARM.props b/builds/msvc/properties/ARM.props new file mode 100644 index 00000000..164b9b1f --- /dev/null +++ b/builds/msvc/properties/ARM.props @@ -0,0 +1,20 @@ + + + + + <_PropertySheetDisplayName>ARM Settings + + + + + WIN32;_WIN32;%(PreprocessorDefinitions) + + + MachineARM + + + /MACHINE:ARM %(AdditionalOptions) + + + + From ada287ad5650248c4d4347f4bf44c56687a728da Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 29 May 2015 16:50:48 +0200 Subject: [PATCH 50/69] Disable guarded memory on WinRT --- src/libsodium/sodium/utils.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 1ba1d8e5..86c994b5 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -23,6 +23,15 @@ # include #endif +#ifdef _WIN32 +# define WINAPI_DESKTOP +# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) +# undef WINAPI_DESKTOP +# elif defined(WINAPI_FAMILY_ONE_PARTITION) && defined(WINAPI_FAMILY_DESKTOP) && !WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY_DESKTOP) +# undef WINAPI_DESKTOP +# endif +#endif + #define CANARY_SIZE 16U #define GARBAGE_VALUE 0xd0 @@ -32,13 +41,13 @@ #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) # define MAP_ANON MAP_ANONYMOUS #endif -#if defined(_WIN32) || (defined(MAP_ANON) && defined(HAVE_MMAP)) || defined(HAVE_POSIX_MEMALIGN) +#if defined(WINAPI_DESKTOP) || (defined(MAP_ANON) && defined(HAVE_MMAP)) || defined(HAVE_POSIX_MEMALIGN) # define HAVE_ALIGNED_MALLOC #endif #if defined(HAVE_MPROTECT) && !(defined(PROT_NONE) && defined(PROT_READ) && defined(PROT_WRITE)) # undef HAVE_MPROTECT #endif -#if defined(HAVE_ALIGNED_MALLOC) && (defined(_WIN32) || defined(HAVE_MPROTECT)) +#if defined(HAVE_ALIGNED_MALLOC) && (defined(WINAPI_DESKTOP) || defined(HAVE_MPROTECT)) # define HAVE_PAGE_PROTECTION #endif @@ -184,7 +193,7 @@ _sodium_alloc_init(void) if (page_size_ > 0L) { page_size = (size_t) page_size_; } -# elif defined(_WIN32) +# elif defined(WINAPI_DESKTOP) SYSTEM_INFO si; GetSystemInfo(&si); page_size = (size_t) si.dwPageSize; @@ -206,7 +215,7 @@ sodium_mlock(void * const addr, const size_t len) #endif #ifdef HAVE_MLOCK return mlock(addr, len); -#elif defined(_WIN32) +#elif defined(WINAPI_DESKTOP) return -(VirtualLock(addr, len) == 0); #else errno = ENOSYS; @@ -223,7 +232,7 @@ sodium_munlock(void * const addr, const size_t len) #endif #ifdef HAVE_MLOCK return munlock(addr, len); -#elif defined(_WIN32) +#elif defined(WINAPI_DESKTOP) return -(VirtualUnlock(addr, len) == 0); #else errno = ENOSYS; @@ -236,7 +245,7 @@ _mprotect_noaccess(void *ptr, size_t size) { #ifdef HAVE_MPROTECT return mprotect(ptr, size, PROT_NONE); -#elif defined(_WIN32) +#elif defined(WINAPI_DESKTOP) DWORD old; return -(VirtualProtect(ptr, size, PAGE_NOACCESS, &old) == 0); #else @@ -250,7 +259,7 @@ _mprotect_readonly(void *ptr, size_t size) { #ifdef HAVE_MPROTECT return mprotect(ptr, size, PROT_READ); -#elif defined(_WIN32) +#elif defined(WINAPI_DESKTOP) DWORD old; return -(VirtualProtect(ptr, size, PAGE_READONLY, &old) == 0); #else @@ -264,7 +273,7 @@ _mprotect_readwrite(void *ptr, size_t size) { #ifdef HAVE_MPROTECT return mprotect(ptr, size, PROT_READ | PROT_WRITE); -#elif defined(_WIN32) +#elif defined(WINAPI_DESKTOP) DWORD old; return -(VirtualProtect(ptr, size, PAGE_READWRITE, &old) == 0); #else @@ -308,7 +317,7 @@ _alloc_aligned(const size_t size) if (posix_memalign(&ptr, page_size, size) != 0) { ptr = NULL; /* LCOV_EXCL_LINE */ } /* LCOV_EXCL_LINE */ -# elif defined(_WIN32) +# elif defined(WINAPI_DESKTOP) ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); # else # error Bug @@ -323,7 +332,7 @@ _free_aligned(unsigned char * const ptr, const size_t size) (void) munmap(ptr, size); # elif defined(HAVE_POSIX_MEMALIGN) free(ptr); -# elif defined(_WIN32) +# elif defined(WINAPI_DESKTOP) VirtualFree(ptr, 0U, MEM_RELEASE); # else # error Bug From 3bd6b8d07451d166528cc03977202f822280b00d Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 29 May 2015 17:11:11 +0200 Subject: [PATCH 51/69] Further simplify WINAPI_DESKTOP --- src/libsodium/sodium/utils.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 86c994b5..ae0d2be8 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -23,13 +23,8 @@ # include #endif -#ifdef _WIN32 +#if defined(_WIN32) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) # define WINAPI_DESKTOP -# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) -# undef WINAPI_DESKTOP -# elif defined(WINAPI_FAMILY_ONE_PARTITION) && defined(WINAPI_FAMILY_DESKTOP) && !WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY_DESKTOP) -# undef WINAPI_DESKTOP -# endif #endif #define CANARY_SIZE 16U From e326ef90301cf570a5e6691c46d4a1d37adc1e07 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 29 May 2015 17:35:54 +0200 Subject: [PATCH 52/69] Do not use timeval. The structure is not defined on Windows RT. --- .../salsa20/randombytes_salsa20_random.c | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index 43e2d0f2..1ab34b70 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -65,27 +65,24 @@ static Salsa20Random stream = { static uint64_t sodium_hrtime(void) { - struct timeval tv; - uint64_t ts = (uint64_t) 0U; - int ret; + uint64_t ts; #ifdef _WIN32 - struct _timeb tb; - + { + struct _timeb tb; # pragma warning(push) # pragma warning(disable: 4996) - _ftime(&tb); + _ftime(&tb); # pragma warning(pop) - tv.tv_sec = (long) tb.time; - tv.tv_usec = ((int) tb.millitm) * 1000; - ret = 0; -#else - ret = gettimeofday(&tv, NULL); -#endif - assert(ret == 0); - if (ret == 0) { - ts = (uint64_t) tv.tv_sec * 1000000U + (uint64_t) tv.tv_usec; + ts = ((uint64_t) tb.time) * 1000000U + ((uint64_t) tb.millitm) * 1000U; } +#else + { + struct timeval tv; + assert(gettimeofday(&tv, NULL) == 0); + ts = ((uint64_t) tv.tv_sec) * 1000000U + (uint64_t) tv.tv_usec; + } +#endif return ts; } From 315029188e94b25a3e338e3b9380b34ef34fbe1c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 10:22:01 +0200 Subject: [PATCH 53/69] Suggest randombytes_stir() --- src/libsodium/include/sodium/randombytes_salsa20_random.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libsodium/include/sodium/randombytes_salsa20_random.h b/src/libsodium/include/sodium/randombytes_salsa20_random.h index 46d38c54..e6d291c5 100644 --- a/src/libsodium/include/sodium/randombytes_salsa20_random.h +++ b/src/libsodium/include/sodium/randombytes_salsa20_random.h @@ -4,8 +4,9 @@ /* * THREAD SAFETY: randombytes_salsa20_random*() functions are - * fork()-safe but not thread-safe. - * Always wrap them in a mutex if you need thread safety. + * not thread-safe. + * Always wrap them in a mutex if you need thread safety, + * and call randombytes_stir() after fork()ing. */ #include From df9209a23521f57c7613b0c85fdeaa39b53740e5 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 10:32:53 +0200 Subject: [PATCH 54/69] Document IETF-compatible ChaCha20 --- ChangeLog | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ChangeLog b/ChangeLog index 5ac0afe0..7d2ca89d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,13 @@ +* Version 1.0.4 + - ChaCha20 with an extended (96 bit) nonce and a 32-bit counter has +been implemented as crypto_stream_chacha20_ietf(), +crypto_stream_chacha20_ietf_xor() and crypto_stream_chacha20_ietf_xor_ic(). +An IETF-compatible version of ChaCha20Poly1305 is available as +crypto_aead_chacha20poly1305_ietf_npubbytes(), +crypto_aead_chacha20poly1305_ietf_encrypt() and +crypto_aead_chacha20poly1305_ietf_decrypt(). + * Version 1.0.3 - In addition to sodium_bin2hex(), sodium_hex2bin() is now a constant-time function. From b2f5f66c86b84ba6a9cc0074346d1e4ebdb58e8c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 10:43:39 +0200 Subject: [PATCH 55/69] Windows Store compat --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index 7d2ca89d..a37abd99 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ An IETF-compatible version of ChaCha20Poly1305 is available as crypto_aead_chacha20poly1305_ietf_npubbytes(), crypto_aead_chacha20poly1305_ietf_encrypt() and crypto_aead_chacha20poly1305_ietf_decrypt(). + - Sodium can now be used in Windows Store apps. * Version 1.0.3 - In addition to sodium_bin2hex(), sodium_hex2bin() is now a From 2efa85d283e6b6d2d58861f6224d3629b0006bf4 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 10:44:02 +0200 Subject: [PATCH 56/69] -dev --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a37abd99..502e3e1f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,5 @@ -* Version 1.0.4 +* Version 1.0.4 (not released yet) - ChaCha20 with an extended (96 bit) nonce and a 32-bit counter has been implemented as crypto_stream_chacha20_ietf(), crypto_stream_chacha20_ietf_xor() and crypto_stream_chacha20_ietf_xor_ic(). From 3c3635e53a87b77fbb10bd826c4e47f054cf9ac4 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 12:29:02 +0200 Subject: [PATCH 57/69] salsa20_random(): just abort(3) if the pid changes and _stir() wasn't called --- .../randombytes/salsa20/randombytes_salsa20_random.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index 1ab34b70..0c3cd1c9 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -271,6 +271,9 @@ randombytes_salsa20_random_stir(void) stream.key[i] ^= m0[i]; } sodium_memzero(m0, sizeof m0); +#ifndef _MSC_VER + stream.pid = getpid(); +#endif } static void @@ -281,11 +284,10 @@ randombytes_salsa20_random_stir_if_needed(void) randombytes_salsa20_random_stir(); } #else - const pid_t pid = getpid(); - - if (stream.initialized == 0 || stream.pid != pid) { - stream.pid = pid; + if (stream.initialized == 0) { randombytes_salsa20_random_stir(); + } else if (stream.pid != getpid()) { + abort(); } #endif } @@ -339,6 +341,7 @@ randombytes_salsa20_random_close(void) close(stream.random_data_source_fd) == 0) { stream.random_data_source_fd = -1; stream.initialized = 0; + stream.pid = (pid_t) 0; ret = 0; } # ifdef SYS_getrandom From 21fc07e6f40fce18249b33f18d8ca8634200fc32 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 1 Jun 2015 12:45:45 +0200 Subject: [PATCH 58/69] Leverage randombytes_salsa20_random_rekey() --- .../salsa20/randombytes_salsa20_random.c | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index 0c3cd1c9..377e4ba2 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -221,6 +221,17 @@ randombytes_salsa20_random_init(void) } #endif +static void +randombytes_salsa20_random_rekey(const unsigned char * const mix) +{ + unsigned char *key = stream.key; + size_t i; + + for (i = (size_t) 0U; i < sizeof stream.key; i++) { + key[i] ^= mix[i]; + } +} + void randombytes_salsa20_random_stir(void) { @@ -267,9 +278,7 @@ randombytes_salsa20_random_stir(void) COMPILER_ASSERT(sizeof stream.key == crypto_auth_hmacsha512256_BYTES); crypto_auth_hmacsha512256(stream.key, k0, sizeof_k0, s); COMPILER_ASSERT(sizeof stream.key <= sizeof m0); - for (i = (size_t) 0U; i < sizeof stream.key; i++) { - stream.key[i] ^= m0[i]; - } + randombytes_salsa20_random_rekey(m0); sodium_memzero(m0, sizeof m0); #ifndef _MSC_VER stream.pid = getpid(); @@ -292,17 +301,6 @@ randombytes_salsa20_random_stir_if_needed(void) #endif } -static void -randombytes_salsa20_random_rekey(const unsigned char * const mix) -{ - unsigned char *key = stream.key; - size_t i; - - for (i = (size_t) 0U; i < sizeof stream.key; i++) { - key[i] ^= mix[i]; - } -} - static uint32_t randombytes_salsa20_random_getword(void) { From ba1cd6a128591b93555c43a5b361d74c8b894c62 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 6 Jun 2015 12:32:23 +0200 Subject: [PATCH 59/69] SHA256: use uint64_t for the counter instead of two uint32_t --- .../crypto_hash/sha256/cp/hash_sha256.c | 52 +++++++++++-------- .../include/sodium/crypto_hash_sha256.h | 2 +- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c b/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c index 738794ac..c98f1bee 100644 --- a/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c +++ b/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c @@ -40,6 +40,7 @@ /* Avoid namespace collisions with BSD . */ #define be32dec _sha256_be32dec #define be32enc _sha256_be32enc +#define be64enc _sha256_be64enc static inline uint32_t be32dec(const void *pp) @@ -53,7 +54,7 @@ be32dec(const void *pp) static inline void be32enc(void *pp, uint32_t x) { - uint8_t * p = (uint8_t *)pp; + uint8_t *p = (uint8_t *)pp; p[3] = x & 0xff; p[2] = (x >> 8) & 0xff; @@ -61,6 +62,21 @@ be32enc(void *pp, uint32_t x) p[0] = (x >> 24) & 0xff; } +static inline void +be64enc(void * pp, uint64_t x) +{ + uint8_t * p = (uint8_t *)pp; + + p[7] = x & 0xff; + p[6] = (x >> 8) & 0xff; + p[5] = (x >> 16) & 0xff; + p[4] = (x >> 24) & 0xff; + p[3] = (x >> 32) & 0xff; + p[2] = (x >> 40) & 0xff; + p[1] = (x >> 48) & 0xff; + p[0] = (x >> 56) & 0xff; +} + static void be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len) { @@ -206,9 +222,9 @@ SHA256_Pad(crypto_hash_sha256_state *state) unsigned char len[8]; uint32_t r, plen; - be32enc_vect(len, state->count, 8); + be64enc(len, state->count); - r = (state->count[1] >> 3) & 0x3f; + r = (state->count >> 3) & 0x3f; plen = (r < 56) ? (56 - r) : (120 - r); crypto_hash_sha256_update(state, PAD, (unsigned long long) plen); @@ -218,16 +234,13 @@ SHA256_Pad(crypto_hash_sha256_state *state) int crypto_hash_sha256_init(crypto_hash_sha256_state *state) { - state->count[0] = state->count[1] = 0; + static const uint32_t sha256_initstate[8] = { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; - state->state[0] = 0x6A09E667; - state->state[1] = 0xBB67AE85; - state->state[2] = 0x3C6EF372; - state->state[3] = 0xA54FF53A; - state->state[4] = 0x510E527F; - state->state[5] = 0x9B05688C; - state->state[6] = 0x1F83D9AB; - state->state[7] = 0x5BE0CD19; + state->count = (uint64_t) 0U; + memcpy(state->state, sha256_initstate, sizeof sha256_initstate); return 0; } @@ -237,20 +250,13 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state, const unsigned char *in, unsigned long long inlen) { - uint32_t bitlen[2]; uint32_t r; - r = (state->count[1] >> 3) & 0x3f; - - bitlen[1] = ((uint32_t)inlen) << 3; - bitlen[0] = (uint32_t)(inlen >> 29); - - /* LCOV_EXCL_START */ - if ((state->count[1] += bitlen[1]) < bitlen[1]) { - state->count[0]++; + if (inlen <= 0U) { + return 0; } - /* LCOV_EXCL_STOP */ - state->count[0] += bitlen[0]; + r = (state->count >> 3) & 0x3f; + state->count += (uint64_t)(inlen) << 3; if (inlen < 64 - r) { memcpy(&state->buf[r], in, inlen); diff --git a/src/libsodium/include/sodium/crypto_hash_sha256.h b/src/libsodium/include/sodium/crypto_hash_sha256.h index 2bc33efe..1bb816f4 100644 --- a/src/libsodium/include/sodium/crypto_hash_sha256.h +++ b/src/libsodium/include/sodium/crypto_hash_sha256.h @@ -23,7 +23,7 @@ extern "C" { typedef struct crypto_hash_sha256_state { uint32_t state[8]; - uint32_t count[2]; + uint64_t count; unsigned char buf[64]; } crypto_hash_sha256_state; SODIUM_EXPORT From 86d92bc11deb43238d53f328af6ee3d0dc9ac565 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 6 Jun 2015 12:33:11 +0200 Subject: [PATCH 60/69] SHA512: just use memcpy() to initialize the state --- .../crypto_hash/sha512/cp/hash_sha512.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c b/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c index e85be74b..9eecff7c 100644 --- a/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c +++ b/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c @@ -244,16 +244,15 @@ SHA512_Pad(crypto_hash_sha512_state *state) int crypto_hash_sha512_init(crypto_hash_sha512_state *state) { - state->count[0] = state->count[1] = 0; + static const uint64_t sha512_initstate[8] = { + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL + }; - state->state[0] = 0x6a09e667f3bcc908ULL; - state->state[1] = 0xbb67ae8584caa73bULL; - state->state[2] = 0x3c6ef372fe94f82bULL; - state->state[3] = 0xa54ff53a5f1d36f1ULL; - state->state[4] = 0x510e527fade682d1ULL; - state->state[5] = 0x9b05688c2b3e6c1fULL; - state->state[6] = 0x1f83d9abfb41bd6bULL; - state->state[7] = 0x5be0cd19137e2179ULL; + state->count[0] = state->count[1] = (uint64_t) 0U; + memcpy(state->state, sha512_initstate, sizeof sha512_initstate); return 0; } From f46439c1e277263f0b9de6af4f8737855256adff Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 6 Jun 2015 12:46:22 +0200 Subject: [PATCH 61/69] Ensure that PBKDF2_SHA256() is not used to output more than 128 Go. --- .../crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c index 9b585a27..85bb48bd 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c @@ -53,6 +53,9 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, int k; size_t clen; + if (dkLen > 0x1fffffffe0UL) { + abort(); + } crypto_auth_hmacsha256_init(&PShctx, passwd, passwdlen); crypto_auth_hmacsha256_update(&PShctx, salt, saltlen); From f2afab4b1b43a26136f7340e03b8eec0b1495646 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Mon, 8 Jun 2015 14:22:39 -0400 Subject: [PATCH 62/69] If browser `crypto` is not available, try `msCrypto` before assuming a Node environment --- src/libsodium/randombytes/randombytes.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index 53a7a2fc..014ad161 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -60,7 +60,8 @@ randombytes_stir(void) EM_ASM({ if (Module.getRandomValue === undefined) { try { - var crypto_ = ("object" === typeof window ? window : self).crypto, + var window_ = "object" === typeof window ? window : self, + crypto_ = typeof window_.crypto !== "undefined" ? window_.crypto : window_.msCrypto, randomValuesStandard = function() { var buf = new Uint32Array(1); crypto_.getRandomValues(buf); From d35b364f3125e7ebe38392134f068b79b2c80e53 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 13 Jun 2015 17:31:49 +0200 Subject: [PATCH 63/69] Blake2b: fix flags on architectures with < 32-bit int --- src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c b/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c index 2610477d..12e55c7b 100644 --- a/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c +++ b/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c @@ -46,14 +46,14 @@ static const uint8_t blake2b_sigma[12][16] = /* LCOV_EXCL_START */ static inline int blake2b_set_lastnode( blake2b_state *S ) { - S->f[1] = ~0ULL; + S->f[1] = -1; return 0; } /* LCOV_EXCL_STOP */ #if 0 static inline int blake2b_clear_lastnode( blake2b_state *S ) { - S->f[1] = 0ULL; + S->f[1] = 0; return 0; } #endif @@ -62,7 +62,7 @@ static inline int blake2b_set_lastblock( blake2b_state *S ) { if( S->last_node ) blake2b_set_lastnode( S ); - S->f[0] = ~0ULL; + S->f[0] = -1; return 0; } #if 0 @@ -70,7 +70,7 @@ static inline int blake2b_clear_lastblock( blake2b_state *S ) { if( S->last_node ) blake2b_clear_lastnode( S ); - S->f[0] = 0ULL; + S->f[0] = 0; return 0; } #endif From facb3c4343e959e22053b71a4113e0f7e2bb8324 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 16 Jun 2015 22:42:13 +0200 Subject: [PATCH 64/69] Implicit conversions --- .../ref10/fe_frombytes_curve25519_ref10.c | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libsodium/crypto_scalarmult/curve25519/ref10/fe_frombytes_curve25519_ref10.c b/src/libsodium/crypto_scalarmult/curve25519/ref10/fe_frombytes_curve25519_ref10.c index f5d92efc..cad8f5a3 100644 --- a/src/libsodium/crypto_scalarmult/curve25519/ref10/fe_frombytes_curve25519_ref10.c +++ b/src/libsodium/crypto_scalarmult/curve25519/ref10/fe_frombytes_curve25519_ref10.c @@ -58,16 +58,16 @@ void fe_frombytes(fe h,const unsigned char *s) carry6 = (h6 + (crypto_int64) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26; carry8 = (h8 + (crypto_int64) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26; - h[0] = h0; - h[1] = h1; - h[2] = h2; - h[3] = h3; - h[4] = h4; - h[5] = h5; - h[6] = h6; - h[7] = h7; - h[8] = h8; - h[9] = h9; + h[0] = (crypto_int32) h0; + h[1] = (crypto_int32) h1; + h[2] = (crypto_int32) h2; + h[3] = (crypto_int32) h3; + h[4] = (crypto_int32) h4; + h[5] = (crypto_int32) h5; + h[6] = (crypto_int32) h6; + h[7] = (crypto_int32) h7; + h[8] = (crypto_int32) h8; + h[9] = (crypto_int32) h9; } #endif From b87b3a7ac70e606223740e4ac9943b08949ecbaf Mon Sep 17 00:00:00 2001 From: Jack Wink Date: Wed, 17 Jun 2015 14:05:46 -0400 Subject: [PATCH 65/69] add arm64-v8a support for android builds --- dist-build/android-armv8-a.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 dist-build/android-armv8-a.sh diff --git a/dist-build/android-armv8-a.sh b/dist-build/android-armv8-a.sh new file mode 100755 index 00000000..275b6ba6 --- /dev/null +++ b/dist-build/android-armv8-a.sh @@ -0,0 +1,4 @@ +#!/bin/sh +export TARGET_ARCH=armv8-a +export CFLAGS="-Os -march=${TARGET_ARCH}" +ARCH=arm64 HOST_COMPILER=aarch64-linux-android "$(dirname "$0")/android-build.sh" From 5f74196b0f70e2af16cb610fb22da21effee1bdf Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 19 Jun 2015 18:55:41 +0200 Subject: [PATCH 66/69] scrypt extra parameters checks --- .../nosse/pwhash_scryptsalsa208sha256_nosse.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c index a9eec746..4786e5bf 100644 --- a/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c +++ b/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c @@ -279,6 +279,10 @@ escrypt_kdf_nosse(escrypt_local_t * local, errno = EFBIG; return -1; } + if (N > UINT32_MAX) { + errno = EFBIG; + return -1; + } if (((N & (N - 1)) != 0) || (N < 2)) { errno = EINVAL; return -1; From e2fca8cac5ae5e6b570face2ec8ebbd5faf50a7e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 22 Jun 2015 13:53:35 +0200 Subject: [PATCH 67/69] Add sodium_increment() --- ChangeLog | 2 ++ src/libsodium/include/sodium/utils.h | 3 +++ src/libsodium/sodium/utils.c | 13 +++++++++++++ test/default/sodium_utils.c | 23 +++++++++++++++++++++++ test/default/sodium_utils.exp | 5 +++++ 5 files changed, 46 insertions(+) diff --git a/ChangeLog b/ChangeLog index 502e3e1f..6f6a4734 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,8 @@ crypto_aead_chacha20poly1305_ietf_npubbytes(), crypto_aead_chacha20poly1305_ietf_encrypt() and crypto_aead_chacha20poly1305_ietf_decrypt(). - Sodium can now be used in Windows Store apps. + - The sodium_increment() helper function has been added, to increment +an arbitrary long number (such as a nonce). * Version 1.0.3 - In addition to sodium_bin2hex(), sodium_hex2bin() is now a diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index 42f21bc2..4384262f 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -95,6 +95,9 @@ int sodium_mprotect_readonly(void *ptr); SODIUM_EXPORT int sodium_mprotect_readwrite(void *ptr); +SODIUM_EXPORT +void sodium_increment(unsigned char *n, const size_t nlen); + /* -------- */ int _sodium_alloc_init(void); diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index ae0d2be8..b41c858c 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -510,3 +510,16 @@ sodium_mprotect_readwrite(void *ptr) { return _sodium_mprotect(ptr, _mprotect_readwrite); } + +void +sodium_increment(unsigned char *n, const size_t nlen) +{ + size_t i; + unsigned int c = 1U; + + for (i = (size_t) 0U; i < nlen; i++) { + c += n[i]; + n[i] = (unsigned char) c; + c >>= 8; + } +} diff --git a/test/default/sodium_utils.c b/test/default/sodium_utils.c index 89274deb..137aab0b 100644 --- a/test/default/sodium_utils.c +++ b/test/default/sodium_utils.c @@ -8,6 +8,8 @@ int main(void) unsigned char buf2[1000]; char buf3[33]; unsigned char buf4[4]; + unsigned char nonce[24]; + char nonce_hex[49]; const char *hex; const char *hex_end; size_t bin_len; @@ -60,5 +62,26 @@ int main(void) } printf("dt5: %ld\n", (long) (hex_end - hex)); + memset(nonce, 0, sizeof nonce); + sodium_increment(nonce, sizeof nonce); + printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex, + nonce, sizeof nonce)); + memset(nonce, 255, sizeof nonce); + sodium_increment(nonce, sizeof nonce); + printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex, + nonce, sizeof nonce)); + nonce[1] = 1U; + sodium_increment(nonce, sizeof nonce); + printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex, + nonce, sizeof nonce)); + nonce[1] = 0U; + sodium_increment(nonce, sizeof nonce); + printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex, + nonce, sizeof nonce)); + nonce[0] = 255U; + nonce[2] = 255U; + sodium_increment(nonce, sizeof nonce); + printf("%s\n", sodium_bin2hex(nonce_hex, sizeof nonce_hex, + nonce, sizeof nonce)); return 0; } diff --git a/test/default/sodium_utils.exp b/test/default/sodium_utils.exp index e087a911..661e6570 100644 --- a/test/default/sodium_utils.exp +++ b/test/default/sodium_utils.exp @@ -11,3 +11,8 @@ dt2: 2 dt3: 11 dt4: 11 dt5: 11 +010000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000 +010100000000000000000000000000000000000000000000 +020000000000000000000000000000000000000000000000 +0001ff000000000000000000000000000000000000000000 From 3822caf6c7c6537ce2afff1073c849bed8220bf8 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 22 Jun 2015 15:56:35 +0200 Subject: [PATCH 68/69] Micro-optimization --- src/libsodium/sodium/utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index b41c858c..609820cf 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -515,11 +515,11 @@ void sodium_increment(unsigned char *n, const size_t nlen) { size_t i; - unsigned int c = 1U; + unsigned int c = 1U << 8; for (i = (size_t) 0U; i < nlen; i++) { + c >>= 8; c += n[i]; n[i] = (unsigned char) c; - c >>= 8; } } From a8e411585167f1e794e3eddadb19aef93907df80 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 22 Jun 2015 17:41:55 +0200 Subject: [PATCH 69/69] Export sodium_increment() to Emscripten --- dist-build/emscripten.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist-build/emscripten.sh b/dist-build/emscripten.sh index 9e43ae08..cc80947e 100755 --- a/dist-build/emscripten.sh +++ b/dist-build/emscripten.sh @@ -2,7 +2,7 @@ export MAKE_FLAGS='-j4' export PREFIX="$(pwd)/libsodium-js" -export EXPORTED_FUNCTIONS='["_crypto_aead_chacha20poly1305_abytes","_crypto_aead_chacha20poly1305_decrypt","_crypto_aead_chacha20poly1305_encrypt","_crypto_aead_chacha20poly1305_ietf_decrypt","_crypto_aead_chacha20poly1305_ietf_encrypt","_crypto_aead_chacha20poly1305_ietf_npubbytes","_crypto_aead_chacha20poly1305_keybytes","_crypto_aead_chacha20poly1305_npubbytes","_crypto_aead_chacha20poly1305_nsecbytes","_crypto_auth","_crypto_auth_bytes","_crypto_auth_keybytes","_crypto_auth_verify","_crypto_box_beforenm","_crypto_box_beforenmbytes","_crypto_box_detached","_crypto_box_detached_afternm","_crypto_box_easy","_crypto_box_easy_afternm","_crypto_box_keypair","_crypto_box_macbytes","_crypto_box_noncebytes","_crypto_box_open_detached","_crypto_box_open_detached_afternm","_crypto_box_open_easy","_crypto_box_open_easy_afternm","_crypto_box_publickeybytes","_crypto_box_seal","_crypto_box_seal_open","_crypto_box_sealbytes","_crypto_box_secretkeybytes","_crypto_box_seed_keypair","_crypto_box_seedbytes","_crypto_generichash","_crypto_generichash_bytes","_crypto_generichash_bytes_max","_crypto_generichash_bytes_min","_crypto_generichash_final","_crypto_generichash_init","_crypto_generichash_keybytes","_crypto_generichash_keybytes_max","_crypto_generichash_keybytes_min","_crypto_generichash_statebytes","_crypto_generichash_update","_crypto_hash","_crypto_hash_bytes","_crypto_pwhash_scryptsalsa208sha256","_crypto_pwhash_scryptsalsa208sha256_ll","_crypto_pwhash_scryptsalsa208sha256_memlimit_interactive","_crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive","_crypto_pwhash_scryptsalsa208sha256_opslimit_interactive","_crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive","_crypto_pwhash_scryptsalsa208sha256_saltbytes","_crypto_pwhash_scryptsalsa208sha256_str","_crypto_pwhash_scryptsalsa208sha256_str_verify","_crypto_pwhash_scryptsalsa208sha256_strbytes","_crypto_pwhash_scryptsalsa208sha256_strprefix","_crypto_scalarmult","_crypto_scalarmult_base","_crypto_scalarmult_bytes","_crypto_scalarmult_scalarbytes","_crypto_secretbox_detached","_crypto_secretbox_easy","_crypto_secretbox_keybytes","_crypto_secretbox_macbytes","_crypto_secretbox_noncebytes","_crypto_secretbox_open_detached","_crypto_secretbox_open_easy","_crypto_shorthash","_crypto_shorthash_bytes","_crypto_shorthash_keybytes","_crypto_sign","_crypto_sign_bytes","_crypto_sign_detached","_crypto_sign_ed25519_pk_to_curve25519","_crypto_sign_ed25519_sk_to_curve25519","_crypto_sign_keypair","_crypto_sign_open","_crypto_sign_publickeybytes","_crypto_sign_secretkeybytes","_crypto_sign_seed_keypair","_crypto_sign_seedbytes","_crypto_sign_verify_detached","_randombytes_buf","_randombytes_close","_randombytes_random","_randombytes_set_implementation","_randombytes_stir","_randombytes_uniform","_sodium_bin2hex","_sodium_hex2bin","_sodium_init","_sodium_library_version_major","_sodium_library_version_minor","_sodium_memcmp","_sodium_memzero","_sodium_version_string"]' +export EXPORTED_FUNCTIONS='["_crypto_aead_chacha20poly1305_abytes","_crypto_aead_chacha20poly1305_decrypt","_crypto_aead_chacha20poly1305_encrypt","_crypto_aead_chacha20poly1305_ietf_decrypt","_crypto_aead_chacha20poly1305_ietf_encrypt","_crypto_aead_chacha20poly1305_ietf_npubbytes","_crypto_aead_chacha20poly1305_keybytes","_crypto_aead_chacha20poly1305_npubbytes","_crypto_aead_chacha20poly1305_nsecbytes","_crypto_auth","_crypto_auth_bytes","_crypto_auth_keybytes","_crypto_auth_verify","_crypto_box_beforenm","_crypto_box_beforenmbytes","_crypto_box_detached","_crypto_box_detached_afternm","_crypto_box_easy","_crypto_box_easy_afternm","_crypto_box_keypair","_crypto_box_macbytes","_crypto_box_noncebytes","_crypto_box_open_detached","_crypto_box_open_detached_afternm","_crypto_box_open_easy","_crypto_box_open_easy_afternm","_crypto_box_publickeybytes","_crypto_box_seal","_crypto_box_seal_open","_crypto_box_sealbytes","_crypto_box_secretkeybytes","_crypto_box_seed_keypair","_crypto_box_seedbytes","_crypto_generichash","_crypto_generichash_bytes","_crypto_generichash_bytes_max","_crypto_generichash_bytes_min","_crypto_generichash_final","_crypto_generichash_init","_crypto_generichash_keybytes","_crypto_generichash_keybytes_max","_crypto_generichash_keybytes_min","_crypto_generichash_statebytes","_crypto_generichash_update","_crypto_hash","_crypto_hash_bytes","_crypto_pwhash_scryptsalsa208sha256","_crypto_pwhash_scryptsalsa208sha256_ll","_crypto_pwhash_scryptsalsa208sha256_memlimit_interactive","_crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive","_crypto_pwhash_scryptsalsa208sha256_opslimit_interactive","_crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive","_crypto_pwhash_scryptsalsa208sha256_saltbytes","_crypto_pwhash_scryptsalsa208sha256_str","_crypto_pwhash_scryptsalsa208sha256_str_verify","_crypto_pwhash_scryptsalsa208sha256_strbytes","_crypto_pwhash_scryptsalsa208sha256_strprefix","_crypto_scalarmult","_crypto_scalarmult_base","_crypto_scalarmult_bytes","_crypto_scalarmult_scalarbytes","_crypto_secretbox_detached","_crypto_secretbox_easy","_crypto_secretbox_keybytes","_crypto_secretbox_macbytes","_crypto_secretbox_noncebytes","_crypto_secretbox_open_detached","_crypto_secretbox_open_easy","_crypto_shorthash","_crypto_shorthash_bytes","_crypto_shorthash_keybytes","_crypto_sign","_crypto_sign_bytes","_crypto_sign_detached","_crypto_sign_ed25519_pk_to_curve25519","_crypto_sign_ed25519_sk_to_curve25519","_crypto_sign_keypair","_crypto_sign_open","_crypto_sign_publickeybytes","_crypto_sign_secretkeybytes","_crypto_sign_seed_keypair","_crypto_sign_seedbytes","_crypto_sign_verify_detached","_randombytes_buf","_randombytes_close","_randombytes_random","_randombytes_set_implementation","_randombytes_stir","_randombytes_uniform","_sodium_bin2hex","_sodium_hex2bin","_sodium_increment","_sodium_init","_sodium_library_version_major","_sodium_library_version_minor","_sodium_memcmp","_sodium_memzero","_sodium_version_string"]' export TOTAL_MEMORY=33554432 export JS_EXPORTS_FLAGS="-s EXPORTED_FUNCTIONS=${EXPORTED_FUNCTIONS}" export LDFLAGS="-s TOTAL_MEMORY=${TOTAL_MEMORY} -s RESERVED_FUNCTION_POINTERS=8 -s NO_BROWSER=1"