Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

TwoFishCipher Class Reference

Cryptographic twofish cipher. More...

#include <twofishcipher.h>

Inheritance diagram for TwoFishCipher:

Inheritance graph
[legend]
Collaboration diagram for TwoFishCipher:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 TwoFishCipher ()
 Construct a twofish cipher; key must be set separately.

 ~TwoFishCipher ()
 Destructor: Cleans up the key data.

const Parameters * GetPar () const
 Return pointer to static parameters.

int SetKey (char *userkey, size_t len)
 Set the key for the following encryption/decryption.

int CheckWeakKey ()
 Weak key check not supported - don't know of weak keys.

int CopyFrom (BlockCipherBase *from)
 Copy the prepared keys from another TwoFishCipher.

void EncryptBlock (char *ibuf, char *obuf)
void DecryptBlock (char *ibuf, char *obuf)

Static Public Attributes

const BlockCipherBase::Parameters par
 Parameters of the block cipher.


Private Member Functions

void _reset ()
 Clean up the key data.

 TwoFishCipher (const TwoFishCipher &)
 Do not use these:.

void operator= (const TwoFishCipher &)

Private Attributes

uint32 c_w [8]
uint32 c_k [32]
uint32 c_s [4][256]

Detailed Description

Cryptographic twofish cipher.

Author:
Wolfgang Wieser ] wwieser (a) gmx <*> de [
This is a very fast implementation of the twofish algorithm.

I fiddled around with the code for several hours to make it smaller (as of executable size) than the original implementation. while not slowing down (too) much. The original author(s) thought unrolling all loops by hand would make the code very fast.

WRONG! I decreased runtime from 0.69sec to 0.48sec by inserting the decryption for() loop (see where DECCYCLE(i) is called)!!!

Originally: about 32kb stripped object code; now: 13kb

While decryption was speed up (0.69 -> 0.48 sec), decrpytion got slower (0.74 -> 0.85 sec) (grrr)

ADVANTAGE: FAST

Note:
sizeof(TwoFishCipher)=4256 bytes (!) snd thus nothing for small stacks..

Definition at line 66 of file twofishcipher.h.


Constructor & Destructor Documentation

TwoFishCipher::TwoFishCipher const TwoFishCipher  )  [private]
 

Do not use these:.

TwoFishCipher::TwoFishCipher  )  [inline]
 

Construct a twofish cipher; key must be set separately.

Definition at line 94 of file twofishcipher.h.

TwoFishCipher::~TwoFishCipher  )  [inline]
 

Destructor: Cleans up the key data.

Definition at line 96 of file twofishcipher.h.

References _reset().


Member Function Documentation

void TwoFishCipher::_reset  )  [private]
 

Clean up the key data.

Definition at line 838 of file twofishcipher.cc.

References c_k, c_s, c_w, and uint32.

Referenced by ~TwoFishCipher().

int TwoFishCipher::CheckWeakKey  )  [virtual]
 

Weak key check not supported - don't know of weak keys.

This function will currently never return 1.

Returns:
This returns 1, if the currently set key is weak. Returns 0 if the key is not weak.

Reimplemented from BlockCipherBase.

Definition at line 698 of file twofishcipher.cc.

int TwoFishCipher::CopyFrom BlockCipherBase from  )  [virtual]
 

Copy the prepared keys from another TwoFishCipher.

Returns:
0 -> OK -2 -> from is not a TwoFishCipher.

Reimplemented from BlockCipherBase.

Definition at line 851 of file twofishcipher.cc.

References c_k, c_s, c_w, BlockCipherBase::Parameters::cipher_ID, BlockCipherBase::GetPar(), par, and uint32.

void TwoFishCipher::DecryptBlock char *  ibuf,
char *  obuf
[virtual]
 

Reimplemented from BlockCipherBase.

Definition at line 779 of file twofishcipher.cc.

References c_k, c_s, INPACK, OUTPACK, and uint32.

Referenced by test_twofish().

void TwoFishCipher::EncryptBlock char *  ibuf,
char *  obuf
[virtual]
 

ibuf and obuf do not have to be aligned.
You may set ibuf=obuf.

Reimplemented from BlockCipherBase.

Definition at line 721 of file twofishcipher.cc.

References c_k, c_s, INPACK, OUTPACK, and uint32.

Referenced by test_twofish().

const Parameters* TwoFishCipher::GetPar  )  const [inline, virtual]
 

Return pointer to static parameters.

Reimplemented from BlockCipherBase.

Definition at line 99 of file twofishcipher.h.

References par.

void TwoFishCipher::operator= const TwoFishCipher  )  [private]
 

int TwoFishCipher::SetKey char *  userkey,
size_t  len
[virtual]
 

Set the key for the following encryption/decryption.

userkey must be 128 or 256 bits long (see BlockCipher::KeyLength()).

The key does not have to be aligned.
The key length is passed in len (must be 16 or 32 [bytes]).

Make sure, len is a value as returned by BlockCipher::KeyLength() or the functin fails.

Returns:
0 -> OK; -2 -> illegal key size.

Reimplemented from BlockCipherBase.

Definition at line 520 of file twofishcipher.cc.

References c_k, c_s, c_w, calc_sb_tbl, CALC_Sx, uint32, Xmds(), Xq0(), and Xq1().

Referenced by test_twofish().


Member Data Documentation

uint32 TwoFishCipher::c_k[32] [private]
 

Definition at line 82 of file twofishcipher.h.

Referenced by _reset(), CopyFrom(), DecryptBlock(), EncryptBlock(), and SetKey().

uint32 TwoFishCipher::c_s[4][256] [private]
 

Definition at line 83 of file twofishcipher.h.

Referenced by _reset(), CopyFrom(), DecryptBlock(), EncryptBlock(), and SetKey().

uint32 TwoFishCipher::c_w[8] [private]
 

Structure for an expanded Twofish key.
s contains the key-dependent S-boxes composed with the MDS matrix;
w contains the eight "whitening" subkeys, K[0] through K[7].
k holds the remaining, "round" subkeys.
Note that k[i] corresponds to what the Twofish paper calls K[i+8].

Definition at line 81 of file twofishcipher.h.

Referenced by _reset(), CopyFrom(), and SetKey().

const BlockCipherBase::Parameters TwoFishCipher::par [static]
 

Initial value:

 
{
    INIT_FIELD(block_size) 16,
    INIT_FIELD(n_key_lengths) 2,
    INIT_FIELD(key_length) twofish_par_kl,
    INIT_FIELD(cipher_ID) 0x2F15,
    INIT_FIELD(name) "twofish"
}
Parameters of the block cipher.

Reimplemented from BlockCipherBase.

Definition at line 867 of file twofishcipher.cc.

Referenced by CopyFrom(), and GetPar().


The documentation for this class was generated from the following files:
Generated on Sat Feb 19 22:35:54 2005 for Ray by doxygen 1.3.5