java.lang.Object
net.i2p.crypto.ChaCha20
ChaCha20, wrapper around Noise ChaChaCore. RFC 7539.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal<int[][]> Per-thread scratch pair:[0]is the input state block,[1]is the keystream block.private static final intNumber of 32-bit words in a ChaCha20 state block.Constructor Summary
ConstructorsMethod Summary
All MethodsModifier and TypeMethodDescriptionstatic voiddecrypt(byte[] key, byte[] iv, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) Encrypt from ciphertext to plaintext.static voiddecrypt(byte[] key, byte[] iv, int ivOffset, byte[] ciphertext, int ciphertextOffset, byte[] plaintext, int plaintextOffset, int length) Encrypt from ciphertext to plaintext.static voidencrypt(byte[] key, byte[] iv, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) Encrypt from plaintext to ciphertext.static voidencrypt(byte[] key, byte[] iv, int ivOffset, byte[] plaintext, int plaintextOffset, byte[] ciphertext, int ciphertextOffset, int length) Encrypt from plaintext to ciphertext.
Field Details
STATE_WORDS
private static final int STATE_WORDSNumber of 32-bit words in a ChaCha20 state block.- See Also:
_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 areChaChaCorestatics andDataHelper.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 keyiv- first 12 bytes used as the ivplaintext- the plaintext to encryptplaintextOffset- offset in plaintextciphertext- the ciphertext output bufferciphertextOffset- offset in ciphertextlength- 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 keyiv- first 12 bytes starting at ivOffset used as the ivivOffset- offset into iv arrayplaintext- the plaintext input bufferplaintextOffset- offset into plaintext bufferciphertext- the ciphertext output bufferciphertextOffset- offset into ciphertext bufferlength- 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 keyiv- first 12 bytes used as the ivciphertext- the ciphertext to decryptciphertextOffset- offset in ciphertextplaintext- the plaintext output bufferplaintextOffset- offset in plaintextlength- 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 keyiv- first 12 bytes starting at ivOffset used as the ivivOffset- offset into iv arrayciphertext- the ciphertext to decryptciphertextOffset- offset in ciphertextplaintext- the plaintext output bufferplaintextOffset- offset in plaintextlength- number of bytes to decrypt