Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# Twofish.c File Reference

`#include "aes.h"`
`#include "table.h"`
`#include <memory.h>`
`#include <assert.h>`
`#include "debug.h"`

Include dependency graph for Twofish.c:

## Defines
#define | CONST
#define | BIG_TAB 0
#define | _sBox8_(N) (((BYTE *) _sBox_) + (N)*256)
#define | FULL_KEY 1
#define | TAB_STR
#define | MOD_STRING "(Full keying)" TAB_STR
#define | Fe32_(x, R)
#define | sbSet(N, i, J, v) { _sBox_[N&2][2*i+(N&1)+2*J]=MDStab[N][v]; }
#define | GetSboxKey
#define | CHECK_TABLE 0
#define | VALIDATE_PARMS 1
#define | Mul_1 m1
#define | Mul_X mX
#define | Mul_Y mY
#define | SetMDS(N)
#define | Mul_1 Mx_1
#define | Mul_X Mx_X
#define | Mul_Y Mx_Y
#define | X_8(N) { d[N]=s[N] ^ x; d[N+1]=s[N+1] ^ x; }
#define | X_32(N) { X_8(N); X_8(N+2); X_8(N+4); X_8(N+6); }
#define | F32(res, x, k32)
#define | one128(N, J) sbSet(N,i,J,p8(N##1)[L0[i+J]]^k0)
#define | sb128(N)
#define | one192(N, J) sbSet(N,i,J,p8(N##1)[p8(N##2)[L0[i+J]]^k1]^k0)
#define | sb192(N)
#define | one256(N, J) sbSet(N,i,J,p8(N##1)[p8(N##2)[L0[i+J]]^k1]^k0)
#define | sb256(N)
#define | LoadBlockE(N) x[N]=Bswap(((DWORD *)input)[N]) ^ sk[INPUT_WHITEN+N] ^ IV[N]
#define | EncryptRound(K, R, id)
#define | Encrypt2(R, id) { EncryptRound(0,R+1,id); EncryptRound(2,R,id); }
#define | StoreBlockE(N) { t0=x[N^2] ^ sk[OUTPUT_WHITEN+N]; ((DWORD *)outBuffer)[N]=Bswap(t0); }
#define | LoadBlockD(N) x[N^2]=Bswap(((DWORD *)input)[N]) ^ sk[OUTPUT_WHITEN+N]
#define | DecryptRound(K, R, id)
#define | Decrypt2(R, id) { DecryptRound(2,R+1,id); DecryptRound(0,R,id); }
#define | StoreBlockD(N) { t0=x[N]^sk[INPUT_WHITEN+N]; ((DWORD *)outBuffer)[N] = Bswap(t0); }
#define | StoreBlockD(N)
## Functions
int | TableOp (int op)
int | ParseHexDword (int bits, char *srcTxt, DWORD *d, char *dstTxt)
DWORD | RS_MDS_Encode (DWORD k0, DWORD k1)
void | BuildMDS (void)
void | ReverseRoundSubkeys (keyInstance *key, BYTE newDir)
void | Xor256 (void *dst, void *src, BYTE b)
int | reKey (keyInstance *key)
int | makeKey (keyInstance *key, BYTE direction, int keyLen, char *keyMaterial)
int | cipherInit (cipherInstance *cipher, BYTE mode, char *IV)
int | blockEncrypt (cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
int | blockDecrypt (cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
## Variables
fullSbox | MDStab
int | needToBuildMDS = 1
int | numRounds [4] = {0,ROUNDS_128,ROUNDS_192,ROUNDS_256}
fullSbox | _sBox_
char * | moduleDescription = "Optimized C "
char * | modeString = "(Full keying)"

## Define Documentation

#define _sBox8_ | ( | N |  | ) | (((BYTE *) _sBox_) + (N)*256)

#define BIG_TAB 0

#define CHECK_TABLE 0

#define CONST

#define Decrypt2 | ( | R,
|  | id |  | ) | { DecryptRound(2,R+1,id); DecryptRound(0,R,id); }

#define DecryptRound | ( | K,
|  | R,
|  | id |  | ) |

