📄 weakprng.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/common/lib/weakprng.c,v 1.3 2003/01/16 18:18:58 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: weakprng.c,v $ * Revision 1.3 2003/01/16 18:18:58 josh * directory structure shifting * * Revision 1.2 2001/11/06 22:15:56 tneale * Fixed for newest file layout * * Revision 1.1.1.1 2001/11/05 17:48:40 tneale * Tornado shuffle * * Revision 1.9 2001/01/19 22:21:33 paul * Update copyright. * * Revision 1.8 2000/03/17 00:17:00 meister * Update copyright message * * Revision 1.7 1998/03/06 20:30:45 sra * Fiddle seeding algorithm per Phil's recommendation. * * Revision 1.6 1998/02/25 22:08:28 mrf * Correct copyright dates. * * Revision 1.5 1998/02/25 15:17:07 mrf * Fix msvc5 compiler warnings. * * Revision 1.4 1998/02/25 04:43:28 sra * Update copyrights. * * Revision 1.3 1998/02/21 23:38:34 sra * Minor changes to WeakPRNG module. * * Revision 1.1 1998/02/21 22:05:36 mrf * Added weak random number generator. * *//* [clearcase]modification history-------------------01b,20apr05,job update copyright notices01a,11dec03,job fix copyright statements*/#ifndef EPILOGUE_INSTALL_H#include <wrn/wm/common/install.h>#endif#ifndef COMMON_CONFIG_H#include <wrn/wm/common/config.h>#endif#ifndef COMMON_GLUE_H#include <wrn/wm/common/glue.h>#endif#ifndef COMMON_WEAKPRNG_H#include <wrn/wm/common/weakprng.h>#endif/* * This file implements a cryptographically WEAK random number generator * suitable only for dithering protocol responses to avoid collisions or * other situations where the purpose of the "random" number is to avoid * simultaneity or repeated use of the same values. For any cryptographic * purposes, the cryptographically strong random number generator (found * in common/lib/prng.c) should be used instead. * * This weak generator is useful for dithering for those protocols which * require it (such as DHCP, RIP, IGMP, Router Discovery and IPv6 Neighbor * Discovery). It has relatively low processing overhead (compared to the * strong random number generator), and it is seeded automatically by * Attache. * * It is the purpose of this weak random number generator, not to produce * truly random numbers, but to produce numbers which are likely to be * different for different hosts on a given link. In order to obtain * this result, it is important that different hosts use different seeds. * Attache seeds (and re-seeds) the generator in several places with values * that are likely to be unique for this host. Seeds include the GLUE_NOW() * value at the end of the attache_init() call, the hardware address or * IPv6 address token provided in the net_if structure during each if_attach() * call, if any, and the IP addresses provided to route_add_address() each * time it is called. Successive seeds do not reset the generator. If a * customer has additional seed information that may help to improve the * uniqueness of the generated values for a particular host (e.g. a box * serial number), he can also input those seeds using the seed call below. */static bits32_t ep_weak_prng_value;/* ep_seed_weak_prng -- * Seeds the weak random number generator with (hopefully unique * for this host) seed data. Data is provided as a buffer of * unsigned bytes and a length (in bytes). We run the generator * as part of the seeding process to reduce the chance of losing * this seed if the generator is seeded again before it is used. */voidep_seed_weak_prng(bits8_t *seed_string, size_t len){ int i; for (i = 0; i < (int) len; i++) { ep_weak_prng_value ^= ((bits32_t) (seed_string[i])) << (8 * (i & 3)); if ((i & 3) == 3) ep_weak_prng(0xFFFFFFFFL); } ep_weak_prng(0xFFFFFFFFL);}/* ep_weak_prng -- * Returns a "random" 32_bit integer less than or equal to 'max'. * * The algorithm is a linear congruential random number generator, * equivilent to the ANSI C rand() function but with constants that * provide better results than those used in the BSD rand() function. * * These constants are from Knuth, chap 3, pp 102 & 170. Don't change * them unless you really understand the underlying math. */bits32_tep_weak_prng(bits32_t max){ ep_weak_prng_value = (ep_weak_prng_value + 1) * 16664525L; return ++max ? ep_weak_prng_value % max : ep_weak_prng_value;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -