APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.sun.crypto.provider

pack-priv abstract Class ChaCha20Cipher

extends CipherSpi
Class Inheritance
Known Direct Subclasses
com.sun.crypto.provider.ChaCha20Cipher.ChaCha20Only, com.sun.crypto.provider.ChaCha20Cipher.ChaCha20Poly1305
Imports
java.io.IOException, java.lang.invoke.MethodHandles, .VarHandle, java.nio.ByteBuffer, .ByteOrder, java.security.*, java.security.spec.AlgorithmParameterSpec, java.util.Arrays, .Objects, javax.crypto.*, javax.crypto.spec.ChaCha20ParameterSpec, .IvParameterSpec, .SecretKeySpec, jdk.internal.vm.annotation.ForceInline, .IntrinsicCandidate, sun.security.util.DerValue

Implementation of the ChaCha20 cipher, as described in RFC 7539.
Since
11

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
pack-priv static interface
ChaCha20Cipher.ChaChaEngine

Interface for the underlying processing engines for ChaCha20

private class
private class
private class

Field Summary

Modifier and TypeField and Description
private boolean
private long
private static final VarHandle
private static final VarHandle
private static final VarHandle
protected String
private Poly1305
private long
private long
private int
private ChaCha20Cipher.ChaChaEngine
private long
private long
private boolean
private byte[]
private final byte[]
private int
private int
private static final int
private static final int
private static final int
private final byte[]
private static final long
protected int
private static final int
private static final int
private byte[]
private static final byte[]
private final int[]
private static final int
private static final int
private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
protected
ChaCha20Cipher()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
private void
authFinalizeData(byte[]
an array containing any remaining data to process.
data
,
int
the offset into the data.
dataOff
,
int
the length of the data to process.
length
,
byte[]
the array to write the resulting tag into
out
,
int
the offset to begin writing the data.
outOff
)

Finalize the data and return the tag.

private void
authPad16(long
the starting length to be padded.
dataLen
)

Based on a given length of data, make the authenticator process zero bytes that will pad the length out to a multiple of 16.

private int

Returns:

the number of bytes processed by this method.
authUpdate
(byte[]
the data to stir into the authenticator.
data
,
int
the offset into the data.
offset
,
int
the length of data to add to the authenticator.
length
)

Update the authenticator state with data.

private void
authWriteLengths(long
the length of the AAD.
aLen
,
long
the length of the application data.
dLen
,
byte[]
the buffer to write the two lengths into.
buf
)

Write the two 64-bit little-endian length fields into an array for processing by the poly1305 authenticator.

private int
bufferCrypt(ByteBuffer input, ByteBuffer output, boolean isUpdate)

Hides javax.crypto.CipherSpi.bufferCrypt.

Implementation for encryption using ByteBuffers.

private static int
chaCha20Block(int[] initState, long counter, byte[] result)

private void
chaCha20Transform(byte[]
the array of bytes for the input
in
,
int
the offset into the input array to start the transform
inOff
,
int
the length of the data to perform the transform on.
inLen
,
byte[]
the output array. It must be large enough to hold the resulting data
out
,
int
the offset into the output array to place the resulting data.
outOff
)

Perform the ChaCha20 transform.

private void
checkKeyAndNonce(byte[]
the byte encoding for the newly provided key
newKeyBytes
,
byte[]
the new nonce to be used with this initialization
newNonce
)

Check the key and nonce bytes to make sure that they do not repeat across reinitialization.

private static byte[]

Returns:

a 12-byte array containing the random nonce.
createRandomNonce
(SecureRandom
a SecureRandom object. If null is provided a new SecureRandom object will be instantiated.
random
)

Create a random 12-byte nonce.

protected byte[]

Returns:

the resulting plaintext or ciphertext bytes (depending on the operation type)
engineDoFinal
(byte[]
the plaintext or ciphertext input bytes (depending on the operation type).
in
,
int
the offset into the input array
inOfs
,
int
the length of the data to use for the update operation.
inLen
)

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Complete the currently running operation using any final data provided by the caller.

