/**1* A simple implementation of Blake2b's internal permutation2* in the form of a sponge.3*4* Author: The Lyra PHC team (http://www.lyra-kdf.net/) -- 2014.5*6* This software is hereby placed in the public domain.7*8* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS9* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED10* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE11* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE12* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR13* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF14* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR15* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,16* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE17* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,18* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.19*/20#include <string.h>21#include <stdio.h>22#include <time.h>23#include "Sponge.h"24#include "Lyra2.h"252627/**28* Initializes the Sponge State. The first 512 bits are set to zeros and the remainder29* receive Blake2b's IV as per Blake2b's specification. <b>Note:</b> Even though sponges30* typically have their internal state initialized with zeros, Blake2b's G function31* has a fixed point: if the internal state and message are both filled with zeros. the32* resulting permutation will always be a block filled with zeros; this happens because33* Blake2b does not use the constants originally employed in Blake2 inside its G function,34* relying on the IV for avoiding possible fixed points.35*36* @param state The 1024-bit array to be initialized37*/38void initState(uint64_t state[/*16*/]) {39//First 512 bis are zeros40memset(state, 0, 64);41//Remainder BLOCK_LEN_BLAKE2_SAFE_BYTES are reserved to the IV42state[8] = blake2b_IV[0];43state[9] = blake2b_IV[1];44state[10] = blake2b_IV[2];45state[11] = blake2b_IV[3];46state[12] = blake2b_IV[4];47state[13] = blake2b_IV[5];48state[14] = blake2b_IV[6];49state[15] = blake2b_IV[7];50}5152/**53* Execute Blake2b's G function, with all 12 rounds.54*55* @param v A 1024-bit (16 uint64_t) array to be processed by Blake2b's G function56*/57__inline static void blake2bLyra(uint64_t *v) {58ROUND_LYRA(0);59ROUND_LYRA(1);60ROUND_LYRA(2);61ROUND_LYRA(3);62ROUND_LYRA(4);63ROUND_LYRA(5);64ROUND_LYRA(6);65ROUND_LYRA(7);66ROUND_LYRA(8);67ROUND_LYRA(9);68ROUND_LYRA(10);69ROUND_LYRA(11);70}7172/**73* Executes a reduced version of Blake2b's G function with only one round74* @param v A 1024-bit (16 uint64_t) array to be processed by Blake2b's G function75*/76__inline static void reducedBlake2bLyra(uint64_t *v) {77ROUND_LYRA(0);78}7980/**81* Performs a squeeze operation, using Blake2b's G function as the82* internal permutation83*84* @param state The current state of the sponge85* @param out Array that will receive the data squeezed86* @param len The number of bytes to be squeezed into the "out" array87*/88void squeeze(uint64_t *state, byte *out, unsigned int len)89{90int fullBlocks = len / BLOCK_LEN_BYTES;91byte *ptr = out;92int i;93//Squeezes full blocks94for (i = 0; i < fullBlocks; i++) {95memcpy(ptr, state, BLOCK_LEN_BYTES);96blake2bLyra(state);97ptr += BLOCK_LEN_BYTES;98}99100//Squeezes remaining bytes101memcpy(ptr, state, (len % BLOCK_LEN_BYTES));102}103104/**105* Performs an absorb operation for a single block (BLOCK_LEN_INT64 words106* of type uint64_t), using Blake2b's G function as the internal permutation107*108* @param state The current state of the sponge109* @param in The block to be absorbed (BLOCK_LEN_INT64 words)110*/111void absorbBlock(uint64_t *state, const uint64_t *in)112{113//XORs the first BLOCK_LEN_INT64 words of "in" with the current state114state[0] ^= in[0];115state[1] ^= in[1];116state[2] ^= in[2];117state[3] ^= in[3];118state[4] ^= in[4];119state[5] ^= in[5];120state[6] ^= in[6];121state[7] ^= in[7];122state[8] ^= in[8];123state[9] ^= in[9];124state[10] ^= in[10];125state[11] ^= in[11];126127//Applies the transformation f to the sponge's state128blake2bLyra(state);129}130131/**132* Performs an absorb operation for a single block (BLOCK_LEN_BLAKE2_SAFE_INT64133* words of type uint64_t), using Blake2b's G function as the internal permutation134*135* @param state The current state of the sponge136* @param in The block to be absorbed (BLOCK_LEN_BLAKE2_SAFE_INT64 words)137*/138void absorbBlockBlake2Safe(uint64_t *state, const uint64_t *in)139{140//XORs the first BLOCK_LEN_BLAKE2_SAFE_INT64 words of "in" with the current state141142state[0] ^= in[0];143state[1] ^= in[1];144state[2] ^= in[2];145state[3] ^= in[3];146state[4] ^= in[4];147state[5] ^= in[5];148state[6] ^= in[6];149state[7] ^= in[7];150151//Applies the transformation f to the sponge's state152blake2bLyra(state);153}154155/**156* Performs a reduced squeeze operation for a single row, from the highest to157* the lowest index, using the reduced-round Blake2b's G function as the158* internal permutation159*160* @param state The current state of the sponge161* @param rowOut Row to receive the data squeezed162*/163void reducedSqueezeRow0(uint64_t* state, uint64_t* rowOut, const uint32_t nCols)164{165uint64_t* ptrWord = rowOut + (nCols-1)*BLOCK_LEN_INT64; //In Lyra2: pointer to M[0][C-1]166unsigned int i;167//M[row][C-1-col] = H.reduced_squeeze()168for (i = 0; i < nCols; i++) {169ptrWord[0] = state[0];170ptrWord[1] = state[1];171ptrWord[2] = state[2];172ptrWord[3] = state[3];173ptrWord[4] = state[4];174ptrWord[5] = state[5];175ptrWord[6] = state[6];176ptrWord[7] = state[7];177ptrWord[8] = state[8];178ptrWord[9] = state[9];179ptrWord[10] = state[10];180ptrWord[11] = state[11];181182//Goes to next block (column) that will receive the squeezed data183ptrWord -= BLOCK_LEN_INT64;184185//Applies the reduced-round transformation f to the sponge's state186reducedBlake2bLyra(state);187}188}189190/**191* Performs a reduced duplex operation for a single row, from the highest to192* the lowest index, using the reduced-round Blake2b's G function as the193* internal permutation194*195* @param state The current state of the sponge196* @param rowIn Row to feed the sponge197* @param rowOut Row to receive the sponge's output198*/199void reducedDuplexRow1(uint64_t *state, uint64_t *rowIn, uint64_t *rowOut, const uint32_t nCols)200{201uint64_t* ptrWordIn = rowIn; //In Lyra2: pointer to prev202uint64_t* ptrWordOut = rowOut + (nCols-1)*BLOCK_LEN_INT64; //In Lyra2: pointer to row203unsigned int i;204205for (i = 0; i < nCols; i++) {206207//Absorbing "M[prev][col]"208state[0] ^= (ptrWordIn[0]);209state[1] ^= (ptrWordIn[1]);210state[2] ^= (ptrWordIn[2]);211state[3] ^= (ptrWordIn[3]);212state[4] ^= (ptrWordIn[4]);213state[5] ^= (ptrWordIn[5]);214state[6] ^= (ptrWordIn[6]);215state[7] ^= (ptrWordIn[7]);216state[8] ^= (ptrWordIn[8]);217state[9] ^= (ptrWordIn[9]);218state[10] ^= (ptrWordIn[10]);219state[11] ^= (ptrWordIn[11]);220221//Applies the reduced-round transformation f to the sponge's state222reducedBlake2bLyra(state);223224//M[row][C-1-col] = M[prev][col] XOR rand225ptrWordOut[0] = ptrWordIn[0] ^ state[0];226ptrWordOut[1] = ptrWordIn[1] ^ state[1];227ptrWordOut[2] = ptrWordIn[2] ^ state[2];228ptrWordOut[3] = ptrWordIn[3] ^ state[3];229ptrWordOut[4] = ptrWordIn[4] ^ state[4];230ptrWordOut[5] = ptrWordIn[5] ^ state[5];231ptrWordOut[6] = ptrWordIn[6] ^ state[6];232ptrWordOut[7] = ptrWordIn[7] ^ state[7];233ptrWordOut[8] = ptrWordIn[8] ^ state[8];234ptrWordOut[9] = ptrWordIn[9] ^ state[9];235ptrWordOut[10] = ptrWordIn[10] ^ state[10];236ptrWordOut[11] = ptrWordIn[11] ^ state[11];237238//Input: next column (i.e., next block in sequence)239ptrWordIn += BLOCK_LEN_INT64;240//Output: goes to previous column241ptrWordOut -= BLOCK_LEN_INT64;242}243}244245/**246* Performs a duplexing operation over "M[rowInOut][col] [+] M[rowIn][col]" (i.e.,247* the wordwise addition of two columns, ignoring carries between words). The248* output of this operation, "rand", is then used to make249* "M[rowOut][(N_COLS-1)-col] = M[rowIn][col] XOR rand" and250* "M[rowInOut][col] = M[rowInOut][col] XOR rotW(rand)", where rotW is a 64-bit251* rotation to the left and N_COLS is a system parameter.252*253* @param state The current state of the sponge254* @param rowIn Row used only as input255* @param rowInOut Row used as input and to receive output after rotation256* @param rowOut Row receiving the output257*258*/259void reducedDuplexRowSetup(uint64_t *state, uint64_t *rowIn, uint64_t *rowInOut, uint64_t *rowOut, const uint32_t nCols)260{261uint64_t* ptrWordIn = rowIn; //In Lyra2: pointer to prev262uint64_t* ptrWordInOut = rowInOut; //In Lyra2: pointer to row*263uint64_t* ptrWordOut = rowOut + (nCols-1)*BLOCK_LEN_INT64; //In Lyra2: pointer to row264unsigned int i;265266for (i = 0; i < nCols; i++) {267268//Absorbing "M[prev] [+] M[row*]"269state[0] ^= (ptrWordIn[0] + ptrWordInOut[0]);270state[1] ^= (ptrWordIn[1] + ptrWordInOut[1]);271state[2] ^= (ptrWordIn[2] + ptrWordInOut[2]);272state[3] ^= (ptrWordIn[3] + ptrWordInOut[3]);273state[4] ^= (ptrWordIn[4] + ptrWordInOut[4]);274state[5] ^= (ptrWordIn[5] + ptrWordInOut[5]);275state[6] ^= (ptrWordIn[6] + ptrWordInOut[6]);276state[7] ^= (ptrWordIn[7] + ptrWordInOut[7]);277state[8] ^= (ptrWordIn[8] + ptrWordInOut[8]);278state[9] ^= (ptrWordIn[9] + ptrWordInOut[9]);279state[10] ^= (ptrWordIn[10] + ptrWordInOut[10]);280state[11] ^= (ptrWordIn[11] + ptrWordInOut[11]);281282//Applies the reduced-round transformation f to the sponge's state283reducedBlake2bLyra(state);284285//M[row][col] = M[prev][col] XOR rand286ptrWordOut[0] = ptrWordIn[0] ^ state[0];287ptrWordOut[1] = ptrWordIn[1] ^ state[1];288ptrWordOut[2] = ptrWordIn[2] ^ state[2];289ptrWordOut[3] = ptrWordIn[3] ^ state[3];290ptrWordOut[4] = ptrWordIn[4] ^ state[4];291ptrWordOut[5] = ptrWordIn[5] ^ state[5];292ptrWordOut[6] = ptrWordIn[6] ^ state[6];293ptrWordOut[7] = ptrWordIn[7] ^ state[7];294ptrWordOut[8] = ptrWordIn[8] ^ state[8];295ptrWordOut[9] = ptrWordIn[9] ^ state[9];296ptrWordOut[10] = ptrWordIn[10] ^ state[10];297ptrWordOut[11] = ptrWordIn[11] ^ state[11];298299//M[row*][col] = M[row*][col] XOR rotW(rand)300ptrWordInOut[0] ^= state[11];301ptrWordInOut[1] ^= state[0];302ptrWordInOut[2] ^= state[1];303ptrWordInOut[3] ^= state[2];304ptrWordInOut[4] ^= state[3];305ptrWordInOut[5] ^= state[4];306ptrWordInOut[6] ^= state[5];307ptrWordInOut[7] ^= state[6];308ptrWordInOut[8] ^= state[7];309ptrWordInOut[9] ^= state[8];310ptrWordInOut[10] ^= state[9];311ptrWordInOut[11] ^= state[10];312313//Inputs: next column (i.e., next block in sequence)314ptrWordInOut += BLOCK_LEN_INT64;315ptrWordIn += BLOCK_LEN_INT64;316//Output: goes to previous column317ptrWordOut -= BLOCK_LEN_INT64;318}319}320321/**322* Performs a duplexing operation over "M[rowInOut][col] [+] M[rowIn][col]" (i.e.,323* the wordwise addition of two columns, ignoring carries between words). The324* output of this operation, "rand", is then used to make325* "M[rowOut][col] = M[rowOut][col] XOR rand" and326* "M[rowInOut][col] = M[rowInOut][col] XOR rotW(rand)", where rotW is a 64-bit327* rotation to the left.328*329* @param state The current state of the sponge330* @param rowIn Row used only as input331* @param rowInOut Row used as input and to receive output after rotation332* @param rowOut Row receiving the output333*334*/335void reducedDuplexRow(uint64_t *state, uint64_t *rowIn, uint64_t *rowInOut, uint64_t *rowOut, const uint32_t nCols)336{337uint64_t* ptrWordInOut = rowInOut; //In Lyra2: pointer to row*338uint64_t* ptrWordIn = rowIn; //In Lyra2: pointer to prev339uint64_t* ptrWordOut = rowOut; //In Lyra2: pointer to row340unsigned int i;341342for (i = 0; i < nCols; i++) {343344//Absorbing "M[prev] [+] M[row*]"345state[0] ^= (ptrWordIn[0] + ptrWordInOut[0]);346state[1] ^= (ptrWordIn[1] + ptrWordInOut[1]);347state[2] ^= (ptrWordIn[2] + ptrWordInOut[2]);348state[3] ^= (ptrWordIn[3] + ptrWordInOut[3]);349state[4] ^= (ptrWordIn[4] + ptrWordInOut[4]);350state[5] ^= (ptrWordIn[5] + ptrWordInOut[5]);351state[6] ^= (ptrWordIn[6] + ptrWordInOut[6]);352state[7] ^= (ptrWordIn[7] + ptrWordInOut[7]);353state[8] ^= (ptrWordIn[8] + ptrWordInOut[8]);354state[9] ^= (ptrWordIn[9] + ptrWordInOut[9]);355state[10] ^= (ptrWordIn[10] + ptrWordInOut[10]);356state[11] ^= (ptrWordIn[11] + ptrWordInOut[11]);357358//Applies the reduced-round transformation f to the sponge's state359reducedBlake2bLyra(state);360361//M[rowOut][col] = M[rowOut][col] XOR rand362ptrWordOut[0] ^= state[0];363ptrWordOut[1] ^= state[1];364ptrWordOut[2] ^= state[2];365ptrWordOut[3] ^= state[3];366ptrWordOut[4] ^= state[4];367ptrWordOut[5] ^= state[5];368ptrWordOut[6] ^= state[6];369ptrWordOut[7] ^= state[7];370ptrWordOut[8] ^= state[8];371ptrWordOut[9] ^= state[9];372ptrWordOut[10] ^= state[10];373ptrWordOut[11] ^= state[11];374375//M[rowInOut][col] = M[rowInOut][col] XOR rotW(rand)376ptrWordInOut[0] ^= state[11];377ptrWordInOut[1] ^= state[0];378ptrWordInOut[2] ^= state[1];379ptrWordInOut[3] ^= state[2];380ptrWordInOut[4] ^= state[3];381ptrWordInOut[5] ^= state[4];382ptrWordInOut[6] ^= state[5];383ptrWordInOut[7] ^= state[6];384ptrWordInOut[8] ^= state[7];385ptrWordInOut[9] ^= state[8];386ptrWordInOut[10] ^= state[9];387ptrWordInOut[11] ^= state[10];388389//Goes to next block390ptrWordOut += BLOCK_LEN_INT64;391ptrWordInOut += BLOCK_LEN_INT64;392ptrWordIn += BLOCK_LEN_INT64;393}394}395396/**397* Prints an array of unsigned chars398*/399void printArray(unsigned char *array, unsigned int size, char *name)400{401unsigned int i;402printf("%s: ", name);403for (i = 0; i < size; i++) {404printf("%2x|", array[i]);405}406printf("\n");407}408409////////////////////////////////////////////////////////////////////////////////////////////////410411412