| Value:
    t0     = Fe32##id(x[K  ],0);                    \
                t1     = Fe32##id(x[K^1],3);                    \
                DebugDump(x,"",(R)+1,0,0,1,0);                  \
                x[K^2] = ROL (x[K^2],1);                        \
                x[K^2]^= t0 +   t1 + sk[ROUND_SUBKEYS+2*(R)  ]; \
                x[K^3]^= t0 + 2*t1 + sk[ROUND_SUBKEYS+2*(R)+1]; \
                x[K^3] = ROR (x[K^3],1);                        \

#define Encrypt2 | ( | R,
|  | id |  | ) | { EncryptRound(0,R+1,id); EncryptRound(2,R,id); }

#define EncryptRound | ( | K,
|  | R,
|  | id |  | ) |

| Value:
    t0     = Fe32##id(x[K  ],0);                    \
                t1     = Fe32##id(x[K^1],3);                    \
                x[K^3] = ROL(x[K^3],1);                         \
                x[K^2]^= t0 +   t1 + sk[ROUND_SUBKEYS+2*(R)  ]; \
                x[K^3]^= t0 + 2*t1 + sk[ROUND_SUBKEYS+2*(R)+1]; \
                x[K^2] = ROR(x[K^2],1);                         \
                DebugDump(x,"",rounds-(R),0,0,1,0);

#define F32 | ( | res,
|  | x,
|  | k32 |  | ) |

| Value:
    {                                                           \
        DWORD t=x;                                                  \
        switch (k64Cnt & 3)                                         \
            {                                                       \
            case 0:                                 \
                        b0(t)   = p8(04)[b0(t)] ^ b0(k32[3]);       \
                        b1(t)   = p8(14)[b1(t)] ^ b1(k32[3]);       \
                        b2(t)   = p8(24)[b2(t)] ^ b2(k32[3]);       \
                        b3(t)   = p8(34)[b3(t)] ^ b3(k32[3]);       \
                            \
            case 3:     b0(t)   = p8(03)[b0(t)] ^ b0(k32[2]);       \
                        b1(t)   = p8(13)[b1(t)] ^ b1(k32[2]);       \
                        b2(t)   = p8(23)[b2(t)] ^ b2(k32[2]);       \
                        b3(t)   = p8(33)[b3(t)] ^ b3(k32[2]);       \
                            \
            case 2:     \
                res=    MDStab[0][p8(01)[p8(02)[b0(t)] ^ b0(k32[1])] ^ b0(k32[0])] ^    \
                        MDStab[1][p8(11)[p8(12)[b1(t)] ^ b1(k32[1])] ^ b1(k32[0])] ^    \
                        MDStab[2][p8(21)[p8(22)[b2(t)] ^ b2(k32[1])] ^ b2(k32[0])] ^    \
                        MDStab[3][p8(31)[p8(32)[b3(t)] ^ b3(k32[1])] ^ b3(k32[0])] ;    \
            }                                                       \
        }

#define Fe32_ | ( | x,
|  | R |  | ) |

| Value:
    (_sBox_[0][2*_b(x,R  )] ^ _sBox_[0][2*_b(x,R+1)+1] ^    \
                        _sBox_[2][2*_b(x,R+2)] ^ _sBox_[2][2*_b(x,R+3)+1])

#define FULL_KEY 1

#define GetSboxKey

#define LoadBlockD | ( | N |  | ) | x[N^2]=Bswap(((DWORD *)input)[N]) ^ sk[OUTPUT_WHITEN+N]

#define LoadBlockE | ( | N |  | ) | x[N]=Bswap(((DWORD *)input)[N]) ^ sk[INPUT_WHITEN+N] ^ IV[N]

#define MOD_STRING "(Full keying)" TAB_STR

#define Mul_1 Mx_1

#define Mul_1 m1

#define Mul_X Mx_X

#define Mul_X mX

#define Mul_Y Mx_Y

#define Mul_Y mY

