From c4160ed41717260b5941e2729c444b8ec051d5f0 Mon Sep 17 00:00:00 2001 From: David Timber Date: Thu, 10 Sep 2020 13:17:25 +0930 Subject: * Impl: rnd interface and WELL512 * cncp: query cnc txt rec after wait not before * In case the child keeps on dying --- src/proone-rnd.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/proone-rnd.c (limited to 'src/proone-rnd.c') 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 +#include + +#include "rnd.h" +#include "util_rt.h" + +#include +#include + + +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 \n", args[0]); + ret = 2; + goto END; + } + if (sscanf(args[1], "%"SCNu32, &max) != 1 || max == 0) { + fprintf(stderr, "Invalid \n"); + ret = 2; + goto END; + } + if (sscanf(args[2], "%"SCNu32, &cnt) != 1) { + fprintf(stderr, "Invalid \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; +} -- cgit