Class RC6Cipher

java.lang.Object
edu.boisestate.lowry.crypto.RC6Cipher
All Implemented Interfaces:
BlockCipher

public class RC6Cipher extends Object implements BlockCipher
An implementation of the RC6-32/20/b block cipher. This implementation is configured for word size w = 32 and r = 20 rounds. It also supports variable secret key lengths 'b' (16, 24, or 32 bytes), corresponding to requirements for the AES-128, AES-192, and AES-256.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
    The block size in bytes.
    private final KeySize
    The key size, corresponding to the RC6 'b' parameter.
    private static final int
    The number of registers.
    private static final int
    The number of rounds for encryption and decryption.
    private int[]
    The round keys S[0, ..., 2r + 3].
  • Constructor Summary

    Constructors
    Constructor
    Description
    RC6Cipher(KeySize keySize)
    Creates an instance of this cipher, configured for the given key size.
  • Method Summary

    Modifier and Type
    Method
    Description
    private int[]
    bytesToRegisters(byte[] bytes)
    Converts an array of bytes to an array of 32-bit registers (words).
    byte[]
    decipher(byte[] ciphertext, byte[] key)
    Deciphers a fixed-length block of ciphertext.
    byte[]
    encipher(byte[] plaintext, byte[] key)
    Enciphers a fixed-length block of plaintext.
    int
    Returns the fixed block size this cipher operates on.
    void
    initKey(byte[] key)
    Preemptively calls the key schedule with the given key for the purpose of performing bulk encryption.
    private int[]
    keySchedule(byte[] key)
    Derives 2r + 4 round keys from the given key, loaded into an array of 32-bit registers.
    private byte[]
    registersToBytes(int[] registers)
    Converts an array of registers to an array of bytes in a little endian fashion.
    void
    Resets the stored key to nothing.
    private void
    rotateRegistersLeft(int[] registers)
    Rotate/permute the registers to the left.
    private void
    rotateRegistersRight(int[] registers)
    Rotate/permute the registers to the left.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BLOCK_SIZE_BYTES

      private static final int BLOCK_SIZE_BYTES
      The block size in bytes. In RC6, the block size is defined as four words (4w). For w = 32 bits (4 bytes), the block size is 16 bytes (128 bits).
      See Also:
    • NUM_REGISTERS

      private static final int NUM_REGISTERS
      The number of registers.
      See Also:
    • NUM_ROUNDS

      private static final int NUM_ROUNDS
      The number of rounds for encryption and decryption.
      See Also:
    • keySize

      private final KeySize keySize
      The key size, corresponding to the RC6 'b' parameter.
    • roundKeys

      private int[] roundKeys
      The round keys S[0, ..., 2r + 3].
  • Constructor Details

    • RC6Cipher

      public RC6Cipher(KeySize keySize)
      Creates an instance of this cipher, configured for the given key size.
      Parameters:
      keySize - The key size for this cipher.
  • Method Details

    • encipher

      public byte[] encipher(byte[] plaintext, byte[] key)
      Description copied from interface: BlockCipher
      Enciphers a fixed-length block of plaintext.
      Specified by:
      encipher in interface BlockCipher
      Parameters:
      plaintext - A block of data with length equivalent to getBlockSize().
      key - The secret key for enciphering.
      Returns:
      The resulting enciphered block.
    • decipher

      public byte[] decipher(byte[] ciphertext, byte[] key)
      Description copied from interface: BlockCipher
      Deciphers a fixed-length block of ciphertext.
      Specified by:
      decipher in interface BlockCipher
      Parameters:
      ciphertext - A block of encrypted data with length equivalent to getBlockSize().
      key - The secret key for deciphering.
      Returns:
      The resulting deciphered block.
    • getBlockSize

      public int getBlockSize()
      Description copied from interface: BlockCipher
      Returns the fixed block size this cipher operates on.
      Specified by:
      getBlockSize in interface BlockCipher
      Returns:
      The block size in bytes.
    • initKey

      public void initKey(byte[] key)
      Preemptively calls the key schedule with the given key for the purpose of performing bulk encryption. A call to this will cause they key parameter for encipher() and decipher() to be ignored.
      Parameters:
      key - The key to initialize.
    • resetKey

      public void resetKey()
      Resets the stored key to nothing. A call to this will cause the key parameter for encipher() and decipher() to be usable following a call to initKey().
    • bytesToRegisters

      private int[] bytesToRegisters(byte[] bytes)
      Converts an array of bytes to an array of 32-bit registers (words). Bytes are placed in a little-endian fashion. The length of bytes is assumed to be a multiple of four.
      Parameters:
      bytes - The array of bytes.
      Returns:
      an array of 32-bit registers.
      Implementation Note:
      We use an int array because ints are always 32 bits in Java, exactly what is needed for the w = 32 parameter. Loading the registers little endian corresponds to, for example, how the placement of bytes into A, B, C, D is described, and the same for the array L in the key schedule.
    • registersToBytes

      private byte[] registersToBytes(int[] registers)
      Converts an array of registers to an array of bytes in a little endian fashion.
      Parameters:
      registers - The array of registers.
      Returns:
      an array of bytes.
    • rotateRegistersLeft

      private void rotateRegistersLeft(int[] registers)
      Rotate/permute the registers to the left. This is the same operation as the parallel assignment step (A, B, C, D) = (B, C, D, A) for enciphering.
      Parameters:
      registers - The registers.
    • rotateRegistersRight

      private void rotateRegistersRight(int[] registers)
      Rotate/permute the registers to the left. This is the same operation as the parallel assignment step (A, B, C, D) = (D, A, B, C) for deciphering.
      Parameters:
      registers - The registers.
    • keySchedule

      private int[] keySchedule(byte[] key)
      Derives 2r + 4 round keys from the given key, loaded into an array of 32-bit registers.
      Parameters:
      key - The private key.
      Returns:
      An array of the round keys as registers.