From 25fdad62e84a4d1c61a52d5b96dc5325982a30de Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 3 May 2013 20:06:03 +0200 Subject: fix partial byte output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- c/sha3.c | 3 +++ java-c-jni/SHA3.c | 3 +++ pure-java/SHA3.java | 3 +++ python3/sha3sum.py | 4 +++- vala/sha3sum.vala | 3 +++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/c/sha3.c b/c/sha3.c index a5e8f69..1dea0d9 100644 --- a/c/sha3.c +++ b/c/sha3.c @@ -691,6 +691,9 @@ extern byte* digest(byte* msg, long msglen) if (olen > 0) keccakF(S); } + if ((n & 7)) + rc[n >> 3] &= (1 << (n & 7)) - 1; + return rc; } diff --git a/java-c-jni/SHA3.c b/java-c-jni/SHA3.c index 9124743..827458a 100644 --- a/java-c-jni/SHA3.c +++ b/java-c-jni/SHA3.c @@ -680,6 +680,9 @@ byte* digest(byte* msg, jint msglen) if (olen > 0) keccakF(S); } + if ((n & 7)) + rc[n >> 3] &= (1 << (n & 7)) - 1; + return rc; } diff --git a/pure-java/SHA3.java b/pure-java/SHA3.java index 0dbbae9..5dad5c7 100644 --- a/pure-java/SHA3.java +++ b/pure-java/SHA3.java @@ -599,6 +599,9 @@ public class SHA3 if (olen > 0) SHA3.keccakF(S); } + if ((SHA3.n & 7) != 0) + rc[rc.length - 1] &= (1 << (SHA3.n & 7)) - 1 + return rc; } diff --git a/python3/sha3sum.py b/python3/sha3sum.py index 43bdf2f..aa0cced 100755 --- a/python3/sha3sum.py +++ b/python3/sha3sum.py @@ -465,7 +465,7 @@ class SHA3: ptr = 0 rr = SHA3.r >> 3 - nn = (SHA3.n + 3) >> 3 + nn = (SHA3.n + 7) >> 3 ww = SHA3.w >> 3 # Absorbing phase @@ -546,6 +546,8 @@ class SHA3: olen -= SHA3.r if olen > 0: SHA3.keccakF(SHA3.S) + if (SHA3.n & 7) != 0: + rc[len(rc) - 1] &= (1 << (SHA3.n & 7)) - 1 return bytes(rc) diff --git a/vala/sha3sum.vala b/vala/sha3sum.vala index da2e2b7..6339d6a 100644 --- a/vala/sha3sum.vala +++ b/vala/sha3sum.vala @@ -602,6 +602,9 @@ class SHA3 : Object if (olen > 0) this.keccakF(S); } + if ((n & 7) != 0) + rc[n >> 3] &= (1 << (n & 7)) - 1; + return rc; } -- cgit v1.2.3-70-g09d2