Exemple #1
0
        /// <summary>
        /// Initialize this class
        /// </summary>
        ///
        /// <param name="CipherParams">The RLWEParameters instance containing the cipher settings</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        ///
        /// <exception cref="CryptoAsymmetricException">Thrown if a Prng that requires pre-initialization is specified; (wrong constructor)</exception>
        public RLWEKeyGenerator(RLWEParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
            {
                throw new CryptoAsymmetricException("RLWEKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());
            }

            m_frcLinear = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            m_rlweParams = CipherParams;
            m_rndEngine  = GetPrng(CipherParams.RandomEngine);
        }
Exemple #2
0
        /// <summary>
        /// Use an initialized prng to generate the key; use this constructor with an Rng that requires pre-initialization, i.e. PBPrng
        /// </summary>
        ///
        /// <param name="CipherParams">The RLWEParameters instance containing the cipher settings</param>
        /// <param name="RngEngine">An initialized Prng instance</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        public RLWEKeyGenerator(RLWEParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            m_rlweParams = CipherParams;
            m_rndEngine  = RngEngine;
            m_frcLinear  = ParallelUtils.ForceLinear;

            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
            {
                ParallelUtils.ForceLinear = true;
            }
            else
            {
                ParallelUtils.ForceLinear = !Parallel;
            }
        }
Exemple #3
0
        /// <summary>
        /// Initialize this class; Prng is created automatically
        /// </summary>
        ///
        /// <param name="CipherParams">The cipher engine</param>
        public RLWEEncrypt(RLWEParameters CipherParams)
        {
            _rndEngine = GetPrng(CipherParams.RandomEngine);
            _N         = CipherParams.N;
            _Q         = CipherParams.Q;
            _Sigma     = CipherParams.Sigma;
            _mFp       = CipherParams.MFP;

            if (CipherParams.N == 256)
            {
                _maxPlainText = 32;
            }
            else
            {
                _maxPlainText = 64;
            }
        }
Exemple #4
0
        /// <summary>
        /// Compare this object instance with another
        /// </summary>
        ///
        /// <param name="Obj">Object to compare</param>
        ///
        /// <returns>True if equal, otherwise false</returns>
        public override bool Equals(Object Obj)
        {
            if (this == Obj)
            {
                return(true);
            }
            if (Obj == null && this != null)
            {
                return(false);
            }

            RLWEParameters other = (RLWEParameters)Obj;

            if (m_N != other.N)
            {
                return(false);
            }
            if (m_Q != other.Q)
            {
                return(false);
            }
            if (m_rndEngineType != other.RandomEngine)
            {
                return(false);
            }
            if (m_dgtEngineType != other.Digest)
            {
                return(false);
            }
            if (m_Sigma != other.Sigma)
            {
                return(false);
            }
            if (m_mFp != other.MFP)
            {
                return(false);
            }

            return(true);
        }
        private void TestEncrypt(RLWEParameters Param)
        {
            RLWEKeyGenerator mkgen = new RLWEKeyGenerator(Param);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] enc;

            using (RLWEEncrypt mpe = new RLWEEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);

                int sz = mpe.MaxPlainText;
                byte[] data = new byte[sz];
                new CSPRng().GetBytes(data);

                enc = mpe.Encrypt(data);

                mpe.Initialize(akp.PrivateKey);
                byte[] dec = mpe.Decrypt(enc);

                if (!Compare.AreEqual(dec, data))
                    throw new Exception("Encryption test: decryption failure!");
                OnProgress(new TestEventArgs(string.Format("Passed N:{0} Q:{1} encryption test", Param.N, Param.Q)));
            }
        }
