Data Structures | Defines | Functions

# serpent.c File Reference

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

## Data Structures

struct | _SerpentInfo

## Defines

#define | Read32Bits(value, p)
#define | SerpentBlocksize 32
#define | Write32Bits(p, value)
#define | DecipherSbox0(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox1(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox2(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox3(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox4(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox5(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox6(alpha, beta, gamma, delta, epsilon)
#define | DecipherSbox7(alpha, beta, gamma, delta, epsilon)
#define | DecipherK(alpha, beta, gamma, delta, epsilon, i)
#define | MixKey(alpha, beta, gamma, delta, i)
#define | EncipherK(alpha, beta, gamma, delta, epsilon, i)
#define | EncipherSbox0(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox1(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox2(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox3(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox4(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox5(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox6(alpha, beta, gamma, delta, epsilon)
#define | EncipherSbox7(alpha, beta, gamma, delta, epsilon)
#define | ChurnKey(alpha, beta, gamma, delta, i, j)
#define | LoadKey(alpha, beta, gamma, delta, i)
#define | MaximumSerpentKeyLength 32
#define | StoreKey(alpha, beta, gamma, delta, i)

## Functions

WizardExport SerpentInfo * | AcquireSerpentInfo (void)
static unsigned int | Trunc32 (unsigned int x)
static unsigned int | RotateRight (unsigned int x, unsigned int n)
WizardExport void | DecipherSerpentBlock (SerpentInfo *serpent_info, const unsigned char *ciphertext, unsigned char *plaintext)
WizardExport SerpentInfo * | DestroySerpentInfo (SerpentInfo *serpent_info)
static unsigned int | RotateLeft (unsigned int x, unsigned int n)
WizardExport void | EncipherSerpentBlock (SerpentInfo *serpent_info, const unsigned char *plaintext, unsigned char *ciphertext)
WizardExport unsigned int | GetSerpentBlocksize (const SerpentInfo *serpent_info)
WizardExport void | SetSerpentKey (SerpentInfo *serpent_info, const StringInfo *key)

## Define Documentation

#define ChurnKey | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | i,
|  |  | j |
| ) |  |  |

Value:

    { \
      beta^=delta; beta^=gamma; beta^=alpha; beta^=0x9e3779b9UL ^ i; \
      beta=RotateLeft(beta,11); crypt_key[j]=beta; \
    }

Referenced by SetSerpentKey().

#define DecipherK | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon,
|  |  | i |
| ) |  |  |

Value:

    { \
      MixKey(alpha,beta,gamma,delta,i) \
      alpha=RotateRight(alpha,5); gamma=RotateRight(gamma,22); \
      alpha^=delta; gamma^=delta; epsilon=beta << 7; alpha^=beta; \
      beta=RotateRight(beta,1); gamma^=epsilon; delta=RotateRight(delta,7); \
      epsilon=alpha << 3; beta^=alpha; delta^=epsilon; \
      alpha=RotateRight(alpha,13); beta^=gamma; delta^=gamma; \
      gamma=RotateRight(gamma,3); \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox0 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      beta^=alpha; epsilon=delta; delta|=beta; epsilon^=beta; \
      alpha=(~alpha); gamma^=delta; delta^=alpha; alpha&=beta; \
      alpha^=gamma; gamma&=delta; delta^=epsilon; gamma^=delta; \
      beta^=delta; delta&=alpha; beta^=alpha; alpha^=gamma; epsilon^=delta; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox1 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      beta^=delta; epsilon=alpha; alpha^=gamma; gamma=(~gamma); \
      epsilon|=beta; epsilon^=delta; delta&=beta; beta^=gamma; \
      gamma&=epsilon; epsilon^=beta; beta|=delta; delta^=alpha; \
      gamma^=alpha; alpha|=epsilon; gamma^=epsilon; beta^=alpha; epsilon^=beta; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox2 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      gamma^=beta; epsilon=delta; delta=(~delta); delta|=gamma; \
      gamma^=epsilon; epsilon^=alpha; delta^=beta; beta|=gamma; \
      gamma^=alpha;  beta^=epsilon; epsilon|=delta; gamma^=delta; \
      epsilon^=gamma; gamma&=beta; gamma^=delta; delta^=epsilon; epsilon^=alpha; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox3 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      gamma^=beta; epsilon=beta; beta&=gamma; beta^=alpha; \
      alpha|=epsilon; epsilon^=delta; alpha^=delta; delta|=beta; \
      beta^=gamma; beta^=delta; alpha^=gamma; gamma^=delta; delta&=beta; \
      beta^=alpha; alpha&=gamma; epsilon^=delta; delta^=alpha; alpha^=beta; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox4 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      gamma^=delta; epsilon=alpha; alpha&=beta; alpha^=gamma; \
      gamma|=delta; epsilon=(~epsilon); beta^=alpha; alpha^=gamma; \
      gamma&=epsilon; gamma^=alpha; alpha|=epsilon; alpha^=delta; \
      delta&=gamma; epsilon^=delta; delta^=beta; beta&=alpha; \
      epsilon^=beta; alpha^=delta; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox5 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=beta; beta|=gamma; gamma^=epsilon; beta^=delta; \
      delta&=epsilon; gamma^=delta; delta|=alpha; alpha=(~alpha); \
      delta^=gamma; gamma|=alpha; epsilon^=beta; gamma^=epsilon; \
      epsilon&=alpha; alpha^=beta; beta^=delta; alpha&=gamma; \
      gamma^=delta; alpha^=gamma; gamma^=epsilon; epsilon^=delta; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox6 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      alpha^=gamma; epsilon=alpha; alpha&=delta; gamma^=delta; \
      alpha^=gamma; delta^=beta; gamma|=epsilon; gamma^=delta; \
      delta&=alpha; alpha=(~alpha); delta^=beta; beta&=gamma; \
      epsilon^=alpha; delta^=epsilon; epsilon^=gamma; alpha^=beta; gamma^=alpha; \
    }

