ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Macros | Typedefs | Functions
twofish.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Twofish_key
 Structure that contains a prepared Twofish key. More...
 

Macros

#define SUCCESS   1
 Return codes. More...
 
#define ERR_UINT32   -2
 
#define ERR_BYTE   -3
 
#define ERR_GET32   -4
 
#define ERR_PUT32   -5
 
#define ERR_ROLR   -6
 
#define ERR_BSWAP   -7
 
#define ERR_SELECTB   -8
 
#define ERR_TEST_ENC   -9
 
#define ERR_TEST_DEC   -10
 
#define ERR_SEQ_ENC   -11
 
#define ERR_SEQ_DEC   -12
 
#define ERR_ODD_KEY   -13
 
#define ERR_INIT   -14
 
#define ERR_KEY_LEN   -15
 
#define ERR_ILL_ARG   -16
 

Typedefs

typedef unsigned char Twofish_Byte
 A Twofish_Byte must be an unsigned 8-bit integer. More...
 
typedef unsigned int Twofish_UInt32
 A Twofish_UInt32 must be an unsigned integer of at least 32 bits. More...
 

Functions

int Twofish_initialise ()
 Initialise and test the Twofish implementation. More...
 
int Twofish_prepare_key (Twofish_Byte key[], int key_len, Twofish_key *xkey)
 Convert a cipher key to the internal form used for encryption and decryption. More...
 
void Twofish_encrypt (Twofish_key *xkey, Twofish_Byte p[16], Twofish_Byte c[16])
 Encrypt a single block of data. More...
 
void Twofish_decrypt (Twofish_key *xkey, Twofish_Byte c[16], Twofish_Byte p[16])
 Decrypt a single block of data. More...
 

Macro Definition Documentation

#define ERR_BSWAP   -7

Definition at line 70 of file twofish.h.

#define ERR_BYTE   -3

Definition at line 66 of file twofish.h.

#define ERR_GET32   -4

Definition at line 67 of file twofish.h.

#define ERR_ILL_ARG   -16

Definition at line 79 of file twofish.h.

#define ERR_INIT   -14

Definition at line 77 of file twofish.h.

#define ERR_KEY_LEN   -15

Definition at line 78 of file twofish.h.

#define ERR_ODD_KEY   -13

Definition at line 76 of file twofish.h.

#define ERR_PUT32   -5

Definition at line 68 of file twofish.h.

#define ERR_ROLR   -6

Definition at line 69 of file twofish.h.

#define ERR_SELECTB   -8

Definition at line 71 of file twofish.h.

#define ERR_SEQ_DEC   -12

Definition at line 75 of file twofish.h.

#define ERR_SEQ_ENC   -11

Definition at line 74 of file twofish.h.

#define ERR_TEST_DEC   -10

Definition at line 73 of file twofish.h.

#define ERR_TEST_ENC   -9

Definition at line 72 of file twofish.h.

#define ERR_UINT32   -2

Definition at line 65 of file twofish.h.

#define SUCCESS   1

Return codes.

Definition at line 64 of file twofish.h.

Typedef Documentation

typedef unsigned char Twofish_Byte

A Twofish_Byte must be an unsigned 8-bit integer.

It must also be the elementary data size of your C platform, i.e. sizeof( Twofish_Byte ) == 1.

Definition at line 40 of file twofish.h.

typedef unsigned int Twofish_UInt32

A Twofish_UInt32 must be an unsigned integer of at least 32 bits.

This type is used only internally in the implementation, so ideally it would not appear in the header file, but it is used inside the Twofish_key structure which means it has to be included here.

Definition at line 49 of file twofish.h.

Function Documentation

void Twofish_decrypt ( Twofish_key xkey,
Twofish_Byte  c[16],
Twofish_Byte  p[16] 
)

Decrypt a single block of data.

This function decrypts a single block of 16 bytes of data. If you want to decrypt a larger or variable-length message, you will have to use a cipher mode, such as CBC or CTR. These are outside the scope of this implementation.

The xkey structure is not modified by this routine, and can be used for further encryption and decryption operations.

Parameters
xkeypointer to Twofish_key, internal form of the key produces by Twofish_prepare_key()
cCiphertext to be decrypted
pPlace to store the plaintext
void Twofish_encrypt ( Twofish_key xkey,
Twofish_Byte  p[16],
Twofish_Byte  c[16] 
)

Encrypt a single block of data.

This function encrypts a single block of 16 bytes of data. If you want to encrypt a larger or variable-length message, you will have to use a cipher mode, such as CBC or CTR. These are outside the scope of this implementation.

The xkey structure is not modified by this routine, and can be used for further encryption and decryption operations.

Parameters
xkeypointer to Twofish_key, internal form of the key produces by Twofish_prepare_key()
pPlaintext to be encrypted
cPlace to store the ciphertext
int Twofish_initialise ( )

Initialise and test the Twofish implementation.

This function MUST be called before any other function in the Twofish implementation is called. It only needs to be called once.

Apart from initialising the implementation it performs a self test. If the Twofish_fatal function is not called, the code passed the test. (See the twofish.c file for details on the Twofish_fatal function.)

Returns
a negative number if an error happend, +1 otherwise
int Twofish_prepare_key ( Twofish_Byte  key[],
int  key_len,
Twofish_key xkey 
)

Convert a cipher key to the internal form used for encryption and decryption.

The cipher key is an array of bytes; the Twofish_Byte type is defined above to a type suitable on your platform.

Any key must be converted to an internal form in the Twofisk_key structure before it can be used. The encryption and decryption functions only work with the internal form. The conversion to internal form need only be done once for each key value.

Be sure to wipe all key storage, including the Twofish_key structure, once you are done with the key data. A simple memset( TwofishKey, 0, sizeof( TwofishKey ) ) will do just fine.

Unlike most implementations, this one allows any key size from 0 bytes to 32 bytes. According to the Twofish specifications, irregular key sizes are handled by padding the key with zeroes at the end until the key size is 16, 24, or 32 bytes, whichever comes first. Note that each key of irregular size is equivalent to exactly one key of 16, 24, or 32 bytes.

WARNING: Short keys have low entropy, and result in low security. Anything less than 8 bytes is utterly insecure. For good security use at least 16 bytes. I prefer to use 32-byte keys to prevent any collision attacks on the key.

The key length argument key_len must be in the proper range. If key_len is not in the range 0,...,32 this routine attempts to generate a fatal error (depending on the code environment), and at best (or worst) returns without having done anything.

Parameters
keyArray of key bytes
key_lenNumber of key bytes, must be in the range 0,1,...,32. xkey Pointer to an Twofish_key structure that will be filled with the internal form of the cipher key.
Returns
a negative number if an error happend, +1 otherwise