Skip to content

Commit 8a2adc7

Browse files
Merge pull request #124 from formosa-crypto/feature/crypto_hash_sct
crypto_hash implementations: Checking for S-CT
2 parents 3a6c9b4 + 11385db commit 8a2adc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+702
-85
lines changed

src/common/keccak/common/fips202_DIRTY.jinc

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
param int KECCAK_ROUNDS=24;
2-
3-
from Jade require "common/keccak/keccak1600/amd64/avx2/keccak1600.jinc"
4-
from Jade require "common/keccak/keccak1600/amd64/avx2/keccakf1600.jinc"
1+
from Jade require "common/keccak/keccak1600/amd64/avx2/keccak1600_nomsf.jinc"
2+
from Jade require "common/keccak/keccak1600/amd64/avx2/keccakf1600_nomsf.jinc"
53
require "fips202_params.jinc"
64

75
#[returnaddress="stack"]

src/common/keccak/keccak1600/amd64/avx2/keccak1600.jinc

+85-34
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,32 @@ inline fn __add_full_block_avx2(
3333
stack u64[28] s_state,
3434
reg ptr u64[25] a_jagged_p,
3535
reg u64 in inlen,
36-
reg u64 rate
37-
) -> reg u256[7], stack u64[28], reg u64, reg u64
36+
reg u64 rate,
37+
#msf reg u64 ms
38+
) -> reg u256[7], stack u64[28], reg u64, reg u64, #msf reg u64
3839
{
3940

4041
inline int i;
4142
reg u64 j l t rate8;
43+
reg bool loop_condition;
4244

4345
rate8 = rate;
4446
rate8 >>= 3;
4547
j = 0;
46-
while ( j < rate8 )
48+
while { loop_condition = ( j < rate8 ); } ( loop_condition )
4749
{
50+
ms = #update_msf(loop_condition, ms);
51+
4852
t = [in + 8*j];
53+
4954
l = a_jagged_p[(int) j];
55+
l = #protect(l, ms);
56+
5057
s_state[(int) l] = t;
5158
j += 1;
59+
5260
}
61+
ms = #update_msf(!loop_condition, ms);
5362

5463
//TODO: check & change to #VPBROADCAST_4u64
5564
t = s_state[0];
@@ -63,7 +72,7 @@ inline fn __add_full_block_avx2(
6372
in += rate;
6473
inlen -= rate;
6574

66-
return state, s_state, in, inlen;
75+
return state, s_state, in, inlen, ms;
6776
}
6877

6978

@@ -74,42 +83,56 @@ inline fn __add_final_block_avx2(
7483
reg ptr u64[25] a_jagged_p,
7584
reg u64 in inlen,
7685
reg u8 trail_byte,
77-
reg u64 rate
78-
) -> reg u256[7]
86+
reg u64 rate,
87+
#msf reg u64 ms
88+
) -> reg u256[7], #msf reg u64
7989
{
8090
inline int i;
8191
reg u64 j l t inlen8;
8292
reg u8 c;
93+
reg bool loop_condition;
8394

8495
s_state = __init_s_state_avx2();
8596

8697
inlen8 = inlen;
8798
inlen8 >>= 3;
8899
j = 0;
89-
while ( j < inlen8 )
100+
101+
while { loop_condition = (j < inlen8); } ( loop_condition )
90102
{
103+
ms = #update_msf(loop_condition, ms);
104+
91105
t = [in + 8*j];
92106
l = a_jagged_p[(int) j];
107+
l = #protect(l, ms);
108+
93109
s_state[(int) l] = t;
94110
j += 1;
95111
}
112+
ms = #update_msf(!loop_condition, ms);
113+
96114
l = a_jagged_p[(int) j];
115+
l = #protect(l, ms);
116+
97117
l <<= 3;
98118
j <<= 3;
99119

100-
while ( j < inlen )
120+
while { loop_condition = ( j < inlen ); } ( loop_condition )
101121
{
122+
ms = #update_msf(loop_condition, ms);
102123
c = (u8)[in + j];
103124
s_state[u8 (int) l] = c;
104125
j += 1;
105126
l += 1;
106127
}
128+
ms = #update_msf(!loop_condition, ms);
107129

108130
s_state[u8 (int) l] = trail_byte;
109131

110132
// j = (rate-1) >> 3;
111133
j = rate; j -= 1; j >>= 3;
112134
l = a_jagged_p[(int) j];
135+
l = #protect(l, ms);
113136
l <<= 3;
114137
// l += ((rate-1) & 0x7)
115138
j = rate; j -= 1; j &= 0x7;
@@ -125,7 +148,7 @@ inline fn __add_final_block_avx2(
125148
for i = 0 to 7
126149
{ state[i] ^= s_state[u256 i]; }
127150

128-
return state;
151+
return state, ms;
129152
}
130153

131154

@@ -134,30 +157,37 @@ inline fn __xtr_full_block_avx2(
134157
reg u256[7] state,
135158
reg ptr u64[25] a_jagged_p,
136159
reg u64 out,
137-
reg u64 len
138-
) -> reg u64
160+
reg u64 len,
161+
#msf reg u64 ms
162+
) -> reg u64, #msf reg u64
139163
{
140164
inline int i;
141165
stack u64[28] s_state;
142166
reg u64 j l t len8;
167+
reg bool loop_condition;
143168

144169
for i = 0 to 7
145170
{ s_state[u256 i] = state[i]; }
146171

147172
len8 = len;
148173
len8 >>= 3;
149174
j = 0;
150-
while ( j < len8 )
175+
while { loop_condition = ( j < len8 ); } ( loop_condition )
151176
{
177+
ms = #update_msf(loop_condition, ms);
178+
152179
l = a_jagged_p[(int) j];
180+
l = #protect(l, ms);
181+
153182
t = s_state[(int) l];
154183
[out + 8*j] = t;
155184
j += 1;
156185
}
186+
ms = #update_msf(!loop_condition, ms);
157187

158188
out += len;
159189

160-
return out;
190+
return out, ms;
161191
}
162192

