Data Structures | Defines | Functions

# twofish.c File Reference

`#include "wizard/studio.h"`
`#include "wizard/exception.h"`
`#include "wizard/exception-private.h"`
`#include "wizard/memory_.h"`
`#include "wizard/twofish.h"`

## Data Structures

struct | _TwofishInfo

## Defines

#define | G1(alpha)
#define | G2(beta)
#define | Read32Bits(value, p)
#define | TwofishBlocksize 16
#define | Write32Bits(p, value)
#define | DecipherRound(alpha, beta, gamma, delta, i)
#define | EncipherRound(alpha, beta, gamma, delta, i)
#define | K128(x, j, k, l, m, n)
#define | K192(x, j, k, l, m, n)
#define | K256(x, j, k, l, m, n)
#define | MaximumTwofishKeyLength 32
#define | Q128(alpha, beta, gamma, delta, i)
#define | Q192(alpha, beta, gamma, delta, i)
#define | Q256(alpha, beta, i)
#define | Sbox(alpha, beta, gamma, delta, i, w, x, y, z)

## Functions

WizardExport TwofishInfo * | AcquireTwofishInfo (void)
WizardExport void | DecipherTwofishBlock (TwofishInfo *twofish_info, const unsigned char *ciphertext, unsigned char *plaintext)
WizardExport TwofishInfo * | DestroyTwofishInfo (TwofishInfo *twofish_info)
WizardExport void | EncipherTwofishBlock (TwofishInfo *twofish_info, const unsigned char *plaintext, unsigned char *ciphertext)
WizardExport unsigned int | GetTwofishBlocksize (const TwofishInfo *twofish_info)
WizardExport void | SetTwofishKey (TwofishInfo *twofish_info, const StringInfo *key)

## Define Documentation

#define DecipherRound | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i |
| ) |  |  |

Value:

    { \
      rho=G1(alpha); sigma=G2(beta); rho+=sigma; sigma+=rho; \
      delta^=sigma+twofish_info->k[2*(i)+1]; delta=(delta >> 1)+(delta << 31); \
      gamma=(gamma << 1)+(gamma >> 31); gamma^=(rho+twofish_info->k[2*(i)]); \
    }

Referenced by DecipherTwofishBlock().

#define EncipherRound | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i |
| ) |  |  |

Value:

    { \
      rho=G1(alpha); sigma=G2(beta); \
      rho+=sigma; sigma+=rho+twofish_info->k[2*(i)+1]; \
      gamma^=rho+twofish_info->k[2*(i)]; gamma=(gamma >> 1)+(gamma << 31); \
      delta=((delta << 1)+(delta >> 31)) ^ sigma; \
    }

Referenced by EncipherTwofishBlock().

#define G1 | ( |  | alpha | ) |

Value:

    (twofish_info->sbox[0][(alpha) & 0xFF]) ^ \
      (twofish_info->sbox[1][((alpha) >> 8) & 0xFF]) ^ \
      (twofish_info->sbox[2][((alpha) >> 16) & 0xFF]) ^ \
      (twofish_info->sbox[3][(alpha) >> 24])

#define G2 | ( |  | beta | ) |

Value:

    (twofish_info->sbox[1][(beta) & 0xFF]) ^ \
      (twofish_info->sbox[2][((beta) >> 8) & 0xFF]) ^ \
      (twofish_info->sbox[3][((beta) >> 16) & 0xFF]) ^ \
      (twofish_info->sbox[0][(beta) >> 24])

#define K128 | ( |  | x,
|  |  | j,
|  |  | k,
|  |  | l,
|  |  | m,
|  |  | n |
| ) |  |  |

Value:

    { \
      rho=Q128(k,l,k,l,0); sigma=Q128(m,n,m,n,4); \
      sigma=(sigma << 8)+(sigma >> 24); rho+=sigma; sigma+=rho; \
      twofish_info->x[j]=rho; twofish_info->x[(j)+1]=(sigma << 9)+(sigma >> 23); \
    }

Referenced by SetTwofishKey().

#define K192 | ( |  | x,
|  |  | j,
|  |  | k,
|  |  | l,
|  |  | m,
|  |  | n |
| ) |  |  |

Value:

    { \
      rho=Q192(l,l,k,k,0); sigma=Q192(n,n,m,m,4); \
      sigma=(sigma << 8)+(sigma >> 24); rho+=sigma; sigma+=rho; \
      twofish_info->x[j]=rho; twofish_info->x[(j)+1]=(sigma << 9)+(sigma >> 23); \
    }

Referenced by SetTwofishKey().

#define K256 | ( |  | x,
|  |  | j,
|  |  | k,
|  |  | l,
|  |  | m,
|  |  | n |
| ) |  |  |

Value:

    { \
      rho=Q256(k,l,0); sigma=Q256(m,n,4); \
      sigma=(sigma << 8)+(sigma >> 24); rho+=sigma; sigma+=rho; \
      twofish_info->x[j]=rho; twofish_info->x[(j)+1]=(sigma << 9)+(sigma >> 23); \
    }

