1 /* chacha.h
2 
3    The ChaCha stream cipher.
4 
5    Copyright (C) 2013 Joachim Strömbergson
6    Copyright (C) 2012 Simon Josefsson
7    Copyright (C) 2014 Niels Möller
8 
9    This file is part of GNU Nettle.
10 
11    GNU Nettle is free software: you can redistribute it and/or
12    modify it under the terms of either:
13 
14      * the GNU Lesser General Public License as published by the Free
15        Software Foundation; either version 3 of the License, or (at your
16        option) any later version.
17 
18    or
19 
20      * the GNU General Public License as published by the Free
21        Software Foundation; either version 2 of the License, or (at your
22        option) any later version.
23 
24    or both in parallel, as here.
25 
26    GNU Nettle is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29    General Public License for more details.
30 
31    You should have received copies of the GNU General Public License and
32    the GNU Lesser General Public License along with this program.  If
33    not, see http://www.gnu.org/licenses/.
34 */
35 
36 #ifndef NETTLE_CHACHA_H_INCLUDED
37 #define NETTLE_CHACHA_H_INCLUDED
38 
39 #include "nettle-types.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /* Name mangling */
46 #define chacha_set_key nettle_chacha_set_key
47 #define chacha_set_nonce nettle_chacha_set_nonce
48 #define chacha_set_nonce96 nettle_chacha_set_nonce96
49 #define chacha_set_counter nettle_chacha_set_counter
50 #define chacha_set_counter32 nettle_chacha_set_counter32
51 #define chacha_crypt nettle_chacha_crypt
52 #define chacha_crypt32 nettle_chacha_crypt32
53 
54 /* Currently, only 256-bit keys are supported. */
55 #define CHACHA_KEY_SIZE 32
56 #define CHACHA_BLOCK_SIZE 64
57 #define CHACHA_NONCE_SIZE 8
58 #define CHACHA_NONCE96_SIZE 12
59 #define CHACHA_COUNTER_SIZE 8
60 #define CHACHA_COUNTER32_SIZE 4
61 
62 #define _CHACHA_STATE_LENGTH 16
63 
64 struct chacha_ctx
65 {
66   /* Indices 0-3 holds a constant (SIGMA or TAU).
67      Indices 4-11 holds the key.
68      Indices 12-13 holds the block counter.
69      Indices 14-15 holds the IV:
70 
71      This creates the state matrix:
72      C C C C
73      K K K K
74      K K K K
75      B B I I
76   */
77   uint32_t state[_CHACHA_STATE_LENGTH];
78 };
79 
80 void
81 chacha_set_key(struct chacha_ctx *ctx, const uint8_t *key);
82 
83 void
84 chacha_set_nonce(struct chacha_ctx *ctx, const uint8_t *nonce);
85 
86 void
87 chacha_set_nonce96(struct chacha_ctx *ctx, const uint8_t *nonce);
88 
89 void
90 chacha_set_counter(struct chacha_ctx *ctx, const uint8_t *counter);
91 
92 void
93 chacha_set_counter32(struct chacha_ctx *ctx, const uint8_t *counter);
94 
95 void
96 chacha_crypt(struct chacha_ctx *ctx, size_t length,
97              uint8_t *dst, const uint8_t *src);
98 
99 void
100 chacha_crypt32(struct chacha_ctx *ctx, size_t length,
101 	       uint8_t *dst, const uint8_t *src);
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
107 #endif /* NETTLE_CHACHA_H_INCLUDED */
108