Merge pull request #1122 from mrdomino/c3-getentropy

c3_rand cleanup
This commit is contained in:
Joe Bryan 2019-01-11 13:17:04 -05:00 committed by GitHub
commit 0ee959b9e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 105 deletions

View File

@ -1,3 +1,4 @@
dist: xenial
language: node_js
node_js:
- 4
@ -32,6 +33,8 @@ addons:
packages:
- python3
- python3-pip
- python3-setuptools
- python3-wheel
- libgmp3-dev
- libsigsegv-dev
- openssl

View File

@ -55,6 +55,11 @@
int
c3_cooked();
/* Fill 16 words (64 bytes) with high-quality entropy.
*/
void
c3_rand(c3_w* rad_w);
/* Short integers.
*/
# define c3_s1(a) ( (a) )

View File

@ -2,8 +2,6 @@
**
** This file is in the public domain.
*/
/** Must be compiled on gcc with C99 support.
**/
#include "config.h"
@ -33,6 +31,7 @@
# include <setjmp.h>
# include <stdio.h>
# include <signal.h>
# include <sys/syscall.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/mman.h>
@ -50,6 +49,7 @@
# include <machine/endian.h>
# include <machine/byte_order.h>
# include <stdio.h>
# include <sys/random.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/mman.h>
@ -140,7 +140,6 @@
# define c3_bswap_16(x) bswap_16(x)
# define c3_bswap_32(x) bswap_32(x)
# define c3_bswap_64(x) bswap_64(x)
# elif defined(U3_OS_osx)
# define c3_bswap_16(x) NXSwapShort(x)
# define c3_bswap_32(x) NXSwapInt(x)
@ -149,7 +148,7 @@
# error "port: byte swap"
# endif
/* Sync
/* Sync.
*/
# if defined(U3_OS_linux)
# define c3_sync(fd) (fdatasync(fd))
@ -161,7 +160,7 @@
# error "port: sync"
# endif
/* Purge
/* Purge.
*/
# if defined(U3_OS_linux)
# include <stdio_ext.h>
@ -172,7 +171,7 @@
# error "port: fpurge"
# endif
/* Stat struct
/* Stat.
*/
# if defined(U3_OS_linux)
# define c3_stat_mtime(dp) (u3_time_t_in_ts((dp)->st_mtime))
@ -186,11 +185,18 @@
# error "port: timeconvert"
# endif
/* Entropy
/* Entropy.
*/
#define c3_rand u3_sist_rand
# if defined(U3_OS_linux)
# define c3_getentropy(B, L) \
((L) == syscall(SYS_getrandom, B, L, 0) ? 0 : -1)
# elif defined(U3_OS_bsd) || defined(U3_OS_osx)
# define c3_getentropy getentropy
# else
# error "port: getentropy"
# endif
/* Static assertion
/* Static assertion.
*/
# define ASSERT_CONCAT_(a, b) a##b
# define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)

View File

@ -1182,11 +1182,6 @@
void
u3_sist_get(const c3_c* key_c, c3_y* val_y);
/* u3_sist_rand(): fill 8 words (32 bytes) with high-quality entropy.
*/
void
u3_sist_rand(c3_w* rad_w);
/** HTTP client.
**/
/* u3_cttp_ef_thus(): send %thus effect to cttp.

View File

@ -12,12 +12,6 @@
#include "all.h"
#include "vere/vere.h"
#if defined(U3_OS_linux)
#define DEVRANDOM "/dev/urandom"
#else
#define DEVRANDOM "/dev/random"
#endif
/* u3_sist_pack(): write a blob to disk, transferring.
*/
c3_d
@ -411,23 +405,15 @@ _sist_bask(c3_c* pop_c, u3_noun may)
}
#endif
/* u3_sist_rand(): fill a 512-bit (16-word) buffer.
/* c3_rand(): fill a 512-bit (16-word) buffer.
*/
void
u3_sist_rand(c3_w* rad_w)
c3_rand(c3_w* rad_w)
{
#if defined(U3_OS_bsd) && defined(__OpenBSD__)
if (-1 == getentropy(rad_w, 64)) {
c3_assert(!"lo_rand");
if ( 0 != c3_getentropy(rad_w, 64) ) {
uL(fprintf(uH, "c3_rand getentropy: %s\n", strerror(errno)));
u3_lo_bail();
}
#else
c3_i fid_i = open(DEVRANDOM, O_RDONLY);
if ( 64 != read(fid_i, (c3_y*) rad_w, 64) ) {
c3_assert(!"lo_rand");
}
close(fid_i);
#endif
}
/* _sist_fast(): offer to save passcode by mug in home directory.