xref: /dragonfly/sys/crypto/chacha20/chacha.h (revision 01016e1b)
122cd51feSMatthew Dillon /* $OpenBSD: chacha.h,v 1.4 2016/08/27 04:04:56 guenther Exp $ */
222cd51feSMatthew Dillon 
322cd51feSMatthew Dillon /*
422cd51feSMatthew Dillon chacha-merged.c version 20080118
522cd51feSMatthew Dillon D. J. Bernstein
622cd51feSMatthew Dillon Public domain.
722cd51feSMatthew Dillon */
822cd51feSMatthew Dillon 
922cd51feSMatthew Dillon #ifndef CHACHA_H
1022cd51feSMatthew Dillon #define CHACHA_H
1122cd51feSMatthew Dillon 
1222cd51feSMatthew Dillon #include <sys/types.h>
1322766d5aSAaron LI 
1422cd51feSMatthew Dillon #include "_chacha.h"
1522cd51feSMatthew Dillon 
1622cd51feSMatthew Dillon #define CHACHA_MINKEYLEN 	16
1722cd51feSMatthew Dillon #define CHACHA_NONCELEN		8
1822cd51feSMatthew Dillon #define CHACHA_CTRLEN		8
1922cd51feSMatthew Dillon #define CHACHA_STATELEN		(CHACHA_NONCELEN + CHACHA_CTRLEN)
2022cd51feSMatthew Dillon #define CHACHA_BLOCKLEN		64
2122cd51feSMatthew Dillon 
2222cd51feSMatthew Dillon #ifdef CHACHA_EMBED
2322cd51feSMatthew Dillon #define LOCAL static
2422cd51feSMatthew Dillon #else
2522cd51feSMatthew Dillon #define LOCAL
2622cd51feSMatthew Dillon #endif
2722cd51feSMatthew Dillon 
28*01016e1bSAaron LI /*
29*01016e1bSAaron LI  * Initialize the context with key <k> of length <kbits> bit.
30*01016e1bSAaron LI  * The recommended key length is 256 bits, although 128-bit keys are
31*01016e1bSAaron LI  * also supported.
32*01016e1bSAaron LI  */
3322766d5aSAaron LI LOCAL void chacha_keysetup(struct chacha_ctx *x, const uint8_t *k,
3422766d5aSAaron LI     uint32_t kbits);
35*01016e1bSAaron LI 
36*01016e1bSAaron LI /*
37*01016e1bSAaron LI  * Setup the context with a 64-bit IV <iv> and a 64-bit counter
38*01016e1bSAaron LI  * <counter>.  If <counter> is NULL, then the value zero (0) is used.
39*01016e1bSAaron LI  *
40*01016e1bSAaron LI  * If CHACHA_NONCE0_CTR128 is defined, then the IV <iv> is ignored
41*01016e1bSAaron LI  * and the counter <counter> must be 128 bits.
42*01016e1bSAaron LI  */
4322766d5aSAaron LI LOCAL void chacha_ivsetup(struct chacha_ctx *x, const uint8_t *iv,
4422766d5aSAaron LI     const uint8_t *counter);
45*01016e1bSAaron LI 
46*01016e1bSAaron LI /*
47*01016e1bSAaron LI  * Encrypt/decrypt the byte string <m> of length <bytes>, write the
48*01016e1bSAaron LI  * result to <c>.
49*01016e1bSAaron LI  *
50*01016e1bSAaron LI  * The output buffer <c> may point to the same area as the input <m>.
51*01016e1bSAaron LI  * In that case, the encryption/decryption is performed in-place.
52*01016e1bSAaron LI  *
53*01016e1bSAaron LI  * If KEYSTREAM_ONLY is defined, then this function only generates a
54*01016e1bSAaron LI  * key stream of the requested length <bytes> and writes to <c>.
55*01016e1bSAaron LI  * The input <m> is unused and should be NULL in this case.
56*01016e1bSAaron LI  */
5722766d5aSAaron LI LOCAL void chacha_encrypt_bytes(struct chacha_ctx *x, const uint8_t *m,
5822766d5aSAaron LI     uint8_t *c, uint32_t bytes);
5922766d5aSAaron LI 
6022cd51feSMatthew Dillon #ifdef CHACHA_NONCE0_CTR128
61*01016e1bSAaron LI /*
62*01016e1bSAaron LI  * Save the current value of the 128-bit counter, which can be restored
63*01016e1bSAaron LI  * by calling chacha_ivsetup(x, NULL, counter).
64*01016e1bSAaron LI  */
654104d691SAaron LI LOCAL void chacha_ctrsave(const struct chacha_ctx *x, uint8_t *counter)
664104d691SAaron LI     __unused; /* maybe unused */
6722cd51feSMatthew Dillon #endif
6822cd51feSMatthew Dillon 
69*01016e1bSAaron LI /*
70*01016e1bSAaron LI  * HChaCha20 is an intermediary block to build XChaCha20, similar to
71*01016e1bSAaron LI  * the HSalsa20 defined for XSalsa20.
72*01016e1bSAaron LI  */
73ec5a219cSAaron LI LOCAL void hchacha20(uint8_t derived_key[32], const uint8_t nonce[16],
74ec5a219cSAaron LI     const uint8_t key[32]) __unused; /* maybe unused */
75ec5a219cSAaron LI 
7622cd51feSMatthew Dillon #endif	/* CHACHA_H */
77