feat(libcrypto): crypto_random
This commit is contained in:
parent
982cc66c3f
commit
a683e1783f
|
@ -35,11 +35,10 @@ AC_CHECK_INCLUDES_DEFAULT
|
|||
AC_C_CONST
|
||||
AC_C_INLINE
|
||||
|
||||
AC_CHECK_FUNCS_ONCE(strtoull)
|
||||
|
||||
AC_CHECK_HEADERS(m4_normalize([
|
||||
libgen.h
|
||||
getopt.h
|
||||
sys/random.h
|
||||
]))
|
||||
|
||||
AC_CHECK_TYPES([struct option], [], [],
|
||||
|
@ -49,6 +48,8 @@ AC_CHECK_TYPES([struct option], [], [],
|
|||
|
||||
AC_CHECK_FUNCS(m4_normalize([
|
||||
getopt_long
|
||||
strtoull
|
||||
getrandom
|
||||
]))
|
||||
|
||||
AC_CONFIG_FILES([Makefile
|
||||
|
|
|
@ -2,4 +2,4 @@ SUBDIRS = tests
|
|||
|
||||
noinst_LIBRARIES = libcrypto.a
|
||||
AM_CFLAGS = -I$(top_srcdir)
|
||||
libcrypto_a_SOURCES = hchacha.c xchacha.c
|
||||
libcrypto_a_SOURCES = hchacha.c xchacha.c random.c
|
57
libcrypto/random.c
Normal file
57
libcrypto/random.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include "random.h"
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif /* HAVE_CONFIG_H */
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
# include <ntsecapi.h>
|
||||
#else
|
||||
# ifdef HAVE_SYS_RANDOM_H
|
||||
# include <sys/random.h>
|
||||
# else
|
||||
# include <stdio.h>
|
||||
# endif /* HAVE_SYS_RANDOM_H */
|
||||
#endif /* _WIN32 */
|
||||
|
||||
int
|
||||
crypto_random(uint8_t *buff, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return (RtlGenRandom(buff, size));
|
||||
#else
|
||||
# ifdef HAVE_GETRANDOM
|
||||
ssize_t ret;
|
||||
|
||||
ret = getrandom(buff, size, 0)
|
||||
if (ret < size)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
# else
|
||||
FILE *fp;
|
||||
size_t total;
|
||||
size_t byte_read;
|
||||
|
||||
fp = fopen("/dev/urandom", "rb");
|
||||
if (fp == NULL)
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
|
||||
total = 0;
|
||||
do {
|
||||
byte_read = fread(buff + total, 1, size - total, fp);
|
||||
if (byte_read == 0)
|
||||
{
|
||||
fclose(fp);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
total += byte_read;
|
||||
} while (total < size);
|
||||
|
||||
fclose(fp);
|
||||
# endif /* HAVE_GETRANDOM */
|
||||
#endif /* _WIN32 */
|
||||
return (0);
|
||||
}
|
8
libcrypto/random.h
Normal file
8
libcrypto/random.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef CRYPTO_RANDOM_H
|
||||
# define CRYPTO_RANDOM_H 1
|
||||
|
||||
# include <stdint.h>
|
||||
|
||||
int crypto_random(uint8_t *buff, size_t size);
|
||||
|
||||
#endif /* CRYPTO_RANDOM_H */
|
|
@ -1,5 +1,5 @@
|
|||
TESTS = test_hchacha test_xchacha
|
||||
check_PROGRAMS = test_hchacha test_xchacha
|
||||
TESTS = test_hchacha test_xchacha test_random
|
||||
check_PROGRAMS = test_hchacha test_xchacha test_random
|
||||
|
||||
AM_CFLAGS = -I../
|
||||
LDADD = -lcmocka ../libcrypto.a
|
||||
|
@ -7,3 +7,5 @@ LDADD = -lcmocka ../libcrypto.a
|
|||
test_hchacha_SOURCES = test_hchacha.c
|
||||
|
||||
test_xchacha_SOURCES = test_xchacha.c
|
||||
|
||||
test_random_SOURCES = test_random.c
|
28
libcrypto/tests/test_random.c
Normal file
28
libcrypto/tests/test_random.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#include "random.h"
|
||||
|
||||
/* test from https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03#section-2.2 */
|
||||
|
||||
|
||||
static void
|
||||
test_random(void **state)
|
||||
{
|
||||
uint8_t buff[512];
|
||||
|
||||
assert_return_code(crypto_random(buff, 512), 0);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_random),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
Loading…
Reference in a new issue