gvanas/KeccakCodePackage — pull request #10 by coruus (2014-04-22 20:40:39+00:00)
Title: Undefined behavior in Inplace32BI

Fixes undefined behavior due to misaligned loads and stores in the 32-bit in-place implementation. For example:

KeccakF-1600/Optimized/KeccakF-1600-inplace32BI.c:935:13: runtime error: store to misaligned address 0x7fff5cb9c221 for type 'UINT32' (aka 'unsigned int'), which requires 4 byte alignment

Fixed using the following spatch (and a sed script, see the previous commit in this branch):

@ toandxor @
expression pI, pS;
expression t, x0, x1;
@@
-toBitInterleavingAndXOR(*(pI++), *(pI++), *(pS++), *(pS++), t, x0, x1);
+UINT32 low, high;
+memcpy(&low, pI++, 4);
+memcpy(&high, pI++, 4);
+toBitInterleavingAndXOR(low, high, *(pS++), *(pS++), t, x0, x1);

@ fromandxor @
expression pI, pS;
expression t, x0, x1;
@@
-fromBitInterleaving(*(pS++), *(pS++), *(pI++), *(pI++), t, x0, x1);
+UINT32 low, high;
+fromBitInterleaving(*(pS++), *(pS++), low, high, t, x0, x1);
+memcpy(pI++, &low, 4);
+memcpy(pI++, &high, 4);

(Caught by LLVM's UBSan runtime; I'm running a pre-3.5 HEAD.)