protected int

Returns:

the length in bytes of the data written into the out buffer.
engineDoFinal
(byte[]
the plaintext or ciphertext input bytes (depending on the operation type).
in
,
int
the offset into the input array
inOfs
,
int
the length of the data to use for the update operation.
inLen
,
byte[]
the byte array that will hold the resulting data. The array must be large enough to hold the resulting data.
out
,
int
the offset for the out buffer to begin writing the resulting data.
outOfs
)

Implements abstract javax.crypto.CipherSpi.engineDoFinal.

Complete the currently running operation using any final data provided by the caller.

protected int

Returns:

the resulting plaintext or ciphertext bytes.
engineDoFinal
(ByteBuffer
the plaintext or ciphertext input bytebuffer.
input
,
ByteBuffer
ByteBuffer that will hold the resulting data. This must be large enough to hold the resulting data.
output
)

Overrides javax.crypto.CipherSpi.engineDoFinal.

Complete the currently running operation using any final data provided by the caller.

protected int

Returns:

This method always returns 0. See the JCE Specification.
engineGetBlockSize
()

Implements abstract javax.crypto.CipherSpi.engineGetBlockSize.

Returns the block size.

protected byte[]

Returns:

the nonce bytes. For ChaCha20 this will be a 12-byte value.
engineGetIV
()

Implements abstract javax.crypto.CipherSpi.engineGetIV.

Get the nonce value used.

protected int

Returns:

the length of the key in bits
engineGetKeySize
(Key
the key to be evaluated
key
)

Overrides javax.crypto.CipherSpi.engineGetKeySize.

Get the length of a provided key in bits.

protected int

Returns:

the output length in bytes.
engineGetOutputSize
(int
the length in bytes of the input
inputLen
)

Implements abstract javax.crypto.CipherSpi.engineGetOutputSize.

Get the output size required to hold the result of the next update or doFinal operation.

protected AlgorithmParameters

Returns:

a null value if the ChaCha20 cipher is used (mode is MODE_NONE), or a ChaCha20Poly1305Parameters object containing the nonce if the mode is MODE_AEAD.
engineGetParameters
()

Implements abstract javax.crypto.CipherSpi.engineGetParameters.

Get the algorithm parameters for this cipher.

protected void
engineInit(int
the type of operation to do. This value may not be Cipher.DECRYPT_MODE or Cipher.UNWRAP_MODE mode because it must generate random parameters like the nonce.
opmode
,
Key
a 256-bit key suitable for ChaCha20
key
,
SecureRandom
a SecureRandom implementation used to create the random nonce. If null is used for the random object, then an internal secure random source will be used to create the nonce.
random
)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initialize the engine using a key and secure random implementation.

protected void
engineInit(int
the type of operation to do. This value must be either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
opmode
,
Key
a 256-bit key suitable for ChaCha20
key
,
AlgorithmParameterSpec
a ChaCha20ParameterSpec that will provide the nonce and initial block counter value.
params
,
SecureRandom
a SecureRandom implementation, this parameter is not used in this form of the initializer.
random
)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initialize the engine using a key and secure random implementation.

protected void
engineInit(int
the type of operation to do. This value must be either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
opmode
,
Key
a 256-bit key suitable for ChaCha20
key
,
AlgorithmParameters
a null value if the algorithm is ChaCha20, or the appropriate AlgorithmParameters object containing the nonce information if the algorithm is ChaCha20-Poly1305.
params
,
SecureRandom
a SecureRandom implementation, may be null.
random
)

Implements abstract javax.crypto.CipherSpi.engineInit.

Initialize the engine using the AlgorithmParameter initialization format.

protected void
engineSetMode(String
The mode value
mode
)

Implements abstract javax.crypto.CipherSpi.engineSetMode.

