Commit 001fbf80 authored by bernd's avatar bernd

Allow to call KeccakF with different round parameters

parent 3a4b9cb8
......@@ -11,21 +11,21 @@ c-library keccak
s" ./keccak" add-libpath
[THEN]
\c #include <KeccakF-1600.h>
\c UINT64* KeccakEncryptLoop(keccak_state state, UINT64 * data, int n)
\c UINT64* KeccakEncryptLoop(keccak_state state, UINT64 * data, int n, int rounds)
\c {
\c while(n>0) {
\c unsigned int p = n >= 128 ? 128 : n;
\c KeccakF(state);
\c KeccakF(state, rounds);
\c KeccakEncrypt(state, data, p);
\c data = (UINT64*)(((char*)data)+p); n-=p;
\c }
\c return data;
\c }
\c UINT64* KeccakDecryptLoop(keccak_state state, UINT64 * data, int n)
\c UINT64* KeccakDecryptLoop(keccak_state state, UINT64 * data, int n, int rounds)
\c {
\c while(n>0) {
\c unsigned int p = n >= 128 ? 128 : n;
\c KeccakF(state);
\c KeccakF(state, rounds);
\c KeccakDecrypt(state, data, p);
\c data = (UINT64*)(((char*)data)+p); n-=p;
\c }
......@@ -34,14 +34,14 @@ c-library keccak
\ ------===< functions >===-------
c-function KeccakInitialize KeccakInitialize -- void
c-function KeccakF KeccakF a -- void
c-function KeccakF KeccakF a n -- void
c-function KeccakInitializeState KeccakInitializeState a -- void
c-function KeccakExtract KeccakExtract a a n -- void
c-function KeccakAbsorb KeccakAbsorb a a n -- void
c-function KeccakEncrypt KeccakEncrypt a a n -- void
c-function KeccakDecrypt KeccakDecrypt a a n -- void
c-function KeccakEncryptLoop KeccakEncryptLoop a a n -- a
c-function KeccakDecryptLoop KeccakDecryptLoop a a n -- a
c-function KeccakEncryptLoop KeccakEncryptLoop a a n n -- a
c-function KeccakDecryptLoop KeccakDecryptLoop a a n n -- a
end-c-library
......@@ -50,10 +50,11 @@ end-c-library
128 Constant keccak#cks
UValue @keccak
24 Value rounds
: keccak0 ( -- ) @keccak KeccakInitializeState ;
: keccak* ( -- ) @keccak KeccakF ;
: keccak* ( -- ) @keccak rounds KeccakF ;
: >keccak ( addr u -- ) @keccak -rot KeccakAbsorb ;
: +keccak ( addr u -- ) @keccak -rot KeccakEncrypt ;
: -keccak ( addr u -- ) @keccak -rot KeccakDecrypt ;
......@@ -102,22 +103,22 @@ keccak-init
\G perform a diffuse round
:noname ( addr u -- )
\G Encrypt message in buffer addr u
@keccak -rot KeccakEncryptLoop drop
@keccak -rot rounds KeccakEncryptLoop drop
; to c:encrypt
:noname ( addr u -- )
\G Decrypt message in buffer addr u
@keccak -rot KeccakDecryptLoop drop
@keccak -rot rounds KeccakDecryptLoop drop
; to c:decrypt ( addr u -- )
:noname ( addr u tag -- )
\G Encrypt message in buffer addr u with auth
{ tag } @keccak -rot KeccakEncryptLoop
{ tag } @keccak -rot rounds KeccakEncryptLoop
keccak*
>r keccak-checksums keccak#cks keccak>
keccak-checksums tag 7 and 4 lshift + r> $10 move
; to c:encrypt+auth ( addr u tag -- )
:noname ( addr u tag -- flag )
\G Decrypt message in buffer addr u, with auth check
{ tag } @keccak -rot KeccakDecryptLoop
{ tag } @keccak -rot rounds KeccakDecryptLoop
keccak*
128@ keccak-checksums keccak#cks keccak>
keccak-checksums tag 7 and 4 lshift + 128@ 128=
......
......@@ -41,7 +41,7 @@ static const UINT32 KeccakF1600RoundConstants_int2[2*24] =
#undef rounds
#define rounds \
#define rounds(round) \
{ \
UINT32 Da0, De0, Di0, Do0, Du0; \
UINT32 Da1, De1, Di1, Do1, Du1; \
......@@ -72,7 +72,7 @@ static const UINT32 KeccakF1600RoundConstants_int2[2*24] =
\
copyFromState(A, state) \
\
for( i = 12; i != 0; --i ) { \
for( i = round/2; i != 0; --i ) { \
Cx = Abu0^Agu0^Aku0^Amu0^Asu0; \
Du1 = Abe1^Age1^Ake1^Ame1^Ase1; \
Da0 = Cx^ROL32(Du1, 1); \
......
......@@ -271,7 +271,7 @@ void extractLanes(int byteCount, UINT32* state, UINT8 *data)
#endif
void KeccakF(keccak_state state)
void KeccakF(keccak_state state, int round)
{
declareABCDE
#if (Unrolling != 24)
......@@ -279,7 +279,7 @@ void KeccakF(keccak_state state)
#endif
copyFromState(A, state)
rounds
rounds(round)
}
void KeccakInitialize()
......
......@@ -172,7 +172,7 @@ ALIGN const UINT64 rot_39_41[2] = {39, 41};
#include "KeccakF-1600-unrolling.macros"
void KeccakF(keccak_state state)
void KeccakF(keccak_state state, int round)
{
declareABCDE
#if (Unrolling != 24)
......@@ -180,7 +180,7 @@ void KeccakF(keccak_state state)
#endif
copyFromState(A, state)
rounds
rounds(round)
#if defined(UseMMX)
_mm_empty();
#endif
......
......@@ -11,38 +11,10 @@ and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#if (Unrolling == 24)
#define rounds \
#if (Unrolling == 12)
#define rounds(round) \
prepareTheta \
thetaRhoPiChiIotaPrepareTheta( 0, A, E) \
thetaRhoPiChiIotaPrepareTheta( 1, E, A) \
thetaRhoPiChiIotaPrepareTheta( 2, A, E) \
thetaRhoPiChiIotaPrepareTheta( 3, E, A) \
thetaRhoPiChiIotaPrepareTheta( 4, A, E) \
thetaRhoPiChiIotaPrepareTheta( 5, E, A) \
thetaRhoPiChiIotaPrepareTheta( 6, A, E) \
thetaRhoPiChiIotaPrepareTheta( 7, E, A) \
thetaRhoPiChiIotaPrepareTheta( 8, A, E) \
thetaRhoPiChiIotaPrepareTheta( 9, E, A) \
thetaRhoPiChiIotaPrepareTheta(10, A, E) \
thetaRhoPiChiIotaPrepareTheta(11, E, A) \
thetaRhoPiChiIotaPrepareTheta(12, A, E) \
thetaRhoPiChiIotaPrepareTheta(13, E, A) \
thetaRhoPiChiIotaPrepareTheta(14, A, E) \
thetaRhoPiChiIotaPrepareTheta(15, E, A) \
thetaRhoPiChiIotaPrepareTheta(16, A, E) \
thetaRhoPiChiIotaPrepareTheta(17, E, A) \
thetaRhoPiChiIotaPrepareTheta(18, A, E) \
thetaRhoPiChiIotaPrepareTheta(19, E, A) \
thetaRhoPiChiIotaPrepareTheta(20, A, E) \
thetaRhoPiChiIotaPrepareTheta(21, E, A) \
thetaRhoPiChiIotaPrepareTheta(22, A, E) \
thetaRhoPiChiIota(23, E, A) \
copyToState(state, A)
#elif (Unrolling == 12)
#define rounds \
prepareTheta \
for(i=0; i<24; i+=12) { \
for(i=0; i<round; i+=12) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+ 1, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+ 2, A, E) \
......@@ -57,24 +29,10 @@ http://creativecommons.org/publicdomain/zero/1.0/
thetaRhoPiChiIotaPrepareTheta(i+11, E, A) \
} \
copyToState(state, A)
#elif (Unrolling == 8)
#define rounds \
prepareTheta \
for(i=0; i<24; i+=8) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+4, A, E) \
thetaRhoPiChiIotaPrepareTheta(i+5, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+6, A, E) \
thetaRhoPiChiIotaPrepareTheta(i+7, E, A) \
} \
copyToState(state, A)
#elif (Unrolling == 6)
#define rounds \
#define rounds(round) \
prepareTheta \
for(i=0; i<24; i+=6) { \
for(i=0; i<round; i+=6) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
......@@ -84,9 +42,9 @@ http://creativecommons.org/publicdomain/zero/1.0/
} \
copyToState(state, A)
#elif (Unrolling == 4)
#define rounds \
#define rounds(round) \
prepareTheta \
for(i=0; i<24; i+=4) { \
for(i=0; i<round; i+=4) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
......@@ -94,9 +52,9 @@ http://creativecommons.org/publicdomain/zero/1.0/
} \
copyToState(state, A)
#elif (Unrolling == 3)
#define rounds \
#define rounds(round) \
prepareTheta \
for(i=0; i<24; i+=3) { \
for(i=0; i<round; i+=3) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
......@@ -104,17 +62,17 @@ http://creativecommons.org/publicdomain/zero/1.0/
} \
copyToState(state, A)
#elif (Unrolling == 2)
#define rounds \
#define rounds(round) \
prepareTheta \
for(i=0; i<24; i+=2) { \
for(i=0; i<round; i+=2) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
} \
copyToState(state, A)
#elif (Unrolling == 1)
#define rounds \
#define rounds(round) \
prepareTheta \
for(i=0; i<24; i++) { \
for(i=0; i<round; i++) { \
thetaRhoPiChiIotaPrepareTheta(i , A, E) \
copyStateVariables(A, E) \
} \
......
......@@ -13,7 +13,7 @@ typedef unsigned long long int UINT64;
typedef STATEI keccak_state[25*sizeof(UINT64)/sizeof(STATEI)];
void KeccakInitialize();
void KeccakF(keccak_state state);
void KeccakF(keccak_state state, int round);
void KeccakInitializeState(keccak_state state);
void KeccakExtract(keccak_state state, UINT64 *data, int laneCount);
void KeccakAbsorb (keccak_state state, UINT64 *data, int laneCount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment