[Git][NTPsec/ntpsec][master] 3 commits: Cleanup ntp_random()
Hal Murray
gitlab at mg.gitlab.com
Sun Jul 8 01:19:55 UTC 2018
Hal Murray pushed to branch master at NTPsec / ntpsec
Commits:
ff4dde80 by Hal Murray at 2018-07-07T22:24:17Z
Cleanup ntp_random()
drop use of ntp_be32dec
check return code - crash if didn't work
- - - - -
693211aa by Hal Murray at 2018-07-07T22:39:56Z
Add crude test for ntp_random
- - - - -
1b3f83d9 by Hal Murray at 2018-07-07T22:48:41Z
Move ntp_random to shared, drop dummy ntp_random() from pymodule.c
- - - - -
6 changed files:
- libntp/ntp_random.c
- libntp/pymodule.c
- libntp/wscript
- tests/common/tests_main.c
- + tests/libntp/ntp_random.c
- tests/wscript
Changes:
=====================================
libntp/ntp_random.c
=====================================
@@ -11,12 +11,21 @@
#include "config.h"
#include "ntp.h"
-#include "ntp_endian.h"
+
+/* NB: RAND_bytes comes from OpenSSL
+ * Starting in version 1.1.1, it reseeds itself occasionally.
+ * That needs access to /dev/urandom which may be blocked by chroot jails.
+ */
int32_t
ntp_random(void)
{
- unsigned char rnd[sizeof(uint32_t)];
- RAND_bytes(rnd, sizeof(rnd));
- return (int32_t)ntp_be32dec(rnd);
+ int err;
+ uint32_t rnd;
+ err = RAND_bytes((unsigned char *)&rnd, sizeof(rnd));
+ if (1 != err) {
+ msyslog(LOG_ERR, "ERR: ntp_random - RAND_bytes failed");
+ exit(1);
+ }
+ return rnd;
}
=====================================
libntp/pymodule.c
=====================================
@@ -142,12 +142,6 @@ ntpc_step_systime(PyObject *self, PyObject *args)
return Py_BuildValue("d", step_systime(full_adjustment, ntp_set_tod));
}
-int32_t ntp_random(void)
-/* stub random function for get_systime() */
-{
- return 0;
-}
-
/* List of functions defined in the module */
static PyMethodDef ntpc_methods[] = {
=====================================
libntp/wscript
=====================================
@@ -15,7 +15,6 @@ def build(ctx):
"macencrypt.c",
"netof.c",
"ntp_endian.c",
- "ntp_random.c",
"ntp_dns.c",
"numtoa.c",
"recvbuff.c",
@@ -33,6 +32,7 @@ def build(ctx):
"lib_strbuf.c",
"msyslog.c",
"ntp_calendar.c",
+ "ntp_random.c",
"prettydate.c",
"statestr.c",
"systime.c",
=====================================
tests/common/tests_main.c
=====================================
@@ -51,6 +51,7 @@ static void RunAllTests(void)
RUN_TEST_GROUP(netof6);
RUN_TEST_GROUP(numtoa);
RUN_TEST_GROUP(prettydate);
+ RUN_TEST_GROUP(random);
RUN_TEST_GROUP(recvbuff);
RUN_TEST_GROUP(refidsmear);
RUN_TEST_GROUP(socktoa);
=====================================
tests/libntp/ntp_random.c
=====================================
@@ -0,0 +1,37 @@
+#include "config.h"
+#include "ntp.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(random);
+
+TEST_SETUP(random) {}
+
+TEST_TEAR_DOWN(random) {}
+
+
+TEST(random, random32) {
+ int i;
+ uint32_t ones = 0;
+ uint32_t zeros = ~0;
+
+ /* This is just a crude sanity check.
+ * It could fail when working correctly,
+ * but the chances are pretty small.
+ * It won't be reproducable. ;)
+ * You can test this code by making the loop count smaller.
+ */
+ for (i=0; i<99; i++) {
+ uint32_t sample = ntp_random();
+ ones |= sample;
+ zeros &= sample;
+ }
+
+ TEST_ASSERT_EQUAL_INT32(~0, ones);
+ TEST_ASSERT_EQUAL_INT32(0, zeros);
+}
+
+TEST_GROUP_RUNNER(random) {
+ RUN_TEST_CASE(random, random32);
+}
=====================================
tests/wscript
=====================================
@@ -35,6 +35,7 @@ def build(ctx):
libntp_source = [
"libntp/authkeys.c",
"libntp/ntp_calendar.c",
+ "libntp/ntp_random.c",
"libntp/clocktime.c",
"libntp/decodenetnum.c",
"libntp/hextolfp.c",
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d3b93d7d29010d7706c748929e3a1086831830f0...1b3f83d95418762bf610a3af5b2555dd5b1373f9
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/d3b93d7d29010d7706c748929e3a1086831830f0...1b3f83d95418762bf610a3af5b2555dd5b1373f9
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180708/c739565f/attachment.html>
More information about the vc
mailing list