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 
51 #define salsa20r12_crypt nettle_salsa20r12_crypt
52 
53 /* Alias for backwards compatibility */
54 #define salsa20_set_iv nettle_salsa20_set_nonce
55 
56 /* In octets.*/
57 #define SALSA20_128_KEY_SIZE 16
58 #define SALSA20_256_KEY_SIZE 32
59 #define SALSA20_BLOCK_SIZE 64
60 #define SALSA20_NONCE_SIZE 8
61 #define SALSA20_IV_SIZE SALSA20_NONCE_SIZE
62 
63 /* Aliases */
64 #define SALSA20_MIN_KEY_SIZE 16
65 #define SALSA20_MAX_KEY_SIZE 32
66 #define SALSA20_KEY_SIZE 32
67 
68 #define _SALSA20_INPUT_LENGTH 16
69 
70 struct salsa20_ctx
71 {
72   /* Indices 1-4 and 11-14 holds the key (two identical copies for the
73      shorter key size), indices 0, 5, 10, 15 are constant, indices 6, 7
74      are the IV, and indices 8, 9 are the block counter:
75 
76      C K K K
77      K C I I
78      B B C K
79      K K K C
80   */
81   uint32_t input[_SALSA20_INPUT_LENGTH];
82 };
83 
84 void
85 salsa20_128_set_key(struct salsa20_ctx *ctx, const uint8_t *key);
86 void
87 salsa20_256_set_key(struct salsa20_ctx *ctx, const uint8_t *key);
88 
89 void
90 salsa20_set_key(struct salsa20_ctx *ctx,
91 		size_t length, const uint8_t *key);
92 
93 void
94 salsa20_set_nonce(struct salsa20_ctx *ctx, const uint8_t *nonce);
95 
96 void
97 salsa20_crypt(struct salsa20_ctx *ctx,
98 	      size_t length, uint8_t *dst,
99 	      const uint8_t *src);
100 
101 void
102 salsa20r12_crypt(struct salsa20_ctx *ctx,
103 		 size_t length, uint8_t *dst,
104 		 const uint8_t *src);
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* NETTLE_SALSA20_H_INCLUDED */
111