From a313e0be660dadddf09a002a04f35202a529c6a5 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 27 May 2015 20:53:54 +0200 Subject: [PATCH] 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);