1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 04:25:10 -07:00

Merge branch 'master' of github.com:jedisct1/libsodium

* 'master' of github.com:jedisct1/libsodium:
  Add -S for curl
  randombytes: make the emscripten version consistent with others
This commit is contained in:
Frank Denis 2019-09-25 17:16:43 +02:00
commit e1bff2608f
2 changed files with 69 additions and 53 deletions

View File

@ -39,11 +39,11 @@ fi
command -v curl >/dev/null 2>&1 && {
echo "Downloading config.guess and config.sub..."
curl -sL -o config.guess \
curl -sSL -o config.guess \
'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD' &&
mv -f config.guess build-aux/config.guess
curl -sL -o config.sub \
curl -sSL -o config.sub \
'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD' &&
mv -f config.sub build-aux/config.sub

View File

@ -30,60 +30,28 @@ static const randombytes_implementation *implementation;
# ifdef __EMSCRIPTEN__
# define RANDOMBYTES_DEFAULT_IMPLEMENTATION NULL
# else
# define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation;
# define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_sysrandom_implementation
# endif
#endif
static void
randombytes_init_if_needed(void)
#ifdef __EMSCRIPTEN__
static const char *
javascript_implementation_name(void)
{
if (implementation == NULL) {
implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION;
randombytes_stir();
}
}
int
randombytes_set_implementation(randombytes_implementation *impl)
{
implementation = impl;
return 0;
}
const char *
randombytes_implementation_name(void)
{
#ifndef __EMSCRIPTEN__
randombytes_init_if_needed();
return implementation->implementation_name();
#else
return "js";
#endif
}
uint32_t
randombytes_random(void)
static uint32_t
javascript_random(void)
{
#ifndef __EMSCRIPTEN__
randombytes_init_if_needed();
return implementation->random();
#else
return EM_ASM_INT_V({
return Module.getRandomValue();
});
#endif
}
void
randombytes_stir(void)
static void
javascript_stir(void)
{
#ifndef __EMSCRIPTEN__
randombytes_init_if_needed();
if (implementation->stir != NULL) {
implementation->stir();
}
#else
EM_ASM({
if (Module.getRandomValue === undefined) {
try {
@ -111,7 +79,66 @@ randombytes_stir(void)
}
}
});
}
static void
javascript_buf(void * const buf, const size_t size)
{
unsigned char *p = (unsigned char *) buf;
size_t i;
for (i = (size_t) 0U; i < size; i++) {
p[i] = (unsigned char) randombytes_random();
}
}
#endif
static void
randombytes_init_if_needed(void)
{
if (implementation == NULL) {
#ifdef __EMSCRIPTEN__
static randombytes_implementation javascript_implementation;
javascript_implementation.implementation_name = javascript_implementation_name;
javascript_implementation.random = javascript_random;
javascript_implementation.stir = javascript_stir;
javascript_implementation.buf = javascript_buf;
implementation = &javascript_implementation;
#else
implementation = RANDOMBYTES_DEFAULT_IMPLEMENTATION;
#endif
randombytes_stir();
}
}
int
randombytes_set_implementation(randombytes_implementation *impl)
{
implementation = impl;
return 0;
}
const char *
randombytes_implementation_name(void)
{
randombytes_init_if_needed();
return implementation->implementation_name();
}
uint32_t
randombytes_random(void)
{
randombytes_init_if_needed();
return implementation->random();
}
void
randombytes_stir(void)
{
randombytes_init_if_needed();
if (implementation->stir != NULL) {
implementation->stir();
}
}
uint32_t
@ -120,12 +147,10 @@ randombytes_uniform(const uint32_t upper_bound)
uint32_t min;
uint32_t r;
#ifndef __EMSCRIPTEN__
randombytes_init_if_needed();
if (implementation->uniform != NULL) {
return implementation->uniform(upper_bound);
}
#endif
if (upper_bound < 2) {
return 0;
}
@ -142,19 +167,10 @@ randombytes_uniform(const uint32_t upper_bound)
void
randombytes_buf(void * const buf, const size_t size)
{
#ifndef __EMSCRIPTEN__
randombytes_init_if_needed();
if (size > (size_t) 0U) {
implementation->buf(buf, size);
}
#else
unsigned char *p = (unsigned char *) buf;
size_t i;
for (i = (size_t) 0U; i < size; i++) {
p[i] = (unsigned char) randombytes_random();
}
#endif
}
void