1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-20 18:45:32 -07:00
This commit is contained in:
Frank Denis 2015-05-27 13:02:56 +02:00
parent fb6bb61a72
commit ba3fe15b75
13 changed files with 167 additions and 208 deletions

View File

@ -10,8 +10,6 @@
#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
@ -26,7 +24,6 @@ auth(void)
size_t mlen; /* message length */
int r;
puts("Example: crypto_auth\n");
/*
@ -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,7 +47,7 @@ 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...");
@ -71,4 +68,3 @@ main(void)
return auth() != 0;
}

View File

@ -10,8 +10,6 @@
#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
@ -71,7 +69,7 @@ box(void)
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);
@ -95,15 +93,16 @@ 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_sk, sizeof bob_sk);
@ -123,4 +122,3 @@ main(void)
return box() != 0;
}

View File

@ -10,8 +10,6 @@
#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
@ -38,8 +36,8 @@ box(void)
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 + crypto_box_ZEROBYTES]; /* plaintext */
unsigned char c[BUFFER_SIZE + crypto_box_ZEROBYTES]; /* ciphertext */
size_t mlen; /* length */
int r;
@ -88,7 +86,7 @@ box(void)
/* read input */
mlen = prompt_input("Input your message > ",
(char*) m + crypto_box_ZEROBYTES,
(char*)m + crypto_box_ZEROBYTES,
sizeof m - crypto_box_ZEROBYTES);
/* must zero at least the padding */
@ -119,16 +117,15 @@ box(void)
/* 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_sk, sizeof bob_sk);
@ -150,4 +147,3 @@ main(void)
return box() != 0;
}

View File

@ -9,8 +9,6 @@
#include "demo_utils.h" /* demo utility header */
/* ================================================================== *
* utility functions *
* ================================================================== */
@ -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");
}

View File

@ -4,19 +4,12 @@
#ifndef DEMO_UTILS_H
#define DEMO_UTILS_H
#include <stdlib.h>
#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 */

View File

@ -10,8 +10,6 @@
#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.
@ -31,9 +29,9 @@ generichash(void)
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;
}

View File

@ -10,8 +10,6 @@
#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
@ -29,7 +27,7 @@ generichashstream(void)
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());
@ -38,8 +36,9 @@ generichashstream(void)
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);
@ -62,4 +61,3 @@ main(void)
generichashstream();
return 0;
}

View File

@ -10,8 +10,6 @@
#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.
@ -25,7 +23,7 @@ hash(void)
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;
}

View File

@ -10,8 +10,6 @@
#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
@ -22,7 +20,7 @@
static int
onetimeauth(void)
{
unsigned char k[crypto_onetimeauth_KEYBYTES];/* key */
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 */
@ -39,22 +37,21 @@ 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...");
@ -75,4 +72,3 @@ main(void)
return onetimeauth() != 0;
}

View File

@ -10,8 +10,6 @@
#include "demo_utils.h" /* utility functions shared by demos */
/*
* This is a wrapper around stream which does XOR automatically.
*
@ -34,7 +32,7 @@ secretbox(void)
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...");
@ -45,7 +43,7 @@ secretbox(void)
putchar('\n');
mlen = prompt_input("Input your message > ",
(char*) m + crypto_secretbox_ZEROBYTES,
(char*)m + crypto_secretbox_ZEROBYTES,
sizeof m - crypto_secretbox_ZEROBYTES);
/* must zero at least the padding */
@ -76,8 +74,7 @@ secretbox(void)
/* 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);
@ -85,8 +82,8 @@ secretbox(void)
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(n, sizeof n);
@ -103,4 +100,3 @@ main(void)
return secretbox() != 0;
}

View File

@ -10,8 +10,6 @@
#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
@ -28,9 +26,9 @@ shorthash(void)
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;
}

View File

@ -10,8 +10,6 @@
#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
@ -45,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("Input your message > ", (char*)m,
sizeof m - crypto_sign_BYTES);
putc('\n', stdout);
puts("Notice the message has no prepended padding");
@ -66,14 +64,15 @@ sign(void)
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);
if (r == 0)
printf("Message: %s\n\n", m);
sodium_memzero(pk, sizeof pk); /* wipe sensitive data */
sodium_memzero(sk, sizeof sk);
@ -90,4 +89,3 @@ main(void)
return sign() != 0;
}

View File

@ -10,8 +10,6 @@
#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.
@ -35,7 +33,7 @@ stream(void)
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,7 +64,8 @@ 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(n, sizeof n);
@ -83,4 +82,3 @@ main(void)
return stream() != 0;
}