Fix build error and set version 2.7.1
[platform/upstream/nettle.git] / nettle.info
1 This is nettle.info, produced by makeinfo version 4.13 from
2 nettle.texinfo.
3
4 This manual is for the Nettle library (version 2.7), a low-level
5 cryptographic library.
6
7    Originally written 2001 by Niels Möller, updated 2013.
8
9      This manual is placed in the public domain. You may freely copy
10      it, in whole or in part, with or without modification. Attribution
11      is appreciated, but not required.
12
13 INFO-DIR-SECTION Encryption
14 START-INFO-DIR-ENTRY
15 * Nettle: (nettle).             A low-level cryptographic library.
16 END-INFO-DIR-ENTRY
17
18 \1f
19 File: nettle.info,  Node: Top,  Next: Introduction,  Prev: (dir),  Up: (dir)
20
21 Nettle
22 ******
23
24 This document describes the Nettle low-level cryptographic library. You
25 can use the library directly from your C programs, or write or use an
26 object-oriented wrapper for your favorite language or application.
27
28    This manual is for the Nettle library (version 2.7), a low-level
29 cryptographic library.
30
31    Originally written 2001 by Niels Möller, updated 2013.
32
33      This manual is placed in the public domain. You may freely copy
34      it, in whole or in part, with or without modification. Attribution
35      is appreciated, but not required.
36
37 * Menu:
38
39 * Introduction::                What is Nettle?
40 * Copyright::                   Your rights.
41 * Conventions::                 General interface conventions.
42 * Example::                     An example program.
43 * Linking::                     Linking with libnettle and libhogweed.
44 * Reference::                   All Nettle functions and features.
45 * Nettle soup::                 For the serious nettle hacker.
46 * Installation::                How to install Nettle.
47 * Index::                       Function and concept index.
48
49  --- The Detailed Node Listing ---
50
51 Reference
52
53 * Hash functions::
54 * Cipher functions::
55 * Cipher modes::
56 * Keyed hash functions::
57 * Key derivation functions::
58 * Public-key algorithms::
59 * Randomness::
60 * ASCII encoding::
61 * Miscellaneous functions::
62 * Compatibility functions::
63
64 Cipher modes
65
66 * CBC::
67 * CTR::
68 * GCM::
69
70 Public-key algorithms
71
72 * RSA::                         The RSA public key algorithm.
73 * DSA::                         The DSA digital signature algorithm.
74 * Elliptic curves::             Elliptic curves and ECDSA
75
76 \1f
77 File: nettle.info,  Node: Introduction,  Next: Copyright,  Prev: Top,  Up: Top
78
79 1 Introduction
80 **************
81
82 Nettle is a cryptographic library that is designed to fit easily in more
83 or less any context: In crypto toolkits for object-oriented languages
84 (C++, Python, Pike, ...), in applications like LSH or GNUPG, or even in
85 kernel space. In most contexts, you need more than the basic
86 cryptographic algorithms, you also need some way to keep track of
87 available algorithms, their properties and variants. You often have
88 some algorithm selection process, often dictated by a protocol you want
89 to implement.
90
91    And as the requirements of applications differ in subtle and not so
92 subtle ways, an API that fits one application well can be a pain to use
93 in a different context. And that is why there are so many different
94 cryptographic libraries around.
95
96    Nettle tries to avoid this problem by doing one thing, the low-level
97 crypto stuff, and providing a _simple_ but general interface to it.  In
98 particular, Nettle doesn't do algorithm selection. It doesn't do memory
99 allocation. It doesn't do any I/O.
100
101    The idea is that one can build several application and context
102 specific interfaces on top of Nettle, and share the code, test cases,
103 benchmarks, documentation, etc. Examples are the Nettle module for the
104 Pike language, and LSH, which both use an object-oriented abstraction
105 on top of the library.
106
107    This manual explains how to use the Nettle library. It also tries to
108 provide some background on the cryptography, and advice on how to best
109 put it to use.
110
111 \1f
112 File: nettle.info,  Node: Copyright,  Next: Conventions,  Prev: Introduction,  Up: Top
113
114 2 Copyright
115 ***********
116
117 Nettle is distributed under the GNU Lesser General Public License
118 (LGPL), see the file COPYING.LIB for details. A few of the individual
119 files are in the public domain. To find the current status of particular
120 files, you have to read the copyright notices at the top of the files.
121
122    This manual is in the public domain. You may freely copy it in whole
123 or in part, e.g., into documentation of programs that build on Nettle.
124 Attribution, as well as contribution of improvements to the text, is of
125 course appreciated, but it is not required.
126
127    A list of the supported algorithms, their origins and licenses:
128
129 _AES_
130      The implementation of the AES cipher (also known as rijndael) is
131      written by Rafael Sevilla. Assembler for x86 by Rafael Sevilla and
132      Niels Möller, Sparc assembler by Niels Möller. Released under the
133      LGPL.
134
135 _ARCFOUR_
136      The implementation of the ARCFOUR (also known as RC4) cipher is
137      written by Niels Möller. Released under the LGPL.
138
139 _ARCTWO_
140      The implementation of the ARCTWO (also known as RC2) cipher is
141      written by Nikos Mavroyanopoulos and modified by Werner Koch and
142      Simon Josefsson. Released under the LGPL.
143
144 _BLOWFISH_
145      The implementation of the BLOWFISH cipher is written by Werner
146      Koch, copyright owned by the Free Software Foundation. Also hacked
147      by Simon Josefsson and Niels Möller. Released under the LGPL.
148
149 _CAMELLIA_
150      The C implementation is by Nippon Telegraph and Telephone
151      Corporation (NTT), heavily modified by Niels Möller. Assembler for
152      x86 and x86_64 by Niels Möller. Released under the LGPL.
153
154 _CAST128_
155      The implementation of the CAST128 cipher is written by Steve Reid.
156      Released into the public domain.
157
158 _DES_
159      The implementation of the DES cipher is written by Dana L. How, and
160      released under the LGPL.
161
162 _GOSTHASH94_
163      The C implementation of the GOST94 message digest is written by
164      Aleksey Kravchenko and was ported from the rhash library by Nikos
165      Mavrogiannopoulos. It is released under the MIT license.
166
167 _MD2_
168      The implementation of MD2 is written by Andrew Kuchling, and hacked
169      some by Andreas Sigfridsson and Niels Möller. Python Cryptography
170      Toolkit license (essentially public domain).
171
172 _MD4_
173      This is almost the same code as for MD5 below, with modifications
174      by Marcus Comstedt. Released into the public domain.
175
176 _MD5_
177      The implementation of the MD5 message digest is written by Colin
178      Plumb.  It has been hacked some more by Andrew Kuchling and Niels
179      Möller.  Released into the public domain.
180
181 _PBKDF2_
182      The C implementation of PBKDF2 is based on earlier work for Shishi
183      and GnuTLS by Simon Josefsson.  Released under the LGPL.
184
185 _RIPEMD160_
186      The implementation of RIPEMD160 message digest is based on the
187      code in libgcrypt, copyright owned by the Free Software
188      Foundation. Ported to Nettle by Andres Mejia. Released under the
189      LGPL.
190
191 _SALSA20_
192      The C implementation of SALSA20 is based on D. J. Bernstein's
193      reference implementation (in the public domain), adapted to Nettle
194      by Simon Josefsson, and heavily modified by Niels Möller. Assembly
195      for x86_64 and ARM by Niels Möller. Released under the LGPL.
196
197 _SERPENT_
198      The implementation of the SERPENT cipher is based on the code in
199      libgcrypt, copyright owned by the Free Software Foundation.
200      Adapted to Nettle by Simon Josefsson and heavily modified by Niels
201      Möller. Assembly for x86_64 by Niels Möller. Released under the
202      LGPL.
203
204 _SHA1_
205      The C implementation of the SHA1 message digest is written by Peter
206      Gutmann, and hacked some more by Andrew Kuchling and Niels Möller.
207      Released into the public domain. Assembler for x86, x86_64 and ARM
208      by Niels Möller, released under the LGPL.
209
210 _SHA2_
211      Written by Niels Möller, using Peter Gutmann's SHA1 code as a
212      model.  Released under the LGPL.
213
214 _SHA3_
215      Written by Niels Möller. Released under the LGPL.
216
217 _TWOFISH_
218      The implementation of the TWOFISH cipher is written by Ruud de
219      Rooij.  Released under the LGPL.
220
221 _UMAC_
222      Written by Niels Möller. Released under the LGPL.
223
224 _RSA_
225      Written by Niels Möller, released under the LGPL. Uses the GMP
226      library for bignum operations.
227
228 _DSA_
229      Written by Niels Möller, released under the LGPL. Uses the GMP
230      library for bignum operations.
231
232 _ECDSA_
233      Written by Niels Möller, released under the LGPL. Uses the GMP
234      library for bignum operations. Development of Nettle's ECC support
235      was funded by the .SE Internet Fund.
236
237 \1f
238 File: nettle.info,  Node: Conventions,  Next: Example,  Prev: Copyright,  Up: Top
239
240 3 Conventions
241 *************
242
243 For each supported algorithm, there is an include file that defines a
244 _context struct_, a few constants, and declares functions for operating
245 on the context. The context struct encapsulates all information needed
246 by the algorithm, and it can be copied or moved in memory with no
247 unexpected effects.
248
249    For consistency, functions for different algorithms are very similar,
250 but there are some differences, for instance reflecting if the key setup
251 or encryption function differ for encryption and decryption, and whether
252 or not key setup can fail. There are also differences between algorithms
253 that don't show in function prototypes, but which the application must
254 nevertheless be aware of. There is no big difference between the
255 functions for stream ciphers and for block ciphers, although they should
256 be used quite differently by the application.
257
258    If your application uses more than one algorithm of the same type,
259 you should probably create an interface that is tailor-made for your
260 needs, and then write a few lines of glue code on top of Nettle.
261
262    By convention, for an algorithm named `foo', the struct tag for the
263 context struct is `foo_ctx', constants and functions uses prefixes like
264 `FOO_BLOCK_SIZE' (a constant) and `foo_set_key' (a function).
265
266    In all functions, strings are represented with an explicit length, of
267 type `unsigned', and a pointer of type `uint8_t *' or `const uint8_t
268 *'. For functions that transform one string to another, the argument
269 order is length, destination pointer and source pointer. Source and
270 destination areas are of the same length. Source and destination may be
271 the same, so that you can process strings in place, but they _must not_
272 overlap in any other way.
273
274    Many of the functions lack return value and can never fail. Those
275 functions which can fail, return one on success and zero on failure.
276
277 \1f
278 File: nettle.info,  Node: Example,  Next: Linking,  Prev: Conventions,  Up: Top
279
280 4 Example
281 *********
282
283 A simple example program that reads a file from standard input and
284 writes its SHA1 check-sum on standard output should give the flavor of
285 Nettle.
286
287      #include <stdio.h>
288      #include <stdlib.h>
289
290      #include <nettle/sha1.h>
291
292      #define BUF_SIZE 1000
293
294      static void
295      display_hex(unsigned length, uint8_t *data)
296      {
297        unsigned i;
298
299        for (i = 0; i<length; i++)
300          printf("%02x ", data[i]);
301
302        printf("\n");
303      }
304
305      int
306      main(int argc, char **argv)
307      {
308        struct sha1_ctx ctx;
309        uint8_t buffer[BUF_SIZE];
310        uint8_t digest[SHA1_DIGEST_SIZE];
311
312        sha1_init(&ctx);
313        for (;;)
314        {
315          int done = fread(buffer, 1, sizeof(buffer), stdin);
316          sha1_update(&ctx, done, buffer);
317          if (done < sizeof(buffer))
318            break;
319        }
320        if (ferror(stdin))
321          return EXIT_FAILURE;
322
323        sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
324
325        display_hex(SHA1_DIGEST_SIZE, digest);
326        return EXIT_SUCCESS;
327      }
328
329    On a typical Unix system, this program can be compiled and linked
330 with the command line
331      gcc sha-example.c -o sha-example -lnettle
332
333 \1f
334 File: nettle.info,  Node: Linking,  Next: Reference,  Prev: Example,  Up: Top
335
336 5 Linking
337 *********
338
339 Nettle actually consists of two libraries, `libnettle' and
340 `libhogweed'. The `libhogweed' library contains those functions of
341 Nettle that uses bignum operations, and depends on the GMP library.
342 With this division, linking works the same for both static and dynamic
343 libraries.
344
345    If an application uses only the symmetric crypto algorithms of Nettle
346 (i.e., block ciphers, hash functions, and the like), it's sufficient to
347 link with `-lnettle'. If an application also uses public-key
348 algorithms, the recommended linker flags are `-lhogweed -lnettle
349 -lgmp'. If the involved libraries are installed as dynamic libraries, it
350 may be sufficient to link with just `-lhogweed', and the loader will
351 resolve the dependencies automatically.
352
353 \1f
354 File: nettle.info,  Node: Reference,  Next: Nettle soup,  Prev: Linking,  Up: Top
355
356 6 Reference
357 ***********
358
359 This chapter describes all the Nettle functions, grouped by family.
360
361 * Menu:
362
363 * Hash functions::
364 * Cipher functions::
365 * Cipher modes::
366 * Keyed hash functions::
367 * Key derivation functions::
368 * Public-key algorithms::
369 * Randomness::
370 * ASCII encoding::
371 * Miscellaneous functions::
372 * Compatibility functions::
373
374 \1f
375 File: nettle.info,  Node: Hash functions,  Next: Cipher functions,  Prev: Reference,  Up: Reference
376
377 6.1 Hash functions
378 ==================
379
380 A cryptographic "hash function" is a function that takes variable size
381 strings, and maps them to strings of fixed, short, length. There are
382 naturally lots of collisions, as there are more possible 1MB files than
383 20 byte strings. But the function is constructed such that is hard to
384 find the collisions. More precisely, a cryptographic hash function `H'
385 should have the following properties:
386
387 _One-way_
388      Given a hash value `H(x)' it is hard to find a string `x' that
389      hashes to that value.
390
391 _Collision-resistant_
392      It is hard to find two different strings, `x' and `y', such that
393      `H(x)' = `H(y)'.
394
395
396    Hash functions are useful as building blocks for digital signatures,
397 message authentication codes, pseudo random generators, association of
398 unique ids to documents, and many other things.
399
400    The most commonly used hash functions are MD5 and SHA1.
401 Unfortunately, both these fail the collision-resistance requirement;
402 cryptologists have found ways to construct colliding inputs. The
403 recommended hash functions for new applications are SHA2 (with main
404 variants SHA256 and SHA512). At the time of this writing (December
405 2012), the winner of the NIST SHA3 competition has recently been
406 announced, and the new SHA3 (earlier known as Keccak) and other top
407 SHA3 candidates may also be reasonable alternatives.
408
409 * Menu:
410
411 * Recommended hash functions::
412 * Legacy hash functions::
413 * nettle_hash abstraction::
414
415 \1f
416 File: nettle.info,  Node: Recommended hash functions,  Next: Legacy hash functions,  Up: Hash functions
417
418 6.1.1 Recommended hash functions
419 --------------------------------
420
421 The following hash functions have no known weaknesses, and are suitable
422 for new applications. The SHA2 family of hash functions were specified
423 by "NIST", intended as a replacement for SHA1.
424
425 6.1.1.1 SHA256
426 ..............
427
428 SHA256 is a member of the SHA2 family. It outputs hash values of 256
429 bits, or 32 octets. Nettle defines SHA256 in `<nettle/sha2.h>'.
430
431  -- Context struct: struct sha256_ctx
432
433  -- Constant: SHA256_DIGEST_SIZE
434      The size of a SHA256 digest, i.e. 32.
435
436  -- Constant: SHA256_DATA_SIZE
437      The internal block size of SHA256. Useful for some special
438      constructions, in particular HMAC-SHA256.
439
440  -- Function: void sha256_init (struct sha256_ctx *CTX)
441      Initialize the SHA256 state.
442
443  -- Function: void sha256_update (struct sha256_ctx *CTX, unsigned
444           LENGTH, const uint8_t *DATA)
445      Hash some more data.
446
447  -- Function: void sha256_digest (struct sha256_ctx *CTX, unsigned
448           LENGTH, uint8_t *DIGEST)
449      Performs final processing and extracts the message digest, writing
450      it to DIGEST. LENGTH may be smaller than `SHA256_DIGEST_SIZE', in
451      which case only the first LENGTH octets of the digest are written.
452
453      This function also resets the context in the same way as
454      `sha256_init'.
455
456    Earlier versions of nettle defined SHA256 in the header file
457 `<nettle/sha.h>', which is now deprecated, but kept for compatibility.
458
459 6.1.1.2 SHA224
460 ..............
461
462 SHA224 is a variant of SHA256, with a different initial state, and with
463 the output truncated to 224 bits, or 28 octets. Nettle defines SHA224 in
464 `<nettle/sha2.h>' (and in `<nettle/sha.h>', for backwards
465 compatibility).
466
467  -- Context struct: struct sha224_ctx
468
469  -- Constant: SHA224_DIGEST_SIZE
470      The size of a SHA224 digest, i.e. 28.
471
472  -- Constant: SHA224_DATA_SIZE
473      The internal block size of SHA224. Useful for some special
474      constructions, in particular HMAC-SHA224.
475
476  -- Function: void sha224_init (struct sha224_ctx *CTX)
477      Initialize the SHA224 state.
478
479  -- Function: void sha224_update (struct sha224_ctx *CTX, unsigned
480           LENGTH, const uint8_t *DATA)
481      Hash some more data.
482
483  -- Function: void sha224_digest (struct sha224_ctx *CTX, unsigned
484           LENGTH, uint8_t *DIGEST)
485      Performs final processing and extracts the message digest, writing
486      it to DIGEST. LENGTH may be smaller than `SHA224_DIGEST_SIZE', in
487      which case only the first LENGTH octets of the digest are written.
488
489      This function also resets the context in the same way as
490      `sha224_init'.
491
492 6.1.1.3 SHA512
493 ..............
494
495 SHA512 is a larger sibling to SHA256, with a very similar structure but
496 with both the output and the internal variables of twice the size. The
497 internal variables are 64 bits rather than 32, making it significantly
498 slower on 32-bit computers. It outputs hash values of 512 bits, or 64
499 octets. Nettle defines SHA512 in `<nettle/sha2.h>' (and in
500 `<nettle/sha.h>', for backwards compatibility).
501
502  -- Context struct: struct sha512_ctx
503
504  -- Constant: SHA512_DIGEST_SIZE
505      The size of a SHA512 digest, i.e. 64.
506
507  -- Constant: SHA512_DATA_SIZE
508      The internal block size of SHA512. Useful for some special
509      constructions, in particular HMAC-SHA512.
510
511  -- Function: void sha512_init (struct sha512_ctx *CTX)
512      Initialize the SHA512 state.
513
514  -- Function: void sha512_update (struct sha512_ctx *CTX, unsigned
515           LENGTH, const uint8_t *DATA)
516      Hash some more data.
517
518  -- Function: void sha512_digest (struct sha512_ctx *CTX, unsigned
519           LENGTH, uint8_t *DIGEST)
520      Performs final processing and extracts the message digest, writing
521      it to DIGEST. LENGTH may be smaller than `SHA512_DIGEST_SIZE', in
522      which case only the first LENGTH octets of the digest are written.
523
524      This function also resets the context in the same way as
525      `sha512_init'.
526
527 6.1.1.4 SHA384
528 ..............
529
530 SHA384 is a variant of SHA512, with a different initial state, and with
531 the output truncated to 384 bits, or 48 octets. Nettle defines SHA384 in
532 `<nettle/sha2.h>' (and in `<nettle/sha.h>', for backwards
533 compatibility).
534
535  -- Context struct: struct sha384_ctx
536
537  -- Constant: SHA384_DIGEST_SIZE
538      The size of a SHA384 digest, i.e. 48.
539
540  -- Constant: SHA384_DATA_SIZE
541      The internal block size of SHA384. Useful for some special
542      constructions, in particular HMAC-SHA384.
543
544  -- Function: void sha384_init (struct sha384_ctx *CTX)
545      Initialize the SHA384 state.
546
547  -- Function: void sha384_update (struct sha384_ctx *CTX, unsigned
548           LENGTH, const uint8_t *DATA)
549      Hash some more data.
550
551  -- Function: void sha384_digest (struct sha384_ctx *CTX, unsigned
552           LENGTH, uint8_t *DIGEST)
553      Performs final processing and extracts the message digest, writing
554      it to DIGEST. LENGTH may be smaller than `SHA384_DIGEST_SIZE', in
555      which case only the first LENGTH octets of the digest are written.
556
557      This function also resets the context in the same way as
558      `sha384_init'.
559
560 6.1.1.5 SHA3-224
561 ................
562
563 The SHA3 hash functions were specified by NIST in response to weaknesses
564 in SHA1, and doubts about SHA2 hash functions which structurally are
565 very similar to SHA1. The standard is a result of a competition, where
566 the winner, also known as Keccak, was designed by Guido Bertoni, Joan
567 Daemen, Michaël Peeters and Gilles Van Assche. It is structurally very
568 different from all widely used earlier hash functions. Like SHA2, there
569 are several variants, with output sizes of 224, 256, 384 and 512 bits
570 (28, 32, 48 and 64 octets, respectively).
571
572    Nettle defines SHA3-224 in `<nettle/sha3.h>'.
573
574  -- Context struct: struct sha3_224_ctx
575
576  -- Constant: SHA3_224_DIGEST_SIZE
577      The size of a SHA3_224 digest, i.e., 28.
578
579  -- Constant: SHA3_224_DATA_SIZE
580      The internal block size of SHA3_224.
581
582  -- Function: void sha3_224_init (struct sha3_224_ctx *CTX)
583      Initialize the SHA3-224 state.
584
585  -- Function: void sha3_224_update (struct sha3_224_ctx *CTX, unsigned
586           LENGTH, const uint8_t *DATA)
587      Hash some more data.
588
589  -- Function: void sha3_224_digest (struct sha3_224_ctx *CTX, unsigned
590           LENGTH, uint8_t *DIGEST)
591      Performs final processing and extracts the message digest, writing
592      it to DIGEST. LENGTH may be smaller than `SHA3_224_DIGEST_SIZE',
593      in which case only the first LENGTH octets of the digest are
594      written.
595
596      This function also resets the context.
597
598 6.1.1.6 SHA3-256
599 ................
600
601 This is SHA3 with 256-bit output size, and possibly the most useful of
602 the SHA3 hash functions.
603
604    Nettle defines SHA3-256 in `<nettle/sha3.h>'.
605
606  -- Context struct: struct sha3_256_ctx
607
608  -- Constant: SHA3_256_DIGEST_SIZE
609      The size of a SHA3_256 digest, i.e., 32.
610
611  -- Constant: SHA3_256_DATA_SIZE
612      The internal block size of SHA3_256.
613
614  -- Function: void sha3_256_init (struct sha3_256_ctx *CTX)
615      Initialize the SHA3-256 state.
616
617  -- Function: void sha3_256_update (struct sha3_256_ctx *CTX, unsigned
618           LENGTH, const uint8_t *DATA)
619      Hash some more data.
620
621  -- Function: void sha3_256_digest (struct sha3_256_ctx *CTX, unsigned
622           LENGTH, uint8_t *DIGEST)
623      Performs final processing and extracts the message digest, writing
624      it to DIGEST. LENGTH may be smaller than `SHA3_256_DIGEST_SIZE',
625      in which case only the first LENGTH octets of the digest are
626      written.
627
628      This function also resets the context.
629
630 6.1.1.7 SHA3-384
631 ................
632
633 This is SHA3 with 384-bit output size.
634
635    Nettle defines SHA3-384 in `<nettle/sha3.h>'.
636
637  -- Context struct: struct sha3_384_ctx
638
639  -- Constant: SHA3_384_DIGEST_SIZE
640      The size of a SHA3_384 digest, i.e., 48.
641
642  -- Constant: SHA3_384_DATA_SIZE
643      The internal block size of SHA3_384.
644
645  -- Function: void sha3_384_init (struct sha3_384_ctx *CTX)
646      Initialize the SHA3-384 state.
647
648  -- Function: void sha3_384_update (struct sha3_384_ctx *CTX, unsigned
649           LENGTH, const uint8_t *DATA)
650      Hash some more data.
651
652  -- Function: void sha3_384_digest (struct sha3_384_ctx *CTX, unsigned
653           LENGTH, uint8_t *DIGEST)
654      Performs final processing and extracts the message digest, writing
655      it to DIGEST. LENGTH may be smaller than `SHA3_384_DIGEST_SIZE',
656      in which case only the first LENGTH octets of the digest are
657      written.
658
659      This function also resets the context.
660
661 6.1.1.8 SHA3-512
662 ................
663
664 This is SHA3 with 512-bit output size.
665
666    Nettle defines SHA3-512 in `<nettle/sha3.h>'.
667
668  -- Context struct: struct sha3_512_ctx
669
670  -- Constant: SHA3_512_DIGEST_SIZE
671      The size of a SHA3_512 digest, i.e. 64.
672
673  -- Constant: SHA3_512_DATA_SIZE
674      The internal block size of SHA3_512.
675
676  -- Function: void sha3_512_init (struct sha3_512_ctx *CTX)
677      Initialize the SHA3-512 state.
678
679  -- Function: void sha3_512_update (struct sha3_512_ctx *CTX, unsigned
680           LENGTH, const uint8_t *DATA)
681      Hash some more data.
682
683  -- Function: void sha3_512_digest (struct sha3_512_ctx *CTX, unsigned
684           LENGTH, uint8_t *DIGEST)
685      Performs final processing and extracts the message digest, writing
686      it to DIGEST. LENGTH may be smaller than `SHA3_512_DIGEST_SIZE',
687      in which case only the first LENGTH octets of the digest are
688      written.
689
690      This function also resets the context.
691
692 \1f
693 File: nettle.info,  Node: Legacy hash functions,  Next: nettle_hash abstraction,  Prev: Recommended hash functions,  Up: Hash functions
694
695 6.1.2 Legacy hash functions
696 ---------------------------
697
698 The hash functions in this section all have some known weaknesses, and
699 should be avoided for new applications. These hash functions are mainly
700 useful for compatibility with old applications and protocols. Some are
701 still considered safe as building blocks for particular constructions,
702 e.g., there seems to be no known attacks against HMAC-SHA1 or even
703 HMAC-MD5. In some important cases, use of a "legacy" hash function does
704 not in itself make the application insecure; if a known weakness is
705 relevant depends on how the hash function is used, and on the threat
706 model.
707
708 6.1.2.1 MD5
709 ...........
710
711 MD5 is a message digest function constructed by Ronald Rivest, and
712 described in `RFC 1321'. It outputs message digests of 128 bits, or 16
713 octets. Nettle defines MD5 in `<nettle/md5.h>'.
714
715  -- Context struct: struct md5_ctx
716
717  -- Constant: MD5_DIGEST_SIZE
718      The size of an MD5 digest, i.e. 16.
719
720  -- Constant: MD5_DATA_SIZE
721      The internal block size of MD5. Useful for some special
722      constructions, in particular HMAC-MD5.
723
724  -- Function: void md5_init (struct md5_ctx *CTX)
725      Initialize the MD5 state.
726
727  -- Function: void md5_update (struct md5_ctx *CTX, unsigned LENGTH,
728           const uint8_t *DATA)
729      Hash some more data.
730
731  -- Function: void md5_digest (struct md5_ctx *CTX, unsigned LENGTH,
732           uint8_t *DIGEST)
733      Performs final processing and extracts the message digest, writing
734      it to DIGEST. LENGTH may be smaller than `MD5_DIGEST_SIZE', in
735      which case only the first LENGTH octets of the digest are written.
736
737      This function also resets the context in the same way as
738      `md5_init'.
739
740    The normal way to use MD5 is to call the functions in order: First
741 `md5_init', then `md5_update' zero or more times, and finally
742 `md5_digest'. After `md5_digest', the context is reset to its initial
743 state, so you can start over calling `md5_update' to hash new data.
744
745    To start over, you can call `md5_init' at any time.
746
747 6.1.2.2 MD2
748 ...........
749
750 MD2 is another hash function of Ronald Rivest's, described in `RFC
751 1319'. It outputs message digests of 128 bits, or 16 octets.  Nettle
752 defines MD2 in `<nettle/md2.h>'.
753
754  -- Context struct: struct md2_ctx
755
756  -- Constant: MD2_DIGEST_SIZE
757      The size of an MD2 digest, i.e. 16.
758
759  -- Constant: MD2_DATA_SIZE
760      The internal block size of MD2.
761
762  -- Function: void md2_init (struct md2_ctx *CTX)
763      Initialize the MD2 state.
764
765  -- Function: void md2_update (struct md2_ctx *CTX, unsigned LENGTH,
766           const uint8_t *DATA)
767      Hash some more data.
768
769  -- Function: void md2_digest (struct md2_ctx *CTX, unsigned LENGTH,
770           uint8_t *DIGEST)
771      Performs final processing and extracts the message digest, writing
772      it to DIGEST. LENGTH may be smaller than `MD2_DIGEST_SIZE', in
773      which case only the first LENGTH octets of the digest are written.
774
775      This function also resets the context in the same way as
776      `md2_init'.
777
778 6.1.2.3 MD4
779 ...........
780
781 MD4 is a predecessor of MD5, described in `RFC 1320'. Like MD5, it is
782 constructed by Ronald Rivest. It outputs message digests of 128 bits,
783 or 16 octets. Nettle defines MD4 in `<nettle/md4.h>'. Use of MD4 is not
784 recommended, but it is sometimes needed for compatibility with existing
785 applications and protocols.
786
787  -- Context struct: struct md4_ctx
788
789  -- Constant: MD4_DIGEST_SIZE
790      The size of an MD4 digest, i.e. 16.
791
792  -- Constant: MD4_DATA_SIZE
793      The internal block size of MD4.
794
795  -- Function: void md4_init (struct md4_ctx *CTX)
796      Initialize the MD4 state.
797
798  -- Function: void md4_update (struct md4_ctx *CTX, unsigned LENGTH,
799           const uint8_t *DATA)
800      Hash some more data.
801
802  -- Function: void md4_digest (struct md4_ctx *CTX, unsigned LENGTH,
803           uint8_t *DIGEST)
804      Performs final processing and extracts the message digest, writing
805      it to DIGEST. LENGTH may be smaller than `MD4_DIGEST_SIZE', in
806      which case only the first LENGTH octets of the digest are written.
807
808      This function also resets the context in the same way as
809      `md4_init'.
810
811 6.1.2.4 RIPEMD160
812 .................
813
814 RIPEMD160 is a hash function designed by Hans Dobbertin, Antoon
815 Bosselaers, and Bart Preneel, as a strengthened version of RIPEMD
816 (which, like MD4 and MD5, fails the collision-resistance requirement).
817 It produces message digests of 160 bits, or 20 octets. Nettle defined
818 RIPEMD160 in `nettle/ripemd160.h'.
819
820  -- Context struct: struct ripemd160_ctx
821
822  -- Constant: RIPEMD160_DIGEST_SIZE
823      The size of a RIPEMD160 digest, i.e. 20.
824
825  -- Constant: RIPEMD160_DATA_SIZE
826      The internal block size of RIPEMD160.
827
828  -- Function: void ripemd160_init (struct ripemd160_ctx *CTX)
829      Initialize the RIPEMD160 state.
830
831  -- Function: void ripemd160_update (struct ripemd160_ctx *CTX,
832           unsigned LENGTH, const uint8_t *DATA)
833      Hash some more data.
834
835  -- Function: void ripemd160_digest (struct ripemd160_ctx *CTX,
836           unsigned LENGTH, uint8_t *DIGEST)
837      Performs final processing and extracts the message digest, writing
838      it to DIGEST. LENGTH may be smaller than `RIPEMD160_DIGEST_SIZE',
839      in which case only the first LENGTH octets of the digest are
840      written.
841
842      This function also resets the context in the same way as
843      `ripemd160_init'.
844
845 6.1.2.5 SHA1
846 ............
847
848 SHA1 is a hash function specified by "NIST" (The U.S. National
849 Institute for Standards and Technology). It outputs hash values of 160
850 bits, or 20 octets. Nettle defines SHA1 in `<nettle/sha1.h>' (and in
851 `<nettle/sha.h>', for backwards compatibility).
852
853  -- Context struct: struct sha1_ctx
854
855  -- Constant: SHA1_DIGEST_SIZE
856      The size of a SHA1 digest, i.e. 20.
857
858  -- Constant: SHA1_DATA_SIZE
859      The internal block size of SHA1. Useful for some special
860      constructions, in particular HMAC-SHA1.
861
862  -- Function: void sha1_init (struct sha1_ctx *CTX)
863      Initialize the SHA1 state.
864
865  -- Function: void sha1_update (struct sha1_ctx *CTX, unsigned LENGTH,
866           const uint8_t *DATA)
867      Hash some more data.
868
869  -- Function: void sha1_digest (struct sha1_ctx *CTX, unsigned LENGTH,
870           uint8_t *DIGEST)
871      Performs final processing and extracts the message digest, writing
872      it to DIGEST. LENGTH may be smaller than `SHA1_DIGEST_SIZE', in
873      which case only the first LENGTH octets of the digest are written.
874
875      This function also resets the context in the same way as
876      `sha1_init'.
877
878 6.1.2.6 GOSTHASH94
879 ..................
880
881 The GOST94 or GOST R 34.11-94 hash algorithm is a Soviet-era algorithm
882 used in Russian government standards (see `RFC 4357').  It outputs
883 message digests of 256 bits, or 32 octets.  Nettle defines GOSTHASH94
884 in `<nettle/gosthash94.h>'.
885
886  -- Context struct: struct gosthash94_ctx
887
888  -- Constant: GOSTHASH94_DIGEST_SIZE
889      The size of a GOSTHASH94 digest, i.e. 32.
890
891  -- Constant: GOSTHASH94_DATA_SIZE
892      The internal block size of GOSTHASH94, i.e., 32.
893
894  -- Function: void gosthash94_init (struct gosthash94_ctx *CTX)
895      Initialize the GOSTHASH94 state.
896
897  -- Function: void gosthash94_update (struct gosthash94_ctx *CTX,
898           unsigned LENGTH, const uint8_t *DATA)
899      Hash some more data.
900
901  -- Function: void gosthash94_digest (struct gosthash94_ctx *CTX,
902           unsigned LENGTH, uint8_t *DIGEST)
903      Performs final processing and extracts the message digest, writing
904      it to DIGEST. LENGTH may be smaller than `GOSTHASH94_DIGEST_SIZE',
905      in which case only the first LENGTH octets of the digest are
906      written.
907
908      This function also resets the context in the same way as
909      `gosthash94_init'.
910
911 \1f
912 File: nettle.info,  Node: nettle_hash abstraction,  Prev: Legacy hash functions,  Up: Hash functions
913
914 6.1.3 The nettle_hash abstraction
915 ---------------------------------
916
917 Nettle includes a struct including information about the supported hash
918 functions. It is defined in `<nettle/nettle-meta.h>', and is used by
919 Nettle's implementation of HMAC (*note Keyed hash functions::).
920
921  -- Meta struct: `struct nettle_hash' name context_size digest_size
922           block_size init update digest
923      The last three attributes are function pointers, of types
924      `nettle_hash_init_func', `nettle_hash_update_func', and
925      `nettle_hash_digest_func'. The first argument to these functions is
926      `void *' pointer to a context struct, which is of size
927      `context_size'.
928
929  -- Constant Struct: struct nettle_hash nettle_md2
930  -- Constant Struct: struct nettle_hash nettle_md4
931  -- Constant Struct: struct nettle_hash nettle_md5
932  -- Constant Struct: struct nettle_hash nettle_ripemd160
933  -- Constant Struct: struct nettle_hash nettle_sha1
934  -- Constant Struct: struct nettle_hash nettle_sha224
935  -- Constant Struct: struct nettle_hash nettle_sha256
936  -- Constant Struct: struct nettle_hash nettle_sha384
937  -- Constant Struct: struct nettle_hash nettle_sha512
938  -- Constant Struct: struct nettle_hash nettle_sha3_256
939  -- Constant Struct: struct nettle_hash nettle_gosthash94
940      These are all the hash functions that Nettle implements.
941
942    Nettle also exports a list of all these hashes.
943
944  -- Constant Array: struct nettle_hash ** nettle_hashes
945      This list can be used to dynamically enumerate or search the
946      supported algorithms. NULL-terminated.
947
948 \1f
949 File: nettle.info,  Node: Cipher functions,  Next: Cipher modes,  Prev: Hash functions,  Up: Reference
950
951 6.2 Cipher functions
952 ====================
953
954 A "cipher" is a function that takes a message or "plaintext" and a
955 secret "key" and transforms it to a "ciphertext". Given only the
956 ciphertext, but not the key, it should be hard to find the plaintext.
957 Given matching pairs of plaintext and ciphertext, it should be hard to
958 find the key.
959
960    There are two main classes of ciphers: Block ciphers and stream
961 ciphers.
962
963    A block cipher can process data only in fixed size chunks, called
964 "blocks". Typical block sizes are 8 or 16 octets. To encrypt arbitrary
965 messages, you usually have to pad it to an integral number of blocks,
966 split it into blocks, and then process each block. The simplest way is
967 to process one block at a time, independent of each other. That mode of
968 operation is called "ECB", Electronic Code Book mode.  However, using
969 ECB is usually a bad idea. For a start, plaintext blocks that are equal
970 are transformed to ciphertext blocks that are equal; that leaks
971 information about the plaintext. Usually you should apply the cipher is
972 some "feedback mode", "CBC" (Cipher Block Chaining) and "CTR" (Counter
973 mode) being two of of the most popular. See *Note Cipher modes::, for
974 information on how to apply CBC and CTR with Nettle.
975
976    A stream cipher can be used for messages of arbitrary length. A
977 typical stream cipher is a keyed pseudo-random generator. To encrypt a
978 plaintext message of N octets, you key the generator, generate N octets
979 of pseudo-random data, and XOR it with the plaintext. To decrypt,
980 regenerate the same stream using the key, XOR it to the ciphertext, and
981 the plaintext is recovered.
982
983    *Caution:* The first rule for this kind of cipher is the same as for
984 a One Time Pad: _never_ ever use the same key twice.
985
986    A common misconception is that encryption, by itself, implies
987 authentication. Say that you and a friend share a secret key, and you
988 receive an encrypted message. You apply the key, and get a plaintext
989 message that makes sense to you. Can you then be sure that it really was
990 your friend that wrote the message you're reading? The answer is no. For
991 example, if you were using a block cipher in ECB mode, an attacker may
992 pick up the message on its way, and reorder, delete or repeat some of
993 the blocks. Even if the attacker can't decrypt the message, he can
994 change it so that you are not reading the same message as your friend
995 wrote. If you are using a block cipher in CBC mode rather than ECB, or
996 are using a stream cipher, the possibilities for this sort of attack
997 are different, but the attacker can still make predictable changes to
998 the message.
999
1000    It is recommended to _always_ use an authentication mechanism in
1001 addition to encrypting the messages. Popular choices are Message
1002 Authentication Codes like HMAC-SHA1 (*note Keyed hash functions::), or
1003 digital signatures like RSA.
1004
1005    Some ciphers have so called "weak keys", keys that results in
1006 undesirable structure after the key setup processing, and should be
1007 avoided. In Nettle, most key setup functions have no return value, but
1008 for ciphers with weak keys, the return value indicates whether or not
1009 the given key is weak. For good keys, key setup returns 1, and for weak
1010 keys, it returns 0. When possible, avoid algorithms that have weak
1011 keys. There are several good ciphers that don't have any weak keys.
1012
1013    To encrypt a message, you first initialize a cipher context for
1014 encryption or decryption with a particular key. You then use the context
1015 to process plaintext or ciphertext messages. The initialization is known
1016 as "key setup". With Nettle, it is recommended to use each context
1017 struct for only one direction, even if some of the ciphers use a single
1018 key setup function that can be used for both encryption and decryption.
1019
1020 6.2.1 AES
1021 ---------
1022
1023 AES is a block cipher, specified by NIST as a replacement for the older
1024 DES standard. The standard is the result of a competition between
1025 cipher designers. The winning design, also known as RIJNDAEL, was
1026 constructed by Joan Daemen and Vincent Rijnmen.
1027
1028    Like all the AES candidates, the winning design uses a block size of
1029 128 bits, or 16 octets, and variable key-size, 128, 192 and 256 bits
1030 (16, 24 and 32 octets) being the allowed key sizes. It does not have
1031 any weak keys. Nettle defines AES in `<nettle/aes.h>'.
1032
1033  -- Context struct: struct aes_ctx
1034
1035  -- Constant: AES_BLOCK_SIZE
1036      The AES block-size, 16.
1037
1038  -- Constant: AES_MIN_KEY_SIZE
1039
1040  -- Constant: AES_MAX_KEY_SIZE
1041
1042  -- Constant: AES_KEY_SIZE
1043      Default AES key size, 32.
1044
1045  -- Function: void aes_set_encrypt_key (struct aes_ctx *CTX, unsigned
1046           LENGTH, const uint8_t *KEY)
1047  -- Function: void aes_set_decrypt_key (struct aes_ctx *CTX, unsigned
1048           LENGTH, const uint8_t *KEY)
1049      Initialize the cipher, for encryption or decryption, respectively.
1050
1051  -- Function: void aes_invert_key (struct aes_ctx *DST, const struct
1052           aes_ctx *SRC)
1053      Given a context SRC initialized for encryption, initializes the
1054      context struct DST for decryption, using the same key. If the same
1055      context struct is passed for both `src' and `dst', it is converted
1056      in place. Calling `aes_set_encrypt_key' and `aes_invert_key' is
1057      more efficient than calling `aes_set_encrypt_key' and
1058      `aes_set_decrypt_key'. This function is mainly useful for
1059      applications which needs to both encrypt and decrypt using the
1060      _same_ key.
1061
1062  -- Function: void aes_encrypt (struct aes_ctx *CTX, unsigned LENGTH,
1063           uint8_t *DST, const uint8_t *SRC)
1064      Encryption function. LENGTH must be an integral multiple of the
1065      block size. If it is more than one block, the data is processed in
1066      ECB mode. `src' and `dst' may be equal, but they must not overlap
1067      in any other way.
1068
1069  -- Function: void aes_decrypt (struct aes_ctx *CTX, unsigned LENGTH,
1070           uint8_t *DST, const uint8_t *SRC)
1071      Analogous to `aes_encrypt'
1072
1073 6.2.2 ARCFOUR
1074 -------------
1075
1076 ARCFOUR is a stream cipher, also known under the trade marked name RC4,
1077 and it is one of the fastest ciphers around. A problem is that the key
1078 setup of ARCFOUR is quite weak, you should never use keys with
1079 structure, keys that are ordinary passwords, or sequences of keys like
1080 "secret:1", "secret:2", .... If you have keys that don't look like
1081 random bit strings, and you want to use ARCFOUR, always hash the key
1082 before feeding it to ARCFOUR. Furthermore, the initial bytes of the
1083 generated key stream leak information about the key; for this reason, it
1084 is recommended to discard the first 512 bytes of the key stream.
1085
1086      /* A more robust key setup function for ARCFOUR */
1087      void
1088      arcfour_set_key_hashed(struct arcfour_ctx *ctx,
1089                             unsigned length, const uint8_t *key)
1090      {
1091        struct sha256_ctx hash;
1092        uint8_t digest[SHA256_DIGEST_SIZE];
1093        uint8_t buffer[0x200];
1094
1095        sha256_init(&hash);
1096        sha256_update(&hash, length, key);
1097        sha256_digest(&hash, SHA256_DIGEST_SIZE, digest);
1098
1099        arcfour_set_key(ctx, SHA256_DIGEST_SIZE, digest);
1100        arcfour_crypt(ctx, sizeof(buffer), buffer, buffer);
1101      }
1102
1103    Nettle defines ARCFOUR in `<nettle/arcfour.h>'.
1104
1105  -- Context struct: struct arcfour_ctx
1106
1107  -- Constant: ARCFOUR_MIN_KEY_SIZE
1108      Minimum key size, 1.
1109
1110  -- Constant: ARCFOUR_MAX_KEY_SIZE
1111      Maximum key size, 256.
1112
1113  -- Constant: ARCFOUR_KEY_SIZE
1114      Default ARCFOUR key size, 16.
1115
1116  -- Function: void arcfour_set_key (struct arcfour_ctx *CTX, unsigned
1117           LENGTH, const uint8_t *KEY)
1118      Initialize the cipher. The same function is used for both
1119      encryption and decryption.
1120
1121  -- Function: void arcfour_crypt (struct arcfour_ctx *CTX, unsigned
1122           LENGTH, uint8_t *DST, const uint8_t *SRC)
1123      Encrypt some data. The same function is used for both encryption
1124      and decryption. Unlike the block ciphers, this function modifies
1125      the context, so you can split the data into arbitrary chunks and
1126      encrypt them one after another. The result is the same as if you
1127      had called `arcfour_crypt' only once with all the data.
1128
1129 6.2.3 ARCTWO
1130 ------------
1131
1132 ARCTWO (also known as the trade marked name RC2) is a block cipher
1133 specified in RFC 2268. Nettle also include a variation of the ARCTWO
1134 set key operation that lack one step, to be compatible with the reverse
1135 engineered RC2 cipher description, as described in a Usenet post to
1136 `sci.crypt' by Peter Gutmann.
1137
1138    ARCTWO uses a block size of 64 bits, and variable key-size ranging
1139 from 1 to 128 octets. Besides the key, ARCTWO also has a second
1140 parameter to key setup, the number of effective key bits, `ekb'.  This
1141 parameter can be used to artificially reduce the key size. In practice,
1142 `ekb' is usually set equal to the input key size.  Nettle defines
1143 ARCTWO in `<nettle/arctwo.h>'.
1144
1145    We do not recommend the use of ARCTWO; the Nettle implementation is
1146 provided primarily for interoperability with existing applications and
1147 standards.
1148
1149  -- Context struct: struct arctwo_ctx
1150
1151  -- Constant: ARCTWO_BLOCK_SIZE
1152      The ARCTWO block-size, 8.
1153
1154  -- Constant: ARCTWO_MIN_KEY_SIZE
1155
1156  -- Constant: ARCTWO_MAX_KEY_SIZE
1157
1158  -- Constant: ARCTWO_KEY_SIZE
1159      Default ARCTWO key size, 8.
1160
1161  -- Function: void arctwo_set_key_ekb (struct arctwo_ctx *CTX, unsigned
1162           LENGTH, const uint8_t *KEY, unsigned EKB)
1163  -- Function: void arctwo_set_key (struct arctwo_ctx *CTX, unsigned
1164           LENGTH, const uint8_t *KEY)
1165  -- Function: void arctwo_set_key_gutmann (struct arctwo_ctx *CTX,
1166           unsigned LENGTH, const uint8_t *KEY)
1167      Initialize the cipher. The same function is used for both
1168      encryption and decryption. The first function is the most general
1169      one, which lets you provide both the variable size key, and the
1170      desired effective key size (in bits). The maximum value for EKB is
1171      1024, and for convenience, `ekb = 0' has the same effect as `ekb =
1172      1024'.
1173
1174      `arctwo_set_key(ctx, length, key)' is equivalent to
1175      `arctwo_set_key_ekb(ctx, length, key, 8*length)', and
1176      `arctwo_set_key_gutmann(ctx, length, key)' is equivalent to
1177      `arctwo_set_key_ekb(ctx, length, key, 1024)'
1178
1179  -- Function: void arctwo_encrypt (struct arctwo_ctx *CTX, unsigned
1180           LENGTH, uint8_t *DST, const uint8_t *SRC)
1181      Encryption function. LENGTH must be an integral multiple of the
1182      block size. If it is more than one block, the data is processed in
1183      ECB mode. `src' and `dst' may be equal, but they must not overlap
1184      in any other way.
1185
1186  -- Function: void arctwo_decrypt (struct arctwo_ctx *CTX, unsigned
1187           LENGTH, uint8_t *DST, const uint8_t *SRC)
1188      Analogous to `arctwo_encrypt'
1189
1190 6.2.4 BLOWFISH
1191 --------------
1192
1193 BLOWFISH is a block cipher designed by Bruce Schneier. It uses a block
1194 size of 64 bits (8 octets), and a variable key size, up to 448 bits. It
1195 has some weak keys. Nettle defines BLOWFISH in `<nettle/blowfish.h>'.
1196
1197  -- Context struct: struct blowfish_ctx
1198
1199  -- Constant: BLOWFISH_BLOCK_SIZE
1200      The BLOWFISH block-size, 8.
1201
1202  -- Constant: BLOWFISH_MIN_KEY_SIZE
1203      Minimum BLOWFISH key size, 8.
1204
1205  -- Constant: BLOWFISH_MAX_KEY_SIZE
1206      Maximum BLOWFISH key size, 56.
1207
1208  -- Constant: BLOWFISH_KEY_SIZE
1209      Default BLOWFISH key size, 16.
1210
1211  -- Function: int blowfish_set_key (struct blowfish_ctx *CTX, unsigned
1212           LENGTH, const uint8_t *KEY)
1213      Initialize the cipher. The same function is used for both
1214      encryption and decryption. Checks for weak keys, returning 1 for
1215      good keys and 0 for weak keys. Applications that don't care about
1216      weak keys can ignore the return value.
1217
1218      `blowfish_encrypt' or `blowfish_decrypt' with a weak key will
1219      crash with an assert violation.
1220
1221  -- Function: void blowfish_encrypt (struct blowfish_ctx *CTX, unsigned
1222           LENGTH, uint8_t *DST, const uint8_t *SRC)
1223      Encryption function. LENGTH must be an integral multiple of the
1224      block size. If it is more than one block, the data is processed in
1225      ECB mode. `src' and `dst' may be equal, but they must not overlap
1226      in any other way.
1227
1228  -- Function: void blowfish_decrypt (struct blowfish_ctx *CTX, unsigned
1229           LENGTH, uint8_t *DST, const uint8_t *SRC)
1230      Analogous to `blowfish_encrypt'
1231
1232 6.2.5 Camellia
1233 --------------
1234
1235 Camellia is a block cipher developed by Mitsubishi and Nippon Telegraph
1236 and Telephone Corporation, described in `RFC3713', and recommended by
1237 some Japanese and European authorities as an alternative to AES. The
1238 algorithm is patented. The implementation in Nettle is derived from the
1239 implementation released by NTT under the GNU LGPL (v2.1 or later), and
1240 relies on the implicit patent license of the LGPL. There is also a
1241 statement of royalty-free licensing for Camellia at
1242 `http://www.ntt.co.jp/news/news01e/0104/010417.html', but this
1243 statement has some limitations which seem problematic for free software.
1244
1245    Camellia uses a the same block size and key sizes as AES: The block
1246 size is 128 bits (16 octets), and the supported key sizes are 128, 192,
1247 and 256 bits. Nettle defines Camellia in `<nettle/camellia.h>'.
1248
1249  -- Context struct: struct camellia_ctx
1250
1251  -- Constant: CAMELLIA_BLOCK_SIZE
1252      The CAMELLIA block-size, 16.
1253
1254  -- Constant: CAMELLIA_MIN_KEY_SIZE
1255
1256  -- Constant: CAMELLIA_MAX_KEY_SIZE
1257
1258  -- Constant: CAMELLIA_KEY_SIZE
1259      Default CAMELLIA key size, 32.
1260
1261  -- Function: void camellia_set_encrypt_key (struct camellia_ctx *CTX,
1262           unsigned LENGTH, const uint8_t *KEY)
1263  -- Function: void camellia_set_decrypt_key (struct camellia_ctx *CTX,
1264           unsigned LENGTH, const uint8_t *KEY)
1265      Initialize the cipher, for encryption or decryption, respectively.
1266
1267  -- Function: void camellia_invert_key (struct camellia_ctx *DST, const
1268           struct camellia_ctx *SRC)
1269      Given a context SRC initialized for encryption, initializes the
1270      context struct DST for decryption, using the same key. If the same
1271      context struct is passed for both `src' and `dst', it is converted
1272      in place. Calling `camellia_set_encrypt_key' and
1273      `camellia_invert_key' is more efficient than calling
1274      `camellia_set_encrypt_key' and `camellia_set_decrypt_key'. This
1275      function is mainly useful for applications which needs to both
1276      encrypt and decrypt using the _same_ key.
1277
1278  -- Function: void camellia_crypt (struct camellia_ctx *CTX, unsigned
1279           LENGTH, uint8_t *DST, const uint8_t *SRC)
1280      The same function is used for both encryption and decryption.
1281      LENGTH must be an integral multiple of the block size. If it is
1282      more than one block, the data is processed in ECB mode. `src' and
1283      `dst' may be equal, but they must not overlap in any other way.
1284
1285 6.2.6 CAST128
1286 -------------
1287
1288 CAST-128 is a block cipher, specified in `RFC 2144'. It uses a 64 bit
1289 (8 octets) block size, and a variable key size of up to 128 bits.
1290 Nettle defines cast128 in `<nettle/cast128.h>'.
1291
1292  -- Context struct: struct cast128_ctx
1293
1294  -- Constant: CAST128_BLOCK_SIZE
1295      The CAST128 block-size, 8.
1296
1297  -- Constant: CAST128_MIN_KEY_SIZE
1298      Minimum CAST128 key size, 5.
1299
1300  -- Constant: CAST128_MAX_KEY_SIZE
1301      Maximum CAST128 key size, 16.
1302
1303  -- Constant: CAST128_KEY_SIZE
1304      Default CAST128 key size, 16.
1305
1306  -- Function: void cast128_set_key (struct cast128_ctx *CTX, unsigned
1307           LENGTH, const uint8_t *KEY)
1308      Initialize the cipher. The same function is used for both
1309      encryption and decryption.
1310
1311  -- Function: void cast128_encrypt (struct cast128_ctx *CTX, unsigned
1312           LENGTH, uint8_t *DST, const uint8_t *SRC)
1313      Encryption function. LENGTH must be an integral multiple of the
1314      block size. If it is more than one block, the data is processed in
1315      ECB mode. `src' and `dst' may be equal, but they must not overlap
1316      in any other way.
1317
1318  -- Function: void cast128_decrypt (struct cast128_ctx *CTX, unsigned
1319           LENGTH, uint8_t *DST, const uint8_t *SRC)
1320      Analogous to `cast128_encrypt'
1321
1322 6.2.7 DES
1323 ---------
1324
1325 DES is the old Data Encryption Standard, specified by NIST. It uses a
1326 block size of 64 bits (8 octets), and a key size of 56 bits. However,
1327 the key bits are distributed over 8 octets, where the least significant
1328 bit of each octet may be used for parity. A common way to use DES is to
1329 generate 8 random octets in some way, then set the least significant bit
1330 of each octet to get odd parity, and initialize DES with the resulting
1331 key.
1332
1333    The key size of DES is so small that keys can be found by brute
1334 force, using specialized hardware or lots of ordinary work stations in
1335 parallel. One shouldn't be using plain DES at all today, if one uses
1336 DES at all one should be using "triple DES", see DES3 below.
1337
1338    DES also has some weak keys. Nettle defines DES in `<nettle/des.h>'.
1339
1340  -- Context struct: struct des_ctx
1341
1342  -- Constant: DES_BLOCK_SIZE
1343      The DES block-size, 8.
1344
1345  -- Constant: DES_KEY_SIZE
1346      DES key size, 8.
1347
1348  -- Function: int des_set_key (struct des_ctx *CTX, const uint8_t *KEY)
1349      Initialize the cipher. The same function is used for both
1350      encryption and decryption. Parity bits are ignored. Checks for
1351      weak keys, returning 1 for good keys and 0 for weak keys.
1352      Applications that don't care about weak keys can ignore the return
1353      value.
1354
1355  -- Function: void des_encrypt (struct des_ctx *CTX, unsigned LENGTH,
1356           uint8_t *DST, const uint8_t *SRC)
1357      Encryption function. LENGTH must be an integral multiple of the
1358      block size. If it is more than one block, the data is processed in
1359      ECB mode. `src' and `dst' may be equal, but they must not overlap
1360      in any other way.
1361
1362  -- Function: void des_decrypt (struct des_ctx *CTX, unsigned LENGTH,
1363           uint8_t *DST, const uint8_t *SRC)
1364      Analogous to `des_encrypt'
1365
1366  -- Function: int des_check_parity (unsigned LENGTH, const uint8_t
1367           *KEY);
1368      Checks that the given key has correct, odd, parity. Returns 1 for
1369      correct parity, and 0 for bad parity.
1370
1371  -- Function: void des_fix_parity (unsigned LENGTH, uint8_t *DST, const
1372           uint8_t *SRC)
1373      Adjusts the parity bits to match DES's requirements. You need this
1374      function if you have created a random-looking string by a key
1375      agreement protocol, and want to use it as a DES key. DST and SRC
1376      may be equal.
1377
1378 6.2.8 DES3
1379 ----------
1380
1381 The inadequate key size of DES has already been mentioned. One way to
1382 increase the key size is to pipe together several DES boxes with
1383 independent keys. It turns out that using two DES ciphers is not as
1384 secure as one might think, even if the key size of the combination is a
1385 respectable 112 bits.
1386
1387    The standard way to increase DES's key size is to use three DES
1388 boxes.  The mode of operation is a little peculiar: the middle DES box
1389 is wired in the reverse direction. To encrypt a block with DES3, you
1390 encrypt it using the first 56 bits of the key, then _decrypt_ it using
1391 the middle 56 bits of the key, and finally encrypt it again using the
1392 last 56 bits of the key. This is known as "ede" triple-DES, for
1393 "encrypt-decrypt-encrypt".
1394
1395    The "ede" construction provides some backward compatibility, as you
1396 get plain single DES simply by feeding the same key to all three boxes.
1397 That should help keeping down the gate count, and the price, of hardware
1398 circuits implementing both plain DES and DES3.
1399
1400    DES3 has a key size of 168 bits, but just like plain DES, useless
1401 parity bits are inserted, so that keys are represented as 24 octets
1402 (192 bits).  As a 112 bit key is large enough to make brute force
1403 attacks impractical, some applications uses a "two-key" variant of
1404 triple-DES.  In this mode, the same key bits are used for the first and
1405 the last DES box in the pipe, while the middle box is keyed
1406 independently. The two-key variant is believed to be secure, i.e. there
1407 are no known attacks significantly better than brute force.
1408
1409    Naturally, it's simple to implement triple-DES on top of Nettle's DES
1410 functions. Nettle includes an implementation of three-key "ede"
1411 triple-DES, it is defined in the same place as plain DES,
1412 `<nettle/des.h>'.
1413
1414  -- Context struct: struct des3_ctx
1415
1416  -- Constant: DES3_BLOCK_SIZE
1417      The DES3 block-size is the same as DES_BLOCK_SIZE, 8.
1418
1419  -- Constant: DES3_KEY_SIZE
1420      DES key size, 24.
1421
1422  -- Function: int des3_set_key (struct des3_ctx *CTX, const uint8_t
1423           *KEY)
1424      Initialize the cipher. The same function is used for both
1425      encryption and decryption. Parity bits are ignored. Checks for
1426      weak keys, returning 1 if all three keys are good keys, and 0 if
1427      one or more key is weak.  Applications that don't care about weak
1428      keys can ignore the return value.
1429
1430    For random-looking strings, you can use `des_fix_parity' to adjust
1431 the parity bits before calling `des3_set_key'.
1432
1433  -- Function: void des3_encrypt (struct des3_ctx *CTX, unsigned LENGTH,
1434           uint8_t *DST, const uint8_t *SRC)
1435      Encryption function. LENGTH must be an integral multiple of the
1436      block size. If it is more than one block, the data is processed in
1437      ECB mode. `src' and `dst' may be equal, but they must not overlap
1438      in any other way.
1439
1440  -- Function: void des3_decrypt (struct des3_ctx *CTX, unsigned LENGTH,
1441           uint8_t *DST, const uint8_t *SRC)
1442      Analogous to `des_encrypt'
1443
1444 6.2.9 Salsa20
1445 -------------
1446
1447 Salsa20 is a fairly recent stream cipher designed by D. J. Bernstein. It
1448 is built on the observation that a cryptographic hash function can be
1449 used for encryption: Form the hash input from the secret key and a
1450 counter, xor the hash output and the first block of the plaintext, then
1451 increment the counter to process the next block (similar to CTR mode,
1452 see *note CTR::). Bernstein defined an encryption algorithm, Snuffle,
1453 in this way to ridicule United States export restrictions which treated
1454 hash functions as nice and harmless, but ciphers as dangerous munitions.
1455
1456    Salsa20 uses the same idea, but with a new specialized hash function
1457 to mix key, block counter, and a couple of constants. It's also designed
1458 for speed; on x86_64, it is currently the fastest cipher offered by
1459 nettle. It uses a block size of 512 bits (64 octets) and there are two
1460 specified key sizes, 128 and 256 bits (16 and 32 octets).
1461
1462    *Caution:* The hash function used in Salsa20 is _not_ directly
1463 applicable for use as a general hash function. It's _not_ collision
1464 resistant if arbitrary inputs are allowed, and furthermore, the input
1465 and output is of fixed size.
1466
1467    When using Salsa20 to process a message, one specifies both a key
1468 and a "nonce", the latter playing a similar rôle to the initialization
1469 vector (IV) used with CBC or CTR mode. For this reason, Nettle uses the
1470 term IV to refer to the Salsa20 nonce. One can use the same key for
1471 several messages, provided one uses a unique random iv for each
1472 message. The iv is 64 bits (8 octets). The block counter is initialized
1473 to zero for each message, and is also 64 bits (8 octets). Nettle
1474 defines Salsa20 in `<nettle/salsa20.h>'.
1475
1476  -- Context struct: struct salsa20_ctx
1477
1478  -- Constant: SALSA20_MIN_KEY_SIZE
1479  -- Constant: SALSA20_MAX_KEY_SIZE
1480      The two supported key sizes, 16 and 32 octets.
1481
1482  -- Constant: SALSA20_KEY_SIZE
1483      Recommended key size, 32.
1484
1485  -- Constant: SALSA20_BLOCK_SIZE
1486      Salsa20 block size, 64.
1487
1488  -- Constant: SALSA20_IV_SIZE
1489      Size of the IV, 8.
1490
1491  -- Function: void salsa20_set_key (struct salsa20_ctx *CTX, unsigned
1492           LENGTH, const uint8_t *KEY)
1493      Initialize the cipher. The same function is used for both
1494      encryption and decryption. Before using the cipher, you _must_
1495      also call `salsa20_set_iv', see below.
1496
1497  -- Function: void salsa20_set_iv (struct salsa20_ctx *CTX, const
1498           uint8_t *IV)
1499      Sets the IV. It is always of size `SALSA20_IV_SIZE', 8 octets.
1500      This function also initializes the block counter, setting it to
1501      zero.
1502
1503  -- Function: void salsa20_crypt (struct salsa20_ctx *CTX, unsigned
1504           LENGTH, uint8_t *DST, const uint8_t *SRC)
1505      Encrypts or decrypts the data of a message, using salsa20. When a
1506      message is encrypted using a sequence of calls to `salsa20_crypt',
1507      all but the last call _must_ use a length that is a multiple of
1508      `SALSA20_BLOCK_SIZE'.
1509
1510    The full salsa20 cipher uses 20 rounds of mixing. Variants of Salsa20
1511 with fewer rounds are possible, and the 12-round variant is specified by
1512 eSTREAM, see `http://www.ecrypt.eu.org/stream/finallist.html'.  Nettle
1513 calls this variant `salsa20r12'. It uses the same context struct and
1514 key setup as the full salsa20 cipher, but a separate function for
1515 encryption and decryption.
1516
1517  -- Function: void salsa20r12_crypt (struct salsa20_ctx *CTX, unsigned
1518           LENGTH, uint8_t *DST, const uint8_t *SRC)
1519      Encrypts or decrypts the data of a message, using salsa20 reduced
1520      to 12 rounds.
1521
1522 6.2.10 SERPENT
1523 --------------
1524
1525 SERPENT is one of the AES finalists, designed by Ross Anderson, Eli
1526 Biham and Lars Knudsen. Thus, the interface and properties are similar
1527 to AES'. One peculiarity is that it is quite pointless to use it with
1528 anything but the maximum key size, smaller keys are just padded to
1529 larger ones. Nettle defines SERPENT in `<nettle/serpent.h>'.
1530
1531  -- Context struct: struct serpent_ctx
1532
1533  -- Constant: SERPENT_BLOCK_SIZE
1534      The SERPENT block-size, 16.
1535
1536  -- Constant: SERPENT_MIN_KEY_SIZE
1537      Minimum SERPENT key size, 16.
1538
1539  -- Constant: SERPENT_MAX_KEY_SIZE
1540      Maximum SERPENT key size, 32.
1541
1542  -- Constant: SERPENT_KEY_SIZE
1543      Default SERPENT key size, 32.
1544
1545  -- Function: void serpent_set_key (struct serpent_ctx *CTX, unsigned
1546           LENGTH, const uint8_t *KEY)
1547      Initialize the cipher. The same function is used for both
1548      encryption and decryption.
1549
1550  -- Function: void serpent_encrypt (struct serpent_ctx *CTX, unsigned
1551           LENGTH, uint8_t *DST, const uint8_t *SRC)
1552      Encryption function. LENGTH must be an integral multiple of the
1553      block size. If it is more than one block, the data is processed in
1554      ECB mode. `src' and `dst' may be equal, but they must not overlap
1555      in any other way.
1556
1557  -- Function: void serpent_decrypt (struct serpent_ctx *CTX, unsigned
1558           LENGTH, uint8_t *DST, const uint8_t *SRC)
1559      Analogous to `serpent_encrypt'
1560
1561 6.2.11 TWOFISH
1562 --------------
1563
1564 Another AES finalist, this one designed by Bruce Schneier and others.
1565 Nettle defines it in `<nettle/twofish.h>'.
1566
1567  -- Context struct: struct twofish_ctx
1568
1569  -- Constant: TWOFISH_BLOCK_SIZE
1570      The TWOFISH block-size, 16.
1571
1572  -- Constant: TWOFISH_MIN_KEY_SIZE
1573      Minimum TWOFISH key size, 16.
1574
1575  -- Constant: TWOFISH_MAX_KEY_SIZE
1576      Maximum TWOFISH key size, 32.
1577
1578  -- Constant: TWOFISH_KEY_SIZE
1579      Default TWOFISH key size, 32.
1580
1581  -- Function: void twofish_set_key (struct twofish_ctx *CTX, unsigned
1582           LENGTH, const uint8_t *KEY)
1583      Initialize the cipher. The same function is used for both
1584      encryption and decryption.
1585
1586  -- Function: void twofish_encrypt (struct twofish_ctx *CTX, unsigned
1587           LENGTH, uint8_t *DST, const uint8_t *SRC)
1588      Encryption function. LENGTH must be an integral multiple of the
1589      block size. If it is more than one block, the data is processed in
1590      ECB mode. `src' and `dst' may be equal, but they must not overlap
1591      in any other way.
1592
1593  -- Function: void twofish_decrypt (struct twofish_ctx *CTX, unsigned
1594           LENGTH, uint8_t *DST, const uint8_t *SRC)
1595      Analogous to `twofish_encrypt'
1596
1597 6.2.12 `struct nettle_cipher'
1598 -----------------------------
1599
1600 Nettle includes a struct including information about some of the more
1601 regular cipher functions. It should be considered a little experimental,
1602 but can be useful for applications that need a simple way to handle
1603 various algorithms. Nettle defines these structs in
1604 `<nettle/nettle-meta.h>'.
1605
1606  -- Meta struct: `struct nettle_cipher' name context_size block_size
1607           key_size set_encrypt_key set_decrypt_key encrypt decrypt
1608      The last four attributes are function pointers, of types
1609      `nettle_set_key_func' and `nettle_crypt_func'. The first argument
1610      to these functions is a `void *' pointer to a context struct,
1611      which is of size `context_size'.
1612
1613  -- Constant Struct: struct nettle_cipher nettle_aes128
1614  -- Constant Struct: struct nettle_cipher nettle_aes192
1615  -- Constant Struct: struct nettle_cipher nettle_aes256
1616  -- Constant Struct: struct nettle_cipher nettle_arctwo40
1617  -- Constant Struct: struct nettle_cipher nettle_arctwo64
1618  -- Constant Struct: struct nettle_cipher nettle_arctwo128
1619  -- Constant Struct: struct nettle_cipher nettle_arctwo_gutmann128
1620  -- Constant Struct: struct nettle_cipher nettle_arcfour128
1621  -- Constant Struct: struct nettle_cipher nettle_camellia128
1622  -- Constant Struct: struct nettle_cipher nettle_camellia192
1623  -- Constant Struct: struct nettle_cipher nettle_camellia256
1624  -- Constant Struct: struct nettle_cipher nettle_cast128
1625  -- Constant Struct: struct nettle_cipher nettle_serpent128
1626  -- Constant Struct: struct nettle_cipher nettle_serpent192
1627  -- Constant Struct: struct nettle_cipher nettle_serpent256
1628  -- Constant Struct: struct nettle_cipher nettle_twofish128
1629  -- Constant Struct: struct nettle_cipher nettle_twofish192
1630  -- Constant Struct: struct nettle_cipher nettle_twofish256
1631      Nettle includes such structs for all the _regular_ ciphers, i.e.
1632      ones without weak keys or other oddities.
1633
1634    Nettle also exports a list of all these ciphers without weak keys or
1635 other oddities.
1636
1637  -- Constant Array: struct nettle_cipher ** nettle_ciphers
1638      This list can be used to dynamically enumerate or search the
1639      supported algorithms. NULL-terminated.
1640
1641 \1f
1642 File: nettle.info,  Node: Cipher modes,  Next: Keyed hash functions,  Prev: Cipher functions,  Up: Reference
1643
1644 6.3 Cipher modes
1645 ================
1646
1647 Cipher modes of operation specifies the procedure to use when encrypting
1648 a message that is larger than the cipher's block size. As explained in
1649 *Note Cipher functions::, splitting the message into blocks and
1650 processing them independently with the block cipher (Electronic Code
1651 Book mode, ECB) leaks information. Besides ECB, Nettle provides three
1652 other modes of operation: Cipher Block Chaining (CBC), Counter mode
1653 (CTR), and Galois/Counter mode (GCM). CBC is widely used, but there are
1654 a few subtle issues of information leakage, see, e.g., SSH CBC
1655 vulnerability (http://www.kb.cert.org/vuls/id/958563). CTR and GCM were
1656 standardized more recently, and are believed to be more secure.  GCM
1657 includes message authentication; for the other modes, one should always
1658 use a MAC (*note Keyed hash functions::) or signature to authenticate
1659 the message.
1660
1661 * Menu:
1662
1663 * CBC::
1664 * CTR::
1665 * GCM::
1666
1667 \1f
1668 File: nettle.info,  Node: CBC,  Next: CTR,  Prev: Cipher modes,  Up: Cipher modes
1669
1670 6.3.1 Cipher Block Chaining
1671 ---------------------------
1672
1673 When using CBC mode, plaintext blocks are not encrypted independently
1674 of each other, like in Electronic Cook Book mode. Instead, when
1675 encrypting a block in CBC mode, the previous ciphertext block is XORed
1676 with the plaintext before it is fed to the block cipher.  When
1677 encrypting the first block, a random block called an "IV", or
1678 Initialization Vector, is used as the "previous ciphertext block". The
1679 IV should be chosen randomly, but it need not be kept secret, and can
1680 even be transmitted in the clear together with the encrypted data.
1681
1682    In symbols, if `E_k' is the encryption function of a block cipher,
1683 and `IV' is the initialization vector, then `n' plaintext blocks
1684 `M_1',... `M_n' are transformed into `n' ciphertext blocks `C_1',...
1685 `C_n' as follows:
1686
1687      C_1 = E_k(IV  XOR M_1)
1688      C_2 = E_k(C_1 XOR M_2)
1689
1690      ...
1691
1692      C_n = E_k(C_(n-1) XOR M_n)
1693
1694    Nettle's includes two functions for applying a block cipher in Cipher
1695 Block Chaining (CBC) mode, one for encryption and one for decryption.
1696 These functions uses `void *' to pass cipher contexts around.
1697
1698  -- Function: void cbc_encrypt (void *CTX, nettle_crypt_func F,
1699           unsigned BLOCK_SIZE, uint8_t *IV, unsigned LENGTH, uint8_t
1700           *DST, const uint8_t *SRC)
1701  -- Function: void cbc_decrypt (void *CTX, void (*F)(), unsigned
1702           BLOCK_SIZE, uint8_t *IV, unsigned LENGTH, uint8_t *DST, const
1703           uint8_t *SRC)
1704      Applies the encryption or decryption function F in CBC mode. The
1705      final ciphertext block processed is copied into IV before
1706      returning, so that large message be processed be a sequence of
1707      calls to `cbc_encrypt'. The function F is of type
1708
1709      `void f (void *CTX, unsigned LENGTH, uint8_t DST, const uint8_t
1710      *SRC)',
1711
1712      and the `cbc_encrypt' and `cbc_decrypt' functions pass their
1713      argument CTX on to F.
1714
1715    There are also some macros to help use these functions correctly.
1716
1717  -- Macro: CBC_CTX (CONTEXT_TYPE, BLOCK_SIZE)
1718      Expands to
1719           {
1720              context_type ctx;
1721              uint8_t iv[block_size];
1722           }
1723
1724    It can be used to define a CBC context struct, either directly,
1725
1726      struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) ctx;
1727
1728    or to give it a struct tag,
1729
1730      struct aes_cbc_ctx CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE);
1731
1732  -- Macro: CBC_SET_IV (CTX, IV)
1733      First argument is a pointer to a context struct as defined by
1734      `CBC_CTX', and the second is a pointer to an Initialization Vector
1735      (IV) that is copied into that context.
1736
1737  -- Macro: CBC_ENCRYPT (CTX, F, LENGTH, DST, SRC)
1738  -- Macro: CBC_DECRYPT (CTX, F, LENGTH, DST, SRC)
1739      A simpler way to invoke `cbc_encrypt' and `cbc_decrypt'. The first
1740      argument is a pointer to a context struct as defined by `CBC_CTX',
1741      and the second argument is an encryption or decryption function
1742      following Nettle's conventions. The last three arguments define
1743      the source and destination area for the operation.
1744
1745    These macros use some tricks to make the compiler display a warning
1746 if the types of F and CTX don't match, e.g. if you try to use an
1747 `struct aes_ctx' context with the `des_encrypt' function.
1748
1749 \1f
1750 File: nettle.info,  Node: CTR,  Next: GCM,  Prev: CBC,  Up: Cipher modes
1751
1752 6.3.2 Counter mode
1753 ------------------
1754
1755 Counter mode (CTR) uses the block cipher as a keyed pseudo-random
1756 generator. The output of the generator is XORed with the data to be
1757 encrypted. It can be understood as a way to transform a block cipher to
1758 a stream cipher.
1759
1760    The message is divided into `n' blocks `M_1',...  `M_n', where `M_n'
1761 is of size `m' which may be smaller than the block size. Except for the
1762 last block, all the message blocks must be of size equal to the
1763 cipher's block size.
1764
1765    If `E_k' is the encryption function of a block cipher, `IC' is the
1766 initial counter, then the `n' plaintext blocks are transformed into `n'
1767 ciphertext blocks `C_1',...  `C_n' as follows:
1768
1769      C_1 = E_k(IC) XOR M_1
1770      C_2 = E_k(IC + 1) XOR M_2
1771
1772      ...
1773
1774      C_(n-1) = E_k(IC + n - 2) XOR M_(n-1)
1775      C_n = E_k(IC + n - 1) [1..m] XOR M_n
1776
1777    The IC is the initial value for the counter, it plays a similar rôle
1778 as the IV for CBC. When adding, `IC + x', IC is interpreted as an
1779 integer, in network byte order. For the last block, `E_k(IC + n - 1)
1780 [1..m]' means that the cipher output is truncated to `m' bytes.
1781
1782  -- Function: void ctr_crypt (void *CTX, nettle_crypt_func F, unsigned
1783           BLOCK_SIZE, uint8_t *CTR, unsigned LENGTH, uint8_t *DST,
1784           const uint8_t *SRC)
1785      Applies the encryption function F in CTR mode. Note that for CTR
1786      mode, encryption and decryption is the same operation, and hence F
1787      should always be the encryption function for the underlying block
1788      cipher.
1789
1790      When a message is encrypted using a sequence of calls to
1791      `ctr_crypt', all but the last call _must_ use a length that is a
1792      multiple of the block size.
1793
1794    Like for CBC, there are also a couple of helper macros.
1795
1796  -- Macro: CTR_CTX (CONTEXT_TYPE, BLOCK_SIZE)
1797      Expands to
1798           {
1799              context_type ctx;
1800              uint8_t ctr[block_size];
1801           }
1802
1803  -- Macro: CTR_SET_COUNTER (CTX, IV)
1804      First argument is a pointer to a context struct as defined by
1805      `CTR_CTX', and the second is a pointer to an initial counter that
1806      is copied into that context.
1807
1808  -- Macro: CTR_CRYPT (CTX, F, LENGTH, DST, SRC)
1809      A simpler way to invoke `ctr_crypt'. The first argument is a
1810      pointer to a context struct as defined by `CTR_CTX', and the second
1811      argument is an encryption function following Nettle's conventions.
1812      The last three arguments define the source and destination area
1813      for the operation.
1814
1815 \1f
1816 File: nettle.info,  Node: GCM,  Prev: CTR,  Up: Cipher modes
1817
1818 6.3.3 Galois counter mode
1819 -------------------------
1820
1821 Galois counter mode is the combination of counter mode with message
1822 authentication based on universal hashing. The main objective of the
1823 design is to provide high performance for hardware implementations,
1824 where other popular MAC algorithms (*note Keyed hash functions::
1825 becomes a bottleneck for high-speed hardware implementations.  It was
1826 proposed by David A. McGrew and John Viega in 2005, and recommended by
1827 NIST in 2007, NIST Special Publication 800-38D
1828 (http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf). It
1829 is constructed on top of a block cipher which must have a block size of
1830 128 bits.
1831
1832    GCM is applied to messages of arbitrary length. The inputs are:
1833
1834    * A key, which can be used for many messages.
1835
1836    * An initialization vector (IV) which _must_ be unique for each
1837      message.
1838
1839    * Additional authenticated data, which is to be included in the
1840      message authentication, but not encrypted. May be empty.
1841
1842    * The plaintext. Maybe empty.
1843
1844    The outputs are a ciphertext, of the same length as the plaintext,
1845 and a message digest of length 128 bits. Nettle's support for GCM
1846 consists of a low-level general interface, some convenience macros, and
1847 specific functions for GCM using AES as the underlying cipher. These
1848 interfaces are defined in `<nettle/gcm.h>'
1849
1850 6.3.3.1 General GCM interface
1851 .............................
1852
1853  -- Context struct: struct gcm_key
1854      Message independent hash sub-key, and related tables.
1855
1856  -- Context struct: struct gcm_ctx
1857      Holds state corresponding to a particular message.
1858
1859  -- Constant: GCM_BLOCK_SIZE
1860      GCM's block size, 16.
1861
1862  -- Constant: GCM_IV_SIZE
1863      Recommended size of the IV, 12. Other sizes are allowed.
1864
1865  -- Function: void gcm_set_key (struct gcm_key *KEY, void *CIPHER,
1866           nettle_crypt_func *F)
1867      Initializes KEY. CIPHER gives a context struct for the underlying
1868      cipher, which must have been previously initialized for
1869      encryption, and F is the encryption function.
1870
1871  -- Function: void gcm_set_iv (struct gcm_ctx *CTX, const struct
1872           gcm_key *KEY, unsigned LENGTH, const uint8_t *IV)
1873      Initializes CTX using the given IV. The KEY argument is actually
1874      needed only if LENGTH differs from `GCM_IV_SIZE'.
1875
1876  -- Function: void gcm_update (struct gcm_ctx *CTX, const struct
1877           gcm_key *KEY, unsigned LENGTH, const uint8_t *DATA)
1878      Provides associated data to be authenticated. If used, must be
1879      called before `gcm_encrypt' or `gcm_decrypt'. All but the last call
1880      for each message _must_ use a length that is a multiple of the
1881      block size.
1882
1883  -- Function: void gcm_encrypt (struct gcm_ctx *CTX, const struct
1884           gcm_key *KEY void *CIPHER, nettle_crypt_func *F, unsigned
1885           LENGTH, uint8_t *DST, const uint8_t *SRC)
1886  -- Function: void gcm_decrypt (struct gcm_ctx *CTX, const struct
1887           gcm_key *KEY, void *CIPHER, nettle_crypt_func *F, unsigned
1888           LENGTH, uint8_t *DST, const uint8_t *SRC)
1889      Encrypts or decrypts the data of a message. CIPHER is the context
1890      struct for the underlying cipher and F is the encryption function.
1891      All but the last call for each message _must_ use a length that is
1892      a multiple of the block size.
1893
1894  -- Function: void gcm_digest (struct gcm_ctx *CTX, const struct
1895           gcm_key *KEY, void *CIPHER, nettle_crypt_func *F, unsigned
1896           LENGTH, uint8_t *DIGEST)
1897      Extracts the message digest (also known "authentication tag").
1898      This is the final operation when processing a message. LENGTH is
1899      usually equal to `GCM_BLOCK_SIZE', but if you provide a smaller
1900      value, only the first LENGTH octets of the digest are written.
1901
1902    To encrypt a message using GCM, first initialize a context for the
1903 underlying block cipher with a key to use for encryption. Then call the
1904 above functions in the following order: `gcm_set_key', `gcm_set_iv',
1905 `gcm_update', `gcm_encrypt', `gcm_digest'. The decryption procedure is
1906 analogous, just calling `gcm_decrypt' instead of `gcm_encrypt' (note
1907 that GCM decryption still uses the encryption function of the
1908 underlying block cipher). To process a new message, using the same key,
1909 call `gcm_set_iv' with a new iv.
1910
1911 6.3.3.2 GCM helper macros
1912 .........................
1913
1914 The following macros are defined.
1915
1916  -- Macro: GCM_CTX (CONTEXT_TYPE)
1917      This defines an all-in-one context struct, including the context
1918      of the underlying cipher, the hash sub-key, and the per-message
1919      state. It expands to
1920           {
1921              context_type cipher;
1922              struct gcm_key key;
1923              struct gcm_ctx gcm;
1924           }
1925
1926    Example use:
1927      struct gcm_aes_ctx GCM_CTX(struct aes_ctx);
1928
1929    The following macros operate on context structs of this form.
1930
1931  -- Macro: GCM_SET_KEY (CTX, SET_KEY, ENCRYPT, LENGTH, DATA)
1932      First argument, CTX, is a context struct as defined by `GCM_CTX'.
1933      SET_KEY and ENCRYPT are functions for setting the encryption key
1934      and for encrypting data using the underlying cipher. LENGTH and
1935      DATA give the key.
1936
1937  -- Macro: GCM_SET_IV (CTX, LENGTH, DATA)
1938      First argument is a context struct as defined by `GCM_CTX'. LENGTH
1939      and DATA give the initialization vector (IV).
1940
1941  -- Macro: GCM_UPDATE (CTX, LENGTH, DATA)
1942      Simpler way to call `gcm_update'. First argument is a context
1943      struct as defined by `GCM_CTX'
1944
1945  -- Macro: GCM_ENCRYPT (CTX, ENCRYPT, LENGTH, DST, SRC)
1946  -- Macro: GCM_DECRYPT (CTX, ENCRYPT, LENGTH, DST, SRC)
1947  -- Macro: GCM_DIGEST (CTX, ENCRYPT, LENGTH, DIGEST)
1948      Simpler way to call `gcm_encrypt', `gcm_decrypt' or `gcm_digest'.
1949      First argument is a context struct as defined by `GCM_CTX'. Second
1950      argument, ENCRYPT, is a pointer to the encryption function of the
1951      underlying cipher.
1952
1953 6.3.3.3 GCM-AES interface
1954 .........................
1955
1956 The following functions implement the common case of GCM using AES as
1957 the underlying cipher.
1958
1959  -- Context struct: struct gcm_aes_ctx
1960      The context struct, defined using `GCM_CTX'.
1961
1962  -- Function: void gcm_aes_set_key (struct gcm_aes_ctx *CTX, unsigned
1963           LENGTH, const uint8_t *KEY)
1964      Initializes CTX using the given key. All valid AES key sizes can
1965      be used.
1966
1967  -- Function: void gcm_aes_set_iv (struct gcm_aes_ctx *CTX, unsigned
1968           LENGTH, const uint8_t *IV)
1969      Initializes the per-message state, using the given IV.
1970
1971  -- Function: void gcm_aes_update (struct gcm_aes_ctx *CTX, unsigned
1972           LENGTH, const uint8_t *DATA)
1973      Provides associated data to be authenticated. If used, must be
1974      called before `gcm_aes_encrypt' or `gcm_aes_decrypt'. All but the
1975      last call for each message _must_ use a length that is a multiple
1976      of the block size.
1977
1978  -- Function: void gcm_aes_encrypt (struct gcm_aes_ctx *CTX, unsigned
1979           LENGTH, uint8_t *DST, const uint8_t *SRC)
1980  -- Function: void gcm_aes_decrypt (struct gcm_aes_ctx *CTX, unsigned
1981           LENGTH, uint8_t *DST, const uint8_t *SRC)
1982      Encrypts or decrypts the data of a message. All but the last call
1983      for each message _must_ use a length that is a multiple of the
1984      block size.
1985
1986
1987  -- Function: void gcm_aes_digest (struct gcm_aes_ctx *CTX, unsigned
1988           LENGTH, uint8_t *DIGEST)
1989      Extracts the message digest (also known "authentication tag").
1990      This is the final operation when processing a message. LENGTH is
1991      usually equal to `GCM_BLOCK_SIZE', but if you provide a smaller
1992      value, only the first LENGTH octets of the digest are written.
1993
1994 \1f
1995 File: nettle.info,  Node: Keyed hash functions,  Next: Key derivation functions,  Prev: Cipher modes,  Up: Reference
1996
1997 6.4 Keyed Hash Functions
1998 ========================
1999
2000 A "keyed hash function", or "Message Authentication Code" (MAC) is a
2001 function that takes a key and a message, and produces fixed size MAC.
2002 It should be hard to compute a message and a matching MAC without
2003 knowledge of the key. It should also be hard to compute the key given
2004 only messages and corresponding MACs.
2005
2006    Keyed hash functions are useful primarily for message authentication,
2007 when Alice and Bob shares a secret: The sender, Alice, computes the MAC
2008 and attaches it to the message. The receiver, Bob, also computes the
2009 MAC of the message, using the same key, and compares that to Alice's
2010 value. If they match, Bob can be assured that the message has not been
2011 modified on its way from Alice.
2012
2013    However, unlike digital signatures, this assurance is not
2014 transferable.  Bob can't show the message and the MAC to a third party
2015 and prove that Alice sent that message. Not even if he gives away the
2016 key to the third party. The reason is that the _same_ key is used on
2017 both sides, and anyone knowing the key can create a correct MAC for any
2018 message. If Bob believes that only he and Alice knows the key, and he
2019 knows that he didn't attach a MAC to a particular message, he knows it
2020 must be Alice who did it. However, the third party can't distinguish
2021 between a MAC created by Alice and one created by Bob.
2022
2023    Keyed hash functions are typically a lot faster than digital
2024 signatures as well.
2025
2026 6.4.1 HMAC
2027 ----------
2028
2029 One can build keyed hash functions from ordinary hash functions. Older
2030 constructions simply concatenate secret key and message and hashes
2031 that, but such constructions have weaknesses. A better construction is
2032 HMAC, described in `RFC 2104'.
2033
2034    For an underlying hash function `H', with digest size `l' and
2035 internal block size `b', HMAC-H is constructed as follows: From a given
2036 key `k', two distinct subkeys `k_i' and `k_o' are constructed, both of
2037 length `b'. The HMAC-H of a message `m' is then computed as `H(k_o |
2038 H(k_i | m))', where `|' denotes string concatenation.
2039
2040    HMAC keys can be of any length, but it is recommended to use keys of
2041 length `l', the digest size of the underlying hash function `H'. Keys
2042 that are longer than `b' are shortened to length `l' by hashing with
2043 `H', so arbitrarily long keys aren't very useful.
2044
2045    Nettle's HMAC functions are defined in `<nettle/hmac.h>'.  There are
2046 abstract functions that use a pointer to a `struct nettle_hash' to
2047 represent the underlying hash function and `void *' pointers that point
2048 to three different context structs for that hash function. There are
2049 also concrete functions for HMAC-MD5, HMAC-RIPEMD160 HMAC-SHA1,
2050 HMAC-SHA256, and HMAC-SHA512. First, the abstract functions:
2051
2052  -- Function: void hmac_set_key (void *OUTER, void *INNER, void *STATE,
2053           const struct nettle_hash *H, unsigned LENGTH, const uint8_t
2054           *KEY)
2055      Initializes the three context structs from the key. The OUTER and
2056      INNER contexts corresponds to the subkeys `k_o' and `k_i'. STATE
2057      is used for hashing the message, and is initialized as a copy of
2058      the INNER context.
2059
2060  -- Function: void hmac_update (void *STATE, const struct nettle_hash
2061           *H, unsigned LENGTH, const uint8_t *DATA)
2062      This function is called zero or more times to process the message.
2063      Actually, `hmac_update(state, H, length, data)' is equivalent to
2064      `H->update(state, length, data)', so if you wish you can use the
2065      ordinary update function of the underlying hash function instead.
2066
2067  -- Function: void hmac_digest (const void *OUTER, const void *INNER,
2068           void *STATE, const struct nettle_hash *H, unsigned LENGTH,
2069           uint8_t *DIGEST)
2070      Extracts the MAC of the message, writing it to DIGEST.  OUTER and
2071      INNER are not modified. LENGTH is usually equal to
2072      `H->digest_size', but if you provide a smaller value, only the
2073      first LENGTH octets of the MAC are written.
2074
2075      This function also resets the STATE context so that you can start
2076      over processing a new message (with the same key).
2077
2078    Like for CBC, there are some macros to help use these functions
2079 correctly.
2080
2081  -- Macro: HMAC_CTX (TYPE)
2082      Expands to
2083           {
2084              type outer;
2085              type inner;
2086              type state;
2087           }
2088
2089    It can be used to define a HMAC context struct, either directly,
2090
2091      struct HMAC_CTX(struct md5_ctx) ctx;
2092
2093    or to give it a struct tag,
2094
2095      struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
2096
2097  -- Macro: HMAC_SET_KEY (CTX, H, LENGTH, KEY)
2098      CTX is a pointer to a context struct as defined by `HMAC_CTX', H
2099      is a pointer to a `const struct nettle_hash' describing the
2100      underlying hash function (so it must match the type of the
2101      components of CTX). The last two arguments specify the secret key.
2102
2103  -- Macro: HMAC_DIGEST (CTX, H, LENGTH, DIGEST)
2104      CTX is a pointer to a context struct as defined by `HMAC_CTX', H
2105      is a pointer to a `const struct nettle_hash' describing the
2106      underlying hash function. The last two arguments specify where the
2107      digest is written.
2108
2109    Note that there is no `HMAC_UPDATE' macro; simply call `hmac_update'
2110 function directly, or the update function of the underlying hash
2111 function.
2112
2113 6.4.2 Concrete HMAC functions
2114 -----------------------------
2115
2116 Now we come to the specialized HMAC functions, which are easier to use
2117 than the general HMAC functions.
2118
2119 6.4.2.1 HMAC-MD5
2120 ................
2121
2122  -- Context struct: struct hmac_md5_ctx
2123
2124  -- Function: void hmac_md5_set_key (struct hmac_md5_ctx *CTX, unsigned
2125           KEY_LENGTH, const uint8_t *KEY)
2126      Initializes the context with the key.
2127
2128  -- Function: void hmac_md5_update (struct hmac_md5_ctx *CTX, unsigned
2129           LENGTH, const uint8_t *DATA)
2130      Process some more data.
2131
2132  -- Function: void hmac_md5_digest (struct hmac_md5_ctx *CTX, unsigned
2133           LENGTH, uint8_t *DIGEST)
2134      Extracts the MAC, writing it to DIGEST. LENGTH may be smaller than
2135      `MD5_DIGEST_SIZE', in which case only the first LENGTH octets of
2136      the MAC are written.
2137
2138      This function also resets the context for processing new messages,
2139      with the same key.
2140
2141 6.4.2.2 HMAC-RIPEMD160
2142 ......................
2143
2144  -- Context struct: struct hmac_ripemd160_ctx
2145
2146  -- Function: void hmac_ripemd160_set_key (struct hmac_ripemd160_ctx
2147           *CTX, unsigned KEY_LENGTH, const uint8_t *KEY)
2148      Initializes the context with the key.
2149
2150  -- Function: void hmac_ripemd160_update (struct hmac_ripemd160_ctx
2151           *CTX, unsigned LENGTH, const uint8_t *DATA)
2152      Process some more data.
2153
2154  -- Function: void hmac_ripemd160_digest (struct hmac_ripemd160_ctx
2155           *CTX, unsigned LENGTH, uint8_t *DIGEST)
2156      Extracts the MAC, writing it to DIGEST. LENGTH may be smaller than
2157      `RIPEMD160_DIGEST_SIZE', in which case only the first LENGTH
2158      octets of the MAC are written.
2159
2160      This function also resets the context for processing new messages,
2161      with the same key.
2162
2163 6.4.2.3 HMAC-SHA1
2164 .................
2165
2166  -- Context struct: struct hmac_sha1_ctx
2167
2168  -- Function: void hmac_sha1_set_key (struct hmac_sha1_ctx *CTX,
2169           unsigned KEY_LENGTH, const uint8_t *KEY)
2170      Initializes the context with the key.
2171
2172  -- Function: void hmac_sha1_update (struct hmac_sha1_ctx *CTX,
2173           unsigned LENGTH, const uint8_t *DATA)
2174      Process some more data.
2175
2176  -- Function: void hmac_sha1_digest (struct hmac_sha1_ctx *CTX,
2177           unsigned LENGTH, uint8_t *DIGEST)
2178      Extracts the MAC, writing it to DIGEST. LENGTH may be smaller than
2179      `SHA1_DIGEST_SIZE', in which case only the first LENGTH octets of
2180      the MAC are written.
2181
2182      This function also resets the context for processing new messages,
2183      with the same key.
2184
2185 6.4.2.4 HMAC-SHA256
2186 ...................
2187
2188  -- Context struct: struct hmac_sha256_ctx
2189
2190  -- Function: void hmac_sha256_set_key (struct hmac_sha256_ctx *CTX,
2191           unsigned KEY_LENGTH, const uint8_t *KEY)
2192      Initializes the context with the key.
2193
2194  -- Function: void hmac_sha256_update (struct hmac_sha256_ctx *CTX,
2195           unsigned LENGTH, const uint8_t *DATA)
2196      Process some more data.
2197
2198  -- Function: void hmac_sha256_digest (struct hmac_sha256_ctx *CTX,
2199           unsigned LENGTH, uint8_t *DIGEST)
2200      Extracts the MAC, writing it to DIGEST. LENGTH may be smaller than
2201      `SHA256_DIGEST_SIZE', in which case only the first LENGTH octets
2202      of the MAC are written.
2203
2204      This function also resets the context for processing new messages,
2205      with the same key.
2206
2207 6.4.2.5 HMAC-SHA512
2208 ...................
2209
2210  -- Context struct: struct hmac_sha512_ctx
2211
2212  -- Function: void hmac_sha512_set_key (struct hmac_sha512_ctx *CTX,
2213           unsigned KEY_LENGTH, const uint8_t *KEY)
2214      Initializes the context with the key.
2215
2216  -- Function: void hmac_sha512_update (struct hmac_sha512_ctx *CTX,
2217           unsigned LENGTH, const uint8_t *DATA)
2218      Process some more data.
2219
2220  -- Function: void hmac_sha512_digest (struct hmac_sha512_ctx *CTX,
2221           unsigned LENGTH, uint8_t *DIGEST)
2222      Extracts the MAC, writing it to DIGEST. LENGTH may be smaller than
2223      `SHA512_DIGEST_SIZE', in which case only the first LENGTH octets
2224      of the MAC are written.
2225
2226      This function also resets the context for processing new messages,
2227      with the same key.
2228
2229 6.4.3 UMAC
2230 ----------
2231
2232 UMAC is a message authentication code based on universal hashing, and
2233 designed for high performance on modern processors (in contrast to GCM,
2234 *Note GCM::, which is designed primarily for hardware performance). On
2235 processors with good integer multiplication performance, it can be 10
2236 times faster than SHA256 and SHA512.  UMAC is specified in `RFC 4418'.
2237
2238    The secret key is always 128 bits (16 octets). The key is used as an
2239 encryption key for the AES block cipher. This cipher is used in counter
2240 mode to generate various internal subkeys needed in UMAC. Messages are
2241 of arbitrary size, and for each message, UMAC also needs a unique
2242 nonce. Nonce values must not be reused for two messages with the same
2243 key, but they need not be kept secret.
2244
2245    The nonce must be at least one octet, and at most 16; nonces shorter
2246 than 16 octets are zero-padded. Nettle's implementation of UMAC
2247 increments the nonce for automatically each message, so explicitly
2248 setting the nonce for each message is optional. This auto-increment
2249 uses network byte order and it takes the length of the nonce into
2250 acount. E.g., if the initial nonce is "abc" (3 octets), this value is
2251 zero-padded to 16 octets for the first message. For the next message,
2252 the nonce is incremented to "abd", and this incremented value is
2253 zero-padded to 16 octets.
2254
2255    UMAC is defined in four variants, for different output sizes: 32
2256 bits (4 octest), 64 bits (8 octets), 96 bits (12 octets) and 128 bits
2257 (16 octets), corresponding to different tradeoffs between speed and
2258 security. Using a shorter output size sometimes (but not always!) gives
2259 the same result as using a longer output size and truncating the result.
2260 So it is important to use the right variant. For consistency with other
2261 hash and MAC functions, Nettle's `_digest' functions for UMAC accept a
2262 length parameter so that the output can be truncated to any desired
2263 size, but it is recommended to stick to the specified output size and
2264 select the umac variant corresponding to the desired size.
2265
2266    The internal block size of UMAC is 1024 octets, and it also
2267 generates more than 1024 bytes of subkeys. This makes the size of the
2268 context struct a bit larger than other hash functions and MAC
2269 algorithms in Nettle.
2270
2271    Nettle defines UMAC in `<nettle/umac.h>'.
2272
2273  -- Context struct: struct umac32_ctx
2274  -- Context struct: struct umac64_ctx
2275  -- Context struct: struct umac96_ctx
2276  -- Context struct: struct umac128_ctx
2277      Each UMAC variant uses its own context struct.
2278
2279  -- Constant: UMAC_KEY_SIZE
2280      The UMAC key size, 16.
2281
2282  -- Constant: UMAC32_DIGEST_SIZE
2283      The size of an UMAC32 digest, 4.
2284
2285  -- Constant: UMAC64_DIGEST_SIZE
2286      The size of an UMAC64 digest, 8.
2287
2288  -- Constant: UMAC96_DIGEST_SIZE
2289      The size of an UMAC96 digest, 12.
2290
2291  -- Constant: UMAC128_DIGEST_SIZE
2292      The size of an UMAC128 digest, 16.
2293
2294  -- Constant: UMAC128_DATA_SIZE
2295      The internal block size of UMAC.
2296
2297  -- Function: void umac32_set_key (struct umac32_ctx *CTX, const
2298           uint8_t *KEY)
2299  -- Function: void umac64_set_key (struct umac64_ctx *CTX, const
2300           uint8_t *KEY)
2301  -- Function: void umac96_set_key (struct umac96_ctx *CTX, const
2302           uint8_t *KEY)
2303  -- Function: void umac128_set_key (struct umac128_ctx *CTX, const
2304           uint8_t *KEY)
2305      These functions initialize the UMAC context struct. They also
2306      initialize the nonce to zero (with length 16, for auto-increment).
2307
2308  -- Function: void umac32_set_nonce (struct umac32_ctx *CTX, unsigned
2309           LENGTH, const uint8_t *NONCE)
2310  -- Function: void umac64_set_nonce (struct umac64_ctx *CTX, unsigned
2311           LENGTH, const uint8_t *NONCE)
2312  -- Function: void umac96_set_nonce (struct umac96_ctx *CTX, unsigned
2313           LENGTH, const uint8_t *NONCE)
2314  -- Function: void umac128_set_nonce (struct umac128_ctx *CTX, unsigned
2315           LENGTH, const uint8_t *NONCE)
2316      Sets the nonce to be used for the next message. In general, nonces
2317      should be set before processing of the message. This is not
2318      strictly required for UMAC (the nonce only affects the final
2319      processing generating the digest), but it is nevertheless
2320      recommended that this function is called _before_ the first
2321      `_update' call for the message.
2322
2323  -- Function: void umac32_update (struct umac32_ctx *CTX, unsigned
2324           LENGTH, const uint8_t *DATA)
2325  -- Function: void umac64_update (struct umac64_ctx *CTX, unsigned
2326           LENGTH, const uint8_t *DATA)
2327  -- Function: void umac96_update (struct umac96_ctx *CTX, unsigned
2328           LENGTH, const uint8_t *DATA)
2329  -- Function: void umac128_update (struct umac128_ctx *CTX, unsigned
2330           LENGTH, const uint8_t *DATA)
2331      These functions are called zero or more times to process the
2332      message.
2333
2334  -- Function: void umac32_digest (struct umac32_ctx *CTX, unsigned
2335           LENGTH, uint8_t *DIGEST)
2336  -- Function: void umac64_digest (struct umac64_ctx *CTX, unsigned
2337           LENGTH, uint8_t *DIGEST)
2338  -- Function: void umac96_digest (struct umac96_ctx *CTX, unsigned
2339           LENGTH, uint8_t *DIGEST)
2340  -- Function: void umac128_digest (struct umac128_ctx *CTX, unsigned
2341           LENGTH, uint8_t *DIGEST)
2342      Extracts the MAC of the message, writing it to DIGEST.  LENGTH is
2343      usually equal to the specified output size, but if you provide a
2344      smaller value, only the first LENGTH octets of the MAC are
2345      written. These functions reset the context for processing of a new
2346      message with the same key. The nonce is incremented as described
2347      above, the new value is used unless you call the `_set_nonce'
2348      function explicitly for each message.
2349
2350 \1f
2351 File: nettle.info,  Node: Key derivation functions,  Next: Public-key algorithms,  Prev: Keyed hash functions,  Up: Reference
2352
2353 6.5 Key derivation Functions
2354 ============================
2355
2356 A "key derivation function" (KDF) is a function that from a given
2357 symmetric key derives other symmetric keys.  A sub-class of KDFs is the
2358 "password-based key derivation functions" (PBKDFs), which take as input
2359 a password or passphrase, and its purpose is typically to strengthen it
2360 and protect against certain pre-computation attacks by using salting
2361 and expensive computation.
2362
2363 6.5.1 PBKDF2
2364 ------------
2365
2366 The most well known PBKDF is the `PKCS #5 PBKDF2' described in `RFC
2367 2898' which uses a pseudo-random function such as HMAC-SHA1.
2368
2369    Nettle's PBKDF2 functions are defined in `<nettle/pbkdf2.h>'.  There
2370 is an abstract function that operate on any PRF implemented via the
2371 `nettle_hash_update_func', `nettle_hash_digest_func' interfaces.  There
2372 is also helper macros and concrete functions PBKDF2-HMAC-SHA1 and
2373 PBKDF2-HMAC-SHA256.  First, the abstract function:
2374
2375  -- Function: void pbkdf2 (void *mac_ctx, nettle_hash_update_func
2376           *update, nettle_hash_digest_func *digest, unsigned
2377           digest_size, unsigned iterations, unsigned salt_length, const
2378           uint8_t *salt, unsigned length, uint8_t *dst)
2379      Derive symmetric key from a password according to PKCS #5 PBKDF2.
2380      The PRF is assumed to have been initialized and this function will
2381      call the UPDATE and DIGEST functions passing the MAC_CTX context
2382      parameter as an argument in order to compute digest of size
2383      DIGEST_SIZE.  Inputs are the salt SALT of length SALT_LENGTH, the
2384      iteration counter ITERATIONS (> 0), and the desired derived output
2385      length LENGTH.  The output buffer is DST which must have room for
2386      at least LENGTH octets.
2387
2388    Like for CBC and HMAC, there is a macro to help use the function
2389 correctly.
2390
2391  -- Macro: PBKDF2 (CTX, UPDATE, DIGEST, DIGEST_SIZE, ITERATIONS,
2392           SALT_LENGTH, SALT, LENGTH, DST)
2393      CTX is a pointer to a context struct passed to the UPDATE and
2394      DIGEST functions (of the types `nettle_hash_update_func' and
2395      `nettle_hash_digest_func' respectively) to implement the
2396      underlying PRF with digest size of DIGEST_SIZE.  Inputs are the
2397      salt SALT of length SALT_LENGTH, the iteration counter ITERATIONS
2398      (> 0), and the desired derived output length LENGTH.  The output
2399      buffer is DST which must have room for at least LENGTH octets.
2400
2401 6.5.2 Concrete PBKDF2 functions
2402 -------------------------------
2403
2404 Now we come to the specialized PBKDF2 functions, which are easier to
2405 use than the general PBKDF2 function.
2406
2407 6.5.2.1 PBKDF2-HMAC-SHA1
2408 ........................
2409
2410  -- Function: void pbkdf2_hmac_sha1 (unsigned KEY_LENGTH, const uint8_t
2411           *KEY, unsigned ITERATIONS, unsigned SALT_LENGTH, const
2412           uint8_t *SALT, unsigned LENGTH, uint8_t *DST)
2413      PBKDF2 with HMAC-SHA1.  Derive LENGTH bytes of key into buffer DST
2414      using the password KEY of length KEY_LENGTH and salt SALT of
2415      length SALT_LENGTH, with iteration counter ITERATIONS (> 0).  The
2416      output buffer is DST which must have room for at least LENGTH
2417      octets.
2418
2419 6.5.2.2 PBKDF2-HMAC-SHA256
2420 ..........................
2421
2422  -- Function: void pbkdf2_hmac_sha256 (unsigned KEY_LENGTH, const
2423           uint8_t *KEY, unsigned ITERATIONS, unsigned SALT_LENGTH,
2424           const uint8_t *SALT, unsigned LENGTH, uint8_t *DST)
2425      PBKDF2 with HMAC-SHA256.  Derive LENGTH bytes of key into buffer
2426      DST using the password KEY of length KEY_LENGTH and salt SALT of
2427      length SALT_LENGTH, with iteration counter ITERATIONS (> 0).  The
2428      output buffer is DST which must have room for at least LENGTH
2429      octets.
2430
2431 \1f
2432 File: nettle.info,  Node: Public-key algorithms,  Next: Randomness,  Prev: Key derivation functions,  Up: Reference
2433
2434 6.6 Public-key algorithms
2435 =========================
2436
2437 Nettle uses GMP, the GNU bignum library, for all calculations with
2438 large numbers. In order to use the public-key features of Nettle, you
2439 must install GMP, at least version 3.0, before compiling Nettle, and
2440 you need to link your programs with `-lhogweed -lnettle -lgmp'.
2441
2442    The concept of "Public-key" encryption and digital signatures was
2443 discovered by Whitfield Diffie and Martin E. Hellman and described in a
2444 paper 1976. In traditional, "symmetric", cryptography, sender and
2445 receiver share the same keys, and these keys must be distributed in a
2446 secure way. And if there are many users or entities that need to
2447 communicate, each _pair_ needs a shared secret key known by nobody else.
2448
2449    Public-key cryptography uses trapdoor one-way functions. A "one-way
2450 function" is a function `F' such that it is easy to compute the value
2451 `F(x)' for any `x', but given a value `y', it is hard to compute a
2452 corresponding `x' such that `y = F(x)'. Two examples are cryptographic
2453 hash functions, and exponentiation in certain groups.
2454
2455    A "trapdoor one-way function" is a function `F' that is one-way,
2456 unless one knows some secret information about `F'. If one knows the
2457 secret, it is easy to compute both `F' and it's inverse.  If this
2458 sounds strange, look at the RSA example below.
2459
2460    Two important uses for one-way functions with trapdoors are
2461 public-key encryption, and digital signatures. The public-key
2462 encryption functions in Nettle are not yet documented; the rest of this
2463 chapter is about digital signatures.
2464
2465    To use a digital signature algorithm, one must first create a
2466 "key-pair": A public key and a corresponding private key. The private
2467 key is used to sign messages, while the public key is used for verifying
2468 that that signatures and messages match. Some care must be taken when
2469 distributing the public key; it need not be kept secret, but if a bad
2470 guy is able to replace it (in transit, or in some user's list of known
2471 public keys), bad things may happen.
2472
2473    There are two operations one can do with the keys. The signature
2474 operation takes a message and a private key, and creates a signature for
2475 the message. A signature is some string of bits, usually at most a few
2476 thousand bits or a few hundred octets. Unlike paper-and-ink signatures,
2477 the digital signature depends on the message, so one can't cut it out of
2478 context and glue it to a different message.
2479
2480    The verification operation takes a public key, a message, and a
2481 string that is claimed to be a signature on the message, and returns
2482 true or false. If it returns true, that means that the three input
2483 values matched, and the verifier can be sure that someone went through
2484 with the signature operation on that very message, and that the
2485 "someone" also knows the private key corresponding to the public key.
2486
2487    The desired properties of a digital signature algorithm are as
2488 follows: Given the public key and pairs of messages and valid
2489 signatures on them, it should be hard to compute the private key, and
2490 it should also be hard to create a new message and signature that is
2491 accepted by the verification operation.
2492
2493    Besides signing meaningful messages, digital signatures can be used
2494 for authorization. A server can be configured with a public key, such
2495 that any client that connects to the service is given a random nonce
2496 message.  If the server gets a reply with a correct signature matching
2497 the nonce message and the configured public key, the client is granted
2498 access. So the configuration of the server can be understood as "grant
2499 access to whoever knows the private key corresponding to this
2500 particular public key, and to no others".
2501
2502 * Menu:
2503
2504 * RSA::                         The RSA public key algorithm.
2505 * DSA::                         The DSA digital signature algorithm.
2506 * Elliptic curves::             Elliptic curves and ECDSA
2507
2508 \1f
2509 File: nettle.info,  Node: RSA,  Next: DSA,  Prev: Public-key algorithms,  Up: Public-key algorithms
2510
2511 6.6.1 RSA
2512 ---------
2513
2514 The RSA algorithm was the first practical digital signature algorithm
2515 that was constructed. It was described 1978 in a paper by Ronald
2516 Rivest, Adi Shamir and L.M. Adleman, and the technique was also
2517 patented in the USA in 1983. The patent expired on September 20, 2000,
2518 and since that day, RSA can be used freely, even in the USA.
2519
2520    It's remarkably simple to describe the trapdoor function behind RSA.
2521 The "one-way"-function used is
2522
2523      F(x) = x^e mod n
2524
2525    I.e. raise x to the `e''th power, while discarding all multiples of
2526 `n'. The pair of numbers `n' and `e' is the public key.  `e' can be
2527 quite small, even `e = 3' has been used, although slightly larger
2528 numbers are recommended. `n' should be about 1000 bits or larger.
2529
2530    If `n' is large enough, and properly chosen, the inverse of F, the
2531 computation of `e''th roots modulo `n', is very difficult.  But,
2532 where's the trapdoor?
2533
2534    Let's first look at how RSA key-pairs are generated. First `n' is
2535 chosen as the product of two large prime numbers `p' and `q' of roughly
2536 the same size (so if `n' is 1000 bits, `p' and `q' are about 500 bits
2537 each). One also computes the number `phi = (p-1)(q-1)', in mathematical
2538 speak, `phi' is the order of the multiplicative group of integers
2539 modulo n.
2540
2541    Next, `e' is chosen. It must have no factors in common with `phi' (in
2542 particular, it must be odd), but can otherwise be chosen more or less
2543 randomly. `e = 65537' is a popular choice, because it makes raising to
2544 the `e''th power particularly efficient, and being prime, it usually
2545 has no factors common with `phi'.
2546
2547    Finally, a number `d', `d < n' is computed such that `e d mod phi =
2548 1'. It can be shown that such a number exists (this is why `e' and
2549 `phi' must have no common factors), and that for all x,
2550
2551      (x^e)^d mod n = x^(ed) mod n = (x^d)^e mod n = x
2552
2553    Using Euclid's algorithm, `d' can be computed quite easily from
2554 `phi' and `e'. But it is still hard to get `d' without knowing `phi',
2555 which depends on the factorization of `n'.
2556
2557    So `d' is the trapdoor, if we know `d' and `y = F(x)', we can
2558 recover x as `y^d mod n'. `d' is also the private half of the RSA
2559 key-pair.
2560
2561    The most common signature operation for RSA is defined in `PKCS#1',
2562 a specification by RSA Laboratories. The message to be signed is first
2563 hashed using a cryptographic hash function, e.g.  MD5 or SHA1. Next,
2564 some padding, the ASN.1 "Algorithm Identifier" for the hash function,
2565 and the message digest itself, are concatenated and converted to a
2566 number `x'. The signature is computed from `x' and the private key as
2567 `s = x^d mod n'(1) (*note RSA-Footnote-1::). The signature, `s' is a
2568 number of about the same size of `n', and it usually encoded as a
2569 sequence of octets, most significant octet first.
2570
2571    The verification operation is straight-forward, `x' is computed from
2572 the message in the same way as above. Then `s^e mod n' is computed, the
2573 operation returns true if and only if the result equals `x'.
2574
2575 6.6.2 Nettle's RSA support
2576 --------------------------
2577
2578 Nettle represents RSA keys using two structures that contain large
2579 numbers (of type `mpz_t').
2580
2581  -- Context struct: rsa_public_key size n e
2582      `size' is the size, in octets, of the modulo, and is used
2583      internally.  `n' and `e' is the public key.
2584
2585  -- Context struct: rsa_private_key size d p q a b c
2586      `size' is the size, in octets, of the modulo, and is used
2587      internally.  `d' is the secret exponent, but it is not actually
2588      used when signing. Instead, the factors `p' and `q', and the
2589      parameters `a', `b' and `c' are used. They are computed from `p',
2590      `q' and `e' such that `a e mod (p - 1) = 1, b e mod (q - 1) = 1, c
2591      q mod p = 1'.
2592
2593    Before use, these structs must be initialized by calling one of
2594
2595  -- Function: void rsa_public_key_init (struct rsa_public_key *PUB)
2596  -- Function: void rsa_private_key_init (struct rsa_private_key *KEY)
2597      Calls `mpz_init' on all numbers in the key struct.
2598
2599    and when finished with them, the space for the numbers must be
2600 deallocated by calling one of
2601
2602  -- Function: void rsa_public_key_clear (struct rsa_public_key *PUB)
2603  -- Function: void rsa_private_key_clear (struct rsa_private_key *KEY)
2604      Calls `mpz_clear' on all numbers in the key struct.
2605
2606    In general, Nettle's RSA functions deviates from Nettle's "no memory
2607 allocation"-policy. Space for all the numbers, both in the key structs
2608 above, and temporaries, are allocated dynamically. For information on
2609 how to customize allocation, see *Note GMP Allocation: (gmp)Custom
2610 Allocation.
2611
2612    When you have assigned values to the attributes of a key, you must
2613 call
2614
2615  -- Function: int rsa_public_key_prepare (struct rsa_public_key *PUB)
2616  -- Function: int rsa_private_key_prepare (struct rsa_private_key *KEY)
2617      Computes the octet size of the key (stored in the `size' attribute,
2618      and may also do other basic sanity checks. Returns one if
2619      successful, or zero if the key can't be used, for instance if the
2620      modulo is smaller than the minimum size needed for RSA operations
2621      specified by PKCS#1.
2622
2623    Before signing or verifying a message, you first hash it with the
2624 appropriate hash function. You pass the hash function's context struct
2625 to the RSA signature function, and it will extract the message digest
2626 and do the rest of the work. There are also alternative functions that
2627 take the hash digest as argument.
2628
2629    There is currently no support for using SHA224 or SHA384 with RSA
2630 signatures, since there's no gain in either computation time nor
2631 message size compared to using SHA256 and SHA512, respectively.
2632
2633    Creation and verification of signatures is done with the following
2634 functions:
2635
2636  -- Function: int rsa_md5_sign (const struct rsa_private_key *KEY,
2637           struct md5_ctx *HASH, mpz_t SIGNATURE)
2638  -- Function: int rsa_sha1_sign (const struct rsa_private_key *KEY,
2639           struct sha1_ctx *HASH, mpz_t SIGNATURE)
2640  -- Function: int rsa_sha256_sign (const struct rsa_private_key *KEY,
2641           struct sha256_ctx *HASH, mpz_t SIGNATURE)
2642  -- Function: int rsa_sha512_sign (const struct rsa_private_key *KEY,
2643           struct sha512_ctx *HASH, mpz_t SIGNATURE)
2644      The signature is stored in SIGNATURE (which must have been
2645      `mpz_init''ed earlier). The hash context is reset so that it can be
2646      used for new messages. Returns one on success, or zero on failure.
2647      Signing fails if the key is too small for the given hash size,
2648      e.g., it's not possible to create a signature using SHA512 and a
2649      512-bit RSA key.
2650
2651  -- Function: int rsa_md5_sign_digest (const struct rsa_private_key
2652           *KEY, const uint8_t *DIGEST, mpz_t SIGNATURE)
2653  -- Function: int rsa_sha1_sign_digest (const struct rsa_private_key
2654           *KEY, const uint8_t *DIGEST, mpz_t SIGNATURE);
2655  -- Function: int rsa_sha256_sign_digest (const struct rsa_private_key
2656           *KEY, const uint8_t *DIGEST, mpz_t SIGNATURE);
2657  -- Function: int rsa_sha512_sign_digest (const struct rsa_private_key
2658           *KEY, const uint8_t *DIGEST, mpz_t SIGNATURE);
2659      Creates a signature from the given hash digest. DIGEST should
2660      point to a digest of size `MD5_DIGEST_SIZE', `SHA1_DIGEST_SIZE',
2661      or `SHA256_DIGEST_SIZE', respectively. The signature is stored in
2662      SIGNATURE (which must have been `mpz_init':ed earlier). Returns
2663      one on success, or zero on failure.
2664
2665  -- Function: int rsa_md5_verify (const struct rsa_public_key *KEY,
2666           struct md5_ctx *HASH, const mpz_t SIGNATURE)
2667  -- Function: int rsa_sha1_verify (const struct rsa_public_key *KEY,
2668           struct sha1_ctx *HASH, const mpz_t SIGNATURE)
2669  -- Function: int rsa_sha256_verify (const struct rsa_public_key *KEY,
2670           struct sha256_ctx *HASH, const mpz_t SIGNATURE)
2671  -- Function: int rsa_sha512_verify (const struct rsa_public_key *KEY,
2672           struct sha512_ctx *HASH, const mpz_t SIGNATURE)
2673      Returns 1 if the signature is valid, or 0 if it isn't. In either
2674      case, the hash context is reset so that it can be used for new
2675      messages.
2676
2677  -- Function: int rsa_md5_verify_digest (const struct rsa_public_key
2678           *KEY, const uint8_t *DIGEST, const mpz_t SIGNATURE)
2679  -- Function: int rsa_sha1_verify_digest (const struct rsa_public_key
2680           *KEY, const uint8_t *DIGEST, const mpz_t SIGNATURE)
2681  -- Function: int rsa_sha256_verify_digest (const struct rsa_public_key
2682           *KEY, const uint8_t *DIGEST, const mpz_t SIGNATURE)
2683  -- Function: int rsa_sha512_verify_digest (const struct rsa_public_key
2684           *KEY, const uint8_t *DIGEST, const mpz_t SIGNATURE)
2685      Returns 1 if the signature is valid, or 0 if it isn't. DIGEST
2686      should point to a digest of size `MD5_DIGEST_SIZE',
2687      `SHA1_DIGEST_SIZE', or `SHA256_DIGEST_SIZE', respectively.
2688
2689    If you need to use the RSA trapdoor, the private key, in a way that
2690 isn't supported by the above functions Nettle also includes a function
2691 that computes `x^d mod n' and nothing more, using the CRT optimization.
2692
2693  -- Function: void rsa_compute_root (struct rsa_private_key *KEY, mpz_t
2694           X, const mpz_t M)
2695      Computes `x = m^d', efficiently.
2696
2697    At last, how do you create new keys?
2698
2699  -- Function: int rsa_generate_keypair (struct rsa_public_key *PUB,
2700           struct rsa_private_key *KEY, void *RANDOM_CTX,
2701           nettle_random_func RANDOM, void *PROGRESS_CTX,
2702           nettle_progress_func PROGRESS, unsigned N_SIZE, unsigned
2703           E_SIZE);
2704      There are lots of parameters. PUB and KEY is where the resulting
2705      key pair is stored. The structs should be initialized, but you
2706      don't need to call `rsa_public_key_prepare' or
2707      `rsa_private_key_prepare' after key generation.
2708
2709      RANDOM_CTX and RANDOM is a randomness generator.
2710      `random(random_ctx, length, dst)' should generate `length' random
2711      octets and store them at `dst'. For advice, see *Note Randomness::.
2712
2713      PROGRESS and PROGRESS_CTX can be used to get callbacks during the
2714      key generation process, in order to uphold an illusion of
2715      progress. PROGRESS can be NULL, in that case there are no
2716      callbacks.
2717
2718      SIZE_N is the desired size of the modulo, in bits. If SIZE_E is
2719      non-zero, it is the desired size of the public exponent and a
2720      random exponent of that size is selected. But if E_SIZE is zero,
2721      it is assumed that the caller has already chosen a value for `e',
2722      and stored it in PUB.  Returns one on success, and zero on
2723      failure. The function can fail for example if if N_SIZE is too
2724      small, or if E_SIZE is zero and `pub->e' is an even number.
2725
2726 \1f
2727 File: nettle.info,  Node: RSA-Footnotes,  Up: RSA
2728
2729    (1) Actually, the computation is not done like this, it is done more
2730 efficiently using `p', `q' and the Chinese remainder theorem (CRT). But
2731 the result is the same.
2732
2733 \1f
2734 File: nettle.info,  Node: DSA,  Next: Elliptic curves,  Prev: RSA,  Up: Public-key algorithms
2735
2736 6.6.3 DSA
2737 ---------
2738
2739 The DSA digital signature algorithm is more complex than RSA. It was
2740 specified during the early 1990s, and in 1994 NIST published FIPS 186
2741 which is the authoritative specification.  Sometimes DSA is referred to
2742 using the acronym DSS, for Digital Signature Standard. The most recent
2743 revision of the specification, FIPS186-3, was issued in 2009, and it
2744 adds support for larger hash functions than sha1.
2745
2746    For DSA, the underlying mathematical problem is the computation of
2747 discrete logarithms. The public key consists of a large prime `p', a
2748 small prime `q' which is a factor of `p-1', a number `g' which
2749 generates a subgroup of order `q' modulo `p', and an element `y' in
2750 that subgroup.
2751
2752    In the original DSA, the size of `q' is fixed to 160 bits, to match
2753 with the SHA1 hash algorithm. The size of `p' is in principle
2754 unlimited, but the standard specifies only nine specific sizes: `512 +
2755 l*64', where `l' is between 0 and 8. Thus, the maximum size of `p' is
2756 1024 bits, and sizes less than 1024 bits are considered obsolete and not
2757 secure.
2758
2759    The subgroup requirement means that if you compute
2760
2761      g^t mod p
2762
2763    for all possible integers `t', you will get precisely `q' distinct
2764 values.
2765
2766    The private key is a secret exponent `x', such that
2767
2768      g^x = y mod p
2769
2770    In mathematical speak, `x' is the "discrete logarithm" of `y' mod
2771 `p', with respect to the generator `g'. The size of `x' will also be
2772 about the same size as `q'. The security of the DSA algorithm relies on
2773 the difficulty of the discrete logarithm problem. Current algorithms to
2774 compute discrete logarithms in this setting, and hence crack DSA, are
2775 of two types. The first type works directly in the (multiplicative)
2776 group of integers mod `p'. The best known algorithm of this type is the
2777 Number Field Sieve, and it's complexity is similar to the complexity of
2778 factoring numbers of the same size as `p'. The other type works in the
2779 smaller `q'-sized subgroup generated by `g', which has a more difficult
2780 group structure. One good algorithm is Pollard-rho, which has
2781 complexity `sqrt(q)'.
2782
2783    The important point is that security depends on the size of _both_
2784 `p' and `q', and they should be chosen so that the difficulty of both
2785 discrete logarithm methods are comparable. Today, the security margin
2786 of the original DSA may be uncomfortably small. Using a `p' of 1024
2787 bits implies that cracking using the number field sieve is expected to
2788 take about the same time as factoring a 1024-bit RSA modulo, and using
2789 a `q' of size 160 bits implies that cracking using Pollard-rho will
2790 take roughly `2^80' group operations. With the size of `q' fixed, tied
2791 to the SHA1 digest size, it may be tempting to increase the size of `p'
2792 to, say, 4096 bits. This will provide excellent resistance against
2793 attacks like the number field sieve which works in the large group. But
2794 it will do very little to defend against Pollard-rho attacking the small
2795 subgroup; the attacker is slowed down at most by a single factor of 10
2796 due to the more expensive group operation. And the attacker will surely
2797 choose the latter attack.
2798
2799    The signature generation algorithm is randomized; in order to create
2800 a DSA signature, you need a good source for random numbers (*note
2801 Randomness::). Let us describe the common case of a 160-bit `q'.
2802
2803    To create a signature, one starts with the hash digest of the
2804 message, `h', which is a 160 bit number, and a random number `k,
2805 0<k<q', also 160 bits. Next, one computes
2806
2807      r = (g^k mod p) mod q
2808      s = k^-1 (h + x r) mod q
2809
2810    The signature is the pair `(r, s)', two 160 bit numbers. Note the
2811 two different mod operations when computing `r', and the use of the
2812 secret exponent `x'.
2813
2814    To verify a signature, one first checks that `0 < r,s < q', and then
2815 one computes backwards,
2816
2817      w = s^-1 mod q
2818      v = (g^(w h) y^(w r) mod p) mod q
2819
2820    The signature is valid if `v = r'. This works out because `w = s^-1
2821 mod q = k (h + x r)^-1 mod q', so that
2822
2823      g^(w h) y^(w r) = g^(w h) (g^x)^(w r) = g^(w (h + x r)) = g^k
2824
2825    When reducing mod `q' this yields `r'. Note that when verifying a
2826 signature, we don't know either `k' or `x': those numbers are secret.
2827
2828    If you can choose between RSA and DSA, which one is best? Both are
2829 believed to be secure. DSA gained popularity in the late 1990s, as a
2830 patent free alternative to RSA. Now that the RSA patents have expired,
2831 there's no compelling reason to want to use DSA. Today, the original
2832 DSA key size does not provide a large security margin, and it should
2833 probably be phased out together with RSA keys of 1024 bits. Using the
2834 revised DSA algorithm with a larger hash function, in particular,
2835 SHA256, a 256-bit `q', and `p' of size 2048 bits or more, should
2836 provide for a more comfortable security margin, but these variants are
2837 not yet in wide use.
2838
2839    DSA signatures are smaller than RSA signatures, which is important
2840 for some specialized applications.
2841
2842    From a practical point of view, DSA's need for a good randomness
2843 source is a serious disadvantage. If you ever use the same `k' (and
2844 `r') for two different message, you leak your private key.
2845
2846 6.6.4 Nettle's DSA support
2847 --------------------------
2848
2849 Like for RSA, Nettle represents DSA keys using two structures,
2850 containing values of type `mpz_t'. For information on how to customize
2851 allocation, see *Note GMP Allocation: (gmp)Custom Allocation.
2852
2853    Most of the DSA functions are very similar to the corresponding RSA
2854 functions, but there are a few differences pointed out below. For a
2855 start, there are no functions corresponding to `rsa_public_key_prepare'
2856 and `rsa_private_key_prepare'.
2857
2858  -- Context struct: dsa_public_key p q g y
2859      The public parameters described above.
2860
2861  -- Context struct: dsa_private_key x
2862      The private key `x'.
2863
2864    Before use, these structs must be initialized by calling one of
2865
2866  -- Function: void dsa_public_key_init (struct dsa_public_key *PUB)
2867  -- Function: void dsa_private_key_init (struct dsa_private_key *KEY)
2868      Calls `mpz_init' on all numbers in the key struct.
2869
2870    When finished with them, the space for the numbers must be
2871 deallocated by calling one of
2872
2873  -- Function: void dsa_public_key_clear (struct dsa_public_key *PUB)
2874  -- Function: void dsa_private_key_clear (struct dsa_private_key *KEY)
2875      Calls `mpz_clear' on all numbers in the key struct.
2876
2877    Signatures are represented using the structure below, and need to be
2878 initialized and cleared in the same way as the key structs.
2879
2880  -- Context struct: dsa_signature r s
2881
2882  -- Function: void dsa_signature_init (struct dsa_signature *SIGNATURE)
2883  -- Function: void dsa_signature_clear (struct dsa_signature *SIGNATURE)
2884      You must call `dsa_signature_init' before creating or using a
2885      signature, and call `dsa_signature_clear' when you are finished
2886      with it.
2887
2888    For signing, you need to provide both the public and the private key
2889 (unlike RSA, where the private key struct includes all information
2890 needed for signing), and a source for random numbers.  Signatures can
2891 use the SHA1 or the SHA256 hash function, although the implementation
2892 of DSA with SHA256 should be considered somewhat experimental due to
2893 lack of official test vectors and interoperability testing.
2894
2895  -- Function: int dsa_sha1_sign (const struct dsa_public_key *PUB,
2896           const struct dsa_private_key *KEY, void *RANDOM_CTX,
2897           nettle_random_func RANDOM, struct sha1_ctx *HASH, struct
2898           dsa_signature *SIGNATURE)
2899  -- Function: int dsa_sha1_sign_digest (const struct dsa_public_key
2900           *PUB, const struct dsa_private_key *KEY, void *RANDOM_CTX,
2901           nettle_random_func RANDOM, const uint8_t *DIGEST, struct
2902           dsa_signature *SIGNATURE)
2903  -- Function: int dsa_sha256_sign (const struct dsa_public_key *PUB,
2904           const struct dsa_private_key *KEY, void *RANDOM_CTX,
2905           nettle_random_func RANDOM, struct sha256_ctx *HASH, struct
2906           dsa_signature *SIGNATURE)
2907  -- Function: int dsa_sha256_sign_digest (const struct dsa_public_key
2908           *PUB, const struct dsa_private_key *KEY, void *RANDOM_CTX,
2909           nettle_random_func RANDOM, const uint8_t *DIGEST, struct
2910           dsa_signature *SIGNATURE)
2911      Creates a signature from the given hash context or digest.
2912      RANDOM_CTX and RANDOM is a randomness generator.
2913      `random(random_ctx, length, dst)' should generate `length' random
2914      octets and store them at `dst'. For advice, see *Note
2915      Randomness::. Returns one on success, or zero on failure.  Signing
2916      fails if the key size and the hash size don't match.
2917
2918    Verifying signatures is a little easier, since no randomness
2919 generator is needed. The functions are
2920
2921  -- Function: int dsa_sha1_verify (const struct dsa_public_key *KEY,
2922           struct sha1_ctx *HASH, const struct dsa_signature *SIGNATURE)
2923  -- Function: int dsa_sha1_verify_digest (const struct dsa_public_key
2924           *KEY, const uint8_t *DIGEST, const struct dsa_signature
2925           *SIGNATURE)
2926  -- Function: int dsa_sha256_verify (const struct dsa_public_key *KEY,
2927           struct sha256_ctx *HASH, const struct dsa_signature
2928           *SIGNATURE)
2929  -- Function: int dsa_sha256_verify_digest (const struct dsa_public_key
2930           *KEY, const uint8_t *DIGEST, const struct dsa_signature
2931           *SIGNATURE)
2932      Verifies a signature. Returns 1 if the signature is valid,
2933      otherwise 0.
2934
2935    Key generation uses mostly the same parameters as the corresponding
2936 RSA function.
2937
2938  -- Function: int dsa_generate_keypair (struct dsa_public_key *PUB,
2939           struct dsa_private_key *KEY, void *RANDOM_CTX,
2940           nettle_random_func RANDOM, void *PROGRESS_CTX,
2941           nettle_progress_func PROGRESS, unsigned P_BITS, unsigned
2942           Q_BITS)
2943      PUB and KEY is where the resulting key pair is stored. The structs
2944      should be initialized before you call this function.
2945
2946      RANDOM_CTX and RANDOM is a randomness generator.
2947      `random(random_ctx, length, dst)' should generate `length' random
2948      octets and store them at `dst'. For advice, see *Note Randomness::.
2949
2950      PROGRESS and PROGRESS_CTX can be used to get callbacks during the
2951      key generation process, in order to uphold an illusion of
2952      progress. PROGRESS can be NULL, in that case there are no
2953      callbacks.
2954
2955      P_BITS and Q_BITS are the desired sizes of `p' and `q'. To
2956      generate keys that conform to the original DSA standard, you must
2957      use `q_bits = 160' and select P_BITS of the form `p_bits = 512 +
2958      l*64', for `0 <= l <= 8', where the smaller sizes are no longer
2959      recommended, so you should most likely stick to `p_bits = 1024'.
2960      Non-standard sizes are possible, in particular `p_bits' larger
2961      than 1024, although DSA implementations can not in general be
2962      expected to support such keys. Also note that using very large
2963      P_BITS, with Q_BITS fixed at 160, doesn't make much sense, because
2964      the security is also limited by the size of the smaller prime.
2965      Using a larger `q_bits' requires switching to a larger hash
2966      function. To generate DSA keys for use with SHA256, use `q_bits =
2967      256' and, e.g., `p_bits = 2048'.
2968
2969      Returns one on success, and zero on failure. The function will
2970      fail if Q_BITS is neither 160 nor 256, or if P_BITS is unreasonably
2971      small.
2972
2973 \1f
2974 File: nettle.info,  Node: Elliptic curves,  Prev: DSA,  Up: Public-key algorithms
2975
2976 6.6.5 Elliptic curves
2977 ---------------------
2978
2979 For cryptographic purposes, an elliptic curve is a mathematical group of
2980 points, and computing logarithms in this group is computationally
2981 difficult problem. Nettle uses additive notation for elliptic curve
2982 groups. If P and Q are two points, and k is an integer, the point sum,
2983 P + Q, and the multiple k P can be computed efficiently, but given only
2984 two points P and Q, finding an integer k such that Q = k P is the
2985 elliptic curve discrete logarithm problem.
2986
2987    Nettle supports standard curves which are all of the form y^2 = x^3
2988 - 3 x + b (mod p), i.e., the points have coordinates (x,y), both
2989 considered as integers modulo a specified prime p. Curves are
2990 represented as a `struct ecc_curve'. Supported curves are declared in
2991 `<nettle/ecc-curve.h>', e.g., `nettle_secp_256r1' for a standardized
2992 curve using the 256-bit prime p = 2^256 - 2^224 + 2^192 + 2^96 - 1. The
2993 contents of these structs is not visible to nettle users. The "bitsize
2994 of the curve" is used as a shorthand for the bitsize of the curve's
2995 prime p, e.g., 256 bits for `nettle_secp_256r1'.
2996
2997 6.6.5.1 Side-channel silence
2998 ............................
2999
3000 Nettle's implementation of the elliptic curve operations is intended to
3001 be side-channel silent. The side-channel attacks considered are:
3002
3003    * Timing attacks If the timing of operations depends on secret
3004      values, an attacker interacting with your system can measure the
3005      response time, and infer information about your secrets, e.g., a
3006      private signature key.
3007
3008    * Attacks using memory caches Assume you have some secret data on a
3009      multi-user system, and that this data is properly protected so
3010      that other users get no direct access to it. If you have a process
3011      operating on the secret data, and this process does memory
3012      accesses depending on the data, e.g, an internal lookup table in
3013      some cryptographic algorithm, an attacker running a separate
3014      process on the same system may use behavior of internal CPU caches
3015      to get information about your secrets.
3016
3017    Nettle's ECC implementation is designed to be "side-channel silent",
3018 and not leak any information to these attacks. Timing and memory
3019 accesses depend only on the size of the input data and its location in
3020 memory, not on the actual data bits. This implies a performance penalty
3021 in several of the building blocks.
3022
3023 6.6.6 ECDSA
3024 -----------
3025
3026 ECDSA is a variant of the DSA digital signature scheme (*note DSA::),
3027 which works over an elliptic curve group rather than over a (subgroup
3028 of) integers modulo p. Like DSA, creating a signature requires a unique
3029 random nonce (repeating the nonce with two different messages reveals
3030 the private key, and any leak or bias in the generation of the nonce
3031 also leaks information about the key).
3032
3033    Unlike DSA, signatures are in general not tied to any particular hash
3034 function or even hash size. Any hash function can be used, and the hash
3035 value is truncated or padded as needed to get a size matching the curve
3036 being used. It is recommended to use a strong cryptographic hash
3037 function with digest size close to the bit size of the curve, e.g.,
3038 SHA256 is a reasonable choice when using ECDSA signature over the curve
3039 secp256r1. A protocol or application using ECDSA has to specify which
3040 curve and which hash function to use, or provide some mechanism for
3041 negotiating.
3042
3043    Nettle defines ECDSA in `<nettle/ecdsa.h>'. We first need to define
3044 the data types used to represent public and private keys.
3045
3046  -- struct: struct ecc_point
3047      Represents a point on an elliptic curve. In particular, it is used
3048      to represent an ECDSA public key.
3049
3050  -- Function: void ecc_point_init (struct ecc_point *P, const
3051           structecc_curve *ECC)
3052      Initializes P to represent points on the given curve ECC.
3053      Allocates storage for the coordinates, using the same allocation
3054      functions as GMP.
3055
3056  -- Function: void ecc_point_clear (struct ecc_point *P)
3057      Deallocate storage.
3058
3059  -- Function: int ecc_point_set (struct ecc_point *P, const mpz_t X,
3060           const mpz_t Y)
3061      Check that the given coordinates represent a point on the curve.
3062      If so, the coordinates are copied and converted to internal
3063      representation, and the function returns 1. Otherwise, it returns
3064      0. Currently, the infinity point (or zero point, with additive
3065      notation) i snot allowed.
3066
3067  -- Function: void ecc_point_get (const struct ecc_point *P, mpz_t X,
3068           mpz_t Y)
3069      Extracts the coordinate of the point P. The output parameters X or
3070      Y may be NULL if the caller doesn't want that coordinate.
3071
3072  -- struct: struct ecc_scalar
3073      Represents an integer in the range 0 < x < group order, where the
3074      "group order" refers to the order of an ECC group. In particular,
3075      it is used to represent an ECDSA private key.
3076
3077  -- Function: void ecc_scalar_init (struct ecc_scalar *S, const struct
3078           ecc_curve *ECC)
3079      Initializes S to represent a scalar suitable for the given curve
3080      ECC. Allocates storage using the same allocation functions as GMP.
3081
3082  -- Function: void ecc_scalar_clear (struct ecc_scalar *S)
3083      Deallocate storage.
3084
3085  -- Function: int ecc_scalar_set (struct ecc_scalar *S, const mpz_t Z)
3086      Check that Z is in the correct range. If so, copies the value to S
3087      and returns 1, otherwise returns 0.
3088
3089  -- Function: void ecc_scalar_get (const struct ecc_scalar *S, mpz_t Z)
3090      Extracts the scalar, in GMP `mpz_t' representation.
3091
3092    To create and verify ECDSA signatures, the following functions are
3093 used.
3094
3095  -- Function: void ecdsa_sign (const struct ecc_scalar *KEY, void
3096           *RANDOM_CTX, nettle_random_func *RANDOM, unsigned
3097           DIGEST_LENGTH, const uint8_t *DIGEST, struct dsa_signature
3098           *SIGNATURE)
3099      Uses the private key KEY to create a signature on DIGEST.
3100      RANDOM_CTX and RANDOM is a randomness generator.
3101      `random(random_ctx, length, dst)' should generate `length' random
3102      octets and store them at `dst'. The signature is stored in
3103      SIGNATURE, in the same was as for plain DSA.
3104
3105  -- Function: int ecdsa_verify (const struct ecc_point *PUB, unsigned
3106           LENGTH, const uint8_t *DIGEST, const struct dsa_signature
3107           *SIGNATURE)
3108      Uses the public key PUB to verify that SIGNATURE is a valid
3109      signature for the message digest DIGEST (of LENGTH octets).
3110      Returns 1 if the signature is valid, otherwise 0.
3111
3112    Finally, to generation of new an ECDSA key pairs
3113
3114  -- Function: void ecdsa_generate_keypair (struct ecc_point *PUB,
3115           struct ecc_scalar *KEY, void *RANDOM_CTX, nettle_random_func
3116           *RANDOM);
3117      PUB and KEY is where the resulting key pair is stored. The structs
3118      should be initialized, for the desired ECC curve, before you call
3119      this function.
3120
3121      RANDOM_CTX and RANDOM is a randomness generator.
3122      `random(random_ctx, length, dst)' should generate `length' random
3123      octets and store them at `dst'. For advice, see *Note Randomness::.
3124
3125 \1f
3126 File: nettle.info,  Node: Randomness,  Next: ASCII encoding,  Prev: Public-key algorithms,  Up: Reference
3127
3128 6.7 Randomness
3129 ==============
3130
3131 A crucial ingredient in many cryptographic contexts is randomness: Let
3132 `p' be a random prime, choose a random initialization vector `iv', a
3133 random key `k' and a random exponent `e', etc. In the theories, it is
3134 assumed that you have plenty of randomness around.  If this assumption
3135 is not true in practice, systems that are otherwise perfectly secure,
3136 can be broken. Randomness has often turned out to be the weakest link
3137 in the chain.
3138
3139    In non-cryptographic applications, such as games as well as
3140 scientific simulation, a good randomness generator usually means a
3141 generator that has good statistical properties, and is seeded by some
3142 simple function of things like the current time, process id, and host
3143 name.
3144
3145    However, such a generator is inadequate for cryptography, for at
3146 least two reasons:
3147
3148    * It's too easy for an attacker to guess the initial seed. Even if
3149      it will take some 2^32 tries before he guesses right, that's far
3150      too easy. For example, if the process id is 16 bits, the
3151      resolution of "current time" is one second, and the attacker knows
3152      what day the generator was seeded, there are only about 2^32
3153      possibilities to try if all possible values for the process id and
3154      time-of-day are tried.
3155
3156    * The generator output reveals too much. By observing only a small
3157      segment of the generator's output, its internal state can be
3158      recovered, and from there, all previous output and all future
3159      output can be computed by the attacker.
3160
3161    A randomness generator that is used for cryptographic purposes must
3162 have better properties. Let's first look at the seeding, as the issues
3163 here are mostly independent of the rest of the generator. The initial
3164 state of the generator (its seed) must be unguessable by the attacker.
3165 So what's unguessable? It depends on what the attacker already knows.
3166 The concept used in information theory to reason about such things is
3167 called "entropy", or "conditional entropy" (not to be confused with the
3168 thermodynamic concept with the same name). A reasonable requirement is
3169 that the seed contains a conditional entropy of at least some 80-100
3170 bits. This property can be explained as follows: Allow the attacker to
3171 ask `n' yes-no-questions, of his own choice, about the seed. If the
3172 attacker, using this question-and-answer session, as well as any other
3173 information he knows about the seeding process, still can't guess the
3174 seed correctly, then the conditional entropy is more than `n' bits.
3175
3176    Let's look at an example. Say information about timing of received
3177 network packets is used in the seeding process. If there is some random
3178 network traffic going on, this will contribute some bits of entropy or
3179 "unguessability" to the seed. However, if the attacker can listen in to
3180 the local network, or if all but a small number of the packets were
3181 transmitted by machines that the attacker can monitor, this additional
3182 information makes the seed easier for the attacker to figure out. Even
3183 if the information is exactly the same, the conditional entropy, or
3184 unguessability, is smaller for an attacker that knows some of it already
3185 before the hypothetical question-and-answer session.
3186
3187    Seeding of good generators is usually based on several sources. The
3188 key point here is that the amount of unguessability that each source
3189 contributes, depends on who the attacker is. Some sources that have been
3190 used are:
3191
3192 High resolution timing of i/o activities
3193      Such as completed blocks from spinning hard disks, network
3194      packets, etc.  Getting access to such information is quite system
3195      dependent, and not all systems include suitable hardware. If
3196      available, it's one of the better randomness source one can find
3197      in a digital, mostly predictable, computer.
3198
3199 User activity
3200      Timing and contents of user interaction events is another popular
3201      source that is available for interactive programs (even if I
3202      suspect that it is sometimes used in order to make the user feel
3203      good, not because the quality of the input is needed or used
3204      properly). Obviously, not available when a machine is unattended.
3205      Also beware of networks: User interaction that happens across a
3206      long serial cable, TELNET session, or even SSH session may be
3207      visible to an attacker, in full or partially.
3208
3209 Audio input
3210      Any room, or even a microphone input that's left unconnected, is a
3211      source of some random background noise, which can be fed into the
3212      seeding process.
3213
3214 Specialized hardware
3215      Hardware devices with the sole purpose of generating random data
3216      have been designed. They range from radioactive samples with an
3217      attached Geiger counter, to amplification of the inherent noise in
3218      electronic components such as diodes and resistors, to
3219      low-frequency sampling of chaotic systems. Hashing successive
3220      images of a Lava lamp is a spectacular example of the latter type.
3221
3222 Secret information
3223      Secret information, such as user passwords or keys, or private
3224      files stored on disk, can provide some unguessability. A problem
3225      is that if the information is revealed at a later time, the
3226      unguessability vanishes. Another problem is that this kind of
3227      information tends to be fairly constant, so if you rely on it and
3228      seed your generator regularly, you risk constructing almost
3229      similar seeds or even constructing the same seed more than once.
3230
3231    For all practical sources, it's difficult but important to provide a
3232 reliable lower bound on the amount of unguessability that it provides.
3233 Two important points are to make sure that the attacker can't observe
3234 your sources (so if you like the Lava lamp idea, remember that you have
3235 to get your own lamp, and not put it by a window or anywhere else where
3236 strangers can see it), and that hardware failures are detected. What if
3237 the bulb in the Lava lamp, which you keep locked into a cupboard
3238 following the above advice, breaks after a few months?
3239
3240    So let's assume that we have been able to find an unguessable seed,
3241 which contains at least 80 bits of conditional entropy, relative to all
3242 attackers that we care about (typically, we must at the very least
3243 assume that no attacker has root privileges on our machine).
3244
3245    How do we generate output from this seed, and how much can we get?
3246 Some generators (notably the Linux `/dev/random' generator) tries to
3247 estimate available entropy and restrict the amount of output. The goal
3248 is that if you read 128 bits from `/dev/random', you should get 128
3249 "truly random" bits. This is a property that is useful in some
3250 specialized circumstances, for instance when generating key material for
3251 a one time pad, or when working with unconditional blinding, but in most
3252 cases, it doesn't matter much. For most application, there's no limit on
3253 the amount of useful "random" data that we can generate from a small
3254 seed; what matters is that the seed is unguessable and that the
3255 generator has good cryptographic properties.
3256
3257    At the heart of all generators lies its internal state. Future output
3258 is determined by the internal state alone. Let's call it the generator's
3259 key. The key is initialized from the unguessable seed. Important
3260 properties of a generator are:
3261
3262 "Key-hiding"
3263      An attacker observing the output should not be able to recover the
3264      generator's key.
3265
3266 "Independence of outputs"
3267      Observing some of the output should not help the attacker to guess
3268      previous or future output.
3269
3270 "Forward secrecy"
3271      Even if an attacker compromises the generator's key, he should not
3272      be able to guess the generator output _before_ the key compromise.
3273
3274 "Recovery from key compromise"
3275      If an attacker compromises the generator's key, he can compute
3276      _all_ future output. This is inevitable if the generator is seeded
3277      only once, at startup. However, the generator can provide a
3278      reseeding mechanism, to achieve recovery from key compromise. More
3279      precisely: If the attacker compromises the key at a particular
3280      time `t_1', there is another later time `t_2', such that if the
3281      attacker observes all output generated between `t_1' and `t_2', he
3282      still can't guess what output is generated after `t_2'.
3283
3284
3285    Nettle includes one randomness generator that is believed to have all
3286 the above properties, and two simpler ones.
3287
3288    ARCFOUR, like any stream cipher, can be used as a randomness
3289 generator. Its output should be of reasonable quality, if the seed is
3290 hashed properly before it is used with `arcfour_set_key'. There's no
3291 single natural way to reseed it, but if you need reseeding, you should
3292 be using Yarrow instead.
3293
3294    The "lagged Fibonacci" generator in `<nettle/knuth-lfib.h>' is a
3295 fast generator with good statistical properties, but is *not* for
3296 cryptographic use, and therefore not documented here. It is included
3297 mostly because the Nettle test suite needs to generate some test data
3298 from a small seed.
3299
3300    The recommended generator to use is Yarrow, described below.
3301
3302 6.7.1 Yarrow
3303 ------------
3304
3305 Yarrow is a family of pseudo-randomness generators, designed for
3306 cryptographic use, by John Kelsey, Bruce Schneier and Niels Ferguson.
3307 Yarrow-160 is described in a paper at
3308 `http://www.counterpane.com/yarrow.html', and it uses SHA1 and
3309 triple-DES, and has a 160-bit internal state. Nettle implements
3310 Yarrow-256, which is similar, but uses SHA256 and AES to get an
3311 internal state of 256 bits.
3312
3313    Yarrow was an almost finished project, the paper mentioned above is
3314 the closest thing to a specification for it, but some smaller details
3315 are left out. There is no official reference implementation or test
3316 cases.  This section includes an overview of Yarrow, but for the
3317 details of Yarrow-256, as implemented by Nettle, you have to consult
3318 the source code. Maybe a complete specification can be written later.
3319
3320    Yarrow can use many sources (at least two are needed for proper
3321 reseeding), and two randomness "pools", referred to as the "slow pool"
3322 and the "fast pool". Input from the sources is fed alternatingly into
3323 the two pools. When one of the sources has contributed 100 bits of
3324 entropy to the fast pool, a "fast reseed" happens and the fast pool is
3325 mixed into the internal state. When at least two of the sources have
3326 contributed at least 160 bits each to the slow pool, a "slow reseed"
3327 takes place. The contents of both pools are mixed into the internal
3328 state. These procedures should ensure that the generator will eventually
3329 recover after a key compromise.
3330
3331    The output is generated by using AES to encrypt a counter, using the
3332 generator's current key. After each request for output, another 256
3333 bits are generated which replace the key. This ensures forward secrecy.
3334
3335    Yarrow can also use a "seed file" to save state across restarts.
3336 Yarrow is seeded by either feeding it the contents of the previous seed
3337 file, or feeding it input from its sources until a slow reseed happens.
3338
3339    Nettle defines Yarrow-256 in `<nettle/yarrow.h>'.
3340
3341  -- Context struct: struct yarrow256_ctx
3342
3343  -- Context struct: struct yarrow_source
3344      Information about a single source.
3345
3346  -- Constant: YARROW256_SEED_FILE_SIZE
3347      Recommended size of the Yarrow-256 seed file.
3348
3349  -- Function: void yarrow256_init (struct yarrow256_ctx *CTX, unsigned
3350           NSOURCES, struct yarrow_source *SOURCES)
3351      Initializes the yarrow context, and its NSOURCES sources. It's
3352      possible to call it with NSOURCES=0 and SOURCES=NULL, if you don't
3353      need the update features.
3354
3355  -- Function: void yarrow256_seed (struct yarrow256_ctx *CTX, unsigned
3356           LENGTH, uint8_t *SEED_FILE)
3357      Seeds Yarrow-256 from a previous seed file. LENGTH should be at
3358      least `YARROW256_SEED_FILE_SIZE', but it can be larger.
3359
3360      The generator will trust you that the SEED_FILE data really is
3361      unguessable. After calling this function, you _must_ overwrite the
3362      old seed file with newly generated data from `yarrow256_random'.
3363      If it's possible for several processes to read the seed file at
3364      about the same time, access must be coordinated using some locking
3365      mechanism.
3366
3367  -- Function: int yarrow256_update (struct yarrow256_ctx *CTX, unsigned
3368           SOURCE, unsigned ENTROPY, unsigned LENGTH, const uint8_t
3369           *DATA)
3370      Updates the generator with data from source SOURCE (an index that
3371      must be smaller than the number of sources). ENTROPY is your
3372      estimated lower bound for the entropy in the data, measured in
3373      bits.  Calling update with zero ENTROPY is always safe, no matter
3374      if the data is random or not.
3375
3376      Returns 1 if a reseed happened, in which case an application using
3377      a seed file may want to generate new seed data with
3378      `yarrow256_random' and overwrite the seed file. Otherwise, the
3379      function returns 0.
3380
3381  -- Function: void yarrow256_random (struct yarrow256_ctx *CTX,
3382           unsigned LENGTH, uint8_t *DST)
3383      Generates LENGTH octets of output. The generator must be seeded
3384      before you call this function.
3385
3386      If you don't need forward secrecy, e.g. if you need non-secret
3387      randomness for initialization vectors or padding, you can gain some
3388      efficiency by buffering, calling this function for reasonably large
3389      blocks of data, say 100-1000 octets at a time.
3390
3391  -- Function: int yarrow256_is_seeded (struct yarrow256_ctx *CTX)
3392      Returns 1 if the generator is seeded and ready to generate output,
3393      otherwise 0.
3394
3395  -- Function: unsigned yarrow256_needed_sources (struct yarrow256_ctx
3396           *CTX)
3397      Returns the number of sources that must reach the threshold before
3398      a slow reseed will happen. Useful primarily when the generator is
3399      unseeded.
3400
3401  -- Function: void yarrow256_fast_reseed (struct yarrow256_ctx *CTX)
3402  -- Function: void yarrow256_slow_reseed (struct yarrow256_ctx *CTX)
3403      Causes a fast or slow reseed to take place immediately, regardless
3404      of the current entropy estimates of the two pools. Use with care.
3405
3406    Nettle includes an entropy estimator for one kind of input source:
3407 User keyboard input.
3408
3409  -- Context struct: struct yarrow_key_event_ctx
3410      Information about recent key events.
3411
3412  -- Function: void yarrow_key_event_init (struct yarrow_key_event_ctx
3413           *CTX)
3414      Initializes the context.
3415
3416  -- Function: unsigned yarrow_key_event_estimate (struct
3417           yarrow_key_event_ctx *CTX, unsigned KEY, unsigned TIME)
3418      KEY is the id of the key (ASCII value, hardware key code, X
3419      keysym, ..., it doesn't matter), and TIME is the timestamp of the
3420      event. The time must be given in units matching the resolution by
3421      which you read the clock. If you read the clock with microsecond
3422      precision, TIME should be provided in units of microseconds. But
3423      if you use `gettimeofday' on a typical Unix system where the clock
3424      ticks 10 or so microseconds at a time, TIME should be given in
3425      units of 10 microseconds.
3426
3427      Returns an entropy estimate, in bits, suitable for calling
3428      `yarrow256_update'. Usually, 0, 1 or 2 bits.
3429
3430 \1f
3431 File: nettle.info,  Node: ASCII encoding,  Next: Miscellaneous functions,  Prev: Randomness,  Up: Reference
3432
3433 6.8 ASCII encoding
3434 ==================
3435
3436 Encryption will transform your data from text into binary format, and
3437 that may be a problem if you want, for example, to send the data as if
3438 it was plain text in an email (or store it along with descriptive text
3439 in a file). You may then use an encoding from binary to text: each
3440 binary byte is translated into a number of bytes of plain text.
3441
3442    A base-N encoding of data is one representation of data that only
3443 uses N different symbols (instead of the 256 possible values of a byte).
3444
3445    The base64 encoding will always use alphanumeric (upper and lower
3446 case) characters and the '+', '/' and '=' symbols to represent the
3447 data. Four output characters are generated for each three bytes of
3448 input. In case the length of the input is not a multiple of three,
3449 padding characters are added at the end.
3450
3451    The base16 encoding, also known as "hexadecimal", uses the decimal
3452 digits and the letters from A to F. Two hexadecimal digits are generated
3453 for each input byte. Base16 may be useful if you want to use the data
3454 for filenames or URLs, for example.
3455
3456    Nettle supports both base64 and base16 encoding and decoding.
3457
3458    Encoding and decoding uses a context struct to maintain its state
3459 (with the exception of base16 encoding, which doesn't need any). To
3460 encode or decode the your data, first initialize the context, then call
3461 the update function as many times as necessary, and complete the
3462 operation by calling the final function.
3463
3464    The following functions can be used to perform base64 encoding and
3465 decoding.  They are defined in `<nettle/base64.h>'.
3466
3467  -- Context struct: struct base64_encode_ctx
3468
3469  -- Function: void base64_encode_init (struct base64_encode_ctx *CTX)
3470      Initializes a base64 context. This is necessary before starting an
3471      encoding session.
3472
3473  -- Function: unsigned base64_encode_single (struct base64_encode_ctx
3474           *CTX, uint8_t *DST, uint8_t SRC)
3475      Encodes a single byte. Returns amount of output (always 1 or 2).
3476
3477  -- Macro: BASE64_ENCODE_LENGTH (LENGTH)
3478      The maximum number of output bytes when passing LENGTH input bytes
3479      to `base64_encode_update'.
3480
3481  -- Function: unsigned base64_encode_update (struct base64_encode_ctx
3482           *CTX, uint8_t *DST, unsigned LENGTH, const uint8_t *SRC)
3483      After CTX is initialized, this function may be called to encode
3484      LENGTH bytes from SRC. The result will be placed in DST, and the
3485      return value will be the number of bytes generated. Note that DST
3486      must be at least of size BASE64_ENCODE_LENGTH(LENGTH).
3487
3488  -- Constant: BASE64_ENCODE_FINAL_LENGTH
3489      The maximum amount of output from `base64_encode_final'.
3490
3491  -- Function: unsigned base64_encode_final (struct base64_encode_ctx
3492           *CTX, uint8_t *DST)
3493      After calling base64_encode_update one or more times, this function
3494      should be called to generate the final output bytes, including any
3495      needed paddding. The return value is the number of output bytes
3496      generated.
3497
3498  -- Context struct: struct base64_decode_ctx
3499
3500  -- Function: void base64_decode_init (struct base64_decode_ctx *CTX)
3501      Initializes a base64 decoding context. This is necessary before
3502      starting a decoding session.
3503
3504  -- Function: int base64_decode_single (struct base64_decode_ctx *CTX,
3505           uint8_t *DST, uint8_t SRC)
3506      Decodes a single byte (SRC) and stores the result in DST.  Returns
3507      amount of output (0 or 1), or -1 on errors.
3508
3509  -- Macro: BASE64_DECODE_LENGTH (LENGTH)
3510      The maximum number of output bytes when passing LENGTH input bytes
3511      to `base64_decode_update'.
3512
3513  -- Function: void base64_decode_update (struct base64_decode_ctx *CTX,
3514           unsigned *DST_LENGTH, uint8_t *DST, unsigned SRC_LENGTH,
3515           const uint8_t *SRC)
3516      After CTX is initialized, this function may be called to decode
3517      SRC_LENGTH bytes from SRC. DST should point to an area of size at
3518      least BASE64_DECODE_LENGTH(LENGTH), and for sanity checking,
3519      DST_LENGTH should be initialized to the size of that area before
3520      the call.  DST_LENGTH is updated to the amount of decoded output.
3521      The function will return 1 on success and 0 on error.
3522
3523  -- Function: int base64_decode_final (struct base64_decode_ctx *CTX)
3524      Check that final padding is correct. Returns 1 on success, and 0 on
3525      error.
3526
3527    Similarly to the base64 functions, the following functions perform
3528 base16 encoding, and are defined in `<nettle/base16.h>'. Note that
3529 there is no encoding context necessary for doing base16 encoding.
3530
3531  -- Function: void base16_encode_single (uint8_t *DST, uint8_t SRC)
3532      Encodes a single byte. Always stores two digits in DST[0] and
3533      DST[1].
3534
3535  -- Macro: BASE16_ENCODE_LENGTH (LENGTH)
3536      The number of output bytes when passing LENGTH input bytes to
3537      `base16_encode_update'.
3538
3539  -- Function: void base16_encode_update (uint8_t *DST, unsigned LENGTH,
3540           const uint8_t *SRC)
3541      Always stores BASE16_ENCODE_LENGTH(LENGTH) digits in DST.
3542
3543  -- Context struct: struct base16_decode_ctx
3544
3545  -- Function: void base16_decode_init (struct base16_decode_ctx *CTX)
3546      Initializes a base16 decoding context. This is necessary before
3547      starting a decoding session.
3548
3549  -- Function: int base16_decode_single (struct base16_decode_ctx *CTX,
3550           uint8_t *DST, uint8_t SRC)
3551      Decodes a single byte from SRC into DST. Returns amount of output
3552      (0 or 1), or -1 on errors.
3553
3554  -- Macro: BASE16_DECODE_LENGTH (LENGTH)
3555      The maximum number of output bytes when passing LENGTH input bytes
3556      to `base16_decode_update'.
3557
3558  -- Function: int base16_decode_update (struct base16_decode_ctx *CTX,
3559           unsigned *DST_LENGTH, uint8_t *DST, unsigned SRC_LENGTH,
3560           const uint8_t *SRC)
3561      After CTX is initialized, this function may be called to decode
3562      SRC_LENGTH bytes from SRC. DST should point to an area of size at
3563      least BASE16_DECODE_LENGTH(LENGTH), and for sanity checking,
3564      DST_LENGTH should be initialized to the size of that area before
3565      the call.  DST_LENGTH is updated to the amount of decoded output.
3566      The function will return 1 on success and 0 on error.
3567
3568  -- Function: int base16_decode_final (struct base16_decode_ctx *CTX)
3569      Checks that the end of data is correct (i.e., an even number of
3570      hexadecimal digits have been seen). Returns 1 on success, and 0 on
3571      error.
3572
3573 \1f
3574 File: nettle.info,  Node: Miscellaneous functions,  Next: Compatibility functions,  Prev: ASCII encoding,  Up: Reference
3575
3576 6.9 Miscellaneous functions
3577 ===========================
3578
3579  -- Function: uint8_t * memxor (uint8_t *DST, const uint8_t *SRC,
3580           size_t N)
3581      XORs the source area on top of the destination area. The interface
3582      doesn't follow the Nettle conventions, because it is intended to be
3583      similar to the ANSI-C `memcpy' function.
3584
3585    `memxor' is declared in `<nettle/memxor.h>'.
3586
3587 \1f
3588 File: nettle.info,  Node: Compatibility functions,  Prev: Miscellaneous functions,  Up: Reference
3589
3590 6.10 Compatibility functions
3591 ============================
3592
3593 For convenience, Nettle includes alternative interfaces to some
3594 algorithms, for compatibility with some other popular crypto toolkits.
3595 These are not fully documented here; refer to the source or to the
3596 documentation for the original implementation.
3597
3598    MD5 is defined in [RFC 1321], which includes a reference
3599 implementation.  Nettle defines a compatible interface to MD5 in
3600 `<nettle/md5-compat.h>'. This file defines the typedef `MD5_CTX', and
3601 declares the functions `MD5Init', `MD5Update' and `MD5Final'.
3602
3603    Eric Young's "libdes" (also part of OpenSSL) is a quite popular DES
3604 implementation. Nettle includes a subset if its interface in
3605 `<nettle/des-compat.h>'. This file defines the typedefs
3606 `des_key_schedule' and `des_cblock', two constants `DES_ENCRYPT' and
3607 `DES_DECRYPT', and declares one global variable `des_check_key', and
3608 the functions `des_cbc_cksum' `des_cbc_encrypt', `des_ecb2_encrypt',
3609 `des_ecb3_encrypt', `des_ecb_encrypt', `des_ede2_cbc_encrypt',
3610 `des_ede3_cbc_encrypt', `des_is_weak_key', `des_key_sched',
3611 `des_ncbc_encrypt' `des_set_key', and `des_set_odd_parity'.
3612
3613 \1f
3614 File: nettle.info,  Node: Nettle soup,  Next: Installation,  Prev: Reference,  Up: Top
3615
3616 7 Traditional Nettle Soup
3617 *************************
3618
3619 For the serious nettle hacker, here is a recipe for nettle soup. 4
3620 servings.
3621
3622      1 liter fresh nettles (urtica dioica)
3623
3624      2 tablespoons butter
3625
3626      3 tablespoons flour
3627
3628      1 liter stock (meat or vegetable)
3629
3630      1/2 teaspoon salt
3631
3632      a tad white pepper
3633
3634      some cream or milk
3635
3636    Gather 1 liter fresh nettles. Use gloves! Small, tender shoots are
3637 preferable but the tops of larger nettles can also be used.
3638
3639    Rinse the nettles very well. Boil them for 10 minutes in lightly
3640 salted water. Strain the nettles and save the water. Hack the nettles.
3641 Melt the butter and mix in the flour. Dilute with stock and the
3642 nettle-water you saved earlier. Add the hacked nettles. If you wish you
3643 can add some milk or cream at this stage. Bring to a boil and let boil
3644 for a few minutes.  Season with salt and pepper.
3645
3646    Serve with boiled egg-halves.
3647
3648 \1f
3649 File: nettle.info,  Node: Installation,  Next: Index,  Prev: Nettle soup,  Up: Top
3650
3651 8 Installation
3652 **************
3653
3654 Nettle uses `autoconf'. To build it, unpack the source and run
3655
3656      ./configure
3657      make
3658      make check
3659      make install
3660
3661 to install in under the default prefix, `/usr/local'.
3662
3663    To get a list of configure options, use `./configure --help'.
3664
3665    By default, both static and shared libraries are built and
3666 installed. To omit building the shared libraries, use the `
3667 --disable-shared' option to `./configure'.
3668
3669    Using GNU make is recommended. For other make programs, in particular
3670 BSD make, you may have to use the `--disable-dependency-tracking'
3671 option to `./configure'.
3672
3673 \1f
3674 File: nettle.info,  Node: Index,  Prev: Installation,  Up: Top
3675
3676 Function and Concept Index
3677 **************************
3678
3679 \0\b[index\0\b]
3680 * Menu:
3681
3682 * aes_decrypt:                           Cipher functions.    (line 123)
3683 * aes_encrypt:                           Cipher functions.    (line 116)
3684 * aes_invert_key:                        Cipher functions.    (line 105)
3685 * aes_set_decrypt_key:                   Cipher functions.    (line 101)
3686 * aes_set_encrypt_key:                   Cipher functions.    (line  99)
3687 * arcfour_crypt:                         Cipher functions.    (line 175)
3688 * arcfour_set_key:                       Cipher functions.    (line 170)
3689 * arctwo_decrypt:                        Cipher functions.    (line 240)
3690 * arctwo_encrypt:                        Cipher functions.    (line 233)
3691 * arctwo_set_key:                        Cipher functions.    (line 217)
3692 * arctwo_set_key_ekb:                    Cipher functions.    (line 215)
3693 * arctwo_set_key_gutmann:                Cipher functions.    (line 219)
3694 * base16_decode_final:                   ASCII encoding.      (line 139)
3695 * base16_decode_init:                    ASCII encoding.      (line 116)
3696 * BASE16_DECODE_LENGTH:                  ASCII encoding.      (line 125)
3697 * base16_decode_single:                  ASCII encoding.      (line 121)
3698 * base16_decode_update:                  ASCII encoding.      (line 131)
3699 * BASE16_ENCODE_LENGTH:                  ASCII encoding.      (line 106)
3700 * base16_encode_single:                  ASCII encoding.      (line 102)
3701 * base16_encode_update:                  ASCII encoding.      (line 111)
3702 * base64_decode_final:                   ASCII encoding.      (line  94)
3703 * base64_decode_init:                    ASCII encoding.      (line  71)
3704 * BASE64_DECODE_LENGTH:                  ASCII encoding.      (line  80)
3705 * base64_decode_single:                  ASCII encoding.      (line  76)
3706 * base64_decode_update:                  ASCII encoding.      (line  86)
3707 * base64_encode_final:                   ASCII encoding.      (line  63)
3708 * base64_encode_init:                    ASCII encoding.      (line  40)
3709 * BASE64_ENCODE_LENGTH:                  ASCII encoding.      (line  48)
3710 * base64_encode_single:                  ASCII encoding.      (line  45)
3711 * base64_encode_update:                  ASCII encoding.      (line  53)
3712 * Block Cipher:                          Cipher functions.    (line  12)
3713 * blowfish_decrypt:                      Cipher functions.    (line 282)
3714 * blowfish_encrypt:                      Cipher functions.    (line 275)
3715 * blowfish_set_key:                      Cipher functions.    (line 265)
3716 * camellia_crypt:                        Cipher functions.    (line 332)
3717 * camellia_invert_key:                   Cipher functions.    (line 321)
3718 * camellia_set_decrypt_key:              Cipher functions.    (line 317)
3719 * camellia_set_encrypt_key:              Cipher functions.    (line 315)
3720 * cast128_decrypt:                       Cipher functions.    (line 372)
3721 * cast128_encrypt:                       Cipher functions.    (line 365)
3722 * cast128_set_key:                       Cipher functions.    (line 360)
3723 * CBC Mode:                              CBC.                 (line   6)
3724 * CBC_CTX:                               CBC.                 (line  51)
3725 * CBC_DECRYPT:                           CBC.                 (line  72)
3726 * cbc_decrypt:                           CBC.                 (line  37)
3727 * CBC_ENCRYPT:                           CBC.                 (line  71)
3728 * cbc_encrypt:                           CBC.                 (line  34)
3729 * CBC_SET_IV:                            CBC.                 (line  66)
3730 * Cipher:                                Cipher functions.    (line   6)
3731 * Cipher Block Chaining:                 CBC.                 (line   6)
3732 * Collision-resistant:                   Hash functions.      (line  18)
3733 * Conditional entropy:                   Randomness.          (line  51)
3734 * Counter Mode:                          CTR.                 (line   6)
3735 * CTR Mode:                              CTR.                 (line   6)
3736 * CTR_CRYPT:                             CTR.                 (line  60)
3737 * ctr_crypt:                             CTR.                 (line  36)
3738 * CTR_CTX:                               CTR.                 (line  48)
3739 * CTR_SET_COUNTER:                       CTR.                 (line  55)
3740 * des3_decrypt:                          Cipher functions.    (line 494)
3741 * des3_encrypt:                          Cipher functions.    (line 487)
3742 * des3_set_key:                          Cipher functions.    (line 476)
3743 * des_check_parity:                      Cipher functions.    (line 420)
3744 * des_decrypt:                           Cipher functions.    (line 416)
3745 * des_encrypt:                           Cipher functions.    (line 409)
3746 * des_fix_parity:                        Cipher functions.    (line 425)
3747 * des_set_key:                           Cipher functions.    (line 401)
3748 * dsa_generate_keypair:                  DSA.                 (line 210)
3749 * dsa_private_key_clear:                 DSA.                 (line 142)
3750 * dsa_private_key_init:                  DSA.                 (line 135)
3751 * dsa_public_key_clear:                  DSA.                 (line 141)
3752 * dsa_public_key_init:                   DSA.                 (line 134)
3753 * dsa_sha1_sign:                         DSA.                 (line 166)
3754 * dsa_sha1_sign_digest:                  DSA.                 (line 170)
3755 * dsa_sha1_verify:                       DSA.                 (line 190)
3756 * dsa_sha1_verify_digest:                DSA.                 (line 193)
3757 * dsa_sha256_sign:                       DSA.                 (line 174)
3758 * dsa_sha256_sign_digest:                DSA.                 (line 178)
3759 * dsa_sha256_verify:                     DSA.                 (line 196)
3760 * dsa_sha256_verify_digest:              DSA.                 (line 199)
3761 * dsa_signature_clear:                   DSA.                 (line 151)
3762 * dsa_signature_init:                    DSA.                 (line 150)
3763 * ecc_point_clear:                       Elliptic curves.     (line  84)
3764 * ecc_point_get:                         Elliptic curves.     (line  96)
3765 * ecc_point_init:                        Elliptic curves.     (line  79)
3766 * ecc_point_set:                         Elliptic curves.     (line  88)
3767 * ecc_scalar_clear:                      Elliptic curves.     (line 110)
3768 * ecc_scalar_get:                        Elliptic curves.     (line 117)
3769 * ecc_scalar_init:                       Elliptic curves.     (line 106)
3770 * ecc_scalar_set:                        Elliptic curves.     (line 113)
3771 * ecdsa_generate_keypair:                Elliptic curves.     (line 144)
3772 * ecdsa_sign:                            Elliptic curves.     (line 126)
3773 * ecdsa_verify:                          Elliptic curves.     (line 135)
3774 * Entropy:                               Randomness.          (line  51)
3775 * Galois Counter Mode:                   GCM.                 (line   6)
3776 * GCM:                                   GCM.                 (line   6)
3777 * gcm_aes_decrypt:                       GCM.                 (line 167)
3778 * gcm_aes_digest:                        GCM.                 (line 174)
3779 * gcm_aes_encrypt:                       GCM.                 (line 165)
3780 * gcm_aes_set_iv:                        GCM.                 (line 154)
3781 * gcm_aes_set_key:                       GCM.                 (line 149)
3782 * gcm_aes_update:                        GCM.                 (line 158)
3783 * GCM_CTX:                               GCM.                 (line 102)
3784 * GCM_DECRYPT:                           GCM.                 (line 132)
3785 * gcm_decrypt:                           GCM.                 (line  74)
3786 * GCM_DIGEST:                            GCM.                 (line 133)
3787 * gcm_digest:                            GCM.                 (line  82)
3788 * GCM_ENCRYPT:                           GCM.                 (line 131)
3789 * gcm_encrypt:                           GCM.                 (line  71)
3790 * GCM_SET_IV:                            GCM.                 (line 123)
3791 * gcm_set_iv:                            GCM.                 (line  58)
3792 * GCM_SET_KEY:                           GCM.                 (line 117)
3793 * gcm_set_key:                           GCM.                 (line  52)
3794 * GCM_UPDATE:                            GCM.                 (line 127)
3795 * gcm_update:                            GCM.                 (line  63)
3796 * gosthash94_digest:                     Legacy hash functions.
3797                                                               (line 211)
3798 * gosthash94_init:                       Legacy hash functions.
3799                                                               (line 203)
3800 * gosthash94_update:                     Legacy hash functions.
3801                                                               (line 207)
3802 * Hash function:                         Hash functions.      (line   6)
3803 * HMAC:                                  Keyed hash functions.
3804                                                               (line  35)
3805 * HMAC_CTX:                              Keyed hash functions.
3806                                                               (line  88)
3807 * HMAC_DIGEST:                           Keyed hash functions.
3808                                                               (line 110)
3809 * hmac_digest:                           Keyed hash functions.
3810                                                               (line  76)
3811 * hmac_md5_digest:                       Keyed hash functions.
3812                                                               (line 140)
3813 * hmac_md5_set_key:                      Keyed hash functions.
3814                                                               (line 132)
3815 * hmac_md5_update:                       Keyed hash functions.
3816                                                               (line 136)
3817 * hmac_ripemd160_digest:                 Keyed hash functions.
3818                                                               (line 162)
3819 * hmac_ripemd160_set_key:                Keyed hash functions.
3820                                                               (line 154)
3821 * hmac_ripemd160_update:                 Keyed hash functions.
3822                                                               (line 158)
3823 * HMAC_SET_KEY:                          Keyed hash functions.
3824                                                               (line 104)
3825 * hmac_set_key:                          Keyed hash functions.
3826                                                               (line  61)
3827 * hmac_sha1_digest:                      Keyed hash functions.
3828                                                               (line 184)
3829 * hmac_sha1_set_key:                     Keyed hash functions.
3830                                                               (line 176)
3831 * hmac_sha1_update:                      Keyed hash functions.
3832                                                               (line 180)
3833 * hmac_sha256_digest:                    Keyed hash functions.
3834                                                               (line 206)
3835 * hmac_sha256_set_key:                   Keyed hash functions.
3836                                                               (line 198)
3837 * hmac_sha256_update:                    Keyed hash functions.
3838                                                               (line 202)
3839 * hmac_sha512_digest:                    Keyed hash functions.
3840                                                               (line 228)
3841 * hmac_sha512_set_key:                   Keyed hash functions.
3842                                                               (line 220)
3843 * hmac_sha512_update:                    Keyed hash functions.
3844                                                               (line 224)
3845 * hmac_update:                           Keyed hash functions.
3846                                                               (line  68)
3847 * KDF:                                   Key derivation functions.
3848                                                               (line   6)
3849 * Key Derivation Function:               Key derivation functions.
3850                                                               (line   6)
3851 * Keyed Hash Function:                   Keyed hash functions.
3852                                                               (line   6)
3853 * MAC:                                   Keyed hash functions.
3854                                                               (line   6)
3855 * md2_digest:                            Legacy hash functions.
3856                                                               (line  79)
3857 * md2_init:                              Legacy hash functions.
3858                                                               (line  71)
3859 * md2_update:                            Legacy hash functions.
3860                                                               (line  75)
3861 * md4_digest:                            Legacy hash functions.
3862                                                               (line 112)
3863 * md4_init:                              Legacy hash functions.
3864                                                               (line 104)
3865 * md4_update:                            Legacy hash functions.
3866                                                               (line 108)
3867 * md5_digest:                            Legacy hash functions.
3868                                                               (line  41)
3869 * md5_init:                              Legacy hash functions.
3870                                                               (line  33)
3871 * md5_update:                            Legacy hash functions.
3872                                                               (line  37)
3873 * memxor:                                Miscellaneous functions.
3874                                                               (line   8)
3875 * Message Authentication Code:           Keyed hash functions.
3876                                                               (line   6)
3877 * One-way:                               Hash functions.      (line  14)
3878 * One-way function:                      Public-key algorithms.
3879                                                               (line  18)
3880 * Password Based Key Derivation Function: Key derivation functions.
3881                                                               (line   6)
3882 * PBKDF:                                 Key derivation functions.
3883                                                               (line   6)
3884 * PBKDF2:                                Key derivation functions.
3885                                                               (line  43)
3886 * pbkdf2:                                Key derivation functions.
3887                                                               (line  29)
3888 * pbkdf2_hmac_sha1:                      Key derivation functions.
3889                                                               (line  63)
3890 * pbkdf2_hmac_sha256:                    Key derivation functions.
3891                                                               (line  75)
3892 * PKCS #5:                               Key derivation functions.
3893                                                               (line   6)
3894 * Public Key Cryptography:               Public-key algorithms.
3895                                                               (line  18)
3896 * Randomness:                            Randomness.          (line   6)
3897 * ripemd160_digest:                      Legacy hash functions.
3898                                                               (line 145)
3899 * ripemd160_init:                        Legacy hash functions.
3900                                                               (line 137)
3901 * ripemd160_update:                      Legacy hash functions.
3902                                                               (line 141)
3903 * rsa_compute_root:                      RSA.                 (line 187)
3904 * rsa_generate_keypair:                  RSA.                 (line 196)
3905 * rsa_md5_sign:                          RSA.                 (line 130)
3906 * rsa_md5_sign_digest:                   RSA.                 (line 145)
3907 * rsa_md5_verify:                        RSA.                 (line 159)
3908 * rsa_md5_verify_digest:                 RSA.                 (line 171)
3909 * rsa_private_key_clear:                 RSA.                 (line  96)
3910 * rsa_private_key_init:                  RSA.                 (line  89)
3911 * rsa_private_key_prepare:               RSA.                 (line 109)
3912 * rsa_public_key_clear:                  RSA.                 (line  95)
3913 * rsa_public_key_init:                   RSA.                 (line  88)
3914 * rsa_public_key_prepare:                RSA.                 (line 108)
3915 * rsa_sha1_sign:                         RSA.                 (line 132)
3916 * rsa_sha1_sign_digest:                  RSA.                 (line 147)
3917 * rsa_sha1_verify:                       RSA.                 (line 161)
3918 * rsa_sha1_verify_digest:                RSA.                 (line 173)
3919 * rsa_sha256_sign:                       RSA.                 (line 134)
3920 * rsa_sha256_sign_digest:                RSA.                 (line 149)
3921 * rsa_sha256_verify:                     RSA.                 (line 163)
3922 * rsa_sha256_verify_digest:              RSA.                 (line 175)
3923 * rsa_sha512_sign:                       RSA.                 (line 136)
3924 * rsa_sha512_sign_digest:                RSA.                 (line 151)
3925 * rsa_sha512_verify:                     RSA.                 (line 165)
3926 * rsa_sha512_verify_digest:              RSA.                 (line 177)
3927 * salsa20_crypt:                         Cipher functions.    (line 557)
3928 * salsa20_set_iv:                        Cipher functions.    (line 551)
3929 * salsa20_set_key:                       Cipher functions.    (line 545)
3930 * salsa20r12_crypt:                      Cipher functions.    (line 571)
3931 * serpent_decrypt:                       Cipher functions.    (line 611)
3932 * serpent_encrypt:                       Cipher functions.    (line 604)
3933 * serpent_set_key:                       Cipher functions.    (line 599)
3934 * sha1_digest:                           Legacy hash functions.
3935                                                               (line 179)
3936 * sha1_init:                             Legacy hash functions.
3937                                                               (line 171)
3938 * sha1_update:                           Legacy hash functions.
3939                                                               (line 175)
3940 * sha224_digest:                         Recommended hash functions.
3941                                                               (line  70)
3942 * sha224_init:                           Recommended hash functions.
3943                                                               (line  62)
3944 * sha224_update:                         Recommended hash functions.
3945                                                               (line  66)
3946 * sha256_digest:                         Recommended hash functions.
3947                                                               (line  34)
3948 * sha256_init:                           Recommended hash functions.
3949                                                               (line  26)
3950 * sha256_update:                         Recommended hash functions.
3951                                                               (line  30)
3952 * sha384_digest:                         Recommended hash functions.
3953                                                               (line 138)
3954 * sha384_init:                           Recommended hash functions.
3955                                                               (line 130)
3956 * sha384_update:                         Recommended hash functions.
3957                                                               (line 134)
3958 * sha3_224_digest:                       Recommended hash functions.
3959                                                               (line 176)
3960 * sha3_224_init:                         Recommended hash functions.
3961                                                               (line 168)
3962 * sha3_224_update:                       Recommended hash functions.
3963                                                               (line 172)
3964 * sha3_256_digest:                       Recommended hash functions.
3965                                                               (line 208)
3966 * sha3_256_init:                         Recommended hash functions.
3967                                                               (line 200)
3968 * sha3_256_update:                       Recommended hash functions.
3969                                                               (line 204)
3970 * sha3_384_digest:                       Recommended hash functions.
3971                                                               (line 239)
3972 * sha3_384_init:                         Recommended hash functions.
3973                                                               (line 231)
3974 * sha3_384_update:                       Recommended hash functions.
3975                                                               (line 235)
3976 * sha3_512_digest:                       Recommended hash functions.
3977                                                               (line 270)
3978 * sha3_512_init:                         Recommended hash functions.
3979                                                               (line 262)
3980 * sha3_512_update:                       Recommended hash functions.
3981                                                               (line 266)
3982 * sha512_digest:                         Recommended hash functions.
3983                                                               (line 105)
3984 * sha512_init:                           Recommended hash functions.
3985                                                               (line  97)
3986 * sha512_update:                         Recommended hash functions.
3987                                                               (line 101)
3988 * Stream Cipher:                         Cipher functions.    (line  12)
3989 * twofish_decrypt:                       Cipher functions.    (line 647)
3990 * twofish_encrypt:                       Cipher functions.    (line 640)
3991 * twofish_set_key:                       Cipher functions.    (line 635)
3992 * UMAC:                                  Keyed hash functions.
3993                                                               (line 238)
3994 * umac128_digest:                        Keyed hash functions.
3995                                                               (line 348)
3996 * umac128_set_key:                       Keyed hash functions.
3997                                                               (line 311)
3998 * umac128_set_nonce:                     Keyed hash functions.
3999                                                               (line 322)
4000 * umac128_update:                        Keyed hash functions.
4001                                                               (line 337)
4002 * umac32_digest:                         Keyed hash functions.
4003                                                               (line 342)
4004 * umac32_set_key:                        Keyed hash functions.
4005                                                               (line 305)
4006 * umac32_set_nonce:                      Keyed hash functions.
4007                                                               (line 316)
4008 * umac32_update:                         Keyed hash functions.
4009                                                               (line 331)
4010 * umac64_digest:                         Keyed hash functions.
4011                                                               (line 344)
4012 * umac64_set_key:                        Keyed hash functions.
4013                                                               (line 307)
4014 * umac64_set_nonce:                      Keyed hash functions.
4015                                                               (line 318)
4016 * umac64_update:                         Keyed hash functions.
4017                                                               (line 333)
4018 * umac96_digest:                         Keyed hash functions.
4019                                                               (line 346)
4020 * umac96_set_key:                        Keyed hash functions.
4021                                                               (line 309)
4022 * umac96_set_nonce:                      Keyed hash functions.
4023                                                               (line 320)
4024 * umac96_update:                         Keyed hash functions.
4025                                                               (line 335)
4026 * yarrow256_fast_reseed:                 Randomness.          (line 277)
4027 * yarrow256_init:                        Randomness.          (line 226)
4028 * yarrow256_is_seeded:                   Randomness.          (line 267)
4029 * yarrow256_needed_sources:              Randomness.          (line 272)
4030 * yarrow256_random:                      Randomness.          (line 258)
4031 * yarrow256_seed:                        Randomness.          (line 232)
4032 * yarrow256_slow_reseed:                 Randomness.          (line 278)
4033 * yarrow256_update:                      Randomness.          (line 245)
4034 * yarrow_key_event_estimate:             Randomness.          (line 293)
4035 * yarrow_key_event_init:                 Randomness.          (line 289)
4036
4037
4038 \1f
4039 Tag Table:
4040 Node: Top\7f543
4041 Node: Introduction\7f2242
4042 Node: Copyright\7f3808
4043 Node: Conventions\7f8514
4044 Node: Example\7f10476
4045 Node: Linking\7f11726
4046 Node: Reference\7f12559
4047 Node: Hash functions\7f12975
4048 Node: Recommended hash functions\7f14533
4049 Node: Legacy hash functions\7f23944
4050 Node: nettle_hash abstraction\7f31662
4051 Node: Cipher functions\7f33302
4052 Node: Cipher modes\7f62750
4053 Node: CBC\7f63778
4054 Node: CTR\7f67052
4055 Node: GCM\7f69579
4056 Node: Keyed hash functions\7f77142
4057 Node: Key derivation functions\7f92106
4058 Node: Public-key algorithms\7f95847
4059 Node: RSA\7f99829
4060 Node: RSA-Footnotes\7f110443
4061 Ref: RSA-Footnote-1\7f110496
4062 Node: DSA\7f110665
4063 Node: Elliptic curves\7f121996
4064 Node: Randomness\7f129060
4065 Node: ASCII encoding\7f144164
4066 Node: Miscellaneous functions\7f150614
4067 Node: Compatibility functions\7f151122
4068 Node: Nettle soup\7f152369
4069 Node: Installation\7f153362
4070 Node: Index\7f154057
4071 \1f
4072 End Tag Table
4073
4074 \1f
4075 Local Variables:
4076 coding: utf-8
4077 End: