From 7df6be802582cba8bd777ed90996662176bdeb12 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Mon, 28 Jan 2013 17:20:09 +0100 Subject: [PATCH] mtrand: remove it and replace with simpler, generic code Where not necessary: - rand() Where necessary (TODO): - use Linux /dev/urandom Signed-off-by: Daniel Borkmann --- astraceroute.c | 26 ++++----- astraceroute/Makefile | 1 - ct_client.c | 11 +--- ct_server.c | 4 +- curvetun/Makefile | 1 - mtrand.c | 155 -------------------------------------------------- mtrand.h | 23 -------- xio.h | 5 ++ 8 files changed, 22 insertions(+), 204 deletions(-) delete mode 100644 mtrand.c delete mode 100644 mtrand.h diff --git a/astraceroute.c b/astraceroute.c index 27fd769a..d4c0be3e 100644 --- a/astraceroute.c +++ b/astraceroute.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -56,7 +57,6 @@ #include "xio.h" #include "aslookup.h" #include "xutils.h" -#include "mtrand.h" #include "ring_rx.h" #include "built_in.h" @@ -288,13 +288,13 @@ static void assemble_data(uint8_t *packet, size_t len, const char *payload) if (payload == NULL) { for (i = 0; i < len; ++i) - packet[i] = (uint8_t) mt_rand_int32(); + packet[i] = (uint8_t) rand(); } else { int lmin = min(len, strlen(payload)); for (i = 0; i < lmin; ++i) packet[i] = (uint8_t) payload[i]; for (i = lmin; i < len; ++i) - packet[i] = (uint8_t) mt_rand_int32(); + packet[i] = (uint8_t) rand(); } } @@ -327,10 +327,10 @@ static void assemble_tcp(uint8_t *packet, size_t len, int syn, int ack, bug_on(len < sizeof(struct tcphdr)); - tcph->source = htons((uint16_t) mt_rand_int32()); + tcph->source = htons((uint16_t) rand()); tcph->dest = htons((uint16_t) dport); - tcph->seq = htonl(mt_rand_int32()); - tcph->ack_seq = (!!ack ? htonl(mt_rand_int32()) : 0); + tcph->seq = htonl(rand()); + tcph->ack_seq = (!!ack ? htonl(rand()) : 0); tcph->doff = 5; tcph->syn = !!syn; tcph->ack = !!ack; @@ -340,9 +340,9 @@ static void assemble_tcp(uint8_t *packet, size_t len, int syn, int ack, tcph->psh = !!psh; tcph->ece = !!ecn; tcph->cwr = !!ecn; - tcph->window = htons((uint16_t) (100 + (mt_rand_int32() % 65435))); + tcph->window = htons((uint16_t) (100 + (rand() % 65435))); tcph->check = 0; - tcph->urg_ptr = (!!urg ? htons((uint16_t) mt_rand_int32()) : 0); + tcph->urg_ptr = (!!urg ? htons((uint16_t) rand()) : 0); } static int assemble_ipv4_tcp(uint8_t *packet, size_t len, int ttl, @@ -363,7 +363,7 @@ static int assemble_ipv4_tcp(uint8_t *packet, size_t len, int ttl, iph->version = 4; iph->tos = (uint8_t) tos; iph->tot_len = htons((uint16_t) len); - iph->id = htons((uint16_t) mt_rand_int32()); + iph->id = htons((uint16_t) rand()); iph->frag_off = nofrag ? IP_DF : 0; iph->ttl = (uint8_t) ttl; iph->protocol = 6; /* TCP */ @@ -397,7 +397,7 @@ static int assemble_ipv6_tcp(uint8_t *packet, size_t len, int ttl, bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6); bug_on(len < sizeof(*ip6h) + sizeof(struct tcphdr)); - ip6h->ip6_flow = htonl(mt_rand_int32() & 0x000fffff); + ip6h->ip6_flow = htonl(rand() & 0x000fffff); ip6h->ip6_vfc = 0x60; ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h)); ip6h->ip6_nxt = 6; /* TCP */ @@ -431,7 +431,7 @@ static int assemble_ipv6_icmp6(uint8_t *packet, size_t len, int ttl, bug_on(src->sa_family != PF_INET6 || dst->sa_family != PF_INET6); bug_on(len < sizeof(*ip6h) + sizeof(struct icmp6hdr)); - ip6h->ip6_flow = htonl(mt_rand_int32() & 0x000fffff); + ip6h->ip6_flow = htonl(rand() & 0x000fffff); ip6h->ip6_vfc = 0x60; ip6h->ip6_plen = htons((uint16_t) len - sizeof(*ip6h)); ip6h->ip6_nxt = 0x3a; /* ICMP6 */ @@ -469,7 +469,7 @@ static int assemble_ipv4_icmp4(uint8_t *packet, size_t len, int ttl, iph->version = 4; iph->tos = 0; iph->tot_len = htons((uint16_t) len); - iph->id = htons((uint16_t) mt_rand_int32()); + iph->id = htons((uint16_t) rand()); iph->frag_off = nofrag ? IP_DF : 0; iph->ttl = (uint8_t) ttl; iph->protocol = 1; /* ICMP4 */ @@ -731,7 +731,7 @@ static int do_trace(const struct ash_cfg *cfg) struct ring dummy_ring; struct pollfd pfd; - mt_init_by_random_device(); + srand(time(NULL)); memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; diff --git a/astraceroute/Makefile b/astraceroute/Makefile index 9c849a3f..cb30183c 100644 --- a/astraceroute/Makefile +++ b/astraceroute/Makefile @@ -8,6 +8,5 @@ astraceroute-objs = xmalloc.o \ tprintf.o \ aslookup.o \ bpf.o \ - mtrand.o \ ring_rx.o \ astraceroute.o diff --git a/ct_client.c b/ct_client.c index 178b6558..93f67afa 100644 --- a/ct_client.c +++ b/ct_client.c @@ -31,7 +31,6 @@ #include "xio.h" #include "xutils.h" #include "curve.h" -#include "mtrand.h" #include "xmalloc.h" #include "curvetun.h" #include "ct_servmgmt.h" @@ -226,8 +225,6 @@ static void notify_init(int fd, int udp, struct curve25519_proto *p, char username[256], path[PATH_MAX], *us, *cbuff, *msg; unsigned char auth[crypto_auth_hmacsha512256_BYTES], *token; - mt_init_by_random_device(); - memset(&hdr, 0, sizeof(hdr)); hdr.flags |= PROTO_FLAG_INIT; @@ -264,7 +261,7 @@ static void notify_init(int fd, int udp, struct curve25519_proto *p, if (unlikely(err)) syslog_panic("Cannot create init hmac message!\n"); - pad = mt_rand_int32() % 200; + pad = secrand() % 200; msg_len = clen + sizeof(auth) + pad; msg = xzmalloc(msg_len); @@ -272,7 +269,7 @@ static void notify_init(int fd, int udp, struct curve25519_proto *p, memcpy(msg + sizeof(auth), cbuff, clen); for (i = sizeof(auth) + clen; i < msg_len; ++i) - msg[i] = (uint8_t) mt_rand_int32(); + msg[i] = (uint8_t) secrand(); hdr.payload = htons((uint16_t) msg_len); @@ -318,9 +315,7 @@ retry: c = xmalloc(sizeof(struct curve25519_struct)); - ret = curve25519_alloc_or_maybe_die(c); - if (ret < 0) - syslog_panic("Cannot init curve!\n"); + curve25519_alloc_or_maybe_die(c); p = get_serv_store_entry_proto_inf(); if (!p) diff --git a/ct_server.c b/ct_server.c index 5e2f2d11..76aa5433 100644 --- a/ct_server.c +++ b/ct_server.c @@ -452,9 +452,7 @@ static void *worker(void *self) fds.fd = ws->efd[0]; fds.events = POLLIN; - ret = curve25519_alloc_or_maybe_die(ws->c); - if (ret < 0) - syslog_panic("Cannot init curve25519!\n"); + curve25519_alloc_or_maybe_die(ws->c); buff = xmalloc_aligned(blen, 64); diff --git a/curvetun/Makefile b/curvetun/Makefile index 054abf2d..76838152 100644 --- a/curvetun/Makefile +++ b/curvetun/Makefile @@ -5,7 +5,6 @@ curvetun-objs = xmalloc.o \ xio.o \ xutils.o \ stun.o \ - mtrand.o \ patricia.o \ trie.o \ hash.o \ diff --git a/mtrand.c b/mtrand.c deleted file mode 100644 index 3ac659e1..00000000 --- a/mtrand.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * netsniff-ng - the packet sniffing beast - * By Daniel Borkmann - * Copyright 2009, 2010 Daniel Borkmann. - * Copyright (C) 1997-2004, Makoto Matsumoto, Takuji Nishimura, and - * Eric Landry; All rights reserved. (3-clause BSD license) - * Daniel Borkmann: Refactored, added initialization functions. - * Subject to the GPL, version 2. - * Reference: M. Matsumoto and T. Nishimura, "Mersenne Twister: - * A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number - * Generator", ACM Transactions on Modeling and Computer Simulation, - * Vol. 8, No. 1, January 1998, pp 3--30. - */ - -#include -#include -#include -#include -#include - -#include "mtrand.h" -#include "xio.h" - -#define N 624 -#define M 397 -#define LEN_INIT 256 - -#define MATRIX_A 0x9908b0dfUL -#define UPPER_MASK 0x80000000UL -#define LOWER_MASK 0x7fffffffUL - -static unsigned long x[N]; -static unsigned long *p0, *p1, *pm; - -void mt_init_by_seed_rand(unsigned long s) -{ - int i; - x[0] = s & 0xffffffffUL; - for (i = 1; i < N; ++i) { - x[i] = (1812433253UL * (x[i - 1] ^ (x[i - 1] >> 30)) + i) & - 0xffffffffUL; - } - p0 = x; - p1 = x + 1; - pm = x + M; -} - -void mt_init_by_seed_time(void) -{ - mt_init_by_seed_rand((unsigned long) time(NULL)); -} - -void mt_init_by_seed_array(unsigned long key[], int len) -{ - int i, j, k; - mt_init_by_seed_rand(19650218UL); - i = 1; - j = 0; - for (k = (N > len ? N : len); k; --k) { - /* Non linear */ - x[i] = ((x[i] ^ ((x[i - 1] ^ (x[i - 1] >> 30)) * - 1664525UL)) + key[j] + j) & 0xffffffffUL; - if (++i >= N) { - x[0] = x[N - 1]; - i = 1; - } - if (++j >= len) - j = 0; - } - for (k = N - 1; k; --k) { - /* Non linear */ - x[i] = ((x[i] ^ ((x[i - 1] ^ (x[i - 1] >> 30)) * - 1566083941UL)) - i) & 0xffffffffUL; - if (++i >= N) { - x[0] = x[N - 1]; - i = 1; - } - } - x[0] = 0x80000000UL; -} - -void mt_init_by_seed_rand_array(void) -{ - int i; - unsigned long k[LEN_INIT]; - srand((unsigned int) time(NULL)); - for (i = 0; i < LEN_INIT; i++) - k[i] = rand(); - mt_init_by_seed_array(k, LEN_INIT); -} - -void mt_init_by_random_device(void) -{ - int fd; - unsigned long k[LEN_INIT]; - fd = open_or_die("/dev/random", O_RDONLY); - read_or_die(fd, k, sizeof(unsigned long) * LEN_INIT); - close(fd); - mt_init_by_seed_array(k, LEN_INIT); -} - -unsigned long mt_rand_int32(void) -{ - /* Interval [0,0xffffffff] */ - unsigned long y; - /* Default seed */ - if (p0 == NULL) - mt_init_by_seed_rand(5489UL); - /* Twisted feedback */ - y = *p0 = *pm++ ^ (((*p0 & UPPER_MASK) | (*p1 & LOWER_MASK)) >> 1) ^ - (-(*p1 & 1) & MATRIX_A); - p0 = p1++; - if (pm == x + N) - pm = x; - if (p1 == x + N) - p1 = x; - /* Temper */ - y ^= y >> 11; - y ^= y << 7 & 0x9d2c5680UL; - y ^= y << 15 & 0xefc60000UL; - y ^= y >> 18; - return y; -} - -long mt_rand_int31(void) -{ - /* Interval [0,0x7fffffff] */ - return (long) mt_rand_int32() >> 1; -} - -double mt_rand_real1(void) -{ - /* Interval [0,1]; Divided by 2^32-1 */ - return mt_rand_int32() * (1.0 / 4294967295.0); -} - -double mt_rand_real2(void) -{ - /* Interval [0,1); Divided by 2^32 */ - return mt_rand_int32() * (1.0 / 4294967296.0); -} - -double mt_rand_real3(void) -{ - /* Interval (0,1); Divided by 2^32 */ - return (((double) mt_rand_int32()) + 0.5) * (1.0 / 4294967296.0); -} - -double mt_rand_res53(void) -{ - /* 53-bit random number on the real interval [0,1) */ - unsigned long a = mt_rand_int32() >> 5, b = mt_rand_int32() >> 6; - return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); -} - diff --git a/mtrand.h b/mtrand.h deleted file mode 100644 index 7b34c1e8..00000000 --- a/mtrand.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * netsniff-ng - the packet sniffing beast - * By Daniel Borkmann - * Copyright 2009, 2010 Daniel Borkmann. - * Subject to the GPL, version 2. - */ - -#ifndef MTRAND_H -#define MTRAND_H - -extern void mt_init_by_seed_rand(unsigned long s); -extern void mt_init_by_seed_time(void); -extern void mt_init_by_seed_array(unsigned long key[], int len); -extern void mt_init_by_seed_rand_array(void); -extern void mt_init_by_random_device(void); -extern unsigned long mt_rand_int32(void); -extern long mt_rand_int31(void); -extern double mt_rand_real1(void); -extern double mt_rand_real2(void); -extern double mt_rand_real3(void); -extern double mt_rand_res53(void); - -#endif /* MTRAND_H */ diff --git a/xio.h b/xio.h index d06587c4..cdda6b88 100644 --- a/xio.h +++ b/xio.h @@ -18,4 +18,9 @@ extern ssize_t write_or_die(int fd, const void *buf, size_t count); extern ssize_t read_exact(int fd, void *buf, size_t len, int mayexit); extern ssize_t write_exact(int fd, void *buf, size_t len, int mayexit); +static inline int secrand(void) +{ + return rand(); +} + #endif /* XIO_H */ -- 2.11.4.GIT