org.bouncycastle.crypto.engines
Class SerpentEngine

java.lang.Object
  extended by org.bouncycastle.crypto.engines.SerpentEngine
All Implemented Interfaces:
BlockCipher

public class SerpentEngine
extends java.lang.Object
implements BlockCipher

Serpent is a 128-bit 32-round block cipher with variable key lengths, including 128, 192 and 256 bit keys conjectured to be at least as secure as three-key triple-DES.

Serpent was designed by Ross Anderson, Eli Biham and Lars Knudsen as a candidate algorithm for the NIST AES Quest.>

For full details see the The Serpent home page


Field Summary
private static int BLOCK_SIZE
           
private  boolean encrypting
           
(package private) static int PHI
           
(package private) static int ROUNDS
           
private  int[] wKey
           
private  int X0
           
private  int X1
           
private  int X2
           
private  int X3
           
 
Constructor Summary
SerpentEngine()
           
 
Method Summary
private  int bytesToWord(byte[] src, int srcOff)
           
private  void decryptBlock(byte[] in, int inOff, byte[] out, int outOff)
          Decrypt one block of ciphertext.
private  void encryptBlock(byte[] in, int inOff, byte[] out, int outOff)
          Encrypt one block of plaintext.
 java.lang.String getAlgorithmName()
          Return the name of the algorithm the cipher implements.
 int getBlockSize()
          Return the block size for this cipher (in bytes).
private  void ib0(int a, int b, int c, int d)
          InvSO - {13, 3,11, 0,10, 6, 5,12, 1,14, 4, 7,15, 9, 8, 2 } - 15 terms.
private  void ib1(int a, int b, int c, int d)
          InvS1 - { 5, 8, 2,14,15, 6,12, 3,11, 4, 7, 9, 1,13,10, 0 } - 14 steps.
private  void ib2(int a, int b, int c, int d)
          InvS2 - {12, 9,15, 4,11,14, 1, 2, 0, 3, 6,13, 5, 8,10, 7 } - 16 steps.
private  void ib3(int a, int b, int c, int d)
          InvS3 - { 0, 9,10, 7,11,14, 6,13, 3, 5,12, 2, 4, 8,15, 1 } - 15 terms
private  void ib4(int a, int b, int c, int d)
          InvS4 - { 5, 0, 8, 3,10, 9, 7,14, 2,12,11, 6, 4,15,13, 1 } - 15 terms.
private  void ib5(int a, int b, int c, int d)
          InvS5 - { 8,15, 2, 9, 4, 1,13,14,11, 6, 5, 3, 7,12,10, 0 } - 16 terms.
private  void ib6(int a, int b, int c, int d)
          InvS6 - {15,10, 1,13, 5, 3, 6, 0, 4, 9,14, 7, 2,12, 8,11 } - 15 terms.
private  void ib7(int a, int b, int c, int d)
          InvS7 - { 3, 0, 6,13, 9,14,15, 8, 5,12,11, 7,10, 1, 4, 2 } - 17 terms.
 void init(boolean encrypting, CipherParameters params)
          initialise a Serpent cipher.
private  void inverseLT()
          Apply the inverse of the linear transformation to the register set.
private  void LT()
          Apply the linear transformation to the register set.
private  int[] makeWorkingKey(byte[] key)
          Expand a user-supplied key material into a session key.
 int processBlock(byte[] in, int inOff, byte[] out, int outOff)
          Process one block of input from the array in and write it to the out array.
 void reset()
          Reset the cipher.
private  int rotateLeft(int x, int bits)
           
private  int rotateRight(int x, int bits)
           
private  void sb0(int a, int b, int c, int d)
          S0 - { 3, 8,15, 1,10, 6, 5,11,14,13, 4, 2, 7, 0, 9,12 } - 15 terms.
private  void sb1(int a, int b, int c, int d)
          S1 - {15,12, 2, 7, 9, 0, 5,10, 1,11,14, 8, 6,13, 3, 4 } - 14 terms.
private  void sb2(int a, int b, int c, int d)
          S2 - { 8, 6, 7, 9, 3,12,10,15,13, 1,14, 4, 0,11, 5, 2 } - 16 terms.
private  void sb3(int a, int b, int c, int d)
          S3 - { 0,15,11, 8,12, 9, 6, 3,13, 1, 2, 4,10, 7, 5,14 } - 16 terms.
private  void sb4(int a, int b, int c, int d)
          S4 - { 1,15, 8, 3,12, 0,11, 6, 2, 5, 4,10, 9,14, 7,13 } - 15 terms.
private  void sb5(int a, int b, int c, int d)
          S5 - {15, 5, 2,11, 4,10, 9,12, 0, 3,14, 8,13, 6, 7, 1 } - 16 terms.
private  void sb6(int a, int b, int c, int d)
          S6 - { 7, 2,12, 5, 8, 4, 6,11,14, 9, 1,15,13, 3,10, 0 } - 15 terms.
private  void sb7(int a, int b, int c, int d)
          S7 - { 1,13,15, 0,14, 8, 2,11, 7, 4,12,10, 9, 3, 5, 6 } - 16 terms.
private  void wordToBytes(int word, byte[] dst, int dstOff)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

private static final int BLOCK_SIZE
See Also:
Constant Field Values

ROUNDS

static final int ROUNDS
See Also:
Constant Field Values

PHI

static final int PHI
See Also:
Constant Field Values

encrypting

private boolean encrypting

wKey

private int[] wKey

X0

private int X0

X1

private int X1

X2

private int X2

X3

private int X3
Constructor Detail

SerpentEngine

public SerpentEngine()
Method Detail

init

public void init(boolean encrypting,
                 CipherParameters params)
initialise a Serpent cipher.

