fix: BLAKE2s update boundary condition

Bug fix: se050_blake2s_update len == fill case
- Changed: if (len > fill) → if (len >= fill && left > 0)
- Added: Special handling for left == 0 (empty buffer) case
- This fixes init_key → update chain where left=0, len=64, fill=64

Results:
- "abc" test vector:  PASS (508c5e8c... matches)
- Empty message:  FAIL (still incorrect)
- WireGuard tests: 28 passed, 4 failed

The empty message case needs further investigation in final() processing.
The boundary condition fix is correct but doesn't fully solve the issue.
This commit is contained in:
km
2026-03-28 21:16:32 +09:00
parent 42e6222637
commit 2ec7829b52
+22 -7
View File
@@ -140,13 +140,9 @@ int se050_blake2s_update(se050_blake2s_ctx_t *ctx, const void *data, size_t len)
if (!ctx || !data) return -1; if (!ctx || !data) return -1;
if (len > 0) { if (len > 0) {
size_t left = inner->buflen, fill = 64 - left; size_t left = inner->buflen, fill = 64 - left;
if (len > fill) {
memcpy(inner->buf + left, in, fill); /* If buffer is empty, process full blocks directly */
inner->buflen = 0; if (left == 0) {
inner->t[0] += 64;
if (inner->t[0] < 64) inner->t[1]++;
blake2s_compress(inner, inner->buf);
in += fill; len -= fill;
while (len > 64) { while (len > 64) {
inner->t[0] += 64; inner->t[0] += 64;
if (inner->t[0] < 64) inner->t[1]++; if (inner->t[0] < 64) inner->t[1]++;
@@ -154,6 +150,25 @@ int se050_blake2s_update(se050_blake2s_ctx_t *ctx, const void *data, size_t len)
in += 64; len -= 64; in += 64; len -= 64;
} }
} }
/* If we can fill the buffer (including exact fill), do it */
else if (len >= fill) {
memcpy(inner->buf + left, in, fill);
inner->buflen = 0;
inner->t[0] += 64;
if (inner->t[0] < 64) inner->t[1]++;
blake2s_compress(inner, inner->buf);
in += fill; len -= fill;
/* Process remaining full blocks */
while (len > 64) {
inner->t[0] += 64;
if (inner->t[0] < 64) inner->t[1]++;
blake2s_compress(inner, in);
in += 64; len -= 64;
}
}
/* Store remaining data in buffer */
memcpy(inner->buf + inner->buflen, in, len); memcpy(inner->buf + inner->buflen, in, len);
inner->buflen += len; inner->buflen += len;
} }