NAME
setupChachastate, chacha_setblock, chacha_encrypt, chacha_encrypt2 – chacha encryption

SYNOPSIS
#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>

void setupChachastate(Chachastate *s, uchar key[],                 int keylen, uchar *nonce, int rounds)

void chacha_encrypt(uchar *data, int len, Chachastate *s)

void chacha_encrypt2(uchar *src, uchar *dst, int len, Chachastate *s)

void chacha_setblock(Chachastate *s, u32int blockno)

DESCRIPTION

Chacha is D J Berstein's symmetric stream cipher, as modified by RFC7539. It uses keys of 256 bits (128 bits is supported here for special purposes). It has an underlying block size of 64 bytes (named as constant ChachaBsize). It is a potential replacement for rc4(2).

SetupChachastate takes a reference to a Chachastate structure, a key of keylen bytes, which should normally be ChachaKeylen, a nonce or initialisation vector of ChachaIVlen bytes (set to all zeros if the argument is nil), and the number of rounds (set to the default of 20 if the argument is zero). With a keylength of 256 bits (32 bytes) and 20 rounds, the function implements the Chacha20 encryption function of RFC7539.

Chacha_encrypt encrypts len bytes of buf in place using the Chachastate in s. Len can be any byte length. Encryption and decryption are the same operation given the same starting state s.

Chacha_encrypt2 is similar, but encrypts len bytes of src into dst without modifying src.

Chacha_setblock sets the Chacha block counter for the next encryption to blockno, allowing seeking in an encrypted stream.

SOURCE
/sys/src/libsec

SEE ALSO
mp(2), aes(2), blowfish(2), des(2), dsa(2), elgamal(2), rc4(2), rsa(2), sechash(2), prime(2), rand(2)
Copyright © 2025 Plan 9 Foundation. All rights reserved.