Esempio n. 1
0
        public override byte[] Transform(byte[] pbMsg, KdfParameters p)
        {
            if(pbMsg == null) throw new ArgumentNullException("pbMsg");
            if(p == null) throw new ArgumentNullException("p");

            Type tRounds = p.GetTypeOf(ParamRounds);
            if(tRounds == null) throw new ArgumentNullException("p.Rounds");
            if(tRounds != typeof(ulong)) throw new ArgumentOutOfRangeException("p.Rounds");
            ulong uRounds = p.GetUInt64(ParamRounds, 0);

            byte[] pbSeed = p.GetByteArray(ParamSeed);
            if(pbSeed == null) throw new ArgumentNullException("p.Seed");

            if(pbMsg.Length != 32)
            {
                Debug.Assert(false);
                pbMsg = CryptoUtil.HashSha256(pbMsg);
            }

            if(pbSeed.Length != 32)
            {
                Debug.Assert(false);
                pbSeed = CryptoUtil.HashSha256(pbSeed);
            }

            return TransformKey(pbMsg, pbSeed, uRounds);
        }
Esempio n. 2
0
		public override byte[] Transform(byte[] pbMsg, KdfParameters p)
		{
			if(pbMsg == null) throw new ArgumentNullException("pbMsg");
			if(p == null) throw new ArgumentNullException("p");

			byte[] pbSalt = p.GetByteArray(ParamSalt);
			if(pbSalt == null)
				throw new ArgumentNullException("p.Salt");
			if((pbSalt.Length < MinSalt) || (pbSalt.Length > MaxSalt))
				throw new ArgumentOutOfRangeException("p.Salt");

			uint uPar = p.GetUInt32(ParamParallelism, 0);
			if((uPar < MinParallelism) || (uPar > MaxParallelism))
				throw new ArgumentOutOfRangeException("p.Parallelism");

			ulong uMem = p.GetUInt64(ParamMemory, 0);
			if((uMem < MinMemory) || (uMem > MaxMemory))
				throw new ArgumentOutOfRangeException("p.Memory");

			ulong uIt = p.GetUInt64(ParamIterations, 0);
			if((uIt < MinIterations) || (uIt > MaxIterations))
				throw new ArgumentOutOfRangeException("p.Iterations");

			uint v = p.GetUInt32(ParamVersion, 0);
			if((v < MinVersion) || (v > MaxVersion))
				throw new ArgumentOutOfRangeException("p.Version");

			byte[] pbSecretKey = p.GetByteArray(ParamSecretKey);
			byte[] pbAssocData = p.GetByteArray(ParamAssocData);

			byte[] pbRet = Argon2d(pbMsg, pbSalt, uPar, uMem, uIt,
				32, v, pbSecretKey, pbAssocData);

			if(uMem > (100UL * 1024UL * 1024UL)) GC.Collect();
			return pbRet;
		}
Esempio n. 3
0
        public override byte[] Transform(byte[] pbMsg, KdfParameters p)
        {
            if (pbMsg == null)
            {
                throw new ArgumentNullException("pbMsg");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }

            byte[] pbSalt = p.GetByteArray(ParamSalt);
            if (pbSalt == null)
            {
                throw new ArgumentNullException("p.Salt");
            }
            if ((pbSalt.Length < MinSalt) || (pbSalt.Length > MaxSalt))
            {
                throw new ArgumentOutOfRangeException("p.Salt");
            }

            uint uPar = p.GetUInt32(ParamParallelism, 0);

            if ((uPar < MinParallelism) || (uPar > MaxParallelism))
            {
                throw new ArgumentOutOfRangeException("p.Parallelism");
            }

            ulong uMem = p.GetUInt64(ParamMemory, 0);

            if ((uMem < MinMemory) || (uMem > MaxMemory))
            {
                throw new ArgumentOutOfRangeException("p.Memory");
            }

            ulong uIt = p.GetUInt64(ParamIterations, 0);

            if ((uIt < MinIterations) || (uIt > MaxIterations))
            {
                throw new ArgumentOutOfRangeException("p.Iterations");
            }

            uint v = p.GetUInt32(ParamVersion, 0);

            if ((v < MinVersion) || (v > MaxVersion))
            {
                throw new ArgumentOutOfRangeException("p.Version");
            }

            byte[] pbSecretKey = p.GetByteArray(ParamSecretKey);
            byte[] pbAssocData = p.GetByteArray(ParamAssocData);

            if (pbSecretKey != null)
            {
                throw new ArgumentOutOfRangeException("Unsupported configuration: non-null pbSecretKey");
            }

            if (pbAssocData != null)
            {
                throw new ArgumentOutOfRangeException("Unsupported configuration: non-null pbAssocData");
            }

            /*
             * byte[] pbRet = Argon2d(pbMsg, pbSalt, uPar, uMem, uIt,
             *      32, v, pbSecretKey, pbAssocData);
             */

            IntPtr msgPtr  = Marshal.AllocHGlobal(pbMsg.Length);
            IntPtr saltPtr = Marshal.AllocHGlobal(pbSalt.Length);
            IntPtr retPtr  = Marshal.AllocHGlobal(32);

            Marshal.Copy(pbMsg, 0, msgPtr, pbMsg.Length);
            Marshal.Copy(pbSalt, 0, saltPtr, pbSalt.Length);

            const UInt32 Argon2_d = 0;

            int ret = argon2_hash(
                (UInt32)uIt, (UInt32)(uMem / 1024), uPar,
                msgPtr, (IntPtr)pbMsg.Length,
                saltPtr, (IntPtr)pbSalt.Length,
                retPtr, (IntPtr)32,
                (IntPtr)0, (IntPtr)0, Argon2_d, v);

            if (ret != 0)
            {
                throw new Exception("argon2_hash failed with " + ret);
            }

            byte[] pbRet = new byte[32];
            Marshal.Copy(retPtr, pbRet, 0, 32);

            Marshal.FreeHGlobal(msgPtr);
            Marshal.FreeHGlobal(saltPtr);
            Marshal.FreeHGlobal(retPtr);

            if (uMem > (100UL * 1024UL * 1024UL))
            {
                GC.Collect();
            }
            return(pbRet);
        }