Exemple #6
0
        /// <summary>
        /// Get the asymmetric parameters from a byte array
        /// </summary>
        /// 
        /// <param name="Data">The encoded parameters</param>
        /// 
        /// <returns>The asymmetric parameters</returns>
        private IAsymmetricParameters GetAsymmetricParams(byte[] Data)
        {
            IAsymmetricParameters param = null;

            try
            {
                if (Data.Length > 4)
                {
                    if (Data[0] == (byte)AsymmetricEngines.McEliece)
                        param = new MPKCParameters(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.NTRU)
                        param = new NTRUParameters(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.RingLWE)
                        param = new RLWEParameters(Data);
                }
                else
                {
                    if (Data[0] == (byte)AsymmetricEngines.McEliece)
                        param = MPKCParamSets.FromId(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.NTRU)
                        param = NTRUParamSets.FromId(Data);
                    else if (Data[0] == (byte)AsymmetricEngines.RingLWE)
                        param = RLWEParamSets.FromId(Data);
                }

                return param;
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("DtmKex:GetAsymmetricParams", "The param set is unknown!", ex);
            }
        }
Exemple #7
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 ///
 /// <param name="CipherParams">The RLWE cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public RLWESign(RLWEParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     m_asyCipher = new RLWEEncrypt(CipherParams);
     m_dgtEngine = GetDigest(Digest);
 }
Exemple #8
0
 /// <summary>
 /// Initialize this class with an initialized Prng
 /// </summary>
 /// 
 /// <param name="CipherParams">The cipher parameters</param>
 /// <param name="Engine">The initialized cipher prng</param>
 public RLWEEncrypt(RLWEParameters CipherParams, IRandom Engine)
 {
     _rndEngine = Engine;
 }
Exemple #9
0
        /// <summary>
        /// Initialize this class; Prng is created automatically
        /// </summary>
        /// 
        /// <param name="CipherParams">The cipher engine</param>
        public RLWEEncrypt(RLWEParameters CipherParams)
        {
            _rndEngine = GetPrng(CipherParams.RandomEngine);
            _N = CipherParams.N;
            _Q = CipherParams.Q;
            _Sigma = CipherParams.Sigma;
            _mFp = CipherParams.MFP;

            if (CipherParams.N == 256)
                _maxPlainText = 32;
            else
                _maxPlainText = 64;
        }
Exemple #10
0
 /// <summary>
 /// Initialize this class with an initialized Prng
 /// </summary>
 ///
 /// <param name="CipherParams">The cipher parameters</param>
 /// <param name="Engine">The initialized cipher prng</param>
 public RLWEEncrypt(RLWEParameters CipherParams, IRandom Engine)
 {
     _rndEngine = Engine;
 }
Exemple #11
0
        /// <summary>
        /// Use an initialized prng to generate the key; use this constructor with an Rng that requires pre-initialization, i.e. PBPrng
        /// </summary>
        /// 
        /// <param name="CipherParams">The RLWEParameters instance containing the cipher settings</param>
        /// <param name="RngEngine">An initialized Prng instance</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        public RLWEKeyGenerator(RLWEParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            _rlweParams = CipherParams;
            _rndEngine = RngEngine;
            _frcLinear = ParallelUtils.ForceLinear;

            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
                ParallelUtils.ForceLinear = true;
            else
                ParallelUtils.ForceLinear = !Parallel;
        }
Exemple #12
0
        /// <summary>
        /// Initialize this class
        /// </summary>
        /// 
        /// <param name="CipherParams">The RLWEParameters instance containing the cipher settings</param>
        /// <param name="Parallel">Use parallel processing when generating a key; set to false if using a passphrase type generator (default is true)</param>
        /// 
        /// <exception cref="CryptoAsymmetricException">Thrown if a Prng that requires pre-initialization is specified; (wrong constructor)</exception>
        public RLWEKeyGenerator(RLWEParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
                throw new CryptoAsymmetricException("RLWEKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());

            _frcLinear = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            _rlweParams = CipherParams;
            _rndEngine = GetPrng(CipherParams.RandomEngine);
        }
Exemple #13
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 /// 
 /// <param name="CipherParams">The RLWE cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public RLWESign(RLWEParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     _asyCipher = new RLWEEncrypt(CipherParams);
     _dgtEngine = GetDigest(Digest);
 }