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">
8 This manual is for the Nettle library (version 2.1), a
9 low-level cryptographic library.
11 <p>Originally written 2001 by Niels Möller, updated 2010.
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.
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 }
30 <h1 class="settitle">Nettle: a low-level cryptographic library</h1>
32 <div class="contents">
33 <h2>Table of Contents</h2>
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>
43 <li><a href="#Hash%20functions">Hash functions</a>
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>
55 <li><a href="#Cipher%20functions">Cipher functions</a>
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>
69 <li><a href="#Cipher%20modes">Cipher modes</a>
71 <li><a href="#Cipher%20modes">Cipher Block Chaining</a>
72 <li><a href="#Cipher%20modes">Counter mode</a>
74 <li><a href="#Keyed%20hash%20functions">Keyed Hash Functions</a>
76 <li><a href="#Keyed%20hash%20functions"><small>HMAC</small></a>
77 <li><a href="#Keyed%20hash%20functions">Concrete <small>HMAC</small> functions</a>
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>
85 <li><a href="#Public-key%20algorithms">Public-key algorithms</a>
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>
91 <li><a href="#Randomness">Randomness</a>
93 <li><a href="#Randomness">Yarrow</a>
95 <li><a href="#Miscellaneous%20functions">Miscellaneous functions</a>
96 <li><a href="#Compatibility%20functions">Compatibility functions</a>
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>
107 Node: <a name="Top">Top</a>,
108 Next: <a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
109 Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
110 Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
114 <h2 class="unnumbered">Nettle</h2>
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.
120 This manual is for the Nettle library (version 2.1), a
121 low-level cryptographic library.
123 <p>Originally written 2001 by Niels Möller, updated 2010.
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.
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.
145 Node: <a name="Introduction">Introduction</a>,
146 Next: <a rel="next" accesskey="n" href="#Copyright">Copyright</a>,
147 Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
148 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
152 <h2 class="chapter">Introduction</h2>
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.
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.
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.
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
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
184 Node: <a name="Copyright">Copyright</a>,
185 Next: <a rel="next" accesskey="n" href="#Conventions">Conventions</a>,
186 Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
187 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
191 <h2 class="chapter">Copyright</h2>
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.
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.
207 <p>A list of the supported algorithms, their origins and licenses:
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
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.
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.
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.
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.
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.
240 <dd>The implementation of the DES cipher is written by Dana L. How, and
241 released under the LGPL.
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).
249 <dd>This is almost the same code as for MD5 below, with modifications by
250 Marcus Comstedt. Released into the public domain.
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.
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.
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.
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.
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.
277 <dd>Written by Niels Möller, released under the LGPL. Uses the GMP library
278 for bignum operations.
281 <dd>Written by Niels Möller, released under the LGPL. Uses the GMP library
282 for bignum operations.
287 Node: <a name="Conventions">Conventions</a>,
288 Next: <a rel="next" accesskey="n" href="#Example">Example</a>,
289 Previous: <a rel="previous" accesskey="p" href="#Copyright">Copyright</a>,
290 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
294 <h2 class="chapter">Conventions</h2>
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
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.
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.
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
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.
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.
333 Node: <a name="Example">Example</a>,
334 Next: <a rel="next" accesskey="n" href="#Linking">Linking</a>,
335 Previous: <a rel="previous" accesskey="p" href="#Conventions">Conventions</a>,
336 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
340 <h2 class="chapter">Example</h2>
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
346 <pre class="example">
348 <pre class="verbatim">#include <stdio.h>
349 #include <stdlib.h>
351 #include <nettle/sha.h>
353 #define BUF_SIZE 1000
356 display_hex(unsigned length, uint8_t *data)
360 for (i = 0; i<length; i++)
361 printf("%02x ", data[i]);
367 main(int argc, char **argv)
370 uint8_t buffer[BUF_SIZE];
371 uint8_t digest[SHA1_DIGEST_SIZE];
376 int done = fread(buffer, 1, sizeof(buffer), stdin);
377 sha1_update(&ctx, done, buffer);
378 if (done < sizeof(buffer))
384 sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
386 display_hex(SHA1_DIGEST_SIZE, digest);
391 <p>On a typical Unix system, this program can be compiled and linked with
393 <pre class="example"> cc sha-example.c -o sha-example -lnettle
398 Node: <a name="Linking">Linking</a>,
399 Next: <a rel="next" accesskey="n" href="#Reference">Reference</a>,
400 Previous: <a rel="previous" accesskey="p" href="#Example">Example</a>,
401 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
405 <h2 class="chapter">Linking</h2>
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
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.
423 Node: <a name="Reference">Reference</a>,
424 Next: <a rel="next" accesskey="n" href="#Nettle%20soup">Nettle soup</a>,
425 Previous: <a rel="previous" accesskey="p" href="#Linking">Linking</a>,
426 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
430 <h2 class="chapter">Reference</h2>
432 <p>This chapter describes all the Nettle functions, grouped by family.
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>:
447 Node: <a name="Hash%20functions">Hash functions</a>,
448 Next: <a rel="next" accesskey="n" href="#Cipher%20functions">Cipher functions</a>,
449 Previous: <a rel="previous" accesskey="p" href="#Reference">Reference</a>,
450 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
454 <h3 class="section">Hash functions</h3>
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:
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.
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>.
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.
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
486 <h3 class="subsection"><small>MD5</small></h4>
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><nettle/md5.h></code>.
495 <td align="left"><b>struct md5_ctx</b><i>
497 <td align="right">Context struct</td>
500 <table width="95%" align="center">
508 <td align="left"><b>MD5_DIGEST_SIZE</b><i>
510 <td align="right">Constant</td>
513 <table width="95%" align="center">
515 The size of an MD5 digest, i.e. 16.
522 <td align="left"><b>MD5_DATA_SIZE</b><i>
524 <td align="right">Constant</td>
527 <table width="95%" align="center">
529 The internal block size of MD5. Useful for some special constructions,
530 in particular HMAC-MD5.
537 <td align="left">void <b>md5_init</b><i> </i>(<i>struct md5_ctx *</i><var>ctx</var><i></i>)<i>
539 <td align="right">Function</td>
542 <table width="95%" align="center">
544 Initialize the MD5 state.
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>
553 <td align="right">Function</td>
556 <table width="95%" align="center">
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>
567 <td align="right">Function</td>
570 <table width="95%" align="center">
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.
577 <p>This function also resets the context in the same way as
578 <code>md5_init</code>.
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
588 <p>To start over, you can call <code>md5_init</code> at any time.
590 <h3 class="subsection"><small>MD2</small></h4>
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><nettle/md2.h></code>.
599 <td align="left"><b>struct md2_ctx</b><i>
601 <td align="right">Context struct</td>
604 <table width="95%" align="center">
612 <td align="left"><b>MD2_DIGEST_SIZE</b><i>
614 <td align="right">Constant</td>
617 <table width="95%" align="center">
619 The size of an MD2 digest, i.e. 16.
626 <td align="left"><b>MD2_DATA_SIZE</b><i>
628 <td align="right">Constant</td>
631 <table width="95%" align="center">
633 The internal block size of MD2.
640 <td align="left">void <b>md2_init</b><i> </i>(<i>struct md2_ctx *</i><var>ctx</var><i></i>)<i>
642 <td align="right">Function</td>
645 <table width="95%" align="center">
647 Initialize the MD2 state.
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>
656 <td align="right">Function</td>
659 <table width="95%" align="center">
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>
670 <td align="right">Function</td>
673 <table width="95%" align="center">
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.
680 <p>This function also resets the context in the same way as
681 <code>md2_init</code>.
685 <h3 class="subsection"><small>MD4</small></h4>
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><nettle/md4.h></code>. Use of MD4 is
690 not recommended, but it is sometimes needed for compatibility with
691 existing applications and protocols.
696 <td align="left"><b>struct md4_ctx</b><i>
698 <td align="right">Context struct</td>
701 <table width="95%" align="center">
709 <td align="left"><b>MD4_DIGEST_SIZE</b><i>
711 <td align="right">Constant</td>
714 <table width="95%" align="center">
716 The size of an MD4 digest, i.e. 16.
723 <td align="left"><b>MD4_DATA_SIZE</b><i>
725 <td align="right">Constant</td>
728 <table width="95%" align="center">
730 The internal block size of MD4.
737 <td align="left">void <b>md4_init</b><i> </i>(<i>struct md4_ctx *</i><var>ctx</var><i></i>)<i>
739 <td align="right">Function</td>
742 <table width="95%" align="center">
744 Initialize the MD4 state.
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>
753 <td align="right">Function</td>
756 <table width="95%" align="center">
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>
767 <td align="right">Function</td>
770 <table width="95%" align="center">
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.
777 <p>This function also resets the context in the same way as
778 <code>md4_init</code>.
782 <h3 class="subsection"><small>SHA1</small></h4>
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><nettle/sha.h></code>.
788 <p>The functions are analogous to the MD5 ones.
793 <td align="left"><b>struct sha1_ctx</b><i>
795 <td align="right">Context struct</td>
798 <table width="95%" align="center">
806 <td align="left"><b>SHA1_DIGEST_SIZE</b><i>
808 <td align="right">Constant</td>
811 <table width="95%" align="center">
813 The size of an SHA1 digest, i.e. 20.
820 <td align="left"><b>SHA1_DATA_SIZE</b><i>
822 <td align="right">Constant</td>
825 <table width="95%" align="center">
827 The internal block size of SHA1. Useful for some special constructions,
828 in particular HMAC-SHA1.
835 <td align="left">void <b>sha1_init</b><i> </i>(<i>struct sha1_ctx *</i><var>ctx</var><i></i>)<i>
837 <td align="right">Function</td>
840 <table width="95%" align="center">
842 Initialize the SHA1 state.
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>
851 <td align="right">Function</td>
854 <table width="95%" align="center">
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>
865 <td align="right">Function</td>
868 <table width="95%" align="center">
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.
875 <p>This function also resets the context in the same way as
876 <code>sha1_init</code>.
880 <h3 class="subsection"><small>SHA256</small></h4>
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><nettle/sha.h></code>.
887 <p>The functions are analogous to the MD5 ones.
892 <td align="left"><b>struct sha256_ctx</b><i>
894 <td align="right">Context struct</td>
897 <table width="95%" align="center">
905 <td align="left"><b>SHA256_DIGEST_SIZE</b><i>
907 <td align="right">Constant</td>
910 <table width="95%" align="center">
912 The size of an SHA256 digest, i.e. 32.
919 <td align="left"><b>SHA256_DATA_SIZE</b><i>
921 <td align="right">Constant</td>
924 <table width="95%" align="center">
926 The internal block size of SHA256. Useful for some special constructions,
927 in particular HMAC-SHA256.
934 <td align="left">void <b>sha256_init</b><i> </i>(<i>struct sha256_ctx *</i><var>ctx</var><i></i>)<i>
936 <td align="right">Function</td>
939 <table width="95%" align="center">
941 Initialize the SHA256 state.
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>
950 <td align="right">Function</td>
953 <table width="95%" align="center">
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>
964 <td align="right">Function</td>
967 <table width="95%" align="center">
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.
974 <p>This function also resets the context in the same way as
975 <code>sha256_init</code>.
979 <h3 class="subsection"><small>SHA224</small></h4>
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><nettle/sha.h></code>.
985 <p>The functions are analogous to the MD5 ones.
990 <td align="left"><b>struct sha224_ctx</b><i>
992 <td align="right">Context struct</td>
995 <table width="95%" align="center">
1001 <table width="100%">
1003 <td align="left"><b>SHA224_DIGEST_SIZE</b><i>
1005 <td align="right">Constant</td>
1008 <table width="95%" align="center">
1010 The size of an SHA224 digest, i.e. 28.
1015 <table width="100%">
1017 <td align="left"><b>SHA224_DATA_SIZE</b><i>
1019 <td align="right">Constant</td>
1022 <table width="95%" align="center">
1024 The internal block size of SHA224. Useful for some special constructions,
1025 in particular HMAC-SHA224.
1030 <table width="100%">
1032 <td align="left">void <b>sha224_init</b><i> </i>(<i>struct sha224_ctx *</i><var>ctx</var><i></i>)<i>
1034 <td align="right">Function</td>
1037 <table width="95%" align="center">
1039 Initialize the SHA224 state.
1044 <table width="100%">
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>
1048 <td align="right">Function</td>
1051 <table width="95%" align="center">
1053 Hash some more data.
1058 <table width="100%">
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>
1062 <td align="right">Function</td>
1065 <table width="95%" align="center">
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.
1072 <p>This function also resets the context in the same way as
1073 <code>sha224_init</code>.
1077 <h3 class="subsection"><small>SHA512</small></h4>
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><nettle/sha.h></code>.
1085 <p>The functions are analogous to the MD5 ones.
1088 <table width="100%">
1090 <td align="left"><b>struct sha512_ctx</b><i>
1092 <td align="right">Context struct</td>
1095 <table width="95%" align="center">
1101 <table width="100%">
1103 <td align="left"><b>SHA512_DIGEST_SIZE</b><i>
1105 <td align="right">Constant</td>
1108 <table width="95%" align="center">
1110 The size of an SHA512 digest, i.e. 64.
1115 <table width="100%">
1117 <td align="left"><b>SHA512_DATA_SIZE</b><i>
1119 <td align="right">Constant</td>
1122 <table width="95%" align="center">
1124 The internal block size of SHA512. Useful for some special constructions,
1125 in particular HMAC-SHA512.
1130 <table width="100%">
1132 <td align="left">void <b>sha512_init</b><i> </i>(<i>struct sha512_ctx *</i><var>ctx</var><i></i>)<i>
1134 <td align="right">Function</td>
1137 <table width="95%" align="center">
1139 Initialize the SHA512 state.
1144 <table width="100%">
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>
1148 <td align="right">Function</td>
1151 <table width="95%" align="center">
1153 Hash some more data.
1158 <table width="100%">
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>
1162 <td align="right">Function</td>
1165 <table width="95%" align="center">
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.
1172 <p>This function also resets the context in the same way as
1173 <code>sha512_init</code>.
1177 <h3 class="subsection"><small>SHA384</small></h4>
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><nettle/sha.h></code>.
1183 <p>The functions are analogous to the MD5 ones.
1186 <table width="100%">
1188 <td align="left"><b>struct sha384_ctx</b><i>
1190 <td align="right">Context struct</td>
1193 <table width="95%" align="center">
1199 <table width="100%">
1201 <td align="left"><b>SHA384_DIGEST_SIZE</b><i>
1203 <td align="right">Constant</td>
1206 <table width="95%" align="center">
1208 The size of an SHA384 digest, i.e. 48.
1213 <table width="100%">
1215 <td align="left"><b>SHA384_DATA_SIZE</b><i>
1217 <td align="right">Constant</td>
1220 <table width="95%" align="center">
1222 The internal block size of SHA384. Useful for some special constructions,
1223 in particular HMAC-SHA384.
1228 <table width="100%">
1230 <td align="left">void <b>sha384_init</b><i> </i>(<i>struct sha384_ctx *</i><var>ctx</var><i></i>)<i>
1232 <td align="right">Function</td>
1235 <table width="95%" align="center">
1237 Initialize the SHA384 state.
1242 <table width="100%">
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>
1246 <td align="right">Function</td>
1249 <table width="95%" align="center">
1251 Hash some more data.
1256 <table width="100%">
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>
1260 <td align="right">Function</td>
1263 <table width="95%" align="center">
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.
1270 <p>This function also resets the context in the same way as
1271 <code>sha384_init</code>.
1275 <h3 class="subsection"><code>struct nettle_hash</code></h4>
1277 <p>Nettle includes a struct including information about the supported hash
1278 functions. It is defined in <code><nettle/nettle-meta.h></code>, and is used
1279 by Nettle's implementation of <small>HMAC</small> see <a href="#Keyed%20hash%20functions">Keyed hash functions</a>.
1282 <table width="100%">
1284 <td align="left"><b></b><code>struct nettle_hash</code><b></b><i> name context_size digest_size block_size init update digest
1286 <td align="right">Meta struct</td>
1289 <table width="95%" align="center">
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>.
1300 <table width="100%">
1302 <td align="left">struct nettle_hash <b>nettle_md2</b><i>
1304 <td align="right">Constant Struct</td>
1308 <td align="left">struct nettle_hash <b>nettle_md4</b><i>
1310 <td align="right">Constant Struct</td>
1314 <td align="left">struct nettle_hash <b>nettle_md5</b><i>
1316 <td align="right">Constant Struct</td>
1320 <td align="left">struct nettle_hash <b>nettle_sha1</b><i>
1322 <td align="right">Constant Struct</td>
1326 <td align="left">struct nettle_hash <b>nettle_sha224</b><i>
1328 <td align="right">Constant Struct</td>
1332 <td align="left">struct nettle_hash <b>nettle_sha256</b><i>
1334 <td align="right">Constant Struct</td>
1338 <td align="left">struct nettle_hash <b>nettle_sha384</b><i>
1340 <td align="right">Constant Struct</td>
1344 <td align="left">struct nettle_hash <b>nettle_sha512</b><i>
1346 <td align="right">Constant Struct</td>
1349 <table width="95%" align="center">
1352 <p>These are all the hash functions that Nettle implements.
1358 Node: <a name="Cipher%20functions">Cipher functions</a>,
1359 Next: <a rel="next" accesskey="n" href="#Cipher%20modes">Cipher modes</a>,
1360 Previous: <a rel="previous" accesskey="p" href="#Hash%20functions">Hash functions</a>,
1361 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
1365 <h3 class="section">Cipher functions</h3>
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.
1373 <p>There are two main classes of ciphers: Block ciphers and stream ciphers.
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.
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.
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.
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.
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>.
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
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
1434 <h3 class="subsection">AES</h4>
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.
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><nettle/aes.h></code>.
1447 <table width="100%">
1449 <td align="left"><b>struct aes_ctx</b><i>
1451 <td align="right">Context struct</td>
1454 <table width="95%" align="center">
1460 <table width="100%">
1462 <td align="left"><b>AES_BLOCK_SIZE</b><i>
1464 <td align="right">Constant</td>
1467 <table width="95%" align="center">
1469 The AES block-size, 16
1474 <table width="100%">
1476 <td align="left"><b>AES_MIN_KEY_SIZE</b><i>
1478 <td align="right">Constant</td>
1481 <table width="95%" align="center">
1487 <table width="100%">
1489 <td align="left"><b>AES_MAX_KEY_SIZE</b><i>
1491 <td align="right">Constant</td>
1494 <table width="95%" align="center">
1500 <table width="100%">
1502 <td align="left"><b>AES_KEY_SIZE</b><i>
1504 <td align="right">Constant</td>
1507 <table width="95%" align="center">
1509 Default AES key size, 32
1514 <table width="100%">
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>
1518 <td align="right">Function</td>
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>
1524 <td align="right">Function</td>
1527 <table width="95%" align="center">
1529 Initialize the cipher, for encryption or decryption, respectively.
1534 <table width="100%">
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>
1538 <td align="right">Function</td>
1541 <table width="95%" align="center">
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.
1555 <table width="100%">
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>
1559 <td align="right">Function</td>
1562 <table width="95%" align="center">
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
1572 <table width="100%">
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>
1576 <td align="right">Function</td>
1579 <table width="95%" align="center">
1581 Analogous to <code>aes_encrypt</code>
1585 <h3 class="subsection">ARCFOUR</h4>
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.
1597 <pre class="example"> /* A more robust key setup function for ARCFOUR */
1599 arcfour_set_key_hashed(struct arcfour_ctx *ctx,
1600 unsigned length, const uint8_t *key)
1602 struct sha256_ctx hash;
1603 uint8_t digest[SHA256_DIGEST_SIZE];
1604 uint8_t buffer[0x200];
1606 sha256_init(&hash);
1607 sha256_update(&hash, length, key);
1608 sha256_digest(&hash, SHA256_DIGEST_SIZE, digest);
1610 arcfour_set_key(ctx, SHA256_DIGEST_SIZE, digest);
1611 arcfour_crypt(ctx, sizeof(buffer), buffer, buffer);
1615 <p>Nettle defines ARCFOUR in <code><nettle/arcfour.h></code>.
1618 <table width="100%">
1620 <td align="left"><b>struct arcfour_ctx</b><i>
1622 <td align="right">Context struct</td>
1625 <table width="95%" align="center">
1631 <table width="100%">
1633 <td align="left"><b>ARCFOUR_MIN_KEY_SIZE</b><i>
1635 <td align="right">Constant</td>
1638 <table width="95%" align="center">
1645 <table width="100%">
1647 <td align="left"><b>ARCFOUR_MAX_KEY_SIZE</b><i>
1649 <td align="right">Constant</td>
1652 <table width="95%" align="center">
1654 Maximum key size, 256
1659 <table width="100%">
1661 <td align="left"><b>ARCFOUR_KEY_SIZE</b><i>
1663 <td align="right">Constant</td>
1666 <table width="95%" align="center">
1668 Default ARCFOUR key size, 16
1673 <table width="100%">
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>
1677 <td align="right">Function</td>
1680 <table width="95%" align="center">
1682 Initialize the cipher. The same function is used for both encryption and
1688 <table width="100%">
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>
1692 <td align="right">Function</td>
1695 <table width="95%" align="center">
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.
1705 <h3 class="subsection">ARCTWO</h4>
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.
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><nettle/arctwo.h></code>.
1720 <p>We do not recommend the use of ARCTWO; the Nettle implementation is
1721 provided primarily for interoperability with existing applications and
1725 <table width="100%">
1727 <td align="left"><b>struct arctwo_ctx</b><i>
1729 <td align="right">Context struct</td>
1732 <table width="95%" align="center">
1738 <table width="100%">
1740 <td align="left"><b>ARCTWO_BLOCK_SIZE</b><i>
1742 <td align="right">Constant</td>
1745 <table width="95%" align="center">
1747 The AES block-size, 8
1752 <table width="100%">
1754 <td align="left"><b>ARCTWO_MIN_KEY_SIZE</b><i>
1756 <td align="right">Constant</td>
1759 <table width="95%" align="center">
1765 <table width="100%">
1767 <td align="left"><b>ARCTWO_MAX_KEY_SIZE</b><i>
1769 <td align="right">Constant</td>
1772 <table width="95%" align="center">
1778 <table width="100%">
1780 <td align="left"><b>ARCTWO_KEY_SIZE</b><i>
1782 <td align="right">Constant</td>
1785 <table width="95%" align="center">
1787 Default ARCTWO key size, 8
1792 <table width="100%">
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>
1796 <td align="right">Function</td>
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>
1802 <td align="right">Function</td>
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>
1808 <td align="right">Function</td>
1811 <table width="95%" align="center">
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>.
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>
1827 <table width="100%">
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>
1831 <td align="right">Function</td>
1834 <table width="95%" align="center">
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.
1844 <table width="100%">
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>
1848 <td align="right">Function</td>
1851 <table width="95%" align="center">
1853 Analogous to <code>arctwo_encrypt</code>
1857 <h3 class="subsection">BLOWFISH</h4>
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><nettle/blowfish.h></code>.
1864 <table width="100%">
1866 <td align="left"><b>struct blowfish_ctx</b><i>
1868 <td align="right">Context struct</td>
1871 <table width="95%" align="center">
1877 <table width="100%">
1879 <td align="left"><b>BLOWFISH_BLOCK_SIZE</b><i>
1881 <td align="right">Constant</td>
1884 <table width="95%" align="center">
1886 The BLOWFISH block-size, 8
1891 <table width="100%">
1893 <td align="left"><b>BLOWFISH_MIN_KEY_SIZE</b><i>
1895 <td align="right">Constant</td>
1898 <table width="95%" align="center">
1900 Minimum BLOWFISH key size, 8
1905 <table width="100%">
1907 <td align="left"><b>BLOWFISH_MAX_KEY_SIZE</b><i>
1909 <td align="right">Constant</td>
1912 <table width="95%" align="center">
1914 Maximum BLOWFISH key size, 56
1919 <table width="100%">
1921 <td align="left"><b>BLOWFISH_KEY_SIZE</b><i>
1923 <td align="right">Constant</td>
1926 <table width="95%" align="center">
1928 Default BLOWFISH key size, 16
1933 <table width="100%">
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>
1937 <td align="right">Function</td>
1940 <table width="95%" align="center">
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.
1947 <p><code>blowfish_encrypt</code> or <code>blowfish_decrypt</code> with a weak key will
1948 crash with an assert violation.
1953 <table width="100%">
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>
1957 <td align="right">Function</td>
1960 <table width="95%" align="center">
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
1970 <table width="100%">
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>
1974 <td align="right">Function</td>
1977 <table width="95%" align="center">
1979 Analogous to <code>blowfish_encrypt</code>
1983 <h3 class="subsection">Camellia</h4>
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 <<code>http://www.ntt.co.jp/news/news01e/0104/010417.html</code>>, but this
1993 statement has some limitations which seem problematic for free software.
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><nettle/camellia.h></code>.
2000 <table width="100%">
2002 <td align="left"><b>struct camellia_ctx</b><i>
2004 <td align="right">Context struct</td>
2007 <table width="95%" align="center">
2013 <table width="100%">
2015 <td align="left"><b>CAMELLIA_BLOCK_SIZE</b><i>
2017 <td align="right">Constant</td>
2020 <table width="95%" align="center">
2022 The CAMELLIA block-size, 16
2027 <table width="100%">
2029 <td align="left"><b>CAMELLIA_MIN_KEY_SIZE</b><i>
2031 <td align="right">Constant</td>
2034 <table width="95%" align="center">
2040 <table width="100%">
2042 <td align="left"><b>CAMELLIA_MAX_KEY_SIZE</b><i>
2044 <td align="right">Constant</td>
2047 <table width="95%" align="center">
2053 <table width="100%">
2055 <td align="left"><b>CAMELLIA_KEY_SIZE</b><i>
2057 <td align="right">Constant</td>
2060 <table width="95%" align="center">
2062 Default CAMELLIA key size, 32
2067 <table width="100%">
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>
2071 <td align="right">Function</td>
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>
2077 <td align="right">Function</td>
2080 <table width="95%" align="center">
2082 Initialize the cipher, for encryption or decryption, respectively.
2087 <table width="100%">
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>
2091 <td align="right">Function</td>
2094 <table width="95%" align="center">
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.
2108 <table width="100%">
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>
2112 <td align="right">Function</td>
2115 <table width="95%" align="center">
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.
2124 <h3 class="subsection">CAST128</h4>
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><nettle/cast128.h></code>.
2131 <table width="100%">
2133 <td align="left"><b>struct cast128_ctx</b><i>
2135 <td align="right">Context struct</td>
2138 <table width="95%" align="center">
2144 <table width="100%">
2146 <td align="left"><b>CAST128_BLOCK_SIZE</b><i>
2148 <td align="right">Constant</td>
2151 <table width="95%" align="center">
2153 The CAST128 block-size, 8
2158 <table width="100%">
2160 <td align="left"><b>CAST128_MIN_KEY_SIZE</b><i>
2162 <td align="right">Constant</td>
2165 <table width="95%" align="center">
2167 Minimum CAST128 key size, 5
2172 <table width="100%">
2174 <td align="left"><b>CAST128_MAX_KEY_SIZE</b><i>
2176 <td align="right">Constant</td>
2179 <table width="95%" align="center">
2181 Maximum CAST128 key size, 16
2186 <table width="100%">
2188 <td align="left"><b>CAST128_KEY_SIZE</b><i>
2190 <td align="right">Constant</td>
2193 <table width="95%" align="center">
2195 Default CAST128 key size, 16
2200 <table width="100%">
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>
2204 <td align="right">Function</td>
2207 <table width="95%" align="center">
2209 Initialize the cipher. The same function is used for both encryption and
2215 <table width="100%">
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>
2219 <td align="right">Function</td>
2222 <table width="95%" align="center">
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
2232 <table width="100%">
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>
2236 <td align="right">Function</td>
2239 <table width="95%" align="center">
2241 Analogous to <code>cast128_encrypt</code>
2245 <h3 class="subsection">DES</h4>
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
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.
2260 <p>DES also has some weak keys. Nettle defines DES in <code><nettle/des.h></code>.
2263 <table width="100%">
2265 <td align="left"><b>struct des_ctx</b><i>
2267 <td align="right">Context struct</td>
2270 <table width="95%" align="center">
2276 <table width="100%">
2278 <td align="left"><b>DES_BLOCK_SIZE</b><i>
2280 <td align="right">Constant</td>
2283 <table width="95%" align="center">
2285 The DES block-size, 8
2290 <table width="100%">
2292 <td align="left"><b>DES_KEY_SIZE</b><i>
2294 <td align="right">Constant</td>
2297 <table width="95%" align="center">
2304 <table width="100%">
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>
2308 <td align="right">Function</td>
2311 <table width="95%" align="center">
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.
2321 <table width="100%">
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>
2325 <td align="right">Function</td>
2328 <table width="95%" align="center">
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
2338 <table width="100%">
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>
2342 <td align="right">Function</td>
2345 <table width="95%" align="center">
2347 Analogous to <code>des_encrypt</code>
2352 <table width="100%">
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>;
2356 <td align="right">Function</td>
2359 <table width="95%" align="center">
2361 Checks that the given key has correct, odd, parity. Returns 1 for
2362 correct parity, and 0 for bad parity.
2367 <table width="100%">
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>
2371 <td align="right">Function</td>
2374 <table width="95%" align="center">
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
2383 <h3 class="subsection">DES3</h4>
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.
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".
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.
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.
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><nettle/des.h></code>.
2419 <table width="100%">
2421 <td align="left"><b>struct des3_ctx</b><i>
2423 <td align="right">Context struct</td>
2426 <table width="95%" align="center">
2432 <table width="100%">
2434 <td align="left"><b>DES3_BLOCK_SIZE</b><i>
2436 <td align="right">Constant</td>
2439 <table width="95%" align="center">
2441 The DES3 block-size is the same as DES_BLOCK_SIZE, 8
2446 <table width="100%">
2448 <td align="left"><b>DES3_KEY_SIZE</b><i>
2450 <td align="right">Constant</td>
2453 <table width="95%" align="center">
2460 <table width="100%">
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>
2464 <td align="right">Function</td>
2467 <table width="95%" align="center">
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
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>.
2481 <table width="100%">
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>
2485 <td align="right">Function</td>
2488 <table width="95%" align="center">
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
2498 <table width="100%">
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>
2502 <td align="right">Function</td>
2505 <table width="95%" align="center">
2507 Analogous to <code>des_encrypt</code>
2511 <h3 class="subsection">SERPENT</h4>
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><nettle/serpent.h></code>.
2520 <table width="100%">
2522 <td align="left"><b>struct serpent_ctx</b><i>
2524 <td align="right">Context struct</td>
2527 <table width="95%" align="center">
2533 <table width="100%">
2535 <td align="left"><b>SERPENT_BLOCK_SIZE</b><i>
2537 <td align="right">Constant</td>
2540 <table width="95%" align="center">
2542 The SERPENT block-size, 16
2547 <table width="100%">
2549 <td align="left"><b>SERPENT_MIN_KEY_SIZE</b><i>
2551 <td align="right">Constant</td>
2554 <table width="95%" align="center">
2556 Minimum SERPENT key size, 16
2561 <table width="100%">
2563 <td align="left"><b>SERPENT_MAX_KEY_SIZE</b><i>
2565 <td align="right">Constant</td>
2568 <table width="95%" align="center">
2570 Maximum SERPENT key size, 32
2575 <table width="100%">
2577 <td align="left"><b>SERPENT_KEY_SIZE</b><i>
2579 <td align="right">Constant</td>
2582 <table width="95%" align="center">
2584 Default SERPENT key size, 32
2589 <table width="100%">
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>
2593 <td align="right">Function</td>
2596 <table width="95%" align="center">
2598 Initialize the cipher. The same function is used for both encryption and
2604 <table width="100%">
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>
2608 <td align="right">Function</td>
2611 <table width="95%" align="center">
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
2621 <table width="100%">
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>
2625 <td align="right">Function</td>
2628 <table width="95%" align="center">
2630 Analogous to <code>serpent_encrypt</code>
2634 <h3 class="subsection">TWOFISH</h4>
2636 <p>Another AES finalist, this one designed by Bruce Schneier and others.
2637 Nettle defines it in <code><nettle/twofish.h></code>.
2640 <table width="100%">
2642 <td align="left"><b>struct twofish_ctx</b><i>
2644 <td align="right">Context struct</td>
2647 <table width="95%" align="center">
2653 <table width="100%">
2655 <td align="left"><b>TWOFISH_BLOCK_SIZE</b><i>
2657 <td align="right">Constant</td>
2660 <table width="95%" align="center">
2662 The TWOFISH block-size, 16
2667 <table width="100%">
2669 <td align="left"><b>TWOFISH_MIN_KEY_SIZE</b><i>
2671 <td align="right">Constant</td>
2674 <table width="95%" align="center">
2676 Minimum TWOFISH key size, 16
2681 <table width="100%">
2683 <td align="left"><b>TWOFISH_MAX_KEY_SIZE</b><i>
2685 <td align="right">Constant</td>
2688 <table width="95%" align="center">
2690 Maximum TWOFISH key size, 32
2695 <table width="100%">
2697 <td align="left"><b>TWOFISH_KEY_SIZE</b><i>
2699 <td align="right">Constant</td>
2702 <table width="95%" align="center">
2704 Default TWOFISH key size, 32
2709 <table width="100%">
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>
2713 <td align="right">Function</td>
2716 <table width="95%" align="center">
2718 Initialize the cipher. The same function is used for both encryption and
2724 <table width="100%">
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>
2728 <td align="right">Function</td>
2731 <table width="95%" align="center">
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
2741 <table width="100%">
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>
2745 <td align="right">Function</td>
2748 <table width="95%" align="center">
2750 Analogous to <code>twofish_encrypt</code>
2754 <h3 class="subsection"><code>struct nettle_cipher</code></h4>
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><nettle/nettle-meta.h></code>.
2763 <table width="100%">
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
2767 <td align="right">Meta struct</td>
2770 <table width="95%" align="center">
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>.
2780 <table width="100%">
2782 <td align="left">struct nettle_cipher <b>nettle_aes128</b><i>
2784 <td align="right">Constant Struct</td>
2788 <td align="left">struct nettle_cipher <b>nettle_aes192</b><i>
2790 <td align="right">Constant Struct</td>
2794 <td align="left">struct nettle_cipher <b>nettle_aes256</b><i>
2796 <td align="right">Constant Struct</td>
2799 <table width="95%" align="center">
2803 <td align="left">struct nettle_cipher <b>nettle_arctwo40;</b><i>
2805 <td align="right">Constant Struct</td>
2809 <td align="left">struct nettle_cipher <b>nettle_arctwo64;</b><i>
2811 <td align="right">Constant Struct</td>
2815 <td align="left">struct nettle_cipher <b>nettle_arctwo128;</b><i>
2817 <td align="right">Constant Struct</td>
2821 <td align="left">struct nettle_cipher <b>nettle_arctwo_gutmann128;</b><i>
2823 <td align="right">Constant Struct</td>
2826 <table width="95%" align="center">
2830 <td align="left">struct nettle_cipher <b>nettle_arcfour128</b><i>
2832 <td align="right">Constant Struct</td>
2835 <table width="95%" align="center">
2839 <td align="left">struct nettle_cipher <b>nettle_camellia128</b><i>
2841 <td align="right">Constant Struct</td>
2845 <td align="left">struct nettle_cipher <b>nettle_camellia192</b><i>
2847 <td align="right">Constant Struct</td>
2851 <td align="left">struct nettle_cipher <b>nettle_camellia256</b><i>
2853 <td align="right">Constant Struct</td>
2856 <table width="95%" align="center">
2860 <td align="left">struct nettle_cipher <b>nettle_cast128</b><i>
2862 <td align="right">Constant Struct</td>
2865 <table width="95%" align="center">
2869 <td align="left">struct nettle_cipher <b>nettle_serpent128</b><i>
2871 <td align="right">Constant Struct</td>
2875 <td align="left">struct nettle_cipher <b>nettle_serpent192</b><i>
2877 <td align="right">Constant Struct</td>
2881 <td align="left">struct nettle_cipher <b>nettle_serpent256</b><i>
2883 <td align="right">Constant Struct</td>
2886 <table width="95%" align="center">
2890 <td align="left">struct nettle_cipher <b>nettle_twofish128</b><i>
2892 <td align="right">Constant Struct</td>
2896 <td align="left">struct nettle_cipher <b>nettle_twofish192</b><i>
2898 <td align="right">Constant Struct</td>
2902 <td align="left">struct nettle_cipher <b>nettle_twofish256</b><i>
2904 <td align="right">Constant Struct</td>
2907 <table width="95%" align="center">
2911 <td align="left">struct nettle_cipher <b>nettle_arctwo40;</b><i>
2913 <td align="right">Constant Struct</td>
2917 <td align="left">struct nettle_cipher <b>nettle_arctwo64;</b><i>
2919 <td align="right">Constant Struct</td>
2923 <td align="left">struct nettle_cipher <b>nettle_arctwo128;</b><i>
2925 <td align="right">Constant Struct</td>
2929 <td align="left">struct nettle_cipher <b>nettle_arctwo_gutmann128;</b><i>
2931 <td align="right">Constant Struct</td>
2934 <table width="95%" align="center">
2937 <p>Nettle includes such structs for all the <em>regular</em> ciphers, i.e.
2938 ones without weak keys or other oddities.
2944 Node: <a name="Cipher%20modes">Cipher modes</a>,
2945 Next: <a rel="next" accesskey="n" href="#Keyed%20hash%20functions">Keyed hash functions</a>,
2946 Previous: <a rel="previous" accesskey="p" href="#Cipher%20functions">Cipher functions</a>,
2947 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
2951 <h3 class="section">Cipher modes</h3>
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
2964 <h3 class="subsection">Cipher Block Chaining</h4>
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.
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:
2980 <pre class="example"> C_1 = E_k(IV XOR M_1)
2981 C_2 = E_k(C_1 XOR M_2)
2985 C_n = E_k(C_(n-1) XOR M_n)
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
2994 <table width="100%">
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>
2998 <td align="right">Function</td>
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>
3004 <td align="right">Function</td>
3007 <table width="95%" align="center">
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
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>,
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>.
3023 <p>There are also some macros to help use these functions correctly.
3026 <table width="100%">
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>
3030 <td align="right">Macro</td>
3033 <table width="95%" align="center">
3036 <pre class="example"> {
3038 uint8_t iv[block_size];
3044 <p>It can be used to define a <small>CBC</small> context struct, either directly,
3046 <pre class="example"> struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) ctx;
3049 <p>or to give it a struct tag,
3051 <pre class="example"> struct aes_cbc_ctx CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE);
3055 <table width="100%">
3057 <td align="left"><b>CBC_SET_IV</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>iv</var><i></i>)<i>
3059 <td align="right">Macro</td>
3062 <table width="95%" align="center">
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.
3071 <table width="100%">
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>
3075 <td align="right">Macro</td>
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>
3081 <td align="right">Macro</td>
3084 <table width="95%" align="center">
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.
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.
3098 <h3 class="subsection">Counter mode</h4>
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.
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.
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:
3115 <pre class="example"> C_1 = E_k(IC) XOR M_1
3116 C_2 = E_k(IC + 1) XOR M_2
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
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.
3131 <table width="100%">
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>
3135 <td align="right">Function</td>
3138 <table width="95%" align="center">
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.
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.
3152 <p>Like for <small>CBC</small>, there are also a couple of helper macros.
3155 <table width="100%">
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>
3159 <td align="right">Macro</td>
3162 <table width="95%" align="center">
3165 <pre class="example"> {
3167 uint8_t ctr[block_size];
3174 <table width="100%">
3176 <td align="left"><b>CTR_SET_COUNTER</b><i> </i>(<i></i><var>ctx</var><i>, </i><var>iv</var><i></i>)<i>
3178 <td align="right">Macro</td>
3181 <table width="95%" align="center">
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.
3190 <table width="100%">
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>
3194 <td align="right">Macro</td>
3197 <table width="95%" align="center">
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
3209 Node: <a name="Keyed%20hash%20functions">Keyed hash functions</a>,
3210 Next: <a rel="next" accesskey="n" href="#Public-key%20algorithms">Public-key algorithms</a>,
3211 Previous: <a rel="previous" accesskey="p" href="#Cipher%20modes">Cipher modes</a>,
3212 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
3216 <h3 class="section">Keyed Hash Functions</h3>
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.
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.
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
3243 <p>Keyed hash functions are typically a lot faster than digital signatures
3246 <h3 class="subsection"><small>HMAC</small></h4>
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>.
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.
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
3266 <p>Nettle's <small>HMAC</small> functions are defined in <code><nettle/hmac.h></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:
3275 <table width="100%">
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>
3279 <td align="right">Function</td>
3282 <table width="95%" align="center">
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.
3292 <table width="100%">
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>
3296 <td align="right">Function</td>
3299 <table width="95%" align="center">
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->update(state, length, data)</code>, so if you wish you can use the
3304 ordinary update function of the underlying hash function instead.
3309 <table width="100%">
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>
3313 <td align="right">Function</td>
3316 <table width="95%" align="center">
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->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.
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).
3328 <p>Like for <small>CBC</small>, there are some macros to help use these
3329 functions correctly.
3332 <table width="100%">
3334 <td align="left"><b>HMAC_CTX</b><i> </i>(<i></i><var>type</var><i></i>)<i>
3336 <td align="right">Macro</td>
3339 <table width="95%" align="center">
3342 <pre class="example"> {
3351 <p>It can be used to define a <small>HMAC</small> context struct, either
3354 <pre class="example"> struct HMAC_CTX(struct md5_ctx) ctx;
3357 <p>or to give it a struct tag,
3359 <pre class="example"> struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
3363 <table width="100%">
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>
3367 <td align="right">Macro</td>
3370 <table width="95%" align="center">
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
3381 <table width="100%">
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>
3385 <td align="right">Macro</td>
3388 <table width="95%" align="center">
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.
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.
3401 <h3 class="subsection">Concrete <small>HMAC</small> functions</h4>
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.
3406 <h3 class="subsubsection"><small>HMAC-MD5</small></h5>
3409 <table width="100%">
3411 <td align="left"><b>struct hmac_md5_ctx</b><i>
3413 <td align="right">Context struct</td>
3416 <table width="95%" align="center">
3422 <table width="100%">
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>
3426 <td align="right">Function</td>
3429 <table width="95%" align="center">
3431 Initializes the context with the key.
3436 <table width="100%">
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>
3440 <td align="right">Function</td>
3443 <table width="95%" align="center">
3445 Process some more data.
3450 <table width="100%">
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>
3454 <td align="right">Function</td>
3457 <table width="95%" align="center">
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.
3463 <p>This function also resets the context for processing new messages, with
3468 <h3 class="subsubsection"><small>HMAC-SHA1</small></h5>
3471 <table width="100%">
3473 <td align="left"><b>struct hmac_sha1_ctx</b><i>
3475 <td align="right">Context struct</td>
3478 <table width="95%" align="center">
3484 <table width="100%">
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>
3488 <td align="right">Function</td>
3491 <table width="95%" align="center">
3493 Initializes the context with the key.
3498 <table width="100%">
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>
3502 <td align="right">Function</td>
3505 <table width="95%" align="center">
3507 Process some more data.
3512 <table width="100%">
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>
3516 <td align="right">Function</td>
3519 <table width="95%" align="center">
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.
3525 <p>This function also resets the context for processing new messages, with
3530 <h3 class="subsubsection"><small>HMAC-SHA256</small></h5>
3533 <table width="100%">
3535 <td align="left"><b>struct hmac_sha256_ctx</b><i>
3537 <td align="right">Context struct</td>
3540 <table width="95%" align="center">
3546 <table width="100%">
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>
3550 <td align="right">Function</td>
3553 <table width="95%" align="center">
3555 Initializes the context with the key.
3560 <table width="100%">
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>
3564 <td align="right">Function</td>
3567 <table width="95%" align="center">
3569 Process some more data.
3574 <table width="100%">
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>
3578 <td align="right">Function</td>
3581 <table width="95%" align="center">
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.
3587 <p>This function also resets the context for processing new messages, with
3592 <h3 class="subsubsection"><small>HMAC-SHA512</small></h5>
3595 <table width="100%">
3597 <td align="left"><b>struct hmac_sha512_ctx</b><i>
3599 <td align="right">Context struct</td>
3602 <table width="95%" align="center">
3608 <table width="100%">
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>
3612 <td align="right">Function</td>
3615 <table width="95%" align="center">
3617 Initializes the context with the key.
3622 <table width="100%">
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>
3626 <td align="right">Function</td>
3629 <table width="95%" align="center">
3631 Process some more data.
3636 <table width="100%">
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>
3640 <td align="right">Function</td>
3643 <table width="95%" align="center">
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.
3649 <p>This function also resets the context for processing new messages, with
3656 Node: <a name="Public-key%20algorithms">Public-key algorithms</a>,
3657 Next: <a rel="next" accesskey="n" href="#Randomness">Randomness</a>,
3658 Previous: <a rel="previous" accesskey="p" href="#Keyed%20hash%20functions">Keyed hash functions</a>,
3659 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
3663 <h3 class="section">Public-key algorithms</h3>
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
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
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.
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.
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
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.
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.
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.
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.
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".
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.
3740 Node: <a name="RSA">RSA</a>,
3741 Next: <a rel="next" accesskey="n" href="#DSA">DSA</a>,
3742 Previous: <a rel="previous" accesskey="p" href="#Public-key%20algorithms">Public-key algorithms</a>,
3743 Up: <a rel="up" accesskey="u" href="#Public-key%20algorithms">Public-key algorithms</a>
3747 <h3 class="subsection"><small>RSA</small></h4>
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>.
3755 <p>It's remarkably simple to describe the trapdoor function behind
3756 <small>RSA</small>. The "one-way"-function used is
3758 <pre class="example"> F(x) = x^e mod n
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
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?
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.
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>.
3784 <p>Finally, a number <code>d</code>, <code>d < 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,
3788 <pre class="example"> (x^e)^d mod n = x^(ed) mod n = (x^d)^e mod n = x
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>.
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.
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.
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
3815 <h3 class="subsection">Nettle's <small>RSA</small> support</h4>
3817 <p>Nettle represents <small>RSA</small> keys using two structures that contain
3818 large numbers (of type <code>mpz_t</code>).
3821 <table width="100%">
3823 <td align="left"><b>rsa_public_key</b><i> size n e
3825 <td align="right">Context struct</td>
3828 <table width="95%" align="center">
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.
3836 <table width="100%">
3838 <td align="left"><b>rsa_private_key</b><i> size d p q a b c
3840 <td align="right">Context struct</td>
3843 <table width="95%" align="center">
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>.
3854 <p>Before use, these structs must be initialized by calling one of
3857 <table width="100%">
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>
3861 <td align="right">Function</td>
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>
3867 <td align="right">Function</td>
3870 <table width="95%" align="center">
3872 Calls <code>mpz_init</code> on all numbers in the key struct.
3876 <p>and when finished with them, the space for the numbers must be
3877 deallocated by calling one of
3880 <table width="100%">
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>
3884 <td align="right">Function</td>
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>
3890 <td align="right">Function</td>
3893 <table width="95%" align="center">
3895 Calls <code>mpz_clear</code> on all numbers in the key struct.
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>.
3905 <p>When you have assigned values to the attributes of a key, you must call
3908 <table width="100%">
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>
3912 <td align="right">Function</td>
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>
3918 <td align="right">Function</td>
3921 <table width="95%" align="center">
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.
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.
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.
3940 <p>Creation and verification of signatures is done with the following functions:
3943 <table width="100%">
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>
3947 <td align="right">Function</td>
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>
3953 <td align="right">Function</td>
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>
3959 <td align="right">Function</td>
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>
3965 <td align="right">Function</td>
3968 <table width="95%" align="center">
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.
3980 <table width="100%">
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>
3984 <td align="right">Function</td>
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>;
3990 <td align="right">Function</td>
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>;
3996 <td align="right">Function</td>
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>;
4002 <td align="right">Function</td>
4005 <table width="95%" align="center">
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.
4016 <table width="100%">
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>
4020 <td align="right">Function</td>
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>
4026 <td align="right">Function</td>
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>
4032 <td align="right">Function</td>
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>
4038 <td align="right">Function</td>
4041 <table width="95%" align="center">
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.
4049 <table width="100%">
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>
4053 <td align="right">Function</td>
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>
4059 <td align="right">Function</td>
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>
4065 <td align="right">Function</td>
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>
4071 <td align="right">Function</td>
4074 <table width="95%" align="center">
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.
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.
4088 <table width="100%">
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>
4092 <td align="right">Function</td>
4095 <table width="95%" align="center">
4097 Computes <code>x = m^d</code>, efficiently.
4101 <p>At last, how do you create new keys?
4104 <table width="100%">
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>;
4108 <td align="right">Function</td>
4111 <table width="95%" align="center">
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.
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>.
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
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->e</code> is an even number.
4141 Node: <a name="DSA">DSA</a>,
4142 Previous: <a rel="previous" accesskey="p" href="#RSA">RSA</a>,
4143 Up: <a rel="up" accesskey="u" href="#Public-key%20algorithms">Public-key algorithms</a>
4147 <h3 class="subsection">Nettle's <small>DSA</small> support</h4>
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>.
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.
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
4171 <p>The subgroup requirement means that if you compute
4173 <pre class="example"> g^t mod p
4176 <p>for all possible integers <code>t</code>, you will get precisely <code>q</code>
4179 <p>The private key is a secret exponent <code>x</code>, such that
4181 <pre class="example"> g^x = y mod p
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>.
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.
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
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<k<q</code>, also 160 bits. Next, one computes
4224 <pre class="example"> r = (g^k mod p) mod q
4225 s = k^-1 (h + x r) mod q
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>.
4232 <p>To verify a signature, one first checks that <code>0 < r,s < q</code>, and
4233 then one computes backwards,
4235 <pre class="example"> w = s^-1 mod q
4236 v = (g^(w h) y^(w r) mod p) mod q
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
4242 <pre class="example"> g^(w h) y^(w r) = g^(w h) (g^x)^(w r) = g^(w (h + x r)) = g^k
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
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.
4261 <p><small>DSA</small> signatures are smaller than <small>RSA</small> signatures,
4262 which is important for some specialized applications.
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
4269 <h3 class="subsection">Nettle's <small>DSA</small> support</h4>
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>.
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>.
4281 <table width="100%">
4283 <td align="left"><b>dsa_public_key</b><i> p q g y
4285 <td align="right">Context struct</td>
4288 <table width="95%" align="center">
4290 The public parameters described above.
4295 <table width="100%">
4297 <td align="left"><b>dsa_private_key</b><i> x
4299 <td align="right">Context struct</td>
4302 <table width="95%" align="center">
4304 The private key <code>x</code>.
4308 <p>Before use, these structs must be initialized by calling one of
4311 <table width="100%">
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>
4315 <td align="right">Function</td>
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>
4321 <td align="right">Function</td>
4324 <table width="95%" align="center">
4326 Calls <code>mpz_init</code> on all numbers in the key struct.
4330 <p>When finished with them, the space for the numbers must be
4331 deallocated by calling one of
4334 <table width="100%">
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>
4338 <td align="right">Function</td>
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>
4344 <td align="right">Function</td>
4347 <table width="95%" align="center">
4349 Calls <code>mpz_clear</code> on all numbers in the key struct.
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.
4357 <table width="100%">
4359 <td align="left"><b>dsa_signature</b><i> r s
4361 <td align="right">Context struct</td>
4364 <table width="95%" align="center">
4370 <table width="100%">
4372 <td align="left">void <b>dsa_signature_init</b><i> </i>(<i>struct dsa_signature *</i><var>signature</var><i></i>)<i>
4374 <td align="right">Function</td>
4378 <td align="left">void <b>dsa_signature_clear</b><i> </i>(<i>struct dsa_signature *</i><var>signature</var><i></i>)<i>
4380 <td align="right">Function</td>
4383 <table width="95%" align="center">
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
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.
4400 <table width="100%">
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>
4404 <td align="right">Function</td>
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>
4410 <td align="right">Function</td>
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>
4416 <td align="right">Function</td>
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>
4422 <td align="right">Function</td>
4425 <table width="95%" align="center">
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.
4436 <p>Verifying signatures is a little easier, since no randomness generator is
4437 needed. The functions are
4440 <table width="100%">
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>
4444 <td align="right">Function</td>
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>
4450 <td align="right">Function</td>
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>
4456 <td align="right">Function</td>
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>
4462 <td align="right">Function</td>
4465 <table width="95%" align="center">
4467 Verifies a signature. Returns 1 if the signature is valid, otherwise 0.
4471 <p>Key generation uses mostly the same parameters as the corresponding
4472 <small>RSA</small> function.
4475 <table width="100%">
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>
4479 <td align="right">Function</td>
4482 <table width="95%" align="center">
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.
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>.
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
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 <= l <= 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 =
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
4520 Node: <a name="Randomness">Randomness</a>,
4521 Next: <a rel="next" accesskey="n" href="#Miscellaneous%20functions">Miscellaneous functions</a>,
4522 Previous: <a rel="previous" accesskey="p" href="#Public-key%20algorithms">Public-key algorithms</a>,
4523 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
4527 <h3 class="section">Randomness</h3>
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.
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.
4542 <p>However, such a generator is inadequate for cryptography, for at least
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.
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
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>
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.
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
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,
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
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
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.
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.
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?
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).
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.
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:
4666 <br><dt><dfn>Key-hiding</dfn>
4667 <dd>An attacker observing the output should not be able to recover the
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.
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.
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>.
4690 <p>Nettle includes one randomness generator that is believed to have all
4691 the above properties, and two simpler ones.
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.
4699 <p>The "lagged Fibonacci" generator in <code><nettle/knuth-lfib.h></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
4705 <p>The recommended generator to use is Yarrow, described below.
4707 <h3 class="subsection">Yarrow</h4>
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 <<code>http://www.counterpane.com/yarrow.html</code>>, 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.
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.
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.
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
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.
4744 <p>Nettle defines Yarrow-256 in <code><nettle/yarrow.h></code>.
4747 <table width="100%">
4749 <td align="left"><b>struct yarrow256_ctx</b><i>
4751 <td align="right">Context struct</td>
4754 <table width="95%" align="center">
4760 <table width="100%">
4762 <td align="left"><b>struct yarrow_source</b><i>
4764 <td align="right">Context struct</td>
4767 <table width="95%" align="center">
4769 Information about a single source.
4774 <table width="100%">
4776 <td align="left"><b>YARROW256_SEED_FILE_SIZE</b><i>
4778 <td align="right">Constant</td>
4781 <table width="95%" align="center">
4783 Recommanded size of the Yarrow-256 seed file.
4788 <table width="100%">
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>
4792 <td align="right">Function</td>
4795 <table width="95%" align="center">
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.
4804 <table width="100%">
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>
4808 <td align="right">Function</td>
4811 <table width="95%" align="center">
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.
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.
4825 <table width="100%">
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>
4829 <td align="right">Function</td>
4832 <table width="95%" align="center">
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.
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
4848 <table width="100%">
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>
4852 <td align="right">Function</td>
4855 <table width="95%" align="center">
4857 Generates <var>length</var> octets of output. The generator must be seeded
4858 before you call this function.
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.
4868 <table width="100%">
4870 <td align="left">int <b>yarrow256_is_seeded</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4872 <td align="right">Function</td>
4875 <table width="95%" align="center">
4877 Returns 1 if the generator is seeded and ready to generate output,
4883 <table width="100%">
4885 <td align="left">unsigned <b>yarrow256_needed_sources</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4887 <td align="right">Function</td>
4890 <table width="95%" align="center">
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.
4898 <table width="100%">
4900 <td align="left">void <b>yarrow256_fast_reseed</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4902 <td align="right">Function</td>
4906 <td align="left">void <b>yarrow256_slow_reseed</b><i> </i>(<i>struct yarrow256_ctx *</i><var>ctx</var><i></i>)<i>
4908 <td align="right">Function</td>
4911 <table width="95%" align="center">
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.
4918 <p>Nettle includes an entropy estimator for one kind of input source: User
4922 <table width="100%">
4924 <td align="left"><b>struct yarrow_key_event_ctx</b><i>
4926 <td align="right">Context struct</td>
4929 <table width="95%" align="center">
4931 Information about recent key events.
4936 <table width="100%">
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>
4940 <td align="right">Function</td>
4943 <table width="95%" align="center">
4945 Initializes the context.
4950 <table width="100%">
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>
4954 <td align="right">Function</td>
4957 <table width="95%" align="center">
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.
4968 <p>Returns an entropy estimate, in bits, suitable for calling
4969 <code>yarrow256_update</code>. Usually, 0, 1 or 2 bits.
4975 Node: <a name="Miscellaneous%20functions">Miscellaneous functions</a>,
4976 Next: <a rel="next" accesskey="n" href="#Compatibility%20functions">Compatibility functions</a>,
4977 Previous: <a rel="previous" accesskey="p" href="#Randomness">Randomness</a>,
4978 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
4982 <h3 class="section">Miscellaneous functions</h3>
4985 <table width="100%">
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>
4989 <td align="right">Function</td>
4992 <table width="95%" align="center">
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.
5000 <p><code>memxor</code> is declared in <code><nettle/memxor.h></code>.
5004 Node: <a name="Compatibility%20functions">Compatibility functions</a>,
5005 Previous: <a rel="previous" accesskey="p" href="#Miscellaneous%20functions">Miscellaneous functions</a>,
5006 Up: <a rel="up" accesskey="u" href="#Reference">Reference</a>
5010 <h3 class="section">Compatibility functions</h3>
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.
5017 <p>MD5 is defined in [RFC 1321], which includes a reference implementation.
5018 Nettle defines a compatible interface to MD5 in
5019 <code><nettle/md5-compat.h></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>.
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><nettle/des-compat.h></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>.
5037 Node: <a name="Nettle%20soup">Nettle soup</a>,
5038 Next: <a rel="next" accesskey="n" href="#Installation">Installation</a>,
5039 Previous: <a rel="previous" accesskey="p" href="#Reference">Reference</a>,
5040 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
5044 <h2 class="chapter">Traditional Nettle Soup</h2>
5046 <p>For the serious nettle hacker, here is a recipe for nettle soup. 4 servings.
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
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.
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.
5068 <p>Serve with boiled egg-halves.
5072 Node: <a name="Installation">Installation</a>,
5073 Next: <a rel="next" accesskey="n" href="#Index">Index</a>,
5074 Previous: <a rel="previous" accesskey="p" href="#Nettle%20soup">Nettle soup</a>,
5075 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
5079 <h2 class="chapter">Installation</h2>
5081 <p>Nettle uses <code>autoconf</code>. To build it, unpack the source and run
5083 <pre class="example"> ./configure
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>.
5094 <p>To get a list of configure options, use <code>./configure --help</code>.
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>.
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>.
5106 Node: <a name="Index">Index</a>,
5107 Previous: <a rel="previous" accesskey="p" href="#Installation">Installation</a>,
5108 Up: <a rel="up" accesskey="u" href="#Top">Top</a>
5112 <h2 class="unnumbered">Function and Concept Index</h2>
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>
5274 <div class="footnote">
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>