Home | History | Annotate | Line # | Download | only in rpc
__rpc_getxid.c revision 1.1
      1 /*	$NetBSD: __rpc_getxid.c,v 1.1 2003/09/09 03:56:23 itojun Exp $	*/
      2 /*	$OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $	*/
      3 
      4 /*
      5  * Copyright 1998 Niels Provos <provos (at) citi.umich.edu>
      6  * All rights reserved.
      7  *
      8  * Theo de Raadt <deraadt (at) openbsd.org> came up with the idea of using
      9  * such a mathematical system to generate more random (yet non-repeating)
     10  * ids to solve the resolver/named problem.  But Niels designed the
     11  * actual system based on the constraints.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *      This product includes software developed by Niels Provos.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * seed = random 31bit
     41  * n = prime, g0 = generator to n,
     42  * j = random so that gcd(j,n-1) == 1
     43  * g = g0^j mod n will be a generator again.
     44  *
     45  * X[0] = random seed.
     46  * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
     47  * with a = 7^(even random) mod m,
     48  *      b = random with gcd(b,m) == 1
     49  *      m = 1836660096 and a maximal period of m-1.
     50  *
     51  * The transaction id is determined by:
     52  * id[n] = seed xor (g^X[n] mod n)
     53  *
     54  * Effectivly the id is restricted to the lower 31 bits, thus
     55  * yielding two different cycles by toggling the msb on and off.
     56  * This avoids reuse issues caused by reseeding.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 #if defined(LIBC_SCCS) && !defined(lint)
     61 __RCSID("$NetBSD: __rpc_getxid.c,v 1.1 2003/09/09 03:56:23 itojun Exp $");
     62 #endif
     63 
     64 #include <sys/types.h>
     65 #include <sys/time.h>
     66 #include <stdlib.h>
     67 #include <rpc/rpc.h>
     68 #include "rpc_internal.h"
     69 
     70 #define RU_OUT  180		/* Time after wich will be reseeded */
     71 #define RU_MAX	1000000000	/* Uniq cycle, avoid blackjack prediction */
     72 #define RU_GEN	2		/* Starting generator */
     73 #define RU_N	2147483629	/* RU_N-1 = 2^2*3^2*59652323 */
     74 #define RU_AGEN	7		/* determine ru_a as RU_AGEN^(2*rand) */
     75 #define RU_M	1836660096	/* RU_M = 2^7*3^15 - don't change */
     76 
     77 #define PFAC_N 3
     78 const static u_int32_t pfacts[PFAC_N] = {
     79 	2,
     80 	3,
     81 	59652323
     82 };
     83 
     84 static u_int32_t ru_x;
     85 static u_int32_t ru_seed, ru_seed2;
     86 static u_int32_t ru_a, ru_b;
     87 static u_int32_t ru_g;
     88 static u_int32_t ru_counter = 0;
     89 static u_int32_t ru_msb = 0;
     90 static long ru_reseed;
     91 
     92 static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
     93 static void initid(void);
     94 
     95 /*
     96  * Do a fast modular exponation, returned value will be in the range
     97  * of 0 - (mod-1)
     98  */
     99 static u_int32_t
    100 pmod(u_int32_t gen, u_int32_t exp, u_int32_t mod)
    101 {
    102 	u_int64_t s, t, u;
    103 
    104 	s = 1;
    105 	t = gen;
    106 	u = exp;
    107 
    108 	while (u) {
    109 		if (u & 1)
    110 			s = (s * t) % mod;
    111 		u >>= 1;
    112 		t = (t * t) % mod;
    113 	}
    114 	return ((u_int32_t)s & 0xffffffff);
    115 }
    116 
    117 /*
    118  * Initalizes the seed and chooses a suitable generator. Also toggles
    119  * the msb flag. The msb flag is used to generate two distinct
    120  * cycles of random numbers and thus avoiding reuse of ids.
    121  *
    122  * This function is called from id_randomid() when needed, an
    123  * application does not have to worry about it.
    124  */
    125 static void
    126 initid(void)
    127 {
    128 	u_int32_t j, i;
    129 	int noprime = 1;
    130 	struct timeval tv;
    131 
    132 	ru_x = arc4random() % RU_M;
    133 
    134 	/* 31 bits of random seed */
    135 	ru_seed = arc4random() & INT32_MAX;
    136 	ru_seed2 = arc4random() & INT32_MAX;
    137 
    138 	/* Determine the LCG we use */
    139 	ru_b = arc4random() | 1;
    140 	ru_a = pmod(RU_AGEN, arc4random() & (~1U), RU_M);
    141 	while (ru_b % 3 == 0)
    142 		ru_b += 2;
    143 
    144 	j = arc4random() % RU_N;
    145 
    146 	/*
    147 	 * Do a fast gcd(j,RU_N-1), so we can find a j with
    148 	 * gcd(j, RU_N-1) == 1, giving a new generator for
    149 	 * RU_GEN^j mod RU_N
    150 	 */
    151 	while (noprime) {
    152 		for (i = 0; i < PFAC_N; i++)
    153 			if (j % pfacts[i] == 0)
    154 				break;
    155 
    156 		if (i >= PFAC_N)
    157 			noprime = 0;
    158 		else
    159 			j = (j + 1) % RU_N;
    160 	}
    161 
    162 	ru_g = pmod(RU_GEN, j, RU_N);
    163 	ru_counter = 0;
    164 
    165 	gettimeofday(&tv, NULL);
    166 	ru_reseed = tv.tv_sec + RU_OUT;
    167 	ru_msb = ru_msb ? 0 : 0x80000000;
    168 }
    169 
    170 u_int32_t
    171 __rpc_getxid(void)
    172 {
    173 	int i, n;
    174 	u_int32_t tmp;
    175 	struct timeval tv;
    176 
    177 	gettimeofday(&tv, NULL);
    178 	if (ru_counter >= RU_MAX || tv.tv_sec > ru_reseed)
    179 		initid();
    180 
    181 	tmp = arc4random();
    182 
    183 	/* Skip a random number of ids */
    184 	n = tmp & 0x3; tmp = tmp >> 2;
    185 	if (ru_counter + n >= RU_MAX)
    186 		initid();
    187 
    188 	for (i = 0; i <= n; i++) {
    189 		/* Linear Congruential Generator */
    190 		ru_x = (ru_a * ru_x + ru_b) % RU_M;
    191 	}
    192 
    193 	ru_counter += i;
    194 
    195 	return (ru_seed ^ pmod(ru_g, ru_seed2 ^ ru_x,RU_N)) | ru_msb;
    196 }
    197