예제 #1
0
        /// <summary>
        /// Serialize a KeyParams class
        /// </summary>
        ///
        /// <param name="KeyObj">A KeyParams class</param>
        ///
        /// <returns>A stream containing the KeyParams data</returns>
        public static Stream Serialize(KeyParams KeyObj)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write(KeyObj.Key != null ? (short)KeyObj.Key.Length : (short)0);
            writer.Write(KeyObj.IV != null ? (short)KeyObj.IV.Length : (short)0);
            writer.Write(KeyObj.IKM != null ? (short)KeyObj.IKM.Length : (short)0);
            writer.Write(KeyObj.ExtKey != null ? (short)KeyObj.ExtKey.Length : (short)0);

            if (KeyObj.Key != null)
            {
                writer.Write(KeyObj.Key);
            }
            if (KeyObj.IV != null)
            {
                writer.Write(KeyObj.IV);
            }
            if (KeyObj.IKM != null)
            {
                writer.Write(KeyObj.IKM);
            }
            if (KeyObj.ExtKey != null)
            {
                writer.Write(KeyObj.ExtKey);
            }

            stream.Seek(0, SeekOrigin.Begin);

            return(stream);
        }
예제 #2
0
        /// <summary>
        /// Compare this KeyParams instance with another
        /// </summary>
        ///
        /// <param name="Obj">KeyParams to compare</param>
        ///
        /// <returns>Returns true if equal</returns>
        public bool Equals(KeyParams Obj)
        {
            if (!Compare.IsEqual(Obj.Key, m_Key))
            {
                return(false);
            }
            if (!Compare.IsEqual(Obj.IV, m_Iv))
            {
                return(false);
            }
            if (!Compare.IsEqual(Obj.IKM, m_Ikm))
            {
                return(false);
            }
            if (!Compare.IsEqual(Obj.ExtKey, m_extKey))
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Initialize the Cipher
        /// </summary>
        ///
        /// <param name="KeyParam">Cipher key container.
        /// <para>Uses the Key and IV fields of KeyParam.
        /// The <see cref="LegalKeySizes"/> property contains valid Key sizes.
        /// IV must be 8 bytes in size.</para>
        /// </param>
        ///
        /// <exception cref="System.ArgumentNullException">Thrown if a null key or iv  is used</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid key or iv size  is used</exception>
        public void Initialize(KeyParams KeyParam)
        {
            // recheck params
            Scope();

            if (KeyParam.IV == null || KeyParam.IV.Length != 8)
            {
                throw new Exception("ChaCha20:Initialize" + "Init parameters must include an IV!", new ArgumentException());
            }
            if (KeyParam.Key == null || KeyParam.Key.Length != 16 && KeyParam.Key.Length != 32)
            {
                throw new Exception("ChaCha20:Initialize" + "Key must be 16 or 32 bytes!", new ArgumentException());
            }
            if (IsParallel && ParallelBlockSize < ParallelMinimumSize || ParallelBlockSize > ParallelMaximumSize)
            {
                throw new Exception("ChaCha20:Initialize" + "The parallel block size is out of bounds!");
            }
            if (IsParallel && ParallelBlockSize % ParallelMinimumSize != 0)
            {
                throw new Exception("ChaCha20:Initialize" + "The parallel block size must be evenly aligned to the ParallelMinimumSize!");
            }

            if (DistributionCode == null)
            {
                if (KeyParam.Key.Length == 16)
                {
                    m_dstCode = (byte[])TAU.Clone();
                }
                else
                {
                    m_dstCode = (byte[])SIGMA.Clone();
                }
            }

            Reset();
            SetKey(KeyParam.Key, KeyParam.IV);

            m_isInitialized = true;
        }