This repo has been archived, Its has been merged to experimental module inside vlib/x/crypto/chacha,20. Development will be carried out there.
Chacha20 (and XChacha20) stream cipher encryption algorithm in V language based on RFC 8439.
Note that ChaCha20, like all stream ciphers, is not authenticated and allows attackers to silently tamper with the plaintext. For this reason, it is more appropriate as a building block than as a standalone encryption mechanism. Instead, consider using secure modules, like chacha20poly1305.
const key_size = 32size of ChaCha20 key, ie 256 bits size, in bytes
const nonce_size = 12size of ietf ChaCha20 nonce, ie 96 bits size, in bytes
const x_nonce_size = 24size of extended ChaCha20 nonce, called XChaCha20, 192 bits
fn encrypt(key []u8, nonce []u8, plaintext []u8) ![]u8encrypt was a thin wrapper around two supported nonce size, ChaCha20 with 96 bits and XChaCha20 with 192 bits nonce. If you want more control with internal counter see encrypt_with_counter
fn encrypt_with_counter(key []u8, nonce []u8, ctr u32, plaintext []u8) ![]u8encrypt_with_counter encrypts plaintext with internal counter set to ctr
fn new_cipher(key []u8, nonce []u8) !&Ciphernew_cipher creates a new ChaCha20 stream cipher with the given 32 bytes key and a 12 or 24 bytes nonce. If a nonce of 24 bytes is provided, the XChaCha20 construction will be used. It returns an error if key or nonce have any other length.
fn (mut c Cipher) free()free the resources taken by the Cipher c. Dont use cipher after .free call
fn (mut c Cipher) reset()reset quickly sets all Cipher's fields to default value
fn (mut c Cipher) set_counter(ctr u32)set_counter sets Cipher's counter
fn (mut c Cipher) encrypt(mut dst []u8, src []u8)encrypt encrypts plaintext in src bytes and stores ciphertext result in dst. Its fullfills cipher.Block.encrypt interface.
fn (mut c Cipher) decrypt(mut dst []u8, src []u8)decrypt does reverse of .encrypt() operation, decrypts ciphertext in src, and stores the result in dst. decrypt fullfills cipher.Block.decrypt interface.
fn (mut c Cipher) xor_key_stream(mut dst []u8, src []u8)xor_key_stream xors each byte in the given slice in the src with a byte from the cipher's key stream. It fullfills cipher.Stream interface. Its does encrypts plaintext message in src and stores ciphertext result in dst in single shot of run of encryption.
fn (mut c Cipher) rekey(key []u8, nonce []u8) !rekey resets internal Cipher's state and reinitializes state with the provided key and nonce