mirror of
https://git.rtems.org/rtems-libbsd/
synced 2025-10-14 07:59:07 +08:00
Move files to match FreeBSD layout
This commit is contained in:
155
freebsd/sys/libkern/arc4random.c
Normal file
155
freebsd/sys/libkern/arc4random.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include <freebsd/machine/rtems-bsd-config.h>
|
||||
|
||||
/*-
|
||||
* THE BEER-WARE LICENSE
|
||||
*
|
||||
* <dan@FreeBSD.ORG> wrote this file. As long as you retain this notice you
|
||||
* can do whatever you want with this stuff. If we meet some day, and you
|
||||
* think this stuff is worth it, you can buy me a beer in return.
|
||||
*
|
||||
* Dan Moschuk
|
||||
*/
|
||||
|
||||
#include <freebsd/sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <freebsd/sys/types.h>
|
||||
#include <freebsd/sys/param.h>
|
||||
#include <freebsd/sys/kernel.h>
|
||||
#include <freebsd/sys/random.h>
|
||||
#include <freebsd/sys/libkern.h>
|
||||
#include <freebsd/sys/lock.h>
|
||||
#include <freebsd/sys/mutex.h>
|
||||
#include <freebsd/sys/time.h>
|
||||
|
||||
#define ARC4_RESEED_BYTES 65536
|
||||
#define ARC4_RESEED_SECONDS 300
|
||||
#define ARC4_KEYBYTES (256 / 8)
|
||||
|
||||
static u_int8_t arc4_i, arc4_j;
|
||||
static int arc4_numruns = 0;
|
||||
static u_int8_t arc4_sbox[256];
|
||||
static time_t arc4_t_reseed;
|
||||
static struct mtx arc4_mtx;
|
||||
|
||||
static u_int8_t arc4_randbyte(void);
|
||||
|
||||
static __inline void
|
||||
arc4_swap(u_int8_t *a, u_int8_t *b)
|
||||
{
|
||||
u_int8_t c;
|
||||
|
||||
c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stir our S-box.
|
||||
*/
|
||||
static void
|
||||
arc4_randomstir (void)
|
||||
{
|
||||
u_int8_t key[256];
|
||||
int r, n;
|
||||
struct timeval tv_now;
|
||||
|
||||
/*
|
||||
* XXX read_random() returns unsafe numbers if the entropy
|
||||
* device is not loaded -- MarkM.
|
||||
*/
|
||||
r = read_random(key, ARC4_KEYBYTES);
|
||||
getmicrouptime(&tv_now);
|
||||
mtx_lock(&arc4_mtx);
|
||||
/* If r == 0 || -1, just use what was on the stack. */
|
||||
if (r > 0) {
|
||||
for (n = r; n < sizeof(key); n++)
|
||||
key[n] = key[n % r];
|
||||
}
|
||||
|
||||
for (n = 0; n < 256; n++) {
|
||||
arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256;
|
||||
arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]);
|
||||
}
|
||||
arc4_i = arc4_j = 0;
|
||||
|
||||
/* Reset for next reseed cycle. */
|
||||
arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS;
|
||||
arc4_numruns = 0;
|
||||
|
||||
/*
|
||||
* Throw away the first N words of output, as suggested in the
|
||||
* paper "Weaknesses in the Key Scheduling Algorithm of RC4"
|
||||
* by Fluher, Mantin, and Shamir. (N = 256 in our case.)
|
||||
*/
|
||||
for (n = 0; n < 256*4; n++)
|
||||
arc4_randbyte();
|
||||
mtx_unlock(&arc4_mtx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize our S-box to its beginning defaults.
|
||||
*/
|
||||
static void
|
||||
arc4_init(void)
|
||||
{
|
||||
int n;
|
||||
|
||||
mtx_init(&arc4_mtx, "arc4_mtx", NULL, MTX_DEF);
|
||||
arc4_i = arc4_j = 0;
|
||||
for (n = 0; n < 256; n++)
|
||||
arc4_sbox[n] = (u_int8_t) n;
|
||||
|
||||
arc4_t_reseed = 0;
|
||||
}
|
||||
|
||||
SYSINIT(arc4_init, SI_SUB_LOCK, SI_ORDER_ANY, arc4_init, NULL);
|
||||
|
||||
/*
|
||||
* Generate a random byte.
|
||||
*/
|
||||
static u_int8_t
|
||||
arc4_randbyte(void)
|
||||
{
|
||||
u_int8_t arc4_t;
|
||||
|
||||
arc4_i = (arc4_i + 1) % 256;
|
||||
arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256;
|
||||
|
||||
arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]);
|
||||
|
||||
arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256;
|
||||
return arc4_sbox[arc4_t];
|
||||
}
|
||||
|
||||
/*
|
||||
* MPSAFE
|
||||
*/
|
||||
void
|
||||
arc4rand(void *ptr, u_int len, int reseed)
|
||||
{
|
||||
u_char *p;
|
||||
struct timeval tv;
|
||||
|
||||
getmicrouptime(&tv);
|
||||
if (reseed ||
|
||||
(arc4_numruns > ARC4_RESEED_BYTES) ||
|
||||
(tv.tv_sec > arc4_t_reseed))
|
||||
arc4_randomstir();
|
||||
|
||||
mtx_lock(&arc4_mtx);
|
||||
arc4_numruns += len;
|
||||
p = ptr;
|
||||
while (len--)
|
||||
*p++ = arc4_randbyte();
|
||||
mtx_unlock(&arc4_mtx);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
arc4random(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
arc4rand(&ret, sizeof ret, 0);
|
||||
return ret;
|
||||
}
|
50
freebsd/sys/libkern/fls.c
Normal file
50
freebsd/sys/libkern/fls.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <freebsd/machine/rtems-bsd-config.h>
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <freebsd/sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <freebsd/sys/libkern.h>
|
||||
|
||||
/*
|
||||
* Find Last Set bit
|
||||
*/
|
||||
int
|
||||
fls(int mask)
|
||||
{
|
||||
int bit;
|
||||
|
||||
if (mask == 0)
|
||||
return (0);
|
||||
for (bit = 1; mask != 1; bit++)
|
||||
mask = (unsigned int)mask >> 1;
|
||||
return (bit);
|
||||
}
|
67
freebsd/sys/libkern/inet_ntoa.c
Normal file
67
freebsd/sys/libkern/inet_ntoa.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <freebsd/machine/rtems-bsd-config.h>
|
||||
|
||||
/*-
|
||||
* Copyright 1994, 1995 Massachusetts Institute of Technology
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software and
|
||||
* its documentation for any purpose and without fee is hereby
|
||||
* granted, provided that both the above copyright notice and this
|
||||
* permission notice appear in all copies, that both the above
|
||||
* copyright notice and this permission notice appear in all
|
||||
* supporting documentation, and that the name of M.I.T. not be used
|
||||
* in advertising or publicity pertaining to distribution of the
|
||||
* software without specific, written prior permission. M.I.T. makes
|
||||
* no representations about the suitability of this software for any
|
||||
* purpose. It is provided "as is" without express or implied
|
||||
* warranty.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
|
||||
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
|
||||
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <freebsd/sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <freebsd/sys/param.h>
|
||||
#include <freebsd/sys/systm.h>
|
||||
|
||||
#include <freebsd/netinet/in.h>
|
||||
|
||||
char *
|
||||
inet_ntoa(struct in_addr ina)
|
||||
{
|
||||
static char buf[4*sizeof "123"];
|
||||
unsigned char *ucp = (unsigned char *)&ina;
|
||||
|
||||
sprintf(buf, "%d.%d.%d.%d",
|
||||
ucp[0] & 0xff,
|
||||
ucp[1] & 0xff,
|
||||
ucp[2] & 0xff,
|
||||
ucp[3] & 0xff);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *
|
||||
inet_ntoa_r(struct in_addr ina, char *buf)
|
||||
{
|
||||
unsigned char *ucp = (unsigned char *)&ina;
|
||||
|
||||
sprintf(buf, "%d.%d.%d.%d",
|
||||
ucp[0] & 0xff,
|
||||
ucp[1] & 0xff,
|
||||
ucp[2] & 0xff,
|
||||
ucp[3] & 0xff);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
80
freebsd/sys/libkern/random.c
Normal file
80
freebsd/sys/libkern/random.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <freebsd/machine/rtems-bsd-config.h>
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)random.c 8.1 (Berkeley) 6/10/93
|
||||
*/
|
||||
|
||||
#include <freebsd/sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <freebsd/sys/libkern.h>
|
||||
|
||||
#define NSHUFF 50 /* to drop some "seed -> 1st value" linearity */
|
||||
|
||||
static u_long randseed = 937186357; /* after srandom(1), NSHUFF counted */
|
||||
|
||||
void
|
||||
srandom(seed)
|
||||
u_long seed;
|
||||
{
|
||||
int i;
|
||||
|
||||
randseed = seed;
|
||||
for (i = 0; i < NSHUFF; i++)
|
||||
(void)random();
|
||||
}
|
||||
|
||||
/*
|
||||
* Pseudo-random number generator for randomizing the profiling clock,
|
||||
* and whatever else we might use it for. The result is uniform on
|
||||
* [0, 2^31 - 1].
|
||||
*/
|
||||
u_long
|
||||
random()
|
||||
{
|
||||
register long x, hi, lo, t;
|
||||
|
||||
/*
|
||||
* Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
|
||||
* From "Random number generators: good ones are hard to find",
|
||||
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
|
||||
* October 1988, p. 1195.
|
||||
*/
|
||||
/* Can't be initialized with 0, so use another value. */
|
||||
if ((x = randseed) == 0)
|
||||
x = 123459876;
|
||||
hi = x / 127773;
|
||||
lo = x % 127773;
|
||||
t = 16807 * lo - 2836 * hi;
|
||||
if (t < 0)
|
||||
t += 0x7fffffff;
|
||||
randseed = t;
|
||||
return (t);
|
||||
}
|
Reference in New Issue
Block a user