void AbsorbQueue(hashState *state) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", state->dataQueue, state->bitsInQueue/8); #endif // state->bitsInQueue is assumed to be equal a multiple of 8 memset(state->dataQueue+state->bitsInQueue/8, 0, state->rate/8-state->bitsInQueue/8); #ifdef ProvideFast576 if (state->rate == 576) KeccakAbsorb576bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast832 if (state->rate == 832) KeccakAbsorb832bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1024 if (state->rate == 1024) KeccakAbsorb1024bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1088 if (state->rate == 1088) KeccakAbsorb1088bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1152 if (state->rate == 1152) KeccakAbsorb1152bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1344 if (state->rate == 1344) KeccakAbsorb1344bits(state->state, state->dataQueue); else #endif KeccakAbsorb(state->state, state->dataQueue, state->rate/64); state->bitsInQueue = 0; }
static void AbsorbQueue(spongeState *state) { // state->bitsInQueue is assumed to be equal to state->rate #ifdef KeccakReference displayBytes(1, "Block to be absorbed", state->dataQueue, state->rate/8); #endif #ifdef ProvideFast576 if (state->rate == 576) KeccakAbsorb576bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast832 if (state->rate == 832) KeccakAbsorb832bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1024 if (state->rate == 1024) KeccakAbsorb1024bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1088 if (state->rate == 1088) KeccakAbsorb1088bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1152 if (state->rate == 1152) KeccakAbsorb1152bits(state->state, state->dataQueue); else #endif #ifdef ProvideFast1344 if (state->rate == 1344) KeccakAbsorb1344bits(state->state, state->dataQueue); else #endif KeccakAbsorb(state->state, state->dataQueue, state->rate/64); state->bitsInQueue = 0; }
static int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen) { unsigned long long i, j, wholeBlocks; unsigned int partialBlock, partialByte; const unsigned char *curData; if ((state->bitsInQueue % 8) != 0) return 1; // Only the last call may contain a partial byte if (state->squeezing) return 1; // Too late for additional input i = 0; while(i < databitlen) { if ((state->bitsInQueue == 0) && (databitlen >= state->rate) && (i <= (databitlen-state->rate))) { wholeBlocks = (databitlen-i)/state->rate; curData = data+i/8; #ifdef ProvideFast576 if (state->rate == 576) { for(j=0; j<wholeBlocks; j++, curData+=576/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb576bits(state->state, curData); } } else #endif #ifdef ProvideFast832 if (state->rate == 832) { for(j=0; j<wholeBlocks; j++, curData+=832/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb832bits(state->state, curData); } } else #endif #ifdef ProvideFast1024 if (state->rate == 1024) { for(j=0; j<wholeBlocks; j++, curData+=1024/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1024bits(state->state, curData); } } else #endif #ifdef ProvideFast1088 if (state->rate == 1088) { for(j=0; j<wholeBlocks; j++, curData+=1088/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1088bits(state->state, curData); } } else #endif #ifdef ProvideFast1152 if (state->rate == 1152) { for(j=0; j<wholeBlocks; j++, curData+=1152/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1152bits(state->state, curData); } } else #endif #ifdef ProvideFast1344 if (state->rate == 1344) { for(j=0; j<wholeBlocks; j++, curData+=1344/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1344bits(state->state, curData); } } else #endif { for(j=0; j<wholeBlocks; j++, curData+=state->rate/8) { #ifdef KeccakReference displayBytes(1, "Block to be absorbed", curData, state->rate/8); #endif KeccakAbsorb(state->state, curData, state->rate/64); } } i += wholeBlocks*state->rate; } else { partialBlock = (unsigned int)(databitlen - i); if (partialBlock+state->bitsInQueue > state->rate) partialBlock = state->rate-state->bitsInQueue; partialByte = partialBlock % 8; partialBlock -= partialByte; memcpy(state->dataQueue+state->bitsInQueue/8, data+i/8, partialBlock/8); state->bitsInQueue += partialBlock; i += partialBlock; if (state->bitsInQueue == state->rate) AbsorbQueue(state); if (partialByte > 0) { unsigned char mask = (1 << partialByte)-1; state->dataQueue[state->bitsInQueue/8] = data[i/8] & mask; state->bitsInQueue += partialByte; i += partialByte; } } } return 0; }
HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen) { DataLength i, j; DataLength partialBlock, partialByte, wholeBlocks; BitSequence lastByte; const BitSequence *curData; if ((state->bitsInQueue % 8) != 0) return FAIL; // Only the last call may contain a partial byte if (state->squeezing) return FAIL; // Too late for additional input i = 0; while(i < databitlen) { if ((state->bitsInQueue == 0) && (databitlen >= state->rate) && (i <= (databitlen-state->rate))) { wholeBlocks = (databitlen-i)/state->rate; curData = data+i/8; #ifdef ProvideFast576 if (state->rate == 576) { for(j=0; j<wholeBlocks; j++, curData+=576/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb576bits(state->state, curData); } } else #endif #ifdef ProvideFast832 if (state->rate == 832) { for(j=0; j<wholeBlocks; j++, curData+=832/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb832bits(state->state, curData); } } else #endif #ifdef ProvideFast1024 if (state->rate == 1024) { for(j=0; j<wholeBlocks; j++, curData+=1024/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1024bits(state->state, curData); } } else #endif #ifdef ProvideFast1088 if (state->rate == 1088) { for(j=0; j<wholeBlocks; j++, curData+=1088/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1088bits(state->state, curData); } } else #endif #ifdef ProvideFast1152 if (state->rate == 1152) { for(j=0; j<wholeBlocks; j++, curData+=1152/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1152bits(state->state, curData); } } else #endif #ifdef ProvideFast1344 if (state->rate == 1344) { for(j=0; j<wholeBlocks; j++, curData+=1344/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb1344bits(state->state, curData); } } else #endif { for(j=0; j<wholeBlocks; j++, curData+=state->rate/8) { #ifdef KeccakReference displayBytes(1, "Data to be absorbed", curData, state->rate/8); #endif KeccakAbsorb(state->state, curData, state->rate/64); } } i += wholeBlocks*state->rate; } else { partialBlock = databitlen - i; if (partialBlock+state->bitsInQueue > state->rate) partialBlock = state->rate-state->bitsInQueue; partialByte = partialBlock % 8; partialBlock -= partialByte; memcpy(state->dataQueue+state->bitsInQueue/8, data+i/8, partialBlock/8); state->bitsInQueue += partialBlock; i += partialBlock; if (state->bitsInQueue == state->rate) AbsorbQueue(state); if (partialByte > 0) { // Align the last partial byte to the least significant bits lastByte = data[i/8] >> (8-partialByte); state->dataQueue[state->bitsInQueue/8] = lastByte; state->bitsInQueue += partialByte; i += partialByte; } } }