protected override void SetKey(byte[] keyBytes, byte[] ivBytes)
        {
            base.SetKey(keyBytes, ivBytes);

            if (keyBytes == null || keyBytes.Length != 32)
            {
                throw new ArgumentException($@"{AlgorithmName} requires a 256 bit key");
            }

            if (ivBytes == null || ivBytes.Length != NonceSize)
            {
                throw new ArgumentException($@"{AlgorithmName} requires a 192 bit nonce");
            }

            var nonceInt = Pack.LE_To_UInt32(ivBytes, 0, 6);

            var chachaKey = HChaCha20Internal(keyBytes, nonceInt);

            SetSigma(engineState);
            SetKey(engineState, chachaKey);
            engineState[12] = 1; // Counter
            engineState[13] = 0;
            engineState[14] = nonceInt[4];
            engineState[15] = nonceInt[5];
        }
        private static uint[] HChaCha20Internal(byte[] key, uint[] nonceInt)
        {
            var x      = new uint[16];
            var intKey = Pack.LE_To_UInt32(key, 0, 8);

            SetSigma(x);
            SetKey(x, intKey);
            SetIntNonce(x, nonceInt);
            DoubleRound(x);
            Array.Copy(x, 12, x, 4, 4);
            return(x);
        }
示例#3
0
        protected override void SetKey(byte[] keyBytes, byte[] ivBytes)
        {
            if (keyBytes != null)
            {
                if (keyBytes.Length != 16 && keyBytes.Length != 32)
                {
                    throw new ArgumentException($@"{AlgorithmName} requires 128 bit or 256 bit key");
                }

                PackTauOrSigma(keyBytes.Length, engineState, 0);

                // Key
                Pack.LE_To_UInt32(keyBytes, 0, engineState, 4, 4);
                Pack.LE_To_UInt32(keyBytes, keyBytes.Length - 16, engineState, 8, 4);
            }

            // IV
            Pack.LE_To_UInt32(ivBytes, 0, engineState, 14, 2);
        }
        protected virtual void SetKey(byte[] keyBytes, byte[] ivBytes)
        {
            if (keyBytes != null)
            {
                if ((keyBytes.Length != 16) && (keyBytes.Length != 32))
                {
                    throw new ArgumentException(AlgorithmName + " requires 128 bit or 256 bit key");
                }

                var tsOff = (keyBytes.Length - 16) / 4;
                engineState[0]  = TauSigma[tsOff];
                engineState[5]  = TauSigma[tsOff + 1];
                engineState[10] = TauSigma[tsOff + 2];
                engineState[15] = TauSigma[tsOff + 3];

                // Key
                Pack.LE_To_UInt32(keyBytes, 0, engineState, 1, 4);
                Pack.LE_To_UInt32(keyBytes, keyBytes.Length - 16, engineState, 11, 4);
            }

            // IV
            Pack.LE_To_UInt32(ivBytes, 0, engineState, 6, 2);
        }