2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* GraxRabble
|
|
|
|
* Demo programs for libsodium.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include <sodium.h> /* library header */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
#include "demo_utils.h" /* utility functions shared by demos */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2015-05-27 07:02:56 -04:00
|
|
|
unsigned char k[crypto_onetimeauth_KEYBYTES]; /* key */
|
|
|
|
unsigned char a[crypto_onetimeauth_BYTES]; /* authentication */
|
2015-05-27 09:39:34 -04:00
|
|
|
unsigned char m[MAX_INPUT_SIZE]; /* message */
|
2015-05-27 07:02:56 -04:00
|
|
|
size_t mlen; /* message length */
|
2014-09-24 17:40:21 -04:00
|
|
|
int r;
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
sodium_memzero(k, sizeof k); /* must zero the key */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_onetimeauth\n");
|
|
|
|
|
2014-09-24 19:39:35 -04:00
|
|
|
/*
|
|
|
|
* 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);
|
2015-05-27 07:02:56 -04:00
|
|
|
prompt_input("Input your key > ", (char*)k, sizeof k);
|
2014-09-24 19:39:35 -04:00
|
|
|
puts("Your key that you entered");
|
|
|
|
print_hex(k, sizeof k);
|
|
|
|
putchar('\n');
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
mlen = prompt_input("Input your message > ", (char*)m, sizeof m);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
2015-05-27 07:02:56 -04:00
|
|
|
printf("Generating %s authentication...\n", crypto_onetimeauth_primitive());
|
2014-09-24 17:40:21 -04:00
|
|
|
crypto_onetimeauth(a, m, mlen, k);
|
|
|
|
|
|
|
|
puts("Format: authentication token::message");
|
|
|
|
print_hex(a, sizeof a);
|
|
|
|
fputs("::", stdout);
|
2015-05-27 07:02:56 -04:00
|
|
|
puts((const char*)m);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
puts("Verifying authentication...");
|
|
|
|
r = crypto_onetimeauth_verify(a, m, mlen, k);
|
|
|
|
print_verification(r);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
|
|
|
sodium_memzero(k, sizeof k); /* wipe sensitive data */
|
2014-09-24 17:40:21 -04:00
|
|
|
sodium_memzero(a, sizeof a);
|
|
|
|
sodium_memzero(m, sizeof m);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
|
|
|
sodium_init();
|
|
|
|
printf("Using LibSodium %s\n", sodium_version_string());
|
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return onetimeauth() != 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|