Raven Core  3.0.0
P2P Digital Currency
sph_shabal.c
Go to the documentation of this file.
1 /* $Id: shabal.c 175 2010-05-07 16:03:20Z tp $ */
2 /*
3  * Shabal implementation.
4  *
5  * ==========================(LICENSE BEGIN)============================
6  *
7  * Copyright (c) 2007-2010 Projet RNRT SAPHIR
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * ===========================(LICENSE END)=============================
29  *
30  * @author Thomas Pornin <thomas.pornin@cryptolog.com>
31  */
32 
33 #include <stddef.h>
34 #include <string.h>
35 
36 #include "sph_shabal.h"
37 #ifdef __cplusplus
38 extern "C"{
39 #endif
40 
41 #ifdef _MSC_VER
42 #pragma warning (disable: 4146)
43 #endif
44 
45 /*
46  * Part of this code was automatically generated (the part between
47  * the "BEGIN" and "END" markers).
48  */
49 
50 #define sM 16
51 
52 #define C32 SPH_C32
53 #define T32 SPH_T32
54 
55 #define O1 13
56 #define O2 9
57 #define O3 6
58 
59 /*
60  * We copy the state into local variables, so that the compiler knows
61  * that it can optimize them at will.
62  */
63 
64 /* BEGIN -- automatically generated code. */
65 
66 #define DECL_STATE \
67  sph_u32 A00, A01, A02, A03, A04, A05, A06, A07, \
68  A08, A09, A0A, A0B; \
69  sph_u32 B0, B1, B2, B3, B4, B5, B6, B7, \
70  B8, B9, BA, BB, BC, BD, BE, BF; \
71  sph_u32 C0, C1, C2, C3, C4, C5, C6, C7, \
72  C8, C9, CA, CB, CC, CD, CE, CF; \
73  sph_u32 M0, M1, M2, M3, M4, M5, M6, M7, \
74  M8, M9, MA, MB, MC, MD, ME, MF; \
75  sph_u32 Wlow, Whigh;
76 
77 #define READ_STATE(state) do { \
78  A00 = (state)->A[0]; \
79  A01 = (state)->A[1]; \
80  A02 = (state)->A[2]; \
81  A03 = (state)->A[3]; \
82  A04 = (state)->A[4]; \
83  A05 = (state)->A[5]; \
84  A06 = (state)->A[6]; \
85  A07 = (state)->A[7]; \
86  A08 = (state)->A[8]; \
87  A09 = (state)->A[9]; \
88  A0A = (state)->A[10]; \
89  A0B = (state)->A[11]; \
90  B0 = (state)->B[0]; \
91  B1 = (state)->B[1]; \
92  B2 = (state)->B[2]; \
93  B3 = (state)->B[3]; \
94  B4 = (state)->B[4]; \
95  B5 = (state)->B[5]; \
96  B6 = (state)->B[6]; \
97  B7 = (state)->B[7]; \
98  B8 = (state)->B[8]; \
99  B9 = (state)->B[9]; \
100  BA = (state)->B[10]; \
101  BB = (state)->B[11]; \
102  BC = (state)->B[12]; \
103  BD = (state)->B[13]; \
104  BE = (state)->B[14]; \
105  BF = (state)->B[15]; \
106  C0 = (state)->C[0]; \
107  C1 = (state)->C[1]; \
108  C2 = (state)->C[2]; \
109  C3 = (state)->C[3]; \
110  C4 = (state)->C[4]; \
111  C5 = (state)->C[5]; \
112  C6 = (state)->C[6]; \
113  C7 = (state)->C[7]; \
114  C8 = (state)->C[8]; \
115  C9 = (state)->C[9]; \
116  CA = (state)->C[10]; \
117  CB = (state)->C[11]; \
118  CC = (state)->C[12]; \
119  CD = (state)->C[13]; \
120  CE = (state)->C[14]; \
121  CF = (state)->C[15]; \
122  Wlow = (state)->Wlow; \
123  Whigh = (state)->Whigh; \
124  } while (0)
125 
126 #define WRITE_STATE(state) do { \
127  (state)->A[0] = A00; \
128  (state)->A[1] = A01; \
129  (state)->A[2] = A02; \
130  (state)->A[3] = A03; \
131  (state)->A[4] = A04; \
132  (state)->A[5] = A05; \
133  (state)->A[6] = A06; \
134  (state)->A[7] = A07; \
135  (state)->A[8] = A08; \
136  (state)->A[9] = A09; \
137  (state)->A[10] = A0A; \
138  (state)->A[11] = A0B; \
139  (state)->B[0] = B0; \
140  (state)->B[1] = B1; \
141  (state)->B[2] = B2; \
142  (state)->B[3] = B3; \
143  (state)->B[4] = B4; \
144  (state)->B[5] = B5; \
145  (state)->B[6] = B6; \
146  (state)->B[7] = B7; \
147  (state)->B[8] = B8; \
148  (state)->B[9] = B9; \
149  (state)->B[10] = BA; \
150  (state)->B[11] = BB; \
151  (state)->B[12] = BC; \
152  (state)->B[13] = BD; \
153  (state)->B[14] = BE; \
154  (state)->B[15] = BF; \
155  (state)->C[0] = C0; \
156  (state)->C[1] = C1; \
157  (state)->C[2] = C2; \
158  (state)->C[3] = C3; \
159  (state)->C[4] = C4; \
160  (state)->C[5] = C5; \
161  (state)->C[6] = C6; \
162  (state)->C[7] = C7; \
163  (state)->C[8] = C8; \
164  (state)->C[9] = C9; \
165  (state)->C[10] = CA; \
166  (state)->C[11] = CB; \
167  (state)->C[12] = CC; \
168  (state)->C[13] = CD; \
169  (state)->C[14] = CE; \
170  (state)->C[15] = CF; \
171  (state)->Wlow = Wlow; \
172  (state)->Whigh = Whigh; \
173  } while (0)
174 
175 #define DECODE_BLOCK do { \
176  M0 = sph_dec32le_aligned(buf + 0); \
177  M1 = sph_dec32le_aligned(buf + 4); \
178  M2 = sph_dec32le_aligned(buf + 8); \
179  M3 = sph_dec32le_aligned(buf + 12); \
180  M4 = sph_dec32le_aligned(buf + 16); \
181  M5 = sph_dec32le_aligned(buf + 20); \
182  M6 = sph_dec32le_aligned(buf + 24); \
183  M7 = sph_dec32le_aligned(buf + 28); \
184  M8 = sph_dec32le_aligned(buf + 32); \
185  M9 = sph_dec32le_aligned(buf + 36); \
186  MA = sph_dec32le_aligned(buf + 40); \
187  MB = sph_dec32le_aligned(buf + 44); \
188  MC = sph_dec32le_aligned(buf + 48); \
189  MD = sph_dec32le_aligned(buf + 52); \
190  ME = sph_dec32le_aligned(buf + 56); \
191  MF = sph_dec32le_aligned(buf + 60); \
192  } while (0)
193 
194 #define INPUT_BLOCK_ADD do { \
195  B0 = T32(B0 + M0); \
196  B1 = T32(B1 + M1); \
197  B2 = T32(B2 + M2); \
198  B3 = T32(B3 + M3); \
199  B4 = T32(B4 + M4); \
200  B5 = T32(B5 + M5); \
201  B6 = T32(B6 + M6); \
202  B7 = T32(B7 + M7); \
203  B8 = T32(B8 + M8); \
204  B9 = T32(B9 + M9); \
205  BA = T32(BA + MA); \
206  BB = T32(BB + MB); \
207  BC = T32(BC + MC); \
208  BD = T32(BD + MD); \
209  BE = T32(BE + ME); \
210  BF = T32(BF + MF); \
211  } while (0)
212 
213 #define INPUT_BLOCK_SUB do { \
214  C0 = T32(C0 - M0); \
215  C1 = T32(C1 - M1); \
216  C2 = T32(C2 - M2); \
217  C3 = T32(C3 - M3); \
218  C4 = T32(C4 - M4); \
219  C5 = T32(C5 - M5); \
220  C6 = T32(C6 - M6); \
221  C7 = T32(C7 - M7); \
222  C8 = T32(C8 - M8); \
223  C9 = T32(C9 - M9); \
224  CA = T32(CA - MA); \
225  CB = T32(CB - MB); \
226  CC = T32(CC - MC); \
227  CD = T32(CD - MD); \
228  CE = T32(CE - ME); \
229  CF = T32(CF - MF); \
230  } while (0)
231 
232 #define XOR_W do { \
233  A00 ^= Wlow; \
234  A01 ^= Whigh; \
235  } while (0)
236 
237 #define SWAP(v1, v2) do { \
238  sph_u32 tmp = (v1); \
239  (v1) = (v2); \
240  (v2) = tmp; \
241  } while (0)
242 
243 #define SWAP_BC do { \
244  SWAP(B0, C0); \
245  SWAP(B1, C1); \
246  SWAP(B2, C2); \
247  SWAP(B3, C3); \
248  SWAP(B4, C4); \
249  SWAP(B5, C5); \
250  SWAP(B6, C6); \
251  SWAP(B7, C7); \
252  SWAP(B8, C8); \
253  SWAP(B9, C9); \
254  SWAP(BA, CA); \
255  SWAP(BB, CB); \
256  SWAP(BC, CC); \
257  SWAP(BD, CD); \
258  SWAP(BE, CE); \
259  SWAP(BF, CF); \
260  } while (0)
261 
262 #define PERM_ELT(xa0, xa1, xb0, xb1, xb2, xb3, xc, xm) do { \
263  xa0 = T32((xa0 \
264  ^ (((xa1 << 15) | (xa1 >> 17)) * 5U) \
265  ^ xc) * 3U) \
266  ^ xb1 ^ (xb2 & ~xb3) ^ xm; \
267  xb0 = T32(~(((xb0 << 1) | (xb0 >> 31)) ^ xa0)); \
268  } while (0)
269 
270 #define PERM_STEP_0 do { \
271  PERM_ELT(A00, A0B, B0, BD, B9, B6, C8, M0); \
272  PERM_ELT(A01, A00, B1, BE, BA, B7, C7, M1); \
273  PERM_ELT(A02, A01, B2, BF, BB, B8, C6, M2); \
274  PERM_ELT(A03, A02, B3, B0, BC, B9, C5, M3); \
275  PERM_ELT(A04, A03, B4, B1, BD, BA, C4, M4); \
276  PERM_ELT(A05, A04, B5, B2, BE, BB, C3, M5); \
277  PERM_ELT(A06, A05, B6, B3, BF, BC, C2, M6); \
278  PERM_ELT(A07, A06, B7, B4, B0, BD, C1, M7); \
279  PERM_ELT(A08, A07, B8, B5, B1, BE, C0, M8); \
280  PERM_ELT(A09, A08, B9, B6, B2, BF, CF, M9); \
281  PERM_ELT(A0A, A09, BA, B7, B3, B0, CE, MA); \
282  PERM_ELT(A0B, A0A, BB, B8, B4, B1, CD, MB); \
283  PERM_ELT(A00, A0B, BC, B9, B5, B2, CC, MC); \
284  PERM_ELT(A01, A00, BD, BA, B6, B3, CB, MD); \
285  PERM_ELT(A02, A01, BE, BB, B7, B4, CA, ME); \
286  PERM_ELT(A03, A02, BF, BC, B8, B5, C9, MF); \
287  } while (0)
288 
289 #define PERM_STEP_1 do { \
290  PERM_ELT(A04, A03, B0, BD, B9, B6, C8, M0); \
291  PERM_ELT(A05, A04, B1, BE, BA, B7, C7, M1); \
292  PERM_ELT(A06, A05, B2, BF, BB, B8, C6, M2); \
293  PERM_ELT(A07, A06, B3, B0, BC, B9, C5, M3); \
294  PERM_ELT(A08, A07, B4, B1, BD, BA, C4, M4); \
295  PERM_ELT(A09, A08, B5, B2, BE, BB, C3, M5); \
296  PERM_ELT(A0A, A09, B6, B3, BF, BC, C2, M6); \
297  PERM_ELT(A0B, A0A, B7, B4, B0, BD, C1, M7); \
298  PERM_ELT(A00, A0B, B8, B5, B1, BE, C0, M8); \
299  PERM_ELT(A01, A00, B9, B6, B2, BF, CF, M9); \
300  PERM_ELT(A02, A01, BA, B7, B3, B0, CE, MA); \
301  PERM_ELT(A03, A02, BB, B8, B4, B1, CD, MB); \
302  PERM_ELT(A04, A03, BC, B9, B5, B2, CC, MC); \
303  PERM_ELT(A05, A04, BD, BA, B6, B3, CB, MD); \
304  PERM_ELT(A06, A05, BE, BB, B7, B4, CA, ME); \
305  PERM_ELT(A07, A06, BF, BC, B8, B5, C9, MF); \
306  } while (0)
307 
308 #define PERM_STEP_2 do { \
309  PERM_ELT(A08, A07, B0, BD, B9, B6, C8, M0); \
310  PERM_ELT(A09, A08, B1, BE, BA, B7, C7, M1); \
311  PERM_ELT(A0A, A09, B2, BF, BB, B8, C6, M2); \
312  PERM_ELT(A0B, A0A, B3, B0, BC, B9, C5, M3); \
313  PERM_ELT(A00, A0B, B4, B1, BD, BA, C4, M4); \
314  PERM_ELT(A01, A00, B5, B2, BE, BB, C3, M5); \
315  PERM_ELT(A02, A01, B6, B3, BF, BC, C2, M6); \
316  PERM_ELT(A03, A02, B7, B4, B0, BD, C1, M7); \
317  PERM_ELT(A04, A03, B8, B5, B1, BE, C0, M8); \
318  PERM_ELT(A05, A04, B9, B6, B2, BF, CF, M9); \
319  PERM_ELT(A06, A05, BA, B7, B3, B0, CE, MA); \
320  PERM_ELT(A07, A06, BB, B8, B4, B1, CD, MB); \
321  PERM_ELT(A08, A07, BC, B9, B5, B2, CC, MC); \
322  PERM_ELT(A09, A08, BD, BA, B6, B3, CB, MD); \
323  PERM_ELT(A0A, A09, BE, BB, B7, B4, CA, ME); \
324  PERM_ELT(A0B, A0A, BF, BC, B8, B5, C9, MF); \
325  } while (0)
326 
327 #define APPLY_P do { \
328  B0 = T32(B0 << 17) | (B0 >> 15); \
329  B1 = T32(B1 << 17) | (B1 >> 15); \
330  B2 = T32(B2 << 17) | (B2 >> 15); \
331  B3 = T32(B3 << 17) | (B3 >> 15); \
332  B4 = T32(B4 << 17) | (B4 >> 15); \
333  B5 = T32(B5 << 17) | (B5 >> 15); \
334  B6 = T32(B6 << 17) | (B6 >> 15); \
335  B7 = T32(B7 << 17) | (B7 >> 15); \
336  B8 = T32(B8 << 17) | (B8 >> 15); \
337  B9 = T32(B9 << 17) | (B9 >> 15); \
338  BA = T32(BA << 17) | (BA >> 15); \
339  BB = T32(BB << 17) | (BB >> 15); \
340  BC = T32(BC << 17) | (BC >> 15); \
341  BD = T32(BD << 17) | (BD >> 15); \
342  BE = T32(BE << 17) | (BE >> 15); \
343  BF = T32(BF << 17) | (BF >> 15); \
344  PERM_STEP_0; \
345  PERM_STEP_1; \
346  PERM_STEP_2; \
347  A0B = T32(A0B + C6); \
348  A0A = T32(A0A + C5); \
349  A09 = T32(A09 + C4); \
350  A08 = T32(A08 + C3); \
351  A07 = T32(A07 + C2); \
352  A06 = T32(A06 + C1); \
353  A05 = T32(A05 + C0); \
354  A04 = T32(A04 + CF); \
355  A03 = T32(A03 + CE); \
356  A02 = T32(A02 + CD); \
357  A01 = T32(A01 + CC); \
358  A00 = T32(A00 + CB); \
359  A0B = T32(A0B + CA); \
360  A0A = T32(A0A + C9); \
361  A09 = T32(A09 + C8); \
362  A08 = T32(A08 + C7); \
363  A07 = T32(A07 + C6); \
364  A06 = T32(A06 + C5); \
365  A05 = T32(A05 + C4); \
366  A04 = T32(A04 + C3); \
367  A03 = T32(A03 + C2); \
368  A02 = T32(A02 + C1); \
369  A01 = T32(A01 + C0); \
370  A00 = T32(A00 + CF); \
371  A0B = T32(A0B + CE); \
372  A0A = T32(A0A + CD); \
373  A09 = T32(A09 + CC); \
374  A08 = T32(A08 + CB); \
375  A07 = T32(A07 + CA); \
376  A06 = T32(A06 + C9); \
377  A05 = T32(A05 + C8); \
378  A04 = T32(A04 + C7); \
379  A03 = T32(A03 + C6); \
380  A02 = T32(A02 + C5); \
381  A01 = T32(A01 + C4); \
382  A00 = T32(A00 + C3); \
383  } while (0)
384 
385 #define INCR_W do { \
386  if ((Wlow = T32(Wlow + 1)) == 0) \
387  Whigh = T32(Whigh + 1); \
388  } while (0)
389 
390 static const sph_u32 A_init_192[] = {
391  C32(0xFD749ED4), C32(0xB798E530), C32(0x33904B6F), C32(0x46BDA85E),
392  C32(0x076934B4), C32(0x454B4058), C32(0x77F74527), C32(0xFB4CF465),
393  C32(0x62931DA9), C32(0xE778C8DB), C32(0x22B3998E), C32(0xAC15CFB9)
394 };
395 
396 static const sph_u32 B_init_192[] = {
397  C32(0x58BCBAC4), C32(0xEC47A08E), C32(0xAEE933B2), C32(0xDFCBC824),
398  C32(0xA7944804), C32(0xBF65BDB0), C32(0x5A9D4502), C32(0x59979AF7),
399  C32(0xC5CEA54E), C32(0x4B6B8150), C32(0x16E71909), C32(0x7D632319),
400  C32(0x930573A0), C32(0xF34C63D1), C32(0xCAF914B4), C32(0xFDD6612C)
401 };
402 
403 static const sph_u32 C_init_192[] = {
404  C32(0x61550878), C32(0x89EF2B75), C32(0xA1660C46), C32(0x7EF3855B),
405  C32(0x7297B58C), C32(0x1BC67793), C32(0x7FB1C723), C32(0xB66FC640),
406  C32(0x1A48B71C), C32(0xF0976D17), C32(0x088CE80A), C32(0xA454EDF3),
407  C32(0x1C096BF4), C32(0xAC76224B), C32(0x5215781C), C32(0xCD5D2669)
408 };
409 
410 static const sph_u32 A_init_224[] = {
411  C32(0xA5201467), C32(0xA9B8D94A), C32(0xD4CED997), C32(0x68379D7B),
412  C32(0xA7FC73BA), C32(0xF1A2546B), C32(0x606782BF), C32(0xE0BCFD0F),
413  C32(0x2F25374E), C32(0x069A149F), C32(0x5E2DFF25), C32(0xFAECF061)
414 };
415 
416 static const sph_u32 B_init_224[] = {
417  C32(0xEC9905D8), C32(0xF21850CF), C32(0xC0A746C8), C32(0x21DAD498),
418  C32(0x35156EEB), C32(0x088C97F2), C32(0x26303E40), C32(0x8A2D4FB5),
419  C32(0xFEEE44B6), C32(0x8A1E9573), C32(0x7B81111A), C32(0xCBC139F0),
420  C32(0xA3513861), C32(0x1D2C362E), C32(0x918C580E), C32(0xB58E1B9C)
421 };
422 
423 static const sph_u32 C_init_224[] = {
424  C32(0xE4B573A1), C32(0x4C1A0880), C32(0x1E907C51), C32(0x04807EFD),
425  C32(0x3AD8CDE5), C32(0x16B21302), C32(0x02512C53), C32(0x2204CB18),
426  C32(0x99405F2D), C32(0xE5B648A1), C32(0x70AB1D43), C32(0xA10C25C2),
427  C32(0x16F1AC05), C32(0x38BBEB56), C32(0x9B01DC60), C32(0xB1096D83)
428 };
429 
430 static const sph_u32 A_init_256[] = {
431  C32(0x52F84552), C32(0xE54B7999), C32(0x2D8EE3EC), C32(0xB9645191),
432  C32(0xE0078B86), C32(0xBB7C44C9), C32(0xD2B5C1CA), C32(0xB0D2EB8C),
433  C32(0x14CE5A45), C32(0x22AF50DC), C32(0xEFFDBC6B), C32(0xEB21B74A)
434 };
435 
436 static const sph_u32 B_init_256[] = {
437  C32(0xB555C6EE), C32(0x3E710596), C32(0xA72A652F), C32(0x9301515F),
438  C32(0xDA28C1FA), C32(0x696FD868), C32(0x9CB6BF72), C32(0x0AFE4002),
439  C32(0xA6E03615), C32(0x5138C1D4), C32(0xBE216306), C32(0xB38B8890),
440  C32(0x3EA8B96B), C32(0x3299ACE4), C32(0x30924DD4), C32(0x55CB34A5)
441 };
442 
443 static const sph_u32 C_init_256[] = {
444  C32(0xB405F031), C32(0xC4233EBA), C32(0xB3733979), C32(0xC0DD9D55),
445  C32(0xC51C28AE), C32(0xA327B8E1), C32(0x56C56167), C32(0xED614433),
446  C32(0x88B59D60), C32(0x60E2CEBA), C32(0x758B4B8B), C32(0x83E82A7F),
447  C32(0xBC968828), C32(0xE6E00BF7), C32(0xBA839E55), C32(0x9B491C60)
448 };
449 
450 static const sph_u32 A_init_384[] = {
451  C32(0xC8FCA331), C32(0xE55C504E), C32(0x003EBF26), C32(0xBB6B8D83),
452  C32(0x7B0448C1), C32(0x41B82789), C32(0x0A7C9601), C32(0x8D659CFF),
453  C32(0xB6E2673E), C32(0xCA54C77B), C32(0x1460FD7E), C32(0x3FCB8F2D)
454 };
455 
456 static const sph_u32 B_init_384[] = {
457  C32(0x527291FC), C32(0x2A16455F), C32(0x78E627E5), C32(0x944F169F),
458  C32(0x1CA6F016), C32(0xA854EA25), C32(0x8DB98ABE), C32(0xF2C62641),
459  C32(0x30117DCB), C32(0xCF5C4309), C32(0x93711A25), C32(0xF9F671B8),
460  C32(0xB01D2116), C32(0x333F4B89), C32(0xB285D165), C32(0x86829B36)
461 };
462 
463 static const sph_u32 C_init_384[] = {
464  C32(0xF764B11A), C32(0x76172146), C32(0xCEF6934D), C32(0xC6D28399),
465  C32(0xFE095F61), C32(0x5E6018B4), C32(0x5048ECF5), C32(0x51353261),
466  C32(0x6E6E36DC), C32(0x63130DAD), C32(0xA9C69BD6), C32(0x1E90EA0C),
467  C32(0x7C35073B), C32(0x28D95E6D), C32(0xAA340E0D), C32(0xCB3DEE70)
468 };
469 
470 static const sph_u32 A_init_512[] = {
471  C32(0x20728DFD), C32(0x46C0BD53), C32(0xE782B699), C32(0x55304632),
472  C32(0x71B4EF90), C32(0x0EA9E82C), C32(0xDBB930F1), C32(0xFAD06B8B),
473  C32(0xBE0CAE40), C32(0x8BD14410), C32(0x76D2ADAC), C32(0x28ACAB7F)
474 };
475 
476 static const sph_u32 B_init_512[] = {
477  C32(0xC1099CB7), C32(0x07B385F3), C32(0xE7442C26), C32(0xCC8AD640),
478  C32(0xEB6F56C7), C32(0x1EA81AA9), C32(0x73B9D314), C32(0x1DE85D08),
479  C32(0x48910A5A), C32(0x893B22DB), C32(0xC5A0DF44), C32(0xBBC4324E),
480  C32(0x72D2F240), C32(0x75941D99), C32(0x6D8BDE82), C32(0xA1A7502B)
481 };
482 
483 static const sph_u32 C_init_512[] = {
484  C32(0xD9BF68D1), C32(0x58BAD750), C32(0x56028CB2), C32(0x8134F359),
485  C32(0xB5D469D8), C32(0x941A8CC2), C32(0x418B2A6E), C32(0x04052780),
486  C32(0x7F07D787), C32(0x5194358F), C32(0x3C60D665), C32(0xBE97D79A),
487  C32(0x950C3434), C32(0xAED9A06D), C32(0x2537DC8D), C32(0x7CDB5969)
488 };
489 
490 /* END -- automatically generated code. */
491 
492 static void
493 shabal_init(void *cc, unsigned size)
494 {
495  /*
496  * We have precomputed initial states for all the supported
497  * output bit lengths.
498  */
499  const sph_u32 *A_init, *B_init, *C_init;
500  sph_shabal_context *sc;
501 
502  switch (size) {
503  case 192:
504  A_init = A_init_192;
505  B_init = B_init_192;
506  C_init = C_init_192;
507  break;
508  case 224:
509  A_init = A_init_224;
510  B_init = B_init_224;
511  C_init = C_init_224;
512  break;
513  case 256:
514  A_init = A_init_256;
515  B_init = B_init_256;
516  C_init = C_init_256;
517  break;
518  case 384:
519  A_init = A_init_384;
520  B_init = B_init_384;
521  C_init = C_init_384;
522  break;
523  case 512:
524  A_init = A_init_512;
525  B_init = B_init_512;
526  C_init = C_init_512;
527  break;
528  default:
529  return;
530  }
531  sc = cc;
532  memcpy(sc->A, A_init, sizeof sc->A);
533  memcpy(sc->B, B_init, sizeof sc->B);
534  memcpy(sc->C, C_init, sizeof sc->C);
535  sc->Wlow = 1;
536  sc->Whigh = 0;
537  sc->ptr = 0;
538 }
539 
540 static void
541 shabal_core(void *cc, const unsigned char *data, size_t len)
542 {
543  sph_shabal_context *sc;
544  unsigned char *buf;
545  size_t ptr;
546  DECL_STATE
547 
548  sc = cc;
549  buf = sc->buf;
550  ptr = sc->ptr;
551 
552  /*
553  * We do not want to copy the state to local variables if the
554  * amount of data is less than what is needed to complete the
555  * current block. Note that it is anyway suboptimal to call
556  * this method many times for small chunks of data.
557  */
558  if (len < (sizeof sc->buf) - ptr) {
559  memcpy(buf + ptr, data, len);
560  ptr += len;
561  sc->ptr = ptr;
562  return;
563  }
564 
565  READ_STATE(sc);
566  while (len > 0) {
567  size_t clen;
568 
569  clen = (sizeof sc->buf) - ptr;
570  if (clen > len)
571  clen = len;
572  memcpy(buf + ptr, data, clen);
573  ptr += clen;
574  data += clen;
575  len -= clen;
576  if (ptr == sizeof sc->buf) {
577  DECODE_BLOCK;
579  XOR_W;
580  APPLY_P;
582  SWAP_BC;
583  INCR_W;
584  ptr = 0;
585  }
586  }
587  WRITE_STATE(sc);
588  sc->ptr = ptr;
589 }
590 
591 static void
592 shabal_close(void *cc, unsigned ub, unsigned n, void *dst, unsigned size_words)
593 {
594  sph_shabal_context *sc;
595  unsigned char *buf;
596  size_t ptr;
597  int i;
598  unsigned z;
599  union {
600  unsigned char tmp_out[64];
601  sph_u32 dummy;
602  } u;
603  size_t out_len;
604  DECL_STATE
605 
606  sc = cc;
607  buf = sc->buf;
608  ptr = sc->ptr;
609  z = 0x80 >> n;
610  buf[ptr] = ((ub & -z) | z) & 0xFF;
611  memset(buf + ptr + 1, 0, (sizeof sc->buf) - (ptr + 1));
612  READ_STATE(sc);
613  DECODE_BLOCK;
615  XOR_W;
616  APPLY_P;
617  for (i = 0; i < 3; i ++) {
618  SWAP_BC;
619  XOR_W;
620  APPLY_P;
621  }
622 
623  /*
624  * We just use our local variables; no need to go through
625  * the state structure. In order to share some code, we
626  * emit the relevant words into a temporary buffer, which
627  * we finally copy into the destination array.
628  */
629  switch (size_words) {
630  case 16:
631  sph_enc32le_aligned(u.tmp_out + 0, B0);
632  sph_enc32le_aligned(u.tmp_out + 4, B1);
633  sph_enc32le_aligned(u.tmp_out + 8, B2);
634  sph_enc32le_aligned(u.tmp_out + 12, B3);
635  /* fall through */
636  case 12:
637  sph_enc32le_aligned(u.tmp_out + 16, B4);
638  sph_enc32le_aligned(u.tmp_out + 20, B5);
639  sph_enc32le_aligned(u.tmp_out + 24, B6);
640  sph_enc32le_aligned(u.tmp_out + 28, B7);
641  /* fall through */
642  case 8:
643  sph_enc32le_aligned(u.tmp_out + 32, B8);
644  /* fall through */
645  case 7:
646  sph_enc32le_aligned(u.tmp_out + 36, B9);
647  /* fall through */
648  case 6:
649  sph_enc32le_aligned(u.tmp_out + 40, BA);
650  sph_enc32le_aligned(u.tmp_out + 44, BB);
651  sph_enc32le_aligned(u.tmp_out + 48, BC);
652  sph_enc32le_aligned(u.tmp_out + 52, BD);
653  sph_enc32le_aligned(u.tmp_out + 56, BE);
654  sph_enc32le_aligned(u.tmp_out + 60, BF);
655  break;
656  default:
657  return;
658  }
659  out_len = size_words << 2;
660  memcpy(dst, u.tmp_out + (sizeof u.tmp_out) - out_len, out_len);
661 // shabal_init(sc, size_words << 5);
662 }
663 #if 0
664 /* see sph_shabal.h */
665 void
666 sph_shabal192_init(void *cc)
667 {
668  shabal_init(cc, 192);
669 }
670 
671 /* see sph_shabal.h */
672 void
673 sph_shabal192(void *cc, const void *data, size_t len)
674 {
675  shabal_core(cc, data, len);
676 }
677 
678 /* see sph_shabal.h */
679 void
680 sph_shabal192_close(void *cc, void *dst)
681 {
682  shabal_close(cc, 0, 0, dst, 6);
683 }
684 
685 /* see sph_shabal.h */
686 void
687 sph_shabal192_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
688 {
689  shabal_close(cc, ub, n, dst, 6);
690 }
691 
692 /* see sph_shabal.h */
693 void
694 sph_shabal224_init(void *cc)
695 {
696  shabal_init(cc, 224);
697 }
698 
699 /* see sph_shabal.h */
700 void
701 sph_shabal224(void *cc, const void *data, size_t len)
702 {
703  shabal_core(cc, data, len);
704 }
705 
706 /* see sph_shabal.h */
707 void
708 sph_shabal224_close(void *cc, void *dst)
709 {
710  shabal_close(cc, 0, 0, dst, 7);
711 }
712 
713 /* see sph_shabal.h */
714 void
715 sph_shabal224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
716 {
717  shabal_close(cc, ub, n, dst, 7);
718 }
719 
720 #endif
721 /* see sph_shabal.h */
722 void
724 {
725  shabal_init(cc, 256);
726 }
727 
728 /* see sph_shabal.h */
729 void
730 sph_shabal256(void *cc, const void *data, size_t len)
731 {
732  shabal_core(cc, data, len);
733 }
734 
735 /* see sph_shabal.h */
736 void
737 sph_shabal256_close(void *cc, void *dst)
738 {
739  shabal_close(cc, 0, 0, dst, 8);
740 }
741 
742 /* see sph_shabal.h */
743 void
744 sph_shabal256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
745 {
746  shabal_close(cc, ub, n, dst, 8);
747 }
748 
749 #if 0
750 /* see sph_shabal.h */
751 void
752 sph_shabal384_init(void *cc)
753 {
754  shabal_init(cc, 384);
755 }
756 
757 /* see sph_shabal.h */
758 void
759 sph_shabal384(void *cc, const void *data, size_t len)
760 {
761  shabal_core(cc, data, len);
762 }
763 
764 /* see sph_shabal.h */
765 void
766 sph_shabal384_close(void *cc, void *dst)
767 {
768  shabal_close(cc, 0, 0, dst, 12);
769 }
770 
771 /* see sph_shabal.h */
772 void
773 sph_shabal384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
774 {
775  shabal_close(cc, ub, n, dst, 12);
776 }
777 #endif
778 
779 /* see sph_shabal.h */
780 void
782 {
783  shabal_init(cc, 512);
784 }
785 
786 /* see sph_shabal.h */
787 void
788 sph_shabal512(void *cc, const void *data, size_t len)
789 {
790  shabal_core(cc, data, len);
791 }
792 
793 /* see sph_shabal.h */
794 void
795 sph_shabal512_close(void *cc, void *dst)
796 {
797  shabal_close(cc, 0, 0, dst, 16);
798 }
799 
800 /* see sph_shabal.h */
801 void
802 sph_shabal512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
803 {
804  shabal_close(cc, ub, n, dst, 16);
805 }
806 #ifdef __cplusplus
807 }
808 #endif
void sph_shabal512_init(void *cc)
Initialize a Shabal-512 context.
Definition: sph_shabal.c:781
void sph_shabal384_close(void *cc, void *dst)
Terminate the current Shabal-384 computation and output the result into the provided buffer...
#define APPLY_P
Definition: sph_shabal.c:327
#define READ_STATE(state)
Definition: sph_shabal.c:77
unsigned char buf[64]
Definition: sph_shabal.h:82
void sph_shabal256_init(void *cc)
Initialize a Shabal-256 context.
Definition: sph_shabal.c:723
void sph_shabal192(void *cc, const void *data, size_t len)
Process some data bytes.
void sph_shabal192_init(void *cc)
Initialize a Shabal-192 context.
#define INPUT_BLOCK_ADD
Definition: sph_shabal.c:194
void sph_shabal384_init(void *cc)
Initialize a Shabal-384 context.
#define DECL_STATE
Definition: sph_shabal.c:66
#define INPUT_BLOCK_SUB
Definition: sph_shabal.c:213
Shabal interface.
This structure is a context for Shabal computations: it contains the intermediate values and some dat...
Definition: sph_shabal.h:80
void sph_shabal512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
Add a few additional bits (0 to 7) to the current computation, then terminate it and output the resul...
Definition: sph_shabal.c:802
void sph_shabal224(void *cc, const void *data, size_t len)
Process some data bytes.
#define INCR_W
Definition: sph_shabal.c:385
#define DECODE_BLOCK
Definition: sph_shabal.c:175
void sph_shabal256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
Add a few additional bits (0 to 7) to the current computation, then terminate it and output the resul...
Definition: sph_shabal.c:744
void sph_shabal512_close(void *cc, void *dst)
Terminate the current Shabal-512 computation and output the result into the provided buffer...
Definition: sph_shabal.c:795
#define XOR_W
Definition: sph_shabal.c:232
#define SWAP_BC
Definition: sph_shabal.c:243
void sph_shabal256_close(void *cc, void *dst)
Terminate the current Shabal-256 computation and output the result into the provided buffer...
Definition: sph_shabal.c:737
sph_u32 A[12]
Definition: sph_shabal.h:84
void sph_shabal224_init(void *cc)
Initialize a Shabal-224 context.
#define C32
Definition: sph_shabal.c:52
sph_u32 B[16]
Definition: sph_shabal.h:84
void sph_shabal192_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
Add a few additional bits (0 to 7) to the current computation, then terminate it and output the resul...
void * memcpy(void *a, const void *b, size_t c)
void sph_shabal192_close(void *cc, void *dst)
Terminate the current Shabal-192 computation and output the result into the provided buffer...
unsigned long sph_u32
Definition: sph_types.h:870
void sph_shabal384(void *cc, const void *data, size_t len)
Process some data bytes.
sph_u32 C[16]
Definition: sph_shabal.h:84
void sph_shabal512(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: sph_shabal.c:788
void sph_shabal384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
Add a few additional bits (0 to 7) to the current computation, then terminate it and output the resul...
void sph_shabal224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
Add a few additional bits (0 to 7) to the current computation, then terminate it and output the resul...
#define WRITE_STATE(state)
Definition: sph_shabal.c:126
void sph_shabal224_close(void *cc, void *dst)
Terminate the current Shabal-224 computation and output the result into the provided buffer...
void sph_shabal256(void *cc, const void *data, size_t len)
Process some data bytes.
Definition: sph_shabal.c:730