@@ -179,14 +179,28 @@ def _unpack_sk(self, sk_bytes):
179
179
180
180
def _unpack_h (self , h_bytes ):
181
181
offsets = [0 ] + list (h_bytes [- self .k :])
182
+ # check offsets are monotonic increasing
183
+ if any (offsets [i ] > offsets [i + 1 ] for i in range (len (offsets ) - 1 )):
184
+ raise ValueError ("Offsets in h_bytes are not monotonic increasing" )
185
+ # check offset[-1] is smaller than the length of h_bytes
186
+ if offsets [- 1 ] > self .omega :
187
+ raise ValueError ("Accumulate offset of hints exceeds omega" )
188
+ # check zero fields are all zeros
189
+ if any (b != 0 for b in h_bytes [offsets [- 1 ] : self .omega ]):
190
+ raise ValueError ("Non-zero fields in h_bytes are not all zeros" )
191
+
182
192
non_zero_positions = [
183
193
list (h_bytes [offsets [i ] : offsets [i + 1 ]]) for i in range (self .k )
184
194
]
185
195
186
196
matrix = []
187
197
for poly_non_zero in non_zero_positions :
188
198
coeffs = [0 for _ in range (256 )]
189
- for non_zero in poly_non_zero :
199
+ for i , non_zero in enumerate (poly_non_zero ):
200
+ if i > 0 and non_zero < poly_non_zero [i - 1 ]:
201
+ raise ValueError (
202
+ "Non-zero positions in h_bytes are not monotonic increasing"
203
+ )
190
204
coeffs [non_zero ] = 1
191
205
matrix .append ([self .R (coeffs )])
192
206
return self .M (matrix )
@@ -310,7 +324,11 @@ def _verify_internal(self, pk_bytes, m, sig_bytes):
310
324
following Algorithm 8 (FIPS 204)
311
325
"""
312
326
rho , t1 = self ._unpack_pk (pk_bytes )
313
- c_tilde , z , h = self ._unpack_sig (sig_bytes )
327
+ try :
328
+ c_tilde , z , h = self ._unpack_sig (sig_bytes )
329
+ except ValueError :
330
+ # verify failed if malformed input signature
331
+ return False
314
332
315
333
if h .sum_hint () > self .omega :
316
334
return False
0 commit comments