Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This document describes the Nettle low-level cryptographic library. You can use the library directly from your C programs, or write or use an object-oriented wrapper for your favorite language or application.
This manual is for the Nettle library (version 3.4), a low-level cryptographic library.
Originally written 2001 by Niels Möller, updated 2017.
This manual is placed in the public domain. You may freely copy it, in whole or in part, with or without modification. Attribution is appreciated, but not required.
| • Introduction: | What is Nettle? | |
| • Copyright: | Your rights. | |
| • Conventions: | General interface conventions. | |
| • Example: | An example program. | |
| • Linking: | Linking with libnettle and libhogweed. | |
| • Reference: | All Nettle functions and features. | |
| • Nettle soup: | For the serious nettle hacker. | |
| • Installation: | How to install Nettle. | |
| • Index: | Function and concept index. | |
— The Detailed Node Listing — Reference | ||
|---|---|---|
| • Hash functions: | ||
| • Cipher functions: | ||
| • Cipher modes: | ||
| • Keyed hash functions: | ||
| • Key derivation functions: | ||
| • Public-key algorithms: | ||
| • Randomness: | ||
| • ASCII encoding: | ||
| • Miscellaneous functions: | ||
| • Compatibility functions: | ||
Hash functions | ||
| • Recommended hash functions: | ||
| • Legacy hash functions: | ||
| • nettle_hash abstraction: | ||
Cipher modes | ||
| • CBC: | ||
| • CTR: | ||
| • CFB: | ||
| • GCM: | ||
| • CCM: | ||
Keyed Hash Functions | ||
| • HMAC: | ||
| • UMAC: | ||
Public-key algorithms | ||
| • RSA: | The RSA public key algorithm. | |
| • DSA: | The DSA digital signature algorithm. | |
| • Elliptic curves: | Elliptic curves and ECDSA | |
Elliptic curves | ||
| • Side-channel silence: | ||
| • ECDSA: | ||
| • Curve 25519: | ||
Nettle is a cryptographic library that is designed to fit easily in more or less any context: In crypto toolkits for object-oriented languages (C++, Python, Pike, ...), in applications like LSH or GNUPG, or even in kernel space. In most contexts, you need more than the basic cryptographic algorithms, you also need some way to keep track of available algorithms, their properties and variants. You often have some algorithm selection process, often dictated by a protocol you want to implement.
And as the requirements of applications differ in subtle and not so subtle ways, an API that fits one application well can be a pain to use in a different context. And that is why there are so many different cryptographic libraries around.
Nettle tries to avoid this problem by doing one thing, the low-level crypto stuff, and providing a simple but general interface to it. In particular, Nettle doesn’t do algorithm selection. It doesn’t do memory allocation. It doesn’t do any I/O.
The idea is that one can build several application and context specific interfaces on top of Nettle, and share the code, test cases, benchmarks, documentation, etc. Examples are the Nettle module for the Pike language, and LSH, which both use an object-oriented abstraction on top of the library.
This manual explains how to use the Nettle library. It also tries to provide some background on the cryptography, and advice on how to best put it to use.
Next: Conventions, Previous: Introduction, Up: Top [Contents][Index]
Nettle is dual licenced under the GNU General Public License version 2 or later, and the GNU Lesser General Public License version 3 or later. When using Nettle, you must comply fully with all conditions of at least one of these licenses. A few of the individual files are licensed under more permissive terms, or in the public domain. To find the current status of particular files, you have to read the copyright notices at the top of the files.
This manual is in the public domain. You may freely copy it in whole or in part, e.g., into documentation of programs that build on Nettle. Attribution, as well as contribution of improvements to the text, is of course appreciated, but it is not required.
A list of the supported algorithms, their origins, and exceptions to the above licensing:
The implementation of the AES cipher (also known as rijndael) is written by Rafael Sevilla. Assembler for x86 by Rafael Sevilla and Niels Möller, Sparc assembler by Niels Möller.
The implementation of the ARCFOUR (also known as RC4) cipher is written by Niels Möller.
The implementation of the ARCTWO (also known as RC2) cipher is written by Nikos Mavroyanopoulos and modified by Werner Koch and Simon Josefsson.
The implementation of the BLOWFISH cipher is written by Werner Koch, copyright owned by the Free Software Foundation. Also hacked by Simon Josefsson and Niels Möller.
The C implementation is by Nippon Telegraph and Telephone Corporation (NTT), heavily modified by Niels Möller. Assembler for x86 and x86_64 by Niels Möller.
The implementation of the CAST128 cipher is written by Steve Reid. Released into the public domain.
Implemented by Joachim Strömbergson, based on the implementation of SALSA20 (see below). Assembly for x86_64 by Niels Möller.
The implementation of the DES cipher is written by Dana L. How, and released under the LGPL, version 2 or later.
The C implementation of the GOST94 message digest is written by Aleksey Kravchenko and was ported from the rhash library by Nikos Mavrogiannopoulos. It is released under the MIT license.
The implementation of MD2 is written by Andrew Kuchling, and hacked some by Andreas Sigfridsson and Niels Möller. Python Cryptography Toolkit license (essentially public domain).
This is almost the same code as for MD5 below, with modifications by Marcus Comstedt. Released into the public domain.
The implementation of the MD5 message digest is written by Colin Plumb. It has been hacked some more by Andrew Kuchling and Niels Möller. Released into the public domain.
The C implementation of PBKDF2 is based on earlier work for Shishi and GnuTLS by Simon Josefsson.
The implementation of RIPEMD160 message digest is based on the code in libgcrypt, copyright owned by the Free Software Foundation. Ported to Nettle by Andres Mejia.
The C implementation of SALSA20 is based on D. J. Bernstein’s reference implementation (in the public domain), adapted to Nettle by Simon Josefsson, and heavily modified by Niels Möller. Assembly for x86_64 and ARM by Niels Möller.
The implementation of the SERPENT cipher is based on the code in libgcrypt, copyright owned by the Free Software Foundation. Adapted to Nettle by Simon Josefsson and heavily modified by Niels Möller. Assembly for x86_64 by Niels Möller.
Based on the implementation by Andrew M. (floodyberry), modified by Nikos Mavrogiannopoulos and Niels Möller. Assembly for x86_64 by Niels Möller.
The C implementation of the SHA1 message digest is written by Peter Gutmann, and hacked some more by Andrew Kuchling and Niels Möller. Released into the public domain. Assembler for x86, x86_64 and ARM by Niels Möller, released under the LGPL.
Written by Niels Möller, using Peter Gutmann’s SHA1 code as a model.
Written by Niels Möller.
The implementation of the TWOFISH cipher is written by Ruud de Rooij.
Written by Niels Möller.
Written by Niels Möller. Uses the GMP library for bignum operations.
Written by Niels Möller. Uses the GMP library for bignum operations.
Written by Niels Möller. Uses the GMP library for bignum operations. Development of Nettle’s ECC support was funded by the .SE Internet Fund.
For each supported algorithm, there is an include file that defines a context struct, a few constants, and declares functions for operating on the context. The context struct encapsulates all information needed by the algorithm, and it can be copied or moved in memory with no unexpected effects.
For consistency, functions for different algorithms are very similar, but there are some differences, for instance reflecting if the key setup or encryption function differ for encryption and decryption, and whether or not key setup can fail. There are also differences between algorithms that don’t show in function prototypes, but which the application must nevertheless be aware of. There is no big difference between the functions for stream ciphers and for block ciphers, although they should be used quite differently by the application.
If your application uses more than one algorithm of the same type, you should probably create an interface that is tailor-made for your needs, and then write a few lines of glue code on top of Nettle.
By convention, for an algorithm named foo, the struct tag for the
context struct is foo_ctx, constants and functions uses prefixes
like FOO_BLOCK_SIZE (a constant) and foo_set_key (a
function).
In all functions, strings are represented with an explicit length, of
type size_t, and a pointer of type uint8_t * or
const uint8_t *. For functions that transform one string to
another, the argument order is length, destination pointer and source
pointer. Source and destination areas are usually of the same length.
When they differ, e.g., for ccm_encrypt_message, the length
argument specifies the size of the destination area. Source and
destination pointers may be equal, so that you can process strings in
place, but source and destination areas must not overlap in any
other way.
Many of the functions lack return value and can never fail. Those functions which can fail, return one on success and zero on failure.
Next: Linking, Previous: Conventions, Up: Top [Contents][Index]
A simple example program that reads a file from standard input and writes its SHA1 check-sum on standard output should give the flavor of Nettle.
#include <stdio.h>
#include <stdlib.h>
#include <nettle/sha1.h>
#define BUF_SIZE 1000
static void
display_hex(unsigned length, uint8_t *data)
{
unsigned i;
for (i = 0; i<length; i++)
printf("%02x ", data[i]);
printf("\n");
}
int
main(int argc, char **argv)
{
struct sha1_ctx ctx;
uint8_t buffer[BUF_SIZE];
uint8_t digest[SHA1_DIGEST_SIZE];
sha1_init(&ctx);
for (;;)
{
int done = fread(buffer, 1, sizeof(buffer), stdin);
sha1_update(&ctx, done, buffer);
if (done < sizeof(buffer))
break;
}
if (ferror(stdin))
return EXIT_FAILURE;
sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
display_hex(SHA1_DIGEST_SIZE, digest);
return EXIT_SUCCESS;
}
On a typical Unix system, this program can be compiled and linked with the command line
gcc sha-example.c -o sha-example -lnettle
Nettle actually consists of two libraries, libnettle and libhogweed. The libhogweed library contains those functions of Nettle that uses bignum operations, and depends on the GMP library. With this division, linking works the same for both static and dynamic libraries.
If an application uses only the symmetric crypto algorithms of Nettle
(i.e., block ciphers, hash functions, and the like), it’s sufficient to
link with -lnettle. If an application also uses public-key
algorithms, the recommended linker flags are -lhogweed -lnettle
-lgmp. If the involved libraries are installed as dynamic libraries, it
may be sufficient to link with just -lhogweed, and the loader
will resolve the dependencies automatically.
Next: Nettle soup, Previous: Linking, Up: Top [Contents][Index]
This chapter describes all the Nettle functions, grouped by family.
Next: Cipher functions, Previous: Reference, Up: Reference [Contents][Index]
A cryptographic hash function is a function that takes variable
size strings, and maps them to strings of fixed, short, length. There
are naturally lots of collisions, as there are more possible 1MB files
than 20 byte strings. But the function is constructed such that is hard
to find the collisions. More precisely, a cryptographic hash function
H should have the following properties:
Given a hash value H(x) it is hard to find a string x
that hashes to that value.
It is hard to find two different strings, x and y, such
that H(x) = H(y).
Hash functions are useful as building blocks for digital signatures, message authentication codes, pseudo random generators, association of unique ids to documents, and many other things.
The most commonly used hash functions are MD5 and SHA1. Unfortunately, both these fail the collision-resistance requirement; cryptologists have found ways to construct colliding inputs. The recommended hash functions for new applications are SHA2 (with main variants SHA256 and SHA512). At the time of this writing (Autumn 2015), SHA3 has recently been standardized, and the new SHA3 and other top SHA3 candidates may also be reasonable alternatives.
| • Recommended hash functions: | ||
| • Legacy hash functions: | ||
| • nettle_hash abstraction: |
Next: Legacy hash functions, Up: Hash functions [Contents][Index]
The following hash functions have no known weaknesses, and are suitable for new applications. The SHA2 family of hash functions were specified by NIST, intended as a replacement for SHA1.
SHA256 is a member of the SHA2 family. It outputs hash values of 256 bits, or 32 octets. Nettle defines SHA256 in <nettle/sha2.h>.
The size of a SHA256 digest, i.e. 32.
The internal block size of SHA256. Useful for some special constructions, in particular HMAC-SHA256.
Initialize the SHA256 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA256_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
sha256_init.
Earlier versions of nettle defined SHA256 in the header file <nettle/sha.h>, which is now deprecated, but kept for compatibility.
SHA224 is a variant of SHA256, with a different initial state, and with the output truncated to 224 bits, or 28 octets. Nettle defines SHA224 in <nettle/sha2.h> (and in <nettle/sha.h>, for backwards compatibility).
The size of a SHA224 digest, i.e. 28.
The internal block size of SHA224. Useful for some special constructions, in particular HMAC-SHA224.
Initialize the SHA224 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA224_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
sha224_init.
SHA512 is a larger sibling to SHA256, with a very similar structure but with both the output and the internal variables of twice the size. The internal variables are 64 bits rather than 32, making it significantly slower on 32-bit computers. It outputs hash values of 512 bits, or 64 octets. Nettle defines SHA512 in <nettle/sha2.h> (and in <nettle/sha.h>, for backwards compatibility).
The size of a SHA512 digest, i.e. 64.
The internal block size of SHA512, 128. Useful for some special constructions, in particular HMAC-SHA512.
Initialize the SHA512 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA512_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
sha512_init.
Several variants of SHA512 have been defined, with a different initial state, and with the output truncated to shorter length than 512 bits. Naming is a bit confused, these algorithms are called SHA512-224, SHA512-256 and SHA384, for output sizes of 224, 256 and 384 bits, respectively. Nettle defines these in <nettle/sha2.h> (and in <nettle/sha.h>, for backwards compatibility).
These context structs are all the same as sha512_ctx. They are defined as simple preprocessor aliases, which may cause some problems if used as identifiers for other purposes. So avoid doing that.
The digest size for each variant, i.e., 28, 32, and 48, respectively.
The internal block size, same as SHA512_BLOCK_SIZE, i.e., 128. Useful for some special constructions, in particular HMAC-SHA384.
Initialize the context struct.
Hash some more data. These are all aliases for sha512_update, which does the same thing.
Performs final processing and extracts the message digest, writing it to digest. length may be smaller than the specified digest size, in which case only the first length octets of the digest are written.
These function also reset the context in the same way as the corresponding init function.
The SHA3 hash functions were specified by NIST in response to weaknesses in SHA1, and doubts about SHA2 hash functions which structurally are very similar to SHA1. SHA3 is a result of a competition, where the winner, also known as Keccak, was designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche. It is structurally very different from all widely used earlier hash functions. Like SHA2, there are several variants, with output sizes of 224, 256, 384 and 512 bits (28, 32, 48 and 64 octets, respectively). In August 2015, it was formally standardized by NIST, as FIPS 202, http://dx.doi.org/10.6028/NIST.FIPS.202.
Note that the SHA3 implementation in earlier versions of Nettle was
based on the specification at the time Keccak was announced as the
winner of the competition, which is incompatible with the final standard
and hence with current versions of Nettle. The nette/sha3.h
defines a preprocessor symbol NETTLE_SHA3_FIPS202 to indicate
conformance with the standard.
Defined to 1 in Nettle versions supporting FIPS 202. Undefined in earlier versions.
Nettle defines SHA3-224 in <nettle/sha3.h>.
The size of a SHA3_224 digest, i.e., 28.
The internal block size of SHA3_224.
Initialize the SHA3-224 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA3_224_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context.
This is SHA3 with 256-bit output size, and possibly the most useful of the SHA3 hash functions.
Nettle defines SHA3-256 in <nettle/sha3.h>.
The size of a SHA3_256 digest, i.e., 32.
The internal block size of SHA3_256.
Initialize the SHA3-256 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA3_256_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context.
This is SHA3 with 384-bit output size.
Nettle defines SHA3-384 in <nettle/sha3.h>.
The size of a SHA3_384 digest, i.e., 48.
The internal block size of SHA3_384.
Initialize the SHA3-384 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA3_384_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context.
This is SHA3 with 512-bit output size.
Nettle defines SHA3-512 in <nettle/sha3.h>.
The size of a SHA3_512 digest, i.e. 64.
The internal block size of SHA3_512.
Initialize the SHA3-512 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA3_512_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context.
Next: nettle_hash abstraction, Previous: Recommended hash functions, Up: Hash functions [Contents][Index]
The hash functions in this section all have some known weaknesses, and should be avoided for new applications. These hash functions are mainly useful for compatibility with old applications and protocols. Some are still considered safe as building blocks for particular constructions, e.g., there seems to be no known attacks against HMAC-SHA1 or even HMAC-MD5. In some important cases, use of a “legacy” hash function does not in itself make the application insecure; if a known weakness is relevant depends on how the hash function is used, and on the threat model.
MD5 is a message digest function constructed by Ronald Rivest, and described in RFC 1321. It outputs message digests of 128 bits, or 16 octets. Nettle defines MD5 in <nettle/md5.h>.
The size of an MD5 digest, i.e. 16.
The internal block size of MD5. Useful for some special constructions, in particular HMAC-MD5.
Initialize the MD5 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
MD5_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
md5_init.
The normal way to use MD5 is to call the functions in order: First
md5_init, then md5_update zero or more times, and finally
md5_digest. After md5_digest, the context is reset to
its initial state, so you can start over calling md5_update to
hash new data.
To start over, you can call md5_init at any time.
MD2 is another hash function of Ronald Rivest’s, described in RFC 1319. It outputs message digests of 128 bits, or 16 octets. Nettle defines MD2 in <nettle/md2.h>.
The size of an MD2 digest, i.e. 16.
The internal block size of MD2.
Initialize the MD2 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
MD2_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
md2_init.
MD4 is a predecessor of MD5, described in RFC 1320. Like MD5, it is constructed by Ronald Rivest. It outputs message digests of 128 bits, or 16 octets. Nettle defines MD4 in <nettle/md4.h>. Use of MD4 is not recommended, but it is sometimes needed for compatibility with existing applications and protocols.
The size of an MD4 digest, i.e. 16.
The internal block size of MD4.
Initialize the MD4 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
MD4_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
md4_init.
RIPEMD160 is a hash function designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel, as a strengthened version of RIPEMD (which, like MD4 and MD5, fails the collision-resistance requirement). It produces message digests of 160 bits, or 20 octets. Nettle defined RIPEMD160 in nettle/ripemd160.h.
The size of a RIPEMD160 digest, i.e. 20.
The internal block size of RIPEMD160.
Initialize the RIPEMD160 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
RIPEMD160_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
ripemd160_init.
SHA1 is a hash function specified by NIST (The U.S. National Institute for Standards and Technology). It outputs hash values of 160 bits, or 20 octets. Nettle defines SHA1 in <nettle/sha1.h> (and in <nettle/sha.h>, for backwards compatibility).
The size of a SHA1 digest, i.e. 20.
The internal block size of SHA1. Useful for some special constructions, in particular HMAC-SHA1.
Initialize the SHA1 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
SHA1_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
sha1_init.
The GOST94 or GOST R 34.11-94 hash algorithm is a Soviet-era algorithm used in Russian government standards (see RFC 4357). It outputs message digests of 256 bits, or 32 octets. Nettle defines GOSTHASH94 in <nettle/gosthash94.h>.
The size of a GOSTHASH94 digest, i.e. 32.
The internal block size of GOSTHASH94, i.e., 32.
Initialize the GOSTHASH94 state.
Hash some more data.
Performs final processing and extracts the message digest, writing it
to digest. length may be smaller than
GOSTHASH94_DIGEST_SIZE, in which case only the first length
octets of the digest are written.
This function also resets the context in the same way as
gosthash94_init.
Previous: Legacy hash functions, Up: Hash functions [Contents][Index]
struct nettle_hash abstractionNettle includes a struct including information about the supported hash functions. It is defined in <nettle/nettle-meta.h>, and is used by Nettle’s implementation of HMAC (see Keyed hash functions).
struct nettle_hash name context_size digest_size block_size init update digestThe last three attributes are function pointers, of types
nettle_hash_init_func *, nettle_hash_update_func *, and
nettle_hash_digest_func *. The first argument to these functions is
void * pointer to a context struct, which is of size
context_size.
These are all the hash functions that Nettle implements.
Nettle also exports a list of all these hashes.
Returns a NULL-terminated list of pointers to supported hash functions. This list can be used to dynamically enumerate or search the supported algorithms.
A macro expanding to a call to nettle_get_hashes, so that one could
write, e.g., nettle_hashes[0]->name for the name of the first
hash function on the list. In earlier versions, this was not a macro but
the actual array of pointers. However, referring directly to the array
makes the array size leak into the ABI in some cases.
Next: Cipher modes, Previous: Hash functions, Up: Reference [Contents][Index]
A cipher is a function that takes a message or plaintext and a secret key and transforms it to a ciphertext. Given only the ciphertext, but not the key, it should be hard to find the plaintext. Given matching pairs of plaintext and ciphertext, it should be hard to find the key.
There are two main classes of ciphers: Block ciphers and stream ciphers.
A block cipher can process data only in fixed size chunks, called blocks. Typical block sizes are 8 or 16 octets. To encrypt arbitrary messages, you usually have to pad it to an integral number of blocks, split it into blocks, and then process each block. The simplest way is to process one block at a time, independent of each other. That mode of operation is called ECB, Electronic Code Book mode. However, using ECB is usually a bad idea. For a start, plaintext blocks that are equal are transformed to ciphertext blocks that are equal; that leaks information about the plaintext. Usually you should apply the cipher is some “feedback mode”, CBC (Cipher Block Chaining) and CTR (Counter mode) being two of of the most popular. See See Cipher modes, for information on how to apply CBC and CTR with Nettle.
A stream cipher can be used for messages of arbitrary length. A typical stream cipher is a keyed pseudo-random generator. To encrypt a plaintext message of n octets, you key the generator, generate n octets of pseudo-random data, and XOR it with the plaintext. To decrypt, regenerate the same stream using the key, XOR it to the ciphertext, and the plaintext is recovered.
Caution: The first rule for this kind of cipher is the same as for a One Time Pad: never ever use the same key twice.
A common misconception is that encryption, by itself, implies authentication. Say that you and a friend share a secret key, and you receive an encrypted message. You apply the key, and get a plaintext message that makes sense to you. Can you then be sure that it really was your friend that wrote the message you’re reading? The answer is no. For example, if you were using a block cipher in ECB mode, an attacker may pick up the message on its way, and reorder, delete or repeat some of the blocks. Even if the attacker can’t decrypt the message, he can change it so that you are not reading the same message as your friend wrote. If you are using a block cipher in CBC mode rather than ECB, or are using a stream cipher, the possibilities for this sort of attack are different, but the attacker can still make predictable changes to the message.
It is recommended to always use an authentication mechanism in addition to encrypting the messages. Popular choices are Message Authentication Codes like HMAC-SHA1 (see Keyed hash functions), or digital signatures like RSA.
Some ciphers have so called “weak keys”, keys that results in undesirable structure after the key setup processing, and should be avoided. In Nettle, most key setup functions have no return value, but for ciphers with weak keys, the return value indicates whether or not the given key is weak. For good keys, key setup returns 1, and for weak keys, it returns 0. When possible, avoid algorithms that have weak keys. There are several good ciphers that don’t have any weak keys.
To encrypt a message, you first initialize a cipher context for encryption or decryption with a particular key. You then use the context to process plaintext or ciphertext messages. The initialization is known as key setup. With Nettle, it is recommended to use each context struct for only one direction, even if some of the ciphers use a single key setup function that can be used for both encryption and decryption.
AES is a block cipher, specified by NIST as a replacement for the older DES standard. The standard is the result of a competition between cipher designers. The winning design, also known as RIJNDAEL, was constructed by Joan Daemen and Vincent Rijnmen.
Like all the AES candidates, the winning design uses a block size of 128
bits, or 16 octets, and three possible key-size, 128, 192 and 256 bits
(16, 24 and 32 octets) being the allowed key sizes. It does not have any
weak keys. Nettle defines AES in <nettle/aes.h>, and there is one
context struct for each key size. (Earlier versions of Nettle used a
single context struct, struct aes_ctx, for all key sizes. This
interface kept for backwards compatibility).
Alternative struct, for the old AES interface.
The AES block-size, 16.
Default AES key size, 32.
Initialize the cipher, for encryption or decryption, respectively.
Given a context src initialized for encryption, initializes the
context struct dst for decryption, using the same key. If the same
context struct is passed for both src and dst, it is
converted in place. These functions are mainly useful for applications
which needs to both encrypt and decrypt using the same key,
because calling, e.g., aes128_set_encrypt_key and
aes128_invert_key, is more efficient than calling
aes128_set_encrypt_key and aes128_set_decrypt_key.
Encryption function. length must be an integral multiple of the
block size. If it is more than one block, the data is processed in ECB
mode. src and dst may be equal, but they must not overlap
in any other way.
Analogous to the encryption functions above.
ARCFOUR is a stream cipher, also known under the trade marked name RC4, and it is one of the fastest ciphers around. A problem is that the key setup of ARCFOUR is quite weak, you should never use keys with structure, keys that are ordinary passwords, or sequences of keys like “secret:1”, “secret:2”, .... If you have keys that don’t look like random bit strings, and you want to use ARCFOUR, always hash the key before feeding it to ARCFOUR. Furthermore, the initial bytes of the generated key stream leak information about the key; for this reason, it is recommended to discard the first 512 bytes of the key stream.
/* A more robust key setup function for ARCFOUR */
void
arcfour_set_key_hashed(struct arcfour_ctx *ctx,
size_t length, const uint8_t *key)
{
struct sha256_ctx hash;
uint8_t digest[SHA256_DIGEST_SIZE];
uint8_t buffer[0x200];
sha256_init(&hash);
sha256_update(&hash, length, key);
sha256_digest(&hash, SHA256_DIGEST_SIZE, digest);
arcfour_set_key(ctx, SHA256_DIGEST_SIZE, digest);
arcfour_crypt(ctx, sizeof(buffer), buffer, buffer);
}
Nettle defines ARCFOUR in <nettle/arcfour.h>.
Minimum key size, 1.
Maximum key size, 256.
Default ARCFOUR key size, 16.
Initialize the cipher. The same function is used for both encryption and decryption.
Encrypt some data. The same function is used for both encryption and
decryption. Unlike the block ciphers, this function modifies the
context, so you can split the data into arbitrary chunks and encrypt
them one after another. The result is the same as if you had called
arcfour_crypt only once with all the data.
ARCTWO (also known as the trade marked name RC2) is a block cipher
specified in RFC 2268. Nettle also include a variation of the ARCTWO
set key operation that lack one step, to be compatible with the
reverse engineered RC2 cipher description, as described in a Usenet
post to sci.crypt by Peter Gutmann.
ARCTWO uses a block size of 64 bits, and variable key-size ranging
from 1 to 128 octets. Besides the key, ARCTWO also has a second
parameter to key setup, the number of effective key bits, ekb.
This parameter can be used to artificially reduce the key size. In
practice, ekb is usually set equal to the input key size.
Nettle defines ARCTWO in <nettle/arctwo.h>.
We do not recommend the use of ARCTWO; the Nettle implementation is provided primarily for interoperability with existing applications and standards.
The ARCTWO block-size, 8.
Default ARCTWO key size, 8.
Initialize the cipher. The same function is used for both encryption
and decryption. The first function is the most general one, which lets
you provide both the variable size key, and the desired effective key
size (in bits). The maximum value for ekb is 1024, and for
convenience, ekb = 0 has the same effect as ekb = 1024.
arctwo_set_key(ctx, length, key) is equivalent to
arctwo_set_key_ekb(ctx, length, key, 8*length), and
arctwo_set_key_gutmann(ctx, length, key) is equivalent to
arctwo_set_key_ekb(ctx, length, key, 1024)
Encryption function. length must be an integral multiple of the
block size. If it is more than one block, the data is processed in ECB
mode. src and dst may be equal, but they must not
overlap in any other way.
Analogous to arctwo_encrypt
BLOWFISH is a block cipher designed by Bruce Schneier. It uses a block size of 64 bits (8 octets), and a variable key size, up to 448 bits. It has some weak keys. Nettle defines BLOWFISH in <nettle/blowfish.h>.
The BLOWFISH block-size, 8.
Minimum BLOWFISH key size, 8.
Maximum BLOWFISH key size, 56.
Default BLOWFISH key size, 16.
Initialize the cipher. The same function is used for both encryption and decryption. Checks for weak keys, returning 1 for good keys and 0 for weak keys. Applications that don’t care about weak keys can ignore the return value.
blowfish_encrypt or blowfish_decrypt with a weak key will
crash with an assert violation.
Encryption function. length must be an integral multiple of the
block size. If it is more than one block, the data is processed in ECB
mode. src and dst may be equal, but they must not overlap
in any other way.
Analogous to blowfish_encrypt
Camellia is a block cipher developed by Mitsubishi and Nippon Telegraph and Telephone Corporation, described in RFC3713. It is recommended by some Japanese and European authorities as an alternative to AES, and it is one of the selected algorithms in the New European Schemes for Signatures, Integrity and Encryption (NESSIE) project. The algorithm is patented. The implementation in Nettle is derived from the implementation released by NTT under the GNU LGPL (v2.1 or later), and relies on the implicit patent license of the LGPL. There is also a statement of royalty-free licensing for Camellia at http://www.ntt.co.jp/news/news01e/0104/010417.html, but this statement has some limitations which seem problematic for free software.
Camellia uses a the same block size and key sizes as AES: The block size
is 128 bits (16 octets), and the supported key sizes are 128, 192, and
256 bits. The variants with 192 and 256 bit keys are identical, except
for the key setup. Nettle defines Camellia in
<nettle/camellia.h>, and there is one context struct for each key
size. (Earlier versions of Nettle used a single context struct,
struct camellia_ctx, for all key sizes. This interface kept for
backwards compatibility).
Contexts structs. Actually, camellia192_ctx is an alias for
camellia256_ctx.
Alternative struct, for the old Camellia interface.
The CAMELLIA block-size, 16.
Default CAMELLIA key size, 32.
Initialize the cipher, for encryption or decryption, respectively.
Given a context src initialized for encryption, initializes the
context struct dst for decryption, using the same key. If the same
context struct is passed for both src and dst, it is
converted in place. These functions are mainly useful for applications
which needs to both encrypt and decrypt using the same key.
The same function is used for both encryption and decryption.
length must be an integral multiple of the block size. If it is
more than one block, the data is processed in ECB mode. src and
dst may be equal, but they must not overlap in any other way.
CAST-128 is a block cipher, specified in RFC 2144. It uses a 64 bit (8 octets) block size, and a variable key size of up to 128 bits. Nettle defines cast128 in <nettle/cast128.h>.
The CAST128 block-size, 8.
Minimum CAST128 key size, 5.
Maximum CAST128 key size, 16.
Default CAST128 key size, 16.
Initialize the cipher. The same function is used for both encryption and decryption.
Encryption function. length must be an integral multiple of the
block size. If it is more than one block, the data is processed in ECB
mode. src and dst may be equal, but they must not overlap
in any other way.
Analogous to cast128_encrypt
ChaCha is a variant of the stream cipher Salsa20, also designed by D. J. Bernstein. For more information on Salsa20, see below. Nettle defines ChaCha in <nettle/chacha.h>.
ChaCha key size, 32.
ChaCha block size, 64.
Size of the nonce, 8.
Initialize the cipher. The same function is used for both encryption and
decryption. Before using the cipher,
you must also call chacha_set_nonce, see below.
Sets the nonce. It is always of size CHACHA_NONCE_SIZE, 8
octets. This function also initializes the block counter, setting it to
zero.
Encrypts or decrypts the data of a message, using ChaCha. When a
message is encrypted using a sequence of calls to chacha_crypt,
all but the last call must use a length that is a multiple of
CHACHA_BLOCK_SIZE.
DES is the old Data Encryption Standard, specified by NIST. It uses a block size of 64 bits (8 octets), and a key size of 56 bits. However, the key bits are distributed over 8 octets, where the least significant bit of each octet may be used for parity. A common way to use DES is to generate 8 random octets in some way, then set the least si