OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [tcpip/] [v2_0/] [src/] [sys/] [netinet/] [ip_id.c] - Blame information for rev 219

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.