diff options
| author | jvoisin | 2017-10-05 16:54:45 +0200 |
|---|---|---|
| committer | GitHub | 2017-10-05 16:54:45 +0200 |
| commit | b769b7d6b9409b3f0d1dd8410fecbeae2e2ddd0c (patch) | |
| tree | 5963179f6db85e4fe253a2d515264192379c0239 /src | |
| parent | 49915f36362e1c80a3adbac498ae743296ba1134 (diff) | |
Use PHP's entropy generation primitive, instead of a ghetto one (#24)
Diffstat (limited to 'src')
| -rw-r--r-- | src/tweetnacl.c | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/src/tweetnacl.c b/src/tweetnacl.c index 937e879..ad5dae5 100644 --- a/src/tweetnacl.c +++ b/src/tweetnacl.c | |||
| @@ -1,3 +1,17 @@ | |||
| 1 | /* Since TweetNacl doesn't come with a `randombytes` implementation, | ||
| 2 | we're using the one from PHP.*/ | ||
| 3 | #include "php_snuffleupagus.h" | ||
| 4 | #include "ext/standard/php_random.h" | ||
| 5 | |||
| 6 | ZEND_DECLARE_MODULE_GLOBALS(snuffleupagus) | ||
| 7 | |||
| 8 | void randombytes(unsigned char *x, unsigned long long xlen) { | ||
| 9 | assert(SIZE_MAX >= ULLONG_MAX); // max(size_t) > max(ull) ? | ||
| 10 | php_random_bytes(x, xlen, 1); | ||
| 11 | } | ||
| 12 | |||
| 13 | // And now, the original code of tweetnacl - https://tweetnacl.cr.yp.to/ | ||
| 14 | |||
| 1 | #include "tweetnacl.h" | 15 | #include "tweetnacl.h" |
| 2 | #define FOR(i,n) for (i = 0;i < n;++i) | 16 | #define FOR(i,n) for (i = 0;i < n;++i) |
| 3 | #define sv static void | 17 | #define sv static void |
| @@ -8,38 +22,6 @@ typedef unsigned long long u64; | |||
| 8 | typedef long long i64; | 22 | typedef long long i64; |
| 9 | typedef i64 gf[16]; | 23 | typedef i64 gf[16]; |
| 10 | 24 | ||
| 11 | |||
| 12 | /* it's really stupid that there isn't a syscall for this */ | ||
| 13 | |||
| 14 | static int fd = -1; | ||
| 15 | |||
| 16 | void randombytes(unsigned char *x,unsigned long long xlen) | ||
| 17 | { | ||
| 18 | int i; | ||
| 19 | |||
| 20 | if (fd == -1) { | ||
| 21 | for (;;) { | ||
| 22 | fd = open("/dev/urandom",O_RDONLY); | ||
| 23 | if (fd != -1) break; | ||
| 24 | sleep(1); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | while (xlen > 0) { | ||
| 29 | if (xlen < 1048576) i = xlen; else i = 1048576; | ||
| 30 | |||
| 31 | i = read(fd,x,i); | ||
| 32 | if (i < 1) { | ||
| 33 | sleep(1); | ||
| 34 | continue; | ||
| 35 | } | ||
| 36 | |||
| 37 | x += i; | ||
| 38 | xlen -= i; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | static const u8 | 25 | static const u8 |
| 44 | _0[16], | 26 | _0[16], |
| 45 | _9[32] = {9}; | 27 | _9[32] = {9}; |
