spritz/spritz.c

filter: Statementstmt 96.0% 147 / 153 Branchbran 94.4% 34 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.7% 181 / 189 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Spritz.xs ↑ Summary blib/lib/Crypt/Spritz.pm »
1 /* spritz.c, spritz C implementation
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  
39 #include "spritz.h"
40  
41 #include <assert.h>
42  
43 /*****************************************************************************/
44  
45 #define SPRITZ_SWAP(a,b) { uint8_t ss_c = (a); (a) = (b); (b) = ss_c; }
46  
47 void
48 100 spritz_init (spritz_state *s)
49 {
50 100 s->a =
51 100 s->i =
52 100 s->j =
53 100 s->k =
54 100 s->z = 0;
55 100 s->w = 1;
56  
57 100 uint_fast8_t v = spritz_N - 1;
58 do
59 25600 s->S[v] = v;
60 25600 ▶ while (v--);
Branch: 1 branch
BranchTrueFalseTotal
while (v--); 25500 100 25600
61 100 }
62  
63 void
64 181197 spritz_update (spritz_state *s)
65 {
66 181197 s->i = s->i + s->w;
67 181197 s->j = s->k + s->S[(uint8_t)(s->j + s->S[s->i])];
68 181197 s->k = s->k + s->i + s->S[s->j];
69 181197 SPRITZ_SWAP (s->S[s->i], s->S[s->j]);
70 181197 }
71  
72 void
73 351 spritz_whip (spritz_state *s, uint_fast16_t r)
74 {
75 180063 ▶ while (r--)
Branch: 1 branch
BranchTrueFalseTotal
while (r--) 179712 351 180063
76 179712 spritz_update (s);
77  
78 351 s->w += 2;
79 351 }
80  
81 void
82 234 spritz_crush (spritz_state *s)
83 {
84 uint_fast16_t v;
85  
86 30186 ▶ for (v = 0; v < (spritz_N >> 1); ++v)
Branch: 1 branch
BranchTrueFalseTotal
for (v = 0; v < (spritz_N >> 1); ++v) 29952 234 30186
87 29952 ▶ if (s->S[v] > s->S[spritz_N - 1 - v])
Branch: 1 branch
BranchTrueFalseTotal
if (s->S[v] > s->S[spritz_N - 1 - v]) 14607 15345 29952
88 14607 SPRITZ_SWAP (s->S[v], s->S[spritz_N - 1 - v]);
89 234 }
90  
91 void
92 117 spritz_shuffle (spritz_state *s)
93 {
94 117 spritz_whip (s, 2 * spritz_N); spritz_crush (s);
95 117 spritz_whip (s, 2 * spritz_N); spritz_crush (s);
96 117 spritz_whip (s, 2 * spritz_N);
97  
98 117 s->a = 0;
99 117 }
100  
101 static void
102 2241 spritz_shuffle_absorb (spritz_state *s)
103 {
104 2241 ▶ if (s->a == (spritz_N >> 1))
Branch: 1 branch
BranchTrueFalseTotal
if (s->a == (spritz_N >> 1)) 0 2241 2241
105 0 spritz_shuffle (s);
106 2241 }
107  
108 static void
109 2124 spritz_absorb_nibble (spritz_state *s, uint8_t x)
110 {
111 2124 spritz_shuffle_absorb (s);
112  
113 2124 SPRITZ_SWAP (s->S[s->a], s->S[(uint8_t)((spritz_N >> 1) + x)]);
114 2124 ++s->a;
115 2124 }
116  
117 static void
118 1062 spritz_absorb_byte (spritz_state *s, uint8_t b)
119 {
120 1062 spritz_absorb_nibble (s, b & 15);
121 1062 spritz_absorb_nibble (s, b >> 4);
122 1062 }
123  
124 void
125 204 spritz_absorb (spritz_state *s, const void *I, size_t I_len)
126 {
127 204 uint8_t *i = (uint8_t *)I;
128  
129 879 ▶ while (I_len--)
Branch: 1 branch
BranchTrueFalseTotal
while (I_len--) 675 204 879
130 675 spritz_absorb_byte (s, *i++);
131 204 }
132  
133 void
134 117 spritz_absorb_stop (spritz_state *s)
135 {
136 117 spritz_shuffle_absorb (s);
137  
138 117 ++s->a;
139 117 }
140  
141 void
142 36 spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len)
143 {
144 36 spritz_absorb (s, I, I_len);
145 36 spritz_absorb_stop (s);
146 36 }
147  
148 static void
149 180 spritz_shuffle_squeeze (spritz_state *s)
150 {
151 180 ▶ if (s->a)
Branch: 1 branch
BranchTrueFalseTotal
if (s->a) 117 63 180
152 117 spritz_shuffle (s);
153 180 }
154  
155 uint8_t
156 1485 spritz_output (spritz_state *s)
157 {
158 1485 uint8_t r = s->z + s->k;
159  
160 1485 r = s->i + s->S[r];
161 1485 r = s->j + s->S[r];
162  
163 1485 return s->z = s->S[r];
164 }
165  
166 /* slightly faster internal helper, drip without squeeze preparation */
167 static uint8_t
168 1485 spritz_drip_nosqueeze (spritz_state *s)
169 {
170 1485 spritz_update (s);
171 1485 return spritz_output (s);
172 }
173  
174 void
175 144 spritz_squeeze (spritz_state *s, void *P, size_t P_len)
176 {
177 144 spritz_shuffle_squeeze (s);
178  
179 144 uint8_t *p = (uint8_t *)P;
180  
181 1197 ▶ while (P_len--)
Branch: 1 branch
BranchTrueFalseTotal
while (P_len--) 1053 144 1197
182 1053 *p++ = spritz_drip_nosqueeze (s);
183 144 }
184  
185 uint8_t
186 0 spritz_drip (spritz_state *s)
187 {
188 0 spritz_shuffle_squeeze (s);
189  
190 0 return spritz_drip_nosqueeze (s);
191 }
192  
193 /*****************************************************************************/
194  
195 void
196 36 spritz_cipher_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len)
197 {
198 36 spritz_init (s);
199  
200 36 spritz_absorb (s, K, K_len);
201  
202 36 ▶ if (IV)
Branch: 1 branch
BranchTrueFalseTotal
if (IV) 0 36 36
203 {
204 0 spritz_absorb_stop (s);
205 0 spritz_absorb (s, IV, IV_len);
206 }
207  
208 36 spritz_shuffle_squeeze (s);
209 36 }
210  
211 void
212 9 spritz_cipher_encrypt (spritz_state *s, const void *I, void *O, size_t len)
213 {
214 9 const uint8_t *i = (const uint8_t *)I;
215 9 uint8_t *o = ( uint8_t *)O;
216  
217 153 ▶ while (len--)
Branch: 1 branch
BranchTrueFalseTotal
while (len--) 144 9 153
218 144 *o++ = *i++ + spritz_drip_nosqueeze (s);
219 9 }
220  
221 void
222 9 spritz_cipher_decrypt (spritz_state *s, const void *I, void *O, size_t len)
223 {
224 9 const uint8_t *i = (const uint8_t *)I;
225 9 uint8_t *o = ( uint8_t *)O;
226  
227 153 ▶ while (len--)
Branch: 1 branch
BranchTrueFalseTotal
while (len--) 144 9 153
228 144 *o++ = *i++ - spritz_drip_nosqueeze (s);
229 9 }
230  
231 /*****************************************************************************/
232  
233 void
234 18 spritz_cipher_xor_crypt (spritz_state *s, const void *I, void *O, size_t len)
235 {
236 18 const uint8_t *i = (const uint8_t *)I;
237 18 uint8_t *o = ( uint8_t *)O;
238  
239 162 ▶ while (len--)
Branch: 1 branch
BranchTrueFalseTotal
while (len--) 144 18 162
240 144 *o++ = *i++ ^ spritz_drip_nosqueeze (s);
241 18 }
242  
243 /*****************************************************************************/
244  
245 void
246 27 spritz_hash_finish (spritz_state *s, void *H, size_t H_len)
247 {
248 27 spritz_absorb_stop (s);
249 assert (H_len <= 0xff);
250 27 spritz_absorb_byte (s, H_len);
251  
252 27 spritz_squeeze (s, H, H_len);
253 27 }
254  
255 /*****************************************************************************/
256  
257 void
258 36 spritz_mac_init (spritz_state *s, const void *K, size_t K_len)
259 {
260 36 spritz_init (s);
261 36 spritz_absorb_and_stop (s, K, K_len);
262 36 }
263  
264 /*****************************************************************************/
265  
266 void
267 9 spritz_aead_encrypt (spritz_state *s, const void *I, void *O, size_t len)
268 {
269 9 const uint8_t *i = (const uint8_t *)I;
270 9 uint8_t *o = ( uint8_t *)O;
271  
272 18 ▶ while (len)
Branch: 1 branch
BranchTrueFalseTotal
while (len) 9 9 18
273 {
274 uint_fast8_t j;
275 uint8_t x[spritz_aead_blocksize];
276 9 uint8_t l = len > sizeof (x) ? sizeof (x) : len;
277 9 len -= l;
278  
279 9 spritz_squeeze (s, x, l);
280  
281 153 ▶ for (j = 0; j < l; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; ++j) 144 9 153
282 {
283 144 *o = *i++ + x[j];
284 144 spritz_absorb_byte (s, *o);
285 144 ++o;
286 }
287 }
288 9 }
289  
290 void
291 9 spritz_aead_decrypt (spritz_state *s, const void *I, void *O, size_t len)
292 {
293 9 const uint8_t *i = (const uint8_t *)I;
294 9 uint8_t *o = ( uint8_t *)O;
295  
296 18 ▶ while (len)
Branch: 1 branch
BranchTrueFalseTotal
while (len) 9 9 18
297 {
298 uint_fast8_t j;
299 uint8_t x[spritz_aead_blocksize];
300 9 uint8_t l = len > sizeof (x) ? sizeof (x) : len;
301 9 len -= l;
302  
303 9 spritz_squeeze (s, x, l);
304  
305 153 ▶ for (j = 0; j < l; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; ++j) 144 9 153
306 {
307 144 spritz_absorb_byte (s, *i);
308 144 *o = *i++ - x[j];
309 144 ++o;
310 }
311 }
312 9 }
313  
314 /*****************************************************************************/
315  
316 void
317 9 spritz_aead_xor_crypt (spritz_state *s, const void *I, void *O, size_t len)
318 {
319 9 const uint8_t *i = (const uint8_t *)I;
320 9 uint8_t *o = ( uint8_t *)O;
321  
322 18 ▶ while (len)
Branch: 1 branch
BranchTrueFalseTotal
while (len) 9 9 18
323 {
324 uint_fast8_t j;
325 uint8_t x[spritz_aead_blocksize];
326 9 uint8_t l = len > sizeof (x) ? sizeof (x) : len;
327 9 len -= l;
328  
329 9 spritz_squeeze (s, x, l);
330  
331 81 ▶ for (j = 0; j < l; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; ++j) 72 9 81
332 72 spritz_absorb_byte (s, *o++ = *i++ ^ x[j]);
333 }
334 9 }
335  
336 /*****************************************************************************/
337  
338 void
339 9 spritz_prng_init (spritz_state *s, const void *S, size_t S_len)
340 {
341 9 spritz_init (s);
342 9 spritz_absorb (s, S, S_len);
343 9 }
344  
« Spritz.xs ↑ Summary blib/lib/Crypt/Spritz.pm »