Esempio n. 1
0
        public byte[][] SplitBlocks(byte[] paddedInputBits)
        {
            Debug.Assert(paddedInputBits.Length % rate == 0);
            int numberOfBlocks = paddedInputBits.Length / rate;

            byte[][] inputBlocks = null;
            byte[]   block;

            /* split message into blocks of size `rate` */
            List <byte[]> inputBlockList = new List <byte[]>(numberOfBlocks);

            for (int i = 0; i < numberOfBlocks; i++)
            {
                block = KeccakHashFunction.SubArray(paddedInputBits, i * rate, rate);
                inputBlockList.Add(block);
            }

            inputBlocks = inputBlockList.ToArray();

            return(inputBlocks);
        }
Esempio n. 2
0
        public byte[] Squeeze(int outputLength)
        {
            byte[] output = new byte[outputLength];

            #if _DEBUG_
            Console.WriteLine("\n#Sponge: begin squeezing phase");
            #endif

            if (outputLength <= rate)
            {
                #if _DEBUG_
                Console.WriteLine("#Sponge: the output length is smaller or equal to the bit rate size ({0} <= {1})", outputLength, rate);
                Console.WriteLine("#Sponge: -> squeeze output from state");
                #endif

                /* append `outputLength` bits of the state to the output */
                output = KeccakHashFunction.SubArray(state, 0, outputLength);

                /* presentation squeezing phase*/
                #region presentation squeezing phase
                if (pres.IsVisible && !pres.skipPresentation)
                {
                    pres.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        pres.labelRound.Content = "";
                        pres.labelBlock.Content = "";
                    }, null);
                }

                if (pres.IsVisible && !pres.skipPresentation)
                {
                    string stateStr  = KeccakHashFunction.GetByteArrayAsString(state, laneSize);
                    string outputStr = KeccakHashFunction.GetByteArrayAsString(output, laneSize);

                    pres.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        pres.textBlockExplanation.Text = string.Format(Resources.PresSqueezingPhaseExplanation, outputLength); // If the hash value is larger than this part the state is repeatedly permuted by Keccak-f such that sufficient output can be extracted.", outputLength);

                        pres.imgBlankPage.Visibility         = Visibility.Visible;
                        pres.labelCurrentPhase.Content       = Resources.PresSqueezingPhase;
                        pres.labelOutput.Visibility          = Visibility.Visible;
                        pres.textBlockStateBeforeAbsorb.Text = stateStr;
                        pres.absorbGrid.Visibility           = Visibility.Visible;
                        pres.labelOldState.Content           = Resources.PresState;

                        pres.textBlockStateAfterAbsorb.Text = outputStr;
                    }, null);
                }
                #endregion
            }
            else
            {
                int remainingOutput = outputLength, i = 0;

                #if _DEBUG_
                int squeezingRounds = remainingOutput % rate == 0 ? (int)(remainingOutput / rate - 1) : (remainingOutput / rate);
                Console.WriteLine("#Sponge: the output length is larger than the bit rate ({0} > {1})", outputLength, rate);
                Console.WriteLine("#Sponge: -> squeeze output from state iteratively ({0} iteration(s) required)\n", squeezingRounds);
                #endif

                /* append size of `rate` bits of the state to the output */
                while (remainingOutput > rate)
                {
                    Array.Copy(state, 0, output, i++ *rate, rate);

                    #if _DEBUG_
                    Console.WriteLine("#Sponge: squeeze iteration #{0}\n", i);
                    #endif

                    remainingOutput -= rate;

                    plugin.ProgressChanged((double)progressionStepCounter + (1.0 / 6.0), (double)progressionSteps);
                    keccak_f.Permute(ref state, progressionStepCounter, progressionSteps);
                    progressionStepCounter++;
                }

                if (remainingOutput > 0)
                {
                    /* append remaining bits of the state to the output to fit the output length */
                    Array.Copy(state, 0, output, i * rate, remainingOutput);
                }
            }

            #if _DEBUG_
            Console.WriteLine("#Sponge: squeezing done!\n");
            #endif

            if (pres.IsVisible)
            {
                pres.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                {
                    pres.spInfo.Visibility = Visibility.Hidden;
                    //pres.labelCurrentStep.Content = "";
                    //pres.labelCurrentPhase.Content = "";
                }, null);
            }

            return(output);
        }