Module Name:    othersrc
Committed By:   agc
Date:           Thu Aug 22 06:32:29 UTC 2013

Modified Files:
        othersrc/external/bsd/multigest/dist: keccak.c keccak.h

Log Message:
minor changes to inline one-line functions, better names for definitions,
use sizeof() to bound memset operations, rather than hardcoding lengths.

no functional changes.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 othersrc/external/bsd/multigest/dist/keccak.c \
    othersrc/external/bsd/multigest/dist/keccak.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/external/bsd/multigest/dist/keccak.c
diff -u othersrc/external/bsd/multigest/dist/keccak.c:1.2 othersrc/external/bsd/multigest/dist/keccak.c:1.3
--- othersrc/external/bsd/multigest/dist/keccak.c:1.2	Sat Aug 17 18:29:35 2013
+++ othersrc/external/bsd/multigest/dist/keccak.c	Thu Aug 22 06:32:29 2013
@@ -47,9 +47,9 @@ fromBytesToWords(uint64_t *stateAsWords,
 {
 	unsigned int i, j;
 
-	for(i=0; i<(KeccakPermutationSize/64); i++) {
+	for(i = 0; i < (KECCAK_PERMUTATION_SIZE_BITS / 64); i++) {
 		stateAsWords[i] = 0;
-		for(j=0; j<(64/8); j++) {
+		for(j = 0; j < (64/8); j++) {
 			stateAsWords[i] |= (uint64_t)(state[i*(64/8)+j]) << (8*j);
 		}
 	}
@@ -60,8 +60,8 @@ fromWordsToBytes(uint8_t *state, const u
 {
 	unsigned int i, j;
 
-	for(i=0; i<(KeccakPermutationSize/64); i++) {
-		for(j=0; j<(64/8); j++) {
+	for(i = 0; i < (KECCAK_PERMUTATION_SIZE_BITS / 64); i++) {
+		for(j = 0; j < (64/8); j++) {
 			state[i*(64/8)+j] = (uint8_t)(stateAsWords[i] >> (8*j)) & 0xFF;
 		}
 	}
@@ -76,17 +76,17 @@ theta(uint64_t *A)
 	unsigned int x, y;
 	uint64_t C[5], D[5];
 
-	for(x=0; x<5; x++) {
+	for(x = 0; x < 5; x++) {
 		C[x] = 0; 
-		for(y=0; y<5; y++) {
+		for(y = 0; y < 5; y++) {
 			C[x] ^= A[INDEX(x, y)];
 		}
 	}
-	for(x=0; x<5; x++) {
+	for(x = 0; x < 5; x++) {
 		D[x] = ROL64(C[(x+1)%5], 1) ^ C[(x+4)%5];
 	}
-	for(x=0; x<5; x++) {
-		for(y=0; y<5; y++) {
+	for(x = 0; x < 5; x++) {
+		for(y = 0; y < 5; y++) {
 			A[INDEX(x, y)] ^= D[x];
 		}
 	}
@@ -97,9 +97,9 @@ rho(KECCAK_CTX *ctx, uint64_t *A)
 {
 	unsigned int x, y;
 
-	for(x=0; x<5; x++) {
-		for(y=0; y<5; y++) {
-			A[INDEX(x, y)] = ROL64(A[INDEX(x, y)], ctx->KeccakRhoOffsets[INDEX(x, y)]);
+	for(x = 0; x < 5; x++) {
+		for(y = 0; y < 5; y++) {
+			A[INDEX(x, y)] = ROL64(A[INDEX(x, y)], ctx->RhoOffsets[INDEX(x, y)]);
 		}
 	}
 }
@@ -110,13 +110,13 @@ pi(uint64_t *A)
 	unsigned int x, y;
 	uint64_t tempA[25];
 
-	for(x=0; x<5; x++) {
-		for(y=0; y<5; y++) {
+	for(x = 0; x < 5; x++) {
+		for(y = 0; y < 5; y++) {
 			tempA[INDEX(x, y)] = A[INDEX(x, y)];
 		}
 	}
-	for(x=0; x<5; x++) {
-		for(y=0; y<5; y++) {
+	for(x = 0; x < 5; x++) {
+		for(y = 0; y < 5; y++) {
 			A[INDEX(0*x+1*y, 2*x+3*y)] = tempA[INDEX(x, y)];
 		}
 	}
@@ -128,11 +128,11 @@ chi(uint64_t *A)
 	unsigned int x, y;
 	uint64_t C[5];
 
-	for(y=0; y<5; y++) { 
-		for(x=0; x<5; x++) {
+	for(y = 0; y < 5; y++) { 
+		for(x = 0; x < 5; x++) {
 			C[x] = A[INDEX(x, y)] ^ ((~A[INDEX(x+1, y)]) & A[INDEX(x+2, y)]);
 		}
-		for(x=0; x<5; x++) {
+		for(x = 0; x < 5; x++) {
 			A[INDEX(x, y)] = C[x];
 		}
 	}
@@ -141,7 +141,7 @@ chi(uint64_t *A)
 static void
 iota(KECCAK_CTX *ctx, uint64_t *A, unsigned int indexRound)
 {
-	A[INDEX(0, 0)] ^= ctx->KeccakRoundConstants[indexRound];
+	A[INDEX(0, 0)] ^= ctx->RoundConstants[indexRound];
 }
 
 static void
@@ -149,7 +149,7 @@ KeccakPermutationOnWords(KECCAK_CTX *ctx
 {
 	unsigned int i;
 
-	for(i=0; i<KeccaknrRounds; i++) {
+	for(i = 0; i < KECCAK_NUM_ROUNDS; i++) {
 		theta(st);
 		rho(ctx, st);
 		pi(st);
@@ -162,7 +162,7 @@ static void
 keccak_permutation(KECCAK_CTX *ctx)
 {
 	const int	indian = 1;
-	uint64_t stateAsWords[KeccakPermutationSize/64];
+	uint64_t stateAsWords[KECCAK_PERMUTATION_SIZE_BITS/64];
 
 	if (*(const char *)(const void *)&indian) {
 		/* little endian */
@@ -179,7 +179,7 @@ KeccakPermutationAfterXor(KECCAK_CTX *ct
 {
 	unsigned int i;
 
-	for(i=0; i<dataLengthInBytes; i++) {
+	for(i = 0; i < dataLengthInBytes; i++) {
 		state[i] ^= data[i];
 	}
 	keccak_permutation(ctx);
@@ -191,7 +191,7 @@ LFSR86540(uint8_t *LFSR)
 	int result = ((*LFSR) & 0x01) != 0;
 
 	if (((*LFSR) & 0x80) != 0) {
-		// Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
+		/* Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1 */
 		(*LFSR) = ((*LFSR) << 1) ^ 0x71;
 	} else {
 		(*LFSR) = (*LFSR) << 1;
@@ -205,12 +205,12 @@ keccak_initialise_RoundConstants(KECCAK_
 	uint8_t LFSRstate = 0x01;
 	unsigned int i, j, bitPosition;
 
-	for(i=0; i<KeccaknrRounds; i++) {
-		ctx->KeccakRoundConstants[i] = 0;
-		for(j=0; j<7; j++) {
-			bitPosition = (1<<j)-1; //2^j-1
+	for(i = 0; i < KECCAK_NUM_ROUNDS; i++) {
+		ctx->RoundConstants[i] = 0;
+		for(j = 0; j < 7; j++) {
+			bitPosition = (1<<j)-1; /*2^j-1 */
 			if (LFSR86540(&LFSRstate)) {
-				ctx->KeccakRoundConstants[i] ^= (uint64_t)1<<bitPosition;
+				ctx->RoundConstants[i] ^= (uint64_t)1<<bitPosition;
 			}
 		}
 	}
@@ -221,11 +221,11 @@ keccak_initialise_RhoOffsets(KECCAK_CTX 
 {
 	unsigned int x, y, t, newX, newY;
 
-	ctx->KeccakRhoOffsets[INDEX(0, 0)] = 0;
+	ctx->RhoOffsets[INDEX(0, 0)] = 0;
 	x = 1;
 	y = 0;
-	for(t=0; t<24; t++) {
-		ctx->KeccakRhoOffsets[INDEX(x, y)] = ((t+1)*(t+2)/2) % 64;
+	for(t = 0; t < 24; t++) {
+		ctx->RhoOffsets[INDEX(x, y)] = ((t+1)*(t+2)/2) % 64;
 		newX = (0*x+1*y) % 5;
 		newY = (2*x+3*y) % 5;
 		x = newX;
@@ -233,31 +233,6 @@ keccak_initialise_RhoOffsets(KECCAK_CTX 
 	}
 }
 
-static void
-keccak_initialize(KECCAK_CTX *ctx)
-{
-	keccak_initialise_RoundConstants(ctx);
-	keccak_initialise_RhoOffsets(ctx);
-}
-
-static void
-keccak_initialise_state(uint8_t *state)
-{
-	memset(state, 0, KeccakPermutationSizeInBytes);
-}
-
-static void
-KeccakAbsorb(KECCAK_CTX *ctx, uint8_t *state, const uint8_t *data, unsigned int laneCount)
-{
-	KeccakPermutationAfterXor(ctx, state, data, laneCount*8);
-}
-
-static void
-keccak_extract(const uint8_t *state, uint8_t *data, unsigned int laneCount)
-{
-	memcpy(data, state, laneCount*8);
-}
-
 static int
 init_sponge(KECCAK_CTX *ctx, unsigned int rate, unsigned int capacity)
 {
@@ -267,12 +242,13 @@ init_sponge(KECCAK_CTX *ctx, unsigned in
 	if ((rate == 0) || (rate >= 1600) || ((rate % 64) != 0)) {
 		return 1;
 	}
-	keccak_initialize(ctx);
+	keccak_initialise_RoundConstants(ctx);
+	keccak_initialise_RhoOffsets(ctx);
 	ctx->rate = rate;
 	ctx->capacity = capacity;
 	ctx->fixedOutputLength = 0;
-	keccak_initialise_state(ctx->state);
-	memset(ctx->dataQueue, 0, KeccakMaximumRateInBytes);
+	memset(ctx->state, 0x0, sizeof(ctx->state));
+	memset(ctx->dataQueue, 0x0, sizeof(ctx->dataQueue));
 	ctx->bitsInQueue = 0;
 	ctx->squeezing = 0;
 	ctx->bitsAvailableForSqueezing = 0;
@@ -282,8 +258,8 @@ init_sponge(KECCAK_CTX *ctx, unsigned in
 static void
 absorb_queue(KECCAK_CTX *ctx)
 {
-	// state->bitsInQueue is assumed to be equal to state->rate
-	KeccakAbsorb(ctx, ctx->state, ctx->dataQueue, ctx->rate/64);
+	/* state->bitsInQueue is assumed to be equal to state->rate */
+	KeccakPermutationAfterXor(ctx, ctx->state, ctx->dataQueue, ctx->rate / 8);
 	ctx->bitsInQueue = 0;
 }
 
@@ -295,17 +271,17 @@ absorb(KECCAK_CTX *ctx, const uint8_t *d
 	const uint8_t *curData;
 
 	if ((ctx->bitsInQueue % 8) != 0) {
-		return 1; // Only the last call may contain a partial byte
+		return 1; /* Only the last call may contain a partial byte */
 	}
 	if (ctx->squeezing) {
-		return 1; // Too late for additional input
+		return 1; /* Too late for additional input */
 	}
 	for (i = 0; i < databitlen ; ) {
 		if ((ctx->bitsInQueue == 0) && (databitlen >= ctx->rate) && (i <= (databitlen-ctx->rate))) {
 			wholeBlocks = (databitlen-i)/ctx->rate;
 			curData = &data[(long)i/8];
 			for(j=0; j<wholeBlocks; j++, curData+=ctx->rate/8) {
-				KeccakAbsorb(ctx, ctx->state, curData, ctx->rate/64);
+				KeccakPermutationAfterXor(ctx, ctx->state, curData, ctx->rate / 8);
 			}
 			i += wholeBlocks*ctx->rate;
 		} else {
@@ -335,7 +311,7 @@ absorb(KECCAK_CTX *ctx, const uint8_t *d
 static void
 PadAndSwitchToSqueezingPhase(KECCAK_CTX *ctx)
 {
-	// Note: the bits are numbered from 0=LSB to 7=MSB
+	/* Note: the bits are numbered from 0=LSB to 7=MSB */
 	if (ctx->bitsInQueue + 1 == ctx->rate) {
 		ctx->dataQueue[ctx->bitsInQueue/8 ] |= 1 << (ctx->bitsInQueue % 8);
 		absorb_queue(ctx);
@@ -346,7 +322,7 @@ PadAndSwitchToSqueezingPhase(KECCAK_CTX 
 	}
 	ctx->dataQueue[(ctx->rate-1)/8] |= 1 << ((ctx->rate-1) % 8);
 	absorb_queue(ctx);
-	keccak_extract(ctx->state, ctx->dataQueue, ctx->rate/64);
+	memcpy(ctx->dataQueue, ctx->state, ctx->rate/8);
 	ctx->bitsAvailableForSqueezing = ctx->rate;
 	ctx->squeezing = 1;
 }
@@ -361,12 +337,12 @@ squeeze(KECCAK_CTX *ctx, uint8_t *output
 		PadAndSwitchToSqueezingPhase(ctx);
 	}
 	if ((outputLength % 8) != 0) {
-		return 1; // Only multiple of 8 bits are allowed, truncation can be done at user level
+		return 1; /* Only multiple of 8 bits are allowed, truncation can be done at user level */
 	}
 	for (i = 0; i < outputLength ; i += partialBlock) {
 		if (ctx->bitsAvailableForSqueezing == 0) {
 			keccak_permutation(ctx);
-			keccak_extract(ctx->state, ctx->dataQueue, ctx->rate/64);
+			memcpy(ctx->dataQueue, ctx->state, ctx->rate/8);
 			ctx->bitsAvailableForSqueezing = ctx->rate;
 		}
 		partialBlock = ctx->bitsAvailableForSqueezing;
@@ -387,7 +363,7 @@ HashReturn
 KECCAK_Init(KECCAK_CTX *ctx, int hashbitlen)
 {
 	switch(hashbitlen) {
-	case 0: // Default parameters, arbitrary length output
+	case 0: /* Default parameters, arbitrary length output */
 		init_sponge((KECCAK_CTX*)ctx, 1024, 576);
 		break;
 	case 224:
@@ -421,7 +397,7 @@ KECCAK_Update(KECCAK_CTX *ctx, const uin
 	if (ret == SUCCESS) {
 		uint8_t lastByte; 
 
-		// Align the last partial byte to the least significant bits
+		/* Align the last partial byte to the least significant bits */
 		lastByte = data[(unsigned long)databitlen/8] >> (8 - (databitlen % 8));
 		return absorb((KECCAK_CTX*)ctx, &lastByte, databitlen % 8);
 	}
Index: othersrc/external/bsd/multigest/dist/keccak.h
diff -u othersrc/external/bsd/multigest/dist/keccak.h:1.2 othersrc/external/bsd/multigest/dist/keccak.h:1.3
--- othersrc/external/bsd/multigest/dist/keccak.h:1.2	Sat Aug 17 18:29:35 2013
+++ othersrc/external/bsd/multigest/dist/keccak.h	Thu Aug 22 06:32:29 2013
@@ -39,13 +39,11 @@ http://creativecommons.org/publicdomain/
 
 #include <inttypes.h>
 
-#define KeccakPermutationSize 1600
-#define KeccakPermutationSizeInBytes (KeccakPermutationSize/8)
-#define KeccakMaximumRate 1536
-#define KeccakMaximumRateInBytes (KeccakMaximumRate/8)
+#define KECCAK_PERMUTATION_SIZE_BITS	1600
+#define KECCAK_MAX_RATE_BITS		1536
 
-#define KeccaknrRounds 24
-#define KeccaknrLanes 25
+#define KECCAK_NUM_ROUNDS		24
+#define KECCAK_NUM_LANES		25
 
 #if defined(__GNUC__)
 #define ALIGN __attribute__ ((aligned(32)))
@@ -56,16 +54,16 @@ http://creativecommons.org/publicdomain/
 #endif
 
 ALIGN typedef struct KECCAK_CTX {
-	ALIGN uint8_t	state[KeccakPermutationSizeInBytes];
-	ALIGN uint8_t	dataQueue[KeccakMaximumRateInBytes];
+	ALIGN uint8_t	state[KECCAK_PERMUTATION_SIZE_BITS / 8];
+	ALIGN uint8_t	dataQueue[KECCAK_MAX_RATE_BITS / 8];
 	uint32_t	rate;
 	uint32_t	capacity;
 	uint32_t	bitsInQueue;
 	uint32_t	fixedOutputLength;
 	int32_t		squeezing;
 	uint32_t	bitsAvailableForSqueezing;
-	uint64_t	KeccakRoundConstants[KeccaknrRounds];
-	uint32_t	KeccakRhoOffsets[KeccaknrLanes];
+	uint64_t	RoundConstants[KECCAK_NUM_ROUNDS];
+	uint32_t	RhoOffsets[KECCAK_NUM_LANES];
 } KECCAK_CTX;
 
 typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;

Reply via email to