summaryrefslogtreecommitdiff
path: root/doc/html/_modules/M2Crypto/Rand.html
blob: b2fa0750f649170bb79c9603e4cd62062a687a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>M2Crypto.Rand &#8212; M2Crypto  documentation</title>
    <link rel="stylesheet" href="../../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
   
  <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for M2Crypto.Rand</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;M2Crypto wrapper for OpenSSL PRNG. Requires OpenSSL 0.9.5 and above.</span>

<span class="sd">Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved.</span>
<span class="sd">Copyright (c) 2014-2017 Matej Cepl. All rights reserved.</span>

<span class="sd">See LICENCE for the license information.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="kn">from</span> <span class="nn">M2Crypto</span> <span class="k">import</span> <span class="n">m2</span><span class="p">,</span> <span class="n">py27plus</span><span class="p">,</span> <span class="n">six</span>
<span class="k">if</span> <span class="n">py27plus</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">AnyStr</span><span class="p">,</span> <span class="n">Tuple</span>  <span class="c1"># noqa</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;rand_seed&#39;</span><span class="p">,</span> <span class="s1">&#39;rand_add&#39;</span><span class="p">,</span> <span class="s1">&#39;load_file&#39;</span><span class="p">,</span> <span class="s1">&#39;save_file&#39;</span><span class="p">,</span> <span class="s1">&#39;rand_bytes&#39;</span><span class="p">,</span>
           <span class="s1">&#39;rand_pseudo_bytes&#39;</span><span class="p">,</span> <span class="s1">&#39;rand_file_name&#39;</span><span class="p">,</span> <span class="s1">&#39;rand_status&#39;</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">RandError</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="n">m2</span><span class="o">.</span><span class="n">rand_init</span><span class="p">(</span><span class="n">RandError</span><span class="p">)</span>


<div class="viewcode-block" id="rand_add"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_add">[docs]</a><span class="k">def</span> <span class="nf">rand_add</span><span class="p">(</span><span class="n">blob</span><span class="p">,</span> <span class="n">entropy</span><span class="p">):</span>
    <span class="c1"># type: (bytes, float) -&gt; None</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Mixes blob into the PRNG state.</span>

<span class="sd">    :param blob: added data</span>
<span class="sd">    :param entropy: (the lower bound of) an estimate of how much randomness</span>
<span class="sd">        is contained in blob, measured in bytes.</span>

<span class="sd">    Thus, if the data at buf are unpredictable to an adversary, this</span>
<span class="sd">    increases the uncertainty about the state and makes the PRNG output less</span>
<span class="sd">    predictable. Suitable input comes from user interaction (random key</span>
<span class="sd">    presses, mouse movements) and certain hardware events.</span>

<span class="sd">    Details about sources of randomness and how to estimate their entropy</span>
<span class="sd">    can be found in the literature, e.g. RFC 1750.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">m2</span><span class="o">.</span><span class="n">rand_add</span><span class="p">(</span><span class="n">blob</span><span class="p">,</span> <span class="n">entropy</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="rand_seed"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_seed">[docs]</a><span class="k">def</span> <span class="nf">rand_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">):</span>
    <span class="c1"># type: (bytes) -&gt; None</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Equivalent to rand_add() when len(seed) == entropy.</span>

<span class="sd">    :param seed: added data (see description at rand_add)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">m2</span><span class="o">.</span><span class="n">rand_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="rand_status"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_status">[docs]</a><span class="k">def</span> <span class="nf">rand_status</span><span class="p">():</span>
    <span class="c1"># type: () -&gt; int</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Check whether there is enough entropy in PRNG.</span>

<span class="sd">    :return: 1 if the PRNG has been seeded with enough</span>
<span class="sd">       data, 0 otherwise.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">rand_status</span><span class="p">()</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="rand_file_name"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_file_name">[docs]</a><span class="k">def</span> <span class="nf">rand_file_name</span><span class="p">():</span>
    <span class="c1"># type: () -&gt; str</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generate a default path for the random seed file.</span>

