Move poly1305_get_tag() into poly1305.c, hide poly1305_init().
[tinc] / src / chacha-poly1305 / chachapoly.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Grigori Goronzy <goronzy@kinoho.net>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24
25 #ifndef CHACHAPOLY_H
26 #define CHACHAPOLY_H
27
28 #include "chacha.h"
29 #include "poly1305.h"
30
31 #define CHACHAPOLY_OK           0
32 #define CHACHAPOLY_INVALID_MAC  -1
33
34 struct chachapoly_ctx {
35         struct chacha_ctx cha_ctx;
36 };
37
38 /**
39  * Initialize ChaCha20-Poly1305 AEAD.
40  * For RFC 7539 conformant AEAD, 256 bit keys must be used.
41  *
42  * \param ctx context data
43  * \param key 16 or 32 bytes of key material
44  * \param key_len key length, 256 or 512 bits
45  * \return success if 0
46  */
47 int chachapoly_init(struct chachapoly_ctx *ctx, const void *key, int key_len);
48
49 /**
50  * Encrypt or decrypt with ChaCha20-Poly1305. The AEAD construction conforms
51  * to RFC 7539.
52  *
53  * \param ctx context data
54  * \param nonce nonce (12 bytes)
55  * \param input plaintext/ciphertext input
56  * \param input_len input length in bytes;
57  * \param output plaintext/ciphertext output
58  * \param tag tag output
59  * \param tag_len tag length in bytes (0-16);
60           if 0, authentification is skipped
61  * \param encrypt decrypt if 0, else encrypt
62  * \return CHACHAPOLY_OK if no error, CHACHAPOLY_INVALID_MAC if auth
63  *         failed when decrypting
64  */
65 int chachapoly_crypt(struct chachapoly_ctx *ctx, const void *nonce,
66                      void *input, int input_len,
67                      void *output, void *tag, int tag_len, int encrypt);
68
69 /**
70  * Encrypt or decrypt with Chacha20-Poly1305 for short messages.
71  * The AEAD construction is different from chachapoly_crypt, but more
72  * efficient for small messages. Up to 32 bytes can be encrypted. The size
73  * of associated data is not restricted. The interface is similar to
74  * chachapoly_crypt.
75  */
76 int chachapoly_crypt_short(struct chachapoly_ctx *ctx, const void *nonce,
77                            void *input, int input_len,
78                            void *output, void *tag, int tag_len, int encrypt);
79
80 #endif