Main Page   Reference Manual   Compound List   File List  

libecc/rng.h

Go to the documentation of this file.
00001 //
00006 //
00007 // This file is part of the libecc package.
00008 // Copyright (C) 2002, by
00009 //
00010 // Carlo Wood, Run on IRC <carlo@alinoe.com>
00011 // RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
00012 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
00013 //
00014 // This program is free software; you can redistribute it and/or
00015 // modify it under the terms of the GNU General Public License
00016 // as published by the Free Software Foundation; either version 2
00017 // of the License, or (at your option) any later version.
00018 //
00019 // This program is distributed in the hope that it will be useful,
00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 // GNU General Public License for more details.
00023 //
00024 // You should have received a copy of the GNU General Public License
00025 // along with this program; if not, write to the Free Software
00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 //
00028 
00029 #ifndef LIBECC_RNG_H
00030 #define LIBECC_RNG_H
00031 
00032 #include <libecc/bitset.h>
00033 
00034 namespacelibecc {
00035 
00036 classrng {
00037   public:
00038     static unsigned int const S_pool_size = 521;                
00039 
00040   private:
00041     uint32_t M_pool[S_pool_size / 32 + 1];
00042     bitset<512> M_out;
00043     unsigned int M_out_cnt;
00044 
00045     classbit_iterator {
00046       private:
00047         uint32_t* M_ptr;
00048         uint32_t M_head_mask;
00049       public:
00050         bit_iterator(uint32_t* pool, int bit) : M_ptr(pool + bit / 32), M_head_mask(1 << bit % 32) { }
00051         void toggle(void) const{ *M_ptr ^= M_head_mask; }
00052         void set(void) { *M_ptr |= M_head_mask; }
00053         void clear(void) { *M_ptr &= ~M_head_mask; }
00054         uint32_t increment_and_test(uint32_t* pool)
00055         {
00056           M_head_mask <<= 1;
00057           if (M_head_mask == 0)
00058           {
00059             M_head_mask = 1;
00060             ++M_ptr;
00061           }
00062           else if (M_head_mask == (1 << (S_pool_size % 32)) && M_ptr - pool == S_pool_size / 32)
00063           {
00064             M_ptr = pool;
00065             M_head_mask = 1;
00066           }
00067           return (*M_ptr & M_head_mask);
00068         }
00069     };
00070 
00071     uint32_t* M_entropy_ptr;
00072     uint32_t const* M_entropy_ptr_end;
00073 
00074     bit_iterator M_head;
00075     bit_iterator M_fbp1;
00076     bit_iterator M_fbp2;
00077     bit_iterator M_fbp3;
00078     bit_iterator M_fbp4;
00079     bit_iterator M_fbp5;
00080     bit_iterator M_fbp6;
00081     bit_iterator M_fbp7;
00082     bit_iterator M_fbp8;
00083     bit_iterator M_fbp9;
00084    
00085   public:
00089     typedef bitset<S_pool_size> pool_type;
00090     rng(pool_type const& seed);
00091 
00092     void generate_512_bits(void);
00094     bitset<512> const& get_512_bits(void) const{ return M_out; }
00095 
00096     void add_entropy(uint32_t const* noise, unsigned int number_of_ints);
00097 
00103     template<unsigned int n>
00104       void add_entropy(bitset<n> const noise)
00105       {
00106         add_entropy(reinterpret_cast<uint32_t const*>(noise.digits_ptr()), bitset<n>::digits * sizeof(bitset_digit_t) / 4);
00107       }
00108 };
00109 
00110 } // namespace libecc
00111 
00112 #endif // LIBECC_RNG_H
Copyright © 2002-2008 Carlo Wood.  All rights reserved.