2014-09-24 17:40:21 -04:00
|
|
|
/*
|
|
|
|
* These are the utility functions shared by all demo programs.
|
|
|
|
*/
|
2015-05-27 09:27:15 -04:00
|
|
|
#include <limits.h>
|
2014-09-24 17:40:21 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-27 09:32:22 -04:00
|
|
|
#include <sodium.h>
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 09:47:49 -04:00
|
|
|
#include "utils.h"
|
2014-09-24 17:40:21 -04:00
|
|
|
|
|
|
|
/*
|
2015-05-27 09:39:34 -04:00
|
|
|
* print_hex() is a wrapper around sodium_bin2hex() which allocates
|
2015-05-27 15:04:32 -04:00
|
|
|
* temporary memory then immediately prints the result followed by \n
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
void
|
2015-05-27 09:27:15 -04:00
|
|
|
print_hex(const void *bin, const size_t bin_len)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 09:27:15 -04:00
|
|
|
char *hex;
|
|
|
|
size_t hex_size;
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 09:27:15 -04:00
|
|
|
if (bin_len >= SIZE_MAX / 2) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
hex_size = bin_len * 2 + 1;
|
|
|
|
if ((hex = malloc(hex_size)) == NULL) {
|
|
|
|
abort();
|
|
|
|
}
|
2014-09-24 17:40:21 -04:00
|
|
|
/* the library supplies a few utility functions like the one below */
|
2015-05-27 09:27:15 -04:00
|
|
|
if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) {
|
|
|
|
abort();
|
|
|
|
}
|
2015-05-27 15:04:32 -04:00
|
|
|
puts(hex);
|
2015-05-27 09:27:15 -04:00
|
|
|
free(hex);
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a prompt for input by user. It will save the input into a buffer
|
|
|
|
* of a specific size with room for the null terminator while removing
|
|
|
|
* trailing newline characters.
|
|
|
|
*/
|
|
|
|
size_t
|
2015-05-27 12:10:28 -04:00
|
|
|
prompt_input(const char *prompt, char *input, const size_t max_input_len,
|
|
|
|
int variable_length)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 12:10:28 -04:00
|
|
|
char input_tmp[MAX_INPUT_SIZE + 1U];
|
2015-05-27 09:27:15 -04:00
|
|
|
size_t actual_input_len;
|
2014-09-24 17:40:21 -04:00
|
|
|
|
2015-05-27 12:10:28 -04:00
|
|
|
if (variable_length != 0) {
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("\nEnter %s (%zu bytes max) > ", prompt, max_input_len);
|
2015-05-27 12:10:28 -04:00
|
|
|
} else {
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("\nEnter %s (%zu bytes) > ", prompt, max_input_len);
|
2015-05-27 12:10:28 -04:00
|
|
|
}
|
2015-05-27 09:27:15 -04:00
|
|
|
fflush(stdout);
|
2015-05-27 12:10:28 -04:00
|
|
|
fgets(input_tmp, sizeof input_tmp, stdin);
|
|
|
|
actual_input_len = strlen(input_tmp);
|
2015-05-27 07:02:56 -04:00
|
|
|
|
2015-05-27 12:10:28 -04:00
|
|
|
/* trim \n */
|
|
|
|
if (actual_input_len > 0 && input_tmp[actual_input_len - 1] == '\n') {
|
|
|
|
input_tmp[actual_input_len - 1] = '\0';
|
2015-05-27 09:27:15 -04:00
|
|
|
--actual_input_len;
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
2015-05-27 12:10:28 -04:00
|
|
|
|
|
|
|
if (actual_input_len > max_input_len) {
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("Warning: truncating input to %zu bytes\n\n", max_input_len);
|
2015-05-27 12:10:28 -04:00
|
|
|
actual_input_len = max_input_len;
|
|
|
|
} else if (actual_input_len < max_input_len && variable_length == 0) {
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n\n",
|
2015-05-27 12:10:28 -04:00
|
|
|
max_input_len, actual_input_len);
|
|
|
|
memset(input, 0, max_input_len);
|
|
|
|
} else {
|
2015-05-27 14:53:54 -04:00
|
|
|
printf("Length: %zu bytes\n\n", actual_input_len);
|
2015-05-27 12:10:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(input, input_tmp, actual_input_len);
|
|
|
|
if (variable_length == 0) {
|
|
|
|
return max_input_len;
|
|
|
|
} else {
|
|
|
|
return actual_input_len;
|
|
|
|
}
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-05-27 09:39:34 -04:00
|
|
|
* Display whether the function was sucessful or failed.
|
2014-09-24 17:40:21 -04:00
|
|
|
*/
|
|
|
|
void
|
2015-05-27 15:01:49 -04:00
|
|
|
print_verification(int ret)
|
2014-09-24 17:40:21 -04:00
|
|
|
{
|
2015-05-27 15:01:49 -04:00
|
|
|
if (ret == 0)
|
2015-05-27 10:29:57 -04:00
|
|
|
puts("Success!\n");
|
2015-05-27 07:02:56 -04:00
|
|
|
else
|
2015-05-27 10:29:57 -04:00
|
|
|
puts("Failure.\n");
|
2014-09-24 17:40:21 -04:00
|
|
|
}
|
2015-05-27 10:10:07 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
sodium_init();
|
|
|
|
printf("Using libsodium %s\n", sodium_version_string());
|
|
|
|
}
|