java.lang.Object
com.southernstorm.noise.crypto.chacha20.ChaChaCore

public final class ChaChaCoreextends Object
Implementation of the ChaCha20 core hash transformation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
    First state word, part of the ChaCha sigma constant.
    private static final int
    Second state word, part of the ChaCha sigma constant.
    private static final int
    Third state word, part of the ChaCha sigma constant.
    private static final int
    Fourth state word, part of the ChaCha sigma constant.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Not called, the core class is never instantiated.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    char4(char c1, char c2, char c3, char c4)
    Packs four characters into one state word.
    private static int
    fromLittleEndian(byte[] key, int offset)
    Reads four bytes as a little endian word.
    static void
    hash(int[] output, int[] input)
    Hashes an input block with ChaCha20.
    static void
    initIV(int[] output, long iv)
    Initializes the 64-bit initialization vector in a ChaCha20 block.
    static void
    initKey256(int[] output, byte[] key, int offset)
    Initializes a ChaCha20 block with a 256-bit key.
    private static int
    leftRotate12(int v)
    Rotates the word left by 12 bits.
    private static int
    leftRotate16(int v)
    Rotates the word left by 16 bits.
    private static int
    leftRotate7(int v)
    Rotates the word left by 7 bits.
    private static int
    leftRotate8(int v)
    Rotates the word left by 8 bits.
    private static void
    quarterRound(int[] v, int a, int b, int c, int d)
    Applies the ChaCha quarter round to four state words.
    static void
    xorBlock(byte[] input, int inputOffset, byte[] output, int outputOffset, int length, int[] block)
    XOR's the output of ChaCha20 with a byte buffer.

    Methods inherited from class Object

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

    • INIT0

      private static final int INIT0
      First state word, part of the ChaCha sigma constant.
    • INIT1

      private static final int INIT1
      Second state word, part of the ChaCha sigma constant.
    • INIT2

      private static final int INIT2
      Third state word, part of the ChaCha sigma constant.
    • INIT3

      private static final int INIT3
      Fourth state word, part of the ChaCha sigma constant.
  • Constructor Details

    • ChaChaCore

      private ChaChaCore()
      Not called, the core class is never instantiated.
  • Method Details

    • hash

      public static void hash(int[] output, int[] input)
      Hashes an input block with ChaCha20.
      Parameters:
      output - The output block, which must contain at least 16 elements and must not overlap with the input.
      input - The input block, which must contain at least 16 elements.
    • char4

      private static int char4(char c1, char c2, char c3, char c4)
      Packs four characters into one state word.
      Parameters:
      c1 - the first character
      c2 - the second character
      c3 - the third character
      c4 - the fourth character
      Returns:
      the packed word
    • fromLittleEndian

      private static int fromLittleEndian(byte[] key, int offset)
      Reads four bytes as a little endian word.
      Parameters:
      key - the bytes to read
      offset - where to read from
      Returns:
      the word read
    • initKey256

      public static void initKey256(int[] output, byte[] key, int offset)
      Initializes a ChaCha20 block with a 256-bit key.
      Parameters:
      output - The output block, which must consist of at least 16 words.
      key - The buffer containing the key.
      offset - Offset of the key in the buffer.
    • initIV

      public static void initIV(int[] output, long iv)
      Initializes the 64-bit initialization vector in a ChaCha20 block.
      Parameters:
      output - The output block, which must consist of at least 16 words and must have been initialized by initKey256(int[], byte[], int) or initKey128().
      iv - The 64-bit initialization vector value. The counter portion of the output block is set to zero.
    • leftRotate16

      private static int leftRotate16(int v)
      Rotates the word left by 16 bits.
      Parameters:
      v - the word to rotate
      Returns:
      the rotated word
    • leftRotate12

      private static int leftRotate12(int v)
      Rotates the word left by 12 bits.
      Parameters:
      v - the word to rotate
      Returns:
      the rotated word
    • leftRotate8

      private static int leftRotate8(int v)
      Rotates the word left by 8 bits.
      Parameters:
      v - the word to rotate
      Returns:
      the rotated word
    • leftRotate7

      private static int leftRotate7(int v)
      Rotates the word left by 7 bits.
      Parameters:
      v - the word to rotate
      Returns:
      the rotated word
    • quarterRound

      private static void quarterRound(int[] v, int a, int b, int c, int d)
      Applies the ChaCha quarter round to four state words.
      Parameters:
      v - the state words
      a - index of the first word
      b - index of the second word
      c - index of the third word
      d - index of the fourth word
    • xorBlock

      public static void xorBlock(byte[] input, int inputOffset, byte[] output, int outputOffset, int length, int[] block)
      XOR's the output of ChaCha20 with a byte buffer.
      Parameters:
      input - The input byte buffer.
      inputOffset - The offset of the first input byte.
      output - The output byte buffer (can be the same as the input).
      outputOffset - The offset of the first output byte.
      length - The number of bytes to XOR between 1 and 64.
      block - The ChaCha20 output block.