linux-old/drivers/char/random.c
<<
>>
Prefs
   1/*
   2 * random.c -- A strong random number generator
   3 *
   4 * Version 1.89, last modified 19-Sep-99
   5 * 
   6 * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999.  All
   7 * rights reserved.
   8 *
   9 * Redistribution and use in source and binary forms, with or without
  10 * modification, are permitted provided that the following conditions
  11 * are met:
  12 * 1. Redistributions of source code must retain the above copyright
  13 *    notice, and the entire permission notice in its entirety,
  14 *    including the disclaimer of warranties.
  15 * 2. Redistributions in binary form must reproduce the above copyright
  16 *    notice, this list of conditions and the following disclaimer in the
  17 *    documentation and/or other materials provided with the distribution.
  18 * 3. The name of the author may not be used to endorse or promote
  19 *    products derived from this software without specific prior
  20 *    written permission.
  21 * 
  22 * ALTERNATIVELY, this product may be distributed under the terms of
  23 * the GNU General Public License, in which case the provisions of the GPL are
  24 * required INSTEAD OF the above restrictions.  (This clause is
  25 * necessary due to a potential bad interaction between the GPL and
  26 * the restrictions contained in a BSD-style copyright.)
  27 * 
  28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  31 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
  32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  34 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  35 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  38 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  39 * DAMAGE.
  40 */
  41
  42/*
  43 * (now, with legal B.S. out of the way.....) 
  44 * 
  45 * This routine gathers environmental noise from device drivers, etc.,
  46 * and returns good random numbers, suitable for cryptographic use.
  47 * Besides the obvious cryptographic uses, these numbers are also good
  48 * for seeding TCP sequence numbers, and other places where it is
  49 * desirable to have numbers which are not only random, but hard to
  50 * predict by an attacker.
  51 *
  52 * Theory of operation
  53 * ===================
  54 * 
  55 * Computers are very predictable devices.  Hence it is extremely hard
  56 * to produce truly random numbers on a computer --- as opposed to
  57 * pseudo-random numbers, which can easily generated by using a
  58 * algorithm.  Unfortunately, it is very easy for attackers to guess
  59 * the sequence of pseudo-random number generators, and for some
  60 * applications this is not acceptable.  So instead, we must try to
  61 * gather "environmental noise" from the computer's environment, which
  62 * must be hard for outside attackers to observe, and use that to
  63 * generate random numbers.  In a Unix environment, this is best done
  64 * from inside the kernel.
  65 * 
  66 * Sources of randomness from the environment include inter-keyboard
  67 * timings, inter-interrupt timings from some interrupts, and other
  68 * events which are both (a) non-deterministic and (b) hard for an
  69 * outside observer to measure.  Randomness from these sources are
  70 * added to an "entropy pool", which is mixed using a CRC-like function.
  71 * This is not cryptographically strong, but it is adequate assuming
  72 * the randomness is not chosen maliciously, and it is fast enough that
  73 * the overhead of doing it on every interrupt is very reasonable.
  74 * As random bytes are mixed into the entropy pool, the routines keep
  75 * an *estimate* of how many bits of randomness have been stored into
  76 * the random number generator's internal state.
  77 * 
  78 * When random bytes are desired, they are obtained by taking the SHA
  79 * hash of the contents of the "entropy pool".  The SHA hash avoids
  80 * exposing the internal state of the entropy pool.  It is believed to
  81 * be computationally infeasible to derive any useful information
  82 * about the input of SHA from its output.  Even if it is possible to
  83 * analyze SHA in some clever way, as long as the amount of data
  84 * returned from the generator is less than the inherent entropy in
  85 * the pool, the output data is totally unpredictable.  For this
  86 * reason, the routine decreases its internal estimate of how many
  87 * bits of "true randomness" are contained in the entropy pool as it
  88 * outputs random numbers.
  89 * 
  90 * If this estimate goes to zero, the routine can still generate
  91 * random numbers; however, an attacker may (at least in theory) be
  92 * able to infer the future output of the generator from prior
  93 * outputs.  This requires successful cryptanalysis of SHA, which is
  94 * not believed to be feasible, but there is a remote possibility.
  95 * Nonetheless, these numbers should be useful for the vast majority
  96 * of purposes.
  97 * 
  98 * Exported interfaces ---- output
  99 * ===============================
 100 * 
 101 * There are three exported interfaces; the first is one designed to
 102 * be used from within the kernel:
 103 *
 104 *      void get_random_bytes(void *buf, int nbytes);
 105 *
 106 * This interface will return the requested number of random bytes,
 107 * and place it in the requested buffer.
 108 * 
 109 * The two other interfaces are two character devices /dev/random and
 110 * /dev/urandom.  /dev/random is suitable for use when very high
 111 * quality randomness is desired (for example, for key generation or
 112 * one-time pads), as it will only return a maximum of the number of
 113 * bits of randomness (as estimated by the random number generator)
 114 * contained in the entropy pool.
 115 * 
 116 * The /dev/urandom device does not have this limit, and will return
 117 * as many bytes as are requested.  As more and more random bytes are
 118 * requested without giving time for the entropy pool to recharge,
 119 * this will result in random numbers that are merely cryptographically
 120 * strong.  For many applications, however, this is acceptable.
 121 *
 122 * Exported interfaces ---- input
 123 * ==============================
 124 * 
 125 * The current exported interfaces for gathering environmental noise
 126 * from the devices are:
 127 * 
 128 *      void add_keyboard_randomness(unsigned char scancode);
 129 *      void add_mouse_randomness(__u32 mouse_data);
 130 *      void add_interrupt_randomness(int irq);
 131 *      void add_blkdev_randomness(int irq);
 132 * 
 133 * add_keyboard_randomness() uses the inter-keypress timing, as well as the
 134 * scancode as random inputs into the "entropy pool".
 135 * 
 136 * add_mouse_randomness() uses the mouse interrupt timing, as well as
 137 * the reported position of the mouse from the hardware.
 138 *
 139 * add_interrupt_randomness() uses the inter-interrupt timing as random
 140 * inputs to the entropy pool.  Note that not all interrupts are good
 141 * sources of randomness!  For example, the timer interrupts is not a
 142 * good choice, because the periodicity of the interrupts is too
 143 * regular, and hence predictable to an attacker.  Disk interrupts are
 144 * a better measure, since the timing of the disk interrupts are more
 145 * unpredictable.
 146 * 
 147 * add_blkdev_randomness() times the finishing time of block requests.
 148 * 
 149 * All of these routines try to estimate how many bits of randomness a
 150 * particular randomness source.  They do this by keeping track of the
 151 * first and second order deltas of the event timings.
 152 *
 153 * Ensuring unpredictability at system startup
 154 * ============================================
 155 * 
 156 * When any operating system starts up, it will go through a sequence
 157 * of actions that are fairly predictable by an adversary, especially
 158 * if the start-up does not involve interaction with a human operator.
 159 * This reduces the actual number of bits of unpredictability in the
 160 * entropy pool below the value in entropy_count.  In order to
 161 * counteract this effect, it helps to carry information in the
 162 * entropy pool across shut-downs and start-ups.  To do this, put the
 163 * following lines an appropriate script which is run during the boot
 164 * sequence: 
 165 *
 166 *      echo "Initializing random number generator..."
 167 *      random_seed=/var/run/random-seed
 168 *      # Carry a random seed from start-up to start-up
 169 *      # Load and then save the whole entropy pool
 170 *      if [ -f $random_seed ]; then
 171 *              cat $random_seed >/dev/urandom
 172 *      else
 173 *              touch $random_seed
 174 *      fi
 175 *      chmod 600 $random_seed
 176 *      poolfile=/proc/sys/kernel/random/poolsize
 177 *      [ -r $poolfile ] && bytes=`cat $poolfile` || bytes=512
 178 *      dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
 179 *
 180 * and the following lines in an appropriate script which is run as
 181 * the system is shutdown:
 182 *
 183 *      # Carry a random seed from shut-down to start-up
 184 *      # Save the whole entropy pool
 185 *      echo "Saving random seed..."
 186 *      random_seed=/var/run/random-seed
 187 *      touch $random_seed
 188 *      chmod 600 $random_seed
 189 *      poolfile=/proc/sys/kernel/random/poolsize
 190 *      [ -r $poolfile ] && bytes=`cat $poolfile` || bytes=512
 191 *      dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
 192 *
 193 * For example, on most modern systems using the System V init
 194 * scripts, such code fragments would be found in
 195 * /etc/rc.d/init.d/random.  On older Linux systems, the correct script
 196 * location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0.
 197 * 
 198 * Effectively, these commands cause the contents of the entropy pool
 199 * to be saved at shut-down time and reloaded into the entropy pool at
 200 * start-up.  (The 'dd' in the addition to the bootup script is to
 201 * make sure that /etc/random-seed is different for every start-up,
 202 * even if the system crashes without executing rc.0.)  Even with
 203 * complete knowledge of the start-up activities, predicting the state
 204 * of the entropy pool requires knowledge of the previous history of
 205 * the system.
 206 *
 207 * Configuring the /dev/random driver under Linux
 208 * ==============================================
 209 *
 210 * The /dev/random driver under Linux uses minor numbers 8 and 9 of
 211 * the /dev/mem major number (#1).  So if your system does not have
 212 * /dev/random and /dev/urandom created already, they can be created
 213 * by using the commands:
 214 *
 215 *      mknod /dev/random c 1 8
 216 *      mknod /dev/urandom c 1 9
 217 * 
 218 * Acknowledgements:
 219 * =================
 220 *
 221 * Ideas for constructing this random number generator were derived
 222 * from Pretty Good Privacy's random number generator, and from private
 223 * discussions with Phil Karn.  Colin Plumb provided a faster random
 224 * number generator, which speed up the mixing function of the entropy
 225 * pool, taken from PGPfone.  Dale Worley has also contributed many
 226 * useful ideas and suggestions to improve this driver.
 227 * 
 228 * Any flaws in the design are solely my responsibility, and should
 229 * not be attributed to the Phil, Colin, or any of authors of PGP.
 230 * 
 231 * The code for SHA transform was taken from Peter Gutmann's
 232 * implementation, which has been placed in the public domain.
 233 * The code for MD5 transform was taken from Colin Plumb's
 234 * implementation, which has been placed in the public domain.
 235 * The MD5 cryptographic checksum was devised by Ronald Rivest, and is
 236 * documented in RFC 1321, "The MD5 Message Digest Algorithm".
 237 * 
 238 * Further background information on this topic may be obtained from
 239 * RFC 1750, "Randomness Recommendations for Security", by Donald
 240 * Eastlake, Steve Crocker, and Jeff Schiller.
 241 */
 242
 243#include <linux/utsname.h>
 244#include <linux/config.h>
 245#include <linux/module.h>
 246#include <linux/kernel.h>
 247#include <linux/major.h>
 248#include <linux/string.h>
 249#include <linux/fcntl.h>
 250#include <linux/slab.h>
 251#include <linux/random.h>
 252#include <linux/poll.h>
 253#include <linux/init.h>
 254#include <linux/interrupt.h>
 255#include <linux/spinlock.h>
 256
 257#include <asm/processor.h>
 258#include <asm/uaccess.h>
 259#include <asm/irq.h>
 260#include <asm/io.h>
 261
 262/*
 263 * Configuration information
 264 */
 265#define DEFAULT_POOL_SIZE 512
 266#define SECONDARY_POOL_SIZE 128
 267#define BATCH_ENTROPY_SIZE 256
 268#define USE_SHA
 269
 270/*
 271 * The minimum number of bits of entropy before we wake up a read on
 272 * /dev/random.  Should always be at least 8, or at least 1 byte.
 273 */
 274static int random_read_wakeup_thresh = 8;
 275
 276/*
 277 * If the entropy count falls under this number of bits, then we
 278 * should wake up processes which are selecting or polling on write
 279 * access to /dev/random.
 280 */
 281static int random_write_wakeup_thresh = 128;
 282
 283/*
 284 * A pool of size .poolwords is stirred with a primitive polynomial
 285 * of degree .poolwords over GF(2).  The taps for various sizes are
 286 * defined below.  They are chosen to be evenly spaced (minimum RMS
 287 * distance from evenly spaced; the numbers in the comments are a
 288 * scaled squared error sum) except for the last tap, which is 1 to
 289 * get the twisting happening as fast as possible.
 290 */
 291static struct poolinfo {
 292        int     poolwords;
 293        int     tap1, tap2, tap3, tap4, tap5;
 294} poolinfo_table[] = {
 295        /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1  -- 115 */
 296        { 2048, 1638,   1231,   819,    411,    1 },
 297
 298        /* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */
 299        { 1024, 817,    615,    412,    204,    1 },
 300#if 0                           /* Alternate polynomial */
 301        /* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */
 302        { 1024, 819,    616,    410,    207,    2 },
 303#endif
 304
 305        /* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */
 306        { 512,  411,    308,    208,    104,    1 },
 307#if 0                           /* Alternates */
 308        /* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */
 309        { 512,  409,    307,    206,    102,    2 },
 310        /* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */
 311        { 512,  409,    309,    205,    103,    2 },
 312#endif
 313
 314        /* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */
 315        { 256,  205,    155,    101,    52,     1 },
 316
 317        /* x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 -- 105 */
 318        { 128,  103,    76,     51,     25,     1 },
 319#if 0   /* Alternate polynomial */
 320        /* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */
 321        { 128,  103,    78,     51,     27,     2 },
 322#endif
 323
 324        /* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */
 325        { 64,   52,     39,     26,     14,     1 },
 326
 327        /* x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 -- 15 */
 328        { 32,   26,     20,     14,     7,      1 },
 329
 330        { 0,    0,      0,      0,      0,      0 },
 331};
 332
 333#define POOLBITS        poolwords*32
 334#define POOLBYTES       poolwords*4
 335
 336/*
 337 * For the purposes of better mixing, we use the CRC-32 polynomial as
 338 * well to make a twisted Generalized Feedback Shift Reigster
 339 *
 340 * (See M. Matsumoto & Y. Kurita, 1992.  Twisted GFSR generators.  ACM
 341 * Transactions on Modeling and Computer Simulation 2(3):179-194.
 342 * Also see M. Matsumoto & Y. Kurita, 1994.  Twisted GFSR generators
 343 * II.  ACM Transactions on Mdeling and Computer Simulation 4:254-266)
 344 *
 345 * Thanks to Colin Plumb for suggesting this.
 346 * 
 347 * We have not analyzed the resultant polynomial to prove it primitive;
 348 * in fact it almost certainly isn't.  Nonetheless, the irreducible factors
 349 * of a random large-degree polynomial over GF(2) are more than large enough
 350 * that periodicity is not a concern.
 351 * 
 352 * The input hash is much less sensitive than the output hash.  All
 353 * that we want of it is that it be a good non-cryptographic hash;
 354 * i.e. it not produce collisions when fed "random" data of the sort
 355 * we expect to see.  As long as the pool state differs for different
 356 * inputs, we have preserved the input entropy and done a good job.
 357 * The fact that an intelligent attacker can construct inputs that
 358 * will produce controlled alterations to the pool's state is not
 359 * important because we don't consider such inputs to contribute any
 360 * randomness.  The only property we need with respect to them is that
 361 * the attacker can't increase his/her knowledge of the pool's state.
 362 * Since all additions are reversible (knowing the final state and the
 363 * input, you can reconstruct the initial state), if an attacker has
 364 * any uncertainty about the initial state, he/she can only shuffle
 365 * that uncertainty about, but never cause any collisions (which would
 366 * decrease the uncertainty).
 367 *
 368 * The chosen system lets the state of the pool be (essentially) the input
 369 * modulo the generator polymnomial.  Now, for random primitive polynomials,
 370 * this is a universal class of hash functions, meaning that the chance
 371 * of a collision is limited by the attacker's knowledge of the generator
 372 * polynomail, so if it is chosen at random, an attacker can never force
 373 * a collision.  Here, we use a fixed polynomial, but we *can* assume that
 374 * ###--> it is unknown to the processes generating the input entropy. <-###
 375 * Because of this important property, this is a good, collision-resistant
 376 * hash; hash collisions will occur no more often than chance.
 377 */
 378
 379/*
 380 * Linux 2.2 compatibility
 381 */
 382#ifndef DECLARE_WAITQUEUE
 383#define DECLARE_WAITQUEUE(WAIT, PTR)    struct wait_queue WAIT = { PTR, NULL }
 384#endif
 385#ifndef DECLARE_WAIT_QUEUE_HEAD
 386#define DECLARE_WAIT_QUEUE_HEAD(WAIT) struct wait_queue *WAIT
 387#endif
 388
 389/*
 390 * Static global variables
 391 */
 392static struct entropy_store *random_state; /* The default global store */
 393static struct entropy_store *sec_random_state; /* secondary store */
 394static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
 395static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
 396
 397/*
 398 * Forward procedure declarations
 399 */
 400#ifdef CONFIG_SYSCTL
 401static void sysctl_init_random(struct entropy_store *random_state);
 402#endif
 403
 404/*****************************************************************
 405 *
 406 * Utility functions, with some ASM defined functions for speed
 407 * purposes
 408 * 
 409 *****************************************************************/
 410
 411/*
 412 * Unfortunately, while the GCC optimizer for the i386 understands how
 413 * to optimize a static rotate left of x bits, it doesn't know how to
 414 * deal with a variable rotate of x bits.  So we use a bit of asm magic.
 415 */
 416#if (!defined (__i386__))
 417static inline __u32 rotate_left(int i, __u32 word)
 418{
 419        return (word << i) | (word >> (32 - i));
 420        
 421}
 422#else
 423static inline __u32 rotate_left(int i, __u32 word)
 424{
 425        __asm__("roll %%cl,%0"
 426                :"=r" (word)
 427                :"0" (word),"c" (i));
 428        return word;
 429}
 430#endif
 431
 432/*
 433 * More asm magic....
 434 * 
 435 * For entropy estimation, we need to do an integral base 2
 436 * logarithm.  
 437 *
 438 * Note the "12bits" suffix - this is used for numbers between
 439 * 0 and 4095 only.  This allows a few shortcuts.
 440 */
 441#if 0   /* Slow but clear version */
 442static inline __u32 int_ln_12bits(__u32 word)
 443{
 444        __u32 nbits = 0;
 445        
 446        while (word >>= 1)
 447                nbits++;
 448        return nbits;
 449}
 450#else   /* Faster (more clever) version, courtesy Colin Plumb */
 451static inline __u32 int_ln_12bits(__u32 word)
 452{
 453        /* Smear msbit right to make an n-bit mask */
 454        word |= word >> 8;
 455        word |= word >> 4;
 456        word |= word >> 2;
 457        word |= word >> 1;
 458        /* Remove one bit to make this a logarithm */
 459        word >>= 1;
 460        /* Count the bits set in the word */
 461        word -= (word >> 1) & 0x555;
 462        word = (word & 0x333) + ((word >> 2) & 0x333);
 463        word += (word >> 4);
 464        word += (word >> 8);
 465        return word & 15;
 466}
 467#endif
 468
 469#if 0
 470#define DEBUG_ENT(fmt, arg...) printk(KERN_DEBUG "random: " fmt, ## arg)
 471#else
 472#define DEBUG_ENT(fmt, arg...) do {} while (0)
 473#endif
 474
 475/**********************************************************************
 476 *
 477 * OS independent entropy store.   Here are the functions which handle
 478 * storing entropy in an entropy pool.
 479 * 
 480 **********************************************************************/
 481
 482struct entropy_store {
 483        unsigned        add_ptr;
 484        int             entropy_count;
 485        int             input_rotate;
 486        int             extract_count;
 487        struct poolinfo poolinfo;
 488        __u32           *pool;
 489};
 490
 491/*
 492 * Initialize the entropy store.  The input argument is the size of
 493 * the random pool.
 494 *
 495 * Returns an negative error if there is a problem.
 496 */
 497static int create_entropy_store(int size, struct entropy_store **ret_bucket)
 498{
 499        struct  entropy_store   *r;
 500        struct  poolinfo        *p;
 501        int     poolwords;
 502
 503        poolwords = (size + 3) / 4; /* Convert bytes->words */
 504        /* The pool size must be a multiple of 16 32-bit words */
 505        poolwords = ((poolwords + 15) / 16) * 16; 
 506
 507        for (p = poolinfo_table; p->poolwords; p++) {
 508                if (poolwords == p->poolwords)
 509                        break;
 510        }
 511        if (p->poolwords == 0)
 512                return -EINVAL;
 513
 514        r = kmalloc(sizeof(struct entropy_store), GFP_KERNEL);
 515        if (!r)
 516                return -ENOMEM;
 517
 518        memset (r, 0, sizeof(struct entropy_store));
 519        r->poolinfo = *p;
 520
 521        r->pool = kmalloc(POOLBYTES, GFP_KERNEL);
 522        if (!r->pool) {
 523                kfree(r);
 524                return -ENOMEM;
 525        }
 526        memset(r->pool, 0, POOLBYTES);
 527        *ret_bucket = r;
 528        return 0;
 529}
 530
 531/* Clear the entropy pool and associated counters. */
 532static void clear_entropy_store(struct entropy_store *r)
 533{
 534        r->add_ptr = 0;
 535        r->entropy_count = 0;
 536        r->input_rotate = 0;
 537        r->extract_count = 0;
 538        memset(r->pool, 0, r->poolinfo.POOLBYTES);
 539}
 540
 541static void free_entropy_store(struct entropy_store *r)
 542{
 543        if (r->pool)
 544                kfree(r->pool);
 545        kfree(r);
 546}
 547
 548/*
 549 * This function adds a byte into the entropy "pool".  It does not
 550 * update the entropy estimate.  The caller should call
 551 * credit_entropy_store if this is appropriate.
 552 * 
 553 * The pool is stirred with a primitive polynomial of the appropriate
 554 * degree, and then twisted.  We twist by three bits at a time because
 555 * it's cheap to do so and helps slightly in the expected case where
 556 * the entropy is concentrated in the low-order bits.
 557 */
 558static void add_entropy_words(struct entropy_store *r, const __u32 *in,
 559                              int nwords)
 560{
 561        static __u32 const twist_table[8] = {
 562                         0, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
 563                0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
 564        unsigned i;
 565        int new_rotate;
 566        int wordmask = r->poolinfo.poolwords - 1;
 567        __u32 w;
 568
 569        while (nwords--) {
 570                w = rotate_left(r->input_rotate, *in++);
 571                i = r->add_ptr = (r->add_ptr - 1) & wordmask;
 572                /*
 573                 * Normally, we add 7 bits of rotation to the pool.
 574                 * At the beginning of the pool, add an extra 7 bits
 575                 * rotation, so that successive passes spread the
 576                 * input bits across the pool evenly.
 577                 */
 578                new_rotate = r->input_rotate + 14;
 579                if (i)
 580                        new_rotate = r->input_rotate + 7;
 581                r->input_rotate = new_rotate & 31;
 582
 583                /* XOR in the various taps */
 584                w ^= r->pool[(i + r->poolinfo.tap1) & wordmask];
 585                w ^= r->pool[(i + r->poolinfo.tap2) & wordmask];
 586                w ^= r->pool[(i + r->poolinfo.tap3) & wordmask];
 587                w ^= r->pool[(i + r->poolinfo.tap4) & wordmask];
 588                w ^= r->pool[(i + r->poolinfo.tap5) & wordmask];
 589                w ^= r->pool[i];
 590                r->pool[i] = (w >> 3) ^ twist_table[w & 7];
 591        }
 592}
 593
 594/*
 595 * Credit (or debit) the entropy store with n bits of entropy
 596 */
 597static void credit_entropy_store(struct entropy_store *r, int nbits)
 598{
 599        if (r->entropy_count + nbits < 0) {
 600                DEBUG_ENT("negative entropy/overflow (%d+%d)\n",
 601                          r->entropy_count, nbits);
 602                r->entropy_count = 0;
 603        } else if (r->entropy_count + nbits > r->poolinfo.POOLBITS) {
 604                r->entropy_count = r->poolinfo.POOLBITS;
 605        } else {
 606                r->entropy_count += nbits;
 607                if (nbits)
 608                        DEBUG_ENT("%s added %d bits, now %d\n",
 609                                  r == sec_random_state ? "secondary" :
 610                                  r == random_state ? "primary" : "unknown",
 611                                  nbits, r->entropy_count);
 612        }
 613}
 614
 615/**********************************************************************
 616 *
 617 * Entropy batch input management
 618 *
 619 * We batch entropy to be added to avoid increasing interrupt latency
 620 *
 621 **********************************************************************/
 622
 623static __u32    *batch_entropy_pool;
 624static int      *batch_entropy_credit;
 625static int      batch_max;
 626static int      batch_head, batch_tail;
 627static struct tq_struct batch_tqueue;
 628static void batch_entropy_process(void *private_);
 629
 630/* note: the size must be a power of 2 */
 631static int __init batch_entropy_init(int size, struct entropy_store *r)
 632{
 633        batch_entropy_pool = kmalloc(2*size*sizeof(__u32), GFP_KERNEL);
 634        if (!batch_entropy_pool)
 635                return -1;
 636        batch_entropy_credit =kmalloc(size*sizeof(int), GFP_KERNEL);
 637        if (!batch_entropy_credit) {
 638                kfree(batch_entropy_pool);
 639                return -1;
 640        }
 641        batch_head = batch_tail = 0;
 642        batch_max = size;
 643        batch_tqueue.routine = batch_entropy_process;
 644        batch_tqueue.data = r;
 645        return 0;
 646}
 647
 648/*
 649 * Changes to the entropy data is put into a queue rather than being added to
 650 * the entropy counts directly.  This is presumably to avoid doing heavy
 651 * hashing calculations during an interrupt in add_timer_randomness().
 652 * Instead, the entropy is only added to the pool once per timer tick.
 653 */
 654void batch_entropy_store(u32 a, u32 b, int num)
 655{
 656        int     new;
 657
 658        if (!batch_max)
 659                return;
 660        
 661        batch_entropy_pool[2*batch_head] = a;
 662        batch_entropy_pool[(2*batch_head) + 1] = b;
 663        batch_entropy_credit[batch_head] = num;
 664
 665        new = (batch_head+1) & (batch_max-1);
 666        if (new != batch_tail) {
 667                queue_task(&batch_tqueue, &tq_timer);
 668                batch_head = new;
 669        } else {
 670                DEBUG_ENT("batch entropy buffer full\n");
 671        }
 672}
 673
 674/*
 675 * Flush out the accumulated entropy operations, adding entropy to the passed
 676 * store (normally random_state).  If that store has enough entropy, alternate
 677 * between randomizing the data of the primary and secondary stores.
 678 */
 679static void batch_entropy_process(void *private_)
 680{
 681        struct entropy_store *r = (struct entropy_store *) private_, *p;
 682        int max_entropy = r->poolinfo.POOLBITS;
 683
 684        if (!batch_max)
 685                return;
 686
 687        p = r;
 688        while (batch_head != batch_tail) {
 689                if (r->entropy_count >= max_entropy) {
 690                        r = (r == sec_random_state) ?   random_state :
 691                                                        sec_random_state;
 692                        max_entropy = r->poolinfo.POOLBITS;
 693                }
 694                add_entropy_words(r, batch_entropy_pool + 2*batch_tail, 2);
 695                credit_entropy_store(r, batch_entropy_credit[batch_tail]);
 696                batch_tail = (batch_tail+1) & (batch_max-1);
 697        }
 698        if (p->entropy_count >= random_read_wakeup_thresh)
 699                wake_up_interruptible(&random_read_wait);
 700}
 701
 702/*********************************************************************
 703 *
 704 * Entropy input management
 705 *
 706 *********************************************************************/
 707
 708/* There is one of these per entropy source */
 709struct timer_rand_state {
 710        __u32           last_time;
 711        __s32           last_delta,last_delta2;
 712        int             dont_count_entropy:1;
 713};
 714
 715static struct timer_rand_state keyboard_timer_state;
 716static struct timer_rand_state mouse_timer_state;
 717static struct timer_rand_state extract_timer_state;
 718#ifndef CONFIG_ARCH_S390
 719static struct timer_rand_state *irq_timer_state[NR_IRQS];
 720#endif
 721static struct timer_rand_state *blkdev_timer_state[MAX_BLKDEV];
 722
 723/*
 724 * This function adds entropy to the entropy "pool" by using timing
 725 * delays.  It uses the timer_rand_state structure to make an estimate
 726 * of how many bits of entropy this call has added to the pool.
 727 *
 728 * The number "num" is also added to the pool - it should somehow describe
 729 * the type of event which just happened.  This is currently 0-255 for
 730 * keyboard scan codes, and 256 upwards for interrupts.
 731 * On the i386, this is assumed to be at most 16 bits, and the high bits
 732 * are used for a high-resolution timer.
 733 *
 734 */
 735static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 736{
 737        __u32           time;
 738        __s32           delta, delta2, delta3;
 739        int             entropy = 0;
 740
 741#if defined (__i386__)
 742        if (cpu_has_tsc) {
 743                __u32 high;
 744                rdtsc(time, high);
 745                num ^= high;
 746        } else {
 747                time = jiffies;
 748        }
 749#elif defined (__x86_64__)
 750        __u32 high;
 751        rdtsc(time, high);
 752        num ^= high;
 753#elif defined (__sparc_v9__)
 754        unsigned long tick = tick_ops->get_tick();
 755
 756        time = (unsigned int) tick;
 757        num ^= (tick >> 32UL);
 758#else
 759        time = jiffies;
 760#endif
 761
 762        /*
 763         * Calculate number of bits of randomness we probably added.
 764         * We take into account the first, second and third-order deltas
 765         * in order to make our estimate.
 766         */
 767        if (!state->dont_count_entropy) {
 768                delta = time - state->last_time;
 769                state->last_time = time;
 770
 771                delta2 = delta - state->last_delta;
 772                state->last_delta = delta;
 773
 774                delta3 = delta2 - state->last_delta2;
 775                state->last_delta2 = delta2;
 776
 777                if (delta < 0)
 778                        delta = -delta;
 779                if (delta2 < 0)
 780                        delta2 = -delta2;
 781                if (delta3 < 0)
 782                        delta3 = -delta3;
 783                if (delta > delta2)
 784                        delta = delta2;
 785                if (delta > delta3)
 786                        delta = delta3;
 787
 788                /*
 789                 * delta is now minimum absolute delta.
 790                 * Round down by 1 bit on general principles,
 791                 * and limit entropy entimate to 12 bits.
 792                 */
 793                delta >>= 1;
 794                delta &= (1 << 12) - 1;
 795
 796                entropy = int_ln_12bits(delta);
 797        }
 798        batch_entropy_store(num, time, entropy);
 799}
 800
 801#ifndef CONFIG_ARCH_S390
 802void add_keyboard_randomness(unsigned char scancode)
 803{
 804        static unsigned char last_scancode;
 805        /* ignore autorepeat (multiple key down w/o key up) */
 806        if (scancode != last_scancode) {
 807                last_scancode = scancode;
 808                add_timer_randomness(&keyboard_timer_state, scancode);
 809        }
 810}
 811
 812void add_mouse_randomness(__u32 mouse_data)
 813{
 814        add_timer_randomness(&mouse_timer_state, mouse_data);
 815}
 816
 817void add_interrupt_randomness(int irq)
 818{
 819        if (irq >= NR_IRQS || irq_timer_state[irq] == 0)
 820                return;
 821
 822        add_timer_randomness(irq_timer_state[irq], 0x100+irq);
 823}
 824#endif
 825
 826void add_blkdev_randomness(int major)
 827{
 828        if (major >= MAX_BLKDEV)
 829                return;
 830
 831        if (blkdev_timer_state[major] == 0) {
 832                rand_initialize_blkdev(major, GFP_ATOMIC);
 833                if (blkdev_timer_state[major] == 0)
 834                        return;
 835        }
 836                
 837        add_timer_randomness(blkdev_timer_state[major], 0x200+major);
 838}
 839
 840/******************************************************************
 841 *
 842 * Hash function definition
 843 *
 844 *******************************************************************/
 845
 846/*
 847 * This chunk of code defines a function
 848 * void HASH_TRANSFORM(__u32 digest[HASH_BUFFER_SIZE + HASH_EXTRA_SIZE],
 849 *              __u32 const data[16])
 850 * 
 851 * The function hashes the input data to produce a digest in the first
 852 * HASH_BUFFER_SIZE words of the digest[] array, and uses HASH_EXTRA_SIZE
 853 * more words for internal purposes.  (This buffer is exported so the
 854 * caller can wipe it once rather than this code doing it each call,
 855 * and tacking it onto the end of the digest[] array is the quick and
 856 * dirty way of doing it.)
 857 *
 858 * It so happens that MD5 and SHA share most of the initial vector
 859 * used to initialize the digest[] array before the first call:
 860 * 1) 0x67452301
 861 * 2) 0xefcdab89
 862 * 3) 0x98badcfe
 863 * 4) 0x10325476
 864 * 5) 0xc3d2e1f0 (SHA only)
 865 * 
 866 * For /dev/random purposes, the length of the data being hashed is
 867 * fixed in length, so appending a bit count in the usual way is not
 868 * cryptographically necessary.
 869 */
 870
 871#ifdef USE_SHA
 872
 873#define HASH_BUFFER_SIZE 5
 874#define HASH_EXTRA_SIZE 80
 875#define HASH_TRANSFORM SHATransform
 876
 877/* Various size/speed tradeoffs are available.  Choose 0..3. */
 878#define SHA_CODE_SIZE 0
 879
 880/*
 881 * SHA transform algorithm, taken from code written by Peter Gutmann,
 882 * and placed in the public domain.
 883 */
 884
 885/* The SHA f()-functions.  */
 886
 887#define f1(x,y,z)   ( z ^ (x & (y^z)) )         /* Rounds  0-19: x ? y : z */
 888#define f2(x,y,z)   (x ^ y ^ z)                 /* Rounds 20-39: XOR */
 889#define f3(x,y,z)   ( (x & y) + (z & (x ^ y)) ) /* Rounds 40-59: majority */
 890#define f4(x,y,z)   (x ^ y ^ z)                 /* Rounds 60-79: XOR */
 891
 892/* The SHA Mysterious Constants */
 893
 894#define K1  0x5A827999L                 /* Rounds  0-19: sqrt(2) * 2^30 */
 895#define K2  0x6ED9EBA1L                 /* Rounds 20-39: sqrt(3) * 2^30 */
 896#define K3  0x8F1BBCDCL                 /* Rounds 40-59: sqrt(5) * 2^30 */
 897#define K4  0xCA62C1D6L                 /* Rounds 60-79: sqrt(10) * 2^30 */
 898
 899#define ROTL(n,X)  ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
 900
 901#define subRound(a, b, c, d, e, f, k, data) \
 902    ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
 903
 904
 905static void SHATransform(__u32 digest[85], __u32 const data[16])
 906{
 907    __u32 A, B, C, D, E;     /* Local vars */
 908    __u32 TEMP;
 909    int i;
 910#define W (digest + HASH_BUFFER_SIZE)   /* Expanded data array */
 911
 912    /*
 913     * Do the preliminary expansion of 16 to 80 words.  Doing it
 914     * out-of-line line this is faster than doing it in-line on
 915     * register-starved machines like the x86, and not really any
 916     * slower on real processors.
 917     */
 918    memcpy(W, data, 16*sizeof(__u32));
 919    for (i = 0; i < 64; i++) {
 920            TEMP = W[i] ^ W[i+2] ^ W[i+8] ^ W[i+13];
 921            W[i+16] = ROTL(1, TEMP);
 922    }
 923
 924    /* Set up first buffer and local data buffer */
 925    A = digest[ 0 ];
 926    B = digest[ 1 ];
 927    C = digest[ 2 ];
 928    D = digest[ 3 ];
 929    E = digest[ 4 ];
 930
 931    /* Heavy mangling, in 4 sub-rounds of 20 iterations each. */
 932#if SHA_CODE_SIZE == 0
 933    /*
 934     * Approximately 50% of the speed of the largest version, but
 935     * takes up 1/16 the space.  Saves about 6k on an i386 kernel.
 936     */
 937    for (i = 0; i < 80; i++) {
 938        if (i < 40) {
 939            if (i < 20)
 940                TEMP = f1(B, C, D) + K1;
 941            else
 942                TEMP = f2(B, C, D) + K2;
 943        } else {
 944            if (i < 60)
 945                TEMP = f3(B, C, D) + K3;
 946            else
 947                TEMP = f4(B, C, D) + K4;
 948        }
 949        TEMP += ROTL(5, A) + E + W[i];
 950        E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
 951    }
 952#elif SHA_CODE_SIZE == 1
 953    for (i = 0; i < 20; i++) {
 954        TEMP = f1(B, C, D) + K1 + ROTL(5, A) + E + W[i];
 955        E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
 956    }
 957    for (; i < 40; i++) {
 958        TEMP = f2(B, C, D) + K2 + ROTL(5, A) + E + W[i];
 959        E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
 960    }
 961    for (; i < 60; i++) {
 962        TEMP = f3(B, C, D) + K3 + ROTL(5, A) + E + W[i];
 963        E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
 964    }
 965    for (; i < 80; i++) {
 966        TEMP = f4(B, C, D) + K4 + ROTL(5, A) + E + W[i];
 967        E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
 968    }
 969#elif SHA_CODE_SIZE == 2
 970    for (i = 0; i < 20; i += 5) {
 971        subRound( A, B, C, D, E, f1, K1, W[ i   ] );
 972        subRound( E, A, B, C, D, f1, K1, W[ i+1 ] );
 973        subRound( D, E, A, B, C, f1, K1, W[ i+2 ] );
 974        subRound( C, D, E, A, B, f1, K1, W[ i+3 ] );
 975        subRound( B, C, D, E, A, f1, K1, W[ i+4 ] );
 976    }
 977    for (; i < 40; i += 5) {
 978        subRound( A, B, C, D, E, f2, K2, W[ i   ] );
 979        subRound( E, A, B, C, D, f2, K2, W[ i+1 ] );
 980        subRound( D, E, A, B, C, f2, K2, W[ i+2 ] );
 981        subRound( C, D, E, A, B, f2, K2, W[ i+3 ] );
 982        subRound( B, C, D, E, A, f2, K2, W[ i+4 ] );
 983    }
 984    for (; i < 60; i += 5) {
 985        subRound( A, B, C, D, E, f3, K3, W[ i   ] );
 986        subRound( E, A, B, C, D, f3, K3, W[ i+1 ] );
 987        subRound( D, E, A, B, C, f3, K3, W[ i+2 ] );
 988        subRound( C, D, E, A, B, f3, K3, W[ i+3 ] );
 989        subRound( B, C, D, E, A, f3, K3, W[ i+4 ] );
 990    }
 991    for (; i < 80; i += 5) {
 992        subRound( A, B, C, D, E, f4, K4, W[ i   ] );
 993        subRound( E, A, B, C, D, f4, K4, W[ i+1 ] );
 994        subRound( D, E, A, B, C, f4, K4, W[ i+2 ] );
 995        subRound( C, D, E, A, B, f4, K4, W[ i+3 ] );
 996        subRound( B, C, D, E, A, f4, K4, W[ i+4 ] );
 997    }
 998#elif SHA_CODE_SIZE == 3 /* Really large version */
 999    subRound( A, B, C, D, E, f1, K1, W[  0 ] );
1000    subRound( E, A, B, C, D, f1, K1, W[  1 ] );
1001    subRound( D, E, A, B, C, f1, K1, W[  2 ] );
1002    subRound( C, D, E, A, B, f1, K1, W[  3 ] );
1003    subRound( B, C, D, E, A, f1, K1, W[  4 ] );
1004    subRound( A, B, C, D, E, f1, K1, W[  5 ] );
1005    subRound( E, A, B, C, D, f1, K1, W[  6 ] );
1006    subRound( D, E, A, B, C, f1, K1, W[  7 ] );
1007    subRound( C, D, E, A, B, f1, K1, W[  8 ] );
1008    subRound( B, C, D, E, A, f1, K1, W[  9 ] );
1009    subRound( A, B, C, D, E, f1, K1, W[ 10 ] );
1010    subRound( E, A, B, C, D, f1, K1, W[ 11 ] );
1011    subRound( D, E, A, B, C, f1, K1, W[ 12 ] );
1012    subRound( C, D, E, A, B, f1, K1, W[ 13 ] );
1013    subRound( B, C, D, E, A, f1, K1, W[ 14 ] );
1014    subRound( A, B, C, D, E, f1, K1, W[ 15 ] );
1015    subRound( E, A, B, C, D, f1, K1, W[ 16 ] );
1016    subRound( D, E, A, B, C, f1, K1, W[ 17 ] );
1017    subRound( C, D, E, A, B, f1, K1, W[ 18 ] );
1018    subRound( B, C, D, E, A, f1, K1, W[ 19 ] );
1019
1020    subRound( A, B, C, D, E, f2, K2, W[ 20 ] );
1021    subRound( E, A, B, C, D, f2, K2, W[ 21 ] );
1022    subRound( D, E, A, B, C, f2, K2, W[ 22 ] );
1023    subRound( C, D, E, A, B, f2, K2, W[ 23 ] );
1024    subRound( B, C, D, E, A, f2, K2, W[ 24 ] );
1025    subRound( A, B, C, D, E, f2, K2, W[ 25 ] );
1026    subRound( E, A, B, C, D, f2, K2, W[ 26 ] );
1027    subRound( D, E, A, B, C, f2, K2, W[ 27 ] );
1028    subRound( C, D, E, A, B, f2, K2, W[ 28 ] );
1029    subRound( B, C, D, E, A, f2, K2, W[ 29 ] );
1030    subRound( A, B, C, D, E, f2, K2, W[ 30 ] );
1031    subRound( E, A, B, C, D, f2, K2, W[ 31 ] );
1032    subRound( D, E, A, B, C, f2, K2, W[ 32 ] );
1033    subRound( C, D, E, A, B, f2, K2, W[ 33 ] );
1034    subRound( B, C, D, E, A, f2, K2, W[ 34 ] );
1035    subRound( A, B, C, D, E, f2, K2, W[ 35 ] );
1036    subRound( E, A, B, C, D, f2, K2, W[ 36 ] );
1037    subRound( D, E, A, B, C, f2, K2, W[ 37 ] );
1038    subRound( C, D, E, A, B, f2, K2, W[ 38 ] );
1039    subRound( B, C, D, E, A, f2, K2, W[ 39 ] );
1040    
1041    subRound( A, B, C, D, E, f3, K3, W[ 40 ] );
1042    subRound( E, A, B, C, D, f3, K3, W[ 41 ] );
1043    subRound( D, E, A, B, C, f3, K3, W[ 42 ] );
1044    subRound( C, D, E, A, B, f3, K3, W[ 43 ] );
1045    subRound( B, C, D, E, A, f3, K3, W[ 44 ] );
1046    subRound( A, B, C, D, E, f3, K3, W[ 45 ] );
1047    subRound( E, A, B, C, D, f3, K3, W[ 46 ] );
1048    subRound( D, E, A, B, C, f3, K3, W[ 47 ] );
1049    subRound( C, D, E, A, B, f3, K3, W[ 48 ] );
1050    subRound( B, C, D, E, A, f3, K3, W[ 49 ] );
1051    subRound( A, B, C, D, E, f3, K3, W[ 50 ] );
1052    subRound( E, A, B, C, D, f3, K3, W[ 51 ] );
1053    subRound( D, E, A, B, C, f3, K3, W[ 52 ] );
1054    subRound( C, D, E, A, B, f3, K3, W[ 53 ] );
1055    subRound( B, C, D, E, A, f3, K3, W[ 54 ] );
1056    subRound( A, B, C, D, E, f3, K3, W[ 55 ] );
1057    subRound( E, A, B, C, D, f3, K3, W[ 56 ] );
1058    subRound( D, E, A, B, C, f3, K3, W[ 57 ] );
1059    subRound( C, D, E, A, B, f3, K3, W[ 58 ] );
1060    subRound( B, C, D, E, A, f3, K3, W[ 59 ] );
1061
1062    subRound( A, B, C, D, E, f4, K4, W[ 60 ] );
1063    subRound( E, A, B, C, D, f4, K4, W[ 61 ] );
1064    subRound( D, E, A, B, C, f4, K4, W[ 62 ] );
1065    subRound( C, D, E, A, B, f4, K4, W[ 63 ] );
1066    subRound( B, C, D, E, A, f4, K4, W[ 64 ] );
1067    subRound( A, B, C, D, E, f4, K4, W[ 65 ] );
1068    subRound( E, A, B, C, D, f4, K4, W[ 66 ] );
1069    subRound( D, E, A, B, C, f4, K4, W[ 67 ] );
1070    subRound( C, D, E, A, B, f4, K4, W[ 68 ] );
1071    subRound( B, C, D, E, A, f4, K4, W[ 69 ] );
1072    subRound( A, B, C, D, E, f4, K4, W[ 70 ] );
1073    subRound( E, A, B, C, D, f4, K4, W[ 71 ] );
1074    subRound( D, E, A, B, C, f4, K4, W[ 72 ] );
1075    subRound( C, D, E, A, B, f4, K4, W[ 73 ] );
1076    subRound( B, C, D, E, A, f4, K4, W[ 74 ] );
1077    subRound( A, B, C, D, E, f4, K4, W[ 75 ] );
1078    subRound( E, A, B, C, D, f4, K4, W[ 76 ] );
1079    subRound( D, E, A, B, C, f4, K4, W[ 77 ] );
1080    subRound( C, D, E, A, B, f4, K4, W[ 78 ] );
1081    subRound( B, C, D, E, A, f4, K4, W[ 79 ] );
1082#else
1083#error Illegal SHA_CODE_SIZE
1084#endif
1085
1086    /* Build message digest */
1087    digest[ 0 ] += A;
1088    digest[ 1 ] += B;
1089    digest[ 2 ] += C;
1090    digest[ 3 ] += D;
1091    digest[ 4 ] += E;
1092
1093        /* W is wiped by the caller */
1094#undef W
1095}
1096
1097#undef ROTL
1098#undef f1
1099#undef f2
1100#undef f3
1101#undef f4
1102#undef K1       
1103#undef K2
1104#undef K3       
1105#undef K4       
1106#undef subRound
1107        
1108#else /* !USE_SHA - Use MD5 */
1109
1110#define HASH_BUFFER_SIZE 4
1111#define HASH_EXTRA_SIZE 0
1112#define HASH_TRANSFORM MD5Transform
1113        
1114/*
1115 * MD5 transform algorithm, taken from code written by Colin Plumb,
1116 * and put into the public domain
1117 */
1118
1119/* The four core functions - F1 is optimized somewhat */
1120
1121/* #define F1(x, y, z) (x & y | ~x & z) */
1122#define F1(x, y, z) (z ^ (x & (y ^ z)))
1123#define F2(x, y, z) F1(z, x, y)
1124#define F3(x, y, z) (x ^ y ^ z)
1125#define F4(x, y, z) (y ^ (x | ~z))
1126
1127/* This is the central step in the MD5 algorithm. */
1128#define MD5STEP(f, w, x, y, z, data, s) \
1129        ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
1130
1131/*
1132 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1133 * reflect the addition of 16 longwords of new data.  MD5Update blocks
1134 * the data and converts bytes into longwords for this routine.
1135 */
1136static void MD5Transform(__u32 buf[HASH_BUFFER_SIZE], __u32 const in[16])
1137{
1138        __u32 a, b, c, d;
1139
1140        a = buf[0];
1141        b = buf[1];
1142        c = buf[2];
1143        d = buf[3];
1144
1145        MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478,  7);
1146        MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
1147        MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
1148        MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
1149        MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf,  7);
1150        MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
1151        MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
1152        MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
1153        MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8,  7);
1154        MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
1155        MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
1156        MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
1157        MD5STEP(F1, a, b, c, d, in[12]+0x6b901122,  7);
1158        MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
1159        MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
1160        MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
1161
1162        MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562,  5);
1163        MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340,  9);
1164        MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
1165        MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
1166        MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d,  5);
1167        MD5STEP(F2, d, a, b, c, in[10]+0x02441453,  9);
1168        MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
1169        MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
1170        MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6,  5);
1171        MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6,  9);
1172        MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
1173        MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
1174        MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905,  5);
1175        MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8,  9);
1176        MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
1177        MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
1178
1179        MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942,  4);
1180        MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
1181        MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
1182        MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
1183        MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44,  4);
1184        MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
1185        MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
1186        MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
1187        MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6,  4);
1188        MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
1189        MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
1190        MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
1191        MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039,  4);
1192        MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
1193        MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
1194        MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
1195
1196        MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244,  6);
1197        MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
1198        MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
1199        MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
1200        MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3,  6);
1201        MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
1202        MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
1203        MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
1204        MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f,  6);
1205        MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
1206        MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
1207        MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
1208        MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82,  6);
1209        MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
1210        MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
1211        MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
1212
1213        buf[0] += a;
1214        buf[1] += b;
1215        buf[2] += c;
1216        buf[3] += d;
1217}
1218
1219#undef F1
1220#undef F2
1221#undef F3
1222#undef F4
1223#undef MD5STEP
1224
1225#endif /* !USE_SHA */
1226
1227/*********************************************************************
1228 *
1229 * Entropy extraction routines
1230 *
1231 *********************************************************************/
1232
1233#define EXTRACT_ENTROPY_USER            1
1234#define EXTRACT_ENTROPY_SECONDARY       2
1235#define TMP_BUF_SIZE                    (HASH_BUFFER_SIZE + HASH_EXTRA_SIZE)
1236#define SEC_XFER_SIZE                   (TMP_BUF_SIZE*4)
1237
1238static ssize_t extract_entropy(struct entropy_store *r, void * buf,
1239                               size_t nbytes, int flags);
1240
1241/*
1242 * This utility inline function is responsible for transfering entropy
1243 * from the primary pool to the secondary extraction pool.  We pull
1244 * randomness under two conditions; one is if there isn't enough entropy
1245 * in the secondary pool.  The other is after we have extracted 1024 bytes,
1246 * at which point we do a "catastrophic reseeding".
1247 */
1248static inline void xfer_secondary_pool(struct entropy_store *r,
1249                                       size_t nbytes, __u32 *tmp)
1250{
1251        if (r->entropy_count < nbytes * 8 &&
1252            r->entropy_count < r->poolinfo.POOLBITS) {
1253                int nwords = min_t(int,
1254                                   r->poolinfo.poolwords - r->entropy_count/32,
1255                                   sizeof(tmp) / 4);
1256
1257                DEBUG_ENT("xfer %d from primary to %s (have %d, need %d)\n",
1258                          nwords * 32,
1259                          r == sec_random_state ? "secondary" : "unknown",
1260                          r->entropy_count, nbytes * 8);
1261
1262                extract_entropy(random_state, tmp, nwords * 4, 0);
1263                add_entropy_words(r, tmp, nwords);
1264                credit_entropy_store(r, nwords * 32);
1265        }
1266        if (r->extract_count > 1024) {
1267                DEBUG_ENT("reseeding %s with %d from primary\n",
1268                          r == sec_random_state ? "secondary" : "unknown",
1269                          sizeof(tmp) * 8);
1270                extract_entropy(random_state, tmp, sizeof(tmp), 0);
1271                add_entropy_words(r, tmp, sizeof(tmp) / 4);
1272                r->extract_count = 0;
1273        }
1274}
1275
1276/*
1277 * This function extracts randomness from the "entropy pool", and
1278 * returns it in a buffer.  This function computes how many remaining
1279 * bits of entropy are left in the pool, but it does not restrict the
1280 * number of bytes that are actually obtained.  If the EXTRACT_ENTROPY_USER
1281 * flag is given, then the buf pointer is assumed to be in user space.
1282 *
1283 * If the EXTRACT_ENTROPY_SECONDARY flag is given, then we are actually
1284 * extracting entropy from the secondary pool, and can refill from the
1285 * primary pool if needed.
1286 *
1287 * Note: extract_entropy() assumes that .poolwords is a multiple of 16 words.
1288 */
1289static ssize_t extract_entropy(struct entropy_store *r, void * buf,
1290                               size_t nbytes, int flags)
1291{
1292        ssize_t ret, i;
1293        __u32 tmp[TMP_BUF_SIZE];
1294        __u32 x;
1295
1296        add_timer_randomness(&extract_timer_state, nbytes);
1297
1298        /* Redundant, but just in case... */
1299        if (r->entropy_count > r->poolinfo.POOLBITS)
1300                r->entropy_count = r->poolinfo.POOLBITS;
1301
1302        if (flags & EXTRACT_ENTROPY_SECONDARY)
1303                xfer_secondary_pool(r, nbytes, tmp);
1304
1305        DEBUG_ENT("%s has %d bits, want %d bits\n",
1306                  r == sec_random_state ? "secondary" :
1307                  r == random_state ? "primary" : "unknown",
1308                  r->entropy_count, nbytes * 8);
1309
1310        if (r->entropy_count / 8 >= nbytes)
1311                r->entropy_count -= nbytes*8;
1312        else
1313                r->entropy_count = 0;
1314
1315        if (r->entropy_count < random_write_wakeup_thresh)
1316                wake_up_interruptible(&random_write_wait);
1317
1318        r->extract_count += nbytes;
1319        
1320        ret = 0;
1321        while (nbytes) {
1322                /*
1323                 * Check if we need to break out or reschedule....
1324                 */
1325                if ((flags & EXTRACT_ENTROPY_USER) && current->need_resched) {
1326                        if (signal_pending(current)) {
1327                                if (ret == 0)
1328                                        ret = -ERESTARTSYS;
1329                                break;
1330                        }
1331                        schedule();
1332                }
1333
1334                /* Hash the pool to get the output */
1335                tmp[0] = 0x67452301;
1336                tmp[1] = 0xefcdab89;
1337                tmp[2] = 0x98badcfe;
1338                tmp[3] = 0x10325476;
1339#ifdef USE_SHA
1340                tmp[4] = 0xc3d2e1f0;
1341#endif
1342                /*
1343                 * As we hash the pool, we mix intermediate values of
1344                 * the hash back into the pool.  This eliminates
1345                 * backtracking attacks (where the attacker knows
1346                 * the state of the pool plus the current outputs, and
1347                 * attempts to find previous ouputs), unless the hash
1348                 * function can be inverted.
1349                 */
1350                for (i = 0, x = 0; i < r->poolinfo.poolwords; i += 16, x+=2) {
1351                        HASH_TRANSFORM(tmp, r->pool+i);
1352                        add_entropy_words(r, &tmp[x%HASH_BUFFER_SIZE], 1);
1353                }
1354                
1355                /*
1356                 * In case the hash function has some recognizable
1357                 * output pattern, we fold it in half.
1358                 */
1359                for (i = 0; i <  HASH_BUFFER_SIZE/2; i++)
1360                        tmp[i] ^= tmp[i + (HASH_BUFFER_SIZE+1)/2];
1361#if HASH_BUFFER_SIZE & 1        /* There's a middle word to deal with */
1362                x = tmp[HASH_BUFFER_SIZE/2];
1363                x ^= (x >> 16);         /* Fold it in half */
1364                ((__u16 *)tmp)[HASH_BUFFER_SIZE-1] = (__u16)x;
1365#endif
1366                
1367                /* Copy data to destination buffer */
1368                i = min(nbytes, HASH_BUFFER_SIZE*sizeof(__u32)/2);
1369                if (flags & EXTRACT_ENTROPY_USER) {
1370                        i -= copy_to_user(buf, (__u8 const *)tmp, i);
1371                        if (!i) {
1372                                ret = -EFAULT;
1373                                break;
1374                        }
1375                } else
1376                        memcpy(buf, (__u8 const *)tmp, i);
1377                nbytes -= i;
1378                buf += i;
1379                ret += i;
1380                add_timer_randomness(&extract_timer_state, nbytes);
1381        }
1382
1383        /* Wipe data just returned from memory */
1384        memset(tmp, 0, sizeof(tmp));
1385        
1386        return ret;
1387}
1388
1389/*
1390 * This function is the exported kernel interface.  It returns some
1391 * number of good random numbers, suitable for seeding TCP sequence
1392 * numbers, etc.
1393 */
1394void get_random_bytes(void *buf, int nbytes)
1395{
1396        if (sec_random_state)  
1397                extract_entropy(sec_random_state, (char *) buf, nbytes, 
1398                                EXTRACT_ENTROPY_SECONDARY);
1399        else if (random_state)
1400                extract_entropy(random_state, (char *) buf, nbytes, 0);
1401        else
1402                printk(KERN_NOTICE "get_random_bytes called before "
1403                                   "random driver initialization\n");
1404}
1405
1406/*********************************************************************
1407 *
1408 * Functions to interface with Linux
1409 *
1410 *********************************************************************/
1411
1412/*
1413 * Initialize the random pool with standard stuff.
1414 *
1415 * NOTE: This is an OS-dependent function.
1416 */
1417static void init_std_data(struct entropy_store *r)
1418{
1419        struct timeval  tv;
1420        __u32           words[2];
1421        char            *p;
1422        int             i;
1423
1424        do_gettimeofday(&tv);
1425        words[0] = tv.tv_sec;
1426        words[1] = tv.tv_usec;
1427        add_entropy_words(r, words, 2);
1428
1429        /*
1430         *      This doesn't lock system.utsname. However, we are generating
1431         *      entropy so a race with a name set here is fine.
1432         */
1433        p = (char *) &system_utsname;
1434        for (i = sizeof(system_utsname) / sizeof(words); i; i--) {
1435                memcpy(words, p, sizeof(words));
1436                add_entropy_words(r, words, sizeof(words)/4);
1437                p += sizeof(words);
1438        }
1439}
1440
1441void __init rand_initialize(void)
1442{
1443        int i;
1444
1445        if (create_entropy_store(DEFAULT_POOL_SIZE, &random_state))
1446                return;         /* Error, return */
1447        if (batch_entropy_init(BATCH_ENTROPY_SIZE, random_state))
1448                return;         /* Error, return */
1449        if (create_entropy_store(SECONDARY_POOL_SIZE, &sec_random_state))
1450                return;         /* Error, return */
1451        clear_entropy_store(random_state);
1452        clear_entropy_store(sec_random_state);
1453        init_std_data(random_state);
1454#ifdef CONFIG_SYSCTL
1455        sysctl_init_random(random_state);
1456#endif
1457#ifndef CONFIG_ARCH_S390
1458        for (i = 0; i < NR_IRQS; i++)
1459                irq_timer_state[i] = NULL;
1460#endif
1461        for (i = 0; i < MAX_BLKDEV; i++)
1462                blkdev_timer_state[i] = NULL;
1463        memset(&keyboard_timer_state, 0, sizeof(struct timer_rand_state));
1464        memset(&mouse_timer_state, 0, sizeof(struct timer_rand_state));
1465        memset(&extract_timer_state, 0, sizeof(struct timer_rand_state));
1466        extract_timer_state.dont_count_entropy = 1;
1467}
1468
1469#ifndef CONFIG_ARCH_S390
1470void rand_initialize_irq(int irq)
1471{
1472        struct timer_rand_state *state;
1473        
1474        if (irq >= NR_IRQS || irq_timer_state[irq])
1475                return;
1476
1477        /*
1478         * If kmalloc returns null, we just won't use that entropy
1479         * source.
1480         */
1481        state = kmalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1482        if (state) {
1483                memset(state, 0, sizeof(struct timer_rand_state));
1484                irq_timer_state[irq] = state;
1485        }
1486}
1487#endif
1488
1489void rand_initialize_blkdev(int major, int mode)
1490{
1491        struct timer_rand_state *state;
1492        
1493        if (major >= MAX_BLKDEV || blkdev_timer_state[major])
1494                return;
1495
1496        /*
1497         * If kmalloc returns null, we just won't use that entropy
1498         * source.
1499         */
1500        state = kmalloc(sizeof(struct timer_rand_state), mode);
1501        if (state) {
1502                memset(state, 0, sizeof(struct timer_rand_state));
1503                blkdev_timer_state[major] = state;
1504        }
1505}
1506
1507
1508static ssize_t
1509random_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
1510{
1511        DECLARE_WAITQUEUE(wait, current);
1512        ssize_t                 n, retval = 0, count = 0;
1513        
1514        if (nbytes == 0)
1515                return 0;
1516
1517        add_wait_queue(&random_read_wait, &wait);
1518        while (nbytes > 0) {
1519                set_current_state(TASK_INTERRUPTIBLE);
1520                
1521                n = nbytes;
1522                if (n > SEC_XFER_SIZE)
1523                        n = SEC_XFER_SIZE;
1524                if (n > random_state->entropy_count / 8)
1525                        n = random_state->entropy_count / 8;
1526                if (n == 0) {
1527                        if (file->f_flags & O_NONBLOCK) {
1528                                retval = -EAGAIN;
1529                                break;
1530                        }
1531                        if (signal_pending(current)) {
1532                                retval = -ERESTARTSYS;
1533                                break;
1534                        }
1535                        schedule();
1536                        continue;
1537                }
1538                n = extract_entropy(sec_random_state, buf, n,
1539                                    EXTRACT_ENTROPY_USER |
1540                                    EXTRACT_ENTROPY_SECONDARY);
1541                if (n < 0) {
1542                        retval = n;
1543                        break;
1544                }
1545                count += n;
1546                buf += n;
1547                nbytes -= n;
1548                break;          /* This break makes the device work */
1549                                /* like a named pipe */
1550        }
1551        current->state = TASK_RUNNING;
1552        remove_wait_queue(&random_read_wait, &wait);
1553
1554        /*
1555         * If we gave the user some bytes, update the access time.
1556         */
1557        if (count != 0) {
1558                UPDATE_ATIME(file->f_dentry->d_inode);
1559        }
1560        
1561        return (count ? count : retval);
1562}
1563
1564static ssize_t
1565urandom_read(struct file * file, char * buf,
1566                      size_t nbytes, loff_t *ppos)
1567{
1568        return extract_entropy(sec_random_state, buf, nbytes,
1569                               EXTRACT_ENTROPY_USER |
1570                               EXTRACT_ENTROPY_SECONDARY);
1571}
1572
1573static unsigned int
1574random_poll(struct file *file, poll_table * wait)
1575{
1576        unsigned int mask;
1577
1578        poll_wait(file, &random_read_wait, wait);
1579        poll_wait(file, &random_write_wait, wait);
1580        mask = 0;
1581        if (random_state->entropy_count >= random_read_wakeup_thresh)
1582                mask |= POLLIN | POLLRDNORM;
1583        if (random_state->entropy_count < random_write_wakeup_thresh)
1584                mask |= POLLOUT | POLLWRNORM;
1585        return mask;
1586}
1587
1588static ssize_t
1589random_write(struct file * file, const char * buffer,
1590             size_t count, loff_t *ppos)
1591{
1592        int             ret = 0;
1593        size_t          bytes;
1594        __u32           buf[16];
1595        const char      *p = buffer;
1596        size_t          c = count;
1597
1598        while (c > 0) {
1599                bytes = min(c, sizeof(buf));
1600
1601                bytes -= copy_from_user(&buf, p, bytes);
1602                if (!bytes) {
1603                        ret = -EFAULT;
1604                        break;
1605                }
1606                c -= bytes;
1607                p += bytes;
1608
1609                add_entropy_words(random_state, buf, (bytes + 3) / 4);
1610        }
1611        if (p == buffer) {
1612                return (ssize_t)ret;
1613        } else {
1614                file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1615                mark_inode_dirty(file->f_dentry->d_inode);
1616                return (ssize_t)(p - buffer);
1617        }
1618}
1619
1620static int
1621random_ioctl(struct inode * inode, struct file * file,
1622             unsigned int cmd, unsigned long arg)
1623{
1624        int *p, size, ent_count;
1625        int retval;
1626        
1627        switch (cmd) {
1628        case RNDGETENTCNT:
1629                ent_count = random_state->entropy_count;
1630                if (put_user(ent_count, (int *) arg))
1631                        return -EFAULT;
1632                return 0;
1633        case RNDADDTOENTCNT:
1634                if (!capable(CAP_SYS_ADMIN))
1635                        return -EPERM;
1636                if (get_user(ent_count, (int *) arg))
1637                        return -EFAULT;
1638                credit_entropy_store(random_state, ent_count);
1639                /*
1640                 * Wake up waiting processes if we have enough
1641                 * entropy.
1642                 */
1643                if (random_state->entropy_count >= random_read_wakeup_thresh)
1644                        wake_up_interruptible(&random_read_wait);
1645                return 0;
1646        case RNDGETPOOL:
1647                if (!capable(CAP_SYS_ADMIN))
1648                        return -EPERM;
1649                p = (int *) arg;
1650                ent_count = random_state->entropy_count;
1651                if (put_user(ent_count, p++) ||
1652                    get_user(size, p) ||
1653                    put_user(random_state->poolinfo.poolwords, p++))
1654                        return -EFAULT;
1655                if (size < 0)
1656                        return -EINVAL;
1657                if (size > random_state->poolinfo.poolwords)
1658                        size = random_state->poolinfo.poolwords;
1659                if (copy_to_user(p, random_state->pool, size * sizeof(__u32)))
1660                        return -EFAULT;
1661                return 0;
1662        case RNDADDENTROPY:
1663                if (!capable(CAP_SYS_ADMIN))
1664                        return -EPERM;
1665                p = (int *) arg;
1666                if (get_user(ent_count, p++))
1667                        return -EFAULT;
1668                if (ent_count < 0)
1669                        return -EINVAL;
1670                if (get_user(size, p++))
1671                        return -EFAULT;
1672                retval = random_write(file, (const char *) p,
1673                                      size, &file->f_pos);
1674                if (retval < 0)
1675                        return retval;
1676                credit_entropy_store(random_state, ent_count);
1677                /*
1678                 * Wake up waiting processes if we have enough
1679                 * entropy.
1680                 */
1681                if (random_state->entropy_count >= random_read_wakeup_thresh)
1682                        wake_up_interruptible(&random_read_wait);
1683                return 0;
1684        case RNDZAPENTCNT:
1685                if (!capable(CAP_SYS_ADMIN))
1686                        return -EPERM;
1687                random_state->entropy_count = 0;
1688                return 0;
1689        case RNDCLEARPOOL:
1690                /* Clear the entropy pool and associated counters. */
1691                if (!capable(CAP_SYS_ADMIN))
1692                        return -EPERM;
1693                clear_entropy_store(random_state);
1694                init_std_data(random_state);
1695                return 0;
1696        default:
1697                return -EINVAL;
1698        }
1699}
1700
1701struct file_operations random_fops = {
1702        read:           random_read,
1703        write:          random_write,
1704        poll:           random_poll,
1705        ioctl:          random_ioctl,
1706};
1707
1708struct file_operations urandom_fops = {
1709        read:           urandom_read,
1710        write:          random_write,
1711        ioctl:          random_ioctl,
1712};
1713
1714/***************************************************************
1715 * Random UUID interface
1716 * 
1717 * Used here for a Boot ID, but can be useful for other kernel 
1718 * drivers.
1719 ***************************************************************/
1720
1721/*
1722 * Generate random UUID
1723 */
1724void generate_random_uuid(unsigned char uuid_out[16])
1725{
1726        get_random_bytes(uuid_out, 16);
1727        /* Set UUID version to 4 --- truely random generation */
1728        uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1729        /* Set the UUID variant to DCE */
1730        uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1731}
1732
1733/********************************************************************
1734 *
1735 * Sysctl interface
1736 *
1737 ********************************************************************/
1738
1739#ifdef CONFIG_SYSCTL
1740
1741#include <linux/sysctl.h>
1742
1743static int sysctl_poolsize;
1744static int min_read_thresh, max_read_thresh;
1745static int min_write_thresh, max_write_thresh;
1746static char sysctl_bootid[16];
1747
1748/*
1749 * This function handles a request from the user to change the pool size 
1750 * of the primary entropy store.
1751 */
1752static int change_poolsize(int poolsize)
1753{
1754        struct entropy_store    *new_store, *old_store;
1755        int                     ret;
1756        
1757        if ((ret = create_entropy_store(poolsize, &new_store)))
1758                return ret;
1759
1760        add_entropy_words(new_store, random_state->pool,
1761                          random_state->poolinfo.poolwords);
1762        credit_entropy_store(new_store, random_state->entropy_count);
1763
1764        sysctl_init_random(new_store);
1765        old_store = random_state;
1766        random_state = batch_tqueue.data = new_store;
1767        free_entropy_store(old_store);
1768        return 0;
1769}
1770
1771static int proc_do_poolsize(ctl_table *table, int write, struct file *filp,
1772                            void *buffer, size_t *lenp)
1773{
1774        int     ret;
1775
1776        sysctl_poolsize = random_state->poolinfo.POOLBYTES;
1777
1778        ret = proc_dointvec(table, write, filp, buffer, lenp);
1779        if (ret || !write ||
1780            (sysctl_poolsize == random_state->poolinfo.POOLBYTES))
1781                return ret;
1782
1783        return change_poolsize(sysctl_poolsize);
1784}
1785
1786static int poolsize_strategy(ctl_table *table, int *name, int nlen,
1787                             void *oldval, size_t *oldlenp,
1788                             void *newval, size_t newlen, void **context)
1789{
1790        int     len;
1791        
1792        sysctl_poolsize = random_state->poolinfo.POOLBYTES;
1793
1794        /*
1795         * We only handle the write case, since the read case gets
1796         * handled by the default handler (and we don't care if the
1797         * write case happens twice; it's harmless).
1798         */
1799        if (newval && newlen) {
1800                len = newlen;
1801                if (len > table->maxlen)
1802                        len = table->maxlen;
1803                if (copy_from_user(table->data, newval, len))
1804                        return -EFAULT;
1805        }
1806
1807        if (sysctl_poolsize != random_state->poolinfo.POOLBYTES)
1808                return change_poolsize(sysctl_poolsize);
1809
1810        return 0;
1811}
1812
1813/*
1814 * These functions is used to return both the bootid UUID, and random
1815 * UUID.  The difference is in whether table->data is NULL; if it is,
1816 * then a new UUID is generated and returned to the user.
1817 * 
1818 * If the user accesses this via the proc interface, it will be returned
1819 * as an ASCII string in the standard UUID format.  If accesses via the 
1820 * sysctl system call, it is returned as 16 bytes of binary data.
1821 */
1822static int proc_do_uuid(ctl_table *table, int write, struct file *filp,
1823                        void *buffer, size_t *lenp)
1824{
1825        ctl_table       fake_table;
1826        unsigned char   buf[64], tmp_uuid[16], *uuid;
1827
1828        uuid = table->data;
1829        if (!uuid) {
1830                uuid = tmp_uuid;
1831                uuid[8] = 0;
1832        }
1833        if (uuid[8] == 0)
1834                generate_random_uuid(uuid);
1835
1836        sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1837                "%02x%02x%02x%02x%02x%02x",
1838                uuid[0],  uuid[1],  uuid[2],  uuid[3],
1839                uuid[4],  uuid[5],  uuid[6],  uuid[7],
1840                uuid[8],  uuid[9],  uuid[10], uuid[11],
1841                uuid[12], uuid[13], uuid[14], uuid[15]);
1842        fake_table.data = buf;
1843        fake_table.maxlen = sizeof(buf);
1844
1845        return proc_dostring(&fake_table, write, filp, buffer, lenp);
1846}
1847
1848static int uuid_strategy(ctl_table *table, int *name, int nlen,
1849                         void *oldval, size_t *oldlenp,
1850                         void *newval, size_t newlen, void **context)
1851{
1852        unsigned char   tmp_uuid[16], *uuid;
1853        unsigned int    len;
1854
1855        if (!oldval || !oldlenp)
1856                return 1;
1857
1858        uuid = table->data;
1859        if (!uuid) {
1860                uuid = tmp_uuid;
1861                uuid[8] = 0;
1862        }
1863        if (uuid[8] == 0)
1864                generate_random_uuid(uuid);
1865
1866        if (get_user(len, oldlenp))
1867                return -EFAULT;
1868        if (len) {
1869                if (len > 16)
1870                        len = 16;
1871                if (copy_to_user(oldval, uuid, len) ||
1872                    put_user(len, oldlenp))
1873                        return -EFAULT;
1874        }
1875        return 1;
1876}
1877
1878ctl_table random_table[] = {
1879        {RANDOM_POOLSIZE, "poolsize",
1880         &sysctl_poolsize, sizeof(int), 0644, NULL,
1881         &proc_do_poolsize, &poolsize_strategy},
1882        {RANDOM_ENTROPY_COUNT, "entropy_avail",
1883         NULL, sizeof(int), 0444, NULL,
1884         &proc_dointvec},
1885        {RANDOM_READ_THRESH, "read_wakeup_threshold",
1886         &random_read_wakeup_thresh, sizeof(int), 0644, NULL,
1887         &proc_dointvec_minmax, &sysctl_intvec, 0,
1888         &min_read_thresh, &max_read_thresh},
1889        {RANDOM_WRITE_THRESH, "write_wakeup_threshold",
1890         &random_write_wakeup_thresh, sizeof(int), 0644, NULL,
1891         &proc_dointvec_minmax, &sysctl_intvec, 0,
1892         &min_write_thresh, &max_write_thresh},
1893        {RANDOM_BOOT_ID, "boot_id",
1894         &sysctl_bootid, 16, 0444, NULL,
1895         &proc_do_uuid, &uuid_strategy},
1896        {RANDOM_UUID, "uuid",
1897         NULL, 16, 0444, NULL,
1898         &proc_do_uuid, &uuid_strategy},
1899        {0}
1900};
1901
1902static void sysctl_init_random(struct entropy_store *random_state)
1903{
1904        min_read_thresh = 8;
1905        min_write_thresh = 0;
1906        max_read_thresh = max_write_thresh = random_state->poolinfo.POOLBITS;
1907        random_table[1].data = &random_state->entropy_count;
1908}
1909#endif  /* CONFIG_SYSCTL */
1910
1911/********************************************************************
1912 *
1913 * Random funtions for networking
1914 *
1915 ********************************************************************/
1916
1917/*
1918 * TCP initial sequence number picking.  This uses the random number
1919 * generator to pick an initial secret value.  This value is hashed
1920 * along with the TCP endpoint information to provide a unique
1921 * starting point for each pair of TCP endpoints.  This defeats
1922 * attacks which rely on guessing the initial TCP sequence number.
1923 * This algorithm was suggested by Steve Bellovin.
1924 *
1925 * Using a very strong hash was taking an appreciable amount of the total
1926 * TCP connection establishment time, so this is a weaker hash,
1927 * compensated for by changing the secret periodically.
1928 */
1929
1930/* F, G and H are basic MD4 functions: selection, majority, parity */
1931#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
1932#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
1933#define H(x, y, z) ((x) ^ (y) ^ (z))
1934
1935/*
1936 * The generic round function.  The application is so specific that
1937 * we don't bother protecting all the arguments with parens, as is generally
1938 * good macro practice, in favor of extra legibility.
1939 * Rotation is separate from addition to prevent recomputation
1940 */
1941#define ROUND(f, a, b, c, d, x, s)      \
1942        (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
1943#define K1 0
1944#define K2 013240474631UL
1945#define K3 015666365641UL
1946
1947/*
1948 * Basic cut-down MD4 transform.  Returns only 32 bits of result.
1949 */
1950static __u32 halfMD4Transform (__u32 const buf[4], __u32 const in[8])
1951{
1952        __u32   a = buf[0], b = buf[1], c = buf[2], d = buf[3];
1953
1954        /* Round 1 */
1955        ROUND(F, a, b, c, d, in[0] + K1,  3);
1956        ROUND(F, d, a, b, c, in[1] + K1,  7);
1957        ROUND(F, c, d, a, b, in[2] + K1, 11);
1958        ROUND(F, b, c, d, a, in[3] + K1, 19);
1959        ROUND(F, a, b, c, d, in[4] + K1,  3);
1960        ROUND(F, d, a, b, c, in[5] + K1,  7);
1961        ROUND(F, c, d, a, b, in[6] + K1, 11);
1962        ROUND(F, b, c, d, a, in[7] + K1, 19);
1963
1964        /* Round 2 */
1965        ROUND(G, a, b, c, d, in[1] + K2,  3);
1966        ROUND(G, d, a, b, c, in[3] + K2,  5);
1967        ROUND(G, c, d, a, b, in[5] + K2,  9);
1968        ROUND(G, b, c, d, a, in[7] + K2, 13);
1969        ROUND(G, a, b, c, d, in[0] + K2,  3);
1970        ROUND(G, d, a, b, c, in[2] + K2,  5);
1971        ROUND(G, c, d, a, b, in[4] + K2,  9);
1972        ROUND(G, b, c, d, a, in[6] + K2, 13);
1973
1974        /* Round 3 */
1975        ROUND(H, a, b, c, d, in[3] + K3,  3);
1976        ROUND(H, d, a, b, c, in[7] + K3,  9);
1977        ROUND(H, c, d, a, b, in[2] + K3, 11);
1978        ROUND(H, b, c, d, a, in[6] + K3, 15);
1979        ROUND(H, a, b, c, d, in[1] + K3,  3);
1980        ROUND(H, d, a, b, c, in[5] + K3,  9);
1981        ROUND(H, c, d, a, b, in[0] + K3, 11);
1982        ROUND(H, b, c, d, a, in[4] + K3, 15);
1983
1984        return buf[1] + b;      /* "most hashed" word */
1985        /* Alternative: return sum of all words? */
1986}
1987
1988#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1989
1990static __u32 twothirdsMD4Transform (__u32 const buf[4], __u32 const in[12])
1991{
1992        __u32   a = buf[0], b = buf[1], c = buf[2], d = buf[3];
1993
1994        /* Round 1 */
1995        ROUND(F, a, b, c, d, in[ 0] + K1,  3);
1996        ROUND(F, d, a, b, c, in[ 1] + K1,  7);
1997        ROUND(F, c, d, a, b, in[ 2] + K1, 11);
1998        ROUND(F, b, c, d, a, in[ 3] + K1, 19);
1999        ROUND(F, a, b, c, d, in[ 4] + K1,  3);
2000        ROUND(F, d, a, b, c, in[ 5] + K1,  7);
2001        ROUND(F, c, d, a, b, in[ 6] + K1, 11);
2002        ROUND(F, b, c, d, a, in[ 7] + K1, 19);
2003        ROUND(F, a, b, c, d, in[ 8] + K1,  3);
2004        ROUND(F, d, a, b, c, in[ 9] + K1,  7);
2005        ROUND(F, c, d, a, b, in[10] + K1, 11);
2006        ROUND(F, b, c, d, a, in[11] + K1, 19);
2007
2008        /* Round 2 */
2009        ROUND(G, a, b, c, d, in[ 1] + K2,  3);
2010        ROUND(G, d, a, b, c, in[ 3] + K2,  5);
2011        ROUND(G, c, d, a, b, in[ 5] + K2,  9);
2012        ROUND(G, b, c, d, a, in[ 7] + K2, 13);
2013        ROUND(G, a, b, c, d, in[ 9] + K2,  3);
2014        ROUND(G, d, a, b, c, in[11] + K2,  5);
2015        ROUND(G, c, d, a, b, in[ 0] + K2,  9);
2016        ROUND(G, b, c, d, a, in[ 2] + K2, 13);
2017        ROUND(G, a, b, c, d, in[ 4] + K2,  3);
2018        ROUND(G, d, a, b, c, in[ 6] + K2,  5);
2019        ROUND(G, c, d, a, b, in[ 8] + K2,  9);
2020        ROUND(G, b, c, d, a, in[10] + K2, 13);
2021
2022        /* Round 3 */
2023        ROUND(H, a, b, c, d, in[ 3] + K3,  3);
2024        ROUND(H, d, a, b, c, in[ 7] + K3,  9);
2025        ROUND(H, c, d, a, b, in[11] + K3, 11);
2026        ROUND(H, b, c, d, a, in[ 2] + K3, 15);
2027        ROUND(H, a, b, c, d, in[ 6] + K3,  3);
2028        ROUND(H, d, a, b, c, in[10] + K3,  9);
2029        ROUND(H, c, d, a, b, in[ 1] + K3, 11);
2030        ROUND(H, b, c, d, a, in[ 5] + K3, 15);
2031        ROUND(H, a, b, c, d, in[ 9] + K3,  3);
2032        ROUND(H, d, a, b, c, in[ 0] + K3,  9);
2033        ROUND(H, c, d, a, b, in[ 4] + K3, 11);
2034        ROUND(H, b, c, d, a, in[ 8] + K3, 15);
2035
2036        return buf[1] + b;      /* "most hashed" word */
2037        /* Alternative: return sum of all words? */
2038}
2039#endif
2040
2041#undef ROUND
2042#undef F
2043#undef G
2044#undef H
2045#undef K1
2046#undef K2
2047#undef K3
2048
2049/* This should not be decreased so low that ISNs wrap too fast. */
2050#define REKEY_INTERVAL  300
2051/*
2052 * Bit layout of the tcp sequence numbers (before adding current time):
2053 * bit 24-31: increased after every key exchange
2054 * bit 0-23: hash(source,dest)
2055 *
2056 * The implementation is similar to the algorithm described
2057 * in the Appendix of RFC 1185, except that
2058 * - it uses a 1 MHz clock instead of a 250 kHz clock
2059 * - it performs a rekey every 5 minutes, which is equivalent
2060 *      to a (source,dest) tulple dependent forward jump of the
2061 *      clock by 0..2^(HASH_BITS+1)
2062 *
2063 * Thus the average ISN wraparound time is 68 minutes instead of
2064 * 4.55 hours.
2065 *
2066 * SMP cleanup and lock avoidance with poor man's RCU.
2067 *                      Manfred Spraul <manfred@colorfullife.com>
2068 *              
2069 */
2070#define COUNT_BITS      8
2071#define COUNT_MASK      ( (1<<COUNT_BITS)-1)
2072#define HASH_BITS       24
2073#define HASH_MASK       ( (1<<HASH_BITS)-1 )
2074
2075static struct keydata {
2076        time_t rekey_time;
2077        __u32   count;          // already shifted to the final position
2078        __u32   secret[12];
2079} ____cacheline_aligned ip_keydata[2];
2080
2081static spinlock_t ip_lock = SPIN_LOCK_UNLOCKED;
2082static unsigned int ip_cnt;
2083
2084static struct keydata *__check_and_rekey(time_t time)
2085{
2086        struct keydata *keyptr;
2087        spin_lock_bh(&ip_lock);
2088        keyptr = &ip_keydata[ip_cnt&1];
2089        if (!keyptr->rekey_time || (time - keyptr->rekey_time) > REKEY_INTERVAL) {
2090                keyptr = &ip_keydata[1^(ip_cnt&1)];
2091                keyptr->rekey_time = time;
2092                get_random_bytes(keyptr->secret, sizeof(keyptr->secret));
2093                keyptr->count = (ip_cnt&COUNT_MASK)<<HASH_BITS;
2094                mb();
2095                ip_cnt++;
2096        }
2097        spin_unlock_bh(&ip_lock);
2098        return keyptr;
2099}
2100
2101static inline struct keydata *check_and_rekey(time_t time)
2102{
2103        struct keydata *keyptr = &ip_keydata[ip_cnt&1];
2104
2105        rmb();
2106        if (!keyptr->rekey_time || (time - keyptr->rekey_time) > REKEY_INTERVAL) {
2107                keyptr = __check_and_rekey(time);
2108        }
2109
2110        return keyptr;
2111}
2112
2113#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2114__u32 secure_tcpv6_sequence_number(__u32 *saddr, __u32 *daddr,
2115                                   __u16 sport, __u16 dport)
2116{
2117        struct timeval  tv;
2118        __u32           seq;
2119        __u32           hash[12];
2120        struct keydata *keyptr;
2121
2122        /* The procedure is the same as for IPv4, but addresses are longer.
2123         * Thus we must use twothirdsMD4Transform.
2124         */
2125
2126        do_gettimeofday(&tv);   /* We need the usecs below... */
2127        keyptr = check_and_rekey(tv.tv_sec);
2128
2129        memcpy(hash, saddr, 16);
2130        hash[4]=(sport << 16) + dport;
2131        memcpy(&hash[5],keyptr->secret,sizeof(__u32)*7);
2132
2133        seq = twothirdsMD4Transform(daddr, hash) & HASH_MASK;
2134        seq += keyptr->count;
2135        seq += tv.tv_usec + tv.tv_sec*1000000;
2136
2137        return seq;
2138}
2139
2140__u32 secure_ipv6_id(__u32 *daddr)
2141{
2142        struct keydata *keyptr;
2143
2144        keyptr = check_and_rekey(CURRENT_TIME);
2145
2146        return halfMD4Transform(daddr, keyptr->secret);
2147}
2148
2149#endif
2150
2151
2152__u32 secure_tcp_sequence_number(__u32 saddr, __u32 daddr,
2153                                 __u16 sport, __u16 dport)
2154{
2155        struct timeval  tv;
2156        __u32           seq;
2157        __u32   hash[4];
2158        struct keydata *keyptr;
2159
2160        /*
2161         * Pick a random secret every REKEY_INTERVAL seconds.
2162         */
2163        do_gettimeofday(&tv);   /* We need the usecs below... */
2164        keyptr = check_and_rekey(tv.tv_sec);
2165
2166        /*
2167         *  Pick a unique starting offset for each TCP connection endpoints
2168         *  (saddr, daddr, sport, dport).
2169         *  Note that the words are placed into the starting vector, which is 
2170         *  then mixed with a partial MD4 over random data.
2171         */
2172        hash[0]=saddr;
2173        hash[1]=daddr;
2174        hash[2]=(sport << 16) + dport;
2175        hash[3]=keyptr->secret[11];
2176
2177        seq = halfMD4Transform(hash, keyptr->secret) & HASH_MASK;
2178        seq += keyptr->count;
2179        /*
2180         *      As close as possible to RFC 793, which
2181         *      suggests using a 250 kHz clock.
2182         *      Further reading shows this assumes 2 Mb/s networks.
2183         *      For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
2184         *      That's funny, Linux has one built in!  Use it!
2185         *      (Networks are faster now - should this be increased?)
2186         */
2187        seq += tv.tv_usec + tv.tv_sec*1000000;
2188#if 0
2189        printk("init_seq(%lx, %lx, %d, %d) = %d\n",
2190               saddr, daddr, sport, dport, seq);
2191#endif
2192        return seq;
2193}
2194
2195/*  The code below is shamelessly stolen from secure_tcp_sequence_number().
2196 *  All blames to Andrey V. Savochkin <saw@msu.ru>.
2197 */
2198__u32 secure_ip_id(__u32 daddr)
2199{
2200        struct keydata *keyptr;
2201        __u32 hash[4];
2202
2203        keyptr = check_and_rekey(CURRENT_TIME);
2204
2205        /*
2206         *  Pick a unique starting offset for each IP destination.
2207         *  The dest ip address is placed in the starting vector,
2208         *  which is then hashed with random data.
2209         */
2210        hash[0] = daddr;
2211        hash[1] = keyptr->secret[9];
2212        hash[2] = keyptr->secret[10];
2213        hash[3] = keyptr->secret[11];
2214
2215        return halfMD4Transform(hash, keyptr->secret);
2216}
2217
2218#ifdef CONFIG_SYN_COOKIES
2219/*
2220 * Secure SYN cookie computation. This is the algorithm worked out by
2221 * Dan Bernstein and Eric Schenk.
2222 *
2223 * For linux I implement the 1 minute counter by looking at the jiffies clock.
2224 * The count is passed in as a parameter, so this code doesn't much care.
2225 */
2226
2227#define COOKIEBITS 24   /* Upper bits store count */
2228#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
2229
2230static int      syncookie_init;
2231static __u32    syncookie_secret[2][16-3+HASH_BUFFER_SIZE];
2232
2233__u32 secure_tcp_syn_cookie(__u32 saddr, __u32 daddr, __u16 sport,
2234                __u16 dport, __u32 sseq, __u32 count, __u32 data)
2235{
2236        __u32   tmp[16 + HASH_BUFFER_SIZE + HASH_EXTRA_SIZE];
2237        __u32   seq;
2238
2239        /*
2240         * Pick two random secrets the first time we need a cookie.
2241         */
2242        if (syncookie_init == 0) {
2243                get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
2244                syncookie_init = 1;
2245        }
2246
2247        /*
2248         * Compute the secure sequence number.
2249         * The output should be:
2250         *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
2251         *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
2252         * Where sseq is their sequence number and count increases every
2253         * minute by 1.
2254         * As an extra hack, we add a small "data" value that encodes the
2255         * MSS into the second hash value.
2256         */
2257
2258        memcpy(tmp+3, syncookie_secret[0], sizeof(syncookie_secret[0]));
2259        tmp[0]=saddr;
2260        tmp[1]=daddr;
2261        tmp[2]=(sport << 16) + dport;
2262        HASH_TRANSFORM(tmp+16, tmp);
2263        seq = tmp[17] + sseq + (count << COOKIEBITS);
2264
2265        memcpy(tmp+3, syncookie_secret[1], sizeof(syncookie_secret[1]));
2266        tmp[0]=saddr;
2267        tmp[1]=daddr;
2268        tmp[2]=(sport << 16) + dport;
2269        tmp[3] = count; /* minute counter */
2270        HASH_TRANSFORM(tmp+16, tmp);
2271
2272        /* Add in the second hash and the data */
2273        return seq + ((tmp[17] + data) & COOKIEMASK);
2274}
2275
2276/*
2277 * This retrieves the small "data" value from the syncookie.
2278 * If the syncookie is bad, the data returned will be out of
2279 * range.  This must be checked by the caller.
2280 *
2281 * The count value used to generate the cookie must be within
2282 * "maxdiff" if the current (passed-in) "count".  The return value
2283 * is (__u32)-1 if this test fails.
2284 */
2285__u32 check_tcp_syn_cookie(__u32 cookie, __u32 saddr, __u32 daddr, __u16 sport,
2286                __u16 dport, __u32 sseq, __u32 count, __u32 maxdiff)
2287{
2288        __u32   tmp[16 + HASH_BUFFER_SIZE + HASH_EXTRA_SIZE];
2289        __u32   diff;
2290
2291        if (syncookie_init == 0)
2292                return (__u32)-1;       /* Well, duh! */
2293
2294        /* Strip away the layers from the cookie */
2295        memcpy(tmp+3, syncookie_secret[0], sizeof(syncookie_secret[0]));
2296        tmp[0]=saddr;
2297        tmp[1]=daddr;
2298        tmp[2]=(sport << 16) + dport;
2299        HASH_TRANSFORM(tmp+16, tmp);
2300        cookie -= tmp[17] + sseq;
2301        /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
2302
2303        diff = (count - (cookie >> COOKIEBITS)) & ((__u32)-1 >> COOKIEBITS);
2304        if (diff >= maxdiff)
2305                return (__u32)-1;
2306
2307        memcpy(tmp+3, syncookie_secret[1], sizeof(syncookie_secret[1]));
2308        tmp[0] = saddr;
2309        tmp[1] = daddr;
2310        tmp[2] = (sport << 16) + dport;
2311        tmp[3] = count - diff;  /* minute counter */
2312        HASH_TRANSFORM(tmp+16, tmp);
2313
2314        return (cookie - tmp[17]) & COOKIEMASK; /* Leaving the data behind */
2315}
2316#endif
2317
2318
2319
2320#ifndef CONFIG_ARCH_S390
2321EXPORT_SYMBOL(add_keyboard_randomness);
2322EXPORT_SYMBOL(add_mouse_randomness);
2323EXPORT_SYMBOL(add_interrupt_randomness);
2324#endif
2325EXPORT_SYMBOL(add_blkdev_randomness);
2326EXPORT_SYMBOL(batch_entropy_store);
2327EXPORT_SYMBOL(generate_random_uuid);
2328
2329
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.