From ea2c1e86e29e956d3565ce6d9d7292de1420af58 Mon Sep 17 00:00:00 2001 From: James Robson Date: Fri, 22 May 2015 09:56:51 -0600 Subject: [PATCH 1/5] Added patch to obtain random bytes for Chrome NaCl via IRT -- allows build with NaCl SDK toolchain --- src/libsodium/randombytes/randombytes.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index 53a7a2fc..f7ce0a94 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -9,6 +9,12 @@ # include #endif + +#ifdef __native_client__ +#include +#include +#endif + #include "randombytes.h" #include "randombytes_sysrandom.h" @@ -114,9 +120,24 @@ void randombytes_buf(void * const buf, const size_t size) { #ifndef __EMSCRIPTEN__ +#ifdef __native_client__ + size_t n = 0; + struct nacl_irt_random rand_intf; + + if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, + &rand_intf, sizeof(rand_intf)) != sizeof(rand_intf)) abort(); + + while (n < size) { + size_t nread; + if (rand_intf.get_random_bytes( + (unsigned char *)buf+n, size-n, &nread) != 0) abort(); + n += nread; + } +#else if (size > (size_t) 0U) { implementation->buf(buf, size); } +#endif #else unsigned char *p = buf; size_t i; From 34a4931d9a0ae5579268f1f7d8a99c738b9b81a7 Mon Sep 17 00:00:00 2001 From: James Robson Date: Mon, 22 Jun 2015 13:02:21 -0500 Subject: [PATCH 2/5] 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; From 8beae1725318fea9376a8f0bd51d21be211ec4da Mon Sep 17 00:00:00 2001 From: James Robson Date: Mon, 22 Jun 2015 13:11:33 -0500 Subject: [PATCH 3/5] revert original hack --- src/libsodium/randombytes/randombytes.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/libsodium/randombytes/randombytes.c b/src/libsodium/randombytes/randombytes.c index f7ce0a94..53a7a2fc 100644 --- a/src/libsodium/randombytes/randombytes.c +++ b/src/libsodium/randombytes/randombytes.c @@ -9,12 +9,6 @@ # include #endif - -#ifdef __native_client__ -#include -#include -#endif - #include "randombytes.h" #include "randombytes_sysrandom.h" @@ -120,24 +114,9 @@ void randombytes_buf(void * const buf, const size_t size) { #ifndef __EMSCRIPTEN__ -#ifdef __native_client__ - size_t n = 0; - struct nacl_irt_random rand_intf; - - if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, - &rand_intf, sizeof(rand_intf)) != sizeof(rand_intf)) abort(); - - while (n < size) { - size_t nread; - if (rand_intf.get_random_bytes( - (unsigned char *)buf+n, size-n, &nread) != 0) abort(); - n += nread; - } -#else if (size > (size_t) 0U) { implementation->buf(buf, size); } -#endif #else unsigned char *p = buf; size_t i; From e119c3e502493c458f69906311969972c052c2dc Mon Sep 17 00:00:00 2001 From: James Robson Date: Mon, 22 Jun 2015 14:16:07 -0500 Subject: [PATCH 4/5] Added .final and .nexe output files to CLEANFILES --- test/default/Makefile.am | 116 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/test/default/Makefile.am b/test/default/Makefile.am index e9ecf755..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 \ From 8444667b8bbe2acf911e5c62a2552e7dd23972c4 Mon Sep 17 00:00:00 2001 From: James Robson Date: Tue, 23 Jun 2015 13:34:29 -0500 Subject: [PATCH 5/5] removed comment --- src/libsodium/include/sodium/randombytes_nativeclient.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libsodium/include/sodium/randombytes_nativeclient.h b/src/libsodium/include/sodium/randombytes_nativeclient.h index 29064815..134349cb 100644 --- a/src/libsodium/include/sodium/randombytes_nativeclient.h +++ b/src/libsodium/include/sodium/randombytes_nativeclient.h @@ -2,10 +2,6 @@ #ifndef randombytes_nativeclient_H #define randombytes_nativeclient_H -/* - * THREAD SAFETY: TODO - */ - #include #include