… | |
… | |
33 | * by deleting the provisions above and replace them with the notice |
33 | * by deleting the provisions above and replace them with the notice |
34 | * and other provisions required by the GPL. If you do not delete the |
34 | * and other provisions required by the GPL. If you do not delete the |
35 | * provisions above, a recipient may use your version of this file under |
35 | * provisions above, a recipient may use your version of this file under |
36 | * either the BSD or the GPL. |
36 | * either the BSD or the GPL. |
37 | */ |
37 | */ |
|
|
38 | #ifndef SPRITZ_H |
|
|
39 | #define SPRITZ_H |
38 | |
40 | |
39 | #include <stdint.h> |
41 | #include <stdint.h> |
40 | #include <sys/types.h> |
42 | #include <sys/types.h> |
41 | |
43 | |
42 | /*******************************************************************************/ |
44 | /*******************************************************************************/ |
43 | /* spritz parameters/state type */ |
45 | /* spritz parameters/state type */ |
44 | |
46 | |
45 | enum { |
47 | enum { |
46 | spritz_N = 256 |
48 | spritz_N = 256, |
|
|
49 | spritz_aead_blocksize = spritz_N >> 2 /* 64 */ |
47 | }; |
50 | }; |
48 | |
51 | |
49 | typedef struct |
52 | typedef struct |
50 | { |
53 | { |
51 | uint8_t a, i, j, k, z, w; |
54 | uint8_t a, i, j, k, z, w; |
… | |
… | |
58 | void spritz_init (spritz_state *s); |
61 | void spritz_init (spritz_state *s); |
59 | void spritz_update (spritz_state *s); |
62 | void spritz_update (spritz_state *s); |
60 | void spritz_whip (spritz_state *s, uint_fast16_t r); |
63 | void spritz_whip (spritz_state *s, uint_fast16_t r); |
61 | void spritz_crush (spritz_state *s); |
64 | void spritz_crush (spritz_state *s); |
62 | void spritz_shuffle (spritz_state *s); |
65 | void spritz_shuffle (spritz_state *s); |
63 | void spritz_absorb_nibble (spritz_state *s, uint8_t x); |
|
|
64 | void spritz_absorb (spritz_state *s, const void *I, size_t I_len); |
66 | void spritz_absorb (spritz_state *s, const void *I, size_t I_len); |
65 | void spritz_absorb_stop (spritz_state *s); |
67 | void spritz_absorb_stop (spritz_state *s); |
66 | void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */ |
68 | void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */ |
67 | uint8_t spritz_output (spritz_state *s); |
69 | uint8_t spritz_output (spritz_state *s); |
68 | void spritz_squeeze (spritz_state *s, void *P, size_t P_len); |
70 | void spritz_squeeze (spritz_state *s, void *P, size_t P_len); |
69 | uint8_t spritz_drip (spritz_state *s); |
71 | uint8_t spritz_drip (spritz_state *s); |
70 | |
72 | |
71 | /*******************************************************************************/ |
73 | /*******************************************************************************/ |
|
|
74 | /* the spritz cipher */ |
|
|
75 | |
|
|
76 | /* no IV is used if IV_len == 0 */ |
|
|
77 | void spritz_cipher_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); |
|
|
78 | |
|
|
79 | /* can be called multiple times/incrementally */ |
|
|
80 | /* can work inplace */ |
|
|
81 | void spritz_cipher_encrypt (spritz_state *s, const void *I, void *O, size_t len); |
|
|
82 | void spritz_cipher_decrypt (spritz_state *s, const void *I, void *O, size_t len); |
|
|
83 | |
|
|
84 | /*******************************************************************************/ |
72 | /* the spritz-xor cipher */ |
85 | /* the spritz-xor cipher */ |
73 | |
86 | |
74 | /* no IV is used if IV_len == 0 */ |
87 | /* no IV is used if IV_len == 0 */ |
75 | void spritz_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); |
88 | static void spritz_cipher_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); |
76 | |
89 | |
77 | /* can be called multiple times/incrementally */ |
90 | /* can be called multiple times/incrementally */ |
78 | /* can work inplace */ |
91 | /* can work inplace */ |
79 | /* works for both encryption and decryption */ |
92 | /* works for both encryption and decryption */ |
80 | void spritz_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); |
93 | void spritz_cipher_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); |
81 | |
94 | |
82 | /*******************************************************************************/ |
95 | /*******************************************************************************/ |
83 | /* the spritz hash */ |
96 | /* the spritz hash */ |
84 | |
97 | |
85 | static void spritz_hash_init (spritz_state *s); |
98 | static void spritz_hash_init (spritz_state *s); |
… | |
… | |
94 | static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
107 | static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
95 | |
108 | |
96 | /*******************************************************************************/ |
109 | /*******************************************************************************/ |
97 | /* spritz authenticated encryption */ |
110 | /* spritz authenticated encryption */ |
98 | |
111 | |
99 | void spritz_aead_init (spritz_state *s, const void *K, size_t K_len); |
112 | static void spritz_aead_init (spritz_state *s, const void *K, size_t K_len); |
100 | static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ |
113 | static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ |
101 | static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ |
114 | static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ |
102 | void spritz_aead_crypt (spritz_state *s, const void *I, void *O, size_t len); |
115 | void spritz_aead_encrypt (spritz_state *s, const void *I, void *O, size_t len); |
|
|
116 | void spritz_aead_decrypt (spritz_state *s, const void *I, void *O, size_t len); |
103 | /* must be called after associated_data, only once, before finish */ |
117 | /* must be called after associated_data, only once, before finish */ |
104 | /* works for both encryption and decryption */ |
118 | /* works for both encryption and decryption */ |
105 | static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
119 | static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
106 | |
120 | |
107 | /*******************************************************************************/ |
121 | /*******************************************************************************/ |
|
|
122 | /* spritz authenticated encryption (xor variant) */ |
|
|
123 | |
|
|
124 | static void spritz_aead_xor_init (spritz_state *s, const void *K, size_t K_len); |
|
|
125 | static void spritz_aead_xor_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ |
|
|
126 | static void spritz_aead_xor_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ |
|
|
127 | void spritz_aead_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); |
|
|
128 | /* must be called after associated_data, only once, before finish */ |
|
|
129 | /* works for both encryption and decryption */ |
|
|
130 | static void spritz_aead_xor_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
|
|
131 | |
|
|
132 | /*******************************************************************************/ |
108 | /* the spritz drbg/csprng */ |
133 | /* the spritz drbg/csprng */ |
109 | |
134 | |
110 | /* constructor takes a seed if S_len != 0, same add spritz_prng_put */ |
135 | /* constructor takes a seed if S_len != 0, same as spritz_prng_add */ |
111 | void spritz_prng_init (spritz_state *s, const void *S, size_t S_len); |
136 | void spritz_prng_init (spritz_state *s, const void *S, size_t S_len); |
112 | static void spritz_prng_put (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */ |
137 | static void spritz_prng_add (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */ |
113 | static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */ |
138 | static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */ |
114 | |
139 | |
115 | /*******************************************************************************/ |
140 | /*******************************************************************************/ |
116 | /* inline functions - some functions are so simple, they are defined inline */ |
141 | /* inline functions - some functions are so simple, they are defined inline */ |
117 | |
142 | |
|
|
143 | /* the spritz-xor cipher inline functions */ |
|
|
144 | |
|
|
145 | static void |
|
|
146 | spritz_cipher_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len) |
|
|
147 | { |
|
|
148 | spritz_cipher_init (s, K, K_len, IV, IV_len); |
|
|
149 | } |
|
|
150 | |
118 | /* the spritz hash inline functions */ |
151 | /* the spritz hash inline functions */ |
119 | |
152 | |
120 | static void |
153 | static void |
121 | spritz_hash_init (spritz_state *s) |
154 | spritz_hash_init (spritz_state *s) |
122 | { |
155 | { |
… | |
… | |
144 | } |
177 | } |
145 | |
178 | |
146 | /* spritz authenticated encryption inline functions */ |
179 | /* spritz authenticated encryption inline functions */ |
147 | |
180 | |
148 | static void |
181 | static void |
|
|
182 | spritz_aead_init (spritz_state *s, const void *K, size_t K_len) |
|
|
183 | { |
|
|
184 | spritz_mac_init (s, K, K_len); |
|
|
185 | } |
|
|
186 | |
|
|
187 | static void |
149 | spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len) |
188 | spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len) |
150 | { |
189 | { |
151 | spritz_absorb_and_stop (s, N, N_len); |
190 | spritz_absorb_and_stop (s, N, N_len); |
152 | } |
191 | } |
153 | |
192 | |
… | |
… | |
161 | spritz_aead_finish (spritz_state *s, void *H, size_t H_len) |
200 | spritz_aead_finish (spritz_state *s, void *H, size_t H_len) |
162 | { |
201 | { |
163 | spritz_mac_finish (s, H, H_len); |
202 | spritz_mac_finish (s, H, H_len); |
164 | } |
203 | } |
165 | |
204 | |
|
|
205 | /* spritz authenticated encryption (xor variant) inline functions */ |
|
|
206 | |
|
|
207 | static void |
|
|
208 | spritz_aead_xor_init (spritz_state *s, const void *K, size_t K_len) |
|
|
209 | { |
|
|
210 | spritz_mac_init (s, K, K_len); |
|
|
211 | } |
|
|
212 | |
|
|
213 | static void |
|
|
214 | spritz_aead_xor_nonce (spritz_state *s, const void *N, size_t N_len) |
|
|
215 | { |
|
|
216 | spritz_absorb_and_stop (s, N, N_len); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static void |
|
|
220 | spritz_aead_xor_associated_data (spritz_state *s, const void *D, size_t D_len) |
|
|
221 | { |
|
|
222 | spritz_absorb_and_stop (s, D, D_len); |
|
|
223 | } |
|
|
224 | |
|
|
225 | static void |
|
|
226 | spritz_aead_xor_finish (spritz_state *s, void *H, size_t H_len) |
|
|
227 | { |
|
|
228 | spritz_mac_finish (s, H, H_len); |
|
|
229 | } |
|
|
230 | |
166 | /* the spritz drbg/csprng inline functions */ |
231 | /* the spritz drbg/csprng inline functions */ |
167 | |
232 | |
168 | static void |
233 | static void |
169 | spritz_prng_put (spritz_state *s, const void *S, size_t S_len) |
234 | spritz_prng_add (spritz_state *s, const void *S, size_t S_len) |
170 | { |
235 | { |
171 | spritz_absorb (s, S, S_len); |
236 | spritz_absorb (s, S, S_len); |
172 | } |
237 | } |
173 | |
238 | |
174 | /* get random bytes */ |
239 | /* get random bytes */ |
… | |
… | |
176 | spritz_prng_get (spritz_state *s, void *R, size_t R_len) |
241 | spritz_prng_get (spritz_state *s, void *R, size_t R_len) |
177 | { |
242 | { |
178 | spritz_squeeze (s, R, R_len); |
243 | spritz_squeeze (s, R, R_len); |
179 | } |
244 | } |
180 | |
245 | |
|
|
246 | #endif |
|
|
247 | |