git: 882a725bdfc7 - main - siphash: allow zero values for final & len in SipBuf()

From: Warner Losh <imp_at_FreeBSD.org>
Date: Mon, 29 Jul 2024 20:47:49 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=882a725bdfc71b9fc3b70cbcc230327528f3635e

commit 882a725bdfc71b9fc3b70cbcc230327528f3635e
Author:     Mark O'Donovan <shiftee@posteo.net>
AuthorDate: 2024-07-11 17:50:31 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-07-29 20:44:27 +0000

    siphash: allow zero values for final & len in SipBuf()
    
    Currently the assert checks for XOR of final and len.
    This assert fails when running the unit tests in siphash_test.c.
    We need to allow the case where both values are zero.
    
    Signed-off-by: Mark O'Donovan <shiftee@posteo.net>
    Reviewed by: imp, cperciva
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1324
---
 sys/crypto/siphash/siphash.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/sys/crypto/siphash/siphash.c b/sys/crypto/siphash/siphash.c
index 92c87d71d391..89b2b18149ef 100644
--- a/sys/crypto/siphash/siphash.c
+++ b/sys/crypto/siphash/siphash.c
@@ -92,16 +92,21 @@ SipBuf(SIPHASH_CTX *ctx, const uint8_t **src, size_t len, int final)
 {
 	size_t x = 0;
 
-	KASSERT((!final && len > 0) || (final && len == 0),
-	    ("%s: invalid parameters", __func__));
+	/* handle hashing 0 length buffer - needed for test vectors */
+	if (len == 0 && final == 0)
+		return (0);
 
-	if (!final) {
+	if (final) {
+		KASSERT(len == 0, ("%s: invalid len param", __func__));
+		ctx->buf.b8[7] = (uint8_t)ctx->bytes;
+	} else {
+		KASSERT((len > 0) && src && *src,
+			("%s: invalid parameters", __func__));
 		x = MIN(len, sizeof(ctx->buf.b64) - ctx->buflen);
 		bcopy(*src, &ctx->buf.b8[ctx->buflen], x);
 		ctx->buflen += x;
 		*src += x;
-	} else
-		ctx->buf.b8[7] = (uint8_t)ctx->bytes;
+	}
 
 	if (ctx->buflen == 8 || final) {
 		ctx->v[3] ^= le64toh(ctx->buf.b64);