163193

@@ -166,27 +196,38 @@ inline fn __xtr_bytes_avx2(
166196
reg u256[7] state,
167197
reg ptr u64[25] a_jagged_p,
168198
reg u64 out,
169-
reg u64 len
199+
reg u64 len,
200+
#msf reg u64 ms
170201
) -> reg u64
171202
{
172203
inline int i;
173204
stack u64[28] s_state;
174205
reg u64 j l t len8;
175206
reg u8 c;
207+
reg bool loop_condition;
176208

177209
for i = 0 to 7
178210
{ s_state[u256 i] = state[i]; }
179211

180212
len8 = len;
181213
len8 >>= 3;
182214
j = 0;
183-
while ( j < len8 )
184-
{ l = a_jagged_p[(int) j];
215+
while { loop_condition = ( j < len8 ); } ( loop_condition )
216+
{
217+
ms = #update_msf(loop_condition, ms);
218+
219+
l = a_jagged_p[(int) j];
220+
l = #protect(l, ms);
221+
185222
t = s_state[(int) l];
186223
[out + 8*j] = t;
187224
j += 1;
188225
}
226+
ms = #update_msf(!loop_condition, ms);
227+
189228
l = a_jagged_p[(int)j];
229+
l = #protect(l, ms);
230+
190231
j <<= 3;
191232
l <<= 3;
192233

@@ -208,65 +249,75 @@ inline fn __absorb_avx2(
208249
reg u256[7] state,
209250
reg u64 in inlen,
210251
reg u8 trail_byte,
211-
reg u64 rate
212-
) -> reg u256[7]
252+
reg u64 rate,
253+
#msf reg u64 ms
254+
) -> reg u256[7], #msf reg u64
213255
{
214256
stack u64[28] s_state;
215257
reg ptr u64[25] a_jagged_p;
258+
reg bool loop_condition;
216259

217260
a_jagged_p = KECCAK_A_JAGGED;
218261
s_state = __init_s_state_avx2();
219262

220263
// intermediate blocks
221-
while ( inlen >= rate )
264+
while { loop_condition = (inlen >= rate); } (loop_condition)
222265
{
223-
state, s_state, in, inlen = __add_full_block_avx2(state, s_state, a_jagged_p, in, inlen, rate);
224-
state = __keccakf1600_avx2(state);
266+
ms = #update_msf(loop_condition, ms);
267+
268+
state, s_state, in, inlen, ms = __add_full_block_avx2(state, s_state, a_jagged_p, in, inlen, rate, ms);
269+
270+
state, ms = __keccakf1600_avx2(state, ms);
225271
}
272+
ms = #update_msf(!loop_condition, ms);
226273

227274
// final block
228-
state = __add_final_block_avx2(state, s_state, a_jagged_p, in, inlen, trail_byte, rate);
275+
state, ms = __add_final_block_avx2(state, s_state, a_jagged_p, in, inlen, trail_byte, rate, ms);
229276

230-
return state;
277+
return state, ms;
231278
}
232279

233280

234-
inline fn __squeeze_avx2(reg u256[7] state, reg u64 out outlen rate)
281+
inline fn __squeeze_avx2(reg u256[7] state, reg u64 out outlen rate, #msf reg u64 ms)
235282
{
236283
reg ptr u64[25] a_jagged_p;
284+
reg bool loop_condition;
237285

238286
a_jagged_p = KECCAK_A_JAGGED;
239287

240288
// intermediate blocks
241-
while ( outlen > rate )
289+
while { loop_condition = (outlen > rate); } ( loop_condition )
242290
{
243-
state = __keccakf1600_avx2(state);
244-
out = __xtr_full_block_avx2(state, a_jagged_p, out, rate);
291+
ms = #update_msf(loop_condition, ms);
292+
293+
state, ms = __keccakf1600_avx2(state, ms);
294+
out, ms = __xtr_full_block_avx2(state, a_jagged_p, out, rate, ms);
245295
outlen -= rate;
246296
}
297+
ms = #update_msf(!loop_condition, ms);
247298

248-
state = __keccakf1600_avx2(state);
249-
out = __xtr_bytes_avx2(state, a_jagged_p, out, outlen);
299+
state, ms = __keccakf1600_avx2(state, ms);
300+
out = __xtr_bytes_avx2(state, a_jagged_p, out, outlen, ms);
250301
}
251302

252303

253-
inline fn __keccak1600_avx2(reg u64 out outlen in inlen, reg u8 trail_byte, reg u64 rate)
304+
inline fn __keccak1600_avx2(reg u64 out outlen in inlen, reg u8 trail_byte, reg u64 rate, #msf reg u64 ms)
254305
{
255306
reg u256[7] state;
256307

257308
state = __keccak_init_avx2();
258309

259310
// absorb
260-
state = __absorb_avx2(state, in, inlen, trail_byte, rate);
311+
state, ms = __absorb_avx2(state, in, inlen, trail_byte, rate, ms);
261312

262313
// squeeze
263-
__squeeze_avx2(state, out, outlen, rate);
314+
__squeeze_avx2(state, out, outlen, rate, ms);
264315
}
265316

266317

267-
fn _keccak1600_avx2(reg u64 out outlen in inlen, reg u8 trail_byte, reg u64 rate)
318+
fn _keccak1600_avx2(reg u64 out outlen in inlen, reg u8 trail_byte, reg u64 rate, #msf reg u64 ms)
268319
{
269-
__keccak1600_avx2(out, outlen, in, inlen, trail_byte, rate);
320+
__keccak1600_avx2(out, outlen, in, inlen, trail_byte, rate, ms);
270321
}
271322

272323

0 commit comments

Comments
 (0)