xref: /dragonfly/sys/crypto/chacha20/chacha.h (revision 03517d4e)
1 /* $OpenBSD: chacha.h,v 1.4 2016/08/27 04:04:56 guenther Exp $ */
2 
3 /*
4 chacha-merged.c version 20080118
5 D. J. Bernstein
6 Public domain.
7 */
8 
9 #ifndef CHACHA_H
10 #define CHACHA_H
11 
12 #include <sys/types.h>
13 
14 #include "_chacha.h"
15 
16 #define CHACHA_MINKEYLEN 	16
17 #define CHACHA_NONCELEN		8
18 #define CHACHA_CTRLEN		8
19 #define CHACHA_STATELEN		(CHACHA_NONCELEN + CHACHA_CTRLEN)
20 #define CHACHA_BLOCKLEN		64
21 
22 #ifdef CHACHA_EMBED
23 #define LOCAL static
24 #else
25 #define LOCAL
26 #endif
27 
28 /*
29  * Initialize the context with key <k> of length <kbits> bit.
30  * The recommended key length is 256 bits, although 128-bit keys are
31  * also supported.
32  */
33 LOCAL void chacha_keysetup(struct chacha_ctx *x, const uint8_t *k,
34     uint32_t kbits);
35 
36 /*
37  * Setup the context with a 64-bit IV <iv> and a 64-bit counter
38  * <counter>.  If <counter> is NULL, then the value zero (0) is used.
39  *
40  * If CHACHA_NONCE0_CTR128 is defined, then the IV <iv> is ignored
41  * and the counter <counter> must be 128 bits.
42  */
43 LOCAL void chacha_ivsetup(struct chacha_ctx *x, const uint8_t *iv,
44     const uint8_t *counter);
45 
46 /*
47  * Encrypt/decrypt the byte string <m> of length <bytes>, write the
48  * result to <c>.
49  *
50  * The output buffer <c> may point to the same area as the input <m>.
51  * In that case, the encryption/decryption is performed in-place.
52  *
53  * If KEYSTREAM_ONLY is defined, then this function only generates a
54  * key stream of the requested length <bytes> and writes to <c>.
55  * The input <m> is unused and should be NULL in this case.
56  */
57 LOCAL void chacha_encrypt_bytes(struct chacha_ctx *x, const uint8_t *m,
58     uint8_t *c, uint32_t bytes);
59 
60 #ifdef CHACHA_NONCE0_CTR128
61 /*
62  * Save the current value of the 128-bit counter, which can be restored
63  * by calling chacha_ivsetup(x, NULL, counter).
64  */
65 LOCAL void chacha_ctrsave(const struct chacha_ctx *x, uint8_t *counter)
66     __unused; /* maybe unused */
67 #endif
68 
69 /*
70  * HChaCha20 is an intermediary block to build XChaCha20, similar to
71  * the HSalsa20 defined for XSalsa20.
72  */
73 LOCAL void hchacha20(uint8_t derived_key[32], const uint8_t nonce[16],
74     const uint8_t key[32]) __unused; /* maybe unused */
75 
76 #endif	/* CHACHA_H */
77