ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/spritz/spritz.h
(Generate patch)

Comparing spritz/spritz.h (file contents):
Revision 1.1 by root, Fri Jan 9 09:11:16 2015 UTC vs.
Revision 1.5 by root, Sat Jan 10 08:29:03 2015 UTC

1//#include "spritz.h" 1/* spritz.h, spritz C implementation, header
2 *
3 * Copyright (c) 2015 Marc Alexander Lehmann <libev@schmorp.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modifica-
7 * tion, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
18 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
20 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
24 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
25 * OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License ("GPL") version 2 or any later version,
29 * in which case the provisions of the GPL are applicable instead of
30 * the above. If you wish to allow the use of your version of this file
31 * only under the terms of the GPL and not to allow others to use your
32 * version of this file under the BSD license, indicate your decision
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
35 * provisions above, a recipient may use your version of this file under
36 * either the BSD or the GPL.
37 */
38#ifndef SPRITZ_H
39#define SPRITZ_H
2 40
3#include <stdint.h> 41#include <stdint.h>
4#include <sys/types.h> 42#include <sys/types.h>
5 43
6/*******************************************************************************/ 44/*******************************************************************************/
22void spritz_init (spritz_state *s); 60void spritz_init (spritz_state *s);
23void spritz_update (spritz_state *s); 61void spritz_update (spritz_state *s);
24void spritz_whip (spritz_state *s, uint_fast16_t r); 62void spritz_whip (spritz_state *s, uint_fast16_t r);
25void spritz_crush (spritz_state *s); 63void spritz_crush (spritz_state *s);
26void spritz_shuffle (spritz_state *s); 64void spritz_shuffle (spritz_state *s);
27void spritz_absorb_nibble (spritz_state *s, uint8_t x);
28void spritz_absorb (spritz_state *s, const void *I, size_t I_len); 65void spritz_absorb (spritz_state *s, const void *I, size_t I_len);
29void spritz_absorb_stop (spritz_state *s); 66void spritz_absorb_stop (spritz_state *s);
30void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */ 67void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */
31uint8_t spritz_output (spritz_state *s); 68uint8_t spritz_output (spritz_state *s);
32void spritz_squeeze (spritz_state *s, void *P, size_t P_len); 69void spritz_squeeze (spritz_state *s, void *P, size_t P_len);
33uint8_t spritz_drip (spritz_state *s); 70uint8_t spritz_drip (spritz_state *s);
34 71
35/*******************************************************************************/ 72/*******************************************************************************/
73/* the spritz cipher */
74
75/* no IV is used if IV_len == 0 */
76void spritz_cipher_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len);
77
78/* can be called multiple times/incrementally */
79/* can work inplace */
80void spritz_cipher_encrypt (spritz_state *s, const void *I, void *O, size_t len);
81void spritz_cipher_decrypt (spritz_state *s, const void *I, void *O, size_t len);
82
83/*******************************************************************************/
36/* the spritz-xor cipher */ 84/* the spritz-xor cipher */
37 85
38/* no IV is used if IV_len == 0 */ 86/* no IV is used if IV_len == 0 */
39void spritz_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); 87static void spritz_cipher_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len);
40 88
41/* can be called multiple times/incrementally */ 89/* can be called multiple times/incrementally */
42/* can work inplace */ 90/* can work inplace */
43/* works for both encryption and decryption */ 91/* works for both encryption and decryption */
44void spritz_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); 92 void spritz_cipher_xor_crypt (spritz_state *s, const void *I, void *O, size_t len);
45 93
46/*******************************************************************************/ 94/*******************************************************************************/
47/* the spritz hash */ 95/* the spritz hash */
48 96
49static void spritz_hash_init (spritz_state *s); 97static void spritz_hash_init (spritz_state *s);
58static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ 106static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */
59 107
60/*******************************************************************************/ 108/*******************************************************************************/
61/* spritz authenticated encryption */ 109/* spritz authenticated encryption */
62 110
63 void spritz_aead_init (spritz_state *s, const void *K, size_t K_len); 111static void spritz_aead_init (spritz_state *s, const void *K, size_t K_len);
64static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ 112static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */
65static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ 113static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */
66 void spritz_aead_crypt (spritz_state *s, const void *I, void *O, size_t len); 114 void spritz_aead_encrypt (spritz_state *s, const void *I, void *O, size_t len);
115 void spritz_aead_decrypt (spritz_state *s, const void *I, void *O, size_t len);
67/* must be called after associated_data, only once, before finish */ 116/* must be called after associated_data, only once, before finish */
68/* works for both encryption and decryption */ 117/* works for both encryption and decryption */
69static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ 118static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */
70 119
71/*******************************************************************************/ 120/*******************************************************************************/
121/* spritz authenticated encryption (xor variant) */
122
123static void spritz_aead_xor_init (spritz_state *s, const void *K, size_t K_len);
124static void spritz_aead_xor_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */
125static void spritz_aead_xor_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */
126 void spritz_aead_xor_crypt (spritz_state *s, const void *I, void *O, size_t len);
127/* must be called after associated_data, only once, before finish */
128/* works for both encryption and decryption */
129static void spritz_aead_xor_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */
130
131/*******************************************************************************/
72/* the spritz drbg/csprng */ 132/* the spritz drbg/csprng */
73 133
74/* constructor takes a seed if S_len != 0, same add spritz_prng_put */ 134/* constructor takes a seed if S_len != 0, same as spritz_prng_add */
75 void spritz_prng_init (spritz_state *s, const void *S, size_t S_len); 135 void spritz_prng_init (spritz_state *s, const void *S, size_t S_len);
76static void spritz_prng_put (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */ 136static void spritz_prng_add (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */
77static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */ 137static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */
78 138
79/*******************************************************************************/ 139/*******************************************************************************/
80/* inline functions - some functions are so simple, they are defined inline */ 140/* inline functions - some functions are so simple, they are defined inline */
81 141
142/* the spritz-xor cipher inline functions */
143
144static void
145spritz_cipher_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len)
146{
147 spritz_cipher_init (s, K, K_len, IV, IV_len);
148}
149
82/* the spritz hash inline functions */ 150/* the spritz hash inline functions */
83 151
84static void 152static void
85spritz_hash_init (spritz_state *s) 153spritz_hash_init (spritz_state *s)
86{ 154{
108} 176}
109 177
110/* spritz authenticated encryption inline functions */ 178/* spritz authenticated encryption inline functions */
111 179
112static void 180static void
181spritz_aead_init (spritz_state *s, const void *K, size_t K_len)
182{
183 spritz_mac_init (s, K, K_len);
184}
185
186static void
113spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len) 187spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len)
114{ 188{
115 spritz_absorb_and_stop (s, N, N_len); 189 spritz_absorb_and_stop (s, N, N_len);
116} 190}
117 191
125spritz_aead_finish (spritz_state *s, void *H, size_t H_len) 199spritz_aead_finish (spritz_state *s, void *H, size_t H_len)
126{ 200{
127 spritz_mac_finish (s, H, H_len); 201 spritz_mac_finish (s, H, H_len);
128} 202}
129 203
204/* spritz authenticated encryption (xor variant) inline functions */
205
206static void
207spritz_aead_xor_init (spritz_state *s, const void *K, size_t K_len)
208{
209 spritz_mac_init (s, K, K_len);
210}
211
212static void
213spritz_aead_xor_nonce (spritz_state *s, const void *N, size_t N_len)
214{
215 spritz_absorb_and_stop (s, N, N_len);
216}
217
218static void
219spritz_aead_xor_associated_data (spritz_state *s, const void *D, size_t D_len)
220{
221 spritz_absorb_and_stop (s, D, D_len);
222}
223
224static void
225spritz_aead_xor_finish (spritz_state *s, void *H, size_t H_len)
226{
227 spritz_mac_finish (s, H, H_len);
228}
229
130/* the spritz drbg/csprng inline functions */ 230/* the spritz drbg/csprng inline functions */
131 231
132static void 232static void
133spritz_prng_put (spritz_state *s, const void *S, size_t S_len) 233spritz_prng_add (spritz_state *s, const void *S, size_t S_len)
134{ 234{
135 spritz_absorb (s, S, S_len); 235 spritz_absorb (s, S, S_len);
136} 236}
137 237
138/* get random bytes */ 238/* get random bytes */
140spritz_prng_get (spritz_state *s, void *R, size_t R_len) 240spritz_prng_get (spritz_state *s, void *R, size_t R_len)
141{ 241{
142 spritz_squeeze (s, R, R_len); 242 spritz_squeeze (s, R, R_len);
143} 243}
144 244
245#endif
246

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines