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; } -