<span class="sd">    :return: string with the filename.</span>
<span class="sd">       The seed file is $RANDFILE if that environment variable</span>
<span class="sd">       is set, $HOME/.rnd otherwise. If $HOME is not set either,</span>
<span class="sd">       an error occurs.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">six</span><span class="o">.</span><span class="n">ensure_text</span><span class="p">(</span><span class="n">m2</span><span class="o">.</span><span class="n">rand_file_name</span><span class="p">())</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="load_file"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.load_file">[docs]</a><span class="k">def</span> <span class="nf">load_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">max_bytes</span><span class="p">):</span>
    <span class="c1"># type: (AnyStr, int) -&gt; int</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read a number of bytes from file filename and adds them to the PRNG.</span>

<span class="sd">    If max_bytes is non-negative, up to to max_bytes are read; starting with</span>
<span class="sd">    OpenSSL 0.9.5, if max_bytes is -1, the complete file is read.</span>

<span class="sd">    :param filename:</span>
<span class="sd">    :param max_bytes:</span>
<span class="sd">    :return: the number of bytes read.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">rand_load_file</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">ensure_str</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span> <span class="n">max_bytes</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="save_file"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.save_file">[docs]</a><span class="k">def</span> <span class="nf">save_file</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="c1"># type: (AnyStr) -&gt; int</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Write a number of random bytes (currently 1024) to file.</span>

<span class="sd">    The file then can be used to initialize the PRNG by calling load_file() in</span>
<span class="sd">    a later session.</span>

<span class="sd">    :param filename:</span>
<span class="sd">    :return: returns the number of bytes written, and -1 if the bytes</span>
<span class="sd">         written were generated without appropriate seed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">rand_save_file</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="rand_bytes"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_bytes">[docs]</a><span class="k">def</span> <span class="nf">rand_bytes</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; bytes</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return n cryptographically strong pseudo-random bytes.</span>

<span class="sd">    An error occurs if the PRNG has not been seeded with enough randomness</span>
<span class="sd">    to ensure an unpredictable byte sequence.</span>

<span class="sd">    :param num: number of bytes to be returned</span>
<span class="sd">    :return: random bytes</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">rand_bytes</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>


<div class="viewcode-block" id="rand_pseudo_bytes"><a class="viewcode-back" href="../../M2Crypto.html#M2Crypto.Rand.rand_pseudo_bytes">[docs]</a><span class="k">def</span> <span class="nf">rand_pseudo_bytes</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
    <span class="c1"># type: (int) -&gt; Tuple[bytes, int]</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return num pseudo-random bytes into buf.</span>

<span class="sd">    Pseudo-random byte sequences generated by this method will be unique</span>
<span class="sd">    if they are of sufficient length, but are not necessarily</span>
<span class="sd">    unpredictable. They can be used for non-cryptographic purposes and for</span>
<span class="sd">    certain purposes in cryptographic protocols, but usually not for key</span>
<span class="sd">    generation etc.</span>

<span class="sd">    Output of the function is mixed into the entropy pool before</span>
<span class="sd">    retrieving the new pseudo-random bytes unless disabled at compile</span>
<span class="sd">    time (see FAQ).</span>

<span class="sd">    :param num: number of bytes to be returned</span>
<span class="sd">    :return: random bytes</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">warnings</span>
    <span class="k">if</span> <span class="n">m2</span><span class="o">.</span><span class="n">OPENSSL_VERSION_NUMBER</span> <span class="o">&gt;=</span> <span class="mh">0x10100000</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;The underlying OpenSSL method has been &#39;</span> <span class="o">+</span>
                      <span class="s1">&#39;deprecated. Use Rand.rand_bytes instead.&#39;</span><span class="p">,</span>
                      <span class="ne">DeprecationWarning</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">m2</span><span class="o">.</span><span class="n">rand_pseudo_bytes</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>  <span class="c1"># pylint: disable=no-member</span></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../index.html">M2Crypto</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../M2Crypto.html">M2Crypto Package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../index.html">Documentation overview</a><ul>
  <li><a href="../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, Matej Cepl <mcepl@cepl.eu>.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>