Initialize Tizen 2.3
[external/nettle.git] / nettle.html
1    <html lang="en">
2 <head>
3 <title>Nettle: a low-level cryptographic library</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5 <meta name="description" content="Nettle: a low-level cryptographic library">
6 <meta name="generator" content="makeinfo 4.6">
7 <!--
8 This manual is for the Nettle library (version 2.1), a
9 low-level cryptographic library.
10
11    <p>Originally written 2001 by Niels Möller, updated 2010.
12
13    <blockquote>
14 This manual is placed in the public domain. You may freely copy it, in
15 whole or in part, with or without modification. Attribution is
16 appreciated, but not required. 
17 </blockquote>
18    -->
19 <meta http-equiv="Content-Style-Type" content="text/css">
20 <style type="text/css"><!--
21   pre.display { font-family:inherit }
22   pre.format  { font-family:inherit }
23   pre.smalldisplay { font-family:inherit; font-size:smaller }
24   pre.smallformat  { font-family:inherit; font-size:smaller }
25   pre.smallexample { font-size:smaller }
26   pre.smalllisp    { font-size:smaller }
27 --></style>
28 </head>
29 <body>
30 <h1 class="settitle">Nettle: a low-level cryptographic library</h1>
31
32 <div class="contents">
33 <h2>Table of Contents</h2>
34 <ul>
35 <li><a name="toc_Top" href="#Top">Nettle</a>
36 <li><a name="toc_Introduction" href="#Introduction">Introduction</a>
37 <li><a name="toc_Copyright" href="#Copyright">Copyright</a>
38 <li><a name="toc_Conventions" href="#Conventions">Conventions</a>
39 <li><a name="toc_Example" href="#Example">Example</a>
40 <li><a name="toc_Linking" href="#Linking">Linking</a>
41 <li><a name="toc_Reference" href="#Reference">Reference</a>
42 <ul>
43 <li><a href="#Hash%20functions">Hash functions</a>
44 <ul>
45 <li><a href="#Hash%20functions"><small>MD5</small></a>
46 <li><a href="#Hash%20functions"><small>MD2</small></a>
47 <li><a href="#Hash%20functions"><small>MD4</small></a>
48 <li><a href="#Hash%20functions"><small>SHA1</small></a>
49 <li><a href="#Hash%20functions"><small>SHA256</small></a>
50 <li><a href="#Hash%20functions"><small>SHA224</small></a>
51 <li><a href="#Hash%20functions"><small>SHA512</small></a>
52 <li><a href="#Hash%20functions"><small>SHA384</small></a>
53 <li><a href="#Hash%20functions"><code>struct nettle_hash</code></a>
54 </li></ul>
55 <li><a href="#Cipher%20functions">Cipher functions</a>
56 <ul>
57 <li><a href="#Cipher%20functions">AES</a>
58 <li><a href="#Cipher%20functions">ARCFOUR</a>
59 <li><a href="#Cipher%20functions">ARCTWO</a>
60 <li><a href="#Cipher%20functions">BLOWFISH</a>
61 <li><a href="#Cipher%20functions">Camellia</a>
62 <li><a href="#Cipher%20functions">CAST128</a>
63 <li><a href="#Cipher%20functions">DES</a>
64 <li><a href="#Cipher%20functions">DES3</a>
65 <li><a href="#Cipher%20functions">SERPENT</a>
66 <li><a href="#Cipher%20functions">TWOFISH</a>
67 <li><a href="#Cipher%20functions"><code>struct nettle_cipher</code></a>
68 </li></ul>
69 <li><a href="#Cipher%20modes">Cipher modes</a>
70 <ul>
71 <li><a href="#Cipher%20modes">Cipher Block Chaining</a>
72 <li><a href="#Cipher%20modes">Counter mode</a>
73 </li></ul>
74 <li><a href="#Keyed%20hash%20functions">Keyed Hash Functions</a>
75 <ul>
76 <li><a href="#Keyed%20hash%20functions"><small>HMAC</small></a>
77 <li><a href="#Keyed%20hash%20functions">Concrete <small>HMAC</small> functions</a>
78 <ul>
79 <li><a href="#Keyed%20hash%20functions"><small>HMAC-MD5</small></a>
80 <li><a href="#Keyed%20hash%20functions"><small>HMAC-SHA1</small></a>
81 <li><a href="#Keyed%20hash%20functions"><small>HMAC-SHA256</small></a>
82 <li><a href="#Keyed%20hash%20functions"><small>HMAC-SHA512</small></a>
83 </li></ul>
84 </li></ul>
85 <li><a href="#Public-key%20algorithms">Public-key algorithms</a>
86 <ul>
87 <li><a href="#RSA"><small>RSA</small></a>
88 <li><a href="#RSA">Nettle's <small>RSA</small> support</a>
89 <li><a href="#DSA">Nettle's <small>DSA</small> support</a>
90 </li></ul>
91 <li><a href="#Randomness">Randomness</a>
92 <ul>
93 <li><a href="#Randomness">Yarrow</a>
94 </li></ul>
95 <li><a href="#Miscellaneous%20functions">Miscellaneous functions</a>
96 <li><a href="#Compatibility%20functions">Compatibility functions</a>
97 </li></ul>
98 <li><a name="toc_Nettle%20soup" href="#Nettle%20soup">Traditional Nettle Soup</a>
99 <li><a name="toc_Installation" href="#Installation">Installation</a>
100 <li><a name="toc_Index" href="#Index">Function and Concept Index</a>
101 </li></ul>
102 </div>
103
104
105 <div class="node">
106 <p><hr>
107 Node:&nbsp;<a name="Top">Top</a>,
108 Next:&nbsp;<a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
109 Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
110 Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
111 <br>
112 </div>
113
114 <h2 class="unnumbered">Nettle</h2>
115
116 <p>This document describes the Nettle low-level cryptographic library. You
117 can use the library directly from your C programs, or write or use an
118 object-oriented wrapper for your favorite language or application.
119
120 This manual is for the Nettle library (version 2.1), a
121 low-level cryptographic library.
122
123    <p>Originally written 2001 by Niels Möller, updated 2010.
124
125    <blockquote>
126 This manual is placed in the public domain. You may freely copy it, in
127 whole or in part, with or without modification. Attribution is
128 appreciated, but not required. 
129 </blockquote>
130    
131 <ul class="menu">
132 <li><a accesskey="1" href="#Introduction">Introduction</a>:                 What is Nettle? 
133 <li><a accesskey="2" href="#Copyright">Copyright</a>:                    Your rights. 
134 <li><a accesskey="3" href="#Conventions">Conventions</a>:                  General interface conventions. 
135 <li><a accesskey="4" href="#Example">Example</a>:                      An example program. 
136 <li><a accesskey="5" href="#Linking">Linking</a>:                      Linking with the libnettle and libhogweed. 
137 <li><a accesskey="6" href="#Reference">Reference</a>:                    All Nettle functions and features. 
138 <li><a accesskey="7" href="#Nettle%20soup">Nettle soup</a>:                  For the serious nettle hacker. 
139 <li><a accesskey="8" href="#Installation">Installation</a>:                 How to install Nettle. 
140 <li><a accesskey="9" href="#Index">Index</a>:                        Function and concept index. 
141 </ul>
142
143 <div class="node">
144 <p><hr>
145 Node:&nbsp;<a name="Introduction">Introduction</a>,
146 Next:&nbsp;<a rel="next" accesskey="n" href="#Copyright">Copyright</a>,
147 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Top">Top</a>,
148 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
149 <br>
150 </div>
151
152 <h2 class="chapter">Introduction</h2>
153
154 <p>Nettle is a cryptographic library that is designed to fit easily in more
155 or less any context: In crypto toolkits for object-oriented languages
156 (C++, Python, Pike, ...), in applications like LSH or GNUPG, or even in
157 kernel space. In most contexts, you need more than the basic
158 cryptographic algorithms, you also need some way to keep track of available
159 algorithms, their properties and variants. You often have some algorithm
160 selection process, often dictated by a protocol you want to implement.
161
162    <p>And as the requirements of applications differ in subtle and not so
163 subtle ways, an API that fits one application well can be a pain to use
164 in a different context. And that is why there are so many different
165 cryptographic libraries around.
166
167    <p>Nettle tries to avoid this problem by doing one thing, the low-level
168 crypto stuff, and providing a <em>simple</em> but general interface to it. 
169 In particular, Nettle doesn't do algorithm selection. It doesn't do
170 memory allocation. It doesn't do any I/O.
171
172    <p>The idea is that one can build several application and context specific
173 interfaces on top of Nettle, and share the code, test cases, benchmarks,
174 documentation, etc. Examples are the Nettle module for the Pike
175 language, and LSH, which both use an object-oriented abstraction on top
176 of the library.
177
178    <p>This manual explains how to use the Nettle library. It also tries to
179 provide some background on the cryptography, and advice on how to best
180 put it to use.
181
182 <div class="node">
183 <p><hr>
184 Node:&nbsp;<a name="Copyright">Copyright</a>,
185 Next:&nbsp;<a rel="next" accesskey="n" href="#Conventions">Conventions</a>,
186 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
187 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
188 <br>
189 </div>
190
191 <h2 class="chapter">Copyright</h2>
192
193 <p>Nettle is distributed under the GNU General Public License (GPL) (see
194 the file COPYING for details). However, most of the individual files
195 are dual licensed under less restrictive licenses like the GNU Lesser
196 General Public License (LGPL), or are in the public domain. This means
197 that if you don't use the parts of nettle that are GPL-only, you have
198 the option to use the Nettle library just as if it were licensed under
199 the LGPL. To find the current status of particular files, you have to
200 read the copyright notices at the top of the files.
201
202    <p>This manual is in the public domain. You may freely copy it in whole or
203 in part, e.g., into documentation of programs that build on Nettle. 
204 Attribution, as well as contribution of improvements to the text, is of
205 course appreciated, but it is not required.
206
207    <p>A list of the supported algorithms, their origins and licenses:
208
209      <dl>
210 <dt><em>AES</em>
211      <dd>The implementation of the AES cipher (also known as rijndael) is written
212 by Rafael Sevilla. Assembler for x86 by Rafael Sevilla and
213 Niels Möller, Sparc assembler by Niels Möller. Released under the
214 LGPL.
215
216      <br><dt><em>ARCFOUR</em>
217      <dd>The implementation of the ARCFOUR (also known as RC4) cipher is written
218 by Niels Möller. Released under the LGPL.
219
220      <br><dt><em>ARCTWO</em>
221      <dd>The implementation of the ARCTWO (also known as RC2) cipher is written
222 by Nikos Mavroyanopoulos and modified by Werner Koch and Simon
223 Josefsson. Released under the LGPL.
224
225      <br><dt><em>BLOWFISH</em>
226      <dd>The implementation of the BLOWFISH cipher is written by Werner Koch,
227 copyright owned by the Free Software Foundation. Also hacked by Ray
228 Dassen and Niels Möller. Released under the GPL.
229
230      <br><dt><em>CAMELLIA</em>
231      <dd>The C implementation is by Nippon Telegraph and Telephone Corporation
232 (NTT), heavily modified by Niels Möller. Assembler for x86 by
233 Niels Möller. Released under the LGPL.
234
235      <br><dt><em>CAST128</em>
236      <dd>The implementation of the CAST128 cipher is written by Steve Reid. 
237 Released into the public domain.
238
239      <br><dt><em>DES</em>
240      <dd>The implementation of the DES cipher is written by Dana L. How, and
241 released under the LGPL.
242
243      <br><dt><em>MD2</em>
244      <dd>The implementation of MD2 is written by Andrew Kuchling, and hacked
245 some by Andreas Sigfridsson and Niels Möller. Python Cryptography
246 Toolkit license (essentially public domain).
247
248      <br><dt><em>MD4</em>
249      <dd>This is almost the same code as for MD5 below, with modifications by
250 Marcus Comstedt. Released into the public domain.
251
252      <br><dt><em>MD5</em>
253      <dd>The implementation of the MD5 message digest is written by Colin Plumb. 
254 It has been hacked some more by Andrew Kuchling and Niels Möller. 
255 Released into the public domain.
256
257      <br><dt><em>SERPENT</em>
258      <dd>The implementation of the SERPENT cipher is written by Ross Anderson,
259 Eli Biham, and Lars Knudsen, adapted to LSH by Rafael Sevilla, and to
260 Nettle by Niels Möller. Released under the GPL.
261
262      <br><dt><em>SHA1</em>
263      <dd>The C implementation of the SHA1 message digest is written by Peter
264 Gutmann, and hacked some more by Andrew Kuchling and Niels Möller. 
265 Released into the public domain. Assembler for x86 by Niels Möller,
266 released under the LGPL.
267
268      <br><dt><em>SHA224, SHA256, SHA384, and SHA512</em>
269      <dd>Written by Niels Möller, using Peter Gutmann's SHA1 code as a model. 
270 Released under the LGPL.
271
272      <br><dt><em>TWOFISH</em>
273      <dd>The implementation of the TWOFISH cipher is written by Ruud de Rooij. 
274 Released under the LGPL.
275
276      <br><dt><em>RSA</em>
277      <dd>Written by Niels Möller, released under the LGPL. Uses the GMP library
278 for bignum operations.
279
280      <br><dt><em>DSA</em>
281      <dd>Written by Niels Möller, released under the LGPL. Uses the GMP library
282 for bignum operations. 
283 </dl>
284
285 <div class="node">
286 <p><hr>
287 Node:&nbsp;<a name="Conventions">Conventions</a>,
288 Next:&nbsp;<a rel="next" accesskey="n" href="#Example">Example</a>,
289 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Copyright">Copyright</a>,
290 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
291 <br>
292 </div>
293
294 <h2 class="chapter">Conventions</h2>
295
296 <p>For each supported algorithm, there is an include file that defines a
297 <em>context struct</em>, a few constants, and declares functions for
298 operating on the context. The context struct encapsulates all information
299 needed by the algorithm, and it can be copied or moved in memory with no
300 unexpected effects.
301
302    <p>For consistency, functions for different algorithms are very similar,
303 but there are some differences, for instance reflecting if the key setup
304 or encryption function differ for encryption and decryption, and whether
305 or not key setup can fail. There are also differences between algorithms
306 that don't show in function prototypes, but which the application must
307 nevertheless be aware of. There is no big difference between the
308 functions for stream ciphers and for block ciphers, although they should
309 be used quite differently by the application.
310
311    <p>If your application uses more than one algorithm of the same type, you
312 should probably create an interface that is tailor-made for your needs,
313 and then write a few lines of glue code on top of Nettle.
314
315    <p>By convention, for an algorithm named <code>foo</code>, the struct tag for the
316 context struct is <code>foo_ctx</code>, constants and functions uses prefixes
317 like <code>FOO_BLOCK_SIZE</code> (a constant) and <code>foo_set_key</code> (a
318 function).
319
320    <p>In all functions, strings are represented with an explicit length, of
321 type <code>unsigned</code>, and a pointer of type <code>uint8_t *</code> or
322 <code>const uint8_t *</code>. For functions that transform one string to
323 another, the argument order is length, destination pointer and source
324 pointer. Source and destination areas are of the same length. Source and
325 destination may be the same, so that you can process strings in place,
326 but they <em>must not</em> overlap in any other way.
327
328    <p>Many of the functions lack return value and can never fail. Those
329 functions which can fail, return one on success and zero on failure.
330
331 <div class="node">
332 <p><hr>
333 Node:&nbsp;<a name="Example">Example</a>,
334 Next:&nbsp;<a rel="next" accesskey="n" href="#Linking">Linking</a>,
335 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Conventions">Conventions</a>,
336 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
337 <br>
338 </div>
339
340 <h2 class="chapter">Example</h2>
341
342 <p>A simple example program that reads a file from standard input and
343 writes its SHA1 checksum on standard output should give the flavor of
344 Nettle.
345
346 <pre class="example">
347
348      <pre class="verbatim">#include &lt;stdio.h>
349      #include &lt;stdlib.h>
350      
351      #include &lt;nettle/sha.h>
352      
353      #define BUF_SIZE 1000
354      
355      static void
356      display_hex(unsigned length, uint8_t *data)
357      {
358        unsigned i;
359      
360        for (i = 0; i&lt;length; i++)
361          printf("%02x ", data[i]);
362      
363        printf("\n");
364      }
365      
366      int
367      main(int argc, char **argv)
368      {
369        struct sha1_ctx ctx;
370        uint8_t buffer[BUF_SIZE];
371        uint8_t digest[SHA1_DIGEST_SIZE];
372        
373        sha1_init(&amp;ctx);
374        for (;;)
375        {
376          int done = fread(buffer, 1, sizeof(buffer), stdin);
377          sha1_update(&amp;ctx, done, buffer);
378          if (done &lt; sizeof(buffer))
379            break;
380        }
381        if (ferror(stdin))
382          return EXIT_FAILURE;
383      
384        sha1_digest(&amp;ctx, SHA1_DIGEST_SIZE, digest);
385      
386        display_hex(SHA1_DIGEST_SIZE, digest);
387        return EXIT_SUCCESS;  
388      }
389      </pre></pre>
390
391    <p>On a typical Unix system, this program can be compiled and linked with
392 the command line
393 <pre class="example">     cc sha-example.c -o sha-example -lnettle
394      </pre>
395
396 <div class="node">
397 <p><hr>
398 Node:&nbsp;<a name="Linking">Linking</a>,
399 Next:&nbsp;<a rel="next" accesskey="n" href="#Reference">Reference</a>,
400 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Example">Example</a>,
401 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
402 <br>
403 </div>
404
405 <h2 class="chapter">Linking</h2>
406
407 <p>Nettle actually consists of two libraries, <code>libnettle</code> and
408 <code>libhogweed</code>. The <code>libhogweed</code> library contains those
409 functions of Nettle that uses bignum operations, and depends on the GMP
410 library. With this division, linking works the same for both static and
411 dynamic libraries.
412
413    <p>If an application uses only the symmetric crypto algorithms of Nettle
414 (i.e., block ciphers, hash functions, and the like), it's sufficient to
415 link with <code>-lnettle</code>. If an application also uses public-key
416 algorithms, the recommended linker flags are <code>-lhogweed -lnettle
417 -lgmp</code>. If the involved libraries are installed as dynamic libraries, it
418 may be sufficient to link with just <code>-lhogweed</code>, and the loader
419 will resolve the dependencies automatically.
420
421 <div class="node">
422 <p><hr>
423 Node:&nbsp;<a name="Reference">Reference</a>,
424 Next:&nbsp;<a rel="next" accesskey="n" href="#Nettle%20soup">Nettle soup</a>,
425 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Linking">Linking</a>,
426 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
427 <br>
428 </div>
429
430 <h2 class="chapter">Reference</h2>
431
432 <p>This chapter describes all the Nettle functions, grouped by family.
433
434 <ul class="menu">
435 <li><a accesskey="1" href="#Hash%20functions">Hash functions</a>: 
436 <li><a accesskey="2" href="#Cipher%20functions">Cipher functions</a>: 
437 <li><a accesskey="3" href="#Cipher%20modes">Cipher modes</a>: 
438 <li><a accesskey="4" href="#Keyed%20hash%20functions">Keyed hash functions</a>: 
439 <li><a accesskey="5" href="#Public-key%20algorithms">Public-key algorithms</a>: 
440 <li><a accesskey="6" href="#Randomness">Randomness</a>: 
441 <li><a accesskey="7" href="#Miscellaneous%20functions">Miscellaneous functions</a>: 
442 <li><a accesskey="8" href="#Compatibility%20functions">Compatibility functions</a>: 
443 </ul>
444
445 <div class="node">
446 <p><hr>
447 Node:&nbsp;<a name="Hash%20functions">Hash functions</a>,
448 Next:&nbsp;<a rel="next" accesskey="n" href="#Cipher%20functions">Cipher functions</a>,
449 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Reference">Reference</a>,
450 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
451 <br>
452 </div>
453
454 <h3 class="section">Hash functions</h3>
455
456 <p>A cryptographic <dfn>hash function</dfn> is a function that takes variable
457 size strings, and maps them to strings of fixed, short, length. There
458 are naturally lots of collisions, as there are more possible 1MB files
459 than 20 byte strings. But the function is constructed such that is hard
460 to find the collisions. More precisely, a cryptographic hash function
461 <code>H</code> should have the following properties:
462
463      <dl>
464
465      <br><dt><em>One-way</em>
466      <dd>Given a hash value <code>H(x)</code> it is hard to find a string <code>x</code>
467 that hashes to that value.
468
469      <br><dt><em>Collision-resistant</em>
470      <dd>It is hard to find two different strings, <code>x</code> and <code>y</code>, such
471 that <code>H(x)</code> = <code>H(y)</code>.
472
473    </dl>
474
475    <p>Hash functions are useful as building blocks for digital signatures,
476 message authentication codes, pseudo random generators, association of
477 unique ids to documents, and many other things.
478
479    <p>The most commonly used hash functions are MD5 and SHA1. Unfortunately,
480 both these fail the collision-resistance requirement; cryptologists have
481 found ways to construct colliding inputs. The recommended hash function
482 for new applications is SHA256, even though it uses a structure similar
483 to MD5 and SHA1. Constructing better hash functions is an urgent research
484 problem.
485
486 <h3 class="subsection"><small>MD5</small></h4>
487
488 <p>MD5 is a message digest function constructed by Ronald Rivest, and
489 described in <cite>RFC 1321</cite>. It outputs message digests of 128 bits, or
490 16 octets. Nettle defines MD5 in <code>&lt;nettle/md5.h&gt;</code>.
491
492 <p>
493 <table width="100%">
494 <tr>
495 <td align="left"><b>struct md5_ctx</b><i>
496      </i></td>
497 <td align="right">Context struct</td>
498 </tr>
499 </table>
500 <table width="95%" align="center">
501 <tr><td>
502 </td></tr>
503 </table>
504
505 <p>
506 <table width="100%">
507 <tr>
508 <td align="left"><b>MD5_DIGEST_SIZE</b><i>
509      </i></td>
510 <td align="right">Constant</td>
511 </tr>
512 </table>
513 <table width="95%" align="center">
514 <tr><td>
515 The size of an MD5 digest, i.e. 16. 
516 </td></tr>
517 </table>
518
519 <p>
520 <table width="100%">
521 <tr>
522 <td align="left"><b>MD5_DATA_SIZE</b><i>
523      </i></td>
524 <td align="right">Constant</td>
525 </tr>
526 </table>
527 <table width="95%" align="center">
528 <tr><td>
529 The internal block size of MD5. Useful for some special constructions,
530 in particular HMAC-MD5. 
531 </td></tr>
532 </table>
533
534 <p>
535 <table width="100%">
536 <tr>
537 <td align="left">void <b>md5_init</b><i> </i>(<i>struct md5_ctx *</i><var>ctx</var><i></i>)<i>
538      </i></td>
539 <td align="right">Function</td>
540 </tr>
541 </table>
542 <table width="95%" align="center">
543 <tr><td>
544 Initialize the MD5 state. 
545 </td></tr>
546 </table>
547
548 <p>
549 <table width="100%">
550 <tr>
551 <td align="left">void <b>md5_update</b><i> </i>(<i>struct md5_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
552      </i></td>
553 <td align="right">Function</td>
554 </tr>
555 </table>
556 <table width="95%" align="center">
557 <tr><td>
558 Hash some more data. 
559 </td></tr>
560 </table>
561
562 <p>
563 <table width="100%">
564 <tr>
565 <td align="left">void <b>md5_digest</b><i> </i>(<i>struct md5_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
566      </i></td>
567 <td align="right">Function</td>
568 </tr>
569 </table>
570 <table width="95%" align="center">
571 <tr><td>
572 Performs final processing and extracts the message digest, writing it
573 to <var>digest</var>. <var>length</var> may be smaller than
574 <code>MD5_DIGEST_SIZE</code>, in which case only the first <var>length</var>
575 octets of the digest are written.
576
577      <p>This function also resets the context in the same way as
578 <code>md5_init</code>. 
579 </td></tr>
580 </table>
581
582    <p>The normal way to use MD5 is to call the functions in order: First
583 <code>md5_init</code>, then <code>md5_update</code> zero or more times, and finally
584 <code>md5_digest</code>. After <code>md5_digest</code>, the context is reset to
585 its initial state, so you can start over calling <code>md5_update</code> to
586 hash new data.
587
588    <p>To start over, you can call <code>md5_init</code> at any time.
589
590 <h3 class="subsection"><small>MD2</small></h4>
591
592 <p>MD2 is another hash function of Ronald Rivest's, described in
593 <cite>RFC 1319</cite>. It outputs message digests of 128 bits, or 16 octets. 
594 Nettle defines MD2 in <code>&lt;nettle/md2.h&gt;</code>.
595
596 <p>
597 <table width="100%">
598 <tr>
599 <td align="left"><b>struct md2_ctx</b><i>
600      </i></td>
601 <td align="right">Context struct</td>
602 </tr>
603 </table>
604 <table width="95%" align="center">
605 <tr><td>
606 </td></tr>
607 </table>
608
609 <p>
610 <table width="100%">
611 <tr>
612 <td align="left"><b>MD2_DIGEST_SIZE</b><i>
613      </i></td>
614 <td align="right">Constant</td>
615 </tr>
616 </table>
617 <table width="95%" align="center">
618 <tr><td>
619 The size of an MD2 digest, i.e. 16. 
620 </td></tr>
621 </table>
622
623 <p>
624 <table width="100%">
625 <tr>
626 <td align="left"><b>MD2_DATA_SIZE</b><i>
627      </i></td>
628 <td align="right">Constant</td>
629 </tr>
630 </table>
631 <table width="95%" align="center">
632 <tr><td>
633 The internal block size of MD2. 
634 </td></tr>
635 </table>
636
637 <p>
638 <table width="100%">
639 <tr>
640 <td align="left">void <b>md2_init</b><i> </i>(<i>struct md2_ctx *</i><var>ctx</var><i></i>)<i>
641      </i></td>
642 <td align="right">Function</td>
643 </tr>
644 </table>
645 <table width="95%" align="center">
646 <tr><td>
647 Initialize the MD2 state. 
648 </td></tr>
649 </table>
650
651 <p>
652 <table width="100%">
653 <tr>
654 <td align="left">void <b>md2_update</b><i> </i>(<i>struct md2_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
655      </i></td>
656 <td align="right">Function</td>
657 </tr>
658 </table>
659 <table width="95%" align="center">
660 <tr><td>
661 Hash some more data. 
662 </td></tr>
663 </table>
664
665 <p>
666 <table width="100%">
667 <tr>
668 <td align="left">void <b>md2_digest</b><i> </i>(<i>struct md2_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
669      </i></td>
670 <td align="right">Function</td>
671 </tr>
672 </table>
673 <table width="95%" align="center">
674 <tr><td>
675 Performs final processing and extracts the message digest, writing it
676 to <var>digest</var>. <var>length</var> may be smaller than
677 <code>MD2_DIGEST_SIZE</code>, in which case only the first <var>length</var>
678 octets of the digest are written.
679
680      <p>This function also resets the context in the same way as
681 <code>md2_init</code>. 
682 </td></tr>
683 </table>
684
685 <h3 class="subsection"><small>MD4</small></h4>
686
687 <p>MD4 is a predecessor of MD5, described in <cite>RFC 1320</cite>. Like MD5, it
688 is constructed by Ronald Rivest. It outputs message digests of 128 bits,
689 or 16 octets. Nettle defines MD4 in <code>&lt;nettle/md4.h&gt;</code>. Use of MD4 is
690 not recommended, but it is sometimes needed for compatibility with
691 existing applications and protocols.
692
693 <p>
694 <table width="100%">
695 <tr>
696 <td align="left"><b>struct md4_ctx</b><i>
697      </i></td>
698 <td align="right">Context struct</td>
699 </tr>
700 </table>
701 <table width="95%" align="center">
702 <tr><td>
703 </td></tr>
704 </table>
705
706 <p>
707 <table width="100%">
708 <tr>
709 <td align="left"><b>MD4_DIGEST_SIZE</b><i>
710      </i></td>
711 <td align="right">Constant</td>
712 </tr>
713 </table>
714 <table width="95%" align="center">
715 <tr><td>
716 The size of an MD4 digest, i.e. 16. 
717 </td></tr>
718 </table>
719
720 <p>
721 <table width="100%">
722 <tr>
723 <td align="left"><b>MD4_DATA_SIZE</b><i>
724      </i></td>
725 <td align="right">Constant</td>
726 </tr>
727 </table>
728 <table width="95%" align="center">
729 <tr><td>
730 The internal block size of MD4. 
731 </td></tr>
732 </table>
733
734 <p>
735 <table width="100%">
736 <tr>
737 <td align="left">void <b>md4_init</b><i> </i>(<i>struct md4_ctx *</i><var>ctx</var><i></i>)<i>
738      </i></td>
739 <td align="right">Function</td>
740 </tr>
741 </table>
742 <table width="95%" align="center">
743 <tr><td>
744 Initialize the MD4 state. 
745 </td></tr>
746 </table>
747
748 <p>
749 <table width="100%">
750 <tr>
751 <td align="left">void <b>md4_update</b><i> </i>(<i>struct md4_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
752      </i></td>
753 <td align="right">Function</td>
754 </tr>
755 </table>
756 <table width="95%" align="center">
757 <tr><td>
758 Hash some more data. 
759 </td></tr>
760 </table>
761
762 <p>
763 <table width="100%">
764 <tr>
765 <td align="left">void <b>md4_digest</b><i> </i>(<i>struct md4_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
766      </i></td>
767 <td align="right">Function</td>
768 </tr>
769 </table>
770 <table width="95%" align="center">
771 <tr><td>
772 Performs final processing and extracts the message digest, writing it
773 to <var>digest</var>. <var>length</var> may be smaller than
774 <code>MD4_DIGEST_SIZE</code>, in which case only the first <var>length</var>
775 octets of the digest are written.
776
777      <p>This function also resets the context in the same way as
778 <code>md4_init</code>. 
779 </td></tr>
780 </table>
781
782 <h3 class="subsection"><small>SHA1</small></h4>
783
784 <p>SHA1 is a hash function specified by <dfn>NIST</dfn> (The U.S. National Institute
785 for Standards and Technology). It outputs hash values of 160 bits, or 20
786 octets. Nettle defines SHA1 in <code>&lt;nettle/sha.h&gt;</code>.
787
788    <p>The functions are analogous to the MD5 ones.
789
790 <p>
791 <table width="100%">
792 <tr>
793 <td align="left"><b>struct sha1_ctx</b><i>
794      </i></td>
795 <td align="right">Context struct</td>
796 </tr>
797 </table>
798 <table width="95%" align="center">
799 <tr><td>
800 </td></tr>
801 </table>
802
803 <p>
804 <table width="100%">
805 <tr>
806 <td align="left"><b>SHA1_DIGEST_SIZE</b><i>
807      </i></td>
808 <td align="right">Constant</td>
809 </tr>
810 </table>
811 <table width="95%" align="center">
812 <tr><td>
813 The size of an SHA1 digest, i.e. 20. 
814 </td></tr>
815 </table>
816
817 <p>
818 <table width="100%">
819 <tr>
820 <td align="left"><b>SHA1_DATA_SIZE</b><i>
821      </i></td>
822 <td align="right">Constant</td>
823 </tr>
824 </table>
825 <table width="95%" align="center">
826 <tr><td>
827 The internal block size of SHA1. Useful for some special constructions,
828 in particular HMAC-SHA1. 
829 </td></tr>
830 </table>
831
832 <p>
833 <table width="100%">
834 <tr>
835 <td align="left">void <b>sha1_init</b><i> </i>(<i>struct sha1_ctx *</i><var>ctx</var><i></i>)<i>
836      </i></td>
837 <td align="right">Function</td>
838 </tr>
839 </table>
840 <table width="95%" align="center">
841 <tr><td>
842 Initialize the SHA1 state. 
843 </td></tr>
844 </table>
845
846 <p>
847 <table width="100%">
848 <tr>
849 <td align="left">void <b>sha1_update</b><i> </i>(<i>struct sha1_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
850      </i></td>
851 <td align="right">Function</td>
852 </tr>
853 </table>
854 <table width="95%" align="center">
855 <tr><td>
856 Hash some more data. 
857 </td></tr>
858 </table>
859
860 <p>
861 <table width="100%">
862 <tr>
863 <td align="left">void <b>sha1_digest</b><i> </i>(<i>struct sha1_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
864      </i></td>
865 <td align="right">Function</td>
866 </tr>
867 </table>
868 <table width="95%" align="center">
869 <tr><td>
870 Performs final processing and extracts the message digest, writing it
871 to <var>digest</var>. <var>length</var> may be smaller than
872 <code>SHA1_DIGEST_SIZE</code>, in which case only the first <var>length</var>
873 octets of the digest are written.
874
875      <p>This function also resets the context in the same way as
876 <code>sha1_init</code>. 
877 </td></tr>
878 </table>
879
880 <h3 class="subsection"><small>SHA256</small></h4>
881
882 <p>SHA256 is another hash function specified by <dfn>NIST</dfn>, intended as a
883 replacement for <small>SHA1</small>, generating larger digests. It outputs
884 hash values of 256 bits, or 32 octets. Nettle defines SHA256 in
885 <code>&lt;nettle/sha.h&gt;</code>.
886
887    <p>The functions are analogous to the MD5 ones.
888
889 <p>
890 <table width="100%">
891 <tr>
892 <td align="left"><b>struct sha256_ctx</b><i>
893      </i></td>
894 <td align="right">Context struct</td>
895 </tr>
896 </table>
897 <table width="95%" align="center">
898 <tr><td>
899 </td></tr>
900 </table>
901
902 <p>
903 <table width="100%">
904 <tr>
905 <td align="left"><b>SHA256_DIGEST_SIZE</b><i>
906      </i></td>
907 <td align="right">Constant</td>
908 </tr>
909 </table>
910 <table width="95%" align="center">
911 <tr><td>
912 The size of an SHA256 digest, i.e. 32. 
913 </td></tr>
914 </table>
915
916 <p>
917 <table width="100%">
918 <tr>
919 <td align="left"><b>SHA256_DATA_SIZE</b><i>
920      </i></td>
921 <td align="right">Constant</td>
922 </tr>
923 </table>
924 <table width="95%" align="center">
925 <tr><td>
926 The internal block size of SHA256. Useful for some special constructions,
927 in particular HMAC-SHA256. 
928 </td></tr>
929 </table>
930
931 <p>
932 <table width="100%">
933 <tr>
934 <td align="left">void <b>sha256_init</b><i> </i>(<i>struct sha256_ctx *</i><var>ctx</var><i></i>)<i>
935      </i></td>
936 <td align="right">Function</td>
937 </tr>
938 </table>
939 <table width="95%" align="center">
940 <tr><td>
941 Initialize the SHA256 state. 
942 </td></tr>
943 </table>
944
945 <p>
946 <table width="100%">
947 <tr>
948 <td align="left">void <b>sha256_update</b><i> </i>(<i>struct sha256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
949      </i></td>
950 <td align="right">Function</td>
951 </tr>
952 </table>
953 <table width="95%" align="center">
954 <tr><td>
955 Hash some more data. 
956 </td></tr>
957 </table>
958
959 <p>
960 <table width="100%">
961 <tr>
962 <td align="left">void <b>sha256_digest</b><i> </i>(<i>struct sha256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
963      </i></td>
964 <td align="right">Function</td>
965 </tr>
966 </table>
967 <table width="95%" align="center">
968 <tr><td>
969 Performs final processing and extracts the message digest, writing it
970 to <var>digest</var>. <var>length</var> may be smaller than
971 <code>SHA256_DIGEST_SIZE</code>, in which case only the first <var>length</var>
972 octets of the digest are written.
973
974      <p>This function also resets the context in the same way as
975 <code>sha256_init</code>. 
976 </td></tr>
977 </table>
978
979 <h3 class="subsection"><small>SHA224</small></h4>
980
981 <p>SHA224 is a variant of SHA256, with a different initial state, and with
982 the output truncated to 224 bits, or 28 octets. Nettle defines SHA224 in
983 <code>&lt;nettle/sha.h&gt;</code>.
984
985    <p>The functions are analogous to the MD5 ones.
986
987 <p>
988 <table width="100%">
989 <tr>
990 <td align="left"><b>struct sha224_ctx</b><i>
991      </i></td>
992 <td align="right">Context struct</td>
993 </tr>
994 </table>
995 <table width="95%" align="center">
996 <tr><td>
997 </td></tr>
998 </table>
999
1000 <p>
1001 <table width="100%">
1002 <tr>
1003 <td align="left"><b>SHA224_DIGEST_SIZE</b><i>
1004      </i></td>
1005 <td align="right">Constant</td>
1006 </tr>
1007 </table>
1008 <table width="95%" align="center">
1009 <tr><td>
1010 The size of an SHA224 digest, i.e. 28. 
1011 </td></tr>
1012 </table>
1013
1014 <p>
1015 <table width="100%">
1016 <tr>
1017 <td align="left"><b>SHA224_DATA_SIZE</b><i>
1018      </i></td>
1019 <td align="right">Constant</td>
1020 </tr>
1021 </table>
1022 <table width="95%" align="center">
1023 <tr><td>
1024 The internal block size of SHA224. Useful for some special constructions,
1025 in particular HMAC-SHA224. 
1026 </td></tr>
1027 </table>
1028
1029 <p>
1030 <table width="100%">
1031 <tr>
1032 <td align="left">void <b>sha224_init</b><i> </i>(<i>struct sha224_ctx *</i><var>ctx</var><i></i>)<i>
1033      </i></td>
1034 <td align="right">Function</td>
1035 </tr>
1036 </table>
1037 <table width="95%" align="center">
1038 <tr><td>
1039 Initialize the SHA224 state. 
1040 </td></tr>
1041 </table>
1042
1043 <p>
1044 <table width="100%">
1045 <tr>
1046 <td align="left">void <b>sha224_update</b><i> </i>(<i>struct sha224_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
1047      </i></td>
1048 <td align="right">Function</td>
1049 </tr>
1050 </table>
1051 <table width="95%" align="center">
1052 <tr><td>
1053 Hash some more data. 
1054 </td></tr>
1055 </table>
1056
1057 <p>
1058 <table width="100%">
1059 <tr>
1060 <td align="left">void <b>sha224_digest</b><i> </i>(<i>struct sha224_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
1061      </i></td>
1062 <td align="right">Function</td>
1063 </tr>
1064 </table>
1065 <table width="95%" align="center">
1066 <tr><td>
1067 Performs final processing and extracts the message digest, writing it
1068 to <var>digest</var>. <var>length</var> may be smaller than
1069 <code>SHA224_DIGEST_SIZE</code>, in which case only the first <var>length</var>
1070 octets of the digest are written.
1071
1072      <p>This function also resets the context in the same way as
1073 <code>sha224_init</code>. 
1074 </td></tr>
1075 </table>
1076
1077 <h3 class="subsection"><small>SHA512</small></h4>
1078
1079 <p>SHA512 is a larger sibling to SHA256, with a very similar structure but
1080 with both the output and the internal variables of twice the size. The
1081 internal variables are 64 bits rather than 32, making it significantly
1082 slower on 32-bit computers. It outputs hash values of 512 bits, or 64
1083 octets. Nettle defines SHA512 in <code>&lt;nettle/sha.h&gt;</code>.
1084
1085    <p>The functions are analogous to the MD5 ones.
1086
1087 <p>
1088 <table width="100%">
1089 <tr>
1090 <td align="left"><b>struct sha512_ctx</b><i>
1091      </i></td>
1092 <td align="right">Context struct</td>
1093 </tr>
1094 </table>
1095 <table width="95%" align="center">
1096 <tr><td>
1097 </td></tr>
1098 </table>
1099
1100 <p>
1101 <table width="100%">
1102 <tr>
1103 <td align="left"><b>SHA512_DIGEST_SIZE</b><i>
1104      </i></td>
1105 <td align="right">Constant</td>
1106 </tr>
1107 </table>
1108 <table width="95%" align="center">
1109 <tr><td>
1110 The size of an SHA512 digest, i.e. 64. 
1111 </td></tr>
1112 </table>
1113
1114 <p>
1115 <table width="100%">
1116 <tr>
1117 <td align="left"><b>SHA512_DATA_SIZE</b><i>
1118      </i></td>
1119 <td align="right">Constant</td>
1120 </tr>
1121 </table>
1122 <table width="95%" align="center">
1123 <tr><td>
1124 The internal block size of SHA512. Useful for some special constructions,
1125 in particular HMAC-SHA512. 
1126 </td></tr>
1127 </table>
1128
1129 <p>
1130 <table width="100%">
1131 <tr>
1132 <td align="left">void <b>sha512_init</b><i> </i>(<i>struct sha512_ctx *</i><var>ctx</var><i></i>)<i>
1133      </i></td>
1134 <td align="right">Function</td>
1135 </tr>
1136 </table>
1137 <table width="95%" align="center">
1138 <tr><td>
1139 Initialize the SHA512 state. 
1140 </td></tr>
1141 </table>
1142
1143 <p>
1144 <table width="100%">
1145 <tr>
1146 <td align="left">void <b>sha512_update</b><i> </i>(<i>struct sha512_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
1147      </i></td>
1148 <td align="right">Function</td>
1149 </tr>
1150 </table>
1151 <table width="95%" align="center">
1152 <tr><td>
1153 Hash some more data. 
1154 </td></tr>
1155 </table>
1156
1157 <p>
1158 <table width="100%">
1159 <tr>
1160 <td align="left">void <b>sha512_digest</b><i> </i>(<i>struct sha512_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
1161      </i></td>
1162 <td align="right">Function</td>
1163 </tr>
1164 </table>
1165 <table width="95%" align="center">
1166 <tr><td>
1167 Performs final processing and extracts the message digest, writing it
1168 to <var>digest</var>. <var>length</var> may be smaller than
1169 <code>SHA512_DIGEST_SIZE</code>, in which case only the first <var>length</var>
1170 octets of the digest are written.
1171
1172      <p>This function also resets the context in the same way as
1173 <code>sha512_init</code>. 
1174 </td></tr>
1175 </table>
1176
1177 <h3 class="subsection"><small>SHA384</small></h4>
1178
1179 <p>SHA384 is a variant of SHA512, with a different initial state, and with
1180 the output truncated to 384 bits, or 48 octets. Nettle defines SHA384 in
1181 <code>&lt;nettle/sha.h&gt;</code>.
1182
1183    <p>The functions are analogous to the MD5 ones.
1184
1185 <p>
1186 <table width="100%">
1187 <tr>
1188 <td align="left"><b>struct sha384_ctx</b><i>
1189      </i></td>
1190 <td align="right">Context struct</td>
1191 </tr>
1192 </table>
1193 <table width="95%" align="center">
1194 <tr><td>
1195 </td></tr>
1196 </table>
1197
1198 <p>
1199 <table width="100%">
1200 <tr>
1201 <td align="left"><b>SHA384_DIGEST_SIZE</b><i>
1202      </i></td>
1203 <td align="right">Constant</td>
1204 </tr>
1205 </table>
1206 <table width="95%" align="center">
1207 <tr><td>
1208 The size of an SHA384 digest, i.e. 48. 
1209 </td></tr>
1210 </table>
1211
1212 <p>
1213 <table width="100%">
1214 <tr>
1215 <td align="left"><b>SHA384_DATA_SIZE</b><i>
1216      </i></td>
1217 <td align="right">Constant</td>
1218 </tr>
1219 </table>
1220 <table width="95%" align="center">
1221 <tr><td>
1222 The internal block size of SHA384. Useful for some special constructions,
1223 in particular HMAC-SHA384. 
1224 </td></tr>
1225 </table>
1226
1227 <p>
1228 <table width="100%">
1229 <tr>
1230 <td align="left">void <b>sha384_init</b><i> </i>(<i>struct sha384_ctx *</i><var>ctx</var><i></i>)<i>
1231      </i></td>
1232 <td align="right">Function</td>
1233 </tr>
1234 </table>
1235 <table width="95%" align="center">
1236 <tr><td>
1237 Initialize the SHA384 state. 
1238 </td></tr>
1239 </table>
1240
1241 <p>
1242 <table width="100%">
1243 <tr>
1244 <td align="left">void <b>sha384_update</b><i> </i>(<i>struct sha384_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
1245      </i></td>
1246 <td align="right">Function</td>
1247 </tr>
1248 </table>
1249 <table width="95%" align="center">
1250 <tr><td>
1251 Hash some more data. 
1252 </td></tr>
1253 </table>
1254
1255 <p>
1256 <table width="100%">
1257 <tr>
1258 <td align="left">void <b>sha384_digest</b><i> </i>(<i>struct sha384_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
1259      </i></td>
1260 <td align="right">Function</td>
1261 </tr>
1262 </table>
1263 <table width="95%" align="center">
1264 <tr><td>
1265 Performs final processing and extracts the message digest, writing it
1266 to <var>digest</var>. <var>length</var> may be smaller than
1267 <code>SHA384_DIGEST_SIZE</code>, in which case only the first <var>length</var>
1268 octets of the digest are written.
1269
1270      <p>This function also resets the context in the same way as
1271 <code>sha384_init</code>. 
1272 </td></tr>
1273 </table>
1274
1275 <h3 class="subsection"><code>struct nettle_hash</code></h4>
1276
1277 <p>Nettle includes a struct including information about the supported hash
1278 functions. It is defined in <code>&lt;nettle/nettle-meta.h&gt;</code>, and is used
1279 by Nettle's implementation of <small>HMAC</small> see <a href="#Keyed%20hash%20functions">Keyed hash functions</a>.
1280
1281 <p>
1282 <table width="100%">
1283 <tr>
1284 <td align="left"><b></b><code>struct nettle_hash</code><b></b><i> name context_size digest_size block_size init update digest
1285      </i></td>
1286 <td align="right">Meta struct</td>
1287 </tr>
1288 </table>
1289 <table width="95%" align="center">
1290 <tr><td>
1291 The last three attributes are function pointers, of types
1292 <code>nettle_hash_init_func</code>, <code>nettle_hash_update_func</code>, and
1293 <code>nettle_hash_digest_func</code>. The first argument to these functions is
1294 <code>void *</code> pointer to a context struct, which is of size
1295 <code>context_size</code>. 
1296 </td></tr>
1297 </table>
1298
1299 <p>
1300 <table width="100%">
1301 <tr>
1302 <td align="left">struct nettle_hash <b>nettle_md2</b><i>
1303      </i></td>
1304 <td align="right">Constant Struct</td>
1305 </tr>
1306
1307 <tr>
1308 <td align="left">struct nettle_hash <b>nettle_md4</b><i>
1309      </i></td>
1310 <td align="right">Constant Struct</td>
1311 </tr>
1312
1313 <tr>
1314 <td align="left">struct nettle_hash <b>nettle_md5</b><i>
1315      </i></td>
1316 <td align="right">Constant Struct</td>
1317 </tr>
1318
1319 <tr>
1320 <td align="left">struct nettle_hash <b>nettle_sha1</b><i>
1321      </i></td>
1322 <td align="right">Constant Struct</td>
1323 </tr>
1324
1325 <tr>
1326 <td align="left">struct nettle_hash <b>nettle_sha224</b><i>
1327      </i></td>
1328 <td align="right">Constant Struct</td>
1329 </tr>
1330
1331 <tr>
1332 <td align="left">struct nettle_hash <b>nettle_sha256</b><i>
1333      </i></td>
1334 <td align="right">Constant Struct</td>
1335 </tr>
1336
1337 <tr>
1338 <td align="left">struct nettle_hash <b>nettle_sha384</b><i>
1339      </i></td>
1340 <td align="right">Constant Struct</td>
1341 </tr>
1342
1343 <tr>
1344 <td align="left">struct nettle_hash <b>nettle_sha512</b><i>
1345      </i></td>
1346 <td align="right">Constant Struct</td>
1347 </tr>
1348 </table>
1349 <table width="95%" align="center">
1350 <tr><td>
1351
1352      <p>These are all the hash functions that Nettle implements. 
1353 </td></tr>
1354 </table>
1355
1356 <div class="node">
1357 <p><hr>
1358 Node:&nbsp;<a name="Cipher%20functions">Cipher functions</a>,
1359 Next:&nbsp;<a rel="next" accesskey="n" href="#Cipher%20modes">Cipher modes</a>,
1360 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Hash%20functions">Hash functions</a>,
1361 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
1362 <br>
1363 </div>
1364
1365 <h3 class="section">Cipher functions</h3>
1366
1367 <p>A <dfn>cipher</dfn> is a function that takes a message or <dfn>plaintext</dfn>
1368 and a secret <dfn>key</dfn> and transforms it to a <dfn>ciphertext</dfn>. Given
1369 only the ciphertext, but not the key, it should be hard to find the
1370 plaintext. Given matching pairs of plaintext and ciphertext, it should
1371 be hard to find the key.
1372
1373    <p>There are two main classes of ciphers: Block ciphers and stream ciphers.
1374
1375    <p>A block cipher can process data only in fixed size chunks, called
1376 <dfn>blocks</dfn>. Typical block sizes are 8 or 16 octets. To encrypt
1377 arbitrary messages, you usually have to pad it to an integral number of
1378 blocks, split it into blocks, and then process each block. The simplest
1379 way is to process one block at a time, independent of each other. That
1380 mode of operation is called <dfn>ECB</dfn>, Electronic Code Book mode. 
1381 However, using <small>ECB</small> is usually a bad idea. For a start, plaintext blocks
1382 that are equal are transformed to ciphertext blocks that are equal; that
1383 leaks information about the plaintext. Usually you should apply the
1384 cipher is some "feedback mode", <dfn>CBC</dfn> (Cipher Block Chaining) and
1385 <dfn>CTR</dfn> (Counter mode) being two of
1386 of the most popular. See See <a href="#Cipher%20modes">Cipher modes</a>, for information on
1387 how to apply <small>CBC</small> and <small>CTR</small> with Nettle.
1388
1389    <p>A stream cipher can be used for messages of arbitrary length. A typical
1390 stream cipher is a keyed pseudo-random generator. To encrypt a plaintext
1391 message of <var>n</var> octets, you key the generator, generate <var>n</var>
1392 octets of pseudo-random data, and XOR it with the plaintext. To decrypt,
1393 regenerate the same stream using the key, XOR it to the ciphertext, and
1394 the plaintext is recovered.
1395
1396    <p><strong>Caution:</strong> The first rule for this kind of cipher is the
1397 same as for a One Time Pad: <em>never</em> ever use the same key twice.
1398
1399    <p>A common misconception is that encryption, by itself, implies
1400 authentication. Say that you and a friend share a secret key, and you
1401 receive an encrypted message. You apply the key, and get a plaintext
1402 message that makes sense to you. Can you then be sure that it really was
1403 your friend that wrote the message you're reading? The answer is no. For
1404 example, if you were using a block cipher in ECB mode, an attacker may
1405 pick up the message on its way, and reorder, delete or repeat some of
1406 the blocks. Even if the attacker can't decrypt the message, he can
1407 change it so that you are not reading the same message as your friend
1408 wrote. If you are using a block cipher in <small>CBC</small> mode rather than
1409 ECB, or are using a stream cipher, the possibilities for this sort of
1410 attack are different, but the attacker can still make predictable
1411 changes to the message.
1412
1413    <p>It is recommended to <em>always</em> use an authentication mechanism in
1414 addition to encrypting the messages. Popular choices are Message
1415 Authentication Codes like <small>HMAC-SHA1</small> see <a href="#Keyed%20hash%20functions">Keyed hash functions</a>, or digital signatures like <small>RSA</small>.
1416
1417    <p>Some ciphers have so called "weak keys", keys that results in
1418 undesirable structure after the key setup processing, and should be
1419 avoided. In Nettle, most key setup functions have no return value, but
1420 for ciphers with weak keys, the return value indicates whether or not
1421 the given key is weak. For good keys, key setup returns 1, and for weak
1422 keys, it returns 0. When possible, avoid algorithms that
1423 have weak keys. There are several good ciphers that don't have any weak
1424 keys.
1425
1426    <p>To encrypt a message, you first initialize a cipher context for
1427 encryption or decryption with a particular key. You then use the context
1428 to process plaintext or ciphertext messages. The initialization is known
1429 as <dfn>key setup</dfn>. With Nettle, it is recommended to use each
1430 context struct for only one direction, even if some of the ciphers use a
1431 single key setup function that can be used for both encryption and
1432 decryption.
1433
1434 <h3 class="subsection">AES</h4>
1435
1436 <p>AES is a block cipher, specified by NIST as a replacement for
1437 the older DES standard. The standard is the result of a competition
1438 between cipher designers. The winning design, also known as RIJNDAEL,
1439 was constructed by Joan Daemen and Vincent Rijnmen.
1440
1441    <p>Like all the AES candidates, the winning design uses a block size of 128
1442 bits, or 16 octets, and variable key-size, 128, 192 and 256 bits (16, 24
1443 and 32 octets) being the allowed key sizes. It does not have any weak
1444 keys. Nettle defines AES in <code>&lt;nettle/aes.h&gt;</code>.
1445
1446 <p>
1447 <table width="100%">
1448 <tr>
1449 <td align="left"><b>struct aes_ctx</b><i>
1450      </i></td>
1451 <td align="right">Context struct</td>
1452 </tr>
1453 </table>
1454 <table width="95%" align="center">
1455 <tr><td>
1456 </td></tr>
1457 </table>
1458
1459 <p>
1460 <table width="100%">
1461 <tr>
1462 <td align="left"><b>AES_BLOCK_SIZE</b><i>
1463      </i></td>
1464 <td align="right">Constant</td>
1465 </tr>
1466 </table>
1467 <table width="95%" align="center">
1468 <tr><td>
1469 The AES block-size, 16
1470 </td></tr>
1471 </table>
1472
1473 <p>
1474 <table width="100%">
1475 <tr>
1476 <td align="left"><b>AES_MIN_KEY_SIZE</b><i>
1477      </i></td>
1478 <td align="right">Constant</td>
1479 </tr>
1480 </table>
1481 <table width="95%" align="center">
1482 <tr><td>
1483 </td></tr>
1484 </table>
1485
1486 <p>
1487 <table width="100%">
1488 <tr>
1489 <td align="left"><b>AES_MAX_KEY_SIZE</b><i>
1490      </i></td>
1491 <td align="right">Constant</td>
1492 </tr>
1493 </table>
1494 <table width="95%" align="center">
1495 <tr><td>
1496 </td></tr>
1497 </table>
1498
1499 <p>
1500 <table width="100%">
1501 <tr>
1502 <td align="left"><b>AES_KEY_SIZE</b><i>
1503      </i></td>
1504 <td align="right">Constant</td>
1505 </tr>
1506 </table>
1507 <table width="95%" align="center">
1508 <tr><td>
1509 Default AES key size, 32
1510 </td></tr>
1511 </table>
1512
1513 <p>
1514 <table width="100%">
1515 <tr>
1516 <td align="left">void <b>aes_set_encrypt_key</b><i> </i>(<i>struct aes_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1517      </i></td>
1518 <td align="right">Function</td>
1519 </tr>
1520
1521 <tr>
1522 <td align="left">void <b>aes_set_decrypt_key</b><i> </i>(<i>struct aes_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1523      </i></td>
1524 <td align="right">Function</td>
1525 </tr>
1526 </table>
1527 <table width="95%" align="center">
1528 <tr><td>
1529 Initialize the cipher, for encryption or decryption, respectively. 
1530 </td></tr>
1531 </table>
1532
1533 <p>
1534 <table width="100%">
1535 <tr>
1536 <td align="left">void <b>aes_invert_key</b><i> </i>(<i>struct aes_ctx *</i><var>dst</var><i>, const struct aes_ctx *</i><var>src</var><i></i>)<i>
1537      </i></td>
1538 <td align="right">Function</td>
1539 </tr>
1540 </table>
1541 <table width="95%" align="center">
1542 <tr><td>
1543 Given a context <var>src</var> initialized for encryption, initializes the
1544 context struct <var>dst</var> for decryption, using the same key. If the same
1545 context struct is passed for both <code>src</code> and <code>dst</code>, it is
1546 converted in place. Calling <code>aes_set_encrypt_key</code> and
1547 <code>aes_invert_key</code> is more efficient than calling
1548 <code>aes_set_encrypt_key</code> and <code>aes_set_decrypt_key</code>. This function
1549 is mainly useful for applications which needs to both encrypt and
1550 decrypt using the <em>same</em> key. 
1551 </td></tr>
1552 </table>
1553
1554 <p>
1555 <table width="100%">
1556 <tr>
1557 <td align="left">void <b>aes_encrypt</b><i> </i>(<i>struct aes_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1558      </i></td>
1559 <td align="right">Function</td>
1560 </tr>
1561 </table>
1562 <table width="95%" align="center">
1563 <tr><td>
1564 Encryption function. <var>length</var> must be an integral multiple of the
1565 block size. If it is more than one block, the data is processed in ECB
1566 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
1567 in any other way. 
1568 </td></tr>
1569 </table>
1570
1571 <p>
1572 <table width="100%">
1573 <tr>
1574 <td align="left">void <b>aes_decrypt</b><i> </i>(<i>struct aes_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1575      </i></td>
1576 <td align="right">Function</td>
1577 </tr>
1578 </table>
1579 <table width="95%" align="center">
1580 <tr><td>
1581 Analogous to <code>aes_encrypt</code>
1582 </td></tr>
1583 </table>
1584
1585 <h3 class="subsection">ARCFOUR</h4>
1586
1587 <p>ARCFOUR is a stream cipher, also known under the trade marked name RC4,
1588 and it is one of the fastest ciphers around. A problem is that the key
1589 setup of ARCFOUR is quite weak, you should never use keys with
1590 structure, keys that are ordinary passwords, or sequences of keys like
1591 "secret:1", "secret:2", <small class="enddots">....</small>. If you have keys that don't look
1592 like random bit strings, and you want to use ARCFOUR, always hash the
1593 key before feeding it to ARCFOUR. Furthermore, the initial bytes of the
1594 generated key stream leak information about the key; for this reason, it
1595 is recommended to discard the first 512 bytes of the key stream.
1596
1597 <pre class="example">     /* A more robust key setup function for ARCFOUR */
1598      void
1599      arcfour_set_key_hashed(struct arcfour_ctx *ctx,
1600                             unsigned length, const uint8_t *key)
1601      {
1602        struct sha256_ctx hash;
1603        uint8_t digest[SHA256_DIGEST_SIZE];
1604        uint8_t buffer[0x200];
1605      
1606        sha256_init(&amp;hash);
1607        sha256_update(&amp;hash, length, key);
1608        sha256_digest(&amp;hash, SHA256_DIGEST_SIZE, digest);
1609      
1610        arcfour_set_key(ctx, SHA256_DIGEST_SIZE, digest);
1611        arcfour_crypt(ctx, sizeof(buffer), buffer, buffer);
1612      }
1613      </pre>
1614
1615    <p>Nettle defines ARCFOUR in <code>&lt;nettle/arcfour.h&gt;</code>.
1616
1617 <p>
1618 <table width="100%">
1619 <tr>
1620 <td align="left"><b>struct arcfour_ctx</b><i>
1621      </i></td>
1622 <td align="right">Context struct</td>
1623 </tr>
1624 </table>
1625 <table width="95%" align="center">
1626 <tr><td>
1627 </td></tr>
1628 </table>
1629
1630 <p>
1631 <table width="100%">
1632 <tr>
1633 <td align="left"><b>ARCFOUR_MIN_KEY_SIZE</b><i>
1634      </i></td>
1635 <td align="right">Constant</td>
1636 </tr>
1637 </table>
1638 <table width="95%" align="center">
1639 <tr><td>
1640 Minimum key size, 1
1641 </td></tr>
1642 </table>
1643
1644 <p>
1645 <table width="100%">
1646 <tr>
1647 <td align="left"><b>ARCFOUR_MAX_KEY_SIZE</b><i>
1648      </i></td>
1649 <td align="right">Constant</td>
1650 </tr>
1651 </table>
1652 <table width="95%" align="center">
1653 <tr><td>
1654 Maximum key size, 256
1655 </td></tr>
1656 </table>
1657
1658 <p>
1659 <table width="100%">
1660 <tr>
1661 <td align="left"><b>ARCFOUR_KEY_SIZE</b><i>
1662      </i></td>
1663 <td align="right">Constant</td>
1664 </tr>
1665 </table>
1666 <table width="95%" align="center">
1667 <tr><td>
1668 Default ARCFOUR key size, 16
1669 </td></tr>
1670 </table>
1671
1672 <p>
1673 <table width="100%">
1674 <tr>
1675 <td align="left">void <b>arcfour_set_key</b><i> </i>(<i>struct arcfour_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1676      </i></td>
1677 <td align="right">Function</td>
1678 </tr>
1679 </table>
1680 <table width="95%" align="center">
1681 <tr><td>
1682 Initialize the cipher. The same function is used for both encryption and
1683 decryption. 
1684 </td></tr>
1685 </table>
1686
1687 <p>
1688 <table width="100%">
1689 <tr>
1690 <td align="left">void <b>arcfour_crypt</b><i> </i>(<i>struct arcfour_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1691      </i></td>
1692 <td align="right">Function</td>
1693 </tr>
1694 </table>
1695 <table width="95%" align="center">
1696 <tr><td>
1697 Encrypt some data. The same function is used for both encryption and
1698 decryption. Unlike the block ciphers, this function modifies the
1699 context, so you can split the data into arbitrary chunks and encrypt
1700 them one after another. The result is the same as if you had called
1701 <code>arcfour_crypt</code> only once with all the data. 
1702 </td></tr>
1703 </table>
1704
1705 <h3 class="subsection">ARCTWO</h4>
1706
1707 <p>ARCTWO (also known as the trade marked name RC2) is a block cipher
1708 specified in RFC 2268. Nettle also include a variation of the ARCTWO
1709 set key operation that lack one step, to be compatible with the
1710 reverse engineered RC2 cipher description, as described in a Usenet
1711 post to <code>sci.crypt</code> by Peter Gutmann.
1712
1713    <p>ARCTWO uses a block size of 64 bits, and variable key-size ranging
1714 from 1 to 128 octets. Besides the key, ARCTWO also has a second
1715 parameter to key setup, the number of effective key bits, <code>ekb</code>. 
1716 This parameter can be used to artificially reduce the key size. In
1717 practice, <code>ekb</code> is usually set equal to the input key size. 
1718 Nettle defines ARCTWO in <code>&lt;nettle/arctwo.h&gt;</code>.
1719
1720    <p>We do not recommend the use of ARCTWO; the Nettle implementation is
1721 provided primarily for interoperability with existing applications and
1722 standards.
1723
1724 <p>
1725 <table width="100%">
1726 <tr>
1727 <td align="left"><b>struct arctwo_ctx</b><i>
1728      </i></td>
1729 <td align="right">Context struct</td>
1730 </tr>
1731 </table>
1732 <table width="95%" align="center">
1733 <tr><td>
1734 </td></tr>
1735 </table>
1736
1737 <p>
1738 <table width="100%">
1739 <tr>
1740 <td align="left"><b>ARCTWO_BLOCK_SIZE</b><i>
1741      </i></td>
1742 <td align="right">Constant</td>
1743 </tr>
1744 </table>
1745 <table width="95%" align="center">
1746 <tr><td>
1747 The AES block-size, 8
1748 </td></tr>
1749 </table>
1750
1751 <p>
1752 <table width="100%">
1753 <tr>
1754 <td align="left"><b>ARCTWO_MIN_KEY_SIZE</b><i>
1755      </i></td>
1756 <td align="right">Constant</td>
1757 </tr>
1758 </table>
1759 <table width="95%" align="center">
1760 <tr><td>
1761 </td></tr>
1762 </table>
1763
1764 <p>
1765 <table width="100%">
1766 <tr>
1767 <td align="left"><b>ARCTWO_MAX_KEY_SIZE</b><i>
1768      </i></td>
1769 <td align="right">Constant</td>
1770 </tr>
1771 </table>
1772 <table width="95%" align="center">
1773 <tr><td>
1774 </td></tr>
1775 </table>
1776
1777 <p>
1778 <table width="100%">
1779 <tr>
1780 <td align="left"><b>ARCTWO_KEY_SIZE</b><i>
1781      </i></td>
1782 <td align="right">Constant</td>
1783 </tr>
1784 </table>
1785 <table width="95%" align="center">
1786 <tr><td>
1787 Default ARCTWO key size, 8
1788 </td></tr>
1789 </table>
1790
1791 <p>
1792 <table width="100%">
1793 <tr>
1794 <td align="left">void <b>arctwo_set_key_ekb</b><i> </i>(<i>struct arctwo_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i>, unsigned </i><var>ekb</var><i></i>)<i>
1795      </i></td>
1796 <td align="right">Function</td>
1797 </tr>
1798
1799 <tr>
1800 <td align="left">void <b>arctwo_set_key</b><i> </i>(<i>struct arctwo_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1801      </i></td>
1802 <td align="right">Function</td>
1803 </tr>
1804
1805 <tr>
1806 <td align="left">void <b>arctwo_set_key_gutmann</b><i> </i>(<i>struct arctwo_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1807      </i></td>
1808 <td align="right">Function</td>
1809 </tr>
1810 </table>
1811 <table width="95%" align="center">
1812 <tr><td>
1813 Initialize the cipher. The same function is used for both encryption
1814 and decryption. The first function is the most general one, which lets
1815 you provide both the variable size key, and the desired effective key
1816 size (in bits). The maximum value for <var>ekb</var> is 1024, and for
1817 convenience, <code>ekb = 0</code> has the same effect as <code>ekb = 1024</code>.
1818
1819      <p><code>arctwo_set_key(ctx, length, key)</code> is equivalent to
1820 <code>arctwo_set_key_ekb(ctx, length, key, 8*length)</code>, and
1821 <code>arctwo_set_key_gutmann(ctx, length, key)</code> is equivalent to
1822 <code>arctwo_set_key_ekb(ctx, length, key, 1024)</code>
1823 </td></tr>
1824 </table>
1825
1826 <p>
1827 <table width="100%">
1828 <tr>
1829 <td align="left">void <b>arctwo_encrypt</b><i> </i>(<i>struct arctwo_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1830      </i></td>
1831 <td align="right">Function</td>
1832 </tr>
1833 </table>
1834 <table width="95%" align="center">
1835 <tr><td>
1836 Encryption function. <var>length</var> must be an integral multiple of the
1837 block size. If it is more than one block, the data is processed in ECB
1838 mode. <code>src</code> and <code>dst</code> may be equal, but they must not
1839 overlap in any other way. 
1840 </td></tr>
1841 </table>
1842
1843 <p>
1844 <table width="100%">
1845 <tr>
1846 <td align="left">void <b>arctwo_decrypt</b><i> </i>(<i>struct arctwo_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1847      </i></td>
1848 <td align="right">Function</td>
1849 </tr>
1850 </table>
1851 <table width="95%" align="center">
1852 <tr><td>
1853 Analogous to <code>arctwo_encrypt</code>
1854 </td></tr>
1855 </table>
1856
1857 <h3 class="subsection">BLOWFISH</h4>
1858
1859 <p>BLOWFISH is a block cipher designed by Bruce Schneier. It uses a block
1860 size of 64 bits (8 octets), and a variable key size, up to 448 bits. It
1861 has some weak keys. Nettle defines BLOWFISH in <code>&lt;nettle/blowfish.h&gt;</code>.
1862
1863 <p>
1864 <table width="100%">
1865 <tr>
1866 <td align="left"><b>struct blowfish_ctx</b><i>
1867      </i></td>
1868 <td align="right">Context struct</td>
1869 </tr>
1870 </table>
1871 <table width="95%" align="center">
1872 <tr><td>
1873 </td></tr>
1874 </table>
1875
1876 <p>
1877 <table width="100%">
1878 <tr>
1879 <td align="left"><b>BLOWFISH_BLOCK_SIZE</b><i>
1880      </i></td>
1881 <td align="right">Constant</td>
1882 </tr>
1883 </table>
1884 <table width="95%" align="center">
1885 <tr><td>
1886 The BLOWFISH block-size, 8
1887 </td></tr>
1888 </table>
1889
1890 <p>
1891 <table width="100%">
1892 <tr>
1893 <td align="left"><b>BLOWFISH_MIN_KEY_SIZE</b><i>
1894      </i></td>
1895 <td align="right">Constant</td>
1896 </tr>
1897 </table>
1898 <table width="95%" align="center">
1899 <tr><td>
1900 Minimum BLOWFISH key size, 8
1901 </td></tr>
1902 </table>
1903
1904 <p>
1905 <table width="100%">
1906 <tr>
1907 <td align="left"><b>BLOWFISH_MAX_KEY_SIZE</b><i>
1908      </i></td>
1909 <td align="right">Constant</td>
1910 </tr>
1911 </table>
1912 <table width="95%" align="center">
1913 <tr><td>
1914 Maximum BLOWFISH key size, 56
1915 </td></tr>
1916 </table>
1917
1918 <p>
1919 <table width="100%">
1920 <tr>
1921 <td align="left"><b>BLOWFISH_KEY_SIZE</b><i>
1922      </i></td>
1923 <td align="right">Constant</td>
1924 </tr>
1925 </table>
1926 <table width="95%" align="center">
1927 <tr><td>
1928 Default BLOWFISH key size, 16
1929 </td></tr>
1930 </table>
1931
1932 <p>
1933 <table width="100%">
1934 <tr>
1935 <td align="left">int <b>blowfish_set_key</b><i> </i>(<i>struct blowfish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
1936      </i></td>
1937 <td align="right">Function</td>
1938 </tr>
1939 </table>
1940 <table width="95%" align="center">
1941 <tr><td>
1942 Initialize the cipher. The same function is used for both encryption and
1943 decryption. Checks for weak keys, returning 1
1944 for good keys and 0 for weak keys. Applications that don't care about
1945 weak keys can ignore the return value.
1946
1947      <p><code>blowfish_encrypt</code> or <code>blowfish_decrypt</code> with a weak key will
1948 crash with an assert violation. 
1949 </td></tr>
1950 </table>
1951
1952 <p>
1953 <table width="100%">
1954 <tr>
1955 <td align="left">void <b>blowfish_encrypt</b><i> </i>(<i>struct blowfish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1956      </i></td>
1957 <td align="right">Function</td>
1958 </tr>
1959 </table>
1960 <table width="95%" align="center">
1961 <tr><td>
1962 Encryption function. <var>length</var> must be an integral multiple of the
1963 block size. If it is more than one block, the data is processed in ECB
1964 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
1965 in any other way. 
1966 </td></tr>
1967 </table>
1968
1969 <p>
1970 <table width="100%">
1971 <tr>
1972 <td align="left">void <b>blowfish_decrypt</b><i> </i>(<i>struct blowfish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
1973      </i></td>
1974 <td align="right">Function</td>
1975 </tr>
1976 </table>
1977 <table width="95%" align="center">
1978 <tr><td>
1979 Analogous to <code>blowfish_encrypt</code>
1980 </td></tr>
1981 </table>
1982
1983 <h3 class="subsection">Camellia</h4>
1984
1985 <p>Camellia is a block cipher developed by Mitsubishi and Nippon Telegraph
1986 and Telephone Corporation, described in <cite>RFC3713</cite>, and recommended
1987 by some Japanese and European authorities as an alternative to AES. The
1988 algorithm is patented. The implementation in Nettle is derived from the
1989 implementation released by NTT under the GNU LGPL (v2.1 or later), and
1990 relies on the implicit patent license of the LGPL. There is also a
1991 statement of royalty-free licensing for Camellia at
1992 &lt;<code>http://www.ntt.co.jp/news/news01e/0104/010417.html</code>&gt;, but this
1993 statement has some limitations which seem problematic for free software.
1994
1995    <p>Camellia uses a the same block size and key sizes as AES: The block size
1996 is 128 bits (16 octets), and the supported key sizes are 128, 192, and
1997 256 bits. Nettle defines Camellia in <code>&lt;nettle/camellia.h&gt;</code>.
1998
1999 <p>
2000 <table width="100%">
2001 <tr>
2002 <td align="left"><b>struct camellia_ctx</b><i>
2003      </i></td>
2004 <td align="right">Context struct</td>
2005 </tr>
2006 </table>
2007 <table width="95%" align="center">
2008 <tr><td>
2009 </td></tr>
2010 </table>
2011
2012 <p>
2013 <table width="100%">
2014 <tr>
2015 <td align="left"><b>CAMELLIA_BLOCK_SIZE</b><i>
2016      </i></td>
2017 <td align="right">Constant</td>
2018 </tr>
2019 </table>
2020 <table width="95%" align="center">
2021 <tr><td>
2022 The CAMELLIA block-size, 16
2023 </td></tr>
2024 </table>
2025
2026 <p>
2027 <table width="100%">
2028 <tr>
2029 <td align="left"><b>CAMELLIA_MIN_KEY_SIZE</b><i>
2030      </i></td>
2031 <td align="right">Constant</td>
2032 </tr>
2033 </table>
2034 <table width="95%" align="center">
2035 <tr><td>
2036 </td></tr>
2037 </table>
2038
2039 <p>
2040 <table width="100%">
2041 <tr>
2042 <td align="left"><b>CAMELLIA_MAX_KEY_SIZE</b><i>
2043      </i></td>
2044 <td align="right">Constant</td>
2045 </tr>
2046 </table>
2047 <table width="95%" align="center">
2048 <tr><td>
2049 </td></tr>
2050 </table>
2051
2052 <p>
2053 <table width="100%">
2054 <tr>
2055 <td align="left"><b>CAMELLIA_KEY_SIZE</b><i>
2056      </i></td>
2057 <td align="right">Constant</td>
2058 </tr>
2059 </table>
2060 <table width="95%" align="center">
2061 <tr><td>
2062 Default CAMELLIA key size, 32
2063 </td></tr>
2064 </table>
2065
2066 <p>
2067 <table width="100%">
2068 <tr>
2069 <td align="left">void <b>camellia_set_encrypt_key</b><i> </i>(<i>struct camellia_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2070      </i></td>
2071 <td align="right">Function</td>
2072 </tr>
2073
2074 <tr>
2075 <td align="left">void <b>camellia_set_decrypt_key</b><i> </i>(<i>struct camellia_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2076      </i></td>
2077 <td align="right">Function</td>
2078 </tr>
2079 </table>
2080 <table width="95%" align="center">
2081 <tr><td>
2082 Initialize the cipher, for encryption or decryption, respectively. 
2083 </td></tr>
2084 </table>
2085
2086 <p>
2087 <table width="100%">
2088 <tr>
2089 <td align="left">void <b>camellia_invert_key</b><i> </i>(<i>struct camellia_ctx *</i><var>dst</var><i>, const struct camellia_ctx *</i><var>src</var><i></i>)<i>
2090      </i></td>
2091 <td align="right">Function</td>
2092 </tr>
2093 </table>
2094 <table width="95%" align="center">
2095 <tr><td>
2096 Given a context <var>src</var> initialized for encryption, initializes the
2097 context struct <var>dst</var> for decryption, using the same key. If the same
2098 context struct is passed for both <code>src</code> and <code>dst</code>, it is
2099 converted in place. Calling <code>camellia_set_encrypt_key</code> and
2100 <code>camellia_invert_key</code> is more efficient than calling
2101 <code>camellia_set_encrypt_key</code> and <code>camellia_set_decrypt_key</code>. This function
2102 is mainly useful for applications which needs to both encrypt and
2103 decrypt using the <em>same</em> key. 
2104 </td></tr>
2105 </table>
2106
2107 <p>
2108 <table width="100%">
2109 <tr>
2110 <td align="left">void <b>camellia_crypt</b><i> </i>(<i>struct camellia_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2111      </i></td>
2112 <td align="right">Function</td>
2113 </tr>
2114 </table>
2115 <table width="95%" align="center">
2116 <tr><td>
2117 The same function is used for both encryption and decryption. 
2118 <var>length</var> must be an integral multiple of the block size. If it is
2119 more than one block, the data is processed in ECB mode. <code>src</code> and
2120 <code>dst</code> may be equal, but they must not overlap in any other way. 
2121 </td></tr>
2122 </table>
2123
2124 <h3 class="subsection">CAST128</h4>
2125
2126 <p>CAST-128 is a block cipher, specified in <cite>RFC 2144</cite>. It uses a 64
2127 bit (8 octets) block size, and a variable key size of up to 128 bits. 
2128 Nettle defines cast128 in <code>&lt;nettle/cast128.h&gt;</code>.
2129
2130 <p>
2131 <table width="100%">
2132 <tr>
2133 <td align="left"><b>struct cast128_ctx</b><i>
2134      </i></td>
2135 <td align="right">Context struct</td>
2136 </tr>
2137 </table>
2138 <table width="95%" align="center">
2139 <tr><td>
2140 </td></tr>
2141 </table>
2142
2143 <p>
2144 <table width="100%">
2145 <tr>
2146 <td align="left"><b>CAST128_BLOCK_SIZE</b><i>
2147      </i></td>
2148 <td align="right">Constant</td>
2149 </tr>
2150 </table>
2151 <table width="95%" align="center">
2152 <tr><td>
2153 The CAST128 block-size, 8
2154 </td></tr>
2155 </table>
2156
2157 <p>
2158 <table width="100%">
2159 <tr>
2160 <td align="left"><b>CAST128_MIN_KEY_SIZE</b><i>
2161      </i></td>
2162 <td align="right">Constant</td>
2163 </tr>
2164 </table>
2165 <table width="95%" align="center">
2166 <tr><td>
2167 Minimum CAST128 key size, 5
2168 </td></tr>
2169 </table>
2170
2171 <p>
2172 <table width="100%">
2173 <tr>
2174 <td align="left"><b>CAST128_MAX_KEY_SIZE</b><i>
2175      </i></td>
2176 <td align="right">Constant</td>
2177 </tr>
2178 </table>
2179 <table width="95%" align="center">
2180 <tr><td>
2181 Maximum CAST128 key size, 16
2182 </td></tr>
2183 </table>
2184
2185 <p>
2186 <table width="100%">
2187 <tr>
2188 <td align="left"><b>CAST128_KEY_SIZE</b><i>
2189      </i></td>
2190 <td align="right">Constant</td>
2191 </tr>
2192 </table>
2193 <table width="95%" align="center">
2194 <tr><td>
2195 Default CAST128 key size, 16
2196 </td></tr>
2197 </table>
2198
2199 <p>
2200 <table width="100%">
2201 <tr>
2202 <td align="left">void <b>cast128_set_key</b><i> </i>(<i>struct cast128_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2203      </i></td>
2204 <td align="right">Function</td>
2205 </tr>
2206 </table>
2207 <table width="95%" align="center">
2208 <tr><td>
2209 Initialize the cipher. The same function is used for both encryption and
2210 decryption. 
2211 </td></tr>
2212 </table>
2213
2214 <p>
2215 <table width="100%">
2216 <tr>
2217 <td align="left">void <b>cast128_encrypt</b><i> </i>(<i>struct cast128_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2218      </i></td>
2219 <td align="right">Function</td>
2220 </tr>
2221 </table>
2222 <table width="95%" align="center">
2223 <tr><td>
2224 Encryption function. <var>length</var> must be an integral multiple of the
2225 block size. If it is more than one block, the data is processed in ECB
2226 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
2227 in any other way. 
2228 </td></tr>
2229 </table>
2230
2231 <p>
2232 <table width="100%">
2233 <tr>
2234 <td align="left">void <b>cast128_decrypt</b><i> </i>(<i>struct cast128_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2235      </i></td>
2236 <td align="right">Function</td>
2237 </tr>
2238 </table>
2239 <table width="95%" align="center">
2240 <tr><td>
2241 Analogous to <code>cast128_encrypt</code>
2242 </td></tr>
2243 </table>
2244
2245 <h3 class="subsection">DES</h4>
2246
2247 <p>DES is the old Data Encryption Standard, specified by NIST. It uses a
2248 block size of 64 bits (8 octets), and a key size of 56 bits. However,
2249 the key bits are distributed over 8 octets, where the least significant
2250 bit of each octet may be used for parity. A common way to use DES is to
2251 generate 8 random octets in some way, then set the least significant bit
2252 of each octet to get odd parity, and initialize DES with the resulting
2253 key.
2254
2255    <p>The key size of DES is so small that keys can be found by brute force,
2256 using specialized hardware or lots of ordinary work stations in
2257 parallel. One shouldn't be using plain DES at all today, if one uses
2258 DES at all one should be using "triple DES", see DES3 below.
2259
2260    <p>DES also has some weak keys. Nettle defines DES in <code>&lt;nettle/des.h&gt;</code>.
2261
2262 <p>
2263 <table width="100%">
2264 <tr>
2265 <td align="left"><b>struct des_ctx</b><i>
2266      </i></td>
2267 <td align="right">Context struct</td>
2268 </tr>
2269 </table>
2270 <table width="95%" align="center">
2271 <tr><td>
2272 </td></tr>
2273 </table>
2274
2275 <p>
2276 <table width="100%">
2277 <tr>
2278 <td align="left"><b>DES_BLOCK_SIZE</b><i>
2279      </i></td>
2280 <td align="right">Constant</td>
2281 </tr>
2282 </table>
2283 <table width="95%" align="center">
2284 <tr><td>
2285 The DES block-size, 8
2286 </td></tr>
2287 </table>
2288
2289 <p>
2290 <table width="100%">
2291 <tr>
2292 <td align="left"><b>DES_KEY_SIZE</b><i>
2293      </i></td>
2294 <td align="right">Constant</td>
2295 </tr>
2296 </table>
2297 <table width="95%" align="center">
2298 <tr><td>
2299 DES key size, 8
2300 </td></tr>
2301 </table>
2302
2303 <p>
2304 <table width="100%">
2305 <tr>
2306 <td align="left">int <b>des_set_key</b><i> </i>(<i>struct des_ctx *</i><var>ctx</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2307      </i></td>
2308 <td align="right">Function</td>
2309 </tr>
2310 </table>
2311 <table width="95%" align="center">
2312 <tr><td>
2313 Initialize the cipher. The same function is used for both encryption and
2314 decryption. Parity bits are ignored. Checks for weak keys, returning 1
2315 for good keys and 0 for weak keys. Applications that don't care about
2316 weak keys can ignore the return value. 
2317 </td></tr>
2318 </table>
2319
2320 <p>
2321 <table width="100%">
2322 <tr>
2323 <td align="left">void <b>des_encrypt</b><i> </i>(<i>struct des_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2324      </i></td>
2325 <td align="right">Function</td>
2326 </tr>
2327 </table>
2328 <table width="95%" align="center">
2329 <tr><td>
2330 Encryption function. <var>length</var> must be an integral multiple of the
2331 block size. If it is more than one block, the data is processed in ECB
2332 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
2333 in any other way. 
2334 </td></tr>
2335 </table>
2336
2337 <p>
2338 <table width="100%">
2339 <tr>
2340 <td align="left">void <b>des_decrypt</b><i> </i>(<i>struct des_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2341      </i></td>
2342 <td align="right">Function</td>
2343 </tr>
2344 </table>
2345 <table width="95%" align="center">
2346 <tr><td>
2347 Analogous to <code>des_encrypt</code>
2348 </td></tr>
2349 </table>
2350
2351 <p>
2352 <table width="100%">
2353 <tr>
2354 <td align="left">int <b>des_check_parity</b><i> </i>(<i>unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>;
2355      </i></td>
2356 <td align="right">Function</td>
2357 </tr>
2358 </table>
2359 <table width="95%" align="center">
2360 <tr><td>
2361 Checks that the given key has correct, odd, parity. Returns 1 for
2362 correct parity, and 0 for bad parity. 
2363 </td></tr>
2364 </table>
2365
2366 <p>
2367 <table width="100%">
2368 <tr>
2369 <td align="left">void <b>des_fix_parity</b><i> </i>(<i>unsigned </i><var>length</var><i>, uint8_t *</i><var>dst</var><i>, const uint8_t *</i><var>src</var><i></i>)<i>
2370      </i></td>
2371 <td align="right">Function</td>
2372 </tr>
2373 </table>
2374 <table width="95%" align="center">
2375 <tr><td>
2376 Adjusts the parity bits to match DES's requirements. You need this
2377 function if you have created a random-looking string by a key agreement
2378 protocol, and want to use it as a DES key. <var>dst</var> and <var>src</var> may
2379 be equal. 
2380 </td></tr>
2381 </table>
2382
2383 <h3 class="subsection">DES3</h4>
2384
2385 <p>The inadequate key size of DES has already been mentioned. One way to
2386 increase the key size is to pipe together several DES boxes with
2387 independent keys. It turns out that using two DES ciphers is not as
2388 secure as one might think, even if the key size of the combination is a
2389 respectable 112 bits.
2390
2391    <p>The standard way to increase DES's key size is to use three DES boxes. 
2392 The mode of operation is a little peculiar: the middle DES box is wired
2393 in the reverse direction. To encrypt a block with DES3, you encrypt it
2394 using the first 56 bits of the key, then <em>decrypt</em> it using the
2395 middle 56 bits of the key, and finally encrypt it again using the last
2396 56 bits of the key. This is known as "ede" triple-DES, for
2397 "encrypt-decrypt-encrypt".
2398
2399    <p>The "ede" construction provides some backward compatibility, as you get
2400 plain single DES simply by feeding the same key to all three boxes. That
2401 should help keeping down the gate count, and the price, of hardware
2402 circuits implementing both plain DES and DES3.
2403
2404    <p>DES3 has a key size of 168 bits, but just like plain DES, useless parity
2405 bits are inserted, so that keys are represented as 24 octets (192 bits). 
2406 As a 112 bit key is large enough to make brute force attacks
2407 impractical, some applications uses a "two-key" variant of triple-DES. 
2408 In this mode, the same key bits are used for the first and the last DES
2409 box in the pipe, while the middle box is keyed independently. The
2410 two-key variant is believed to be secure, i.e. there are no known
2411 attacks significantly better than brute force.
2412
2413    <p>Naturally, it's simple to implement triple-DES on top of Nettle's DES
2414 functions. Nettle includes an implementation of three-key "ede"
2415 triple-DES, it is defined in the same place as plain DES,
2416 <code>&lt;nettle/des.h&gt;</code>.
2417
2418 <p>
2419 <table width="100%">
2420 <tr>
2421 <td align="left"><b>struct des3_ctx</b><i>
2422      </i></td>
2423 <td align="right">Context struct</td>
2424 </tr>
2425 </table>
2426 <table width="95%" align="center">
2427 <tr><td>
2428 </td></tr>
2429 </table>
2430
2431 <p>
2432 <table width="100%">
2433 <tr>
2434 <td align="left"><b>DES3_BLOCK_SIZE</b><i>
2435      </i></td>
2436 <td align="right">Constant</td>
2437 </tr>
2438 </table>
2439 <table width="95%" align="center">
2440 <tr><td>
2441 The DES3 block-size is the same as DES_BLOCK_SIZE, 8
2442 </td></tr>
2443 </table>
2444
2445 <p>
2446 <table width="100%">
2447 <tr>
2448 <td align="left"><b>DES3_KEY_SIZE</b><i>
2449      </i></td>
2450 <td align="right">Constant</td>
2451 </tr>
2452 </table>
2453 <table width="95%" align="center">
2454 <tr><td>
2455 DES key size, 24
2456 </td></tr>
2457 </table>
2458
2459 <p>
2460 <table width="100%">
2461 <tr>
2462 <td align="left">int <b>des3_set_key</b><i> </i>(<i>struct des3_ctx *</i><var>ctx</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2463      </i></td>
2464 <td align="right">Function</td>
2465 </tr>
2466 </table>
2467 <table width="95%" align="center">
2468 <tr><td>
2469 Initialize the cipher. The same function is used for both encryption and
2470 decryption. Parity bits are ignored. Checks for weak keys, returning 1
2471 if all three keys are good keys, and 0 if one or more key is weak. 
2472 Applications that don't care about weak keys can ignore the return
2473 value. 
2474 </td></tr>
2475 </table>
2476
2477    <p>For random-looking strings, you can use <code>des_fix_parity</code> to adjust
2478 the parity bits before calling <code>des3_set_key</code>.
2479
2480 <p>
2481 <table width="100%">
2482 <tr>
2483 <td align="left">void <b>des3_encrypt</b><i> </i>(<i>struct des3_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2484      </i></td>
2485 <td align="right">Function</td>
2486 </tr>
2487 </table>
2488 <table width="95%" align="center">
2489 <tr><td>
2490 Encryption function. <var>length</var> must be an integral multiple of the
2491 block size. If it is more than one block, the data is processed in ECB
2492 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
2493 in any other way. 
2494 </td></tr>
2495 </table>
2496
2497 <p>
2498 <table width="100%">
2499 <tr>
2500 <td align="left">void <b>des3_decrypt</b><i> </i>(<i>struct des3_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2501      </i></td>
2502 <td align="right">Function</td>
2503 </tr>
2504 </table>
2505 <table width="95%" align="center">
2506 <tr><td>
2507 Analogous to <code>des_encrypt</code>
2508 </td></tr>
2509 </table>
2510
2511 <h3 class="subsection">SERPENT</h4>
2512
2513 <p>SERPENT is one of the AES finalists, designed by Ross Anderson, Eli
2514 Biham and Lars Knudsen. Thus, the interface and properties are similar
2515 to AES'. One peculiarity is that it is quite pointless to use it with
2516 anything but the maximum key size, smaller keys are just padded to
2517 larger ones. Nettle defines SERPENT in <code>&lt;nettle/serpent.h&gt;</code>.
2518
2519 <p>
2520 <table width="100%">
2521 <tr>
2522 <td align="left"><b>struct serpent_ctx</b><i>
2523      </i></td>
2524 <td align="right">Context struct</td>
2525 </tr>
2526 </table>
2527 <table width="95%" align="center">
2528 <tr><td>
2529 </td></tr>
2530 </table>
2531
2532 <p>
2533 <table width="100%">
2534 <tr>
2535 <td align="left"><b>SERPENT_BLOCK_SIZE</b><i>
2536      </i></td>
2537 <td align="right">Constant</td>
2538 </tr>
2539 </table>
2540 <table width="95%" align="center">
2541 <tr><td>
2542 The SERPENT block-size, 16
2543 </td></tr>
2544 </table>
2545
2546 <p>
2547 <table width="100%">
2548 <tr>
2549 <td align="left"><b>SERPENT_MIN_KEY_SIZE</b><i>
2550      </i></td>
2551 <td align="right">Constant</td>
2552 </tr>
2553 </table>
2554 <table width="95%" align="center">
2555 <tr><td>
2556 Minimum SERPENT key size, 16
2557 </td></tr>
2558 </table>
2559
2560 <p>
2561 <table width="100%">
2562 <tr>
2563 <td align="left"><b>SERPENT_MAX_KEY_SIZE</b><i>
2564      </i></td>
2565 <td align="right">Constant</td>
2566 </tr>
2567 </table>
2568 <table width="95%" align="center">
2569 <tr><td>
2570 Maximum SERPENT key size, 32
2571 </td></tr>
2572 </table>
2573
2574 <p>
2575 <table width="100%">
2576 <tr>
2577 <td align="left"><b>SERPENT_KEY_SIZE</b><i>
2578      </i></td>
2579 <td align="right">Constant</td>
2580 </tr>
2581 </table>
2582 <table width="95%" align="center">
2583 <tr><td>
2584 Default SERPENT key size, 32
2585 </td></tr>
2586 </table>
2587
2588 <p>
2589 <table width="100%">
2590 <tr>
2591 <td align="left">void <b>serpent_set_key</b><i> </i>(<i>struct serpent_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2592      </i></td>
2593 <td align="right">Function</td>
2594 </tr>
2595 </table>
2596 <table width="95%" align="center">
2597 <tr><td>
2598 Initialize the cipher. The same function is used for both encryption and
2599 decryption. 
2600 </td></tr>
2601 </table>
2602
2603 <p>
2604 <table width="100%">
2605 <tr>
2606 <td align="left">void <b>serpent_encrypt</b><i> </i>(<i>struct serpent_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2607      </i></td>
2608 <td align="right">Function</td>
2609 </tr>
2610 </table>
2611 <table width="95%" align="center">
2612 <tr><td>
2613 Encryption function. <var>length</var> must be an integral multiple of the
2614 block size. If it is more than one block, the data is processed in ECB
2615 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
2616 in any other way. 
2617 </td></tr>
2618 </table>
2619
2620 <p>
2621 <table width="100%">
2622 <tr>
2623 <td align="left">void <b>serpent_decrypt</b><i> </i>(<i>struct serpent_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2624      </i></td>
2625 <td align="right">Function</td>
2626 </tr>
2627 </table>
2628 <table width="95%" align="center">
2629 <tr><td>
2630 Analogous to <code>serpent_encrypt</code>
2631 </td></tr>
2632 </table>
2633
2634 <h3 class="subsection">TWOFISH</h4>
2635
2636 <p>Another AES finalist, this one designed by Bruce Schneier and others. 
2637 Nettle defines it in <code>&lt;nettle/twofish.h&gt;</code>.
2638
2639 <p>
2640 <table width="100%">
2641 <tr>
2642 <td align="left"><b>struct twofish_ctx</b><i>
2643      </i></td>
2644 <td align="right">Context struct</td>
2645 </tr>
2646 </table>
2647 <table width="95%" align="center">
2648 <tr><td>
2649 </td></tr>
2650 </table>
2651
2652 <p>
2653 <table width="100%">
2654 <tr>
2655 <td align="left"><b>TWOFISH_BLOCK_SIZE</b><i>
2656      </i></td>
2657 <td align="right">Constant</td>
2658 </tr>
2659 </table>
2660 <table width="95%" align="center">
2661 <tr><td>
2662 The TWOFISH block-size, 16
2663 </td></tr>
2664 </table>
2665
2666 <p>
2667 <table width="100%">
2668 <tr>
2669 <td align="left"><b>TWOFISH_MIN_KEY_SIZE</b><i>
2670      </i></td>
2671 <td align="right">Constant</td>
2672 </tr>
2673 </table>
2674 <table width="95%" align="center">
2675 <tr><td>
2676 Minimum TWOFISH key size, 16
2677 </td></tr>
2678 </table>
2679
2680 <p>
2681 <table width="100%">
2682 <tr>
2683 <td align="left"><b>TWOFISH_MAX_KEY_SIZE</b><i>
2684      </i></td>
2685 <td align="right">Constant</td>
2686 </tr>
2687 </table>
2688 <table width="95%" align="center">
2689 <tr><td>
2690 Maximum TWOFISH key size, 32
2691 </td></tr>
2692 </table>
2693
2694 <p>
2695 <table width="100%">
2696 <tr>
2697 <td align="left"><b>TWOFISH_KEY_SIZE</b><i>
2698      </i></td>
2699 <td align="right">Constant</td>
2700 </tr>
2701 </table>
2702 <table width="95%" align="center">
2703 <tr><td>
2704 Default TWOFISH key size, 32
2705 </td></tr>
2706 </table>
2707
2708 <p>
2709 <table width="100%">
2710 <tr>
2711 <td align="left">void <b>twofish_set_key</b><i> </i>(<i>struct twofish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
2712      </i></td>
2713 <td align="right">Function</td>
2714 </tr>
2715 </table>
2716 <table width="95%" align="center">
2717 <tr><td>
2718 Initialize the cipher. The same function is used for both encryption and
2719 decryption. 
2720 </td></tr>
2721 </table>
2722
2723 <p>
2724 <table width="100%">
2725 <tr>
2726 <td align="left">void <b>twofish_encrypt</b><i> </i>(<i>struct twofish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2727      </i></td>
2728 <td align="right">Function</td>
2729 </tr>
2730 </table>
2731 <table width="95%" align="center">
2732 <tr><td>
2733 Encryption function. <var>length</var> must be an integral multiple of the
2734 block size. If it is more than one block, the data is processed in ECB
2735 mode. <code>src</code> and <code>dst</code> may be equal, but they must not overlap
2736 in any other way. 
2737 </td></tr>
2738 </table>
2739
2740 <p>
2741 <table width="100%">
2742 <tr>
2743 <td align="left">void <b>twofish_decrypt</b><i> </i>(<i>struct twofish_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>dst</var><i>, uint8_t *</i><var>src</var><i></i>)<i>
2744      </i></td>
2745 <td align="right">Function</td>
2746 </tr>
2747 </table>
2748 <table width="95%" align="center">
2749 <tr><td>
2750 Analogous to <code>twofish_encrypt</code>
2751 </td></tr>
2752 </table>
2753
2754 <h3 class="subsection"><code>struct nettle_cipher</code></h4>
2755
2756 <p>Nettle includes a struct including information about some of the more
2757 regular cipher functions. It should be considered a little experimental,
2758 but can be useful for applications that need a simple way to handle
2759 various algorithms. Nettle defines these structs in
2760 <code>&lt;nettle/nettle-meta.h&gt;</code>.
2761
2762 <p>
2763 <table width="100%">
2764 <tr>
2765 <td align="left"><b></b><code>struct nettle_cipher</code><b></b><i> name context_size block_size key_size set_encrypt_key set_decrypt_key encrypt decrypt
2766      </i></td>
2767 <td align="right">Meta struct</td>
2768 </tr>
2769 </table>
2770 <table width="95%" align="center">
2771 <tr><td>
2772 The last four attributes are function pointers, of types
2773 <code>nettle_set_key_func</code> and <code>nettle_crypt_func</code>. The first
2774 argument to these functions is a <code>void *</code> pointer to a context
2775 struct, which is of size <code>context_size</code>. 
2776 </td></tr>
2777 </table>
2778
2779 <p>
2780 <table width="100%">
2781 <tr>
2782 <td align="left">struct nettle_cipher <b>nettle_aes128</b><i>
2783      </i></td>
2784 <td align="right">Constant Struct</td>
2785 </tr>
2786
2787 <tr>
2788 <td align="left">struct nettle_cipher <b>nettle_aes192</b><i>
2789      </i></td>
2790 <td align="right">Constant Struct</td>
2791 </tr>
2792
2793 <tr>
2794 <td align="left">struct nettle_cipher <b>nettle_aes256</b><i>
2795      </i></td>
2796 <td align="right">Constant Struct</td>
2797 </tr>
2798 </table>
2799 <table width="95%" align="center">
2800 <tr><td>
2801
2802 <tr>
2803 <td align="left">struct nettle_cipher <b>nettle_arctwo40;</b><i>
2804      </i></td>
2805 <td align="right">Constant Struct</td>
2806 </tr>
2807
2808 <tr>
2809 <td align="left">struct nettle_cipher <b>nettle_arctwo64;</b><i>
2810      </i></td>
2811 <td align="right">Constant Struct</td>
2812 </tr>
2813
2814 <tr>
2815 <td align="left">struct nettle_cipher <b>nettle_arctwo128;</b><i>
2816      </i></td>
2817 <td align="right">Constant Struct</td>
2818 </tr>
2819
2820 <tr>
2821 <td align="left">struct nettle_cipher <b>nettle_arctwo_gutmann128;</b><i>
2822      </i></td>
2823 <td align="right">Constant Struct</td>
2824 </tr>
2825 </table>
2826 <table width="95%" align="center">
2827 <tr><td>
2828
2829 <tr>
2830 <td align="left">struct nettle_cipher <b>nettle_arcfour128</b><i>
2831      </i></td>
2832 <td align="right">Constant Struct</td>
2833 </tr>
2834 </table>
2835 <table width="95%" align="center">
2836 <tr><td>
2837
2838 <tr>
2839 <td align="left">struct nettle_cipher <b>nettle_camellia128</b><i>
2840      </i></td>
2841 <td align="right">Constant Struct</td>
2842 </tr>
2843
2844 <tr>
2845 <td align="left">struct nettle_cipher <b>nettle_camellia192</b><i>
2846      </i></td>
2847 <td align="right">Constant Struct</td>
2848 </tr>
2849
2850 <tr>
2851 <td align="left">struct nettle_cipher <b>nettle_camellia256</b><i>
2852      </i></td>
2853 <td align="right">Constant Struct</td>
2854 </tr>
2855 </table>
2856 <table width="95%" align="center">
2857 <tr><td>
2858
2859 <tr>
2860 <td align="left">struct nettle_cipher <b>nettle_cast128</b><i>
2861      </i></td>
2862 <td align="right">Constant Struct</td>
2863 </tr>
2864 </table>
2865 <table width="95%" align="center">
2866 <tr><td>
2867
2868 <tr>
2869 <td align="left">struct nettle_cipher <b>nettle_serpent128</b><i>
2870      </i></td>
2871 <td align="right">Constant Struct</td>
2872 </tr>
2873
2874 <tr>
2875 <td align="left">struct nettle_cipher <b>nettle_serpent192</b><i>
2876      </i></td>
2877 <td align="right">Constant Struct</td>
2878 </tr>
2879
2880 <tr>
2881 <td align="left">struct nettle_cipher <b>nettle_serpent256</b><i>
2882      </i></td>
2883 <td align="right">Constant Struct</td>
2884 </tr>
2885 </table>
2886 <table width="95%" align="center">
2887 <tr><td>
2888
2889 <tr>
2890 <td align="left">struct nettle_cipher <b>nettle_twofish128</b><i>
2891      </i></td>
2892 <td align="right">Constant Struct</td>
2893 </tr>
2894
2895 <tr>
2896 <td align="left">struct nettle_cipher <b>nettle_twofish192</b><i>
2897      </i></td>
2898 <td align="right">Constant Struct</td>
2899 </tr>
2900
2901 <tr>
2902 <td align="left">struct nettle_cipher <b>nettle_twofish256</b><i>
2903      </i></td>
2904 <td align="right">Constant Struct</td>
2905 </tr>
2906 </table>
2907 <table width="95%" align="center">
2908 <tr><td>
2909
2910 <tr>
2911 <td align="left">struct nettle_cipher <b>nettle_arctwo40;</b><i>
2912      </i></td>
2913 <td align="right">Constant Struct</td>
2914 </tr>
2915
2916 <tr>
2917 <td align="left">struct nettle_cipher <b>nettle_arctwo64;</b><i>
2918      </i></td>
2919 <td align="right">Constant Struct</td>
2920 </tr>
2921
2922 <tr>
2923 <td align="left">struct nettle_cipher <b>nettle_arctwo128;</b><i>
2924      </i></td>
2925 <td align="right">Constant Struct</td>
2926 </tr>
2927
2928 <tr>
2929 <td align="left">struct nettle_cipher <b>nettle_arctwo_gutmann128;</b><i>
2930      </i></td>
2931 <td align="right">Constant Struct</td>
2932 </tr>
2933 </table>
2934 <table width="95%" align="center">
2935 <tr><td>
2936
2937      <p>Nettle includes such structs for all the <em>regular</em> ciphers, i.e. 
2938 ones without weak keys or other oddities. 
2939 </td></tr>
2940 </table>
2941
2942 <div class="node">
2943 <p><hr>
2944 Node:&nbsp;<a name="Cipher%20modes">Cipher modes</a>,
2945 Next:&nbsp;<a rel="next" accesskey="n" href="#Keyed%20hash%20functions">Keyed hash functions</a>,
2946 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Cipher%20functions">Cipher functions</a>,
2947 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
2948 <br>
2949 </div>
2950
2951 <h3 class="section">Cipher modes</h3>
2952
2953 <p>Cipher modes of operation specifies the procedure to use when
2954 encrypting a message that is larger than the cipher's block size. As
2955 explained in See <a href="#Cipher%20functions">Cipher functions</a>, splitting the message into blocks
2956 and processing them independently with the block cipher (Electronic Code
2957 Book mode, <small>ECB</small>) leaks information. Besides <small>ECB</small>,
2958 Nettle provides two other modes of operation: Cipher Block Chaining
2959 (<small>CBC</small>) and Counter mode (<small>CTR</small>). <small>CBC</small> is
2960 widely used, but there are a few subtle issues of information leakage. 
2961 <small>CTR</small> was standardized more recently, and is believed to be more
2962 secure.
2963
2964 <h3 class="subsection">Cipher Block Chaining</h4>
2965
2966 <p>When using <small>CBC</small> mode, plaintext blocks are not encrypted
2967 independently of each other, like in Electronic Cook Book mode. Instead,
2968 when encrypting a block in <small>CBC</small> mode, the previous ciphertext
2969 block is XORed with the plaintext before it is fed to the block cipher. 
2970 When encrypting the first block, a random block called an <dfn>IV</dfn>, or
2971 Initialization Vector, is used as the "previous ciphertext block". The
2972 IV should be chosen randomly, but it need not be kept secret, and can
2973 even be transmitted in the clear together with the encrypted data.
2974
2975    <p>In symbols, if <code>E_k</code> is the encryption function of a block cipher,
2976 and <code>IV</code> is the initialization vector, then <code>n</code> plaintext blocks
2977 <code>M_1</code>,<small class="dots">...</small> <code>M_n</code> are transformed into <code>n</code> ciphertext blocks
2978 <code>C_1</code>,<small class="dots">...</small> <code>C_n</code> as follows:
2979
2980 <pre class="example">     C_1 = E_k(IV  XOR M_1)
2981      C_2 = E_k(C_1 XOR M_2)
2982      
2983      ...
2984      
2985      C_n = E_k(C_(n-1) XOR M_n)
2986      </pre>
2987
2988    <p>Nettle's includes two functions for applying a block cipher in Cipher
2989 Block Chaining (<small>CBC</small>) mode, one for encryption and one for
2990 decryption. These functions uses <code>void *</code> to pass cipher contexts
2991 around.
2992
2993 <p>
2994 <table width="100%">
2995 <tr>
2996 <td align="left">void <b>cbc_encrypt</b><i> </i>(<i>void *</i><var>ctx</var><i>, nettle_crypt_func </i><var>f</var><i>, unsigned </i><var>block_size</var><i>, uint8_t *</i><var>iv</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>dst</var><i>, const uint8_t *</i><var>src</var><i></i>)<i>
2997      </i></td>
2998 <td align="right">Function</td>
2999 </tr>
3000
3001 <tr>
3002 <td align="left">void <b>cbc_decrypt</b><i> </i>(<i>void *</i><var>ctx</var><i>, void </i>(<i>*</i><var>f</var><i></i>)<i></i>(<i></i>)<i>, unsigned </i><var>block_size</var><i>, uint8_t *</i><var>iv</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>dst</var><i>, const uint8_t *</i><var>src</var><i></i>)<i>
3003      </i></td>
3004 <td align="right">Function</td>
3005 </tr>
3006 </table>
3007 <table width="95%" align="center">
3008 <tr><td>
3009
3010      <p>Applies the encryption or decryption function <var>f</var> in <small>CBC</small>
3011 mode. The final ciphertext block processed is copied into <var>iv</var>
3012 before returning, so that large message be processed be a sequence of
3013 calls to <code>cbc_encrypt</code>. The function <var>f</var> is of type
3014
3015      <p><code>void f (void *</code><var>ctx</var><code>, unsigned </code><var>length</var><code>, uint8_t </code><var>dst</var><code>,
3016 const uint8_t *</code><var>src</var><code>)</code>,
3017
3018      <p>and the <code>cbc_encrypt</code> and <code>cbc_decrypt</code> functions pass their
3019 argument <var>ctx</var> on to <var>f</var>. 
3020 </td></tr>
3021 </table>
3022
3023    <p>There are also some macros to help use these functions correctly.
3024
3025 <p>
3026 <table width="100%">
3027 <tr>
3028 <td align="left"><b>CBC_CTX</b><i> </i>(<i></i><var>context_type</var><i>, </i><var>block_size</var><i></i>)<i>
3029      </i></td>
3030 <td align="right">Macro</td>
3031 </tr>
3032 </table>
3033 <table width="95%" align="center">
3034 <tr><td>
3035 Expands into
3036      <pre class="example">          {
3037              context_type ctx;
3038              uint8_t iv[block_size];
3039           }
3040           </pre>
3041      </td></tr>
3042 </table>
3043
3044    <p>It can be used to define a <small>CBC</small> context struct, either directly,
3045
3046 <pre class="example">     struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) ctx;
3047      </pre>
3048
3049    <p>or to give it a struct tag,
3050
3051 <pre class="example">     struct aes_cbc_ctx CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE);
3052      </pre>
3053
3054 <p>
3055 <table width="100%">
3056 <tr>
3057 <td align="left"><b>CBC_SET_IV</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>iv</var><i></i>)<i>
3058      </i></td>
3059 <td align="right">Macro</td>
3060 </tr>
3061 </table>
3062 <table width="95%" align="center">
3063 <tr><td>
3064 First argument is a pointer to a context struct as defined by <code>CBC_CTX</code>,
3065 and the second is a pointer to an Initialization Vector (IV) that is
3066 copied into that context. 
3067 </td></tr>
3068 </table>
3069
3070 <p>
3071 <table width="100%">
3072 <tr>
3073 <td align="left"><b>CBC_ENCRYPT</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>f</var><i>, </i><var>length</var><i>, </i><var>dst</var><i>, </i><var>src</var><i></i>)<i>
3074      </i></td>
3075 <td align="right">Macro</td>
3076 </tr>
3077
3078 <tr>
3079 <td align="left"><b>CBC_DECRYPT</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>f</var><i>, </i><var>length</var><i>, </i><var>dst</var><i>, </i><var>src</var><i></i>)<i>
3080      </i></td>
3081 <td align="right">Macro</td>
3082 </tr>
3083 </table>
3084 <table width="95%" align="center">
3085 <tr><td>
3086 A simpler way to invoke <code>cbc_encrypt</code> and <code>cbc_decrypt</code>. The
3087 first argument is a pointer to a context struct as defined by
3088 <code>CBC_CTX</code>, and the second argument is an encryption or decryption
3089 function following Nettle's conventions. The last three arguments define
3090 the source and destination area for the operation. 
3091 </td></tr>
3092 </table>
3093
3094    <p>These macros use some tricks to make the compiler display a warning if
3095 the types of <var>f</var> and <var>ctx</var> don't match, e.g. if you try to use
3096 an <code>struct aes_ctx</code> context with the <code>des_encrypt</code> function.
3097
3098 <h3 class="subsection">Counter mode</h4>
3099
3100 <p>Counter mode (<small>CTR</small>) uses the block cipher as a keyed
3101 pseudo-random generator. The output of the generator is XORed with the
3102 data to be encrypted. It can be understood as a way to transform a block
3103 cipher to a stream cipher.
3104
3105    <p>The message is divided into <code>n</code> blocks <code>M_1</code>,<small class="dots">...</small>
3106 <code>M_n</code>, where <code>M_n</code> is of size <code>m</code> which may be smaller
3107 than the block size. Except for the last block, all the message blocks
3108 must be of size equal to the cipher's block size.
3109
3110    <p>If <code>E_k</code> is the encryption function of a block cipher, <code>IC</code> is
3111 the initial counter, then the <code>n</code> plaintext blocks are
3112 transformed into <code>n</code> ciphertext blocks <code>C_1</code>,<small class="dots">...</small>
3113 <code>C_n</code> as follows:
3114
3115 <pre class="example">     C_1 = E_k(IC) XOR M_1
3116      C_2 = E_k(IC + 1) XOR M_2
3117      
3118      ...
3119      
3120      C_(n-1) = E_k(IC + n - 2) XOR M_(n-1)
3121      C_n = E_k(IC + n - 1) [1..m] XOR M_n
3122      </pre>
3123
3124    <p>The <small>IC</small> is the initial value for the counter, it plays a
3125 similar role as the <small>IV</small> for <small>CBC</small>. When adding,
3126 <code>IC + x</code>, <small>IC</small> is interpreted as an integer, in network
3127 byte order. For the last block, <code>E_k(IC + n - 1) [1..m]</code> means that
3128 the cipher output is truncated to <code>m</code> bytes.
3129
3130 <p>
3131 <table width="100%">
3132 <tr>
3133 <td align="left">void <b>ctr_crypt</b><i> </i>(<i>void *</i><var>ctx</var><i>, nettle_crypt_func </i><var>f</var><i>, unsigned </i><var>block_size</var><i>, uint8_t *</i><var>ctr</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>dst</var><i>, const uint8_t *</i><var>src</var><i></i>)<i>
3134      </i></td>
3135 <td align="right">Function</td>
3136 </tr>
3137 </table>
3138 <table width="95%" align="center">
3139 <tr><td>
3140
3141      <p>Applies the encryption function <var>f</var> in <small>CTR</small> mode. Note that
3142 for <small>CTR</small> mode, encryption and decryption is the same operation,
3143 and hence <var>f</var> should always be the encryption function for the
3144 underlying block cipher.
3145
3146      <p>When a message is encrypted using a sequence of calls to
3147 <code>ctr_crypt</code>, all but the last call <em>must</em> use a length that is
3148 a multiple of the block size. 
3149 </td></tr>
3150 </table>
3151
3152    <p>Like for <small>CBC</small>, there are also a couple of helper macros.
3153
3154 <p>
3155 <table width="100%">
3156 <tr>
3157 <td align="left"><b>CTR_CTX</b><i> </i>(<i></i><var>context_type</var><i>, </i><var>block_size</var><i></i>)<i>
3158      </i></td>
3159 <td align="right">Macro</td>
3160 </tr>
3161 </table>
3162 <table width="95%" align="center">
3163 <tr><td>
3164 Expands into
3165      <pre class="example">          {
3166              context_type ctx;
3167              uint8_t ctr[block_size];
3168           }
3169           </pre>
3170      </td></tr>
3171 </table>
3172
3173 <p>
3174 <table width="100%">
3175 <tr>
3176 <td align="left"><b>CTR_SET_COUNTER</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>iv</var><i></i>)<i>
3177      </i></td>
3178 <td align="right">Macro</td>
3179 </tr>
3180 </table>
3181 <table width="95%" align="center">
3182 <tr><td>
3183 First argument is a pointer to a context struct as defined by
3184 <code>CTR_CTX</code>, and the second is a pointer to an initial counter that
3185 is copied into that context. 
3186 </td></tr>
3187 </table>
3188
3189 <p>
3190 <table width="100%">
3191 <tr>
3192 <td align="left"><b>CTR_CRYPT</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>f</var><i>, </i><var>length</var><i>, </i><var>dst</var><i>, </i><var>src</var><i></i>)<i>
3193      </i></td>
3194 <td align="right">Macro</td>
3195 </tr>
3196 </table>
3197 <table width="95%" align="center">
3198 <tr><td>
3199 A simpler way to invoke <code>ctr_crypt</code>. The first argument is a
3200 pointer to a context struct as defined by <code>CTR_CTX</code>, and the second
3201 argument is an encryption function following Nettle's conventions. The
3202 last three arguments define the source and destination area for the
3203 operation. 
3204 </td></tr>
3205 </table>
3206
3207 <div class="node">
3208 <p><hr>
3209 Node:&nbsp;<a name="Keyed%20hash%20functions">Keyed hash functions</a>,
3210 Next:&nbsp;<a rel="next" accesskey="n" href="#Public-key%20algorithms">Public-key algorithms</a>,
3211 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Cipher%20modes">Cipher modes</a>,
3212 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
3213 <br>
3214 </div>
3215
3216 <h3 class="section">Keyed Hash Functions</h3>
3217
3218 <p>A <dfn>keyed hash function</dfn>, or <dfn>Message Authentication Code</dfn>
3219 (<small>MAC</small>) is a function that takes a key and a message, and
3220 produces fixed size <small>MAC</small>. It should be hard to compute a
3221 message and a matching <small>MAC</small> without knowledge of the key. It
3222 should also be hard to compute the key given only messages and
3223 corresponding <small>MAC</small>s.
3224
3225    <p>Keyed hash functions are useful primarily for message authentication,
3226 when Alice and Bob shares a secret: The sender, Alice, computes the
3227 <small>MAC</small> and attaches it to the message. The receiver, Bob, also computes
3228 the <small>MAC</small> of the message, using the same key, and compares that
3229 to Alice's value. If they match, Bob can be assured that
3230 the message has not been modified on its way from Alice.
3231
3232    <p>However, unlike digital signatures, this assurance is not transferable. 
3233 Bob can't show the message and the <small>MAC</small> to a third party and
3234 prove that Alice sent that message. Not even if he gives away the key to
3235 the third party. The reason is that the <em>same</em> key is used on both
3236 sides, and anyone knowing the key can create a correct <small>MAC</small> for
3237 any message. If Bob believes that only he and Alice knows the key, and
3238 he knows that he didn't attach a <small>MAC</small> to a particular message,
3239 he knows it must be Alice who did it. However, the third party can't
3240 distinguish between a <small>MAC</small> created by Alice and one created by
3241 Bob.
3242
3243    <p>Keyed hash functions are typically a lot faster than digital signatures
3244 as well.
3245
3246 <h3 class="subsection"><small>HMAC</small></h4>
3247
3248 <p>One can build keyed hash functions from ordinary hash functions. Older
3249 constructions simply concatenate secret key and message and hashes that, but
3250 such constructions have weaknesses. A better construction is
3251 <small>HMAC</small>, described in <cite>RFC 2104</cite>.
3252
3253    <p>For an underlying hash function <code>H</code>, with digest size <code>l</code> and
3254 internal block size <code>b</code>, <small>HMAC-H</small> is constructed as
3255 follows: From a given key <code>k</code>, two distinct subkeys <code>k_i</code> and
3256 <code>k_o</code> are constructed, both of length <code>b</code>. The
3257 <small>HMAC-H</small> of a message <code>m</code> is then computed as <code>H(k_o |
3258 H(k_i | m))</code>, where <code>|</code> denotes string concatenation.
3259
3260    <p><small>HMAC</small> keys can be of any length, but it is recommended to use
3261 keys of length <code>l</code>, the digest size of the underlying hash function
3262 <code>H</code>. Keys that are longer than <code>b</code> are shortened to length
3263 <code>l</code> by hashing with <code>H</code>, so arbitrarily long keys aren't
3264 very useful.
3265
3266    <p>Nettle's <small>HMAC</small> functions are defined in <code>&lt;nettle/hmac.h&gt;</code>. 
3267 There are abstract functions that use a pointer to a <code>struct
3268 nettle_hash</code> to represent the underlying hash function and <code>void
3269 *</code> pointers that point to three different context structs for that hash
3270 function. There are also concrete functions for <small>HMAC-MD5</small>,
3271 <small>HMAC-SHA1</small>, <small>HMAC-SHA256</small>, and <small>HMAC-SHA512</small>. 
3272 First, the abstract functions:
3273
3274 <p>
3275 <table width="100%">
3276 <tr>
3277 <td align="left">void <b>hmac_set_key</b><i> </i>(<i>void *</i><var>outer</var><i>, void *</i><var>inner</var><i>, void *</i><var>state</var><i>, const struct nettle_hash *</i><var>H</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
3278      </i></td>
3279 <td align="right">Function</td>
3280 </tr>
3281 </table>
3282 <table width="95%" align="center">
3283 <tr><td>
3284 Initializes the three context structs from the key. The <var>outer</var> and
3285 <var>inner</var> contexts corresponds to the subkeys <code>k_o</code> and
3286 <code>k_i</code>. <var>state</var> is used for hashing the message, and is
3287 initialized as a copy of the <var>inner</var> context. 
3288 </td></tr>
3289 </table>
3290
3291 <p>
3292 <table width="100%">
3293 <tr>
3294 <td align="left">void <b>hmac_update</b><i> </i>(<i>void *</i><var>state</var><i>, const struct nettle_hash *</i><var>H</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
3295      </i></td>
3296 <td align="right">Function</td>
3297 </tr>
3298 </table>
3299 <table width="95%" align="center">
3300 <tr><td>
3301 This function is called zero or more times to process the message. 
3302 Actually, <code>hmac_update(state, H, length, data)</code> is equivalent to
3303 <code>H-&gt;update(state, length, data)</code>, so if you wish you can use the
3304 ordinary update function of the underlying hash function instead. 
3305 </td></tr>
3306 </table>
3307
3308 <p>
3309 <table width="100%">
3310 <tr>
3311 <td align="left">void <b>hmac_digest</b><i> </i>(<i>const void *</i><var>outer</var><i>, const void *</i><var>inner</var><i>, void *</i><var>state</var><i>, const struct nettle_hash *</i><var>H</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
3312      </i></td>
3313 <td align="right">Function</td>
3314 </tr>
3315 </table>
3316 <table width="95%" align="center">
3317 <tr><td>
3318 Extracts the <small>MAC</small> of the message, writing it to <var>digest</var>. 
3319 <var>outer</var> and <var>inner</var> are not modified. <var>length</var> is usually
3320 equal to <code>H-&gt;digest_size</code>, but if you provide a smaller value,
3321 only the first <var>length</var> octets of the <small>MAC</small> are written.
3322
3323      <p>This function also resets the <var>state</var> context so that you can start
3324 over processing a new message (with the same key). 
3325 </td></tr>
3326 </table>
3327
3328    <p>Like for <small>CBC</small>, there are some macros to help use these
3329 functions correctly.
3330
3331 <p>
3332 <table width="100%">
3333 <tr>
3334 <td align="left"><b>HMAC_CTX</b><i> </i>(<i></i><var>type</var><i></i>)<i>
3335      </i></td>
3336 <td align="right">Macro</td>
3337 </tr>
3338 </table>
3339 <table width="95%" align="center">
3340 <tr><td>
3341 Expands into
3342      <pre class="example">          {
3343              type outer;
3344              type inner;
3345              type state;
3346           }
3347           </pre>
3348      </td></tr>
3349 </table>
3350
3351    <p>It can be used to define a <small>HMAC</small> context struct, either
3352 directly,
3353
3354 <pre class="example">     struct HMAC_CTX(struct md5_ctx) ctx;
3355      </pre>
3356
3357    <p>or to give it a struct tag,
3358
3359 <pre class="example">     struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
3360      </pre>
3361
3362 <p>
3363 <table width="100%">
3364 <tr>
3365 <td align="left"><b>HMAC_SET_KEY</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>H</var><i>, </i><var>length</var><i>, </i><var>key</var><i></i>)<i>
3366      </i></td>
3367 <td align="right">Macro</td>
3368 </tr>
3369 </table>
3370 <table width="95%" align="center">
3371 <tr><td>
3372 <var>ctx</var> is a pointer to a context struct as defined by
3373 <code>HMAC_CTX</code>, <var>H</var> is a pointer to a <code>const struct
3374 nettle_hash</code> describing the underlying hash function (so it must match
3375 the type of the components of <var>ctx</var>). The last two arguments specify
3376 the secret key. 
3377 </td></tr>
3378 </table>
3379
3380 <p>
3381 <table width="100%">
3382 <tr>
3383 <td align="left"><b>HMAC_DIGEST</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>H</var><i>, </i><var>length</var><i>, </i><var>digest</var><i></i>)<i>
3384      </i></td>
3385 <td align="right">Macro</td>
3386 </tr>
3387 </table>
3388 <table width="95%" align="center">
3389 <tr><td>
3390 <var>ctx</var> is a pointer to a context struct as defined by
3391 <code>HMAC_CTX</code>, <var>H</var> is a pointer to a <code>const struct
3392 nettle_hash</code> describing the underlying hash function. The last two
3393 arguments specify where the digest is written. 
3394 </td></tr>
3395 </table>
3396
3397    <p>Note that there is no <code>HMAC_UPDATE</code> macro; simply call
3398 <code>hmac_update</code> function directly, or the update function of the
3399 underlying hash function.
3400
3401 <h3 class="subsection">Concrete <small>HMAC</small> functions</h4>
3402
3403 <p>Now we come to the specialized <small>HMAC</small> functions, which are
3404 easier to use than the general <small>HMAC</small> functions.
3405
3406 <h3 class="subsubsection"><small>HMAC-MD5</small></h5>
3407
3408 <p>
3409 <table width="100%">
3410 <tr>
3411 <td align="left"><b>struct hmac_md5_ctx</b><i>
3412      </i></td>
3413 <td align="right">Context struct</td>
3414 </tr>
3415 </table>
3416 <table width="95%" align="center">
3417 <tr><td>
3418 </td></tr>
3419 </table>
3420
3421 <p>
3422 <table width="100%">
3423 <tr>
3424 <td align="left">void <b>hmac_md5_set_key</b><i> </i>(<i>struct hmac_md5_ctx *</i><var>ctx</var><i>, unsigned </i><var>key_length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
3425      </i></td>
3426 <td align="right">Function</td>
3427 </tr>
3428 </table>
3429 <table width="95%" align="center">
3430 <tr><td>
3431 Initializes the context with the key. 
3432 </td></tr>
3433 </table>
3434
3435 <p>
3436 <table width="100%">
3437 <tr>
3438 <td align="left">void <b>hmac_md5_update</b><i> </i>(<i>struct hmac_md5_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
3439      </i></td>
3440 <td align="right">Function</td>
3441 </tr>
3442 </table>
3443 <table width="95%" align="center">
3444 <tr><td>
3445 Process some more data. 
3446 </td></tr>
3447 </table>
3448
3449 <p>
3450 <table width="100%">
3451 <tr>
3452 <td align="left">void <b>hmac_md5_digest</b><i> </i>(<i>struct hmac_md5_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
3453      </i></td>
3454 <td align="right">Function</td>
3455 </tr>
3456 </table>
3457 <table width="95%" align="center">
3458 <tr><td>
3459 Extracts the <small>MAC</small>, writing it to <var>digest</var>. <var>length</var> may be smaller than
3460 <code>MD5_DIGEST_SIZE</code>, in which case only the first <var>length</var>
3461 octets of the <small>MAC</small> are written.
3462
3463      <p>This function also resets the context for processing new messages, with
3464 the same key. 
3465 </td></tr>
3466 </table>
3467
3468 <h3 class="subsubsection"><small>HMAC-SHA1</small></h5>
3469
3470 <p>
3471 <table width="100%">
3472 <tr>
3473 <td align="left"><b>struct hmac_sha1_ctx</b><i>
3474      </i></td>
3475 <td align="right">Context struct</td>
3476 </tr>
3477 </table>
3478 <table width="95%" align="center">
3479 <tr><td>
3480 </td></tr>
3481 </table>
3482
3483 <p>
3484 <table width="100%">
3485 <tr>
3486 <td align="left">void <b>hmac_sha1_set_key</b><i> </i>(<i>struct hmac_sha1_ctx *</i><var>ctx</var><i>, unsigned </i><var>key_length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
3487      </i></td>
3488 <td align="right">Function</td>
3489 </tr>
3490 </table>
3491 <table width="95%" align="center">
3492 <tr><td>
3493 Initializes the context with the key. 
3494 </td></tr>
3495 </table>
3496
3497 <p>
3498 <table width="100%">
3499 <tr>
3500 <td align="left">void <b>hmac_sha1_update</b><i> </i>(<i>struct hmac_sha1_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
3501      </i></td>
3502 <td align="right">Function</td>
3503 </tr>
3504 </table>
3505 <table width="95%" align="center">
3506 <tr><td>
3507 Process some more data. 
3508 </td></tr>
3509 </table>
3510
3511 <p>
3512 <table width="100%">
3513 <tr>
3514 <td align="left">void <b>hmac_sha1_digest</b><i> </i>(<i>struct hmac_sha1_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
3515      </i></td>
3516 <td align="right">Function</td>
3517 </tr>
3518 </table>
3519 <table width="95%" align="center">
3520 <tr><td>
3521 Extracts the <small>MAC</small>, writing it to <var>digest</var>. <var>length</var> may be smaller than
3522 <code>SHA1_DIGEST_SIZE</code>, in which case only the first <var>length</var>
3523 octets of the <small>MAC</small> are written.
3524
3525      <p>This function also resets the context for processing new messages, with
3526 the same key. 
3527 </td></tr>
3528 </table>
3529
3530 <h3 class="subsubsection"><small>HMAC-SHA256</small></h5>
3531
3532 <p>
3533 <table width="100%">
3534 <tr>
3535 <td align="left"><b>struct hmac_sha256_ctx</b><i>
3536      </i></td>
3537 <td align="right">Context struct</td>
3538 </tr>
3539 </table>
3540 <table width="95%" align="center">
3541 <tr><td>
3542 </td></tr>
3543 </table>
3544
3545 <p>
3546 <table width="100%">
3547 <tr>
3548 <td align="left">void <b>hmac_sha256_set_key</b><i> </i>(<i>struct hmac_sha256_ctx *</i><var>ctx</var><i>, unsigned </i><var>key_length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
3549      </i></td>
3550 <td align="right">Function</td>
3551 </tr>
3552 </table>
3553 <table width="95%" align="center">
3554 <tr><td>
3555 Initializes the context with the key. 
3556 </td></tr>
3557 </table>
3558
3559 <p>
3560 <table width="100%">
3561 <tr>
3562 <td align="left">void <b>hmac_sha256_update</b><i> </i>(<i>struct hmac_sha256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
3563      </i></td>
3564 <td align="right">Function</td>
3565 </tr>
3566 </table>
3567 <table width="95%" align="center">
3568 <tr><td>
3569 Process some more data. 
3570 </td></tr>
3571 </table>
3572
3573 <p>
3574 <table width="100%">
3575 <tr>
3576 <td align="left">void <b>hmac_sha256_digest</b><i> </i>(<i>struct hmac_sha256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
3577      </i></td>
3578 <td align="right">Function</td>
3579 </tr>
3580 </table>
3581 <table width="95%" align="center">
3582 <tr><td>
3583 Extracts the <small>MAC</small>, writing it to <var>digest</var>. <var>length</var> may be smaller than
3584 <code>SHA256_DIGEST_SIZE</code>, in which case only the first <var>length</var>
3585 octets of the <small>MAC</small> are written.
3586
3587      <p>This function also resets the context for processing new messages, with
3588 the same key. 
3589 </td></tr>
3590 </table>
3591
3592 <h3 class="subsubsection"><small>HMAC-SHA512</small></h5>
3593
3594 <p>
3595 <table width="100%">
3596 <tr>
3597 <td align="left"><b>struct hmac_sha512_ctx</b><i>
3598      </i></td>
3599 <td align="right">Context struct</td>
3600 </tr>
3601 </table>
3602 <table width="95%" align="center">
3603 <tr><td>
3604 </td></tr>
3605 </table>
3606
3607 <p>
3608 <table width="100%">
3609 <tr>
3610 <td align="left">void <b>hmac_sha512_set_key</b><i> </i>(<i>struct hmac_sha512_ctx *</i><var>ctx</var><i>, unsigned </i><var>key_length</var><i>, const uint8_t *</i><var>key</var><i></i>)<i>
3611      </i></td>
3612 <td align="right">Function</td>
3613 </tr>
3614 </table>
3615 <table width="95%" align="center">
3616 <tr><td>
3617 Initializes the context with the key. 
3618 </td></tr>
3619 </table>
3620
3621 <p>
3622 <table width="100%">
3623 <tr>
3624 <td align="left">void <b>hmac_sha512_update</b><i> </i>(<i>struct hmac_sha512_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
3625      </i></td>
3626 <td align="right">Function</td>
3627 </tr>
3628 </table>
3629 <table width="95%" align="center">
3630 <tr><td>
3631 Process some more data. 
3632 </td></tr>
3633 </table>
3634
3635 <p>
3636 <table width="100%">
3637 <tr>
3638 <td align="left">void <b>hmac_sha512_digest</b><i> </i>(<i>struct hmac_sha512_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>digest</var><i></i>)<i>
3639      </i></td>
3640 <td align="right">Function</td>
3641 </tr>
3642 </table>
3643 <table width="95%" align="center">
3644 <tr><td>
3645 Extracts the <small>MAC</small>, writing it to <var>digest</var>. <var>length</var> may be smaller than
3646 <code>SHA512_DIGEST_SIZE</code>, in which case only the first <var>length</var>
3647 octets of the <small>MAC</small> are written.
3648
3649      <p>This function also resets the context for processing new messages, with
3650 the same key. 
3651 </td></tr>
3652 </table>
3653
3654 <div class="node">
3655 <p><hr>
3656 Node:&nbsp;<a name="Public-key%20algorithms">Public-key algorithms</a>,
3657 Next:&nbsp;<a rel="next" accesskey="n" href="#Randomness">Randomness</a>,
3658 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Keyed%20hash%20functions">Keyed hash functions</a>,
3659 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
3660 <br>
3661 </div>
3662
3663 <h3 class="section">Public-key algorithms</h3>
3664
3665 <p>Nettle uses <small>GMP</small>, the GNU bignum library, for all calculations
3666 with large numbers. In order to use the public-key features of Nettle,
3667 you must install <small>GMP</small>, at least version 3.0, before compiling
3668 Nettle, and you need to link your programs with <code>-lhogweed -lnettle
3669 -lgmp</code>.
3670
3671    <p>The concept of <dfn>Public-key</dfn> encryption and digital signatures was
3672 discovered by Whitfield Diffie and Martin E. Hellman and described in a
3673 paper 1976. In traditional, "symmetric", cryptography, sender and
3674 receiver share the same keys, and these keys must be distributed in a
3675 secure way. And if there are many users or entities that need to
3676 communicate, each <em>pair</em> needs a shared secret key known by nobody
3677 else.
3678
3679    <p>Public-key cryptography uses trapdoor one-way functions. A
3680 <dfn>one-way function</dfn> is a function <code>F</code> such that it is easy to
3681 compute the value <code>F(x)</code> for any <code>x</code>, but given a value
3682 <code>y</code>, it is hard to compute a corresponding <code>x</code> such that
3683 <code>y = F(x)</code>. Two examples are cryptographic hash functions, and
3684 exponentiation in certain groups.
3685
3686    <p>A <dfn>trapdoor one-way function</dfn> is a function <code>F</code> that is
3687 one-way, unless one knows some secret information about <code>F</code>. If one
3688 knows the secret, it is easy to compute both <code>F</code> and it's inverse. 
3689 If this sounds strange, look at the <small>RSA</small> example below.
3690
3691    <p>Two important uses for one-way functions with trapdoors are public-key
3692 encryption, and digital signatures. The public-key encryption functions
3693 in Nettle are not yet documented; the rest of this chapter is about
3694 digital signatures.
3695
3696    <p>To use a digital signature algorithm, one must first create a
3697 <dfn>key-pair</dfn>: A public key and a corresponding private key. The private
3698 key is used to sign messages, while the public key is used for verifying
3699 that that signatures and messages match. Some care must be taken when
3700 distributing the public key; it need not be kept secret, but if a bad
3701 guy is able to replace it (in transit, or in some user's list of known
3702 public keys), bad things may happen.
3703
3704    <p>There are two operations one can do with the keys. The signature
3705 operation takes a message and a private key, and creates a signature for
3706 the message. A signature is some string of bits, usually at most a few
3707 thousand bits or a few hundred octets. Unlike paper-and-ink signatures,
3708 the digital signature depends on the message, so one can't cut it out of
3709 context and glue it to a different message.
3710
3711    <p>The verification operation takes a public key, a message, and a string
3712 that is claimed to be a signature on the message, and returns true or
3713 false. If it returns true, that means that the three input values
3714 matched, and the verifier can be sure that someone went through with the
3715 signature operation on that very message, and that the "someone" also
3716 knows the private key corresponding to the public key.
3717
3718    <p>The desired properties of a digital signature algorithm are as follows:
3719 Given the public key and pairs of messages and valid signatures on them,
3720 it should be hard to compute the private key, and it should also be hard
3721 to create a new message and signature that is accepted by the
3722 verification operation.
3723
3724    <p>Besides signing meaningful messages, digital signatures can be used for
3725 authorization. A server can be configured with a public key, such that
3726 any client that connects to the service is given a random nonce message. 
3727 If the server gets a reply with a correct signature matching the nonce
3728 message and the configured public key, the client is granted access. So
3729 the configuration of the server can be understood as "grant access to
3730 whoever knows the private key corresponding to this particular public
3731 key, and to no others".
3732
3733 <ul class="menu">
3734 <li><a accesskey="1" href="#RSA">RSA</a>:                          The RSA public key algorithm. 
3735 <li><a accesskey="2" href="#DSA">DSA</a>:                          The DSA digital signature algorithm. 
3736 </ul>
3737
3738 <div class="node">
3739 <p><hr>
3740 Node:&nbsp;<a name="RSA">RSA</a>,
3741 Next:&nbsp;<a rel="next" accesskey="n" href="#DSA">DSA</a>,
3742 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Public-key%20algorithms">Public-key algorithms</a>,
3743 Up:&nbsp;<a rel="up" accesskey="u" href="#Public-key%20algorithms">Public-key algorithms</a>
3744 <br>
3745 </div>
3746
3747 <h3 class="subsection"><small>RSA</small></h4>
3748
3749 <p>The <small>RSA</small> algorithm was the first practical digital signature
3750 algorithm that was constructed. It was described 1978 in a paper by
3751 Ronald Rivest, Adi Shamir and L.M. Adleman, and the technique was also
3752 patented in the <small>USA</small> in 1983. The patent expired on September 20, 2000, and since
3753 that day, <small>RSA</small> can be used freely, even in the <small>USA</small>.
3754
3755    <p>It's remarkably simple to describe the trapdoor function behind
3756 <small>RSA</small>. The "one-way"-function used is
3757
3758 <pre class="example">     F(x) = x^e mod n
3759      </pre>
3760
3761    <p>I.e. raise x to the <code>e</code>:th power, while discarding all multiples of
3762 <code>n</code>. The pair of numbers <code>n</code> and <code>e</code> is the public key. 
3763 <code>e</code> can be quite small, even <code>e = 3</code> has been used, although
3764 slightly larger numbers are recommended. <code>n</code> should be about 1000
3765 bits or larger.
3766
3767    <p>If <code>n</code> is large enough, and properly chosen, the inverse of F,
3768 the computation of <code>e</code>:th roots modulo <code>n</code>, is very difficult. 
3769 But, where's the trapdoor?
3770
3771    <p>Let's first look at how <small>RSA</small> key-pairs are generated. First
3772 <code>n</code> is chosen as the product of two large prime numbers <code>p</code>
3773 and <code>q</code> of roughly the same size (so if <code>n</code> is 1000 bits,
3774 <code>p</code> and <code>q</code> are about 500 bits each). One also computes the
3775 number <code>phi = (p-1)(q-1)</code>, in mathematical speak, <code>phi</code> is the
3776 order of the multiplicative group of integers modulo n.
3777
3778    <p>Next, <code>e</code> is chosen. It must have no factors in common with <code>phi</code> (in
3779 particular, it must be odd), but can otherwise be chosen more or less
3780 randomly. <code>e = 65537</code> is a popular choice, because it makes raising
3781 to the <code>e</code>'th power particularly efficient, and being prime, it
3782 usually has no factors common with <code>phi</code>.
3783
3784    <p>Finally, a number <code>d</code>, <code>d &lt; n</code> is computed such that <code>e d
3785 mod phi = 1</code>. It can be shown that such a number exists (this is why
3786 <code>e</code> and <code>phi</code> must have no common factors), and that for all x,
3787
3788 <pre class="example">     (x^e)^d mod n = x^(ed) mod n = (x^d)^e mod n = x
3789      </pre>
3790
3791    <p>Using Euclid's algorithm, <code>d</code> can be computed quite easily from
3792 <code>phi</code> and <code>e</code>. But it is still hard to get <code>d</code> without
3793 knowing <code>phi</code>, which depends on the factorization of <code>n</code>.
3794
3795    <p>So <code>d</code> is the trapdoor, if we know <code>d</code> and <code>y = F(x)</code>, we can
3796 recover x as <code>y^d mod n</code>. <code>d</code> is also the private half of
3797 the <small>RSA</small> key-pair.
3798
3799    <p>The most common signature operation for <small>RSA</small> is defined in
3800 <cite>PKCS#1</cite>, a specification by RSA Laboratories. The message to be
3801 signed is first hashed using a cryptographic hash function, e.g. 
3802 <small>MD5</small> or <small>SHA1</small>. Next, some padding, the <small>ASN.1</small>
3803 "Algorithm Identifier" for the hash function, and the message digest
3804 itself, are concatenated and converted to a number <code>x</code>. The
3805 signature is computed from <code>x</code> and the private key as <code>s = x^d
3806 mod n</code><a rel="footnote" href="#fn-1"><sup>1</sup></a>. The signature, <code>s</code> is a
3807 number of about the same size of <code>n</code>, and it usually encoded as a
3808 sequence of octets, most significant octet first.
3809
3810    <p>The verification operation is straight-forward, <code>x</code> is computed
3811 from the message in the same way as above. Then <code>s^e mod n</code> is
3812 computed, the operation returns true if and only if the result equals
3813 <code>x</code>.
3814
3815 <h3 class="subsection">Nettle's <small>RSA</small> support</h4>
3816
3817 <p>Nettle represents <small>RSA</small> keys using two structures that contain
3818 large numbers (of type <code>mpz_t</code>).
3819
3820 <p>
3821 <table width="100%">
3822 <tr>
3823 <td align="left"><b>rsa_public_key</b><i> size n e
3824      </i></td>
3825 <td align="right">Context struct</td>
3826 </tr>
3827 </table>
3828 <table width="95%" align="center">
3829 <tr><td>
3830 <code>size</code> is the size, in octets, of the modulo, and is used internally. 
3831 <code>n</code> and <code>e</code> is the public key. 
3832 </td></tr>
3833 </table>
3834
3835 <p>
3836 <table width="100%">
3837 <tr>
3838 <td align="left"><b>rsa_private_key</b><i> size d p q a b c
3839      </i></td>
3840 <td align="right">Context struct</td>
3841 </tr>
3842 </table>
3843 <table width="95%" align="center">
3844 <tr><td>
3845 <code>size</code> is the size, in octets, of the modulo, and is used internally. 
3846 <code>d</code> is the secret exponent, but it is not actually used when
3847 signing. Instead, the factors <code>p</code> and <code>q</code>, and the parameters
3848 <code>a</code>, <code>b</code> and <code>c</code> are used. They are computed from <code>p</code>,
3849 <code>q</code> and <code>e</code> such that <code>a e mod (p - 1) = 1, b e mod (q -
3850 1) = 1, c q mod p = 1</code>. 
3851 </td></tr>
3852 </table>
3853
3854    <p>Before use, these structs must be initialized by calling one of
3855
3856 <p>
3857 <table width="100%">
3858 <tr>
3859 <td align="left">void <b>rsa_public_key_init</b><i> </i>(<i>struct rsa_public_key *</i><var>pub</var><i></i>)<i>
3860      </i></td>
3861 <td align="right">Function</td>
3862 </tr>
3863
3864 <tr>
3865 <td align="left">void <b>rsa_private_key_init</b><i> </i>(<i>struct rsa_private_key *</i><var>key</var><i></i>)<i>
3866      </i></td>
3867 <td align="right">Function</td>
3868 </tr>
3869 </table>
3870 <table width="95%" align="center">
3871 <tr><td>
3872 Calls <code>mpz_init</code> on all numbers in the key struct. 
3873 </td></tr>
3874 </table>
3875
3876    <p>and when finished with them, the space for the numbers must be
3877 deallocated by calling one of
3878
3879 <p>
3880 <table width="100%">
3881 <tr>
3882 <td align="left">void <b>rsa_public_key_clear</b><i> </i>(<i>struct rsa_public_key *</i><var>pub</var><i></i>)<i>
3883      </i></td>
3884 <td align="right">Function</td>
3885 </tr>
3886
3887 <tr>
3888 <td align="left">void <b>rsa_private_key_clear</b><i> </i>(<i>struct rsa_private_key *</i><var>key</var><i></i>)<i>
3889      </i></td>
3890 <td align="right">Function</td>
3891 </tr>
3892 </table>
3893 <table width="95%" align="center">
3894 <tr><td>
3895 Calls <code>mpz_clear</code> on all numbers in the key struct. 
3896 </td></tr>
3897 </table>
3898
3899    <p>In general, Nettle's <small>RSA</small> functions deviates from Nettle's "no
3900 memory allocation"-policy. Space for all the numbers, both in the key structs
3901 above, and temporaries, are allocated dynamically. For information on how
3902 to customize allocation, see
3903 See <a href="gmp.html#Custom%20Allocation">GMP Allocation</a>.
3904
3905    <p>When you have assigned values to the attributes of a key, you must call
3906
3907 <p>
3908 <table width="100%">
3909 <tr>
3910 <td align="left">int <b>rsa_public_key_prepare</b><i> </i>(<i>struct rsa_public_key *</i><var>pub</var><i></i>)<i>
3911      </i></td>
3912 <td align="right">Function</td>
3913 </tr>
3914
3915 <tr>
3916 <td align="left">int <b>rsa_private_key_prepare</b><i> </i>(<i>struct rsa_private_key *</i><var>key</var><i></i>)<i>
3917      </i></td>
3918 <td align="right">Function</td>
3919 </tr>
3920 </table>
3921 <table width="95%" align="center">
3922 <tr><td>
3923 Computes the octet size of the key (stored in the <code>size</code> attribute,
3924 and may also do other basic sanity checks. Returns one if successful, or
3925 zero if the key can't be used, for instance if the modulo is smaller
3926 than the minimum size needed for <small>RSA</small> operations specified by PKCS#1. 
3927 </td></tr>
3928 </table>
3929
3930    <p>Before signing or verifying a message, you first hash it with the
3931 appropriate hash function. You pass the hash function's context struct
3932 to the <small>RSA</small> signature function, and it will extract the message
3933 digest and do the rest of the work. There are also alternative functions
3934 that take the hash digest as argument.
3935
3936    <p>There is currently no support for using SHA224 or SHA384 with
3937 <small>RSA</small> signatures, since there's no gain in either computation
3938 time nor message size compared to using SHA256 and SHA512, respectively.
3939
3940    <p>Creation and verification of signatures is done with the following functions:
3941
3942 <p>
3943 <table width="100%">
3944 <tr>
3945 <td align="left">int <b>rsa_md5_sign</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, struct md5_ctx *</i><var>hash</var><i>, mpz_t </i><var>signature</var><i></i>)<i>
3946      </i></td>
3947 <td align="right">Function</td>
3948 </tr>
3949
3950 <tr>
3951 <td align="left">int <b>rsa_sha1_sign</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, struct sha1_ctx *</i><var>hash</var><i>, mpz_t </i><var>signature</var><i></i>)<i>
3952      </i></td>
3953 <td align="right">Function</td>
3954 </tr>
3955
3956 <tr>
3957 <td align="left">int <b>rsa_sha256_sign</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, struct sha256_ctx *</i><var>hash</var><i>, mpz_t </i><var>signature</var><i></i>)<i>
3958      </i></td>
3959 <td align="right">Function</td>
3960 </tr>
3961
3962 <tr>
3963 <td align="left">int <b>rsa_sha512_sign</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, struct sha512_ctx *</i><var>hash</var><i>, mpz_t </i><var>signature</var><i></i>)<i>
3964      </i></td>
3965 <td align="right">Function</td>
3966 </tr>
3967 </table>
3968 <table width="95%" align="center">
3969 <tr><td>
3970 The signature is stored in <var>signature</var> (which must have been
3971 <code>mpz_init</code>'ed earlier). The hash context is reset so that it can be
3972 used for new messages. Returns one on success, or zero on failure. 
3973 Signing fails if the key is too small for the given hash size, e.g.,
3974 it's not possible to create a signature using SHA512 and a 512-bit
3975 <small>RSA</small> key. 
3976 </td></tr>
3977 </table>
3978
3979 <p>
3980 <table width="100%">
3981 <tr>
3982 <td align="left">int <b>rsa_md5_sign_digest</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, mpz_t </i><var>signature</var><i></i>)<i>
3983      </i></td>
3984 <td align="right">Function</td>
3985 </tr>
3986
3987 <tr>
3988 <td align="left">int <b>rsa_sha1_sign_digest</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, mpz_t </i><var>signature</var><i></i>)<i>;
3989      </i></td>
3990 <td align="right">Function</td>
3991 </tr>
3992
3993 <tr>
3994 <td align="left">int <b>rsa_sha256_sign_digest</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, mpz_t </i><var>signature</var><i></i>)<i>;
3995      </i></td>
3996 <td align="right">Function</td>
3997 </tr>
3998
3999 <tr>
4000 <td align="left">int <b>rsa_sha512_sign_digest</b><i> </i>(<i>const struct rsa_private_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, mpz_t </i><var>signature</var><i></i>)<i>;
4001      </i></td>
4002 <td align="right">Function</td>
4003 </tr>
4004 </table>
4005 <table width="95%" align="center">
4006 <tr><td>
4007 Creates a signature from the given hash digest. <var>digest</var> should
4008 point to a digest of size <code>MD5_DIGEST_SIZE</code>,
4009 <code>SHA1_DIGEST_SIZE</code>, or <code>SHA256_DIGEST_SIZE</code>, respectively. The
4010 signature is stored in <var>signature</var> (which must have been
4011 <code>mpz_init</code>:ed earlier). Returns one on success, or zero on failure. 
4012 </td></tr>
4013 </table>
4014
4015 <p>
4016 <table width="100%">
4017 <tr>
4018 <td align="left">int <b>rsa_md5_verify</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, struct md5_ctx *</i><var>hash</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4019      </i></td>
4020 <td align="right">Function</td>
4021 </tr>
4022
4023 <tr>
4024 <td align="left">int <b>rsa_sha1_verify</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, struct sha1_ctx *</i><var>hash</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4025      </i></td>
4026 <td align="right">Function</td>
4027 </tr>
4028
4029 <tr>
4030 <td align="left">int <b>rsa_sha256_verify</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, struct sha256_ctx *</i><var>hash</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4031      </i></td>
4032 <td align="right">Function</td>
4033 </tr>
4034
4035 <tr>
4036 <td align="left">int <b>rsa_sha512_verify</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, struct sha512_ctx *</i><var>hash</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4037      </i></td>
4038 <td align="right">Function</td>
4039 </tr>
4040 </table>
4041 <table width="95%" align="center">
4042 <tr><td>
4043 Returns 1 if the signature is valid, or 0 if it isn't. In either case,
4044 the hash context is reset so that it can be used for new messages. 
4045 </td></tr>
4046 </table>
4047
4048 <p>
4049 <table width="100%">
4050 <tr>
4051 <td align="left">int <b>rsa_md5_verify_digest</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4052      </i></td>
4053 <td align="right">Function</td>
4054 </tr>
4055
4056 <tr>
4057 <td align="left">int <b>rsa_sha1_verify_digest</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4058      </i></td>
4059 <td align="right">Function</td>
4060 </tr>
4061
4062 <tr>
4063 <td align="left">int <b>rsa_sha256_verify_digest</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4064      </i></td>
4065 <td align="right">Function</td>
4066 </tr>
4067
4068 <tr>
4069 <td align="left">int <b>rsa_sha512_verify_digest</b><i> </i>(<i>const struct rsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const mpz_t </i><var>signature</var><i></i>)<i>
4070      </i></td>
4071 <td align="right">Function</td>
4072 </tr>
4073 </table>
4074 <table width="95%" align="center">
4075 <tr><td>
4076 Returns 1 if the signature is valid, or 0 if it isn't. <var>digest</var> should
4077 point to a digest of size <code>MD5_DIGEST_SIZE</code>,
4078 <code>SHA1_DIGEST_SIZE</code>, or <code>SHA256_DIGEST_SIZE</code>, respectively. 
4079 </td></tr>
4080 </table>
4081
4082    <p>If you need to use the <small>RSA</small> trapdoor, the private key, in a way
4083 that isn't supported by the above functions Nettle also includes a
4084 function that computes <code>x^d mod n</code> and nothing more, using the
4085 <small>CRT</small> optimization.
4086
4087 <p>
4088 <table width="100%">
4089 <tr>
4090 <td align="left">void <b>rsa_compute_root</b><i> </i>(<i>struct rsa_private_key *</i><var>key</var><i>, mpz_t </i><var>x</var><i>, const mpz_t </i><var>m</var><i></i>)<i>
4091      </i></td>
4092 <td align="right">Function</td>
4093 </tr>
4094 </table>
4095 <table width="95%" align="center">
4096 <tr><td>
4097 Computes <code>x = m^d</code>, efficiently. 
4098 </td></tr>
4099 </table>
4100
4101    <p>At last, how do you create new keys?
4102
4103 <p>
4104 <table width="100%">
4105 <tr>
4106 <td align="left">int <b>rsa_generate_keypair</b><i> </i>(<i>struct rsa_public_key *</i><var>pub</var><i>, struct rsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, void *</i><var>progress_ctx</var><i>, nettle_progress_func </i><var>progress</var><i>, unsigned </i><var>n_size</var><i>, unsigned </i><var>e_size</var><i></i>)<i>;
4107      </i></td>
4108 <td align="right">Function</td>
4109 </tr>
4110 </table>
4111 <table width="95%" align="center">
4112 <tr><td>
4113 There are lots of parameters. <var>pub</var> and <var>key</var> is where the
4114 resulting key pair is stored. The structs should be initialized, but you
4115 don't need to call <code>rsa_public_key_prepare</code> or
4116 <code>rsa_private_key_prepare</code> after key generation.
4117
4118      <p><var>random_ctx</var> and <var>random</var> is a randomness generator. 
4119 <code>random(random_ctx, length, dst)</code> should generate <code>length</code>
4120 random octets and store them at <code>dst</code>. For advice, see
4121 See <a href="#Randomness">Randomness</a>.
4122
4123      <p><var>progress</var> and <var>progress_ctx</var> can be used to get callbacks
4124 during the key generation process, in order to uphold an illusion of
4125 progress. <var>progress</var> can be NULL, in that case there are no
4126 callbacks.
4127
4128      <p><var>size_n</var> is the desired size of the modulo, in bits. If <var>size_e</var>
4129 is non-zero, it is the desired size of the public exponent and a random
4130 exponent of that size is selected. But if <var>e_size</var> is zero, it is
4131 assumed that the caller has already chosen a value for <code>e</code>, and
4132 stored it in <var>pub</var>. 
4133 Returns one on success, and zero on failure. The function can fail for
4134 example if if <var>n_size</var> is too small, or if <var>e_size</var> is zero and
4135 <code>pub-&gt;e</code> is an even number. 
4136 </td></tr>
4137 </table>
4138
4139 <div class="node">
4140 <p><hr>
4141 Node:&nbsp;<a name="DSA">DSA</a>,
4142 Previous:&nbsp;<a rel="previous" accesskey="p" href="#RSA">RSA</a>,
4143 Up:&nbsp;<a rel="up" accesskey="u" href="#Public-key%20algorithms">Public-key algorithms</a>
4144 <br>
4145 </div>
4146
4147 <h3 class="subsection">Nettle's <small>DSA</small> support</h4>
4148
4149 <p>The <small>DSA</small> digital signature algorithm is more complex than
4150 <small>RSA</small>. It was specified during the early 1990s, and in 1994 NIST
4151 published <small>FIPS</small> 186 which is the authoritative specification. 
4152 Sometimes <small>DSA</small> is referred to using the acronym <small>DSS</small>,
4153 for Digital Signature Standard. The most recent revision of the
4154 specification, FIPS186-3, was issueed in 2009, and it adds support for
4155 larger hash functions than <small>sha1</small>.
4156
4157    <p>For <small>DSA</small>, the underlying mathematical problem is the
4158 computation of discreet logarithms. The public key consists of a large
4159 prime <code>p</code>, a small prime <code>q</code> which is a factor of <code>p-1</code>,
4160 a number <code>g</code> which generates a subgroup of order <code>q</code> modulo
4161 <code>p</code>, and an element <code>y</code> in that subgroup.
4162
4163    <p>In the original <small>DSA</small>, the size of <code>q</code> is fixed to 160
4164 bits, to match with the <small>SHA1</small> hash algorithm. The size of
4165 <code>p</code> is in principle unlimited, but the
4166 standard specifies only nine specific sizes: <code>512 + l*64</code>, where
4167 <code>l</code> is between 0 and 8. Thus, the maximum size of <code>p</code> is 1024
4168 bits, and sizes less than 1024 bits are considered obsolete and not
4169 secure.
4170
4171    <p>The subgroup requirement means that if you compute
4172
4173 <pre class="example">     g^t mod p
4174      </pre>
4175
4176    <p>for all possible integers <code>t</code>, you will get precisely <code>q</code>
4177 distinct values.
4178
4179    <p>The private key is a secret exponent <code>x</code>, such that
4180
4181 <pre class="example">     g^x = y mod p
4182      </pre>
4183
4184    <p>In mathematical speak, <code>x</code> is the <dfn>discrete logarithm</dfn> of
4185 <code>y</code> mod <code>p</code>, with respect to the generator <code>g</code>. The size
4186 of <code>x</code> will also be about the same size as <code>q</code>. The security of the
4187 <small>DSA</small> algorithm relies on the difficulty of the discrete
4188 logarithm problem. Current algorithms to compute discrete logarithms in
4189 this setting, and hence crack <small>DSA</small>, are of two types. The first
4190 type works directly in the (multiplicative) group of integers mod
4191 <code>p</code>. The best known algorithm of this type is the Number Field
4192 Sieve, and it's complexity is similar to the complexity of factoring
4193 numbers of the same size as <code>p</code>. The other type works in the
4194 smaller <code>q</code>-sized subgroup generated by <code>g</code>, which has a more
4195 difficult group structure. One good algorithm is Pollard-rho, which has
4196 complexity <code>sqrt(q)</code>.
4197
4198    <p>The important point is that security depends on the size of <em>both</em>
4199 <code>p</code> and <code>q</code>, and they should be choosen so that the difficulty
4200 of both discrete logarithm methods are comparable. Today, the security
4201 margin of the original <small>DSA</small> may be uncomfortably small. Using a
4202 <code>p</code> of 1024 bits implies that cracking using the number field sieve
4203 is expected to take about the same time as factoring a 1024-bit
4204 <small>RSA</small> modulo, and using a <code>q</code> of size 160 bits implies
4205 that cracking using Pollard-rho will take roughly <code>2^80</code> group
4206 operations. With the size of <code>q</code> fixed, tied to the <small>SHA1</small>
4207 digest size, it may be tempting to increase the size of <code>p</code> to,
4208 say, 4096 bits. This will provide excellent resistance against attacks
4209 like the number field sieve which works in the large group. But it will
4210 do very little to defend against Pollard-rho attacking the small
4211 subgroup; the attacker is slowed down at most by a single factor of 10
4212 due to the more expensive group operation. And the attacker will surely
4213 choose the latter attack.
4214
4215    <p>The signature generation algorithm is randomized; in order to create a
4216 <small>DSA</small> signature, you need a good source for random numbers
4217 (see <a href="#Randomness">Randomness</a>). Let us describe the common case of a 160-bit
4218 <code>q</code>.
4219
4220    <p>To create a signature, one starts with the hash digest of the message,
4221 <code>h</code>, which is a 160 bit number, and a random number <code>k,
4222 0&lt;k&lt;q</code>, also 160 bits. Next, one computes
4223
4224 <pre class="example">     r = (g^k mod p) mod q
4225      s = k^-1 (h + x r) mod q
4226      </pre>
4227
4228    <p>The signature is the pair <code>(r, s)</code>, two 160 bit numbers. Note the
4229 two different mod operations when computing <code>r</code>, and the use of the
4230 secret exponent <code>x</code>.
4231
4232    <p>To verify a signature, one first checks that <code>0 &lt; r,s &lt; q</code>, and
4233 then one computes backwards,
4234
4235 <pre class="example">     w = s^-1 mod q
4236      v = (g^(w h) y^(w r) mod p) mod q
4237      </pre>
4238
4239    <p>The signature is valid if <code>v = r</code>. This works out because <code>w =
4240 s^-1 mod q = k (h + x r)^-1 mod q</code>, so that
4241
4242 <pre class="example">     g^(w h) y^(w r) = g^(w h) (g^x)^(w r) = g^(w (h + x r)) = g^k
4243      </pre>
4244
4245    <p>When reducing mod <code>q</code> this yields <code>r</code>. Note that when
4246 verifying a signature, we don't know either <code>k</code> or <code>x</code>: those
4247 numbers are secret.
4248
4249    <p>If you can choose between <small>RSA</small> and <small>DSA</small>, which one is
4250 best? Both are believed to be secure. <small>DSA</small> gained popularity in
4251 the late 1990s, as a patent free alternative to <small>RSA</small>. Now that
4252 the <small>RSA</small> patents have expired, there's no compelling reason to
4253 want to use <small>DSA</small>. Today, the original <small>DSA</small> key size
4254 does not provide a large security margin, and it should probably be
4255 phased out together with <small>RSA</small> keys of 1024 bits. Using the
4256 revised <small>DSA</small> algorithm with a larger hash function, in
4257 particular, <small>SHA256</small>, a 256-bit <code>q</code>, and <code>p</code> of size
4258 2048 bits or more, should provide for a more comfortable security
4259 margin, but these variants are not yet in wide use.
4260
4261    <p><small>DSA</small> signatures are smaller than <small>RSA</small> signatures,
4262 which is important for some specialized applications.
4263
4264    <p>From a practical point of view, <small>DSA</small>'s need for a good
4265 randomness source is a serious disadvantage. If you ever use the same
4266 <code>k</code> (and <code>r</code>) for two different message, you leak your private
4267 key.
4268
4269 <h3 class="subsection">Nettle's <small>DSA</small> support</h4>
4270
4271 <p>Like for <small>RSA</small>, Nettle represents <small>DSA</small> keys using two
4272 structures, containing values of type <code>mpz_t</code>. For information on
4273 how to customize allocation, see See <a href="gmp.html#Custom%20Allocation">GMP Allocation</a>.
4274
4275    <p>Most of the <small>DSA</small> functions are very similar to the
4276 corresponding <small>RSA</small> functions, but there are a few differences
4277 pointed out below. For a start, there are no functions corresponding to
4278 <code>rsa_public_key_prepare</code> and <code>rsa_private_key_prepare</code>.
4279
4280 <p>
4281 <table width="100%">
4282 <tr>
4283 <td align="left"><b>dsa_public_key</b><i> p q g y
4284      </i></td>
4285 <td align="right">Context struct</td>
4286 </tr>
4287 </table>
4288 <table width="95%" align="center">
4289 <tr><td>
4290 The public parameters described above. 
4291 </td></tr>
4292 </table>
4293
4294 <p>
4295 <table width="100%">
4296 <tr>
4297 <td align="left"><b>dsa_private_key</b><i> x
4298      </i></td>
4299 <td align="right">Context struct</td>
4300 </tr>
4301 </table>
4302 <table width="95%" align="center">
4303 <tr><td>
4304 The private key <code>x</code>. 
4305 </td></tr>
4306 </table>
4307
4308    <p>Before use, these structs must be initialized by calling one of
4309
4310 <p>
4311 <table width="100%">
4312 <tr>
4313 <td align="left">void <b>dsa_public_key_init</b><i> </i>(<i>struct dsa_public_key *</i><var>pub</var><i></i>)<i>
4314      </i></td>
4315 <td align="right">Function</td>
4316 </tr>
4317
4318 <tr>
4319 <td align="left">void <b>dsa_private_key_init</b><i> </i>(<i>struct dsa_private_key *</i><var>key</var><i></i>)<i>
4320      </i></td>
4321 <td align="right">Function</td>
4322 </tr>
4323 </table>
4324 <table width="95%" align="center">
4325 <tr><td>
4326 Calls <code>mpz_init</code> on all numbers in the key struct. 
4327 </td></tr>
4328 </table>
4329
4330    <p>When finished with them, the space for the numbers must be
4331 deallocated by calling one of
4332
4333 <p>
4334 <table width="100%">
4335 <tr>
4336 <td align="left">void <b>dsa_public_key_clear</b><i> </i>(<i>struct dsa_public_key *</i><var>pub</var><i></i>)<i>
4337      </i></td>
4338 <td align="right">Function</td>
4339 </tr>
4340
4341 <tr>
4342 <td align="left">void <b>dsa_private_key_clear</b><i> </i>(<i>struct dsa_private_key *</i><var>key</var><i></i>)<i>
4343      </i></td>
4344 <td align="right">Function</td>
4345 </tr>
4346 </table>
4347 <table width="95%" align="center">
4348 <tr><td>
4349 Calls <code>mpz_clear</code> on all numbers in the key struct. 
4350 </td></tr>
4351 </table>
4352
4353    <p>Signatures are represented using the structure below, and need to be
4354 initialized and cleared in the same way as the key structs.
4355
4356 <p>
4357 <table width="100%">
4358 <tr>
4359 <td align="left"><b>dsa_signature</b><i> r s
4360      </i></td>
4361 <td align="right">Context struct</td>
4362 </tr>
4363 </table>
4364 <table width="95%" align="center">
4365 <tr><td>
4366 </td></tr>
4367 </table>
4368
4369 <p>
4370 <table width="100%">
4371 <tr>
4372 <td align="left">void <b>dsa_signature_init</b><i> </i>(<i>struct dsa_signature *</i><var>signature</var><i></i>)<i>
4373      </i></td>
4374 <td align="right">Function</td>
4375 </tr>
4376
4377 <tr>
4378 <td align="left">void <b>dsa_signature_clear</b><i> </i>(<i>struct dsa_signature *</i><var>signature</var><i></i>)<i>
4379      </i></td>
4380 <td align="right">Function</td>
4381 </tr>
4382 </table>
4383 <table width="95%" align="center">
4384 <tr><td>
4385 You must call <code>dsa_signature_init</code> before creating or using a
4386 signature, and call <code>dsa_signature_clear</code> when you are finished
4387 with it. 
4388 </td></tr>
4389 </table>
4390
4391    <p>For signing, you need to provide both the public and the private key
4392 (unlike <small>RSA</small>, where the private key struct includes all
4393 information needed for signing), and a source for random numbers. 
4394 Signatures can use the <small>SHA1</small> or the <small>SHA256</small> hash
4395 function, although the implementation of <small>DSA</small> with
4396 <small>SHA256</small> should be considered somewhat experimental due to lack
4397 of official test vectors and interoperability testing.
4398
4399 <p>
4400 <table width="100%">
4401 <tr>
4402 <td align="left">int <b>dsa_sha1_sign</b><i> </i>(<i>const struct dsa_public_key *</i><var>pub</var><i>, const struct dsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, struct sha1_ctx *</i><var>hash</var><i>, struct dsa_signature *</i><var>signature</var><i></i>)<i>
4403      </i></td>
4404 <td align="right">Function</td>
4405 </tr>
4406
4407 <tr>
4408 <td align="left">int <b>dsa_sha1_sign_digest</b><i> </i>(<i>const struct dsa_public_key *</i><var>pub</var><i>, const struct dsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, const uint8_t *</i><var>digest</var><i>, struct dsa_signature *</i><var>signature</var><i></i>)<i>
4409      </i></td>
4410 <td align="right">Function</td>
4411 </tr>
4412
4413 <tr>
4414 <td align="left">int <b>dsa_sha256_sign</b><i> </i>(<i>const struct dsa_public_key *</i><var>pub</var><i>, const struct dsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, struct sha256_ctx *</i><var>hash</var><i>, struct dsa_signature *</i><var>signature</var><i></i>)<i>
4415      </i></td>
4416 <td align="right">Function</td>
4417 </tr>
4418
4419 <tr>
4420 <td align="left">int <b>dsa_sha256_sign_digest</b><i> </i>(<i>const struct dsa_public_key *</i><var>pub</var><i>, const struct dsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, const uint8_t *</i><var>digest</var><i>, struct dsa_signature *</i><var>signature</var><i></i>)<i>
4421      </i></td>
4422 <td align="right">Function</td>
4423 </tr>
4424 </table>
4425 <table width="95%" align="center">
4426 <tr><td>
4427 Creates a signature from the given hash context or digest. 
4428 <var>random_ctx</var> and <var>random</var> is a randomness generator. 
4429 <code>random(random_ctx, length, dst)</code> should generate <code>length</code>
4430 random octets and store them at <code>dst</code>. For advice, see
4431 See <a href="#Randomness">Randomness</a>. Returns one on success, or zero on failure. 
4432 Signing fails if the key size and the hash size don't match. 
4433 </td></tr>
4434 </table>
4435
4436    <p>Verifying signatures is a little easier, since no randomness generator is
4437 needed. The functions are
4438
4439 <p>
4440 <table width="100%">
4441 <tr>
4442 <td align="left">int <b>dsa_sha1_verify</b><i> </i>(<i>const struct dsa_public_key *</i><var>key</var><i>, struct sha1_ctx *</i><var>hash</var><i>, const struct dsa_signature *</i><var>signature</var><i></i>)<i>
4443      </i></td>
4444 <td align="right">Function</td>
4445 </tr>
4446
4447 <tr>
4448 <td align="left">int <b>dsa_sha1_verify_digest</b><i> </i>(<i>const struct dsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const struct dsa_signature *</i><var>signature</var><i></i>)<i>
4449      </i></td>
4450 <td align="right">Function</td>
4451 </tr>
4452
4453 <tr>
4454 <td align="left">int <b>dsa_sha256_verify</b><i> </i>(<i>const struct dsa_public_key *</i><var>key</var><i>, struct sha256_ctx *</i><var>hash</var><i>, const struct dsa_signature *</i><var>signature</var><i></i>)<i>
4455      </i></td>
4456 <td align="right">Function</td>
4457 </tr>
4458
4459 <tr>
4460 <td align="left">int <b>dsa_sha256_verify_digest</b><i> </i>(<i>const struct dsa_public_key *</i><var>key</var><i>, const uint8_t *</i><var>digest</var><i>, const struct dsa_signature *</i><var>signature</var><i></i>)<i>
4461      </i></td>
4462 <td align="right">Function</td>
4463 </tr>
4464 </table>
4465 <table width="95%" align="center">
4466 <tr><td>
4467 Verifies a signature. Returns 1 if the signature is valid, otherwise 0. 
4468 </td></tr>
4469 </table>
4470
4471    <p>Key generation uses mostly the same parameters as the corresponding
4472 <small>RSA</small> function.
4473
4474 <p>
4475 <table width="100%">
4476 <tr>
4477 <td align="left">int <b>dsa_generate_keypair</b><i> </i>(<i>struct dsa_public_key *</i><var>pub</var><i>, struct dsa_private_key *</i><var>key</var><i>, void *</i><var>random_ctx</var><i>, nettle_random_func </i><var>random</var><i>, void *</i><var>progress_ctx</var><i>, nettle_progress_func </i><var>progress</var><i>, unsigned </i><var>p_bits</var><i>, unsigned </i><var>q_bits</var><i></i>)<i>
4478      </i></td>
4479 <td align="right">Function</td>
4480 </tr>
4481 </table>
4482 <table width="95%" align="center">
4483 <tr><td>
4484 <var>pub</var> and <var>key</var> is where the resulting key pair is stored. The
4485 structs should be initialized before you call this function.
4486
4487      <p><var>random_ctx</var> and <var>random</var> is a randomness generator. 
4488 <code>random(random_ctx, length, dst)</code> should generate <code>length</code>
4489 random octets and store them at <code>dst</code>. For advice, see
4490 See <a href="#Randomness">Randomness</a>.
4491
4492      <p><var>progress</var> and <var>progress_ctx</var> can be used to get callbacks
4493 during the key generation process, in order to uphold an illusion of
4494 progress. <var>progress</var> can be NULL, in that case there are no
4495 callbacks.
4496
4497      <p><var>p_bits</var> and <var>q_bits</var> are the desired sizes of <code>p</code> and
4498 <code>q</code>. To generate keys that conform to the original <small>DSA</small>
4499 standard, you must use <code>q_bits = 160</code> and select <var>p_bits</var> of
4500 the form <code>p_bits = 512 + l*64</code>, for <code>0 &lt;= l &lt;= 8</code>, where the
4501 smaller sizes are no longer recommended, so you should most likely stick
4502 to <code>p_bits = 1024</code>. Non-standard sizes are possible, in particular
4503 <code>p_bits</code> larger than 1024, although <small>DSA</small> implementations
4504 can not in general be expected to support such keys. Also note that
4505 using very large <var>p_bits</var>, with <var>q_bits</var> fixed at 160, doesn't
4506 make much sense, because the security is also limited by the size of the
4507 smaller prime. Using a larger <code>q_bits</code> requires switchign to a
4508 larger hash function. To generate <small>DSA</small> keys for use with
4509 <small>SHA256</small>, use <code>q_bits = 256</code> and, e.g., <code>p_bits =
4510 2048</code>.
4511
4512      <p>Returns one on success, and zero on failure. The function will fail if
4513 <var>q_bits</var> is neither 160 nor 256, or if <var>p_bits</var> is unreasonably
4514 small. 
4515 </td></tr>
4516 </table>
4517
4518 <div class="node">
4519 <p><hr>
4520 Node:&nbsp;<a name="Randomness">Randomness</a>,
4521 Next:&nbsp;<a rel="next" accesskey="n" href="#Miscellaneous%20functions">Miscellaneous functions</a>,
4522 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Public-key%20algorithms">Public-key algorithms</a>,
4523 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
4524 <br>
4525 </div>
4526
4527 <h3 class="section">Randomness</h3>
4528
4529 <p>A crucial ingredient in many cryptographic contexts is randomness: Let
4530 <code>p</code> be a random prime, choose a random initialization vector
4531 <code>iv</code>, a random key <code>k</code> and a random exponent <code>e</code>, etc. In
4532 the theories, it is assumed that you have plenty of randomness around. 
4533 If this assumption is not true in practice, systems that are otherwise
4534 perfectly secure, can be broken. Randomness has often turned out to be
4535 the weakest link in the chain.
4536
4537    <p>In non-cryptographic applications, such as games as well as scientific
4538 simulation, a good randomness generator usually means a generator that
4539 has good statistical properties, and is seeded by some simple function
4540 of things like the current time, process id, and host name.
4541
4542    <p>However, such a generator is inadequate for cryptography, for at least
4543 two reasons:
4544
4545      <ul>
4546
4547      <li>It's too easy for an attacker to guess the initial seed. Even if it will
4548 take some 2^32 tries before he guesses right, that's far too easy. For
4549 example, if the process id is 16 bits, the resolution of "current time"
4550 is one second, and the attacker knows what day the generator was seeded,
4551 there are only about 2^32 possibilities to try if all possible values
4552 for the process id and time-of-day are tried.
4553
4554      <li>The generator output reveals too much. By observing only a small segment
4555 of the generator's output, its internal state can be recovered, and from
4556 there, all previous output and all future output can be computed by the
4557 attacker. 
4558 </ul>
4559
4560    <p>A randomness generator that is used for cryptographic purposes must have
4561 better properties. Let's first look at the seeding, as the issues here
4562 are mostly independent of the rest of the generator. The initial state
4563 of the generator (its seed) must be unguessable by the attacker. So
4564 what's unguessable? It depends on what the attacker already knows. The
4565 concept used in information theory to reason about such things is called
4566 "entropy", or "conditional entropy" (not to be confused with the
4567 thermodynamic concept with the same name). A reasonable requirement is
4568 that the seed contains a conditional entropy of at least some 80-100
4569 bits. This property can be explained as follows: Allow the attacker to
4570 ask <code>n</code> yes-no-questions, of his own choice, about the seed. If
4571 the attacker, using this question-and-answer session, as well as any
4572 other information he knows about the seeding process, still can't guess
4573 the seed correctly, then the conditional entropy is more than <code>n</code>
4574 bits.
4575
4576    <p>Let's look at an example. Say information about timing of received
4577 network packets is used in the seeding process. If there is some random
4578 network traffic going on, this will contribute some bits of entropy or
4579 "unguessability" to the seed. However, if the attacker can listen in to
4580 the local network, or if all but a small number of the packets were
4581 transmitted by machines that the attacker can monitor, this additional
4582 information makes the seed easier for the attacker to figure out. Even
4583 if the information is exactly the same, the conditional entropy, or
4584 unguessability, is smaller for an attacker that knows some of it already
4585 before the hypothetical question-and-answer session.
4586
4587    <p>Seeding of good generators is usually based on several sources. The key
4588 point here is that the amount of unguessability that each source
4589 contributes, depends on who the attacker is. Some sources that have been
4590 used are:
4591
4592      <dl>
4593 <dt>High resolution timing of i/o activities
4594      <dd>Such as completed blocks from spinning hard disks, network packets, etc. 
4595 Getting access to such information is quite system dependent, and not
4596 all systems include suitable hardware. If available, it's one of the
4597 better randomness source one can find in a digital, mostly predictable,
4598 computer.
4599
4600      <br><dt>User activity
4601      <dd>Timing and contents of user interaction events is another popular source
4602 that is available for interactive programs (even if I suspect that it is
4603 sometimes used in order to make the user feel good, not because the
4604 quality of the input is needed or used properly). Obviously, not
4605 available when a machine is unattended. Also beware of networks: User
4606 interaction that happens across a long serial cable, <small>TELNET</small>
4607 session, or even <small>SSH</small> session may be visible to an attacker, in
4608 full or partially.
4609
4610      <br><dt>Audio input
4611      <dd>Any room, or even a microphone input that's left unconnected, is a
4612 source of some random background noise, which can be fed into the
4613 seeding process.
4614
4615      <br><dt>Specialized hardware
4616      <dd>Hardware devices with the sole purpose of generating random data have
4617 been designed. They range from radioactive samples with an attached
4618 Geiger counter, to amplification of the inherent noise in electronic
4619 components such as diodes and resistors, to low-frequency sampling of
4620 chaotic systems. Hashing successive images of a Lava lamp is a
4621 spectacular example of the latter type.
4622
4623      <br><dt>Secret information
4624      <dd>Secret information, such as user passwords or keys, or private files
4625 stored on disk, can provide some unguessability. A problem is that if
4626 the information is revealed at a later time, the unguessability
4627 vanishes. Another problem is that this kind of information tends to be
4628 fairly constant, so if you rely on it and seed your generator regularly,
4629 you risk constructing almost similar seeds or even constructing the same
4630 seed more than once. 
4631 </dl>
4632
4633    <p>For all practical sources, it's difficult but important to provide a
4634 reliable lower bound on the amount of unguessability that it provides. 
4635 Two important points are to make sure that the attacker can't observe
4636 your sources (so if you like the Lava lamp idea, remember that you have
4637 to get your own lamp, and not put it by a window or anywhere else where
4638 strangers can see it), and that hardware failures are detected. What if
4639 the bulb in the Lava lamp, which you keep locked into a cupboard
4640 following the above advice, breaks after a few months?
4641
4642    <p>So let's assume that we have been able to find an unguessable seed,
4643 which contains at least 80 bits of conditional entropy, relative to all
4644 attackers that we care about (typically, we must at the very least
4645 assume that no attacker has root privileges on our machine).
4646
4647    <p>How do we generate output from this seed, and how much can we get? Some
4648 generators (notably the Linux <code>/dev/random</code> generator) tries to
4649 estimate available entropy and restrict the amount of output. The goal
4650 is that if you read 128 bits from <code>/dev/random</code>, you should get 128
4651 "truly random" bits. This is a property that is useful in some
4652 specialized circumstances, for instance when generating key material for
4653 a one time pad, or when working with unconditional blinding, but in most
4654 cases, it doesn't matter much. For most application, there's no limit on
4655 the amount of useful "random" data that we can generate from a small
4656 seed; what matters is that the seed is unguessable and that the
4657 generator has good cryptographic properties.
4658
4659    <p>At the heart of all generators lies its internal state. Future output
4660 is determined by the internal state alone. Let's call it the generator's
4661 key. The key is initialized from the unguessable seed. Important
4662 properties of a generator are:
4663
4664      <dl>
4665
4666      <br><dt><dfn>Key-hiding</dfn>
4667      <dd>An attacker observing the output should not be able to recover the
4668 generator's key.
4669
4670      <br><dt><dfn>Independence of outputs</dfn>
4671      <dd>Observing some of the output should not help the attacker to guess
4672 previous or future output.
4673
4674      <br><dt><dfn>Forward secrecy</dfn>
4675      <dd>Even if an attacker compromises the generator's key, he should not be
4676 able to guess the generator output <em>before</em> the key compromise.
4677
4678      <br><dt><dfn>Recovery from key compromise</dfn>
4679      <dd>If an attacker compromises the generator's key, he can compute
4680 <em>all</em> future output. This is inevitable if the generator is seeded
4681 only once, at startup. However, the generator can provide a reseeding
4682 mechanism, to achieve recovery from key compromise. More precisely: If
4683 the attacker compromises the key at a particular time <code>t_1</code>, there
4684 is another later time <code>t_2</code>, such that if the attacker observes all
4685 output generated between <code>t_1</code> and <code>t_2</code>, he still can't guess
4686 what output is generated after <code>t_2</code>.
4687
4688    </dl>
4689
4690    <p>Nettle includes one randomness generator that is believed to have all
4691 the above properties, and two simpler ones.
4692
4693    <p><small>ARCFOUR</small>, like any stream cipher, can be used as a randomness
4694 generator. Its output should be of reasonable quality, if the seed is
4695 hashed properly before it is used with <code>arcfour_set_key</code>. There's
4696 no single natural way to reseed it, but if you need reseeding, you
4697 should be using Yarrow instead.
4698
4699    <p>The "lagged Fibonacci" generator in <code>&lt;nettle/knuth-lfib.h&gt;</code> is a
4700 fast generator with good statistical properties, but is <strong>not</strong> for
4701 cryptographic use, and therefore not documented here. It is included
4702 mostly because the Nettle test suite needs to generate some test data
4703 from a small seed.
4704
4705    <p>The recommended generator to use is Yarrow, described below.
4706
4707 <h3 class="subsection">Yarrow</h4>
4708
4709 <p>Yarrow is a family of pseudo-randomness generators, designed for
4710 cryptographic use, by John Kelsey, Bruce Schneier and Niels Ferguson. 
4711 Yarrow-160 is described in a paper at
4712 &lt;<code>http://www.counterpane.com/yarrow.html</code>&gt;, and it uses <small>SHA1</small>
4713 and triple-DES, and has a 160-bit internal state. Nettle implements
4714 Yarrow-256, which is similar, but uses <small>SHA256</small> and
4715 <small>AES</small> to get an internal state of 256 bits.
4716
4717    <p>Yarrow was an almost finished project, the paper mentioned above is the
4718 closest thing to a specification for it, but some smaller details are
4719 left out. There is no official reference implementation or test cases. 
4720 This section includes an overview of Yarrow, but for the details of
4721 Yarrow-256, as implemented by Nettle, you have to consult the source
4722 code. Maybe a complete specification can be written later.
4723
4724    <p>Yarrow can use many sources (at least two are needed for proper
4725 reseeding), and two randomness "pools", referred to as the "slow pool" and
4726 the "fast pool". Input from the sources is fed alternatingly into the
4727 two pools. When one of the sources has contributed 100 bits of entropy
4728 to the fast pool, a "fast reseed" happens and the fast pool is mixed
4729 into the internal state. When at least two of the sources have
4730 contributed at least 160 bits each to the slow pool, a "slow reseed"
4731 takes place. The contents of both pools are mixed into the internal
4732 state. These procedures should ensure that the generator will eventually
4733 recover after a key compromise.
4734
4735    <p>The output is generated by using <small>AES</small> to encrypt a counter,
4736 using the generator's current key. After each request for output,
4737 another 256 bits are generated which replace the key. This ensures
4738 forward secrecy.
4739
4740    <p>Yarrow can also use a <dfn>seed file</dfn> to save state across restarts. 
4741 Yarrow is seeded by either feeding it the contents of the previous seed
4742 file, or feeding it input from its sources until a slow reseed happens.
4743
4744    <p>Nettle defines Yarrow-256 in <code>&lt;nettle/yarrow.h&gt;</code>.
4745
4746 <p>
4747 <table width="100%">
4748 <tr>
4749 <td align="left"><b>struct yarrow256_ctx</b><i>
4750      </i></td>
4751 <td align="right">Context struct</td>
4752 </tr>
4753 </table>
4754 <table width="95%" align="center">
4755 <tr><td>
4756 </td></tr>
4757 </table>
4758
4759 <p>
4760 <table width="100%">
4761 <tr>
4762 <td align="left"><b>struct yarrow_source</b><i>
4763      </i></td>
4764 <td align="right">Context struct</td>
4765 </tr>
4766 </table>
4767 <table width="95%" align="center">
4768 <tr><td>
4769 Information about a single source. 
4770 </td></tr>
4771 </table>
4772
4773 <p>
4774 <table width="100%">
4775 <tr>
4776 <td align="left"><b>YARROW256_SEED_FILE_SIZE</b><i>
4777      </i></td>
4778 <td align="right">Constant</td>
4779 </tr>
4780 </table>
4781 <table width="95%" align="center">
4782 <tr><td>
4783 Recommanded size of the Yarrow-256 seed file. 
4784 </td></tr>
4785 </table>
4786
4787 <p>
4788 <table width="100%">
4789 <tr>
4790 <td align="left">void <b>yarrow256_init</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i>, unsigned </i><var>nsources</var><i>, struct yarrow_source *</i><var>sources</var><i></i>)<i>
4791      </i></td>
4792 <td align="right">Function</td>
4793 </tr>
4794 </table>
4795 <table width="95%" align="center">
4796 <tr><td>
4797 Initializes the yarrow context, and its <var>nsources</var> sources. It's
4798 possible to call it with <var>nsources</var>=0 and <var>sources</var>=NULL, if
4799 you don't need the update features. 
4800 </td></tr>
4801 </table>
4802
4803 <p>
4804 <table width="100%">
4805 <tr>
4806 <td align="left">void <b>yarrow256_seed</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>seed_file</var><i></i>)<i>
4807      </i></td>
4808 <td align="right">Function</td>
4809 </tr>
4810 </table>
4811 <table width="95%" align="center">
4812 <tr><td>
4813 Seeds Yarrow-256 from a previous seed file. <var>length</var> should be at least
4814 <code>YARROW256_SEED_FILE_SIZE</code>, but it can be larger.
4815
4816      <p>The generator will trust you that the <var>seed_file</var> data really is
4817 unguessable. After calling this function, you <em>must</em> overwrite the old
4818 seed file with newly generated data from <code>yarrow256_random</code>. If it's
4819 possible for several processes to read the seed file at about the same
4820 time, access must be coordinated using some locking mechanism. 
4821 </td></tr>
4822 </table>
4823
4824 <p>
4825 <table width="100%">
4826 <tr>
4827 <td align="left">int <b>yarrow256_update</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i>, unsigned </i><var>source</var><i>, unsigned </i><var>entropy</var><i>, unsigned </i><var>length</var><i>, const uint8_t *</i><var>data</var><i></i>)<i>
4828      </i></td>
4829 <td align="right">Function</td>
4830 </tr>
4831 </table>
4832 <table width="95%" align="center">
4833 <tr><td>
4834 Updates the generator with data from source <var>SOURCE</var> (an index that
4835 must be smaller than the number of sources). <var>entropy</var> is your
4836 estimated lower bound for the entropy in the data, measured in bits. 
4837 Calling update with zero <var>entropy</var> is always safe, no matter if the
4838 data is random or not.
4839
4840      <p>Returns 1 if a reseed happened, in which case an application using a
4841 seed file may want to generate new seed data with
4842 <code>yarrow256_random</code> and overwrite the seed file. Otherwise, the
4843 function returns 0. 
4844 </td></tr>
4845 </table>
4846
4847 <p>
4848 <table width="100%">
4849 <tr>
4850 <td align="left">void <b>yarrow256_random</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i>, unsigned </i><var>length</var><i>, uint8_t *</i><var>dst</var><i></i>)<i>
4851      </i></td>
4852 <td align="right">Function</td>
4853 </tr>
4854 </table>
4855 <table width="95%" align="center">
4856 <tr><td>
4857 Generates <var>length</var> octets of output. The generator must be seeded
4858 before you call this function.
4859
4860      <p>If you don't need forward secrecy, e.g. if you need non-secret
4861 randomness for initialization vectors or padding, you can gain some
4862 efficiency by buffering, calling this function for reasonably large
4863 blocks of data, say 100-1000 octets at a time. 
4864 </td></tr>
4865 </table>
4866
4867 <p>
4868 <table width="100%">
4869 <tr>
4870 <td align="left">int <b>yarrow256_is_seeded</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4871      </i></td>
4872 <td align="right">Function</td>
4873 </tr>
4874 </table>
4875 <table width="95%" align="center">
4876 <tr><td>
4877 Returns 1 if the generator is seeded and ready to generate output,
4878 otherwise 0. 
4879 </td></tr>
4880 </table>
4881
4882 <p>
4883 <table width="100%">
4884 <tr>
4885 <td align="left">unsigned <b>yarrow256_needed_sources</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4886      </i></td>
4887 <td align="right">Function</td>
4888 </tr>
4889 </table>
4890 <table width="95%" align="center">
4891 <tr><td>
4892 Returns the number of sources that must reach the threshold before a
4893 slow reseed will happen. Useful primarily when the generator is unseeded. 
4894 </td></tr>
4895 </table>
4896
4897 <p>
4898 <table width="100%">
4899 <tr>
4900 <td align="left">void <b>yarrow256_fast_reseed</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4901      </i></td>
4902 <td align="right">Function</td>
4903 </tr>
4904
4905 <tr>
4906 <td align="left">void <b>yarrow256_slow_reseed</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4907      </i></td>
4908 <td align="right">Function</td>
4909 </tr>
4910 </table>
4911 <table width="95%" align="center">
4912 <tr><td>
4913 Causes a fast or slow reseed to take place immediately, regardless of the
4914 current entropy estimates of the two pools. Use with care. 
4915 </td></tr>
4916 </table>
4917
4918    <p>Nettle includes an entropy estimator for one kind of input source: User
4919 keyboard input.
4920
4921 <p>
4922 <table width="100%">
4923 <tr>
4924 <td align="left"><b>struct yarrow_key_event_ctx</b><i>
4925      </i></td>
4926 <td align="right">Context struct</td>
4927 </tr>
4928 </table>
4929 <table width="95%" align="center">
4930 <tr><td>
4931 Information about recent key events. 
4932 </td></tr>
4933 </table>
4934
4935 <p>
4936 <table width="100%">
4937 <tr>
4938 <td align="left">void <b>yarrow_key_event_init</b><i> </i>(<i>struct yarrow_key_event_ctx *</i><var>ctx</var><i></i>)<i>
4939      </i></td>
4940 <td align="right">Function</td>
4941 </tr>
4942 </table>
4943 <table width="95%" align="center">
4944 <tr><td>
4945 Initializes the context. 
4946 </td></tr>
4947 </table>
4948
4949 <p>
4950 <table width="100%">
4951 <tr>
4952 <td align="left">unsigned <b>yarrow_key_event_estimate</b><i> </i>(<i>struct yarrow_key_event_ctx *</i><var>ctx</var><i>, unsigned </i><var>key</var><i>, unsigned </i><var>time</var><i></i>)<i>
4953      </i></td>
4954 <td align="right">Function</td>
4955 </tr>
4956 </table>
4957 <table width="95%" align="center">
4958 <tr><td>
4959 <var>key</var> is the id of the key (ASCII value, hardware key code, X
4960 keysym, <small class="dots">...</small>, it doesn't matter), and <var>time</var> is the timestamp of
4961 the event. The time must be given in units matching the resolution by
4962 which you read the clock. If you read the clock with microsecond
4963 precision, <var>time</var> should be provided in units of microseconds. But
4964 if you use <code>gettimeofday</code> on a typical Unix system where the clock
4965 ticks 10 or so microseconds at a time, <var>time</var> should be given in
4966 units of 10 microseconds.
4967
4968      <p>Returns an entropy estimate, in bits, suitable for calling
4969 <code>yarrow256_update</code>. Usually, 0, 1 or 2 bits. 
4970 </td></tr>
4971 </table>
4972
4973 <div class="node">
4974 <p><hr>
4975 Node:&nbsp;<a name="Miscellaneous%20functions">Miscellaneous functions</a>,
4976 Next:&nbsp;<a rel="next" accesskey="n" href="#Compatibility%20functions">Compatibility functions</a>,
4977 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Randomness">Randomness</a>,
4978 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
4979 <br>
4980 </div>
4981
4982 <h3 class="section">Miscellaneous functions</h3>
4983
4984 <p>
4985 <table width="100%">
4986 <tr>
4987 <td align="left">uint8_t * <b>memxor</b><i> </i>(<i>uint8_t *</i><var>dst</var><i>, const uint8_t *</i><var>src</var><i>, size_t </i><var>n</var><i></i>)<i>
4988      </i></td>
4989 <td align="right">Function</td>
4990 </tr>
4991 </table>
4992 <table width="95%" align="center">
4993 <tr><td>
4994 XORs the source area on top of the destination area. The interface
4995 doesn't follow the Nettle conventions, because it is intended to be
4996 similar to the ANSI-C <code>memcpy</code> function. 
4997 </td></tr>
4998 </table>
4999
5000    <p><code>memxor</code> is declared in <code>&lt;nettle/memxor.h&gt;</code>.
5001
5002 <div class="node">
5003 <p><hr>
5004 Node:&nbsp;<a name="Compatibility%20functions">Compatibility functions</a>,
5005 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Miscellaneous%20functions">Miscellaneous functions</a>,
5006 Up:&nbsp;<a rel="up" accesskey="u" href="#Reference">Reference</a>
5007 <br>
5008 </div>
5009
5010 <h3 class="section">Compatibility functions</h3>
5011
5012 <p>For convenience, Nettle includes alternative interfaces to some
5013 algorithms, for compatibility with some other popular crypto toolkits. 
5014 These are not fully documented here; refer to the source or to the
5015 documentation for the original implementation.
5016
5017    <p>MD5 is defined in [RFC 1321], which includes a reference implementation. 
5018 Nettle defines a compatible interface to MD5 in
5019 <code>&lt;nettle/md5-compat.h&gt;</code>. This file defines the typedef
5020 <code>MD5_CTX</code>, and declares the functions <code>MD5Init</code>, <code>MD5Update</code> and
5021 <code>MD5Final</code>.
5022
5023    <p>Eric Young's "libdes" (also part of OpenSSL) is a quite popular DES
5024 implementation. Nettle includes a subset if its interface in
5025 <code>&lt;nettle/des-compat.h&gt;</code>. This file defines the typedefs
5026 <code>des_key_schedule</code> and <code>des_cblock</code>, two constants
5027 <code>DES_ENCRYPT</code> and <code>DES_DECRYPT</code>, and declares one global
5028 variable <code>des_check_key</code>, and the functions <code>des_cbc_cksum</code>
5029 <code>des_cbc_encrypt</code>, <code>des_ecb2_encrypt</code>,
5030 <code>des_ecb3_encrypt</code>, <code>des_ecb_encrypt</code>,
5031 <code>des_ede2_cbc_encrypt</code>, <code>des_ede3_cbc_encrypt</code>,
5032 <code>des_is_weak_key</code>, <code>des_key_sched</code>, <code>des_ncbc_encrypt</code>
5033 <code>des_set_key</code>, and <code>des_set_odd_parity</code>.
5034
5035 <div class="node">
5036 <p><hr>
5037 Node:&nbsp;<a name="Nettle%20soup">Nettle soup</a>,
5038 Next:&nbsp;<a rel="next" accesskey="n" href="#Installation">Installation</a>,
5039 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Reference">Reference</a>,
5040 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
5041 <br>
5042 </div>
5043
5044 <h2 class="chapter">Traditional Nettle Soup</h2>
5045
5046 <p>For the serious nettle hacker, here is a recipe for nettle soup. 4 servings.
5047
5048      <ul>
5049 <li>1 liter fresh nettles (urtica dioica)
5050 <li>2 tablespoons butter
5051 <li>3 tablespoons flour
5052 <li>1 liter stock (meat or vegetable)
5053 <li>1/2 teaspoon salt
5054 <li>a tad white pepper
5055 <li>some cream or milk
5056 </ul>
5057
5058    <p>Gather 1 liter fresh nettles. Use gloves! Small, tender shoots are
5059 preferable but the tops of larger nettles can also be used.
5060
5061    <p>Rinse the nettles very well. Boil them for 10 minutes in lightly salted
5062 water. Strain the nettles and save the water. Hack the nettles. Melt the
5063 butter and mix in the flour. Dilute with stock and the nettle-water you
5064 saved earlier. Add the hacked nettles. If you wish you can add some milk
5065 or cream at this stage. Bring to a boil and let boil for a few minutes. 
5066 Season with salt and pepper.
5067
5068    <p>Serve with boiled egg-halves.
5069
5070 <div class="node">
5071 <p><hr>
5072 Node:&nbsp;<a name="Installation">Installation</a>,
5073 Next:&nbsp;<a rel="next" accesskey="n" href="#Index">Index</a>,
5074 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Nettle%20soup">Nettle soup</a>,
5075 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
5076 <br>
5077 </div>
5078
5079 <h2 class="chapter">Installation</h2>
5080
5081 <p>Nettle uses <code>autoconf</code>. To build it, unpack the source and run
5082
5083 <pre class="example">     ./configure
5084      make
5085      make check
5086      make install
5087      </pre>
5088
5089 <p>to install in the default location, <code>/usr/local</code>. The library files
5090 are installed in <code>/use/local/lib/libnettle.a</code>
5091 <code>/use/local/lib/libhogweed.a</code> and the include files are installed
5092 in <code>/use/local/include/nettle/</code>.
5093
5094    <p>To get a list of configure options, use <code>./configure --help</code>.
5095
5096    <p>By default, only static libraries are built and installed. To also build
5097 and install shared libraries, use the <code> --enable-shared</code> option
5098 to <code>./configure</code>.
5099
5100    <p>Using GNU make is recommended. For other make programs, in particular
5101 BSD make, you may have to use the <code>--disable-dependency-tracking</code>
5102 option to <code>./configure</code>.
5103
5104 <div class="node">
5105 <p><hr>
5106 Node:&nbsp;<a name="Index">Index</a>,
5107 Previous:&nbsp;<a rel="previous" accesskey="p" href="#Installation">Installation</a>,
5108 Up:&nbsp;<a rel="up" accesskey="u" href="#Top">Top</a>
5109 <br>
5110 </div>
5111
5112 <h2 class="unnumbered">Function and Concept Index</h2>
5113
5114 <ul class="index-cp" compact>
5115 <li><code>aes_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5116 <li><code>aes_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5117 <li><code>aes_invert_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5118 <li><code>aes_set_decrypt_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5119 <li><code>aes_set_encrypt_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5120 <li><code>arcfour_crypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5121 <li><code>arcfour_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5122 <li><code>arctwo_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5123 <li><code>arctwo_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5124 <li><code>arctwo_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5125 <li><code>arctwo_set_key_ekb</code>: <a href="#Cipher%20functions">Cipher functions</a>
5126 <li><code>arctwo_set_key_gutmann</code>: <a href="#Cipher%20functions">Cipher functions</a>
5127 <li>Block Cipher: <a href="#Cipher%20functions">Cipher functions</a>
5128 <li><code>blowfish_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5129 <li><code>blowfish_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5130 <li><code>blowfish_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5131 <li><code>camellia_crypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5132 <li><code>camellia_invert_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5133 <li><code>camellia_set_decrypt_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5134 <li><code>camellia_set_encrypt_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5135 <li><code>cast128_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5136 <li><code>cast128_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5137 <li><code>cast128_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5138 <li>CBC Mode: <a href="#Cipher%20modes">Cipher modes</a>
5139 <li><code>CBC_CTX</code>: <a href="#Cipher%20modes">Cipher modes</a>
5140 <li><code>CBC_DECRYPT</code>: <a href="#Cipher%20modes">Cipher modes</a>
5141 <li><code>cbc_decrypt</code>: <a href="#Cipher%20modes">Cipher modes</a>
5142 <li><code>CBC_ENCRYPT</code>: <a href="#Cipher%20modes">Cipher modes</a>
5143 <li><code>cbc_encrypt</code>: <a href="#Cipher%20modes">Cipher modes</a>
5144 <li><code>CBC_SET_IV</code>: <a href="#Cipher%20modes">Cipher modes</a>
5145 <li>Cipher: <a href="#Cipher%20functions">Cipher functions</a>
5146 <li>Cipher Block Chaining: <a href="#Cipher%20modes">Cipher modes</a>
5147 <li>Collision-resistant: <a href="#Hash%20functions">Hash functions</a>
5148 <li>Conditional entropy: <a href="#Randomness">Randomness</a>
5149 <li>Counter Mode: <a href="#Cipher%20modes">Cipher modes</a>
5150 <li>CTR Mode: <a href="#Cipher%20modes">Cipher modes</a>
5151 <li><code>CTR_CRYPT</code>: <a href="#Cipher%20modes">Cipher modes</a>
5152 <li><code>ctr_crypt</code>: <a href="#Cipher%20modes">Cipher modes</a>
5153 <li><code>CTR_CTX</code>: <a href="#Cipher%20modes">Cipher modes</a>
5154 <li><code>CTR_SET_COUNTER</code>: <a href="#Cipher%20modes">Cipher modes</a>
5155 <li><code>des3_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5156 <li><code>des3_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5157 <li><code>des3_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5158 <li><code>des_check_parity</code>: <a href="#Cipher%20functions">Cipher functions</a>
5159 <li><code>des_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5160 <li><code>des_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5161 <li><code>des_fix_parity</code>: <a href="#Cipher%20functions">Cipher functions</a>
5162 <li><code>des_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5163 <li><code>dsa_generate_keypair</code>: <a href="#DSA">DSA</a>
5164 <li><code>dsa_private_key_clear</code>: <a href="#DSA">DSA</a>
5165 <li><code>dsa_private_key_init</code>: <a href="#DSA">DSA</a>
5166 <li><code>dsa_public_key_clear</code>: <a href="#DSA">DSA</a>
5167 <li><code>dsa_public_key_init</code>: <a href="#DSA">DSA</a>
5168 <li><code>dsa_sha1_sign</code>: <a href="#DSA">DSA</a>
5169 <li><code>dsa_sha1_sign_digest</code>: <a href="#DSA">DSA</a>
5170 <li><code>dsa_sha1_verify</code>: <a href="#DSA">DSA</a>
5171 <li><code>dsa_sha1_verify_digest</code>: <a href="#DSA">DSA</a>
5172 <li><code>dsa_sha256_sign</code>: <a href="#DSA">DSA</a>
5173 <li><code>dsa_sha256_sign_digest</code>: <a href="#DSA">DSA</a>
5174 <li><code>dsa_sha256_verify</code>: <a href="#DSA">DSA</a>
5175 <li><code>dsa_sha256_verify_digest</code>: <a href="#DSA">DSA</a>
5176 <li><code>dsa_signature_clear</code>: <a href="#DSA">DSA</a>
5177 <li><code>dsa_signature_init</code>: <a href="#DSA">DSA</a>
5178 <li>Entropy: <a href="#Randomness">Randomness</a>
5179 <li>Hash function: <a href="#Hash%20functions">Hash functions</a>
5180 <li><code>HMAC_CTX</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5181 <li><code>HMAC_DIGEST</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5182 <li><code>hmac_digest</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5183 <li><code>hmac_md5_digest</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5184 <li><code>hmac_md5_set_key</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5185 <li><code>hmac_md5_update</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5186 <li><code>HMAC_SET_KEY</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5187 <li><code>hmac_set_key</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5188 <li><code>hmac_sha1_digest</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5189 <li><code>hmac_sha1_set_key</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5190 <li><code>hmac_sha1_update</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5191 <li><code>hmac_sha256_digest</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5192 <li><code>hmac_sha256_set_key</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5193 <li><code>hmac_sha256_update</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5194 <li><code>hmac_sha512_digest</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5195 <li><code>hmac_sha512_set_key</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5196 <li><code>hmac_sha512_update</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5197 <li><code>hmac_update</code>: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5198 <li>Keyed Hash Function: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5199 <li>MAC: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5200 <li><code>md2_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5201 <li><code>md2_init</code>: <a href="#Hash%20functions">Hash functions</a>
5202 <li><code>md2_update</code>: <a href="#Hash%20functions">Hash functions</a>
5203 <li><code>md4_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5204 <li><code>md4_init</code>: <a href="#Hash%20functions">Hash functions</a>
5205 <li><code>md4_update</code>: <a href="#Hash%20functions">Hash functions</a>
5206 <li><code>md5_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5207 <li><code>md5_init</code>: <a href="#Hash%20functions">Hash functions</a>
5208 <li><code>md5_update</code>: <a href="#Hash%20functions">Hash functions</a>
5209 <li><code>memxor</code>: <a href="#Miscellaneous%20functions">Miscellaneous functions</a>
5210 <li>Message Authentication Code: <a href="#Keyed%20hash%20functions">Keyed hash functions</a>
5211 <li>One-way: <a href="#Hash%20functions">Hash functions</a>
5212 <li>One-way function: <a href="#Public-key%20algorithms">Public-key algorithms</a>
5213 <li>Public Key Cryptography: <a href="#Public-key%20algorithms">Public-key algorithms</a>
5214 <li>Randomness: <a href="#Randomness">Randomness</a>
5215 <li><code>rsa_compute_root</code>: <a href="#RSA">RSA</a>
5216 <li><code>rsa_generate_keypair</code>: <a href="#RSA">RSA</a>
5217 <li><code>rsa_md5_sign</code>: <a href="#RSA">RSA</a>
5218 <li><code>rsa_md5_sign_digest</code>: <a href="#RSA">RSA</a>
5219 <li><code>rsa_md5_verify</code>: <a href="#RSA">RSA</a>
5220 <li><code>rsa_md5_verify_digest</code>: <a href="#RSA">RSA</a>
5221 <li><code>rsa_private_key_clear</code>: <a href="#RSA">RSA</a>
5222 <li><code>rsa_private_key_init</code>: <a href="#RSA">RSA</a>
5223 <li><code>rsa_private_key_prepare</code>: <a href="#RSA">RSA</a>
5224 <li><code>rsa_public_key_clear</code>: <a href="#RSA">RSA</a>
5225 <li><code>rsa_public_key_init</code>: <a href="#RSA">RSA</a>
5226 <li><code>rsa_public_key_prepare</code>: <a href="#RSA">RSA</a>
5227 <li><code>rsa_sha1_sign</code>: <a href="#RSA">RSA</a>
5228 <li><code>rsa_sha1_sign_digest</code>: <a href="#RSA">RSA</a>
5229 <li><code>rsa_sha1_verify</code>: <a href="#RSA">RSA</a>
5230 <li><code>rsa_sha1_verify_digest</code>: <a href="#RSA">RSA</a>
5231 <li><code>rsa_sha256_sign</code>: <a href="#RSA">RSA</a>
5232 <li><code>rsa_sha256_sign_digest</code>: <a href="#RSA">RSA</a>
5233 <li><code>rsa_sha256_verify</code>: <a href="#RSA">RSA</a>
5234 <li><code>rsa_sha256_verify_digest</code>: <a href="#RSA">RSA</a>
5235 <li><code>rsa_sha512_sign</code>: <a href="#RSA">RSA</a>
5236 <li><code>rsa_sha512_sign_digest</code>: <a href="#RSA">RSA</a>
5237 <li><code>rsa_sha512_verify</code>: <a href="#RSA">RSA</a>
5238 <li><code>rsa_sha512_verify_digest</code>: <a href="#RSA">RSA</a>
5239 <li><code>serpent_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5240 <li><code>serpent_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5241 <li><code>serpent_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5242 <li><code>sha1_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5243 <li><code>sha1_init</code>: <a href="#Hash%20functions">Hash functions</a>
5244 <li><code>sha1_update</code>: <a href="#Hash%20functions">Hash functions</a>
5245 <li><code>sha224_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5246 <li><code>sha224_init</code>: <a href="#Hash%20functions">Hash functions</a>
5247 <li><code>sha224_update</code>: <a href="#Hash%20functions">Hash functions</a>
5248 <li><code>sha256_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5249 <li><code>sha256_init</code>: <a href="#Hash%20functions">Hash functions</a>
5250 <li><code>sha256_update</code>: <a href="#Hash%20functions">Hash functions</a>
5251 <li><code>sha384_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5252 <li><code>sha384_init</code>: <a href="#Hash%20functions">Hash functions</a>
5253 <li><code>sha384_update</code>: <a href="#Hash%20functions">Hash functions</a>
5254 <li><code>sha512_digest</code>: <a href="#Hash%20functions">Hash functions</a>
5255 <li><code>sha512_init</code>: <a href="#Hash%20functions">Hash functions</a>
5256 <li><code>sha512_update</code>: <a href="#Hash%20functions">Hash functions</a>
5257 <li>Stream Cipher: <a href="#Cipher%20functions">Cipher functions</a>
5258 <li><code>twofish_decrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5259 <li><code>twofish_encrypt</code>: <a href="#Cipher%20functions">Cipher functions</a>
5260 <li><code>twofish_set_key</code>: <a href="#Cipher%20functions">Cipher functions</a>
5261 <li><code>yarrow256_fast_reseed</code>: <a href="#Randomness">Randomness</a>
5262 <li><code>yarrow256_init</code>: <a href="#Randomness">Randomness</a>
5263 <li><code>yarrow256_is_seeded</code>: <a href="#Randomness">Randomness</a>
5264 <li><code>yarrow256_needed_sources</code>: <a href="#Randomness">Randomness</a>
5265 <li><code>yarrow256_random</code>: <a href="#Randomness">Randomness</a>
5266 <li><code>yarrow256_seed</code>: <a href="#Randomness">Randomness</a>
5267 <li><code>yarrow256_slow_reseed</code>: <a href="#Randomness">Randomness</a>
5268 <li><code>yarrow256_update</code>: <a href="#Randomness">Randomness</a>
5269 <li><code>yarrow_key_event_estimate</code>: <a href="#Randomness">Randomness</a>
5270 <li><code>yarrow_key_event_init</code>: <a href="#Randomness">Randomness</a>
5271 </ul>
5272
5273
5274    <div class="footnote">
5275 <hr>
5276 <h4>Footnotes</h4>
5277 <ol type="1">
5278 <li><a name="fn-1"></a>
5279 <p>Actually, the computation is not done like this, it is
5280 done more efficiently using <code>p</code>, <code>q</code> and the Chinese remainder
5281 theorem (<small>CRT</small>). But the result is the same.</p>
5282
5283    </ol><hr></div>
5284
5285    </body></html>
5286