Set the mode of operation.

protected void
engineSetPadding(String
The padding type. The only allowed value is NoPadding case insensitive.
padding
)

Implements abstract javax.crypto.CipherSpi.engineSetPadding.

Set the padding scheme.

protected Key

Returns:

the unwrapped key as a Key object.
engineUnwrap
(byte[]
the key to unwrap.
wrappedKey
,
String
the algorithm associated with the wrapped key
algorithm
,
int
the type of the wrapped key. This is one of SECRET_KEY, PRIVATE_KEY, or PUBLIC_KEY.
type
)

Overrides javax.crypto.CipherSpi.engineUnwrap.

Unwrap a Key using this Cipher's current encryption parameters.

protected byte[]

Returns:

the resulting plaintext or ciphertext bytes (depending on the operation type)
engineUpdate
(byte[]
the plaintext or ciphertext input bytes (depending on the operation type).
in
,
int
the offset into the input array
inOfs
,
int
the length of the data to use for the update operation.
inLen
)

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Update the currently running operation with additional data

protected int

Returns:

the length in bytes of the data written into the out buffer.
engineUpdate
(byte[]
the plaintext or ciphertext input bytes (depending on the operation type).
in
,
int
the offset into the input array
inOfs
,
int
the length of the data to use for the update operation.
inLen
,
byte[]
the byte array that will hold the resulting data. The array must be large enough to hold the resulting data.
out
,
int
the offset for the out buffer to begin writing the resulting data.
outOfs
)

Implements abstract javax.crypto.CipherSpi.engineUpdate.

Update the currently running operation with additional data

protected int

Returns:

the length in bytes of the data written into the output buffer.
engineUpdate
(ByteBuffer
the plaintext or ciphertext ByteBuffer
input
,
ByteBuffer
ByteBuffer that will hold the resulting data. This must be large enough to hold the resulting data.
output
)

Overrides javax.crypto.CipherSpi.engineUpdate.

Update the currently running operation with additional data

protected void
engineUpdateAAD(byte[]
the byte array containing the authentication data.
src
,
int
the starting offset in the buffer to update.
offset
,
int
the amount of authentication data to update.
len
)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Update additional authenticated data (AAD).

protected void
engineUpdateAAD(ByteBuffer
the ByteBuffer containing the authentication data.
src
)

Overrides javax.crypto.CipherSpi.engineUpdateAAD.

Update additional authenticated data (AAD).

protected byte[]

Returns:

a byte array consisting of the wrapped key.
engineWrap
(Key
the key to wrap. The data that will be encrypted will be the provided Key in its encoded form.
key
)

Overrides javax.crypto.CipherSpi.engineWrap.

Wrap a Key using this Cipher's current encryption parameters.

private static byte[]

Returns:

the key bytes
getEncodedKey
(Key
the Key object used for this Cipher
key
)

Return the encoded key as a byte array

private static int

Returns:

the number of keystream bytes generated. In a pure Java method this will always be 64 bytes, but intrinsics that make use of AVX2 or AVX512 registers may generate multiple blocks of keystream in a single call and therefore may be a larger multiple of 64.
implChaCha20Block
(int[]
the starting state using the current counter value.
initState
,
byte[]
the array that will hold the result of the ChaCha20 block function.
result
)

Perform a full 20-round ChaCha20 transform on the initial state.

private void
init(int
the type of operation to do. This value must be either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
opmode
,
Key
a 256-bit key suitable for ChaCha20
key
,
byte[]
the new nonce value for this initialization.
newNonce
)

Perform additional initialization actions based on the key and operation type.

private void
initAuthenticator()

Perform initialization steps for the authenticator

private void
resetStartState()

reset the Cipher's state to the values it had after the initial init() call.

private void
setInitialState()

Set the initial state.

private static void
xor(byte[] in1, int off1, byte[] in2, int off2, byte[] out, int outOff, int len)