diff --git a/configure.ac b/configure.ac index 8e356131..72a85f9f 100644 --- a/configure.ac +++ b/configure.ac @@ -430,6 +430,8 @@ AS_IF([test "x$EMSCRIPTEN" != "x"],[ AC_SUBST(TEST_LDFLAGS) AM_CONDITIONAL([EMSCRIPTEN], [test "x$EMSCRIPTEN" != "x"]) +AM_CONDITIONAL([NATIVECLIENT], [test "x$NATIVECLIENT" != "x"]) + dnl Libtool. LT_INIT([dlopen]) diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index d6813454..ec265f81 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -157,8 +157,14 @@ libsodium_la_SOURCES = \ if !EMSCRIPTEN libsodium_la_SOURCES += \ - randombytes/salsa20/randombytes_salsa20_random.c \ - randombytes/sysrandom/randombytes_sysrandom.c + randombytes/salsa20/randombytes_salsa20_random.c +if NATIVECLIENT +libsodium_la_SOURCES += \ + randombytes/nativeclient/randombytes_nativeclient.c +else +libsodium_la_SOURCES += \ + randombytes/sysrandom/randombytes_sysrandom.c +endif endif if HAVE_TI_MODE diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index 894c371a..a86b5817 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -50,6 +50,7 @@ SODIUM_EXPORT = \ sodium/randombytes.h \ sodium/randombytes_salsa20_random.h \ sodium/randombytes_sysrandom.h \ + sodium/randombytes_nativeclient.h \ sodium/runtime.h \ sodium/utils.h diff --git a/src/libsodium/include/sodium.h b/src/libsodium/include/sodium.h index 207bdede..7e9e13e8 100644 --- a/src/libsodium/include/sodium.h +++ b/src/libsodium/include/sodium.h @@ -43,6 +43,7 @@ #include "sodium/randombytes.h" #include "sodium/randombytes_salsa20_random.h" #include "sodium/randombytes_sysrandom.h" +#include "sodium/randombytes_nativeclient.h" #include "sodium/runtime.h" #include "sodium/utils.h" #include "sodium/version.h" diff --git a/src/libsodium/include/sodium/randombytes_nativeclient.h b/src/libsodium/include/sodium/randombytes_nativeclient.h new file mode 100644 index 00000000..134349cb --- /dev/null +++ b/src/libsodium/include/sodium/randombytes_nativeclient.h @@ -0,0 +1,39 @@ + +#ifndef randombytes_nativeclient_H +#define randombytes_nativeclient_H + +#include +#include + +#include "export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +SODIUM_EXPORT +extern struct randombytes_implementation randombytes_nativeclient_implementation; + +SODIUM_EXPORT +const char *randombytes_nativeclient_implementation_name(void); + +SODIUM_EXPORT +uint32_t randombytes_nativeclient(void); + +SODIUM_EXPORT +void randombytes_nativeclient_stir(void); + +SODIUM_EXPORT +uint32_t randombytes_nativeclient_uniform(const uint32_t upper_bound); + +SODIUM_EXPORT +void randombytes_nativeclient_buf(void * const buf, const size_t size); + +SODIUM_EXPORT +int randombytes_nativeclient_close(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c b/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c new file mode 100644 index 00000000..f0a997e7 --- /dev/null +++ b/src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "nacl/nacl_random.h" +#include "utils.h" +#include "randombytes.h" +#include "randombytes_nativeclient.h" + +void +randombytes_nativeclient_random_stir(void) +{ +} + +int +randombytes_nativeclient_random_close(void) +{ + return 0; +} + +uint32_t +randombytes_nativeclient_random(void) +{ + uint32_t r; + + randombytes_nativeclient_buf(&r, sizeof r); + + return r; +} + +void +randombytes_nativeclient_buf(void * const buf, const size_t size) +{ + size_t readnb; + + if (nacl_secure_random(buf, size, &readnb) != 0) { + abort(); + } + + assert(readnb == size); +} + +const char * +randombytes_nativeclient_implementation_name(void) +{ + return "nativeclient"; +} + +struct randombytes_implementation randombytes_nativeclient_implementation = { + SODIUM_C99(.implementation_name =) randombytes_nativeclient_implementation_name, + SODIUM_C99(.random =) randombytes_nativeclient_random, + SODIUM_C99(.stir =) randombytes_nativeclient_random_stir, + SODIUM_C99(.uniform =) NULL, + SODIUM_C99(.buf =) randombytes_nativeclient_buf, + SODIUM_C99(.close =) randombytes_nativeclient_random_close +}; diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index 014ad161..29becfc0 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -11,10 +11,16 @@ #include "randombytes.h" #include "randombytes_sysrandom.h" +#include "randombytes_nativeclient.h" #ifndef __EMSCRIPTEN__ +#ifdef __native_client__ +static const randombytes_implementation *implementation = + &randombytes_nativeclient_implementation; +#else static const randombytes_implementation *implementation = &randombytes_sysrandom_implementation; +#endif #else static const randombytes_implementation *implementation = NULL; #endif diff --git a/test/default/Makefile.am b/test/default/Makefile.am index a4bf6e7d..7b95c963 100644 --- a/test/default/Makefile.am +++ b/test/default/Makefile.am @@ -122,6 +122,122 @@ DISTCLEANFILES = \ stream4.res \ verify1.res +if NATIVECLIENT +CLEANFILES = \ + aead_chacha20poly1305.final \ + auth.final \ + auth2.final \ + auth3.final \ + auth5.final \ + auth6.final \ + auth7.final \ + box.final \ + box2.final \ + box7.final \ + box8.final \ + box_easy.final \ + box_easy2.final \ + box_seal.final \ + box_seed.final \ + chacha20.final \ + core1.final \ + core2.final \ + core3.final \ + core4.final \ + core5.final \ + core6.final \ + ed25519_convert.final \ + generichash.final \ + generichash2.final \ + generichash3.final \ + hash.final \ + hash2.final \ + hash3.final \ + onetimeauth.final \ + onetimeauth2.final \ + onetimeauth7.final \ + pwhash.final \ + pwhash_scrypt_ll.final \ + randombytes.final \ + scalarmult.final \ + scalarmult2.final \ + scalarmult5.final \ + scalarmult6.final \ + scalarmult7.final \ + secretbox.final \ + secretbox2.final \ + secretbox7.final \ + secretbox8.final \ + secretbox_easy.final \ + secretbox_easy2.final \ + shorthash.final \ + sign.final \ + sodium_core.final \ + sodium_utils.final \ + sodium_version.final \ + stream.final \ + stream2.final \ + stream3.final \ + stream4.final \ + verify1.final \ + aead_chacha20poly1305.nexe \ + auth.nexe \ + auth2.nexe \ + auth3.nexe \ + auth5.nexe \ + auth6.nexe \ + auth7.nexe \ + box.nexe \ + box2.nexe \ + box7.nexe \ + box8.nexe \ + box_easy.nexe \ + box_easy2.nexe \ + box_seal.nexe \ + box_seed.nexe \ + chacha20.nexe \ + core1.nexe \ + core2.nexe \ + core3.nexe \ + core4.nexe \ + core5.nexe \ + core6.nexe \ + ed25519_convert.nexe \ + generichash.nexe \ + generichash2.nexe \ + generichash3.nexe \ + hash.nexe \ + hash2.nexe \ + hash3.nexe \ + onetimeauth.nexe \ + onetimeauth2.nexe \ + onetimeauth7.nexe \ + pwhash.nexe \ + pwhash_scrypt_ll.nexe \ + randombytes.nexe \ + scalarmult.nexe \ + scalarmult2.nexe \ + scalarmult5.nexe \ + scalarmult6.nexe \ + scalarmult7.nexe \ + secretbox.nexe \ + secretbox2.nexe \ + secretbox7.nexe \ + secretbox8.nexe \ + secretbox_easy.nexe \ + secretbox_easy2.nexe \ + shorthash.nexe \ + sign.nexe \ + sodium_core.nexe \ + sodium_utils.nexe \ + sodium_version.nexe \ + stream.nexe \ + stream2.nexe \ + stream3.nexe \ + stream4.nexe \ + verify1.nexe +endif + AM_CPPFLAGS = \ -DTEST_SRCDIR=\"@srcdir@\" \ -I$(top_srcdir)/src/libsodium/include \ @@ -190,10 +306,12 @@ TESTS_TARGETS = \ verify1 if !EMSCRIPTEN +if !NATIVECLIENT TESTS_TARGETS += \ sodium_utils2 \ sodium_utils3 endif +endif check_PROGRAMS = $(TESTS_TARGETS) @@ -373,4 +491,8 @@ stream4_LDADD = $(TESTS_LDADD) verify1_SOURCE = cmptest.h verify1.c verify1_LDADD = $(TESTS_LDADD) +if NATIVECLIENT +LOG_COMPILER = ./nacl-test-wrapper.sh +endif + verify: check diff --git a/test/default/nacl-test-wrapper.sh b/test/default/nacl-test-wrapper.sh new file mode 100755 index 00000000..85053515 --- /dev/null +++ b/test/default/nacl-test-wrapper.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +if [ -z "$NACL_SDK_ROOT" -o -z "$PNACL_TRANSLATE" -o -z "$PNACL_FINALIZE" ] +then + echo "One or more variables need to be set: + \$NACL_SDK_ROOT=$NACL_SDK_ROOT + \$PNACL_TRANSLATE=$PNACL_TRANSLATE + \$PNACL_FINALIZE=$PNACL_FINALIZE" + exit 1 +fi + +if [ ! -f "$1.nexe" ] +then + $PNACL_FINALIZE "$1" -o "$1.final" + $PNACL_TRANSLATE -arch `uname -m` "$1.final" -o "$1.nexe" +fi + +command -v python >/dev/null 2>&1 || { echo >&2 "I require python but it's not installed. Aborting."; exit 1; } +ANY=(`find $NACL_SDK_ROOT -name sel_ldr.py`) +if [ -z ${ANY[0]} ] +then + echo "Couldn't find a sel_ldr.py under $NACL_SDK_ROOT" + exit 1 +fi +python ${ANY[0]} "$1.nexe" diff --git a/test/default/randombytes.c b/test/default/randombytes.c index 4d8efff0..414a6d19 100644 --- a/test/default/randombytes.c +++ b/test/default/randombytes.c @@ -105,7 +105,11 @@ static uint32_t randombytes_uniform_impl(const uint32_t upper_bound) static int impl_tests(void) { +#ifndef __native_client__ randombytes_implementation impl = randombytes_sysrandom_implementation; +#else + randombytes_implementation impl = randombytes_nativeclient_implementation; +#endif uint32_t v = randombytes_random(); impl.uniform = randombytes_uniform_impl;