aboutsummaryrefslogtreecommitdiffstats
path: root/c/sha3.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-02-11 09:40:46 +0100
committerMattias Andrée <maandree@operamail.com>2014-02-11 09:40:46 +0100
commit443d2a176017cba5ec3c38f0e0ebc8d0aa458c74 (patch)
tree99ec5a0651dd2014a90b1cb30202bf36592e46f1 /c/sha3.c
parentadd thread support (diff)
downloadsha3sum-443d2a176017cba5ec3c38f0e0ebc8d0aa458c74.tar.gz
sha3sum-443d2a176017cba5ec3c38f0e0ebc8d0aa458c74.tar.bz2
sha3sum-443d2a176017cba5ec3c38f0e0ebc8d0aa458c74.tar.xz
c version: make with WITH_WIPE=yes to wipe the state and message buffer before freeing
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'c/sha3.c')
-rw-r--r--c/sha3.c43
1 files changed, 43 insertions, 0 deletions
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 */