Referenced by DecipherSerpentBlock().

#define DecipherSbox7 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=delta; delta&=alpha; alpha^=gamma; gamma|=epsilon; \
      epsilon^=beta; alpha=(~alpha); beta|=delta; epsilon^=alpha; \
      alpha&=gamma; alpha^=beta; beta&=gamma; delta^=gamma; \
      epsilon^=delta; gamma&=delta; delta|=alpha; beta^=epsilon; \
      delta^=epsilon; epsilon&=alpha;  epsilon^=gamma; \
    }

Referenced by DecipherSerpentBlock().

#define EncipherK | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon,
|  |  | i |
| ) |  |  |

Value:

    { \
      alpha=RotateLeft(alpha,13); gamma=RotateLeft(gamma,3); \
      beta^=alpha; epsilon=alpha << 3; delta^=gamma; beta^=gamma; \
      beta=RotateLeft(beta,1); delta^=epsilon; delta=RotateLeft(delta,7); \
      epsilon=beta << 7; alpha^=beta; gamma^=delta; alpha^=delta; \
      gamma^=epsilon; delta^=crypt_key[4*(i)+3]; beta^=crypt_key[4*(i)+1]; \
      alpha=RotateLeft(alpha,5); gamma=RotateLeft(gamma,22); \
      alpha^=crypt_key[4*(i)+0]; gamma^=crypt_key[4*(i)+2]; \
    }

Referenced by EncipherSerpentBlock().

#define EncipherSbox0 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=delta; delta|=alpha; alpha^=epsilon; epsilon^=gamma; \
      epsilon=(~epsilon); delta^=beta; beta&=alpha; beta^=epsilon; \
      gamma^=alpha; alpha^=delta; epsilon|=alpha; alpha^=gamma; \
      gamma&=beta; delta^=gamma; beta=(~beta); gamma^=epsilon; beta^=gamma; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox1 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=beta; beta^=alpha; alpha^=delta; delta=~delta; \
      epsilon&=beta; alpha|=beta; delta^=gamma; alpha^=delta; \
      beta^=delta; delta^=epsilon; beta|=epsilon; epsilon^=gamma; \
      gamma&=alpha; gamma^=beta; beta|=alpha; alpha=(~alpha); \
      alpha^=gamma; epsilon^=beta; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox2 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      delta=(~delta); beta^=alpha; epsilon=alpha; alpha&=gamma; \
      alpha^=delta; delta|=epsilon; gamma^=beta; delta^=beta; \
      beta&=alpha; alpha^=gamma; gamma&=delta; delta|=beta; \
      alpha=(~alpha); delta^=alpha; epsilon^=alpha; alpha^=gamma; beta|=gamma; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox3 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=beta; beta^=delta; delta|=alpha; epsilon&=alpha; \
      alpha^=gamma; gamma^=beta; beta&=delta; gamma^=delta; \
      alpha|=epsilon; epsilon^=delta; beta^=alpha; alpha&=delta; \
      delta&=epsilon; delta^=gamma; epsilon|=beta; gamma&=beta; \
      epsilon^=delta; alpha^=delta; delta^=gamma; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox4 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=delta; delta&=alpha; alpha^=epsilon; delta^=gamma; \
      gamma|=epsilon; alpha^=beta; epsilon^=delta; gamma|=alpha; \
      gamma^=beta; beta&=alpha; beta^=epsilon; epsilon&=gamma; \
      gamma^=delta; epsilon^=alpha; delta|=beta; beta=(~beta); delta^=alpha; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox5 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=beta;  beta|=alpha; gamma^=beta; delta=(~delta); \
      epsilon^=alpha; alpha^=gamma; beta&=epsilon; epsilon|=delta; \
      epsilon^=alpha; alpha&=delta; beta^=delta; delta^=gamma; \
      alpha^=beta; gamma&=epsilon; beta^=gamma; gamma&=alpha; delta^=gamma; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox6 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      epsilon=beta;  delta^=alpha;  beta^=gamma;  gamma^=alpha; \
      alpha&=delta; beta|=delta; epsilon=(~epsilon); alpha^=beta; \
      beta^=gamma; delta^=epsilon; epsilon^=alpha; gamma&=alpha; \
      epsilon^=beta; gamma^=delta; delta&=beta; delta^=alpha; beta^=gamma; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

