summaryrefslogtreecommitdiffstats
path: root/libtommath/bn_s_mp_rand_platform.c
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2019-10-07 11:37:36 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2019-10-07 11:37:36 (GMT)
commit1e29c4f4ad0ec8903b99967d45b6ea0ea76c6111 (patch)
tree2312b71d74326ff86bcbd4acecb0f10a1c0ad63c /libtommath/bn_s_mp_rand_platform.c
parent1cb1fee6edc063cb49beb0188c2a3db4771846fa (diff)
downloadtcl-1e29c4f4ad0ec8903b99967d45b6ea0ea76c6111.zip
tcl-1e29c4f4ad0ec8903b99967d45b6ea0ea76c6111.tar.gz
tcl-1e29c4f4ad0ec8903b99967d45b6ea0ea76c6111.tar.bz2
Update to latest "develop" branch
Diffstat (limited to 'libtommath/bn_s_mp_rand_platform.c')
-rw-r--r--libtommath/bn_s_mp_rand_platform.c63
1 files changed, 27 insertions, 36 deletions
diff --git a/libtommath/bn_s_mp_rand_platform.c b/libtommath/bn_s_mp_rand_platform.c
index 1349b7c..07555db 100644
--- a/libtommath/bn_s_mp_rand_platform.c
+++ b/libtommath/bn_s_mp_rand_platform.c
@@ -8,11 +8,16 @@
* - Windows
*/
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
-# define MP_ARC4RANDOM
+#define BN_S_READ_ARC4RANDOM_C
+static mp_err s_read_arc4random(void *p, size_t n)
+{
+ arc4random_buf(p, n);
+ return MP_OKAY;
+}
#endif
#if defined(_WIN32) || defined(_WIN32_WCE)
-#define MP_WIN_CSP
+#define BN_S_READ_WINCSP_C
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
@@ -33,7 +38,7 @@
# pragma warning(pop)
#endif
-static mp_err s_read_win_csp(void *p, size_t n)
+static mp_err s_read_wincsp(void *p, size_t n)
{
static HCRYPTPROV hProv = 0;
if (hProv == 0) {
@@ -50,9 +55,9 @@ static mp_err s_read_win_csp(void *p, size_t n)
}
#endif /* WIN32 */
-#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
+#if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 25)
-#define MP_GETRANDOM
+#define BN_S_READ_GETRANDOM_C
#include <sys/random.h>
#include <errno.h>
@@ -78,7 +83,8 @@ static mp_err s_read_getrandom(void *p, size_t n)
/* We assume all platforms besides windows provide "/dev/urandom".
* In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
*/
-#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
+#if !defined(BN_S_READ_WINCSP_C) && !defined(MP_NO_DEV_URANDOM)
+#define BN_S_READ_URANDOM_C
#ifndef MP_DEV_URANDOM
#define MP_DEV_URANDOM "/dev/urandom"
#endif
@@ -86,7 +92,7 @@ static mp_err s_read_getrandom(void *p, size_t n)
#include <errno.h>
#include <unistd.h>
-static mp_err s_read_dev_urandom(void *p, size_t n)
+static mp_err s_read_urandom(void *p, size_t n)
{
int fd;
char *q = (char *)p;
@@ -115,6 +121,7 @@ static mp_err s_read_dev_urandom(void *p, size_t n)
#endif
#if defined(MP_PRNG_ENABLE_LTM_RNG)
+#define BN_S_READ_LTM_RNG
unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
void (*ltm_rng_callback)(void);
@@ -128,37 +135,21 @@ static mp_err s_read_ltm_rng(void *p, size_t n)
}
#endif
+mp_err s_read_arc4random(void *p, size_t n);
+mp_err s_read_wincsp(void *p, size_t n);
+mp_err s_read_getrandom(void *p, size_t n);
+mp_err s_read_urandom(void *p, size_t n);
+mp_err s_read_ltm_rng(void *p, size_t n);
+
mp_err s_mp_rand_platform(void *p, size_t n)
{
-#if defined(MP_ARC4RANDOM)
- arc4random_buf(p, n);
- return MP_OKAY;
-#else
-
- mp_err res = MP_ERR;
-
-#if defined(MP_WIN_CSP)
- res = s_read_win_csp(p, n);
- if (res == MP_OKAY) return res;
-#endif
-
-#if defined(MP_GETRANDOM)
- res = s_read_getrandom(p, n);
- if (res == MP_OKAY) return res;
-#endif
-
-#if defined(MP_DEV_URANDOM)
- res = s_read_dev_urandom(p, n);
- if (res == MP_OKAY) return res;
-#endif
-
-#if defined(MP_PRNG_ENABLE_LTM_RNG)
- res = s_read_ltm_rng(p, n);
- if (res == MP_OKAY) return res;
-#endif
-
- return res;
-#endif
+ mp_err err = MP_ERR;
+ if ((err != MP_OKAY) && MP_HAS(S_READ_ARC4RANDOM)) err = s_read_arc4random(p, n);
+ if ((err != MP_OKAY) && MP_HAS(S_READ_WINCSP)) err = s_read_wincsp(p, n);
+ if ((err != MP_OKAY) && MP_HAS(S_READ_GETRANDOM)) err = s_read_getrandom(p, n);
+ if ((err != MP_OKAY) && MP_HAS(S_READ_URANDOM)) err = s_read_urandom(p, n);
+ if ((err != MP_OKAY) && MP_HAS(S_READ_LTM_RNG)) err = s_read_ltm_rng(p, n);
+ return err;
}
#endif