LibTomCrypt - Open Source
LibTomCrypt - Open Source
Developer Manual
Tom St Denis
LibTom Projects
This document is part of the LibTomCrypt package and is hereby released into the public
domain.
Tom St Denis
Ottawa, Ontario
Canada
Contents
1 Introduction 1
1.1 What is the LibTomCrypt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 What the library IS for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why did I write it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.1 Modular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Patent Disclosure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Thanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
iii
3.4.8 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4.9 LRW Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.4.10 XTS Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4.11 F8 Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.5 Encrypt and Authenticate Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5.1 EAX Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5.2 OCB Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5.3 CCM Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.5.4 GCM Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10 Standards Support 95
10.1 ASN.1 Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
10.1.1 SEQUENCE Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
10.1.2 SET and SET OF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
10.1.3 ASN.1 INTEGER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
10.1.4 ASN.1 BIT STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
10.1.5 ASN.1 OCTET STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
10.1.6 ASN.1 OBJECT IDENTIFIER . . . . . . . . . . . . . . . . . . . . . . . . . . 101
10.1.7 ASN.1 IA5 STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
10.1.8 ASN.1 PRINTABLE STRING . . . . . . . . . . . . . . . . . . . . . . . . . . 102
10.1.9 ASN.1 UTF8 STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.1.10 ASN.1 UTCTIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.1.11 ASN.1 CHOICE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10.1.12 ASN.1 Flexi Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10.2 Password Based Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
10.2.1 PKCS #5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
10.2.2 Algorithm One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
10.2.3 Algorithm Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
11 Miscellaneous 109
11.1 Base64 Encoding and Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
11.2 Primality Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
14 Optimizations 123
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
14.2 Ciphers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
14.2.1 Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.2 Internal ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.3 Key Lengths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.4 Block Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.5 Rounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.6 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.7 Single block ECB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
14.2.8 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
14.2.9 Key Sizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
14.2.10 Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
14.3 One–Way Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
14.3.1 Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
14.3.2 Internal ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
14.3.3 Digest Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
14.3.4 Block Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
14.3.5 OID Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
14.3.6 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.3.7 Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.3.8 Done . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.3.9 Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.3.10 HMAC Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.4 Pseudo–Random Number Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
14.4.1 Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.2 Export Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.3 Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.4 Entropy Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.5 Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.6 Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.7 Done . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.4.8 Exporting and Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.5 BigNum Math Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.5.1 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
14.5.2 ECC Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
14.5.3 RSA Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
List of Figures
ix
x
Chapter 1
Introduction
1. I am too lazy to figure out someone else’s API. I’d rather invent my own simpler API and use
that.
The idea is that I am not striving to replace OpenSSL or Crypto++ or Cryptlib or etc. I’m
trying to write my own crypto library and hopefully along the way others will appreciate the work.
1
2 www.libtom.org
With this library all core functions (ciphers, hashes, prngs, and bignum) have the same prototype
definition. They all load and store data in a format independent of the platform. This means if you
encrypt with Blowfish on a PPC it should decrypt on an x86 with zero problems. The consistent
API also means that if you learn how to use Blowfish with the library you know how to use Safer+,
RC6, or Serpent as well. With all of the core functions there are central descriptor tables that can
be used to make a program automatically pick between ciphers, hashes and PRNGs at run-time.
That means your application can support all ciphers/hashes/prngs/bignum without changing the
source code.
Not only did I strive to make a consistent and simple API to work with but I also attempted
to make the library configurable in terms of its build options. Out of the box the library will build
with any modern version of GCC without having to use configure scripts. This means that the
library will work with platforms where development tools may be limited (e.g. no autoconf).
On top of making the build simple and the API approachable I’ve also attempted for a reasonably
high level of robustness and efficiency. LibTomCrypt traps and returns a series of errors ranging
from invalid arguments to buffer overflows/overruns. It is mostly thread safe and has been clocked
on various platforms with cycles per byte timings that are comparable (and often favourable) to
other libraries such as OpenSSL and Crypto++.
1.2.1 Modular
The LibTomCrypt package has also been written to be very modular. The block ciphers, one–way
hashes, pseudo–random number generators (PRNG), and bignum math routines are all used within
the API through descriptor tables which are essentially structures with pointers to functions. While
you can still call particular functions directly (e.g. sha256 process()) this descriptor interface allows
the developer to customize their usage of the library.
For example, consider a hardware platform with a specialized RNG device. Obviously one would
like to tap that for the PRNG needs within the library (e.g. making a RSA key). All the developer
has to do is write a descriptor and the few support routines required for the device. After that
the rest of the API can make use of it without change. Similarly imagine a few years down the
road when AES2 (or whatever they call it) has been invented. It can be added to the library and
used within applications with zero modifications to the end applications provided they are written
properly.
This flexibility within the library means it can be used with any combination of primitive
algorithms and unlike libraries like OpenSSL is not tied to direct routines. For instance, in OpenSSL
there are CBC block mode routines for every single cipher. That means every time you add or
remove a cipher from the library you have to update the associated support code as well. In
LibTomCrypt the associated code (chaining modes in this case) are not directly tied to the ciphers.
That is a new cipher can be added to the library by simply providing the key setup, ECB decrypt
and encrypt and test vector routines. After that all five chaining mode routines can make use of
the cipher right away.
1.3 License
The project is hereby released as public domain.
1.4 Patent Disclosure 3
1.5 Thanks
I would like to give thanks to the following people (in no particular order) for helping me develop
this project from early on:
2. Richard Heathfield
3. Ajay K. Agrawal
4. Brian Gladman
5. Svante Seleborg
6. Clay Culver
7. Jason Klapste
8. Dobes Vandermeer
9. Daniel Richards
10. Wayne Scott
There have been quite a few other people as well. Please check the change log to see who else
has contributed from time to time.
4 www.libtom.org
Chapter 2
2.1 Introduction
In general the API is very simple to memorize and use. Most of the functions return either void
or int. Functions that return int will return CRYPT OK if the function was successful, or one
of the many error codes if it failed. Certain functions that return int will return −1 to indicate an
error. These functions will be explicitly commented upon. When a function does return a CRYPT
error code it can be translated into a string with
There is no initialization routine for the library and for the most part the code is thread safe.
The only thread related issue is if you use the same symmetric cipher, hash or public key state data
in multiple threads. Normally that is not an issue.
To include the prototypes for LibTomCrypt.a into your own program simply include tomcrypt.h
like so:
#include <tomcrypt.h>
int main(void) {
5
6 www.libtom.org
return 0;
}
The header file tomcrypt.h also includes stdio.h, string.h, stdlib.h, time.h and ctype.h.
2.2 Macros
There are a few helper macros to make the coding process a bit easier. The first set are related to
loading and storing 32/64-bit words in little/big endian format. The macros are:
2.3 Functions with Variable Length Output 7
return 0;
}
In the above example if the size of the RSA public key was more than 1024 bytes this function would
return an error code indicating a buffer overflow would have occurred. If the function succeeds,
it stores the length of the output back into x so that the calling application will know how many
bytes were used.
As of v1.13, most functions will update your length on failure to indicate the size required by
the function. Not all functions support this so please check the source before you rely on it doing
that.
#include <tomcrypt.h>
int main(void) {
rsa_key key;
int err;
return 0;
}
2.5 Functions that use Arrays of Octets 9
The XXX setup() routine will setup the cipher to be used with a given number of rounds and
a given key length (in bytes). The number of rounds can be set to zero to use the default, which is
generally a good idea.
If the function returns successfully the variable skey will have a scheduled key stored in it. It’s
important to note that you should only used this scheduled key with the intended cipher. For
example, if you call blowfish setup() do not pass the scheduled key onto rc5 ecb encrypt(). All
built–in setup functions do not allocate memory off the heap so when you are done with a key you
can simply discard it (e.g. they can be on the stack). However, to maintain proper coding practices
you should always call the respective XXX done() function. This allows for quicker porting to
applications with externally supplied plugins.
11
12 www.libtom.org
3.1.3 Self–Testing
int XXX_test(void);
This function will return CRYPT OK if the cipher matches the test vectors from the design
publication it is based upon.
/* now given a 20 byte key what keysize does Twofish want to use? */
keysize = 20;
if ((err = twofish_keysize(&keysize)) != CRYPT_OK) {
printf("Error getting key size: %s\n", error_to_string(err));
return -1;
}
printf("Twofish suggested a key size of %d\n", keysize);
return 0;
}
1 The size of which depends on which cipher you are using.
2 pt stands for plaintext.
3 ct stands for ciphertext.
4 As published in their design papers.
3.1 Core Functions 13
For the software based ciphers within LibTomCrypt, these functions will not do anything. However,
user supplied cipher descriptors may require to be called for resource management purposes. To be
compliant, all functions which call a cipher setup function must also call the respective cipher done
function when finished.
#include <tomcrypt.h>
int main(void)
{
unsigned char pt[8], ct[8], key[8];
symmetric_key skey;
int err;
return 0;
}
struct _cipher_descriptor {
/** name of cipher */
char *name;
/** internal ID */
unsigned char ID;
Where name is the lower case ASCII version of the name. The fields min key length and
max key length are the minimum and maximum key sizes in bytes. The block length member is the
block size of the cipher in bytes. As a good rule of thumb it is assumed that the cipher supports
the min and max key lengths but not always everything in between. The default rounds field is the
default number of rounds that will be used.
For a plugin to be compliant it must provide at least each function listed before the accelerators
begin. Accelerators are optional, and if missing will be emulated in software.
The remaining fields are all pointers to the core functions for each cipher. The end of the
cipher descriptor array is marked when name equals NULL.
As of this release the current cipher descriptors elements are the following:
3.3.1 Notes
1. For AES, (also known as Rijndael) there are four descriptors which complicate issues a little. The
descriptors rijndael desc and rijndael enc desc provide the cipher named rijndael. The descriptors
aes desc and aes enc desc provide the cipher name aes. Functionally both rijndael and aes are the
same cipher. The only difference is when you call find cipher() you have to pass the correct name. The
cipher descriptors with enc in the middle (e.g. rijndael enc desc) are related to an implementation of
Rijndael with only the encryption routine and tables. The decryption and self–test function pointers
of both encrypt only descriptors are set to NULL and should not be called.
The encrypt only descriptors are useful for applications that only use the encryption function of the
cipher. Algorithms such as EAX, PMAC and OMAC only require the encryption function. So far
this encrypt only functionality has only been implemented for Rijndael as it makes the most sense
for this cipher.
2. Note that for DES and 3DES they use 8 and 24 byte keys but only 7 and 21 [respectively] bytes of
the keys are in fact used for the purposes of encryption. My suggestion is just to use random 8/24
byte keys instead of trying to make a 8/24 byte string from the real 7/21 byte key.
3. Note that Twofish has additional configuration options (Figure 3.2) that take place at build time.
These options are found in the file tomcrypt cfg.h. The first option is TWOFISH SMALL which when
defined will force the Twofish code to not pre-compute the Twofish g(X) function as a set of four
8 × 32 s-boxes. This means that a scheduled key will require less ram but the resulting cipher will be
slower. The second option is TWOFISH TABLES which when defined will force the Twofish code to
use pre-computed tables for the two s-boxes q0 , q1 as well as the multiplication by the polynomials 5B
and EF used in the MDS multiplication. As a result the code is faster and slightly larger. The speed
increase is useful when TWOFISH SMALL is defined since the s-boxes and MDS multiply form the
heart of the Twofish round function.
if (register_cipher(&blowfish_desc)) == -1) {
printf("Unable to register Blowfish cipher.");
return -1;
}
/* use Rijndael */
/* remove it */
if ((err = unregister_cipher(&rijndael_desc)) != CRYPT_OK) {
printf("Error removing Rijndael: %s\n", error_to_string(err));
return -1;
}
return 0;
}
This snippet is a small program that registers Rijndael.
18 www.libtom.org
ECB Mode
ECB or Electronic Codebook Mode is the simplest method to use. It is given as:
Ci = Ek (Pi ) (3.1)
This mode is very weak since it allows people to swap blocks and perform replay attacks if the same
key is used more than once.
CBC Mode
CBC or Cipher Block Chaining mode is a simple mode designed to prevent trivial forms of replay
and swap attacks on ciphers. It is given as:
Ci = Ek (Pi ⊕ Ci−1 ) (3.2)
It is important that the initial vector be unique and preferably random for each message encrypted
under the same key.
CTR Mode
CTR or Counter Mode is a mode which only uses the encryption function of the cipher. Given a
initial vector which is treated as a large binary counter the CTR mode is given as:
C−1 = C−1 + 1 (mod 2W )
Ci = Pi ⊕ Ek (C−1 ) (3.3)
Where W is the size of a block in bits (e.g. 64 for Blowfish). As long as the initial vector is random
for each message encrypted under the same key replay and swap attacks are infeasible. CTR mode
may look simple but it is as secure as the block cipher is under a chosen plaintext attack (provided
the initial vector is unique).
CFB Mode
CFB or Ciphertext Feedback Mode is a mode akin to CBC. It is given as:
Ci = Pi ⊕ C−1
C−1 = Ek (Ci ) (3.4)
Note that in this library the output feedback width is equal to the size of the block cipher. That is
this mode is used to encrypt whole blocks at a time. However, the library will buffer data allowing
the user to encrypt or decrypt partial blocks without a delay. When this mode is first setup it will
initially encrypt the initial vector as required.
3.4 Symmetric Modes of Operations 19
OFB Mode
OFB or Output Feedback Mode is a mode akin to CBC as well. It is given as:
C−1 = Ek (C−1 )
Ci = Pi ⊕ C−1 (3.5)
Like the CFB mode the output width in CFB mode is the same as the width of the block cipher.
OFB mode will also buffer the output which will allow you to encrypt or decrypt partial blocks
without delay.
2. Provably as secure as the block cipher being used under a chosen plaintext attack.
5. Allows the encryption of block sizes that are not equal to the size of the block cipher.
The CTR, CFB and OFB routines provided allow you to encrypt block sizes that differ from the
ciphers block size. They accomplish this by buffering the data required to complete a block. This
allows you to encrypt or decrypt any size block of memory with either of the three modes.
The ECB and CBC modes process blocks of the same size as the cipher at a time. Therefore,
they are less flexible than the other modes.
3.4.4 Initialization
The library provides simple support routines for handling CBC, CTR, CFB, OFB and ECB encoded
messages. Assuming the mode you want is XXX there is a structure called symmetric XXX that
will contain the information required to use that mode. They have identical setup routines (except
CTR and ECB mode):
20 www.libtom.org
In each case, cipher is the index into the cipher descriptor array of the cipher you want to
use. The IV value is the initialization vector to be used with the cipher. You must fill the IV
yourself and it is assumed they are the same length as the block size5 of the cipher you choose. It
is important that the IV be random for each unique message you want to encrypt. The parameters
key, keylen and num rounds are the same as in the XXX setup() function call. The final parameter
is a pointer to the structure you want to hold the information for the mode of operation.
The routines return CRYPT OK if the cipher initialized correctly, otherwise, they return an
error code.
CTR Mode
In the case of CTR mode there is an additional parameter ctr mode which specifies the mode that
the counter is to be used in. If CTR COUNTER LITTLE ENDIAN was specified then the
counter will be treated as a little endian value. Otherwise, if CTR COUNTER BIG ENDIAN
was specified the counter will be treated as a big endian value. As of v1.15 the RFC 3686 style of
increment then encrypt is also supported. By OR’ing LTC CTR RFC3686 with the CTR mode
value, ctr start() will increment the counter before encrypting it for the first time.
As of V1.17, the library supports variable length counters for CTR mode. The (optional) counter
length is specified by OR’ing the octet length of the counter against the ctr mode parameter. The
default, zero, indicates that a full block length counter will be used. This also ensures backwards
compatibility with software that uses older versions of the library.
symmetric_CTR ctr;
int err;
unsigned char IV[16], key[16];
5 In other words the size of a block of plaintext for the cipher, e.g. 8 for DES, 16 for AES, etc.
3.4 Symmetric Modes of Operations 21
Changing the counter size has little (really no) effect on the performance of the CTR chaining
mode. It is provided for compatibility with other software (and hardware) which have smaller fixed
sized counters.
3.4.6 IV Manipulation
To change or read the IV of a previously initialized chaining mode use the following two functions.
The XXX getiv() functions will read the IV out of the chaining mode and store it into IV along
with the length of the IV stored in len. The XXX setiv will initialize the chaining mode state as if
the original IV were the new IV specified. The length of the IV passed in must be the size of the
ciphers block size.
The XXX setiv() functions are handy if you wish to change the IV without re–keying the cipher.
What the setiv function will do depends on the mode being changed. In CBC mode, the new IV
replaces the existing IV as if it were the last ciphertext block. In CFB mode, the IV is encrypted
as if it were the prior encrypted pad. In CTR mode, the IV is encrypted without first incrementing
it (regardless of the LTC RFC 3686 flag presence). In F8 mode, the IV is encrypted and becomes
the new pad. It does not change the salted IV, and is only meant to allow seeking within a session.
In LRW, it changes the tweak, forcing a computation of the tweak pad, allowing for seeking within
the session. In OFB mode, the IV is encrypted and becomes the new pad.
This will terminate the stream (by terminating the cipher) and return CRYPT OK if success-
ful.
3.4 Symmetric Modes of Operations 23
3.4.8 Examples
#include <tomcrypt.h>
int main(void)
{
unsigned char key[16], IV[16], buffer[512];
symmetric_CTR ctr;
int x, err;
return 0;
}
This will initialize the LRW context with the given (16 octet) IV, cipher K1 key of length
keylen octets and the (16 octet) K2 tweak. While LRW was specified to be used only with AES,
LibTomCrypt will allow any 128–bit block cipher to be specified as indexed by cipher. The number
of rounds for the block cipher num rounds can be 0 to use the default number of rounds for the
given cipher.
To process data use the following functions:
3.4 Symmetric Modes of Operations 25
These will encrypt (or decrypt) the plaintext to the ciphertext buffer (or vice versa). The length
is specified by len in octets but must be a multiple of 16. The LRW code uses a fast tweak update
such that consecutive blocks are encrypted faster than if random seeking where used.
To manipulate the IV use the following functions:
These will get or set the 16–octet IV. Note that setting the IV is the same as seeking and unlike
other modes is not a free operation. It requires updating the entire tweak which is slower than
sequential use. Avoid seeking excessively in performance constrained code.
To terminate the LRW state use the following:
This will start the XTS mode with the two keys pointed to by key1 and key2 of length keylen octets
each.
To encrypt or decrypt a sector use the following calls:
int xts_encrypt(
const unsigned char *pt, unsigned long ptlen,
unsigned char *ct,
const unsigned char *tweak,
symmetric_xts *xts);
int xts_decrypt(
const unsigned char *ct, unsigned long ptlen,
unsigned char *pt,
const unsigned char *tweak,
symmetric_xts *xts);
The first will encrypt the plaintext pointed to by pt of length ptlen octets, and store the ciphertext
in the array pointed to by ct. It uses the 128–bit tweak pointed to by tweak to encrypt the block.
The decrypt function performs the opposite operation. Both functions support ciphertext stealing
(blocks that are not multiples of 16 bytes).
The P1619 specification states the tweak for sector number shall be represented as a 128–bit
little endian string.
To terminate the XTS state call the following function:
3.4.11 F8 Mode
The F8 Chaining mode (see RFC 3711 for instance) is yet another chaining mode for block ciphers.
It behaves much like CTR mode in that it XORs a keystream against the plaintext to encrypt. F8
mode comes with the additional twist that the counter value is secret, encrypted by a salt key. We
initialize F8 mode with the following function call:
This will start the F8 mode state using key as the secret key, IV as the counter. It uses the salt key
as IV encryption key (m in the RFC 3711). The salt key can be shorter than the secret key but it
should not be longer.
To encrypt or decrypt data we use the following two functions:
3.4 Symmetric Modes of Operations 27
These will encrypt or decrypt a variable length array of bytes using the F8 mode state specified.
The length is specified in bytes and does not have to be a multiple of the ciphers block size.
To change or retrieve the current counter IV value use the following functions:
These work with the current IV value only and not the encrypted IV value specified during the call
to f8 start(). The purpose of these two functions is to be able to seek within a current session only.
If you want to change the session IV you will have to call f8 done() and then start a new state with
f8 start().
To terminate an F8 state call the following function:
The function eax encrypt will encrypt the bytes in pt of length octets, and store the ciphertext in
ct. Note: ct and pt may be the same region in memory. This function will also send the ciphertext
through the OMAC function. The function eax decrypt decrypts ct, and stores it in pt. This also
allows pt and ct to be the same region in memory.
You cannot both encrypt or decrypt with the same eax context. For bi–directional communica-
tion you will need to initialize two EAX contexts (preferably with different headers and nonces).
Note: both of these functions allow you to send the data in any granularity but the order is
important. While the eax init() function allows you to add initial header data to the stream you
can also add header data during the EAX stream with the following.
This will add the length octet from header to the given eax header. Once the message is finished,
the tag (checksum) may be computed with the following function:
This will terminate the EAX state eax, and store up to taglen bytes of the message tag in tag. The
function then stores how many bytes of the tag were written out back in to taglen.
The EAX mode code can be tested to ensure it matches the test vectors by calling the following
function:
int eax_test(void);
This requires that the AES (or Rijndael) block cipher be registered with the cipher descriptor table
first.
#include <tomcrypt.h>
int main(void)
{
int err;
eax_state eax;
unsigned char pt[64], ct[64], nonce[16], key[16], tag[16];
unsigned long taglen;
if (register_cipher(&rijndael_desc) == -1) {
printf("Error registering Rijndael");
return EXIT_FAILURE;
}
/* initialize context */
if ((err = eax_init( &eax, /* context */
30 www.libtom.org
find_cipher("rijndael"), /* cipher id */
nonce, /* the nonce */
16, /* nonce is 16 bytes */
"TestApp", /* example header */
7) /* header length */
) != CRYPT_OK) {
printf("Error eax_init: %s", error_to_string(err));
return EXIT_FAILURE;
}
You can also perform an entire EAX state on a block of memory in a single function call with
the following functions.
int eax_encrypt_authenticate_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
const unsigned char *pt, unsigned long ptlen,
unsigned char *ct,
unsigned char *tag, unsigned long *taglen);
int eax_decrypt_verify_memory(
3.5 Encrypt and Authenticate Modes 31
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
const unsigned char *ct, unsigned long ctlen,
unsigned char *pt,
unsigned char *tag, unsigned long taglen,
int *res);
Both essentially just call eax init() followed by eax encrypt() (or eax decrypt() respectively)
and eax done(). The parameters have the same meaning as with those respective functions.
The only difference is eax decrypt verify memory() does not emit a tag. Instead you pass it a
tag as input and it compares it against the tag it computed while decrypting the message. If the
tags match then it stores a 1 in res, otherwise it stores a 0.
This will initialize the ocb context using cipher descriptor cipher. It will use a key of length
keylen and the random nonce. Note that nonce must be a random (public) string the same length
as the block ciphers block size (e.g. 16 bytes for AES).
This mode has no Associated Data like EAX mode does which means you cannot authenticate
metadata along with the stream. To encrypt or decrypt data use the following.
This will encrypt (or decrypt for the latter) a fixed length of data from pt to ct (vice versa for
the latter). They assume that pt and ct are the same size as the block cipher’s block size. Note
that you cannot call both functions given a single ocb state. For bi-directional communication you
will have to initialize two ocb states (with different nonces). Also pt and ct may point to the same
location in memory.
9 See P. Rogaway, M. Bellare, J. Black, T. Krovetz, OCB: A Block Cipher Mode of Operation for Efficient
Authenticated Encryption.
32 www.libtom.org
State Termination
When you are finished encrypting the message you call the following function to compute the tag.
This will terminate an encrypt stream ocb. If you have trailing bytes of plaintext that will not
complete a block you can pass them here. This will also encrypt the ptlen bytes in pt and store
them in ct. It will also store up to taglen bytes of the tag into tag.
Note that ptlen must be less than or equal to the block size of block cipher chosen. Also note
that if you have an input message equal to the length of the block size then you pass the data here
(not to ocb encrypt()) only.
To terminate a decrypt stream and compared the tag you call the following.
Similarly to the previous function you can pass trailing message bytes into this function. This will
compute the tag of the message (internally) and then compare it against the taglen bytes of tag
provided. By default res is set to zero. If all taglen bytes of tag can be verified then res is set to
one (authenticated message).
Packet Functions
To make life simpler the following two functions are provided for memory bound OCB.
int ocb_encrypt_authenticate_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *nonce,
const unsigned char *pt, unsigned long ptlen,
unsigned char *ct,
unsigned char *tag, unsigned long *taglen);
This will OCB encrypt the message pt of length ptlen, and store the ciphertext in ct. The length
ptlen can be any arbitrary length.
3.5 Encrypt and Authenticate Modes 33
int ocb_decrypt_verify_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *nonce,
const unsigned char *ct, unsigned long ctlen,
unsigned char *pt,
const unsigned char *tag, unsigned long taglen,
int *res);
Similarly, this will OCB decrypt, and compare the internally computed tag against the tag
provided. res is set appropriately.
int ccm_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
unsigned char *ct,
unsigned char *tag, unsigned long *taglen,
int direction);
This performs the CCM operation on the data. The cipher variable indicates which cipher in
the descriptor table to use. It must have a 16–byte block size for CCM.
The key can be specified in one of two fashions. First, it can be passed as an array of octets in
key of length keylen. Alternatively, it can be passed in as a previously scheduled key in uskey. The
latter fashion saves time when the same key is used for multiple packets. If uskey is not NULL,
then key may be NULL (and vice-versa).
The nonce or salt is nonce of length noncelen octets. The header is meta–data you want to send
with the message but not have encrypted, it is stored in header of length headerlen octets. The
header can be zero octets long (if headerlen = 0 then you can pass header as NULL).
The plaintext is stored in pt, and the ciphertext in ct. The length of both are expected to be
equal and is passed in as ptlen. It is allowable that pt = ct. The direction variable indicates whether
encryption (direction = CCM ENCRYPT) or decryption (direction = CCM DECRYPT) is
to be performed.
As implemented, this version of CCM cannot handle header or plaintext data longer than 232 −1
octets long.
You can test the implementation of CCM with the following function.
int ccm_test(void);
34 www.libtom.org
This will return CRYPT OK if the CCM routine passes known test vectors. It requires AES
or Rijndael to be registered previously, otherwise it will return CRYPT NOP.
CCM Example
The following is a sample of how to call CCM.
#include <tomcrypt.h>
int main(void)
{
unsigned char key[16], nonce[12], pt[32], ct[32],
tag[16], tagcp[16];
unsigned long taglen;
int err;
/* register cipher */
register_cipher(&aes_desc);
/* encrypt it */
taglen = sizeof(tag);
if ((err =
ccm_memory(find_cipher("aes"),
key, 16, /* 128-bit key */
NULL, /* not prescheduled */
nonce, 12, /* 96-bit nonce */
NULL, 0, /* no header */
pt, 32, /* 32-byte plaintext */
ct, /* ciphertext */
tag, &taglen,
CCM_ENCRYPT)) != CRYPT_OK) {
printf("ccm_memory error %s\n", error_to_string(err));
return -1;
}
/* ct[0..31] and tag[0..15] now hold the output */
/* decrypt it */
taglen = sizeof(tagcp);
if ((err =
ccm_memory(find_cipher("aes"),
key, 16, /* 128-bit key */
NULL, /* not prescheduled */
nonce, 12, /* 96-bit nonce */
NULL, 0, /* no header */
ct, 32, /* 32-byte ciphertext */
pt, /* plaintext */
tagcp, &taglen,
CCM_DECRYPT)) != CRYPT_OK) {
printf("ccm_memory error %s\n", error_to_string(err));
3.5 Encrypt and Authenticate Modes 35
return -1;
}
Initialization
To initialize the GCM context with a secret key call the following function.
This initializes the GCM state gcm for the given cipher indexed by cipher, with a secret key key of
length keylen octets. The cipher chosen must have a 16–byte block size (e.g., AES).
Initial Vector
After the state has been initialized (or reset) the next step is to add the session (or packet) initial
vector. It should be unique per packet encrypted.
This adds the initial vector octets from IV of length IVlen to the GCM state gcm. You can call
this function as many times as required to process the entire IV.
Note: the GCM protocols provides a shortcut for 12–byte IVs where no pre-processing is to be
done. If you want to minimize per packet latency it is ideal to only use 12–byte IVs. You can just
increment it like a counter for each packet.
36 www.libtom.org
This adds the additional authentication data adata of length adatalen to the GCM state gcm.
Plaintext Processing
After the AAD has been processed, the plaintext (or ciphertext depending on the direction) can be
processed.
This processes message data where pt is the plaintext and ct is the ciphertext. The length of both
are equal and stored in ptlen. Depending on the mode pt is the input and ct is the output (or vice
versa). When direction equals GCM ENCRYPT the plaintext is read, encrypted and stored in
the ciphertext buffer. When direction equals GCM DECRYPT the opposite occurs.
State Termination
To terminate a GCM state and retrieve the message authentication tag call the following function.
This terminates the GCM state gcm and stores the tag in tag of length taglen octets.
State Reset
The call to gcm init() will perform considerable pre–computation (when GCM TABLES is de-
fined) and if you’re going to be dealing with a lot of packets it is very costly to have to call it
repeatedly. To aid in this endeavour, the reset function has been provided.
This will reset the GCM state gcm to the state that gcm init() left it. The user would then call
gcm add iv(), gcm add aad(), etc.
3.5 Encrypt and Authenticate Modes 37
One–Shot Packet
To process a single packet under any given key the following helper function can be used.
int gcm_memory(
int cipher,
const unsigned char *key,
unsigned long keylen,
const unsigned char *IV, unsigned long IVlen,
const unsigned char *adata, unsigned long adatalen,
unsigned char *pt, unsigned long ptlen,
unsigned char *ct,
unsigned char *tag, unsigned long *taglen,
int direction);
This will initialize the GCM state with the given key, IV and AAD value then proceed to encrypt
or decrypt the message text and store the final message tag. The definition of the variables is the
same as it is for all the manual functions.
If you are processing many packets under the same key you shouldn’t use this function as it
invokes the pre–computation with each call.
Example Usage
The following is an example usage of how to use GCM over multiple packets with a shared secret
key.
#include <tomcrypt.h>
/* Add the IV */
if ((err = gcm_add_iv(gcm, iv, ivlen)) != CRYPT_OK) {
return err;
}
return CRYPT_OK;
}
int main(void)
{
gcm_state gcm;
unsigned char key[16], IV[12], pt[PACKET_SIZE];
int err, x;
unsigned long ptlen;
/* register AES */
register_cipher(&aes_desc);
This simply sets up the hash to the default state governed by the specifications of the hash. To
add data to the message being hashed call:
Essentially all hash messages are virtually infinitely1 long message which are buffered. The data
can be passed in any sized chunks as long as the order of the bytes are the same the message digest
(hash output) will be the same. For example, this means that:
41
42 www.libtom.org
This function will finish up the hash and store the result in the out array. You must ensure
that out is long enough for the hash in question. Often hashes are used to get keys for symmetric
ciphers so the XXX done() functions will wipe the md variable before returning automatically.
To test a hash function call:
int XXX_test(void);
This will return CRYPT OK if the hash matches the test vectors, otherwise it returns an error
code. An example snippet that hashes a message with md5 is given below.
#include <tomcrypt.h>
int main(void)
{
hash_state md;
unsigned char *in = "hello world", out[16];
return 0;
}
The name member is the name of the hash function (all lowercase). The hashsize member is
the size of the digest output in bytes, while blocksize is the size of blocks the hash expects to the
compression function. Technically, this detail is not important for high level developers but is useful
to know for performance reasons.
The init member initializes the hash, process passes data through the hash, done terminates the
hash and retrieves the digest. The test member tests the hash against the specified test vectors.
There is a function to search the array as well called int find hash(char *name). It returns -1 if
the hash is not found, otherwise, the position in the descriptor table of the hash.
In addition, there is also find hash oid() which finds a hash by the ASN.1 OBJECT IDENTI-
FIER string.
int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
You can use the table to indirectly call a hash function that is chosen at run-time. For example:
#include <tomcrypt.h>
int main(void)
{
unsigned char buffer[100], hash[MAXBLOCKSIZE];
int idx, x;
hash_state md;
/* dump to screen */
for (x = 0; x < hash_descriptor[idx].hashsize; x++)
printf("%02x ", hash[x]);
44 www.libtom.org
printf("\n");
return 0;
}
This will hash the data pointed to by in of length inlen. The hash used is indexed by the hash
parameter. The message digest is stored in out, and the outlen parameter is updated to hold the
message digest size.
The next helper function allows for the hashing of a file based on a file name.
This will hash the file named by fname using the hash indexed by hash. The file named in
this function call must be readable by the user owning the process performing the request. This
function can be omitted by the LTC NO FILE define, which forces it to return CRYPT NOP
when it is called. The message digest is stored in out, and the outlen parameter is updated to hold
the message digest size.
This will hash the file identified by the handle in using the hash indexed by hash. This will
begin hashing from the current file pointer position, and will not rewind the file pointer when
finished. This function can be omitted by the LTC NO FILE define, which forces it to return
CRYPT NOP when it is called. The message digest is stored in out, and the outlen parameter is
updated to hold the message digest size.
To perform the above hash with md5 the following code could be used:
#include <tomcrypt.h>
int main(void)
{
int idx, err;
unsigned long len;
4.2 Hash Descriptors 45
The following hashes are provided as of this release within the LibTomCrypt library:
46 www.libtom.org
A cipher has to be registered with CHC (and also in the cipher descriptor tables with regis-
ter cipher()). The chc register() function will bind a cipher to the CHC system. Only one cipher
can be bound to the CHC hash at a time. There are additional requirements for the system to
work.
2. The cipher must allow an input key the size of the block size.
#include <tomcrypt.h>
int main(void)
{
int err;
4.4 Notice 47
4.4 Notice
It is highly recommended that you not use the MD4 or MD5 hashes for the purposes of digital
signatures or authentication codes. These hashes are provided for completeness and they still can
be used for the purposes of password hashing or one-way accumulators (e.g. Yarrow).
The other hashes such as the SHA-1, SHA-2 (that includes SHA-512, SHA-384 and SHA-256)
and TIGER-192 are still considered secure for all purposes you would normally use a hash for.
48 www.libtom.org
Chapter 5
The hmac parameter is the state for the HMAC code. The hash parameter is the index into the
descriptor table of the hash you want to use to authenticate the message. The key parameter is the
pointer to the array of chars that make up the key. The keylen parameter is the length (in octets)
of the key you want to use to authenticate the message. To send octets of a message through the
HMAC system you must use the following function:
hmac is the HMAC state you are working with. in is the array of octets to send into the HMAC
process. inlen is the number of octets to process. Like the hash process routines, you can send the
data in arbitrarily sized chunks. When you are finished with the HMAC process you must call the
following function to get the HMAC code:
49
50 www.libtom.org
The hmac parameter is the HMAC state you are working with. The out parameter is the array of
octets where the HMAC code should be stored. You must set outlen to the size of the destination
buffer before calling this function. It is updated with the length of the HMAC code produced
(depending on which hash was picked). If outlen is less than the size of the message digest (and
ultimately the HMAC code) then the HMAC code is truncated as per FIPS-198 specifications (e.g.
take the first outlen bytes).
There are two utility functions provided to make using HMACs easier to do. They accept the
key and information about the message (file pointer, address in memory), and produce the HMAC
result in one shot. These are useful if you want to avoid calling the three step process yourself.
int hmac_memory(
int hash,
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
This will produce an HMAC code for the array of octets in in of length inlen. The index into
the hash descriptor table must be provided in hash. It uses the key from key with a key length of
keylen. The result is stored in the array of octets out and the length in outlen. The value of outlen
must be set to the size of the destination buffer before calling this function. Similarly for files there
is the following function:
int hmac_file(
int hash,
const char *fname,
const unsigned char *key, unsigned long keylen,
unsigned char *out, unsigned long *outlen);
hash is the index into the hash descriptor table of the hash you want to use. fname is the filename
to process. key is the array of octets to use as the key of length keylen. out is the array of octets
where the result should be stored.
To test if the HMAC code is working there is the following function:
int hmac_test(void);
Which returns CRYPT OK if the code passes otherwise it returns an error code. Some example
code for using the HMAC system is given below.
#include <tomcrypt.h>
int main(void)
{
int idx, err;
hmac_state hmac;
unsigned char key[16], dst[MAXBLOCKSIZE];
unsigned long dstlen;
5.2 OMAC Support 51
/* register SHA-1 */
if (register_hash(&sha1_desc) == -1) {
printf("Error registering SHA1\n");
return -1;
}
/* return */
return 0;
}
The omac parameter is the state for the OMAC algorithm. The cipher parameter is the index into
the cipher descriptor table of the cipher2 you wish to use. The key and keylen parameters are the
keys used to authenticate the data.
To send data through the algorithm call
This will send inlen bytes from in through the active OMAC state state. Returns CRYPT OK if
the function succeeds. The function is not sensitive to the granularity of the data. For example,
When you are done processing the message you can call the following to compute the message
tag.
Which will terminate the OMAC and output the tag (MAC) to out. Note that unlike the HMAC
and other code outlen can be smaller than the default MAC size (for instance AES would make a
16-byte tag). Part of the OMAC specification states that the output may be truncated. So if you
pass in outlen = 5 and use AES as your cipher than the output MAC code will only be five bytes
long. If outlen is larger than the default size it is set to the default size to show how many bytes
were actually used.
Similar to the HMAC code the file and memory functions are also provided. To OMAC a buffer
of memory in one shot use the following function.
int omac_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
This will compute the OMAC of inlen bytes of in using the key key of length keylen bytes and the
cipher specified by the cipher ’th entry in the cipher descriptor table. It will store the MAC in out
with the same rules as omac done.
To OMAC a file use
2 The cipher must have a 64 or 128 bit block size. Such as CAST5, Blowfish, DES, AES, Twofish, etc.
5.2 OMAC Support 53
int omac_file(
int cipher,
const unsigned char *key, unsigned long keylen,
const char *filename,
unsigned char *out, unsigned long *outlen);
Which will OMAC the entire contents of the file specified by filename using the key key of
length keylen bytes and the cipher specified by the cipher ’th entry in the cipher descriptor table.
It will store the MAC in out with the same rules as omac done.
To test if the OMAC code is working there is the following function:
int omac_test(void);
Which returns CRYPT OK if the code passes otherwise it returns an error code. Some example
code for using the OMAC system is given below.
#include <tomcrypt.h>
int main(void)
{
int idx, err;
omac_state omac;
unsigned char key[16], dst[MAXBLOCKSIZE];
unsigned long dstlen;
/* register Rijndael */
if (register_cipher(&rijndael_desc) == -1) {
printf("Error registering Rijndael\n");
return -1;
}
return -1;
}
printf("The omac is %lu bytes long\n", dstlen);
/* return */
return 0;
}
Which initializes the pmac state with the given cipher and key of length keylen bytes. The chosen
cipher must have a 64 or 128 bit block size (e.x. AES).
To MAC data simply send it through the process function.
This will process inlen bytes of in in the given state. The function is not sensitive to the granularity
of the data. For example,
When a complete message has been processed the following function can be called to compute
the message tag.
This will store up to outlen bytes of the tag for the given state into out. Note that if outlen is larger
than the size of the tag it is set to the amount of bytes stored in out.
Similar to the OMAC code the file and memory functions are also provided. To PMAC a buffer
of memory in one shot use the following function.
3 J.Black, P.Rogaway, A Block–Cipher Mode of Operation for Parallelizable Message Authentication
5.4 Pelican MAC 55
int pmac_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
This will compute the PMAC of msglen bytes of msg using the key key of length keylen bytes, and
the cipher specified by the cipher ’th entry in the cipher descriptor table. It will store the MAC in
out with the same rules as pmac done().
To PMAC a file use
int pmac_file(
int cipher,
const unsigned char *key, unsigned long keylen,
const char *filename,
unsigned char *out, unsigned long *outlen);
Which will PMAC the entire contents of the file specified by filename using the key key of length
keylen bytes, and the cipher specified by the cipher ’th entry in the cipher descriptor table. It will
store the MAC in out with the same rules as pmac done().
To test if the PMAC code is working there is the following function:
int pmac_test(void);
Which returns CRYPT OK if the code passes otherwise it returns an error code.
5.4.1 Example
#include <tomcrypt.h>
int main(void)
{
pelican_state pelstate;
unsigned char key[32], tag[16];
int err;
return EXIT_SUCCESS;
}
5.5 XCBC-MAC 57
5.5 XCBC-MAC
As of LibTomCrypt v1.15, XCBC-MAC (RFC 3566) has been provided to support TLS encryption
suites. Like OMAC, it computes a message authentication code by using a cipher in CBC mode.
It also uses a single key which it expands into the requisite three keys for the MAC function. A
XCBC–MAC state is initialized with the following function:
This will initialize the XCBC–MAC state xcbc, with the key specified in key of length keylen
octets. The cipher indicated by the cipher index can be either a 64 or 128–bit block cipher. This
will return CRYPT OK on success.
It is possible to use XCBC in a three key mode by OR’ing the value LTC XCBC PURE
against the keylen parameter. In this mode, the key is interpretted as three keys. If the cipher
has a block size of n octets, the first key is then keylen − 2n octets and is the encryption key.
The next 2n octets are the K1 and K2 padding keys (used on the last block). For example, to use
AES–192 keylen should be 24 + 2 · 16 = 56 octets. The three keys are interpretted as if they were
concatenated in the key buffer.
To process data through XCBC–MAC use the following function:
This will add the message octets pointed to by in of length inlen to the XCBC–MAC state
pointed to by state. Like the other MAC functions, the granularity of the input is not important
but the order is. This will return CRYPT OK on success.
To compute the MAC tag value use the following function:
This will retrieve the XCBC–MAC tag from the state pointed to by state, and store it in the
array pointed to by out. The outlen parameter specifies the maximum size of the destination
buffer, and is updated to hold the final size of the tag when the function returns. This will return
CRYPT OK on success.
Helper functions are provided to make parsing memory buffers and files easier. The following
functions are provided:
int xcbc_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
58 www.libtom.org
This will compute the XCBC–MAC of msglen bytes of msg, using the key key of length keylen
bytes, and the cipher specified by the cipher ’th entry in the cipher descriptor table. It will store
the MAC in out with the same rules as xcbc done().
To xcbc a file use
int xcbc_file(
int cipher,
const unsigned char *key, unsigned long keylen,
const char *filename,
unsigned char *out, unsigned long *outlen);
Which will XCBC–MAC the entire contents of the file specified by filename using the key key of
length keylen bytes, and the cipher specified by the cipher ’th entry in the cipher descriptor table.
It will store the MAC in out with the same rules as xcbc done().
To test XCBC–MAC for RFC 3566 compliance use the following function:
int xcbc_test(void);
This will return CRYPT OK on success. This requires the AES or Rijndael descriptor be
previously registered, otherwise, it will return CRYPT NOP.
5.6 F9–MAC
The F9–MAC is yet another CBC–MAC variant proposed for the 3GPP standard. Originally
specified to be used with the KASUMI block cipher, it can also be used with other ciphers. For
LibTomCrypt, the F9–MAC code can use any cipher.
This will initialize the F9–MAC state f9, with the key specified in key of length keylen octets.
The cipher indicated by the cipher index can be either a 64 or 128–bit block cipher. This will
return CRYPT OK on success.
To process data through F9–MAC use the following function:
int f9_process( f9_state *state,
const unsigned char *in,
unsigned long inlen);
This will add the message octets pointed to by in of length inlen to the F9–MAC state pointed
to by state. Like the other MAC functions, the granularity of the input is not important but the
order is. This will return CRYPT OK on success.
To compute the MAC tag value use the following function:
int f9_done( f9_state *state,
unsigned char *out,
unsigned long *outlen);
This will retrieve the F9–MAC tag from the state pointed to by state, and store it in the array
pointed to by out. The outlen parameter specifies the maximum size of the destination buffer, and is
updated to hold the final size of the tag when the function returns. This will return CRYPT OK
on success.
Helper functions are provided to make parsing memory buffers and files easier. The following
functions are provided:
int f9_memory(
int cipher,
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
This will compute the F9–MAC of msglen bytes of msg, using the key key of length keylen bytes,
and the cipher specified by the cipher ’th entry in the cipher descriptor table. It will store the MAC
in out with the same rules as f9 done().
To F9–MAC a file use
int f9_file(
int cipher,
const unsigned char *key, unsigned long keylen,
const char *filename,
unsigned char *out, unsigned long *outlen);
Which will F9–MAC the entire contents of the file specified by filename using the key key of
length keylen bytes, and the cipher specified by the cipher ’th entry in the cipher descriptor table.
It will store the MAC in out with the same rules as f9 done().
To test f9–MAC for RFC 3566 compliance use the following function:
int f9_test(void);
This will return CRYPT OK on success. This requires the AES or Rijndael descriptor be
previously registered, otherwise, it will return CRYPT NOP.
60 www.libtom.org
Chapter 6
Pseudo-Random Number
Generators
61
62 www.libtom.org
This will terminate a PRNG state and free any memory (if any) allocated. To export a PRNG
state so that you can later resume the PRNG call the following.
This will write a PRNG state to the buffer out of length outlen bytes. The idea of the export
is meant to be used as a seed file. That is, when the program starts up there will not likely be that
much entropy available. To import a state to seed a PRNG call the following function.
This will call the start and add entropy functions of the given PRNG. It will use the state in
in of length inlen as the initial seed. You must pass the same seed length as was exported by the
corresponding export function.
Note that importing a state will not resume the PRNG from where it left off. That is, if you
export a state, emit (say) 8 bytes and then import the previously exported state the next 8 bytes
will not specifically equal the 8 bytes you generated previously.
When a program is first executed the normal course of operation is:
1. Gather entropy from your sources for a given period of time or number of events.
2. Start, use your entropy via add entropy and ready the PRNG yourself.
When your program is finished you simply call the export function and save the state to a
medium (disk, flash memory, etc). The next time your application starts up you can detect the
state, feed it to the import function and go on your way. It is ideal that (as soon as possible) after
start up you export a fresh state. This helps in the case that the program aborts or the machine is
powered down without being given a chance to exit properly.
Note that even if you have a state to import it is important to add new entropy to the state.
However, there is less pressure to do so.
To test a PRNG for operational conformity call the following functions.
int XXX_test(void);
6.1.1 Remarks
It is possible to be adding entropy and reading from a PRNG at the same time. For example, if
you first seed the PRNG and call ready() you can now read from it. You can also keep adding new
entropy to it. The new entropy will not be used in the PRNG until ready() is called again. This
allows the PRNG to be used and re-seeded at the same time. No real error checking is guaranteed
to see if the entropy is sufficient, or if the PRNG is even in a ready state before reading.
6.2 PRNG Descriptors 63
6.1.2 Example
Below is a simple snippet to read 10 bytes from Yarrow. It is important to note that this snippet
is NOT secure since the entropy added is not random.
#include <tomcrypt.h>
int main(void)
{
prng_state prng;
unsigned char buf[10];
int err;
/* start it */
if ((err = yarrow_start(&prng)) != CRYPT_OK) {
printf("Start error: %s\n", error_to_string(err));
}
/* add entropy */
if ((err = yarrow_add_entropy("hello world", 11, &prng))
!= CRYPT_OK) {
printf("Add_entropy error: %s\n", error_to_string(err));
}
/* ready and read */
if ((err = yarrow_ready(&prng)) != CRYPT_OK) {
printf("Ready error: %s\n", error_to_string(err));
}
printf("Read %lu bytes from yarrow\n",
yarrow_read(buf, sizeof(buf), &prng));
return 0;
}
prng_state *);
int (*test)(void);
};
To find a PRNG in the descriptor table the following function can be used:
This will search the PRNG descriptor table for the PRNG named name. It will return -1 if the
PRNG is not found, otherwise, it returns the index into the descriptor table.
Just like the ciphers and hashes, you must register your prng before you can use it. The two
functions provided work exactly as those for the cipher registry functions. They are the following:
The register function will register the PRNG, and return the index into the table where it was
placed (or -1 for error). It will avoid registering the same descriptor twice, and will return the
index of the current placement in the table if the caller attempts to register it more than once. The
unregister function will return CRYPT OK if the PRNG was found and removed. Otherwise, it
returns CRYPT ERROR.
Yarrow
Yarrow is fast PRNG meant to collect an unspecified amount of entropy from sources (keyboard,
mouse, interrupts, etc), and produce an unbounded string of random bytes.
Note: This PRNG is still secure for most tasks but is no longer recommended. Users should
use Fortuna instead.
6.2 PRNG Descriptors 65
Fortuna
Fortuna is a fast attack tolerant and more thoroughly designed PRNG suitable for long term usage.
It is faster than the default implementation of Yarrow1 while providing more security.
Fortuna is slightly less flexible than Yarrow in the sense that it only works with the AES block
cipher and SHA–256 hash function. Technically, Fortuna will work with any block cipher that
accepts a 256–bit key, and any hash that produces at least a 256–bit output. However, to make the
implementation simpler it has been fixed to those choices.
Fortuna is more secure than Yarrow in the sense that attackers who learn parts of the entropy
being added to the PRNG learn far less about the state than that of Yarrow. Without getting
into to many details Fortuna has the ability to recover from state determination attacks where the
attacker starts to learn information from the PRNGs output about the internal state. Yarrow on
the other hand, cannot recover from that problem until new entropy is added to the pool and put
to use through the ready() function.
RC4
RC4 is an old stream cipher that can also double duty as a PRNG in a pinch. You key RC4 by
calling add entropy(), and setup the key by calling ready(). You can only add up to 256 bytes via
add entropy().
When you read from RC4, the output is XOR’ed against your buffer you provide. In this
manner, you can use rc4 read() as an encrypt (and decrypt) function.
You really should not use RC4. This is not because RC4 is weak, (though biases are known to
exist) but simply due to the fact that faster alternatives exist.
SOBER-128
SOBER–128 is a stream cipher designed by the QUALCOMM Australia team. Like RC4, you key it
by calling add entropy(). There is no need to call ready() for this PRNG as it does not do anything.
Note: this cipher has several oddities about how it operates. The first call to add entropy() sets
the cipher’s key. Every other time call to the add entropy() function sets the cipher’s IV variable.
The IV mechanism allows you to encrypt several messages with the same key, and not re–use the
same key material.
Unlike Yarrow and Fortuna, all of the entropy (and hence security) of this algorithm rests in
the data you pass it on the first call to add entropy(). All buffers sent to add entropy() must have
a length that is a multiple of four bytes.
Like RC4, the output of SOBER–128 is XOR’ed against the buffer you provide it. In this
manner, you can use sober128 read() as an encrypt (and decrypt) function.
Since SOBER-128 has a fixed keying scheme, and is very fast (faster than RC4) the ideal usage
of SOBER-128 is to key it from the output of Fortuna (or Yarrow), and use it to encrypt messages.
It is also ideal for simulations which need a high quality (and fast) stream of bytes.
Example Usage
#include <tomcrypt.h>
1 Yarrow has been implemented to work with most cipher and hash combos based on which you have chosen to
int main(void)
{
prng_state prng;
unsigned char buf[32];
int err;
/* encrypt buffer */
strcpy(buf,"hello world");
if (rc4_read(buf, 11, &prng) != 11) {
printf("RC4 read error\n");
exit(-1);
}
return 0;
}
Which will try one of three methods of getting random data. The first is to open the popular
6.3 The Secure RNG 67
/dev/random device which on most *NIX platforms provides cryptographic random bits2 . The
second method is to try the Microsoft Cryptographic Service Provider, and read the RNG. The
third method is an ANSI C clock drift method that is also somewhat popular but gives bits of lower
entropy. The callback parameter is a pointer to a function that returns void. It is used when the
slower ANSI C RNG must be used so the calling application can still work. This is useful since
the ANSI C RNG has a throughput of roughly three bytes a second. The callback pointer may be
set to NULL to avoid using it if you do not want to. The function returns the number of bytes
actually read from any RNG source. There is a function to help setup a PRNG as well:
This will try to initialize the prng with a state of at least bits of entropy. The callback parameter
works much like the callback in rng get bytes(). It is highly recommended that you use this function
to setup your PRNGs unless you have a platform where the RNG does not work well. Example
usage of this function is given below:
#include <tomcrypt.h>
int main(void)
{
ecc_key mykey;
prng_state prng;
int err;
/* register yarrow */
if (register_prng(&yarrow_desc) == -1) {
printf("Error registering Yarrow\n");
return -1;
}
Microsoft CSP.
68 www.libtom.org
#include <tomcrypt.h>
int main(void)
{
ecc_key mykey;
int err;
/* register SPRNG */
if (register_prng(&sprng_desc) == -1) {
printf("Error registering SPRNG\n");
return -1;
}
7.1 Introduction
RSA wrote the PKCS #1 specifications which detail RSA Public Key Cryptography. In the specifi-
cations are padding algorithms for encryption and signatures. The standard includes the v1.5 and
v2.1 algorithms. To simplify matters a little the v2.1 encryption and signature padding algorithms
are called OAEP and PSS respectively.
69
70 www.libtom.org
Only encryption padding requires a PRNG. When performing signature padding the prng idx
parameter may be left to zero as it is not checked for validity.
int pkcs_1_v1_5_decode(
const unsigned char *msg,
unsigned long msglen,
int block_type,
unsigned long modulus_bitlen,
unsigned char *out,
unsigned long *outlen,
int *is_valid);
This will remove the PKCS padding data pointed to by msg of length msglen. The decoded data is
stored in out of length outlen. If the padding is valid, a 1 is stored in is valid, otherwise, a 0 is stored.
The block type parameter must be set to either LTC PKCS 1 EME or LTC PKCS 1 EMSA
depending on whether encryption or signature padding is being removed.
int pkcs_1_oaep_encode(
const unsigned char *msg,
unsigned long msglen,
const unsigned char *lparam,
unsigned long lparamlen,
unsigned long modulus_bitlen,
prng_state *prng,
int prng_idx,
int hash_idx,
unsigned char *out,
unsigned long *outlen);
This accepts msg as input of length msglen which will be OAEP padded. The lparam variable
is an additional system specific tag that can be applied to the encoding. This is useful to identify
which system encoded the message. If no variance is desired then lparam can be set to NULL.
7.4 PKCS #1 Digital Signatures 71
OAEP encoding requires the length of the modulus in bits in order to calculate the size of
the output. This is passed as the parameter modulus bitlen. hash idx is the index into the hash
descriptor table of the hash desired. PKCS #1 allows any hash to be used but both the encoder
and decoder must use the same hash in order for this to succeed. The size of hash output affects the
maximum sized input message. prng idx and prng are the random number generator arguments
required to randomize the padding process. The padded message is stored in out along with the
length in outlen.
If h is the length of the hash and m the length of the modulus (both in octets) then the maximum
payload for msg is m − 2h − 2. For example, with a 1024–bit RSA key and SHA–1 as the hash the
maximum payload is 86 bytes.
Note that when the message is padded it still has not been RSA encrypted. You must pass the
output of this function to rsa exptmod() to encrypt it.
This function decodes an OAEP encoded message and outputs the original message that was
passed to the OAEP encoder. msg is the output of pkcs 1 oaep encode() of length msglen. lparam
is the same system variable passed to the OAEP encoder. If it does not match what was used
during encoding this function will not decode the packet. modulus bitlen is the size of the RSA
modulus in bits and must match what was used during encoding. Similarly the hash idx index into
the hash descriptor table must match what was used during encoding.
If the function succeeds it decodes the OAEP encoded message into out of length outlen and
stores a 1 in res. If the packet is invalid it stores 0 in res and if the function fails for another reason
it returns an error code.
int pkcs_1_pss_encode(
const unsigned char *msghash,
unsigned long msghashlen,
unsigned long saltlen,
72 www.libtom.org
prng_state *prng,
int prng_idx,
int hash_idx,
unsigned long modulus_bitlen,
unsigned char *out,
unsigned long *outlen);
This function assumes the message to be PSS encoded has previously been hashed. The input
hash msghash is of length msghashlen. PSS allows a variable length random salt (it can be zero
length) to be introduced in the signature process. hash idx is the index into the hash descriptor
table of the hash to use. prng idx and prng are the random number generator information required
for the salt.
Similar to OAEP encoding modulus bitlen is the size of the RSA modulus (in bits). It limits
the size of the salt. If m is the length of the modulus h the length of the hash output (in octets)
then there can be m − h − 2 bytes of salt.
This function does not actually sign the data it merely pads the hash of a message so that it
can be processed by rsa exptmod().
int pkcs_1_pss_decode(
const unsigned char *msghash,
unsigned long msghashlen,
const unsigned char *sig,
unsigned long siglen,
unsigned long saltlen,
int hash_idx,
unsigned long modulus_bitlen,
int *res);
This will decode the PSS encoded message in sig of length siglen and compare it to values in
msghash of length msghashlen. If the block is a valid PSS block and the decoded hash equals the
hash supplied res is set to non–zero. Otherwise, it is set to zero. The rest of the parameters are as
in the PSS encode call.
It’s important to use the same saltlen and hash for both encoding and decoding as otherwise
the procedure will not work.
The system begins with with two primes p and q and their product N = pq. The order or Euler
totient of the multiplicative sub-group formed modulo N is given as ϕ(N ) = (p − 1)(q − 1) which
can be reduced to lcm(p − 1, q − 1). The public key consists of the composite N and some integer
e such that gcd(e, ϕ(N )) = 1. The private key consists of the composite N and the inverse of e
modulo ϕ(N ) often simply denoted as de ≡ 1 (mod ϕ(N )).
A person who wants to encrypt with your public key simply forms an integer (the plaintext)
M such that 1 < M < N − 2 and computes the ciphertext C = M e (mod N ). Since finding the
inverse exponent d given only N and e appears to be intractable only the owner of the private key
d
can decrypt the ciphertext and compute C d ≡ (M e ) ≡ M 1 ≡ M (mod N ). Similarly the owner of
the private key can sign a message by decrypting it. Others can verify it by encrypting it.
Currently RSA is a difficult system to cryptanalyze provided that both primes are large and not
close to each other. Ideally e should be larger than 100 to prevent direct analysis. For example, if e
is three and you do not pad the plaintext to be encrypted than it is possible that M 3 < N in which
case finding the cube-root would be trivial. The most often suggested value for e is 65537 since
it is large enough to make such attacks impossible and also well designed for fast exponentiation
(requires 16 squarings and one multiplication).
It is important to pad the input to RSA since it has particular mathematical structure. For
instance M1d M2d = (M1 M2 )d which can be used to forge a signature. Suppose M3 = M1 M2 is a
message you want to have a forged signature for. Simply get the signatures for M1 and M2 on
their own and multiply the result together. Similar tricks can be used to deduce plaintexts from
ciphertexts. It is important not only to sign the hash of documents only but also to pad the inputs
with data to remove such structure.
Where wprng is the index into the PRNG descriptor array. The size parameter is the size in
bytes of the RSA modulus desired. The e parameter is the encryption exponent desired, typical
values are 3, 17, 257 and 65537. Stick with 65537 since it is big enough to prevent trivial math
attacks, and not super slow. The key parameter is where the constructed key is placed. All keys
must be at least 128 bytes, and no more than 512 bytes in size (that is from 1024 to 4096 bits).
Note: the rsa make key() function allocates memory at run–time when you make the key. Make
sure to call rsa free() (see below) when you are finished with the key. If rsa make key() fails it will
automatically free the memory allocated.
There are two types of RSA keys. The types are PK PRIVATE and PK PUBLIC. The first
type is a private RSA key which includes the CRT parameters1 in the form of a RSAPrivateKey
(PKCS #1 compliant). The second type, is a public RSA key which only includes the modulus and
public exponent. It takes the form of a RSAPublicKey (PKCS #1 compliant).
1 As of v0.99 the PK PRIVATE OPTIMIZED type has been deprecated, and has been replaced by the
PK PRIVATE type.
74 www.libtom.org
int rsa_encrypt_key(
const unsigned char *in,
unsigned long inlen,
unsigned char *out,
unsigned long *outlen,
const unsigned char *lparam,
unsigned long lparamlen,
prng_state *prng,
int prng_idx,
int hash_idx,
rsa_key *key);
This function will OAEP pad in of length inlen bytes, RSA encrypt it, and store the ciphertext in
out of length outlen octets. The lparam and lparamlen are the same parameters you would pass to
pkcs 1 oaep encode().
int rsa_encrypt_key_ex(
const unsigned char *in,
unsigned long inlen,
unsigned char *out,
7.7 RSA Key Decryption 75
The parameters are all the same as for rsa encrypt key() except for the addition of the padding
parameter. It must be set to LTC PKCS 1 V1 5 to perform v1.5 encryption, or set to LTC PKCS 1 OAEP
to perform v2.1 encryption.
When performing v1.5 encryption, the hash and lparam parameters are totally ignored and can
be set to NULL or zero (respectively).
This function will RSA decrypt in of length inlen then OAEP de-pad the resulting data and store
it in out of length outlen. The lparam and lparamlen are the same parameters you would pass to
pkcs 1 oaep decode().
If the RSA decrypted data is not a valid OAEP packet then stat is set to 0. Otherwise, it is set
to 1.
int rsa_decrypt_key_ex(
const unsigned char *in,
unsigned long inlen,
unsigned char *out,
unsigned long *outlen,
const unsigned char *lparam,
unsigned long lparamlen,
76 www.libtom.org
int hash_idx,
int padding,
int *stat,
rsa_key *key);
Similar to the extended encryption, the new parameter padding indicates which version of the
PKCS #1 standard to use. It must be set to LTC PKCS 1 V1 5 to perform v1.5 decryption, or
set to LTC PKCS 1 OAEP to perform v2.1 decryption.
When performing v1.5 decryption, the hash and lparam parameters are totally ignored and can
be set to NULL or zero (respectively).
This will PSS encode the message digest pointed to by in of length inlen octets. Next, the PSS
encoded hash will be RSA signed and the output stored in the buffer pointed to by out of length
outlen octets.
The hash idx parameter indicates which hash will be used to create the PSS encoding. It should
be the same as the hash used to hash the message being signed. The saltlen parameter indicates
the length of the desired salt, and should typically be small. A good default value is between 8 and
16 octets. Strictly, it must be small than modulus len − hLen − 2 where modulus len is the size of
the RSA modulus (in octets), and hLen is the length of the message digest produced by the chosen
hash.
int rsa_sign_hash_ex(
const unsigned char *in,
unsigned long inlen,
unsigned char *out,
unsigned long *outlen,
int padding,
7.9 RSA Signature Verification 77
prng_state *prng,
int prng_idx,
int hash_idx,
unsigned long saltlen,
rsa_key *key);
This will PKCS encode the message digest pointed to by in of length inlen octets. Next, the
PKCS encoded hash will be RSA signed and the output stored in the buffer pointed to by out of
length outlen octets. The padding parameter must be set to LTC PKCS 1 V1 5 to produce a
v1.5 signature, otherwise, it must be set to LTC PKCS 1 PSS to produce a v2.1 signature.
When performing a v1.5 signature the prng, prng idx, and hash idx parameters are not checked
and can be left to any values such as {NULL, 0, 0}.
This will RSA verify the signature pointed to by sig of length siglen octets. Next, the RSA
decoded data is PSS decoded and the extracted hash is compared against the message digest pointed
to by msghash of length msghashlen octets.
If the RSA decoded data is not a valid PSS message, or if the PSS decoded hash does not match
the msghash value, res is set to 0. Otherwise, if the function succeeds, and signature is valid res is
set to 1.
int rsa_verify_hash_ex(
const unsigned char *sig,
unsigned long siglen,
const unsigned char *hash,
unsigned long hashlen,
int padding,
int hash_idx,
unsigned long saltlen,
int *stat,
rsa_key *key);
78 www.libtom.org
This will RSA verify the signature pointed to by sig of length siglen octets. Next, the RSA
decoded data is PKCS decoded and the extracted hash is compared against the message digest
pointed to by msghash of length msghashlen octets.
If the RSA decoded data is not a valid PSS message, or if the PKCS decoded hash does not
match the msghash value, res is set to 0. Otherwise, if the function succeeds, and signature is valid
res is set to 1.
The padding parameter must be set to LTC PKCS 1 V1 5 to perform a v1.5 verification.
Otherwise, it must be set to LTC PKCS 1 PSS to perform a v2.1 verification. When performing
a v1.5 verification the hash idx parameter is ignored.
/* register prng/hash */
if (register_prng(&sprng_desc) == -1) {
printf("Error registering sprng");
return EXIT_FAILURE;
}
if (register_hash(&sha1_desc) == -1) {
printf("Error registering sha1");
return EXIT_FAILURE;
}
hash_idx = find_hash("sha1");
prng_idx = find_prng("sprng");
This will export the RSA key in either a RSAPublicKey or RSAPrivateKey (PKCS #1 types)
depending on the value of type. When it is set to PK PRIVATE the export format will be
RSAPrivateKey and otherwise it will be RSAPublicKey.
This will import the key stored in inlen and import it to key. If the function fails it will automati-
cally free any allocated memory. This function can import both RSAPublicKey and RSAPrivateKey
formats.
As of v1.06 this function can also import OpenSSL DER formatted public RSA keys. They are
essentially encapsulated RSAPublicKeys. LibTomCrypt will import the key, strip off the additional
data (it’s the preferred hash) and fill in the rsa key structure as if it were a native RSAPublicKey.
Note that there is no function provided to export in this format.
Chapter 8
8.1 Background
The library provides a set of core ECC functions as well that are designed to be the Elliptic Curve
analogy of all of the Diffie-Hellman routines in the previous chapter. Elliptic curves (of certain
forms) have the benefit that they are harder to attack (no sub-exponential attacks exist unlike
normal DH crypto) in fact the fastest attack requires the square root of the order of the base point
in time. That means if you use a base point of order 2192 (which would represent a 192-bit key)
then the work factor is 296 in order to find the secret key.
The curves in this library are taken from the following website:
http://csrc.nist.gov/cryptval/dss.htm
As of v1.15 three new curves from the SECG standards are also included they are the secp112r1,
secp128r1, and secp160r1 curves. These curves were added to support smaller devices which do not
need as large keys for security.
They are all curves over the integers modulo a prime. The curves have the basic equation that
is:
y 2 = x3 − 3x + b (mod p) (8.1)
The variable b is chosen such that the number of points is nearly maximal. In fact the order
of the base points β provided are very close to p that is ||ϕ(β)||∼||p||. The curves range in order
from ∼2112 points to ∼2521 . According to the source document any key size greater than or equal
to 256-bits is sufficient for long term security.
81
82 www.libtom.org
which will build LTC using the TFM math library and enabling this new feature. The feature is
not enabled by default as it is NOT thread safe (by default). It supports the LTC locking macros
(such as by enabling LTC PTHREAD), but by default is not locked.
The optimization works by using a Fixed Point multiplier on any base point you use twice or
more in a short period of time. It has a limited size cache (of FP ENTRIES entries) which it uses to
hold recent bases passed to ltc ecc mulmod(). Any base detected to be used twice is sent through
the pre–computation phase, and then the fixed point algorithm can be used. For example, if you
use a NIST base point twice in a row, the 2nd and all subsequent point multiplications with that
point will use the faster algorithm.
The optimization uses a window on the multiplicand of FP LUT bits (default: 8, min: 2, max:
12), and this controls the memory/time trade-off. The larger the value the faster the algorithm
will be but the more memory it will take. The memory usage is 3 · 2F P LU T integers which by
default with TFM amounts to about 400kB of memory. Tuning TFM (by changing FP SIZE) can
decrease the usage by a fair amount. Memory is only used by a cache entry if it is active. Both
FP ENTRIES and FP LUT are definable on the command line if you wish to override them. For
instance,
CFLAGS="-DTFM_DESC -DMECC_FP -DFP_ENTRIES=8 -DFP_LUT=6" make
would define a window of 6 bits and limit the cache to 8 entries. Generally, it is better to first
tune TFM by adjusting FP SIZE (from tfm.h). It defaults to 4096 bits (512 bytes) which is way
more than what is required by ECC. At most, you need 1152 bits to accommodate ECC–521. If
you’re only using (say) ECC–256 you will only need 576 bits, which would reduce the memory
usage by 700%.
The first flags bit denotes whether the key is public (zero) or private (one).
8.4 ECC Curve Parameters 83
int ecc_make_key_ex(
prng_state *prng,
int wprng,
ecc_key *key,
const ltc_ecc_set_type *dp);
This function generates a random ECC key over the curve specified by the parameters by dp.
The rest of the parameters are equivalent to those from the original key generation function.
This will export the key with the given type (PK PUBLIC or PK PRIVATE), and store it to
out.
This will import the ECC key from in, and store it in the ecc key structure pointed to by key. If
the operation fails it will free any allocated memory automatically.
This will import the key from the array pointed to by in of length inlen octets. The key is stored
in the ECC structure pointed to by key. The curve is specified by the parameters pointed to by dp.
The function will free all internally allocated memory upon error.
The ECC key pointed to by key is exported in public fashion to the array pointed to by out. The
ANSI X9.63 format used is from section 4.3.6 of the standard. It does not allow for the export of
private keys.
This will import the key stored in the array pointed to by in of length inlen octets. The imported
key is stored in the ECC key pointed to by key. The function will free any allocated memory upon
error.
This will import the key stored in the array pointed to by in of length inlen octets using the domain
parameters pointed to by dp. The imported key is stored in the ECC key pointed to by key. The
function will free any allocated memory upon error.
86 www.libtom.org
As the name implies this function encrypts a (symmetric) key, and is not intended for encrypting
long messages directly. It will encrypt the plaintext in the array pointed to by in of length inlen
octets. It uses the public ECC key pointed to by key, and hash algorithm indexed by hash to
construct a shared secret which may be XOR’ed against the plaintext. The ciphertext is stored in
the output buffer pointed to by out of length outlen octets.
The data is encrypted to the public ECC key such that only the holder of the private key can
decrypt the payload. To have multiple recipients multiple call to this function for each public ECC
key is required.
This function will decrypt an encrypted payload. The key provided must be the private key
corresponding to the public key used during encryption. If the wrong key is provided the function
will not specifically return an error code. It is important to use some form of challenge response in
that case (e.g. compute a MAC of a known string).
This function will EC–DSA sign the message digest stored in the array pointed to by in of length
inlen octets. The signature will be stored in the array pointed to by out of length outlen octets.
The function requires a properly seeded PRNG, and the ECC key provided must be a private key.
This function will verify the EC-DSA signature in the array pointed to by sig of length siglen
octets, against the message digest pointed to by the array hash of length hashlen. It will store a
non–zero value in stat if the signature is valid. Note: the function will not return an error if the
signature is invalid. It will return an error, if the actual signature payload is an invalid format.
The ECC key must be the public (or private) ECC key corresponding to the key that performed
the signature.
9.1 Introduction
The Digital Signature Algorithm (or DSA) is a variant of the ElGamal Signature scheme which has
been modified to reduce the bandwidth of the signatures. For example, to have 80-bits of security
with ElGamal, you need a group with an order of at least 1024–bits. With DSA, you need a group
of order at least 160–bits. By comparison, the ElGamal signature would require at least 256 bytes
of storage, whereas the DSA signature would require only at least 40 bytes.
89
90 www.libtom.org
When you are finished with a DSA key you can call the following function to free the memory
used.
void dsa_free(dsa_key *key);
3. g = hr (mod p) a generator of order q modulo p. h can be any non-trivial random value. For
this library they start at h = 2 and step until g is not 1.
1. q must be prime.
2. p must be prime.
5. (p − 1) ≡ 0 (mod q).
6. g q ≡ 1 (mod p).
7. 1 < y < p − 1
8. y q ≡ 1 (mod p).
Tests one and two ensure that the values will at least form a field which is required for the
signatures to function. Tests three and four ensure that the generator g is not set to a trivial
value which would make signature forgery easier. Test five ensures that q divides the order of
multiplicative sub-group of Z/pZ. Test six ensures that the generator actually generates a prime
order group. Tests seven and eight ensure that the public key is within range and belongs to a
group of prime order. Note that test eight does not prove that g generated y only that y belongs
to a multiplicative sub-group of order q.
The following function will perform these tests.
This will test key and store the result in stat. If the result is stat = 0 the DSA key failed one
of the tests and should not be used at all. If the result is stat = 1 the DSA key is valid (as far as
valid mathematics are concerned).
9.5 Signatures
9.5.1 Signature Generation
To generate a DSA signature call the following function:
92 www.libtom.org
Which will sign the data in in of length inlen bytes. The signature is stored in out and the size of
the signature in outlen. If the signature is longer than the size you initially specify in outlen nothing
is stored and the function returns an error code. The DSA key must be of the PK PRIVATE
persuasion.
Which will verify the data in hash of length inlen against the signature stored in sig of length
siglen. It will set stat to 1 if the signature is valid, otherwise it sets stat to 0.
This will encrypt the payload in in of length inlen and store the ciphertext in the output buffer
out. The length of the ciphertext outlen must be originally set to the length of the output buffer.
The DSA key can be a public key.
This will decrypt the ciphertext in of length inlen, and store the original payload in out of length
outlen. The DSA key must be a private key.
This will export the DSA key to the buffer out and set the length in outlen (which must have been
previously initialized to the maximum buffer size). The type variable may be either PK PRIVATE
or PK PUBLIC depending on whether you want to export a private or public copy of the DSA
key.
This will import the DSA key from the buffer in of length inlen to the key. If the process fails
the function will automatically free all of the heap allocated in the process (you don’t have to call
dsa free()).
94 www.libtom.org
Chapter 10
Standards Support
typedef struct {
int type;
void *data;
unsigned long size;
int used;
struct ltc_asn1_list_ *prev, *next,
*child, *parent;
} ltc_asn1_list;
The type field is one of the following ASN.1 field definitions. The data pointer is a void pointer
to the data to be encoded (or the destination) and the size field is specific to what you are encoding
(e.g. number of bits in the BIT STRING data type). The used field is primarily for the CHOICE
decoder and reflects if the particular member of a list was the decoded data type. To help build
the lists in an orderly fashion the macro LTC SET ASN1(list, index, Type, Data, Size) has been
provided.
It will assign to the index th position in the list the triplet (Type, Data, Size). An example
usage would be:
...
ltc_asn1_list sequence[3];
unsigned long three=3;
95
96 www.libtom.org
The macro is relatively safe with respect to modifying variables, for instance the following code
is equivalent.
...
ltc_asn1_list sequence[3];
unsigned long three=3;
int x=0;
LTC_SET_ASN1(sequence, x++, LTC_ASN1_IA5_STRING, "hello", 5);
LTC_SET_ASN1(sequence, x++, LTC_ASN1_SHORT_INTEGER, &three, 1);
LTC_SET_ASN1(sequence, x++, LTC_ASN1_NULL, NULL, 0);
SEQUENCE Encoding
To encode a sequence a ltc asn1 list array must be initialized with the members of the sequence
and their respective pointers. The encoding is performed with the following function.
10.1 ASN.1 Formats 97
SEQUENCE Decoding
Decoding a SEQUENCE is similar to encoding. You set up an array of ltc asn1 list where in this
case the size member is the maximum size (in certain cases). For types such as IA5 STRING, BIT
STRING, OCTET STRING (etc) the size field is updated after successful decoding to reflect how
many units of the respective type has been loaded.
int der_decode_sequence(const unsigned char *in,
unsigned long inlen,
ltc_asn1_list *list,
unsigned long outlen);
This will decode upto outlen items from the input buffer in of length inlen octets. The function
will stop (gracefully) when it runs out of items to decode. It will fail (for among other reasons)
when it runs out of input bytes to read, a data type is invalid or a heap failure occurred.
For the following types the size field will be updated to reflect the number of units read of the
given type.
1. BIT STRING
2. OCTET STRING
3. OBJECT IDENTIFIER
4. IA5 STRING
5. PRINTABLE STRING
SEQUENCE Length
The length of a SEQUENCE can be determined with the following function.
int der_length_sequence(ltc_asn1_list *list,
unsigned long inlen,
unsigned long *outlen);
This will get the encoding size for the given list of length inlen and store it in outlen.
98 www.libtom.org
These either encode or decode (respectively) a SEQUENCE data type where the items in the
sequence are specified after the length parameter.
The list of items are specified as a triple of the form (type, size, data) where type is an int, size
is a unsigned long and data is void pointer. The list of items must be terminated with an item
with the type LTC ASN1 EOL.
It is ideal that you cast the size values to unsigned long to ensure that the proper data type is
passed to the function. Constants such as 1 without a cast or prototype are of type int by default.
Appending UL or pre-pending (unsigned long) is enough to cast it to the correct type.
buflen = sizeof(buf);
if ((err =
der_encode_sequence_multi(buf, &buflen,
LTC_ASN1_IA5_STRING, 5UL, "Hello",
LTC_ASN1_IA5_STRING, 7UL, " World!",
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
// error handling
}
This example encodes a SEQUENCE with two IA5 STRING types containing “Hello” and “
World!” respectively. Note the usage of the UL modifier on the size parameters. This forces the
compiler to pass the numbers as the required unsigned long type that the function expects.
SET Encoding
SETs use the same array structure of ltc asn1 list that the SEQUENCE functions use. They are
encoded with the following function:
10.1 ASN.1 Formats 99
This will encode the list of ASN.1 objects in list of length inlen objects, and store the output
in out of length outlen bytes. The function will make a copy of the list provided, and sort it by the
TAG. Objects with identical TAGs are additionally sorted on their original placement in the array
(to make the process deterministic).
This function will NOT recognize DEFAULT objects, and it is the responsibility of the caller
to remove them as required.
SET Decoding
The SET type can be decoded with the following function.
This will decode the SET specified by list of length outlen objects from the input buffer in of
length inlen octets.
It handles the fact that SETs are not strictly ordered and will make multiple passes (as required)
through the list to decode all the objects.
SET Length
The length of a SET can be determined by calling der length sequence() since they have the same
encoding length.
SET OF Encoding
A SET OF object is an array of identical objects (e.g. OCTET STRING) sorted in ascending order
by the DER encoding of the object. They are used to store objects deterministically based solely
on their encoding. It uses the same array structure of ltc asn1 list that the SEQUENCE functions
use. They are encoded with the following function.
This will encode a SET OF containing the list of inlen ASN.1 objects and store the encoding
in the output buffer out of length outlen.
The routine will first encode the SET OF in an unordered fashion (in a temporary buffer) then
sort using the XQSORT macro and copy back to the output buffer. This means you need at least
enough memory to keep an additional copy of the output on the heap.
100 www.libtom.org
SET OF Decoding
Since the decoding of a SET OF object is unambiguous it can be decoded with der decode sequence().
SET OF Length
Like the SET type the der length sequence() function can be used to determine the length of a SET
OF object.
These will encode or decode a signed INTEGER data type using the bignum data type to store
the large INTEGER. To encode smaller values without allocating a bignum to store the value, the
short INTEGER functions were made available.
These will encode or decode an unsigned unsigned long type (only reads upto 32–bits). For
values in the range 0 . . . 232 − 1 the integer and short integer functions can encode and decode each
others outputs.
These will encode or decode a BIT STRING data type. The bits are passed in (or read out)
using one char per bit. A non–zero value will be interpreted as a one bit, and a zero value a zero
bit.
These will encode or decode an OCTET STRING data type. The octets are stored using one
unsigned char each.
These will encode or decode an OBJECT IDENTIFIER object. The words of the OID are
stored in individual unsigned long elements, and must be in the range 0 . . . 232 − 1.
These will encode or decode an UTF8 STRING. The characters are read or stored in individual
wchar t elements. These function performs no internal mapping and treat the characters as literals.
These functions use the wchar t type which is not universally available. In those cases, the
library will typedef it to unsigned long. If you intend to use the ISO C functions for working with
wide–char arrays, you should make sure that wchar t has been defined previously.
typedef struct {
unsigned YY, /* year 00--99 */
MM, /* month 01--12 */
DD, /* day 01--31 */
hh, /* hour 00--23 */
mm, /* minute 00--59 */
ss, /* second 00--59 */
off_dir, /* timezone offset direction 0 == +, 1 == - */
off_hh, /* timezone offset hours */
off_mm; /* timezone offset minutes */
} ltc_utctime;
The time can be offset plus or minus a set amount of hours (off hh) and minutes (off mm).
When off dir is zero, the time will be added otherwise it will be subtracted. For instance, the array
{5, 6, 20, 22, 4, 00, 0, 5, 0} represents the current time of 2005, June 20th, 22:04:00 with a time offset
of +05h00.
The encoder will store time in one of the two ASN.1 formats, either YYMMDDhhmmssZ or
YYMMDDhhmmss±hhmm, and perform minimal error checking on the input. The decoder will
read all valid ASN.1 formats and perform range checking on the values (not complete but rational)
useful for catching packet errors.
It is suggested that decoded data be further scrutinized (e.g. days of month in particular).
This will decode the input in the in field of length inlen. It uses the provided ASN.1 list specified
in the list field which has outlen elements. The inlen field will be updated with the length of the
decoded data type, as well as the respective entry in the list field will have the used flag set to
non–zero to reflect it was the data type decoded.
Sample Decoding
Suppose we decode the following structure:
User ::= SEQUENCE {
Name IA5 STRING
LoginToken SEQUENCE {
passwdHash OCTET STRING
pubkey ECCPublicKey
}
LastOn UTCTIME
}
At this point list would point to the SEQUENCE identified by User. It would have no sibblings
(prev or next), and only a child node. Walking to the child node with the following code will bring
us to the Name portion of the SEQUENCE:
list = list->child;
Now list points to the Name member (with the tag IA5 STRING). The data, size, and type members
of list should reflect that of an IA5 STRING. The sibbling will now be the LoginToken SEQUENCE.
The sibbling has a child node which points to the passwdHash OCTET STRING. We can walk to
this node with the following code:
106 www.libtom.org
At this point, list will point to the passwdHash member of the innermost SEQUENCE. This node
has a sibbling, the pubkey member of the SEQUENCE. The LastOn member of the SEQUENCE
is a sibbling of the LoginToken node, if we wanted to walk there we would have to go up and over
via:
list = list->parent->next;
At this point, we are pointing to the last node of the list. Lists are terminated in all directions by a
NULL pointer. All nodes are doubly linked so that you can walk up and down the nodes without
keeping pointers lying around.
This will free all of the memory allocated by der decode sequence flexi().
Where password is the user’s password. Since the algorithm allows binary passwords you must also
specify the length in password len. The salt is a fixed size 8–byte array which should be random
10.2 Password Based Cryptography 107
for each user and session. The iteration count is the delay desired on the password. The hash idx
is the index of the hash you wish to use in the descriptor table.
The output of length up to outlen is stored in out. If outlen is initially larger than the size of
the hash functions output it is set to the number of bytes stored. If it is smaller than not all of the
hash output is stored in out.
Where password is the users password. Since the algorithm allows binary passwords you must also
specify the length in password len. The salt is an array of size salt len. It should be random for
each user and session. The iteration count is the delay desired on the password. The hash idx is
the index of the hash you wish to use in the descriptor table. The output of length up to outlen is
stored in out.
&outlen))
!= CRYPT_OK) {
/* error handle */
}
/* now extract it */
memcpy(cipher_key, outbuf, 16);
memcpy(cipher_iv, outbuf+16, 16);
memcpy(mac_key, outbuf+32, 16);
Miscellaneous
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
Those characters are supported in the 7-bit ASCII map, which means they can be used for transport
over common e-mail, usenet and HTTP mediums. The format of an encoded stream is just a literal
sequence of ASCII characters where a group of four represent 24-bits of input. The first four chars
of the encoders output is the length of the original input. After the first four characters is the rest
of the message.
Often, it is desirable to line wrap the output to fit nicely in an e-mail or usenet posting. The
decoder allows you to put any character (that is not in the above sequence) in between any character
of the encoders output. You may not however, break up the first four characters.
To encode a binary string in base64 call:
Where in is the binary string and out is where the ASCII output is placed. You must set the value
of outlen prior to calling this function and it sets the length of the base64 output in outlen when it
is done. To decode a base64 string call:
109
110 www.libtom.org
This puts a one in result if the number is probably prime, otherwise it places a zero in it. It is
assumed that if it returns an error that the value in result is undefined. To make a random prime
call:
Where len is the size of the prime in bytes (2 ≤ len ≤ 256). You can set len to the negative size
you want to get a prime of the form p ≡ 3 (mod 4). So if you want a 1024-bit prime of this sort
pass len = -128 to the function. Upon success it will return CRYPT OK and N will contain an
integer which is very likely prime.
Programming Guidelines
111
112 www.libtom.org
registering your PRNG, all of the library functions that need a PRNG can instantly take advantage
of it. The same applies for ciphers, hashes, and bignum math routines.
Note that n is not the bit-length but the magnitude. For example, for a 1024-bit key n = 21024 .
The work required is:
Using the above tables the following suggestions for key sizes seems appropriate:
Security Goal RSA/DH Key Size (bits) ECC Key Size (bits)
Near term 1024 160
Short term 1536 192
Long Term 2560 384
12.5 Thread Safety 113
13.1 Introduction
The library is fairly flexible about how it can be built, used, and generally distributed. Additions
are being made with each new release that will make the library even more flexible. Each of the
classes of functions can be disabled during the build process to make a smaller library. This is
particularly useful for shared libraries.
As of v1.06 of the library, the build process has been moved to two steps for the typical LibTom-
Crypt application. This is because LibTomCrypt no longer provides a math API on its own and
relies on third party libraries (such as LibTomMath, GnuMP, or TomsFastMath).
The build process now consists of installing a math library first, and then building and installing
LibTomCrypt with a math library configured. Note that LibTomCrypt can be built with no internal
math descriptors. This means that one must be provided at either build, or run time for the
application. LibTomCrypt comes with three math descriptors that provide a standard interface to
math libraries.
115
116 www.libtom.org
will build and install the libraries with the gmake tool. Similarly,
will build the library using arm–gcc as the compiler and arm–ar as the archiver.
This will turn off optimizations and add -g3 to the CFLAGS which enables debugging.
This will build the library and install it to the directories under /home/tom/project/. e.g.
/home/tom/project/:
total 1
drwxr-xr-x 2 tom users 80 Jul 30 16:02 docs
drwxr-xr-x 2 tom users 528 Jul 30 16:02 include
drwxr-xr-x 2 tom users 80 Jul 30 16:02 lib
/home/tom/project/docs:
total 452
-rwxr-xr-x 1 tom users 459009 Jul 30 16:02 crypt.pdf
/home/tom/project/include:
13.3 Extra libraries 117
total 132
-rwxr-xr-x 1 tom users 2482 Jul 30 16:02 tomcrypt.h
-rwxr-xr-x 1 tom users 702 Jul 30 16:02 tomcrypt_argchk.h
-rwxr-xr-x 1 tom users 2945 Jul 30 16:02 tomcrypt_cfg.h
-rwxr-xr-x 1 tom users 22763 Jul 30 16:02 tomcrypt_cipher.h
-rwxr-xr-x 1 tom users 5174 Jul 30 16:02 tomcrypt_custom.h
-rwxr-xr-x 1 tom users 11314 Jul 30 16:02 tomcrypt_hash.h
-rwxr-xr-x 1 tom users 11571 Jul 30 16:02 tomcrypt_mac.h
-rwxr-xr-x 1 tom users 13614 Jul 30 16:02 tomcrypt_macros.h
-rwxr-xr-x 1 tom users 14714 Jul 30 16:02 tomcrypt_math.h
-rwxr-xr-x 1 tom users 632 Jul 30 16:02 tomcrypt_misc.h
-rwxr-xr-x 1 tom users 10934 Jul 30 16:02 tomcrypt_pk.h
-rwxr-xr-x 1 tom users 2634 Jul 30 16:02 tomcrypt_pkcs.h
-rwxr-xr-x 1 tom users 7067 Jul 30 16:02 tomcrypt_prng.h
-rwxr-xr-x 1 tom users 1467 Jul 30 16:02 tomcrypt_test.h
/home/tom/project/lib:
total 1073
-rwxr-xr-x 1 tom users 1096284 Jul 30 16:02 libtomcrypt.a
This will install the library using the TomsFastMath library and link the libtfm.a library out
of the default library search path. The two defines are explained below. You can specify mul-
tiple archives (say if you want to support two math libraries, or add on additional code) to the
EXTRALIBS variable by separating them by a space.
Note that EXTRALIBS is not required if you are only making and installing the static library
but none of the test programs.
That will build LibTomCrypt (including the TomsFastMath descriptor), and install it in the
default locations indicated previously. You can enable the built–in LibTomMath descriptor as well
(or in place of the TomsFastMath descriptor). Similarly, you can build the library with no built–in
math descriptors.
make install
118 www.libtom.org
In this case, no math descriptors are present in the library and they will have to be made
available at build or run time before you can use any of the public key functions.
Note that even if you include the built–in descriptors you must link against the source library
as well.
This will compile myprogram and link it against the LibTomCrypt library as well as Toms-
FastMath (which must have been previously installed). Note that we define TFM DESC for
compilation. This is so that the TFM descriptor symbol will be defined for the client application
to make use of without giving warnings.
This will build and install the library and link the shared object against the TomsFastMath
library (which must be installed as a shared object as well). The shared build process requires
libtool to be installed.
ARGTYPE
This lets you control how the LTC ARGCHK macro will behave. The macro is used to check
pointers inside the functions against NULL. There are four settings for ARGTYPE. When set to
0, it will have the default behaviour of printing a message to stderr and raising a SIGABRT signal.
This is provided so all platforms that use LibTomCrypt can have an error that functions similarly.
When set to 1, it will simply pass on to the assert() macro. When set to 2, the macro will display
the error to stderr then return execution to the caller. This could lead to a segmentation fault (e.g.
when a pointer is NULL) but is useful if you handle signals on your own. When set to 3, it will
resolve to a empty macro and no error checking will be performed. Finally, when set to 4, it will
return CRYPT INVALID ARG to the caller.
Endianess
There are five macros related to endianess issues. For little endian platforms define, ENDIAN LITTLE.
For big endian platforms define ENDIAN BIG. Similarly when the default word size of an un-
signed long is 32-bits define ENDIAN 32BITWORD or define ENDIAN 64BITWORD when
13.7 The Configure Script 119
its 64-bits. If you do not define any of them the library will automatically use ENDIAN NEUTRAL
which will work on all platforms.
Currently LibTomCrypt will detect x86-32, x86-64, MIPS R5900, SPARC and SPARC64 running
GCC as well as x86-32 running MSVC.
That will build and install the library with all descriptors (and link against all), but only use
TomsFastMath in the timing demo.
Chapter 14
Optimizations
14.1 Introduction
The entire API was designed with plug and play in mind at the low level. That is you can swap out
any cipher, hash, PRNG or bignum library and the dependent API will not require updating. This
has the nice benefit that one can add ciphers (etc.) not have to re–write portions of the API. For
the most part, LibTomCrypt has also been written to be highly portable and easy to build out of
the box on pretty much any platform. As such there are no assembler inlines throughout the code,
I make no assumptions about the platform, etc...
That works well for most cases but there are times where performance is of the essence. This API
allows optimized routines to be dropped in–place of the existing portable routines. For instance,
hand optimized assembler versions of AES could be provided. Any existing function that uses the
cipher could automatically use the optimized code without re–writing. This also paves the way for
hardware drivers that can access hardware accelerated cryptographic devices.
At the heart of this flexibility is the descriptor system. A descriptor is essentially just a C struct
which describes the algorithm and provides pointers to functions that do the required work. For a
given class of operation (e.g. cipher, hash, prng, bignum) the functions of a descriptor have identical
prototypes which makes development simple. In most dependent routines all an end developer has
to do is register XXX() the descriptor and they are set.
14.2 Ciphers
The ciphers in LibTomCrypt are accessed through the ltc cipher descriptor structure.
struct ltc_cipher_descriptor {
/** name of cipher */
char *name;
/** internal ID */
unsigned char ID;
123
124 www.libtom.org
*/
void (*done)(symmetric_key *skey);
@param pt Plaintext
@param ct Ciphertext
@param blocks The number of complete blocks to process
@param IV The initial value (input/output)
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_cbc_decrypt)(const unsigned char *ct,
unsigned char *pt,
unsigned long blocks,
unsigned char *IV,
symmetric_key *skey);
int (*f9_memory)(
const unsigned char *key, unsigned long keylen,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
};
14.2.1 Name
The name parameter specifies the name of the cipher. This is what a developer would pass to
find cipher() to find the cipher in the descriptor tables.
14.2.2 Internal ID
This is a single byte Internal ID you can use to distinguish ciphers from each other.
14.2.5 Rounds
Some ciphers allow different number of rounds to be used. Usually you just use the default. The
default round count is default rounds.
14.2.6 Setup
To initialize a cipher (for ECB mode) the function setup() was provided. It accepts an array of key
octets key of length keylen octets. The user can specify the number of rounds they want through
num rounds where num rounds = 0 means use the default. The destination of a scheduled key is
stored in skey.
Inside the symmetric key union there is a void *data which you can use to allocate data if you
need a data structure that does not fit with the existing ones provided. Just make sure in your
done() function that you free the allocated memory.
14.2.8 Testing
The test() function is used to self–test the device. It takes no arguments and returns CRYPT OK if
all is working properly. You may return CRYPT NOP to indicate that no testing was performed.
14.2.10 Acceleration
The next set of functions cover the accelerated functionality of the cipher descriptor. Any combi-
nation of these functions may be set to NULL to indicate it is not supported. In those cases the
software defaults are used (using the single ECB block routines).
Accelerated ECB
These two functions are meant for cases where a user wants to encrypt (in ECB mode no less) an
array of blocks. These functions are accessed through the accel ecb encrypt and accel ecb decrypt
pointers. The blocks count is the number of complete blocks to process.
Accelerated CBC
These two functions are meant for accelerated CBC encryption. These functions are accessed
through the accel cbc encrypt and accel cbc decrypt pointers. The blocks value is the number of
complete blocks to process. The IV is the CBC initial vector. It is an input upon calling this
function and must be updated by the function before returning.
Accelerated CTR
This function is meant for accelerated CTR encryption. It is accessible through the accel ctr encrypt
pointer. The blocks value is the number of complete blocks to process. The IV is the CTR counter
vector. It is an input upon calling this function and must be updated by the function before return-
ing. The mode value indicates whether the counter is big (mode = CTR COUNTER BIG ENDIAN)
or little (mode = CTR COUNTER LITTLE ENDIAN) endian.
This function (and the way it’s called) differs from the other two since ctr encrypt() allows any
size input plaintext. The accelerator will only be called if the following conditions are met.
3. The remaining length of the input to process is greater than or equal to the block size.
14.2 Ciphers 131
The CTR pad is empty when a multiple (including zero) blocks of text have been processed.
That is, if you pass in seven bytes to AES–CTR mode you would have to pass in a minimum of
nine extra bytes before the accelerator could be called. The CTR accelerator must increment the
counter (and store it back into the buffer provided) before encrypting it to create the pad.
The accelerator will only be used to encrypt whole blocks. Partial blocks are always handled in
software.
Accelerated LRW
These functions are meant for accelerated LRW. They process blocks of input in lengths of multiples
of 16 octets. They must accept the IV and tweak state variables and updated them prior to
returning. Note that you may want to disable LRW TABLES in tomcrypt custom.h if you intend
to use accelerators for LRW.
While both encrypt and decrypt accelerators are not required it is suggested as it makes
lrw setiv() more efficient.
Note that calling lrw done() will only invoke the cipher descriptor[].done() function on the sym-
metric key parameter of the LRW state. That means if your device requires any (LRW specific)
resources you should free them in your ciphers() done function. The simplest way to think of it is
to write the plugin solely to do LRW with the cipher. That way cipher descriptor[].setup() means
to init LRW resources and cipher descriptor[].done() means to free them.
Accelerated CCM
This function is meant for accelerated CCM encryption or decryption. It processes the entire packet
in one call. You can optimize the work flow somewhat by allowing the caller to call the setup()
function first to schedule the key if your accelerator cannot do the key schedule on the fly (for
instance). This function MUST support both key passing methods.
Accelerated GCM
This function is meant for accelerated GCM encryption or decryption. It processes the entire packet
in one call. Note that the setup() function will not be called prior to this. This function must handle
scheduling the key provided on its own. It is called when the user calls gcm memory().
Accelerated OMAC
This function is meant to perform an optimized OMAC1 (CMAC) message authentication code
computation when the user calls omac memory().
132 www.libtom.org
Accelerated XCBC-MAC
This function is meant to perform an optimized XCBC-MAC message authentication code compu-
tation when the user calls xcbc memory().
Accelerated F9
This function is meant to perform an optimized F9 message authentication code computation when
the user calls f9 memory(). Like f9 memory(), it requires the caller to perform any 3GPP related
padding before calling in order to ensure proper compliance with F9.
/** internal ID */
unsigned char ID;
/** Self-test
@return CRYPT_OK if successful,
CRYPT_NOP if self-tests have been disabled
*/
int (*test)(void);
14.3.1 Name
This is the name the hash is known by and what find hash() will look for.
14.3.2 Internal ID
This is the internal ID byte used to distinguish the hash from other hashes.
14.3.6 Initialization
The init function initializes the hash and prepares it to process message bytes.
14.3.7 Process
This processes message bytes. The algorithm must accept any length of input that the hash would
allow. The input is not guaranteed to be a multiple of the block size in length.
14.3.8 Done
The done function terminates the hash and returns the message digest.
14.3.9 Acceleration
A compatible accelerator must allow processing data in any granularity which may require internal
padding on the driver side.
struct ltc_prng_descriptor {
/** Name of the PRNG */
char *name;
int (*test)(void);
};
14.4.1 Name
The name by which find prng() will find the PRNG.
14.4.3 Start
Initialize the PRNG and make it ready to accept entropy.
14.4.5 Ready
This function makes the PRNG ready to read from by processing the entropy added. The behaviour
of this function depends on the specific PRNG used.
14.4.6 Read
Read from the PRNG and return the number of bytes read. This function does not have to fill the
buffer but it is best if it does as many protocols do not retry reads and will fail on the first try.
14.4.7 Done
Terminate a PRNG state. The behaviour of this function depends on the particular PRNG used.
/** Bits per digit, amount of bits must fit in an unsigned long */
int bits_per_digit;
/** deinit
@param a The number to free
@return CRYPT_OK on success
*/
void (*deinit)(void *a);
/** copy
@param src The number to copy from
@param dst The number to write to
@return CRYPT_OK on success
*/
int (*copy)(void *src, void *dst);
*/
unsigned long (*get_int)(void *a);
*/
int (*twoexpt)(void *a , int n);
/** gcd
@param a The first integer
@param b The second integer
@param c The destination for (a, b)
@return CRYPT_OK on success
*/
int (*gcd)(void *a, void *b, void *c);
/** lcm
@param a The first integer
@param b The second integer
@param c The destination for [a, b]
@return CRYPT_OK on success
*/
int (*lcm)(void *a, void *b, void *c);
int wprng,
int size,
long e,
rsa_key *key);
Most of the functions are fairly straightforward and do not need documentation. We’ll cover
the basic conventions of the API and then explain the accelerated functions.
14.5.1 Conventions
All bignums are accessed through an opaque void * data type. You must internally cast the pointer
if you need to access members of your bignum structure. During the init calls a void ** will be
passed where you allocate your structure and set the pointer then initialize the number to zero.
During the deinit calls you must free the bignum as well as the structure you allocated to place it
in.
All functions except the Montgomery reductions work from left to right with the arguments.
For example, mul(a, b, c) computes c ← ab.
All functions (except where noted otherwise) return CRYPT OK to signify a successful oper-
ation. All error codes must be valid LibTomCrypt error codes.
The digit routines (including functions with the i suffix) use a unsigned long to represent the
digit. If your internal digit is larger than this you must then partition your digits. Normally this
does not matter as unsigned long will be the same size as your register size. Note that if your digit
is smaller than an unsigned long that is also acceptable as the bits per digit parameter will specify
this.
typedef struct {
/** The x co-ordinate */
void *x;
/** The y co-ordinate */
void *y;
/** The z co-ordinate */
void *z;
} ecc_point;
All ECC functions must use this mapping system. The only exception is when you remap
all ECC callbacks which will allow you to have more control over how the ECC math will be
implemented. Out of the box you only have three parameters per point to use (x, y, z) however,
these are just void pointers. They could point to anything you want. The only further exception is
the export functions which expects the values to be in affine format.
Point Multiply
This will multiply the point G by the scalar k and store the result in the point R. The value should
be mapped to affine only if map is set to one.
Point Addition
This will add the point P to the point Q and store it in the point R. The mp parameter is the b
value from the montgomery setup() call. The input points may be in either affine (with z = 1) or
projective format and the output point is always projective.
Point Mapping
This will map the point P back from projective to affine. The output point P must be of the form
(x, y, 1).
Shamir’s Trick
To accelerate EC–DSA verification the library provides a built–in function called ltc ecc mul2add().
This performs two point multiplications and an addition in roughly the time of one point multipli-
cation. It is called from ecc verify hash() if an accelerator is not present. The acclerator function
must allow the points to overlap (e.g., A ← k1 A + k2 B) and must return the final point in affine
format.
int type;
/** The public exponent */
void *e;
/** The private exponent */
void *d;
/** The modulus */
void *N;
/** The p factor of N */
void *p;
/** The q factor of N */
void *q;
/** The 1/q mod p CRT param */
void *qP;
/** The d mod (p - 1) CRT param */
void *dP;
/** The d mod (q - 1) CRT param */
void *dQ;
} rsa_key;
The call reads the in buffer as an unsigned char array in big endian format. Then it performs
the exponentiation and stores the output in big endian format to the out buffer. The output must
be zero padded (leading bytes) so that the length of the output matches the length of the modulus
(in bytes). For example, for RSA–1024 the output is always 128 bytes regardless of how small the
numerical value of the exponentiation is.
Since the function is given the entire RSA key (for private keys only) CRT is possible as pre-
scribed in the PKCS #1 v2.1 specification.
Index
148
Index 149
saferp desc, 15
Secure RNG, 66
SET, 98