Sophie

Sophie

distrib > Mandriva > 10.2 > i586 > media > contrib > by-pkgid > 7457b841ac8136d3a1a9d3d960c5252e > files > 1383

libcryptopp-doc-5.2.1-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Crypto++: rng.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.7 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>rng.cpp</h1><pre class="fragment"><div>00001 <span class="comment">// rng.cpp - written and placed in the public domain by Wei Dai</span>
00002 
00003 <span class="preprocessor">#include "pch.h"</span>
00004 
00005 <span class="preprocessor">#include "rng.h"</span>
00006 
00007 <span class="preprocessor">#include &lt;time.h&gt;</span>
00008 <span class="preprocessor">#include &lt;math.h&gt;</span>
00009 
00010 NAMESPACE_BEGIN(CryptoPP)
00011 
00012 <span class="comment">// linear congruential generator</span>
00013 <span class="comment">// originally by William S. England</span>
00014 
00015 <span class="comment">// do not use for cryptographic purposes</span>
00016 
00017 <span class="comment">/*</span>
00018 <span class="comment">** Original_numbers are the original published m and q in the</span>
00019 <span class="comment">** ACM article above.  John Burton has furnished numbers for</span>
00020 <span class="comment">** a reportedly better generator.  The new numbers are now</span>
00021 <span class="comment">** used in this program by default.</span>
00022 <span class="comment">*/</span>
00023 
00024 #ifndef LCRNG_ORIGINAL_NUMBERS
00025 <span class="keyword">const</span> word32 LC_RNG::m=2147483647L;
00026 <span class="keyword">const</span> word32 LC_RNG::q=44488L;
00027 
00028 <span class="keyword">const</span> word16 LC_RNG::a=(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)48271L;
00029 <span class="keyword">const</span> word16 LC_RNG::r=3399;
00030 <span class="preprocessor">#else</span>
00031 <span class="preprocessor"></span><span class="keyword">const</span> word32 LC_RNG::m=2147483647L;
00032 <span class="keyword">const</span> word32 LC_RNG::q=127773L;
00033 
00034 <span class="keyword">const</span> word16 LC_RNG::a=16807;
00035 <span class="keyword">const</span> word16 LC_RNG::r=2836;
00036 <span class="preprocessor">#endif</span>
00037 <span class="preprocessor"></span>
<a name="l00038"></a><a class="code" href="class_l_c___r_n_g.html#_l_c___r_n_ga1">00038</a> byte <a class="code" href="class_l_c___r_n_g.html#_l_c___r_n_ga1">LC_RNG::GenerateByte</a>()
00039 {
00040         word32 hi = seed/q;
00041         word32 lo = seed%q;
00042 
00043         <span class="keywordtype">long</span> test = a*lo - r*hi;
00044 
00045         <span class="keywordflow">if</span> (test &gt; 0)
00046                 seed = test;
00047         <span class="keywordflow">else</span>
00048                 seed = test+ m;
00049 
00050         <span class="keywordflow">return</span> (GETBYTE(seed, 0) ^ GETBYTE(seed, 1) ^ GETBYTE(seed, 2) ^ GETBYTE(seed, 3));
00051 }
00052 
00053 <span class="comment">// ********************************************************</span>
00054 
00055 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>
00056 <span class="preprocessor"></span>
00057 X917RNG::X917RNG(<a class="code" href="class_block_transformation.html">BlockTransformation</a> *c, <span class="keyword">const</span> byte *seed, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> deterministicTimeVector)
00058         : cipher(c),
00059           S(cipher-&gt;BlockSize()),
00060           dtbuf(S),
00061           randseed(seed, S),
00062           randbuf(S),
00063           randbuf_counter(0),
00064           m_deterministicTimeVector(deterministicTimeVector)
00065 {
00066         <span class="keywordflow">if</span> (m_deterministicTimeVector)
00067         {
00068                 memset(dtbuf, 0, S);
00069                 memcpy(dtbuf, (byte *)&amp;m_deterministicTimeVector, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(m_deterministicTimeVector), S));
00070         }
00071         <span class="keywordflow">else</span>
00072         {
00073                 time_t tstamp1 = time(0);
00074                 xorbuf(dtbuf, (byte *)&amp;tstamp1, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp1), S));
00075                 cipher-&gt;ProcessBlock(dtbuf);
00076                 clock_t tstamp2 = clock();
00077                 xorbuf(dtbuf, (byte *)&amp;tstamp2, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp2), S));
00078                 cipher-&gt;ProcessBlock(dtbuf);
00079         }
00080 }
00081 
<a name="l00082"></a><a class="code" href="class_x917_r_n_g.html#_x917_r_n_ga1">00082</a> byte <a class="code" href="class_x917_r_n_g.html#_x917_r_n_ga1">X917RNG::GenerateByte</a>()
00083 {
00084         <span class="keywordflow">if</span> (randbuf_counter==0)
00085         {
00086                 <span class="comment">// calculate new enciphered timestamp</span>
00087                 <span class="keywordflow">if</span> (m_deterministicTimeVector)
00088                 {
00089                         xorbuf(dtbuf, (byte *)&amp;m_deterministicTimeVector, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(m_deterministicTimeVector), S));
00090                         <span class="keywordflow">while</span> (++m_deterministicTimeVector == 0) {}     <span class="comment">// skip 0</span>
00091                 }
00092                 <span class="keywordflow">else</span>
00093                 {
00094                         clock_t tstamp = clock();
00095                         xorbuf(dtbuf, (byte *)&amp;tstamp, STDMIN((<span class="keywordtype">int</span>)<span class="keyword">sizeof</span>(tstamp), S));
00096                 }
00097                 cipher-&gt;ProcessBlock(dtbuf);
00098 
00099                 <span class="comment">// combine enciphered timestamp with seed</span>
00100                 xorbuf(randseed, dtbuf, S);
00101 
00102                 <span class="comment">// generate a new block of random bytes</span>
00103                 cipher-&gt;ProcessBlock(randseed, randbuf);
00104 
00105                 <span class="comment">// compute new seed vector</span>
00106                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;S; i++)
00107                         randseed[i] = randbuf[i] ^ dtbuf[i];
00108                 cipher-&gt;ProcessBlock(randseed);
00109 
00110                 randbuf_counter=S;
00111         }
00112         <span class="keywordflow">return</span>(randbuf[--randbuf_counter]);
00113 }
00114 
00115 <span class="preprocessor">#endif</span>
00116 <span class="preprocessor"></span>
00117 MaurerRandomnessTest::MaurerRandomnessTest()
00118         : sum(0.0), n(0)
00119 {
00120         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;V; i++)
00121                 tab[i] = 0;
00122 }
00123 
<a name="l00124"></a><a class="code" href="class_maurer_randomness_test.html#_maurer_randomness_testa1">00124</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_maurer_randomness_test.html#_maurer_randomness_testa1">MaurerRandomnessTest::Put2</a>(<span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">int</span> messageEnd, <span class="keywordtype">bool</span> blocking)
00125 {
00126         <span class="keywordflow">while</span> (length--)
00127         {
00128                 byte inByte = *inString++;
00129                 <span class="keywordflow">if</span> (n &gt;= Q)
00130                         sum += log(<span class="keywordtype">double</span>(n - tab[inByte]));
00131                 tab[inByte] = n;
00132                 n++;
00133         }
00134         <span class="keywordflow">return</span> 0;
00135 }
00136 
00137 <span class="keywordtype">double</span> MaurerRandomnessTest::GetTestValue()<span class="keyword"> const</span>
00138 <span class="keyword"></span>{
00139         <span class="keywordflow">if</span> (BytesNeeded() &gt; 0)
00140                 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"MaurerRandomnessTest: "</span> + IntToString(BytesNeeded()) + <span class="stringliteral">" more bytes of input needed"</span>);
00141 
00142         <span class="keywordtype">double</span> fTu = (sum/(n-Q))/log(2.0);      <span class="comment">// this is the test value defined by Maurer</span>
00143 
00144         <span class="keywordtype">double</span> value = fTu * 0.1392;            <span class="comment">// arbitrarily normalize it to</span>
00145         <span class="keywordflow">return</span> value &gt; 1.0 ? 1.0 : value;       <span class="comment">// a number between 0 and 1</span>
00146 }
00147 
00148 NAMESPACE_END
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Sun Nov 7 08:23:59 2004 for Crypto++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address>
</body>
</html>