The friendly Operating System for the Internet of Things
chacha.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 D. J. Bernstein (dedicated to the public domain)
3  * Copyright (C) 2015 RenĂ© Kijewski <rene.kijewski@fu-berlin.de>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23 
34 #ifndef CRYPTO_CHACHA_H
35 #define CRYPTO_CHACHA_H
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
48 typedef struct
49 {
50  uint32_t state[16];
51  uint8_t rounds;
52 } chacha_ctx;
53 
66 int chacha_init(chacha_ctx *ctx,
67  unsigned rounds,
68  const uint8_t *key, uint32_t keylen,
69  const uint8_t nonce[8]);
70 
83 void chacha_keystream_bytes(chacha_ctx *ctx, void *x);
84 
98 void chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c);
99 
103 static inline void chacha_decrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c)
104 {
105  chacha_encrypt_bytes(ctx, m, c);
106 }
107 
126 void chacha_prng_seed(const void *data, size_t bytes);
127 
134 uint32_t chacha_prng_next(void);
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif /* ifndef CRYPTO_CHACHA_H */
141 
int chacha_init(chacha_ctx *ctx, unsigned rounds, const uint8_t *key, uint32_t keylen, const uint8_t nonce[8])
Initialize a ChaCha context.
void chacha_prng_seed(const void *data, size_t bytes)
Seed the pseudo-random number generator.
void chacha_keystream_bytes(chacha_ctx *ctx, void *x)
Generate next block in the keystream.
void chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c)
Encode or decode a block of data.
uint8_t rounds
Number of iterations.
Definition: chacha.h:51
uint32_t chacha_prng_next(void)
Extract a number from the pseudo-random number generator.
A ChaCha cipher stream context.
Definition: chacha.h:48
static void chacha_decrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c)
Encode or decode a block of data.
Definition: chacha.h:103