java.lang.Object
net.i2p.crypto.ChaCha20

public final class ChaCha20extends Object
ChaCha20, wrapper around Noise ChaChaCore. RFC 7539.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final ThreadLocal<int[][]>
    Per-thread scratch pair: [0] is the input state block, [1] is the keystream block.
    private static final int
    Number of 32-bit words in a ChaCha20 state block.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Utility class, not intended for instantiation.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    static void
    decrypt(byte[] key, byte[] iv, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)
    Encrypt from ciphertext to plaintext.
    static void
    decrypt(byte[] key, byte[] iv, int ivOffset, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)
    Encrypt from ciphertext to plaintext.
    static void
    encrypt(byte[] key, byte[] iv, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
    Encrypt from plaintext to ciphertext.
    static void
    encrypt(byte[] key, byte[] iv, int ivOffset, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
    Encrypt from plaintext to ciphertext.

    Methods inherited from class Object

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

    • STATE_WORDS

      private static final int STATE_WORDS
      Number of 32-bit words in a ChaCha20 state block.
      See Also:
    • _SCRATCH

      private static final ThreadLocal<int[][]> _SCRATCH
      Per-thread scratch pair: [0] is the input state block, [1] is the keystream block.

      Both arrays are strictly internal - they never escape encrypt(byte[], byte[], int, byte[], int, byte[], int, int), so no caller can retain or observe them, and a thread-confined instance is safe. Reentry is impossible: the only callees are ChaChaCore statics and DataHelper.fromLongLE(byte[], int, int), all of which are leaf code with no callbacks back into this class.

      This saves two 128-byte allocations per call; SSU2 protects 2-5 header blocks per packet and NTCP2/LS2 do the same per session.

  • Constructor Details

    • ChaCha20

      private ChaCha20()
      Utility class, not intended for instantiation.
  • Method Details

    • encrypt

      public static void encrypt(byte[] key, byte[] iv, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
      Encrypt from plaintext to ciphertext.
      Parameters:
      key - first 32 bytes used as the key
      iv - first 12 bytes used as the iv
      plaintext - the plaintext to encrypt
      plaintextOffset - offset in plaintext
      ciphertext - the ciphertext output buffer
      ciphertextOffset - offset in ciphertext
      length - number of bytes to encrypt
    • encrypt

      public static void encrypt(byte[] key, byte[] iv, int ivOffset, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length)
      Encrypt from plaintext to ciphertext.
      Parameters:
      key - first 32 bytes used as the key
      iv - first 12 bytes starting at ivOffset used as the iv
      ivOffset - offset into iv array
      plaintext - the plaintext input buffer
      plaintextOffset - offset into plaintext buffer
      ciphertext - the ciphertext output buffer
      ciphertextOffset - offset into ciphertext buffer
      length - the number of bytes to encrypt
    • decrypt

      public static void decrypt(byte[] key, byte[] iv, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)
      Encrypt from ciphertext to plaintext.
      Parameters:
      key - first 32 bytes used as the key
      iv - first 12 bytes used as the iv
      ciphertext - the ciphertext to decrypt
      ciphertextOffset - offset in ciphertext
      plaintext - the plaintext output buffer
      plaintextOffset - offset in plaintext
      length - number of bytes to decrypt
    • decrypt

      public static void decrypt(byte[] key, byte[] iv, int ivOffset, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length)
      Encrypt from ciphertext to plaintext.
      Parameters:
      key - first 32 bytes used as the key
      iv - first 12 bytes starting at ivOffset used as the iv
      ivOffset - offset into iv array
      ciphertext - the ciphertext to decrypt
      ciphertextOffset - offset in ciphertext
      plaintext - the plaintext output buffer
      plaintextOffset - offset in plaintext
      length - number of bytes to decrypt