chacha20poly1305.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Koen Zandberg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
27 #include "crypto/poly1305.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define CHACHA20POLY1305_CONSTANT_BYTES (16U)
34 #define CHACHA20POLY1305_KEY_BYTES (32U)
35 #define CHACHA20POLY1305_COUNTER_BYTES (4U)
36 #define CHACHA20POLY1305_NONCE_BYTES (12U)
37 #define CHACHA20POLY1305_TAG_BYTES (16U)
38 #define CHACHA20POLY1305_BLOCK_BYTES (64U)
43 typedef union {
45  uint32_t words[CHACHA20POLY1305_BLOCK_BYTES / sizeof(uint32_t)];
46  struct {
48  uint32_t constant[CHACHA20POLY1305_CONSTANT_BYTES / sizeof(uint32_t)];
50  uint32_t key[CHACHA20POLY1305_KEY_BYTES / sizeof(uint32_t)];
52  uint32_t counter[CHACHA20POLY1305_COUNTER_BYTES / sizeof(uint32_t)];
54  uint32_t nonce[CHACHA20POLY1305_NONCE_BYTES / sizeof(uint32_t)];
55  }__attribute__ ((__packed__)) split;
57 
61 typedef union {
62  /* We need both the state matrix and the poly1305 state, but nearly not at
63  * the same time. This works as long as the first 8 members of state
64  * overlap fully or completely not with the first and second key parts
65  * from the @ref poly1305_ctx_t struct */
69 
90 void chacha20poly1305_encrypt(uint8_t *cipher, const uint8_t *msg,
91  size_t msglen, const uint8_t *aad, size_t aadlen,
92  const uint8_t *key, const uint8_t *nonce);
110 int chacha20poly1305_decrypt(const uint8_t *cipher, size_t cipherlen,
111  uint8_t *msg, size_t *msglen,
112  const uint8_t *aad, size_t aadlen,
113  const uint8_t *key, const uint8_t *nonce);
114 
127 void chacha20_encrypt_decrypt( const uint8_t *key,
128  const uint8_t *nonce,
129  uint32_t counter,
130  const uint8_t *input,
131  size_t input_length,
132  uint8_t *output);
143  const uint8_t *key,
144  const uint8_t *nonce,
145  const uint32_t counter);
146 
155  const uint8_t *input,
156  uint8_t *output);
157 
167  const uint8_t *input,
168  size_t input_length,
169  uint8_t *output);
170 
171 #ifdef __cplusplus
172 }
173 #endif
#define CHACHA20POLY1305_BLOCK_BYTES
Block length in bytes.
int chacha20poly1305_decrypt(const uint8_t *cipher, size_t cipherlen, uint8_t *msg, size_t *msglen, const uint8_t *aad, size_t aadlen, const uint8_t *key, const uint8_t *nonce)
Verify the tag and decrypt a ciphertext to plaintext.
void chacha20_encrypt_decrypt(const uint8_t *key, const uint8_t *nonce, uint32_t counter, const uint8_t *input, size_t input_length, uint8_t *output)
Encrypt a plaintext to ciphertext with the ChaCha20 algorithm.
void chacha20_update(chacha20_ctx_t *ctx, const uint8_t *input, uint8_t *output)
Update a ChaCha20 encrypt or decrypt multipart operation.
void chacha20poly1305_encrypt(uint8_t *cipher, const uint8_t *msg, size_t msglen, const uint8_t *aad, size_t aadlen, const uint8_t *key, const uint8_t *nonce)
Encrypt a plaintext to ciphertext and append a tag to protect the ciphertext and additional data.
#define CHACHA20POLY1305_KEY_BYTES
Key length in bytes.
void chacha20_finish(chacha20_ctx_t *ctx, const uint8_t *input, size_t input_length, uint8_t *output)
Finish a ChaCha20 encrypt or decrypt multipart operation.
#define CHACHA20POLY1305_CONSTANT_BYTES
Constants length in bytes.
#define CHACHA20POLY1305_NONCE_BYTES
Nonce length in bytes.
void chacha20_setup(chacha20_ctx_t *ctx, const uint8_t *key, const uint8_t *nonce, const uint32_t counter)
Setup a ChaCha20 encrypt or decrypt multipart operation.
#define CHACHA20POLY1305_COUNTER_BYTES
Counter length in bytes.
Poly1305 MAC interface.
Poly1305 context.
Definition: poly1305.h:46
Context of a ChaCha20 multipart operation.
Chacha20poly1305 state struct.
chacha20_ctx_t chacha20
The context of the ChaCha20 operation.
poly1305_ctx_t poly
Poly1305 state for the MAC.