From 443d2a176017cba5ec3c38f0e0ebc8d0aa458c74 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 11 Feb 2014 09:40:46 +0100 Subject: c version: make with WITH_WIPE=yes to wipe the state and message buffer before freeing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 6 +++++- c/sha3.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e9130aa..ab950c5 100644 --- a/Makefile +++ b/Makefile @@ -20,12 +20,16 @@ JAVADIRS=-s "java" -d "bin/java" -cp "java" JAVAFLAGS=-Xlint $(JAVA_OPTIMISE) JAVA_FLAGS=$(JAVADIRS) $(JAVAFLAGS) +WITH_WIPE=yes CFLAGS=-W{all,extra} -pedantic $(C_OPTIMISE) -fPIC ifeq ($(WITH_C99),yes) CFLAGS+=-std=c99 -DWITH_C99 endif ifeq ($(WITH_THREADLOCAL),yes) - CFLAGS+=-DWITH_THREADLOCAL + CFLAGS+=-DWITH_THREADLOCAL -DWITH_WIPE +endif +ifeq ($(WITH_WIPE),yes) + CFLAGS+=-DWITH_WIPE endif SOFLAGS=-W{all,extra} -pedantic $(C_OPTIMISE) -shared CPPFLAGS= diff --git a/c/sha3.c b/c/sha3.c index 3b0c28e..5ac3144 100644 --- a/c/sha3.c +++ b/c/sha3.c @@ -552,13 +552,24 @@ extern void sha3_initialise(long bitrate, long capacity, long output) */ extern void sha3_dispose() { + #ifdef WITH_WIPE + long i; + #endif if (S != null) { + #ifdef WITH_WIPE + for (i = 0; i < 25; i++) + *(S + i) = 0; + #endif free(S); S = null; } if (M != null) { + #ifdef WITH_WIPE + for (i = 0; i < mlen; i++) + *(M + i) = 0; + #endif free(M); M = null; } @@ -580,7 +591,17 @@ extern void sha3_update(byte* restrict msg, long msglen) long nnn; if (mptr + msglen > mlen) + #ifdef WITH_WIPE M = (byte*)realloc(M, mlen = (mlen + msglen) << 1); + #else + { + long mlen_ = mlen; + char* M_ = (byte*)malloc(mlen = (mlen + msglen) << 1); + sha3_arraycopy(M, 0, M_, 0, mlen_); + free(M); + M = M_; + } + #endif sha3_arraycopy(msg, 0, M, mptr, msglen); len = mptr += msglen; len -= len % ((r * b) >> 3); @@ -620,6 +641,10 @@ extern void sha3_update(byte* restrict msg, long msglen) len -= rr; } + #ifdef WITH_WIPE + for (i = 0; i < nnn; i++) + *(_msg + i) = 0; + #endif free(_msg); } @@ -648,10 +673,24 @@ extern byte* sha3_digest(byte* restrict msg, long msglen, boolean withReturn) else { if (mptr + msglen > mlen) + #ifdef WITH_WIPE M = (byte*)realloc(M, mlen += msglen); + #else + { + long mlen_ = mlen; + char* M_ = (byte*)malloc(mlen += msglen); + sha3_arraycopy(M, 0, M_, 0, mlen_); + free(M); + M = M_; + } + #endif sha3_arraycopy(msg, 0, M, mptr, msglen); message = sha3_pad10star1(M, mptr + msglen, r, &len); } + #ifdef WITH_WIPE + for (i = 0; i < mlen; i++) + *(M + i) = 0; + #endif free(M); M = null; rc = (byte*)malloc(((n + 7) >> 3) * sizeof(byte)); @@ -688,6 +727,10 @@ extern byte* sha3_digest(byte* restrict msg, long msglen, boolean withReturn) len -= rr; } + #ifdef WITH_WIPE + for (i = 0; i < nnn; i++) + *(_msg + i) = 0; + #endif free(_msg); /* Squeezing phase */ -- cgit v1.2.3-70-g09d2