#define one128 | ( | N,
|  | J |  | ) | sbSet(N,i,J,p8(N##1)[L0[i+J]]^k0)

#define one192 | ( | N,
|  | J |  | ) | sbSet(N,i,J,p8(N##1)[p8(N##2)[L0[i+J]]^k1]^k0)

#define one256 | ( | N,
|  | J |  | ) | sbSet(N,i,J,p8(N##1)[p8(N##2)[L0[i+J]]^k1]^k0)

#define sb128 | ( | N |  | ) |

| Value:
    {                   \
                    Xor256(L0,p8(N##2),b##N(sKey[1]));  \
                    { register DWORD k0=b##N(sKey[0]);  \
                    for (i=0;i<256;i+=2) { one128(N,0); one128(N,1); } } }

#define sb192 | ( | N |  | ) |

| Value:
    {                       \
                    Xor256(L0,p8(N##3),b##N(sKey[2]));  \
                    { register DWORD k0=b##N(sKey[0]);  \
                      register DWORD k1=b##N(sKey[1]);  \
                      for (i=0;i<256;i+=2) { one192(N,0); one192(N,1); } } }

#define sb256 | ( | N |  | ) |

| Value:
    {                                       \
                    Xor256(L1,p8(N##4),b##N(sKey[3]));                  \
                    for (i=0;i<256;i+=2) {L0[i  ]=p8(N##3)[L1[i]];      \
                                          L0[i+1]=p8(N##3)[L1[i+1]]; }  \
                    Xor256(L0,L0,b##N(sKey[2]));                        \
                    { register DWORD k0=b##N(sKey[0]);                  \
                      register DWORD k1=b##N(sKey[1]);                  \
                      for (i=0;i<256;i+=2) { one256(N,0); one256(N,1); } } }

#define sbSet | ( | N,
|  | i,
|  | J,
|  | v |  | ) | { _sBox_[N&2][2*i+(N&1)+2*J]=MDStab[N][v]; }

#define SetMDS | ( | N |  | ) |

| Value:
    b0(d) = M0##N[P_##N##0];    \
            b1(d) = M1##N[P_##N##0];    \
            b2(d) = M2##N[P_##N##0];    \
            b3(d) = M3##N[P_##N##0];    \
            MDStab[N][i] = d;

#define StoreBlockD | ( | N |  | ) |

| Value:
    x[N]   ^= sk[INPUT_WHITEN+N] ^ IV[N];   \
                            IV[N]   = Bswap(((DWORD *)input)[N]);   \
                            ((DWORD *)outBuffer)[N] = Bswap(x[N]);

#define StoreBlockD | ( | N |  | ) | { t0=x[N]^sk[INPUT_WHITEN+N]; ((DWORD *)outBuffer)[N] = Bswap(t0); }

#define StoreBlockE | ( | N |  | ) | { t0=x[N^2] ^ sk[OUTPUT_WHITEN+N]; ((DWORD *)outBuffer)[N]=Bswap(t0); }

#define TAB_STR

#define VALIDATE_PARMS 1

#define X_32 | ( | N |  | ) | { X_8(N); X_8(N+2); X_8(N+4); X_8(N+6); }

#define X_8 | ( | N |  | ) | { d[N]=s[N] ^ x; d[N+1]=s[N+1] ^ x; }

## Function Documentation

int blockDecrypt | ( | cipherInstance * | cipher,
|  | keyInstance * | key,
|  | BYTE * | input,
|  | int | inputLen,
|  | BYTE * | outBuffer
| ) |

int blockEncrypt | ( | cipherInstance * | cipher,
|  | keyInstance * | key,
|  | BYTE * | input,
|  | int | inputLen,
|  | BYTE * | outBuffer
| ) |

void BuildMDS | ( | void |  | ) |

int cipherInit | ( | cipherInstance * | cipher,
|  | BYTE | mode,
|  | char * | IV
| ) |

int makeKey | ( | keyInstance * | key,
|  | BYTE | direction,
|  | int | keyLen,
|  | char * | keyMaterial
| ) |

int ParseHexDword | ( | int | bits,
|  | char * | srcTxt,
|  | DWORD * | d,
|  | char * | dstTxt
| ) |

int reKey | ( | keyInstance * | key | ) |

void ReverseRoundSubkeys | ( | keyInstance * | key,
|  | BYTE | newDir
| ) |

DWORD RS_MDS_Encode | ( | DWORD | k0,
|  | DWORD | k1
| ) |

int TableOp | ( | int | op | ) |

void Xor256 | ( | void * | dst,
|  | void * | src,
|  | BYTE | b
| ) |

## Variable Documentation

fullSbox _sBox_`[static]`

fullSbox MDStab

char* modeString = "(Full keying)"

char* moduleDescription = "Optimized C "

int needToBuildMDS = 1

int numRounds[4] = {0,ROUNDS_128,ROUNDS_192,ROUNDS_256}