Node:Top,
Next:Introduction,
Previous:(dir),
Up:(dir)
This document describes the nettle low-level cryptographic library. You
can use the library directly from your C-programs, or (recommended)
write or use an object-oriented wrapper for your favourite language or
application.
This manual coresponds to version 0.2 of the library.
Node:Introduction,
Next:Copyright,
Previous:Top,
Up:Top
Introduction
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 on 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, testcases, banchmarks,
documentation, etc. For this first version, the only application using
Nettle is LSH, and it uses an object-oriented abstraction on top of the
library.
Node:Copyright,
Next:Conventions,
Previous:Introduction,
Up:Top
Copyright
Nettle is distributed under the GNU General Public License (see the file
COPYING for details). However, many of the individual files are dual
licensed under less restrictive licenses like the GNU Lesser General
Public License, or public domain. Consult the headers in each file for
details.
It is conceivable that future versions will use the LGPL rather than the
GPL, mail me if you have questions or suggestions.
A list of the supported algorithms, their origins and licenses:
- AES
- The implementation of the AES cipher (also known as rijndael) is written
by Rafael Sevilla. Released under the LGPL.
- ARCFOUR
- The implementation of the ARCFOUR (also known as RC4) cipher is written
by Niels Möller. Released under the LGPL.
- BLOWFISH
- The implementation of the BLOWFISH cipher is written by Werner Koch,
copyright owned by the Free Software Foundation. Also hacked by Ray
Dassen and Niels Möller. Released under the GPL.
- CAST128
- The implementation of the CAST128 cipher is written by Steve Reid.
Released into the public domain.
- DES
- The implementation of the DES cipher is written by Dana L. How, and
released under the LGPL.
- MD5
- 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.
- SERPENT
- The implementation of the SERPENT cipher is written by Ross Anderson,
Eli Biham, and Lars Knudsen, adapted to LSH by Rafael Sevilla, and to
Nettle by Niels Möller.
- SHA1
- The 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.
- TWOFISH
- The implementation of the TWOFISH cipher is written by Ruud de Rooij.
Released under the LGPL.
Node:Conventions,
Next:Example,
Previous:Copyright,
Up:Top
Conventions
For each supported algorithm, there is an include file that defines a
context struct, a few constants, and declares functions for
operating on the state. The context struct encapsulates all information
needed by the algorithm, and it can be copied or moved in memory with no
unexpected effects.
The functions for similar algorithms are similar, but there are some
differences, for instance reflecting if the key setup or encryption
function differ for encryption and encryption, and whether or not key
setup can fail. There are also differences that doesn't show in function
prototypes, but which the application must nevertheless be aware of.
There is no difference between stream ciphers and block ciphers,
although they should be used quite differently by the application.
If your application uses more than one algorithm, 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 a
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.
Node:Example,
Next:Reference,
Previous:Conventions,
Up:Top
Example
A simple example program that reads a file from standard in and writes
its SHA1 checksum on stdout should give the flavour of Nettle.
/* FIXME: This code is untested. */
#include <stdio.h>
#include <stdlib.h>
#include <nettle/sha1.h>
#define BUF_SIZE 1000
static void
display_hex(unsigned length, uint8_t *data)
{
static const char digits[16] = "0123456789abcdef";
unsigned i;
for (i = 0; i<length; i++)
{
uint8_t byte = data[i];
printf("%c%c ", digits[(byte / 16) & 0xf], digits[byte & 0xf]);
}
}
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);
if (done <= 0)
break;
sha1_update(&ctx, done, buf);
}
if (ferror(stdin))
return EXIT_FAILURE;
sha1_finish(&ctx);
sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
display_hex(SHA1_DIGEST_SIZE, digest);
return EXIT_SUCCESS;
}
Node:Reference,
Next:Installation,
Previous:Example,
Up:Top
Reference
This chapter describes all the Nettle functions, grouped by family.
Node:Hash functions,
Next:Cipher functions,
Previous:Reference,
Up:Reference
Hash functions
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:
- One-way
- Given a hash value
H(x)
it is hard to find a string x
that hashes to that value.
- Collision-resistant
- 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, associating
unique id:s to documents, and many other things.
MD5
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>
.
struct md5_ctx
|
Context struct |
The size of an MD5 digest, i.e. 16.
|
The internal block size of MD5. Useful for some special constructions,
in particular HMAC-MD5.
|
void md5_init (struct md5_ctx *ctx)
|
Function |
Initialize the MD5 state.
|
void md5_update (struct md5_ctx *ctx, unsigned length, const uint8_t *data)
|
Function |
void md5_final (struct md5_ctx *ctx)
|
Function |
Performs final processing that is needed after all input data has been
processed with md5_update .
|
void md5_digest (struct md5_ctx *ctx, unsigned length, uint8_t *digest)
|
Function |
Extracts the 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 functions doesn't change the state in any way.
|
The normal way to use MD5 is to call the functions in order: First
md5_init
, then md5_update
zero or more times, then
md5_final
, and at last md5_digest
zero or more times.
To start over, you can call md5_init
at any time.
SHA1
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>
.
The functions are analogous to the MD5 ones.
struct sha1_ctx
|
Context struct |
SHA1_DIGEST_SIZE
|
Constant |
The size of an SHA1 digest, i.e. 20.
|
The internal block size of SHA1. Useful for some special constructions,
in particular HMAC-SHA1.
|
void sha1_init (struct sha1_ctx *ctx)
|
Function |
Initialize the SHA1 state.
|
void sha1_update (struct sha1_ctx *ctx, unsigned length, const uint8_t *data)
|
Function |
void sha1_final (struct sha1_ctx *ctx)
|
Function |
Performs final processing that is needed after all input data has been
processed with sha1_update .
|
void sha1_digest (struct sha1_ctx *ctx, unsigned length, uint8_t *digest)
|
Function |
Extracts the 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 functions doesn't change the state in any way.
|
Node:Cipher functions,
Next:Miscellaneous functions,
Previous:Hash functions,
Up:Reference
Cipher functions
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
cleartext. Given matching pairs of plaintext and ciphertext, it should
be hard to find the key.
To do this, you first initialize the cipher context for encryption or
decryption with a particular key, then use it to process plaintext och
ciphertext messages. The initialization is also called 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.
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) being one
of the most popular.
A stream cipher can be used for messages of arbitrary length; a typical
stream cipher is a keyed pseudorandom generator. To encrypt a plaintext
message of n octets, you key the generator, generate n
octets of pseudorandom 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, apply the key, and get a cleartext 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 anser 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
Authetication Codes like HMAC-SHA1, or digital signatures.
Some ciphers have so called "weak keys", keys that results in
undesirable structure after the key setup processing, and should be
avoided. In Nettle, the presence of weak keys for a cipher mean that the
key setup function can fail, so you have to check its return value. In
addition, the context struct has a field status
, that is set to a
non-zero value if key setup fails. When possible, avoid algorithm that
have weak keys. There are several good ciphers that don't have any weak
keys.
AES
AES is a quite new block cipher, specified by NIST as a replacement for
the older DES standard. It is the result of a competition between cipher
designers, and the winning design, constructed by Joan Daemen and
Vincent Rijnmen. Before it won the competition, it was known under the
name RIJNDAEL.
Like all the AES candidates, the winning design uses a block size of 128
bits, or 16 octets, and variable keysize, 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>
.
struct aes_ctx
|
Context struct |
AES_MIN_KEY_SIZE
|
Constant |
AES_MAX_KEY_SIZE
|
Constant |
void aes_set_key (struct aes_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption.
|
void aes_encrypt (struct aes_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void aes_decrypt (struct aes_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
ARCFOUR
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. For example
/* A more robust key setup function for ARCFOUR */
void
my_arcfour_set_key(struct arcfour_ctx *ctx,
unsigned length, const uint8_t *key)
{
struct sha1_ctx hash;
uint8_t digest[SHA1_DIGEST_SIZE];
sha1_init(&hash);
sha1_update(&hash, length, key);
sha1_final(&hash);
sha1_digest(&hash, SHA1_DIGEST_SIZE, digest);
arcfour_set_key(ctx, SHA1_DIGEST_SIZE, digest);
}
Nettle defines ARCFOUR in <nettle/arcfour.h>
.
struct arcfour_ctx
|
Context struct |
ARCFOUR_BLOCK_SIZE
|
Constant |
The ARCFOUR blocksize, 16
|
ARCFOUR_MIN_KEY_SIZE
|
Constant |
ARCFOUR_MAX_KEY_SIZE
|
Constant |
ARCFOUR_KEY_SIZE
|
Constant |
Default ARCFOUR key size, 16
|
void arcfour_set_key (struct arcfour_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption.
|
void arcfour_crypt (struct arcfour_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
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.
|
CAST128
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>
.
struct cast128_ctx
|
Context struct |
CAST128_BLOCK_SIZE
|
Constant |
CAST128_MIN_KEY_SIZE
|
Constant |
Minumim CAST128 key size, 5
|
CAST128_MAX_KEY_SIZE
|
Constant |
Maximum CAST128 key size, 16
|
CAST128_KEY_SIZE
|
Constant |
Default CAST128 key size, 16
|
void cast128_set_key (struct cast128_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption.
|
void cast128_encrypt (struct cast128_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void cast128_decrypt (struct cast128_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
Analogous to cast128_encrypt
|
BLOWFISH
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>
.
struct blowfish_ctx
|
Context struct |
BLOWFISH_BLOCK_SIZE
|
Constant |
The BLOWFISH blocksize, 8
|
BLOWFISH_MIN_KEY_SIZE
|
Constant |
Minimum BLOWFISH key size, 8
|
BLOWFISH_MAX_KEY_SIZE
|
Constant |
Maximum BLOWFISH key size, 56
|
BLOWFISH_KEY_SIZE
|
Constant |
Default BLOWFISH key size, 16
|
int blowfish_set_key (struct blowfish_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption. Returns 1 on success, and 0 if the key was weak. Calling
blowfish_encrypt or blowfish_decrypt with a weak key will
crash with an assert violation.
|
void blowfish_encrypt (struct blowfish_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void blowfish_decrypt (struct blowfish_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
Analogous to blowfish_encrypt
|
DES
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 is 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
parallell. One shouldn't be using plain DES at all today, if one uses
DES at all one should be using "triple DES", three DES ciphers piped
together, with three (or sometimes just two) independent keys.
DES also has some weak keys. Nettle defines DES in <nettle/des.h>
.
struct des_ctx
|
Context struct |
int des_set_key (struct des_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption. Returns 1 on success, and 0 if the key was weak or had bad
parity. Calling des_encrypt or des_decrypt with a bad key
will crash with an assert violation.
|
void des_encrypt (struct des_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void des_decrypt (struct des_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
SERPENT
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 pecularity 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>
.
struct serpent_ctx
|
Context struct |
SERPENT_BLOCK_SIZE
|
Constant |
The SERPENT blocksize, 16
|
SERPENT_MIN_KEY_SIZE
|
Constant |
Minumim SERPENT key size, 16
|
SERPENT_MAX_KEY_SIZE
|
Constant |
Maximum SERPENT key size, 32
|
SERPENT_KEY_SIZE
|
Constant |
Default SERPENT key size, 32
|
void serpent_set_key (struct serpent_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption.
|
void serpent_encrypt (struct serpent_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void serpent_decrypt (struct serpent_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
Analogous to serpent_encrypt
|
TWOFISH
Another AES finalist, this one designed by Bruce Schneier and others.
Nettle defines it in <nettle/twofish.h>
.
struct twofish_ctx
|
Context struct |
TWOFISH_BLOCK_SIZE
|
Constant |
The TWOFISH blocksize, 16
|
TWOFISH_MIN_KEY_SIZE
|
Constant |
Minumim TWOFISH key size, 16
|
TWOFISH_MAX_KEY_SIZE
|
Constant |
Maximum TWOFISH key size, 32
|
TWOFISH_KEY_SIZE
|
Constant |
Default TWOFISH key size, 32
|
void twofish_set_key (struct twofish_ctx *ctx, unsigned length, const uint8_t *key)
|
Function |
Initialize the cipher. The same function is used for both encryption and
decryption.
|
void twofish_encrypt (struct twofish_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
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.
|
void twofish_decrypt (struct twofish_ctx *ctx, unsigned length, const uint8_t *dst, uint8_t *src)
|
Function |
Analogous to twofish_encrypt
|
Node:Miscellaneous functions,
Previous:Cipher functions,
Up:Reference
Miscellaneous functions
uint8_t * memxor (uint8_t *dst, const uint8_t *src, size_t n)
|
Function |
XOR:s the source area on top of the destination area. The interface
doesn't follow the Nettle conventions, because it is intended to be
similar to the ANSI-C memcpy function.
|
Node:Installation,
Next:Index,
Previous:Reference,
Up:Top
Installation
Nettle uses autoconf
and automake
. To build it,
unpack the source and run
./configure
make
make check
make install
to install in the default location, /usr/local
. The library is
installed in /use/local/lib/libnettle.a
and the include files are
installed in /use/local/include/nettle/
.
Only static libraries are installed.
Node:Index,
Previous:Installation,
Up:Top
Function and Concept Index