xref: /dragonfly/sys/crypto/chachapoly.h (revision 59e2d684)
1 /*
2  * Copyright (c) 2015 Mike Belopuhov
3  * Copyright (c) 2023 Aaron LI <aly@aaronly.me>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _CHACHAPOLY_H_
19 #define _CHACHAPOLY_H_
20 
21 #define CHACHA20POLY1305_KEY_SIZE	32
22 #define CHACHA20POLY1305_AUTHTAG_SIZE	16
23 #define CHACHA20POLY1305_NONCE_SIZE	12
24 #define XCHACHA20POLY1305_NONCE_SIZE	24
25 
26 /*
27  * ChaCha20-Poly1305 AEAD cipher (RFC 8439)
28  *
29  * NOTE: Support in-place encryption/decryption; i.e., the output buffer
30  *       points to the same location as the input.
31  */
32 void chacha20poly1305_encrypt(uint8_t *, const uint8_t *, size_t,
33 			      const uint8_t *, size_t,
34 			      const uint8_t[CHACHA20POLY1305_NONCE_SIZE],
35 			      const uint8_t[CHACHA20POLY1305_KEY_SIZE]);
36 bool chacha20poly1305_decrypt(uint8_t *, const uint8_t *, size_t,
37 			      const uint8_t *, size_t,
38 			      const uint8_t[CHACHA20POLY1305_NONCE_SIZE],
39 			      const uint8_t[CHACHA20POLY1305_KEY_SIZE]);
40 
41 /*
42  * XChaCha20-Poly1305 AEAD cipher
43  * (extended nonce size from 96 bits to 192 bits)
44  *
45  * NOTE: Support in-place encryption/decryption, as above.
46  */
47 void xchacha20poly1305_encrypt(uint8_t *, const uint8_t *, size_t,
48 			       const uint8_t *, size_t,
49 			       const uint8_t[XCHACHA20POLY1305_NONCE_SIZE],
50 			       const uint8_t[CHACHA20POLY1305_KEY_SIZE]);
51 bool xchacha20poly1305_decrypt(uint8_t *, const uint8_t *, size_t,
52 			       const uint8_t *, size_t,
53 			       const uint8_t[XCHACHA20POLY1305_NONCE_SIZE],
54 			       const uint8_t[CHACHA20POLY1305_KEY_SIZE]);
55 
56 #endif	/* _CHACHAPOLY_H_ */
57