430198845c804b7883aad844ee78d2f5e95b0dc9
[platform/upstream/nettle.git] / salsa20.h
1 /* salsa20.h
2
3    The Salsa20 stream cipher.
4
5    Copyright (C) 2012 Simon Josefsson
6    Copyright (C) 2001 Niels Möller
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34
35 #ifndef NETTLE_SALSA20_H_INCLUDED
36 #define NETTLE_SALSA20_H_INCLUDED
37
38 #include "nettle-types.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /* Name mangling */
45 #define salsa20_set_key nettle_salsa20_set_key
46 #define salsa20_128_set_key nettle_salsa20_128_set_key
47 #define salsa20_256_set_key nettle_salsa20_256_set_key
48 #define salsa20_set_nonce nettle_salsa20_set_nonce
49 #define salsa20_crypt nettle_salsa20_crypt
50 #define _salsa20_core _nettle_salsa20_core
51
52 #define salsa20r12_crypt nettle_salsa20r12_crypt
53
54 /* Alias for backwards compatibility */
55 #define salsa20_set_iv nettle_salsa20_set_nonce
56
57 /* In octets.*/
58 #define SALSA20_128_KEY_SIZE 16
59 #define SALSA20_256_KEY_SIZE 32
60 #define SALSA20_BLOCK_SIZE 64
61 #define SALSA20_NONCE_SIZE 8
62 #define SALSA20_IV_SIZE SALSA20_NONCE_SIZE
63
64 /* Aliases */
65 #define SALSA20_MIN_KEY_SIZE 16
66 #define SALSA20_MAX_KEY_SIZE 32
67 #define SALSA20_KEY_SIZE 32
68
69 #define _SALSA20_INPUT_LENGTH 16
70
71 struct salsa20_ctx
72 {
73   /* Indices 1-4 and 11-14 holds the key (two identical copies for the
74      shorter key size), indices 0, 5, 10, 15 are constant, indices 6, 7
75      are the IV, and indices 8, 9 are the block counter:
76
77      C K K K
78      K C I I
79      B B C K
80      K K K C
81   */
82   uint32_t input[_SALSA20_INPUT_LENGTH];
83 };
84
85 void
86 salsa20_128_set_key(struct salsa20_ctx *ctx, const uint8_t *key);
87 void
88 salsa20_256_set_key(struct salsa20_ctx *ctx, const uint8_t *key);
89
90 void
91 salsa20_set_key(struct salsa20_ctx *ctx,
92                 size_t length, const uint8_t *key);
93
94 void
95 salsa20_set_nonce(struct salsa20_ctx *ctx, const uint8_t *nonce);
96   
97 void
98 salsa20_crypt(struct salsa20_ctx *ctx,
99               size_t length, uint8_t *dst,
100               const uint8_t *src);
101
102 void
103 salsa20r12_crypt(struct salsa20_ctx *ctx,
104                  size_t length, uint8_t *dst,
105                  const uint8_t *src);
106
107 void
108 _salsa20_core(uint32_t *dst, const uint32_t *src, unsigned rounds);
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif /* NETTLE_SALSA20_H_INCLUDED */