Referenced by SetTwofishKey().

#define MaximumTwofishKeyLength 32

Referenced by SetTwofishKey().

#define Q128 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i |
| ) |  |  |

Value:

    mds[0][q0[alpha ^ crypt_key[(i)+8]] ^ crypt_key[i]] ^ \
      mds[1][q0[beta ^ crypt_key[(i)+9]] ^ crypt_key[(i)+1]] ^ \
      mds[2][q1[gamma ^ crypt_key[(i)+10]] ^ crypt_key[(i)+2]] ^ \
      mds[3][q1[delta ^ crypt_key[(i)+11]] ^ crypt_key[(i)+3]]

#define Q192 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i |
| ) |  |  |

Value:

    Q128(q0[alpha ^ crypt_key[(i)+16]],q1[beta ^ crypt_key[(i)+17]], \
        q0[gamma ^ crypt_key[(i)+18]],q1[delta ^ crypt_key[(i)+19]],i)

#define Q256 | ( |  | alpha,
|  |  | beta,
|  |  | i |
| ) |  |  |

Value:

    Q192(q1[beta ^ crypt_key[(i)+24]],q1[alpha ^ crypt_key[(i)+25]], \
        q0[alpha ^ crypt_key[(i)+26]],q0[beta ^ crypt_key[(i)+27]],i)

#define Read32Bits | ( |  | value,
|  |  | p |
| ) |  |  |

Value:

    { \
      (value)=(*p++); \
      (value)|=(*p++) << 8; \
      (value)|=(*p++) << 16; \
      (value)|=(*p++) << 24; \
    }

#define Sbox | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i,
|  |  | w,
|  |  | x,
|  |  | y,
|  |  | z |
| ) |  |  |

Value:

    { \
      if (crypt_key[i] != 0) \
        { \
          xi=polynomial_to_exponent[crypt_key[i]-1]; \
          (alpha)^=exponent_to_polynomial[xi+(w)]; \
          (beta)^=exponent_to_polynomial[xi+(x)]; \
          (gamma)^=exponent_to_polynomial[xi+(y)]; \
          (delta)^=exponent_to_polynomial[xi+(z)]; \
       } \
    }

Referenced by SetTwofishKey().

#define TwofishBlocksize 16

#define Write32Bits | ( |  | p,
|  |  | value |
| ) |  |  |

Value:

    { \
      *p++=(unsigned char) (value); \
      *p++=(unsigned char) ((value) >> 8); \
      *p++=(unsigned char) ((value) >> 16); \
      *p++=(unsigned char) ((value) >> 24); \
    }

Referenced by DecipherTwofishBlock(), and EncipherTwofishBlock().

## Function Documentation

WizardExport TwofishInfo* AcquireTwofishInfo | ( | void |  | ) |

References AcquireWizardMemory(), _TwofishInfo::blocksize, CipherDomain, MemoryError, ResetWizardMemory(), _TwofishInfo::signature, ThrowWizardFatalError, and _TwofishInfo::timestamp.

Referenced by AcquireCipherInfo().

WizardExport void DecipherTwofishBlock | ( | TwofishInfo * | twofish_info,
|  | const unsigned char * | ciphertext,
|  | unsigned char * | plaintext |
| ) |  |  |

References DecipherRound, _TwofishInfo::w, and Write32Bits.

Referenced by AcquireCipherInfo().

WizardExport TwofishInfo* DestroyTwofishInfo | ( | TwofishInfo * | twofish_info | ) |

References CipherDomain, GetWizardModule, LogWizardEvent(), RelinquishWizardMemory(), _TwofishInfo::signature, TraceEvent, WizardAssert, and WizardSignature.

Referenced by DestroyCipherInfo().

WizardExport void EncipherTwofishBlock | ( | TwofishInfo * | twofish_info,
|  | const unsigned char * | plaintext,
|  | unsigned char * | ciphertext |
| ) |  |  |

References EncipherRound, _TwofishInfo::w, and Write32Bits.

Referenced by AcquireCipherInfo().

WizardExport unsigned int GetTwofishBlocksize | ( | const TwofishInfo * | twofish_info | ) |

References _TwofishInfo::blocksize, CipherDomain, GetWizardModule, LogWizardEvent(), _TwofishInfo::signature, TraceEvent, WizardAssert, and WizardSignature.

Referenced by AcquireCipherInfo().

WizardExport void SetTwofishKey | ( | TwofishInfo * | twofish_info,
|  | const StringInfo * | key |
| ) |  |  |

References CipherDomain, GetStringInfoDatum(), GetStringInfoLength(), GetWizardModule, K128, K192, K256, LogWizardEvent(), MaximumTwofishKeyLength, Min, ResetWizardMemory(), _TwofishInfo::sbox, Sbox, _TwofishInfo::signature, TraceEvent, WizardAssert, and WizardSignature.

Referenced by SetCipherKey().