java.lang.Object
com.southernstorm.noise.crypto.chacha20.ChaChaCore
Implementation of the ChaCha20 core hash transformation.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intFirst state word, part of the ChaCha sigma constant.private static final intSecond state word, part of the ChaCha sigma constant.private static final intThird state word, part of the ChaCha sigma constant.private static final intFourth state word, part of the ChaCha sigma constant.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static intchar4(char c1, char c2, char c3, char c4) Packs four characters into one state word.private static intfromLittleEndian(byte[] key, int offset) Reads four bytes as a little endian word.static voidhash(int[] output, int[] input) Hashes an input block with ChaCha20.static voidinitIV(int[] output, long iv) Initializes the 64-bit initialization vector in a ChaCha20 block.static voidinitKey256(int[] output, byte[] key, int offset) Initializes a ChaCha20 block with a 256-bit key.private static intleftRotate12(int v) Rotates the word left by 12 bits.private static intleftRotate16(int v) Rotates the word left by 16 bits.private static intleftRotate7(int v) Rotates the word left by 7 bits.private static intleftRotate8(int v) Rotates the word left by 8 bits.private static voidquarterRound(int[] v, int a, int b, int c, int d) Applies the ChaCha quarter round to four state words.static voidxorBlock(byte[] input, int inputOffset, byte[] output, int outputOffset, int length, int[] block) XOR's the output of ChaCha20 with a byte buffer.
Field Details
INIT0
private static final int INIT0First state word, part of the ChaCha sigma constant.INIT1
private static final int INIT1Second state word, part of the ChaCha sigma constant.INIT2
private static final int INIT2Third state word, part of the ChaCha sigma constant.INIT3
private static final int INIT3Fourth 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 characterc2- the second characterc3- the third characterc4- 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 readoffset- 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 byinitKey256(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 wordsa- index of the first wordb- index of the second wordc- index of the third wordd- 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.