Skip to content

Commit 45e5f4e

Browse files
authored
Merge pull request #15 from yodalee/check_hint_field_in_unpack_h
Check hint format in unpack_h method
2 parents 5f32539 + 9c4ae29 commit 45e5f4e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/dilithium_py/ml_dsa/ml_dsa.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,28 @@ def _unpack_sk(self, sk_bytes):
179179

180180
def _unpack_h(self, h_bytes):
181181
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+
182192
non_zero_positions = [
183193
list(h_bytes[offsets[i] : offsets[i + 1]]) for i in range(self.k)
184194
]
185195

186196
matrix = []
187197
for poly_non_zero in non_zero_positions:
188198
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+
)
190204
coeffs[non_zero] = 1
191205
matrix.append([self.R(coeffs)])
192206
return self.M(matrix)
@@ -310,7 +324,11 @@ def _verify_internal(self, pk_bytes, m, sig_bytes):
310324
following Algorithm 8 (FIPS 204)
311325
"""
312326
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
314332

315333
if h.sum_hint() > self.omega:
316334
return False

0 commit comments

Comments
 (0)