void SHA3_absorb(struct SHA3Context * ctx, unsigned char * data, unsigned long len) { int n; /* If data was left in buffer, fill with fresh data and absorb */ if (ctx->numbytes != 0) { n = ctx->rsiz - ctx->numbytes; if (len < n) { memcpy(ctx->buffer + ctx->numbytes, data, len); ctx->numbytes += len; return; } memcpy(ctx->buffer + ctx->numbytes, data, n); KeccakAbsorb(ctx->state, ctx->buffer, ctx->rsiz); data += n; len -= n; } /* Absorb data in blocks of [rsiz] bytes */ while (len >= ctx->rsiz) { KeccakAbsorb(ctx->state, data, ctx->rsiz); data += ctx->rsiz; len -= ctx->rsiz; } /* Save remaining data */ if (len > 0) memcpy(ctx->buffer, data, len); ctx->numbytes = len; }
void SHA3_extract(unsigned char padding, struct SHA3Context * ctx, unsigned char * output) { int i, j, n; /* Apply final padding */ n = ctx->numbytes; ctx->buffer[n] = padding; n++; memset(ctx->buffer + n, 0, ctx->rsiz - n); ctx->buffer[ctx->rsiz - 1] |= 0x80; /* Absorb remaining data + padding */ KeccakAbsorb(ctx->state, ctx->buffer, ctx->rsiz); /* Extract hash as low bits of state */ for (i = 0, j = 0; j < ctx->hsiz; i += 1, j += 8) { u64 st = ctx->state[i]; output[j] = st; output[j + 1] = st >> 8; output[j + 2] = st >> 16; output[j + 3] = st >> 24; if (j + 4 >= ctx->hsiz) break; output[j + 4] = st >> 32; output[j + 5] = st >> 40; output[j + 6] = st >> 48; output[j + 7] = st >> 56; } }
int Duplexing(duplexState *state, const unsigned char *in, unsigned int inBitLen, unsigned char *out, unsigned int outBitLen, unsigned int rounds) { KECCAK_ALIGN unsigned char block[KeccakPermutationSizeInBytes]; if (inBitLen > state->rho_max) return 1; if ((inBitLen % 8) != 0) { unsigned char mask = ~((1 << (inBitLen % 8)) - 1); if ((in[inBitLen/8] & mask) != 0) return 1; // The bits of the last incomplete byte must be aligned on the LSB } if (outBitLen > state->rate) return 1; // The output length must not be greater than the rate memcpy(block, in, (inBitLen+7)/8); memset(block+(inBitLen+7)/8, 0, ((state->rate+63)/64)*8 - (inBitLen+7)/8); block[inBitLen/8] |= 1 << (inBitLen%8); block[(state->rate-1)/8] |= 1 << ((state->rate-1) % 8); #ifdef KeccakReference displayBytes(1, "Block to be absorbed (after padding)", block, (state->rate+7)/8); #endif KeccakAbsorb(state->state, block, (state->rate+63)/64, rounds); KeccakExtract(state->state, block, (state->rate+63)/64); memcpy(out, block, (outBitLen+7)/8); if ((outBitLen % 8) != 0) { unsigned char mask = (1 << (outBitLen % 8)) - 1; out[outBitLen/8] &= mask; } return 0; }
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; }
int main() { UINT32 x = 0; int i; { UINT32 state[50]; const UINT32 imageOfAllZero[50] = { 0xD33D89FB, 0xC4B60CAD, 0x2FAD58B0, 0x88AE581B, 0xF4262C1A, 0x8A53D3EF, 0x77B4B09B, 0xE0147822, 0x10A38DCF, 0xB6305181, 0xF723F2BE, 0xF9C67B78, 0x4EB02ABA, 0x8FCCC118, 0x2DC2E52E, 0xA3B29275, 0x342F5536, 0xE4DD320A, 0x45C7C3EA, 0x493D8BE4, 0x9C1717E7, 0xF3E75194, 0x12A23D11, 0xEDD52441, 0x13E6DBFF, 0x8C61BB03, 0x945B1B82, 0x1E4A11A5, 0x1C3453E7, 0x0D730C1B, 0x3B9C1D29, 0x0C534AF4, 0xA6EC29CC, 0x4FFDAA4D, 0x96C7DAA5, 0x45487850, 0x4ECFBC29, 0xE630383B, 0x26806B48, 0xA7EB2B5A, 0x62D02426, 0x8265F750, 0x49D20B1A, 0x20E4D82C, 0x6F72B2B8, 0x1C45D049, 0xFEA9F415, 0x4D0E74C7, 0x8DFDEA09, 0xFCF72ED2 }; // Test 1 (all-zero state through Keccak-f[1600]) memset(state, 0, 50*sizeof(UINT32)); KeccakPermutation((unsigned char*)state); for(i=0; i<50; i++) if (state[i] != imageOfAllZero[i]) for( ; ; ) { // Kaccek (aka other algo) x++; } // For benchmarking { #ifdef ProvideFast1024 KeccakAbsorb1024bits((unsigned char*)state, (unsigned char*)imageOfAllZero); #else KeccakAbsorb((unsigned char*)state, (unsigned char*)imageOfAllZero, 16); #endif } } { hashState state; const UINT8 Msg29[4] = { 0x53, 0x58, 0x7B, 0xC8 }; const UINT8 Msg29_out[160] = { 0xDE, 0xEB, 0xFB, 0x5F, 0xBC, 0x67, 0x14, 0x3A, 0x70, 0xF5, 0xEE, 0x51, 0x8F, 0x3C, 0xE2, 0x0A, 0x70, 0x2A, 0x3C, 0x25, 0x0C, 0x22, 0xD9, 0x39, 0xD7, 0xEE, 0xF5, 0x98, 0xA3, 0x9C, 0xA5, 0xC5, 0x37, 0x41, 0xB6, 0xF5, 0x7B, 0x58, 0x40, 0xAD, 0xD2, 0x8E, 0xF6, 0x14, 0x0A, 0xAD, 0x9D, 0x4C, 0x2B, 0x8E, 0xCC, 0x6A, 0x89, 0xFC, 0x5E, 0xFE, 0x73, 0x1F, 0x5E, 0x69, 0x7B, 0x83, 0xB8, 0x1C, 0x27, 0xED, 0xE0, 0xD2, 0x26, 0xBB, 0x30, 0xDE, 0x0A, 0x93, 0xF5, 0xCE, 0xDB, 0xC1, 0x6E, 0x32, 0xBA, 0x9D, 0x6B, 0x10, 0x48, 0x8A, 0x5A, 0x0E, 0x55, 0x5C, 0xB2, 0x96, 0x9F, 0x51, 0xE5, 0x8D, 0x46, 0xF0, 0x03, 0xF5, 0x0F, 0x9D, 0x84, 0x5A, 0xAF, 0x43, 0x07, 0x66, 0x76, 0x23, 0x82, 0xAD, 0xFD, 0x9B, 0x4C, 0xF0, 0x59, 0x16, 0xDF, 0xD6, 0x5C, 0x8A, 0x8C, 0xFC, 0xDE, 0xC5, 0xD0, 0x45, 0x34, 0x07, 0x38, 0x7D, 0xBC, 0xF3, 0xA7, 0x44, 0x26, 0x8E, 0x85, 0xB3, 0x5B, 0x50, 0x0E, 0xDD, 0x1E, 0xD5, 0x09, 0x01, 0x55, 0xA6, 0x35, 0xBF, 0xA4, 0x6A, 0xC2, 0x4D, 0xA7, 0x98, 0xE8, 0x24 }; UINT8 output[160]; // Test 2 (message of length 29 from ShortMsgKAT_0.txt) Init(&state, 0); Update(&state, Msg29, 29); Final(&state, 0); Squeeze(&state, output, 160*8); for(i=0; i<160; i++) if (output[i] != Msg29_out[i]) for( ; ; ) { // Kaccek (aka other algo) x++; } } for ( ; ; ) ; }
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; } } }