Specified by:
init in interface BlockCipher
Parameters:
encrypting - whether or not we are for encryption.
params - the parameters required to set up the cipher.
Throws:
java.lang.IllegalArgumentException - if the params argument is inappropriate.

getAlgorithmName

public java.lang.String getAlgorithmName()
Description copied from interface: BlockCipher
Return the name of the algorithm the cipher implements.

Specified by:
getAlgorithmName in interface BlockCipher
Returns:
the name of the algorithm the cipher implements.

getBlockSize

public int getBlockSize()
Description copied from interface: BlockCipher
Return the block size for this cipher (in bytes).

Specified by:
getBlockSize in interface BlockCipher
Returns:
the block size for this cipher in bytes.

processBlock

public final int processBlock(byte[] in,
                              int inOff,
                              byte[] out,
                              int outOff)
Process one block of input from the array in and write it to the out array.

Specified by:
processBlock in interface BlockCipher
Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.
Throws:
DataLengthException - if there isn't enough data in in, or space in out.
java.lang.IllegalStateException - if the cipher isn't initialised.

reset

public void reset()
Description copied from interface: BlockCipher
Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).

Specified by:
reset in interface BlockCipher

makeWorkingKey

private int[] makeWorkingKey(byte[] key)
                      throws java.lang.IllegalArgumentException
Expand a user-supplied key material into a session key.

Parameters:
key - The user-key bytes (multiples of 4) to use.
Throws:
java.lang.IllegalArgumentException

rotateLeft

private int rotateLeft(int x,
                       int bits)

rotateRight

private int rotateRight(int x,
                        int bits)

bytesToWord

private int bytesToWord(byte[] src,
                        int srcOff)

wordToBytes

private void wordToBytes(int word,
                         byte[] dst,
                         int dstOff)

encryptBlock

private void encryptBlock(byte[] in,
                          int inOff,
                          byte[] out,
                          int outOff)
Encrypt one block of plaintext.

Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.

decryptBlock

private void decryptBlock(byte[] in,
                          int inOff,
                          byte[] out,
                          int outOff)
Decrypt one block of ciphertext.

Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.

sb0

private void sb0(int a,
                 int b,
                 int c,
                 int d)
S0 - { 3, 8,15, 1,10, 6, 5,11,14,13, 4, 2, 7, 0, 9,12 } - 15 terms.


ib0

private void ib0(int a,
                 int b,
                 int c,
                 int d)
InvSO - {13, 3,11, 0,10, 6, 5,12, 1,14, 4, 7,15, 9, 8, 2 } - 15 terms.


sb1

private void sb1(int a,
                 int b,
                 int c,
                 int d)
S1 - {15,12, 2, 7, 9, 0, 5,10, 1,11,14, 8, 6,13, 3, 4 } - 14 terms.


ib1

private void ib1(int a,
                 int b,
                 int c,
                 int d)
InvS1 - { 5, 8, 2,14,15, 6,12, 3,11, 4, 7, 9, 1,13,10, 0 } - 14 steps.


sb2

private void sb2(int a,
                 int b,
                 int c,
                 int d)
S2 - { 8, 6, 7, 9, 3,12,10,15,13, 1,14, 4, 0,11, 5, 2 } - 16 terms.


ib2

private void ib2(int a,
                 int b,
                 int c,
                 int d)
InvS2 - {12, 9,15, 4,11,14, 1, 2, 0, 3, 6,13, 5, 8,10, 7 } - 16 steps.


sb3

private void sb3(int a,
                 int b,
                 int c,
                 int d)
S3 - { 0,15,11, 8,12, 9, 6, 3,13, 1, 2, 4,10, 7, 5,14 } - 16 terms.


ib3

private void ib3(int a,
                 int b,
                 int c,
                 int d)
InvS3 - { 0, 9,10, 7,11,14, 6,13, 3, 5,12, 2, 4, 8,15, 1 } - 15 terms


sb4

private void sb4(int a,
                 int b,
                 int c,
                 int d)
S4 - { 1,15, 8, 3,12, 0,11, 6, 2, 5, 4,10, 9,14, 7,13 } - 15 terms.


ib4

private void ib4(int a,
                 int b,
                 int c,
                 int d)
InvS4 - { 5, 0, 8, 3,10, 9, 7,14, 2,12,11, 6, 4,15,13, 1 } - 15 terms.


sb5

private void sb5(int a,
                 int b,
                 int c,
                 int d)
S5 - {15, 5, 2,11, 4,10, 9,12, 0, 3,14, 8,13, 6, 7, 1 } - 16 terms.


ib5

private void ib5(int a,
                 int b,
                 int c,
                 int d)
InvS5 - { 8,15, 2, 9, 4, 1,13,14,11, 6, 5, 3, 7,12,10, 0 } - 16 terms.


sb6

private void sb6(int a,
                 int b,
                 int c,
                 int d)
S6 - { 7, 2,12, 5, 8, 4, 6,11,14, 9, 1,15,13, 3,10, 0 } - 15 terms.


ib6

private void ib6(int a,
                 int b,
                 int c,
                 int d)
InvS6 - {15,10, 1,13, 5, 3, 6, 0, 4, 9,14, 7, 2,12, 8,11 } - 15 terms.


sb7

private void sb7(int a,
                 int b,
                 int c,
                 int d)
S7 - { 1,13,15, 0,14, 8, 2,11, 7, 4,12,10, 9, 3, 5, 6 } - 16 terms.


ib7

private void ib7(int a,
                 int b,
                 int c,
                 int d)
InvS7 - { 3, 0, 6,13, 9,14,15, 8, 5,12,11, 7,10, 1, 4, 2 } - 17 terms.


LT

private void LT()
Apply the linear transformation to the register set.


inverseLT

private void inverseLT()
Apply the inverse of the linear transformation to the register set.