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 09:47:49 -04:00
|
|
|
#include "utils.h" /* utility functions shared by demos */
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2015-05-27 10:29:57 -04:00
|
|
|
unsigned char key[crypto_auth_KEYBYTES];
|
|
|
|
unsigned char mac[crypto_auth_BYTES];
|
|
|
|
unsigned char message[MAX_INPUT_SIZE];
|
|
|
|
size_t message_len;
|
|
|
|
int ret;
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
puts("Example: crypto_auth\n");
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 10:29:57 -04:00
|
|
|
memset(key, 0, sizeof key);
|
|
|
|
prompt_input("Enter a key > ", (char*)key, sizeof key);
|
|
|
|
puts("Complete key:");
|
|
|
|
print_hex(key, sizeof key);
|
2014-09-24 19:39:35 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
2015-05-27 10:29:57 -04:00
|
|
|
message_len = prompt_input("Enter a message > ",
|
|
|
|
(char*)message, sizeof message);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
printf("Generating %s authentication...\n", crypto_auth_primitive());
|
2015-05-27 10:29:57 -04:00
|
|
|
crypto_auth(mac, message, message_len, key);
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 10:29:57 -04:00
|
|
|
puts("Format: authentication tag::message");
|
|
|
|
print_hex(mac, sizeof mac);
|
2014-09-24 17:40:21 -04:00
|
|
|
fputs("::", stdout);
|
2015-05-27 10:29:57 -04:00
|
|
|
puts((const char*)message);
|
2014-09-24 17:40:21 -04:00
|
|
|
putchar('\n');
|
|
|
|
|
2015-05-27 10:29:57 -04:00
|
|
|
puts("Verifying authentication tag...");
|
|
|
|
ret = crypto_auth_verify(mac, message, message_len, key);
|
|
|
|
print_verification(ret);
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 10:29:57 -04:00
|
|
|
sodium_memzero(key, sizeof key); /* wipe sensitive data */
|
|
|
|
sodium_memzero(mac, sizeof mac);
|
|
|
|
sodium_memzero(message, sizeof message);
|
|
|
|
|
|
|
|
return ret;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-27 06:41:43 -04:00
|
|
|
main(void)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 10:10:07 -04:00
|
|
|
init();
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 06:41:43 -04:00
|
|
|
return auth() != 0;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|