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 2.3), a low-level cryptographic library.
Originally written 2001 by Niels Möller, updated 2011.
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.
--- The Detailed Node Listing ---
Reference
Cipher modes
Public-key algorithms
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.
Nettle is distributed under the GNU Lesser General Public License (LGPL), see the file COPYING.LIB for details. A few of the individual files are 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 licenses:
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 unsigned, 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 of the same length. Source and
destination may be the same, so that you can process strings in place,
but they 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.
A simple example program that reads a file from standard input and writes its SHA1 checksum on standard output should give the flavor of Nettle.
#include <stdio.h>
#include <stdlib.h>
#include <nettle/sha.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
cc 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.
This chapter describes all the Nettle functions, grouped by family.
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:
H(x) it is hard to find a string x
that hashes to that value.
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 function for new applications is SHA256, even though it uses a structure similar to MD5 and SHA1. Constructing better hash functions is an urgent research problem.
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 internal block size of MD5. Useful for some special constructions, in particular HMAC-MD5.
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>.
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.
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.
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/sha.h>.
The functions are analogous to the MD5 ones.
The internal block size of SHA1. Useful for some special constructions, in particular HMAC-SHA1.
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.
SHA256 is another hash function specified by NIST, intended as a replacement for SHA1, generating larger digests. It outputs hash values of 256 bits, or 32 octets. Nettle defines SHA256 in <nettle/sha.h>.
The functions are analogous to the MD5 ones.
The internal block size of SHA256. Useful for some special constructions, in particular HMAC-SHA256.
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.
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/sha.h>.
The functions are analogous to the MD5 ones.
The internal block size of SHA224. Useful for some special constructions, in particular HMAC-SHA224.
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/sha.h>.
The functions are analogous to the MD5 ones.
The internal block size of SHA512. Useful for some special constructions, in particular HMAC-SHA512.
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.
SHA384 is a variant of SHA512, with a different initial state, and with the output truncated to 384 bits, or 48 octets. Nettle defines SHA384 in <nettle/sha.h>.
The functions are analogous to the MD5 ones.
The internal block size of SHA384. Useful for some special constructions, in particular HMAC-SHA384.
Hash some more data.
Performs final processing and extracts the message digest, writing it to digest. length may be smaller than
SHA384_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
sha384_init.
struct nettle_hashNettle 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, andnettle_hash_digest_func. The first argument to these functions isvoid *pointer to a context struct, which is of sizecontext_size.
These are all the hash functions that Nettle implements.
Nettle also exports a list of all these hashes. This list can be used to dynamically enumerate or search the supported algorithms: — Constant Struct: struct nettle_hash ** nettle_hashes
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 variable 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>.
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
srcanddst, it is converted in place. Callingaes_set_encrypt_keyandaes_invert_keyis more efficient than callingaes_set_encrypt_keyandaes_set_decrypt_key. This function is mainly useful for applications which needs to both encrypt and decrypt using the same 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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
aes_encrypt
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,
unsigned 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>.
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_cryptonly 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.
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 = 0has the same effect asekb = 1024.
arctwo_set_key(ctx, length, key)is equivalent toarctwo_set_key_ekb(ctx, length, key, 8*length), andarctwo_set_key_gutmann(ctx, length, key)is equivalent toarctwo_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.
srcanddstmay 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>.
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_encryptorblowfish_decryptwith 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.
srcanddstmay 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, and recommended by some Japanese and European authorities as an alternative to AES. 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. Nettle defines Camellia in <nettle/camellia.h>.
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
srcanddst, it is converted in place. Callingcamellia_set_encrypt_keyandcamellia_invert_keyis more efficient than callingcamellia_set_encrypt_keyandcamellia_set_decrypt_key. This function is 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.
srcanddstmay 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>.
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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
cast128_encrypt
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 significant bit of each octet to get odd parity, and initialize DES with the resulting key.
The key size of DES is so small that keys can be found by brute force, using specialized hardware or lots of ordinary work stations in parallel. One shouldn't be using plain DES at all today, if one uses DES at all one should be using “triple DES”, see DES3 below.
DES also has some weak keys. Nettle defines DES in <nettle/des.h>.
Initialize the cipher. The same function is used for both encryption and decryption. Parity bits are ignored. 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.
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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
des_encrypt
Checks that the given key has correct, odd, parity. Returns 1 for correct parity, and 0 for bad parity.
Adjusts the parity bits to match DES's requirements. You need this function if you have created a random-looking string by a key agreement protocol, and want to use it as a DES key. dst and src may be equal.
The inadequate key size of DES has already been mentioned. One way to increase the key size is to pipe together several DES boxes with independent keys. It turns out that using two DES ciphers is not as secure as one might think, even if the key size of the combination is a respectable 112 bits.
The standard way to increase DES's key size is to use three DES boxes. The mode of operation is a little peculiar: the middle DES box is wired in the reverse direction. To encrypt a block with DES3, you encrypt it using the first 56 bits of the key, then decrypt it using the middle 56 bits of the key, and finally encrypt it again using the last 56 bits of the key. This is known as “ede” triple-DES, for “encrypt-decrypt-encrypt”.
The “ede” construction provides some backward compatibility, as you get plain single DES simply by feeding the same key to all three boxes. That should help keeping down the gate count, and the price, of hardware circuits implementing both plain DES and DES3.
DES3 has a key size of 168 bits, but just like plain DES, useless parity bits are inserted, so that keys are represented as 24 octets (192 bits). As a 112 bit key is large enough to make brute force attacks impractical, some applications uses a “two-key” variant of triple-DES. In this mode, the same key bits are used for the first and the last DES box in the pipe, while the middle box is keyed independently. The two-key variant is believed to be secure, i.e. there are no known attacks significantly better than brute force.
Naturally, it's simple to implement triple-DES on top of Nettle's DES functions. Nettle includes an implementation of three-key “ede” triple-DES, it is defined in the same place as plain DES, <nettle/des.h>.
Initialize the cipher. The same function is used for both encryption and decryption. Parity bits are ignored. Checks for weak keys, returning 1 if all three keys are good keys, and 0 if one or more key is weak. Applications that don't care about weak keys can ignore the return value.
For random-looking strings, you can use des_fix_parity to adjust
the parity bits before calling des3_set_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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
des_encrypt
SERPENT is one of the AES finalists, designed by Ross Anderson, Eli Biham and Lars Knudsen. Thus, the interface and properties are similar to AES'. One peculiarity is that it is quite pointless to use it with anything but the maximum key size, smaller keys are just padded to larger ones. Nettle defines SERPENT in <nettle/serpent.h>.
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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
serpent_encrypt
Another AES finalist, this one designed by Bruce Schneier and others. Nettle defines it in <nettle/twofish.h>.
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.
srcanddstmay be equal, but they must not overlap in any other way.
Analogous to
twofish_encrypt
struct nettle_cipherNettle includes a struct including information about some of the more regular cipher functions. It should be considered a little experimental, but can be useful for applications that need a simple way to handle various algorithms. Nettle defines these structs in <nettle/nettle-meta.h>.
struct nettle_cipher name context_size block_size key_size set_encrypt_key set_decrypt_key encrypt decryptThe last four attributes are function pointers, of types
nettle_set_key_funcandnettle_crypt_func. The first argument to these functions is avoid *pointer to a context struct, which is of sizecontext_size.
Nettle includes such structs for all the regular ciphers, i.e. ones without weak keys or other oddities.
Nettle also exports a list of all these ciphers without weak keys or other oddities. This list can be used to dynamically enumerate or search the supported algorithms: — Constant Struct: struct nettle_cipher ** nettle_ciphers
Cipher modes of operation specifies the procedure to use when encrypting a message that is larger than the cipher's block size. As explained in See Cipher functions, splitting the message into blocks and processing them independently with the block cipher (Electronic Code Book mode, ECB) leaks information. Besides ECB, Nettle provides three other modes of operation: Cipher Block Chaining (CBC), Counter mode (CTR), and Galois/Counter mode (gcm). CBC is widely used, but there are a few subtle issues of information leakage, see, e.g., SSH CBC vulnerability. CTR and GCM were standardized more recently, and are believed to be more secure. GCM includes message authentication; for the other modes, one should always use a MAC (see Keyed hash functions) or signature to authenticate the message.
When using CBC mode, plaintext blocks are not encrypted independently of each other, like in Electronic Cook Book mode. Instead, when encrypting a block in CBC mode, the previous ciphertext block is XORed with the plaintext before it is fed to the block cipher. When encrypting the first block, a random block called an IV, or Initialization Vector, is used as the “previous ciphertext block”. The IV should be chosen randomly, but it need not be kept secret, and can even be transmitted in the clear together with the encrypted data.
In symbols, if E_k is the encryption function of a block cipher,
and IV is the initialization vector, then n plaintext blocks
M_1,... M_n are transformed into n ciphertext blocks
C_1,... C_n as follows:
C_1 = E_k(IV XOR M_1)
C_2 = E_k(C_1 XOR M_2)
...
C_n = E_k(C_(n-1) XOR M_n)
Nettle's includes two functions for applying a block cipher in Cipher
Block Chaining (CBC) mode, one for encryption and one for
decryption. These functions uses void * to pass cipher contexts
around.
Applies the encryption or decryption function f in CBC mode. The final ciphertext block processed is copied into iv before returning, so that large message be processed be a sequence of calls to
cbc_encrypt. The function f is of type
void f (void *ctx, unsignedlength, uint8_tdst, const uint8_t *src),and the
cbc_encryptandcbc_decryptfunctions pass their argument ctx on to f.
There are also some macros to help use these functions correctly.
Expands to
{ context_type ctx; uint8_t iv[block_size]; }
It can be used to define a CBC context struct, either directly,
struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) ctx;
or to give it a struct tag,
struct aes_cbc_ctx CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE);
First argument is a pointer to a context struct as defined by
CBC_CTX, and the second is a pointer to an Initialization Vector (IV) that is copied into that context.
A simpler way to invoke
cbc_encryptandcbc_decrypt. The first argument is a pointer to a context struct as defined byCBC_CTX, and the second argument is an encryption or decryption function following Nettle's conventions. The last three arguments define the source and destination area for the operation.
These macros use some tricks to make the compiler display a warning if
the types of f and ctx don't match, e.g. if you try to use
an struct aes_ctx context with the des_encrypt function.
Counter mode (CTR) uses the block cipher as a keyed pseudo-random generator. The output of the generator is XORed with the data to be encrypted. It can be understood as a way to transform a block cipher to a stream cipher.
The message is divided into n blocks M_1,...
M_n, where M_n is of size m which may be smaller
than the block size. Except for the last block, all the message blocks
must be of size equal to the cipher's block size.
If E_k is the encryption function of a block cipher, IC is
the initial counter, then the n plaintext blocks are
transformed into n ciphertext blocks C_1,...
C_n as follows:
C_1 = E_k(IC) XOR M_1
C_2 = E_k(IC + 1) XOR M_2
...
C_(n-1) = E_k(IC + n - 2) XOR M_(n-1)
C_n = E_k(IC + n - 1) [1..m] XOR M_n
The IC is the initial value for the counter, it plays a
similar role as the IV for CBC. When adding,
IC + x, IC is interpreted as an integer, in network
byte order. For the last block, E_k(IC + n - 1) [1..m] means that
the cipher output is truncated to m bytes.
Applies the encryption function f in CTR mode. Note that for CTR mode, encryption and decryption is the same operation, and hence f should always be the encryption function for the underlying block cipher.
When a message is encrypted using a sequence of calls to
ctr_crypt, all but the last call must use a length that is a multiple of the block size.
Like for CBC, there are also a couple of helper macros.
Expands to
{ context_type ctx; uint8_t ctr[block_size]; }
First argument is a pointer to a context struct as defined by
CTR_CTX, and the second is a pointer to an initial counter that is copied into that context.
A simpler way to invoke
ctr_crypt. The first argument is a pointer to a context struct as defined byCTR_CTX, and the second argument is an encryption function following Nettle's conventions. The last three arguments define the source and destination area for the operation.
Galois counter mode is the combination of counter mode with message authentication based on universal hashing. The main objective of the design is to provide high performance for hardware implementations, where other popular MAC algorithms (see Keyed hash functions becomes a bottleneck for high-speed hardware implementations. It was proposed by David A. McGrew and John Viega in 2005, and recommended by NIST in 2007, NIST Special Publication 800-38D. It is constructed on top of a block cipher which must have a block size of 128 bits.
GCM is applied to messages of arbitrary length. The inputs are:
The outputs are a ciphertext, of the same length as the plaintext, and a message digest of length 128 bits. Nettle's support for GCM consists of a low-level general interface, some convenience macros, and specific functions for GCM using AES as the underlying cipher. These interfaces are defined in <nettle/gcm.h>
Initializes key. cipher gives a context struct for the underlying cipher, which must have been previously initialized for encryption, and f is the encryption function.
Initializes ctx using the given IV. The key argument is actually needed only if length differs from
GCM_IV_SIZE.
Provides associated data to be authenticated. If used, must be called before
gcm_encryptorgcm_decrypt. All but the last call for each message must use a length that is a multiple of the block size.
Encrypts or decrypts the data of a message. cipher is the context struct for the underlying cipher and f is the encryption function. All but the last call for each message must use a length that is a multiple of the block size.
Extracts the message digest (also known “authentication tag”). This is the final operation when processing a message. length is usually equal to
GCM_BLOCK_SIZE, but if you provide a smaller value, only the first length octets of the digest are written.
To encrypt a message using GCM, first initialize a context for
the underlying block cipher with a key to use for encryption. Then call
the above functions in the following order: gcm_set_key,
gcm_set_iv, gcm_update, gcm_encrypt,
gcm_digest. The decryption procedure is analogous, just calling
gcm_decrypt instead of gcm_encrypt (note that
GCM decryption still uses the encryption function of the
underlying block cipher). To process a new message, using the same key,
call gcm_set_iv with a new iv.
The following macros are defined.
This defines an all-in-one context struct, including the context of the underlying cipher, the hash subkey, and the per-message state. It expands to
{ context_type cipher; struct gcm_key key; struct gcm_ctx gcm; }
Example use:
struct gcm_aes_ctx GCM_CTX(struct aes_ctx);
The following macros operate on context structs of this form.
First argument, ctx, is a context struct as defined by
GCM_CTX. set_key and encrypt are functions for setting the encryption key and for encrypting data using the underlying cipher. length and data give the key.
First argument is a context struct as defined by
GCM_CTX. length and data give the initialization vector (IV).
Simpler way to call
gcm_update. First argument is a context struct as defined byGCM_CTX
Simpler way to call
gcm_encrypt,gcm_decryptorgcm_digest. First argument is a context struct as defined byGCM_CTX. Second argument, encrypt, is a pointer to the encryption function of the underlying cipher.
The following functions implement the common case of GCM using AES as the underlying cipher.
Initializes ctx using the given key. All valid AES key sizes can be used.
Initializes the per-message state, using the given IV.
Provides associated data to be authenticated. If used, must be called before
gcm_aes_encryptorgcm_aes_decrypt. All but the last call for each message must use a length that is a multiple of the block size.
Encrypts or decrypts the data of a message. All but the last call for each message must use a length that is a multiple of the block size.
Extracts the message digest (also known “authentication tag”). This is the final operation when processing a message. length is usually equal to
GCM_BLOCK_SIZE, but if you provide a smaller value, only the first length octets of the digest are written.
A keyed hash function, or Message Authentication Code (MAC) is a function that takes a key and a message, and produces fixed size MAC. It should be hard to compute a message and a matching MAC without knowledge of the key. It should also be hard to compute the key given only messages and corresponding MACs.
Keyed hash functions are useful primarily for message authentication, when Alice and Bob shares a secret: The sender, Alice, computes the MAC and attaches it to the message. The receiver, Bob, also computes the MAC of the message, using the same key, and compares that to Alice's value. If they match, Bob can be assured that the message has not been modified on its way from Alice.
However, unlike digital signatures, this assurance is not transferable. Bob can't show the message and the MAC to a third party and prove that Alice sent that message. Not even if he gives away the key to the third party. The reason is that the same key is used on both sides, and anyone knowing the key can create a correct MAC for any message. If Bob believes that only he and Alice knows the key, and he knows that he didn't attach a MAC to a particular message, he knows it must be Alice who did it. However, the third party can't distinguish between a MAC created by Alice and one created by Bob.
Keyed hash functions are typically a lot faster than digital signatures as well.
One can build keyed hash functions from ordinary hash functions. Older constructions simply concatenate secret key and message and hashes that, but such constructions have weaknesses. A better construction is HMAC, described in RFC 2104.
For an underlying hash function H, with digest size l and
internal block size b, HMAC-H is constructed as
follows: From a given key k, two distinct subkeys k_i and
k_o are constructed, both of length b. The
HMAC-H of a message m is then computed as H(k_o |
H(k_i | m)), where | denotes string concatenation.
HMAC keys can be of any length, but it is recommended to use
keys of length l, the digest size of the underlying hash function
H. Keys that are longer than b are shortened to length
l by hashing with H, so arbitrarily long keys aren't
very useful.
Nettle's HMAC functions are defined in <nettle/hmac.h>.
There are abstract functions that use a pointer to a struct
nettle_hash to represent the underlying hash function and void *
pointers that point to three different context structs for that hash
function. There are also concrete functions for HMAC-MD5,
HMAC-RIPEMD160 HMAC-SHA1, HMAC-SHA256, and
HMAC-SHA512. First, the abstract functions:
Initializes the three context structs from the key. The outer and inner contexts corresponds to the subkeys
k_oandk_i. state is used for hashing the message, and is initialized as a copy of the inner context.
This function is called zero or more times to process the message. Actually,
hmac_update(state, H, length, data)is equivalent toH->update(state, length, data), so if you wish you can use the ordinary update function of the underlying hash function instead.
Extracts the MAC of the message, writing it to digest. outer and inner are not modified. length is usually equal to
H->digest_size, but if you provide a smaller value, only the first length octets of the MAC are written.This function also resets the state context so that you can start over processing a new message (with the same key).
Like for CBC, there are some macros to help use these functions correctly.
It can be used to define a HMAC context struct, either directly,
struct HMAC_CTX(struct md5_ctx) ctx;
or to give it a struct tag,
struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function (so it must match the type of the components of ctx). The last two arguments specify the secret key.
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function. The last two arguments specify where the digest is written.
Note that there is no HMAC_UPDATE macro; simply call
hmac_update function directly, or the update function of the
underlying hash function.
Now we come to the specialized HMAC functions, which are easier to use than the general HMAC functions.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
MD5_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
RIPEMD160_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA1_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA256_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA512_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Nettle uses GMP, the GNU bignum library, for all calculations
with large numbers. In order to use the public-key features of Nettle,
you must install GMP, at least version 3.0, before compiling
Nettle, and you need to link your programs with -lhogweed -lnettle
-lgmp.
The concept of Public-key encryption and digital signatures was discovered by Whitfield Diffie and Martin E. Hellman and described in a paper 1976. In traditional, “symmetric”, cryptography, sender and receiver share the same keys, and these keys must be distributed in a secure way. And if there are many users or entities that need to communicate, each pair needs a shared secret key known by nobody else.
Public-key cryptography uses trapdoor one-way functions. A
one-way function is a function F such that it is easy to
compute the value F(x) for any x, but given a value
y, it is hard to compute a corresponding x such that
y = F(x). Two examples are cryptographic hash functions, and
exponentiation in certain groups.
A trapdoor one-way function is a function F that is
one-way, unless one knows some secret information about F. If one
knows the secret, it is easy to compute both F and it's inverse.
If this sounds strange, look at the RSA example below.
Two important uses for one-way functions with trapdoors are public-key encryption, and digital signatures. The public-key encryption functions in Nettle are not yet documented; the rest of this chapter is about digital signatures.
To use a digital signature algorithm, one must first create a key-pair: A public key and a corresponding private key. The private key is used to sign messages, while the public key is used for verifying that that signatures and messages match. Some care must be taken when distributing the public key; it need not be kept secret, but if a bad guy is able to replace it (in transit, or in some user's list of known public keys), bad things may happen.
There are two operations one can do with the keys. The signature operation takes a message and a private key, and creates a signature for the message. A signature is some string of bits, usually at most a few thousand bits or a few hundred octets. Unlike paper-and-ink signatures, the digital signature depends on the message, so one can't cut it out of context and glue it to a different message.
The verification operation takes a public key, a message, and a string that is claimed to be a signature on the message, and returns true or false. If it returns true, that means that the three input values matched, and the verifier can be sure that someone went through with the signature operation on that very message, and that the “someone” also knows the private key corresponding to the public key.
The desired properties of a digital signature algorithm are as follows: Given the public key and pairs of messages and valid signatures on them, it should be hard to compute the private key, and it should also be hard to create a new message and signature that is accepted by the verification operation.
Besides signing meaningful messages, digital signatures can be used for authorization. A server can be configured with a public key, such that any client that connects to the service is given a random nonce message. If the server gets a reply with a correct signature matching the nonce message and the configured public key, the client is granted access. So the configuration of the server can be understood as “grant access to whoever knows the private key corresponding to this particular public key, and to no others”.
The RSA algorithm was the first practical digital signature algorithm that was constructed. It was described 1978 in a paper by Ronald Rivest, Adi Shamir and L.M. Adleman, and the technique was also patented in the USA in 1983. The patent expired on September 20, 2000, and since that day, RSA can be used freely, even in the USA.
It's remarkably simple to describe the trapdoor function behind RSA. The “one-way”-function used is
F(x) = x^e mod n
I.e. raise x to the e:th power, while discarding all multiples of
n. The pair of numbers n and e is the public key.
e can be quite small, even e = 3 has been used, although
slightly larger numbers are recommended. n should be about 1000
bits or larger.
If n is large enough, and properly chosen, the inverse of F,
the computation of e:th roots modulo n, is very difficult.
But, where's the trapdoor?
Let's first look at how RSA and creandex-hmac_005fupdate-217">
This function is called zero or more times to process the message. Actually,
hmac_update(state, H, length, data)is equivalent toH->update(state, length, data), so if you wish you can use the ordinary update function of the underlying hash function instead.
Extracts the MAC of the message, writing it to digest. outer and inner are not modified. length is usually equal to
H->digest_size, but if you provide a smaller value, only the first length octets of the MAC are written.This function also resets the state context so that you can start over processing a new message (with the same key).
Like for CBC, there are some macros to help use these functions correctly.
It can be used to define a HMAC context struct, either directly,
struct HMAC_CTX(struct md5_ctx) ctx;
or to give it a struct tag,
struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function (so it must match the type of the components of ctx). The last two arguments specify the secret key.
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function. The last two arguments specify where the digest is written.
Note that there is no HMAC_UPDATE macro; simply call
hmac_update function directly, or the update function of the
underlying hash function.
Now we come to the specialized HMAC functions, which are easier to use than the general HMAC functions.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
MD5_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
RIPEMD160_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA1_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA256_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA512_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Nettle uses GMP, the GNU bignum library, for all calculations
with large numbers. In order to use the public-key features of Nettle,
you must install GMP, at least version 3.0, before compiling
Nettle, and you need to link your programs with -lhogweed -lnettle
-lgmp.
The concept of Public-key encryption and digital signatures was discovered by Whitfield Diffie and Martin E. Hellman and described in a paper 1976. In traditional, “symmetric”, cryptography, sender and receiver share the same keys, and these keys must be distributed in a secure way. And if there are many users or entities that need to communicate, each pair needs a shared secret key known by nobody else.
Public-key cryptography uses trapdoor one-way functions. A
one-way function is a function F such that it is easy to
compute the value F(x) for any x, but given a value
y, it is hard to compute a corresponding x such that
y = F(x). Two examples are cryptographic hash functions, and
exponentiation in certain groups.
A trapdoor one-way function is a function F that is
one-way, unless one knows some secret information about F. If one
knows the secret, it is easy to compute both F and it's inverse.
If this sounds strange, look at the RSA example below.
Two important uses for one-way functions with trapdoors are public-key encryption, and digital signatures. The public-key encryption functions in Nettle are not yet documented; the rest of this chapter is about digital signatures.
To use a digital signature algorithm, one must first create a key-pair: A public key and a corresponding private key. The private key is used to sign messages, while the public key is used for verifying that that signatures and messages match. Some care must be taken when distributing the public key; it need not be kept secret, but if a bad guy is able to replace it (in transit, or in some user's list of known public keys), bad things may happen.
There are two operations one can do with the keys. The signature operation takes a message and a private key, and creates a signature for the message. A signature is some string of bits, usually at most a few thousand bits or a few hundred octets. Unlike paper-and-ink signatures, the digital signature depends on the message, so one can't cut it out of context and glue it to a different message.
The verification operation takes a public key, a message, and a string that is claimed to be a signature on the message, and returns true or false. If it returns true, that means that the three input values matched, and the verifier can be sure that someone went through with the signature operation on that very message, and that the “someone” also knows the private key corresponding to the public key.
The desired properties of a digital signature algorithm are as follows: Given the public key and pairs of messages and valid signatures on them, it should be hard to compute the private key, and it should also be hard to create a new message and signature that is accepted by the verification operation.
Besides signing meaningful messages, digital signatures can be used for authorization. A server can be configured with a public key, such that any client that connects to the service is given a random nonce message. If the server gets a reply with a correct signature matching the nonce message and the configured public key, the client is granted access. So the configuration of the server can be understood as “grant access to whoever knows the private key corresponding to this particular public key, and to no others”.
The RSA algorithm was the first practical digital signature algorithm that was constructed. It was described 1978 in a paper by Ronald Rivest, Adi Shamir and L.M. Adleman, and the technique was also patented in the USA in 1983. The patent expired on September 20, 2000, and since that day, RSA can be used freely, even in the USA.
It's remarkably simple to describe the trapdoor function behind RSA. The “one-way”-function used is
F(x) = x^e mod n
I.e. raise x to the e:th power, while discarding all multiples of
n. The pair of numbers n and e is the public key.
e can be quite small, even e = 3 has been used, although
slightly larger numbers are recommended. n should be about 1000
bits or larger.
If n is large enough, and properly chosen, the inverse of F,
the computation of e:th roots modulo n, is very difficult.
But, where's the trapdoor?
Let's first look at how RSA and creandex-hmac_005fupdate-217">
This function is called zero or more times to process the message. Actually,
hmac_update(state, H, length, data)is equivalent toH->update(state, length, data), so if you wish you can use the ordinary update function of the underlying hash function instead.
Extracts the MAC of the message, writing it to digest. outer and inner are not modified. length is usually equal to
H->digest_size, but if you provide a smaller value, only the first length octets of the MAC are written.This function also resets the state context so that you can start over processing a new message (with the same key).
Like for CBC, there are some macros to help use these functions correctly.
It can be used to define a HMAC context struct, either directly,
struct HMAC_CTX(struct md5_ctx) ctx;
or to give it a struct tag,
struct hmac_md5_ctx HMAC_CTX (struct md5_ctx);
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function (so it must match the type of the components of ctx). The last two arguments specify the secret key.
ctx is a pointer to a context struct as defined by
HMAC_CTX, H is a pointer to aconst struct nettle_hashdescribing the underlying hash function. The last two arguments specify where the digest is written.
Note that there is no HMAC_UPDATE macro; simply call
hmac_update function directly, or the update function of the
underlying hash function.
Now we come to the specialized HMAC functions, which are easier to use than the general HMAC functions.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
MD5_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
RIPEMD160_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA1_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA256_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.
Initializes the context with the key.
Process some more data.
Extracts the MAC, writing it to digest. length may be smaller than
SHA512_DIGEST_SIZE, in which case only the first length octets of the MAC are written.This function also resets the context for processing new messages, with the same key.