Commit 6032d722 authored by Marek Vavruša's avatar Marek Vavruša

lib/utils: moved isaac cspring here

parent e1e4f8b0
......@@ -19,7 +19,6 @@
#include <libknot/descriptor.h>
#include <libknot/rrtype/rdname.h>
#include "ccan/isaac/isaac.h"
#include "lib/layer/iterate.h"
#include "lib/resolve.h"
#include "lib/rplan.h"
......@@ -27,24 +26,11 @@
#include "lib/nsrep.h"
#include "lib/module.h"
#define SEED_SIZE 256
#define DEBUG_MSG(fmt...) QRDEBUG(kr_rplan_current(&req->rplan), "iter", fmt)
/* Iterator often walks through packet section, this is an abstraction. */
typedef int (*rr_callback_t)(const knot_rrset_t *, unsigned, struct kr_request *);
/** @internal CSPRNG context */
static isaac_ctx ISAAC;
/** @internal Reseed isaac context. */
int iterate_init(struct kr_module *module)
{
uint8_t seed[SEED_SIZE];
kr_randseed((char *)seed, sizeof(seed));
isaac_reseed(&ISAAC, seed, sizeof(seed));
return kr_ok();
}
/** Return minimized QNAME/QTYPE for current zone cut. */
static const knot_dname_t *minimized_qname(struct kr_query *query, uint16_t *qtype)
{
......@@ -368,7 +354,7 @@ int kr_make_query(struct kr_query *query, knot_pkt_t *pkt)
}
/* Query built, expect answer. */
query->id = isaac_next_uint(&ISAAC, UINT16_MAX);
query->id = kr_rand_uint(UINT16_MAX);
knot_wire_set_id(pkt->wire, query->id);
pkt->parsed = pkt->size;
return kr_ok();
......
......@@ -124,7 +124,7 @@ static int eval_nsrep(const char *k, void *v, void *baton)
ns->reputation = reputation;
} else {
/* With 5% chance, probe server with a probability given by its RTT / MAX_RTT */
unsigned roll = rand() % KR_NS_MAX_SCORE;
unsigned roll = kr_rand_uint(KR_NS_MAX_SCORE);
if ((roll % 100 < 5) && (roll >= score)) {
update_nsrep(ns, (const knot_dname_t *)k, addr, score);
ns->reputation = reputation;
......
......@@ -21,10 +21,16 @@
#include <stdio.h>
#include <sys/time.h>
#include "ccan/isaac/isaac.h"
#include "lib/defines.h"
#include "lib/utils.h"
#include "lib/generic/array.h"
/** @internal CSPRNG context */
static isaac_ctx ISAAC;
static bool isaac_seeded = false;
#define SEED_SIZE 256
/*
* Macros.
*/
......@@ -95,7 +101,7 @@ static int seed_file(FILE *fp, char *buf, size_t buflen)
return 0;
}
int kr_randseed(char *buf, size_t buflen)
static int randseed(char *buf, size_t buflen)
{
/* This is adapted from Tor's crypto_seed_rng() */
static const char *filenames[] = {
......@@ -115,6 +121,23 @@ int kr_randseed(char *buf, size_t buflen)
return 0;
}
int kr_rand_reseed(void)
{
uint8_t seed[SEED_SIZE];
randseed((char *)seed, sizeof(seed));
isaac_reseed(&ISAAC, seed, sizeof(seed));
return kr_ok();
}
unsigned kr_rand_uint(unsigned max)
{
if (!isaac_seeded) {
kr_rand_reseed();
isaac_seeded = true;
}
return isaac_next_uint(&ISAAC, max);
}
int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have)
{
if (*have >= want) {
......
......@@ -37,8 +37,11 @@ extern void _cleanup_fclose(FILE **p);
/** Concatenate N strings. */
char* kr_strcatdup(unsigned n, ...);
/** Fill buffer with random seed. */
int kr_randseed(char *buf, size_t buflen);
/** Reseed CSPRNG context. */
int kr_rand_reseed(void);
/** Get pseudo-random value. */
unsigned kr_rand_uint(unsigned max);
/** Memory reservation routine for mm_ctx_t */
int mm_reserve(void *baton, char **mem, size_t elm_size, size_t want, size_t *have);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment