From 34a4931d9a0ae5579268f1f7d8a99c738b9b81a7 Mon Sep 17 00:00:00 2001 From: James Robson Date: Mon, 22 Jun 2015 13:02:21 -0500 Subject: [PATCH] Initial patch for Chrome NaCl implementation --- configure.ac | 2 + src/libsodium/Makefile.am | 10 +++- src/libsodium/include/Makefile.am | 1 + src/libsodium/include/sodium.h | 1 + .../include/sodium/randombytes_nativeclient.h | 43 ++++++++++++++ .../nativeclient/randombytes_nativeclient.c | 56 +++++++++++++++++++ src/libsodium/randombytes/randombytes.c | 6 ++ test/default/Makefile.am | 6 ++ test/default/nacl-test-wrapper.sh | 25 +++++++++ test/default/randombytes.c | 4 ++ 10 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/libsodium/include/sodium/randombytes_nativeclient.h create mode 100644 src/libsodium/randombytes/nativeclient/randombytes_nativeclient.c create mode 100755 test/default/nacl-test-wrapper.sh diff --git a/configure.ac b/configure.ac index 21b8f102..2694c78b 100644 --- a/configure.ac +++ b/configure.ac @@ -428,6 +428,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..29064815 --- /dev/null +++ b/src/libsodium/include/sodium/randombytes_nativeclient.h @@ -0,0 +1,43 @@ + +#ifndef randombytes_nativeclient_H +#define randombytes_nativeclient_H + +/* + * THREAD SAFETY: TODO + */ + +#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 53a7a2fc..8444cd4e 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..e9ecf755 100644 --- a/test/default/Makefile.am +++ b/test/default/Makefile.am @@ -190,10 +190,12 @@ TESTS_TARGETS = \ verify1 if !EMSCRIPTEN +if !NATIVECLIENT TESTS_TARGETS += \ sodium_utils2 \ sodium_utils3 endif +endif check_PROGRAMS = $(TESTS_TARGETS) @@ -373,4 +375,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;