#define EncipherSbox7 | ( |  | alpha,
|  |  | beta,
|  |  | gamma,
|  |  | delta,
|  |  | epsilon |
| ) |  |  |

Value:

    { \
      beta=(~beta); epsilon=beta; alpha=(~alpha); beta&=gamma; \
      beta^=delta; delta|=epsilon; epsilon^=gamma; gamma^=delta; \
      delta^=alpha; alpha|=beta;  gamma&=alpha; alpha^=epsilon; \
      epsilon^=delta; delta&=alpha; epsilon^=beta; gamma^=epsilon; \
      delta^=beta; epsilon|=alpha; epsilon^=beta; \
    }

Referenced by EncipherSerpentBlock(), and SetSerpentKey().

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

Value:

    { \
      alpha=crypt_key[i]; beta=crypt_key[(i)+1]; \
      gamma=crypt_key[(i)+2]; delta=crypt_key[(i)+3]; \
    }

Referenced by SetSerpentKey().

#define MaximumSerpentKeyLength 32

Referenced by SetSerpentKey().

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

Value:

    { \
      alpha^=crypt_key[4*(i)+0]; beta^=crypt_key[4*(i)+1]; \
      gamma^=crypt_key[4*(i)+2]; delta^=crypt_key[4*(i)+3]; \
    }

Referenced by DecipherSerpentBlock(), and EncipherSerpentBlock().

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

Value:

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

Referenced by DecipherSerpentBlock(), EncipherSerpentBlock(), and SetSerpentKey().

#define SerpentBlocksize 32

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

Value:

    { \
      crypt_key[i]=alpha; crypt_key[(i)+1]=beta; \
      crypt_key[(i)+2]=gamma; crypt_key[(i)+3]=delta; \
    }

Referenced by SetSerpentKey().

#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 DecipherSerpentBlock(), and EncipherSerpentBlock().

## Function Documentation

WizardExport SerpentInfo* AcquireSerpentInfo | ( | void |  | ) |

References AcquireWizardMemory(), _SerpentInfo::blocksize, CipherError, MemoryError, ResetWizardMemory(), _SerpentInfo::signature, ThrowWizardFatalError, and _SerpentInfo::timestamp.

Referenced by AcquireCipherInfo().

WizardExport void DecipherSerpentBlock | ( | SerpentInfo * | serpent_info,
|  | const unsigned char * | ciphertext,
|  | unsigned char * | plaintext |
| ) |  |  |

References _SerpentInfo::crypt_key, DecipherK, DecipherSbox0, DecipherSbox1, DecipherSbox2, DecipherSbox3, DecipherSbox4, DecipherSbox5, DecipherSbox6, DecipherSbox7, MixKey, Read32Bits, and Write32Bits.

Referenced by AcquireCipherInfo().

WizardExport SerpentInfo* DestroySerpentInfo | ( | SerpentInfo * | serpent_info | ) |

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

Referenced by DestroyCipherInfo().

WizardExport void EncipherSerpentBlock | ( | SerpentInfo * | serpent_info,
|  | const unsigned char * | plaintext,
|  | unsigned char * | ciphertext |
| ) |  |  |

References _SerpentInfo::crypt_key, EncipherK, EncipherSbox0, EncipherSbox1, EncipherSbox2, EncipherSbox3, EncipherSbox4, EncipherSbox5, EncipherSbox6, EncipherSbox7, MixKey, Read32Bits, and Write32Bits.

Referenced by AcquireCipherInfo().

WizardExport unsigned int GetSerpentBlocksize | ( | const SerpentInfo * | serpent_info | ) |

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

Referenced by AcquireCipherInfo().

static unsigned int RotateLeft | ( | unsigned int | x,
|  | unsigned int | n |
| ) |  |  | `[inline, static]`

References Trunc32().

static unsigned int RotateRight | ( | unsigned int | x,
|  | unsigned int | n |
| ) |  |  | `[inline, static]`

References Trunc32().

WizardExport void SetSerpentKey | ( | SerpentInfo * | serpent_info,
|  | const StringInfo * | key |
| ) |  |  |

References ChurnKey, CipherDomain, _SerpentInfo::crypt_key, EncipherSbox0, EncipherSbox1, EncipherSbox2, EncipherSbox3, EncipherSbox4, EncipherSbox5, EncipherSbox6, EncipherSbox7, GetStringInfoDatum(), GetStringInfoLength(), GetWizardModule, LoadKey, LogWizardEvent(), MaximumSerpentKeyLength, Min, Read32Bits, _SerpentInfo::signature, StoreKey, TraceEvent, WizardAssert, and WizardSignature.

Referenced by SetCipherKey().

static unsigned int Trunc32 | ( | unsigned int | x | ) | `[inline, static]`

Referenced by RotateLeft(), and RotateRight().