Ejemplo n.º 1
0
        static double Decrypt(int Iterations, MPKCParameters Param)
        {
            MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] ptext = new CSPRng().GetBytes(64);
            byte[] rtext = new byte[64];
            byte[] ctext;
            Stopwatch runTimer = new Stopwatch();

            using (MPKCEncrypt mpe = new MPKCEncrypt(Param))
            {
                mpe.Initialize(akp.PublicKey);
                ctext = mpe.Encrypt(ptext);
                mpe.Initialize(akp.PrivateKey);

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                    rtext = mpe.Decrypt(ctext);
                runTimer.Stop();
            }

            //if (!Compare.AreEqual(ptext, rtext))
            //    throw new Exception("Encryption test: decryption failure!");

            return runTimer.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 /// 
 /// <param name="CiphersParams">The MPKCParameters instance containing thecipher settings</param>
 public MPKCKeyGenerator(MPKCParameters CiphersParams)
 {
     _mpkcParams = (MPKCParameters)CiphersParams;
     // set source of randomness
     _rndEngine = GetPrng(_mpkcParams.RandomEngine);
     _M = _mpkcParams.M;
     _N = _mpkcParams.N;
     _T = _mpkcParams.T;
     _fieldPoly = _mpkcParams.FieldPolynomial;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the cipher engine
        /// </summary>
        ///
        /// <param name="CipherParams">The cipher parameters</param>
        ///
        /// <returns>An initialized cipher</returns>
        private IMPKCCiphers GetEngine(MPKCParameters CipherParams)
        {
            switch (CipherParams.CCA2Engine)
            {
            case CCA2Ciphers.KobaraImai:
                return(new KobaraImaiCipher(CipherParams));

            case CCA2Ciphers.Pointcheval:
                return(new PointchevalCipher(CipherParams));

            default:
                return(new FujisakiCipher(CipherParams));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initialize this class
        /// </summary>
        /// 
        /// <param name="CipherParams">The MPKCParameters instance containing thecipher 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 MPKCKeyGenerator(MPKCParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
                throw new CryptoAsymmetricException("MPKCKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());

            _frcLinear = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            _mpkcParams = (MPKCParameters)CipherParams;
            // set source of randomness
            _rndEngine = GetPrng(_mpkcParams.RandomEngine);
            _M = _mpkcParams.M;
            _N = _mpkcParams.N;
            _T = _mpkcParams.T;
            _fieldPoly = _mpkcParams.FieldPolynomial;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initialize this class
        /// </summary>
        ///
        /// <param name="CipherParams">The MPKCParameters instance containing thecipher 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 MPKCKeyGenerator(MPKCParameters CipherParams, bool Parallel = true)
        {
            if (CipherParams.RandomEngine == Prngs.PBPrng)
            {
                throw new CryptoAsymmetricException("MPKCKeyGenerator:Ctor", "Passphrase based digest and CTR generators must be pre-initialized, use the other constructor!", new ArgumentException());
            }

            _frcLinear = ParallelUtils.ForceLinear;
            ParallelUtils.ForceLinear = !Parallel;
            _mpkcParams = (MPKCParameters)CipherParams;
            // set source of randomness
            _rndEngine = GetPrng(_mpkcParams.RandomEngine);
            _M         = _mpkcParams.M;
            _N         = _mpkcParams.N;
            _T         = _mpkcParams.T;
            _fieldPoly = _mpkcParams.FieldPolynomial;
        }
Ejemplo n.º 6
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);
            }

            MPKCParameters other = (MPKCParameters)Obj;

            if (Digest != other.Digest)
            {
                return(false);
            }
            if (CCA2Engine != other.CCA2Engine)
            {
                return(false);
            }
            if (RandomEngine != other.RandomEngine)
            {
                return(false);
            }
            if (M != other.M)
            {
                return(false);
            }
            if (N != other.N)
            {
                return(false);
            }
            if (T != other.T)
            {
                return(false);
            }
            if (FieldPolynomial != other.FieldPolynomial)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 7
0
        static double Encrypt(int Iterations, MPKCParameters Param)
        {
            MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param);
            IAsymmetricKeyPair akp = mkgen.GenerateKeyPair();
            byte[] ptext = new CSPRng().GetBytes(64);
            byte[] ctext;
            Stopwatch runTimer = new Stopwatch();

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

                runTimer.Start();
                for (int i = 0; i < Iterations; i++)
                    ctext = mpe.Encrypt(ptext);
                runTimer.Stop();
            }

            return runTimer.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 8
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 MPKCKeyGenerator(MPKCParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            _mpkcParams = (MPKCParameters)CipherParams;
            // set source of randomness
            _rndEngine = RngEngine;
            _M         = _mpkcParams.M;
            _N         = _mpkcParams.N;
            _T         = _mpkcParams.T;
            _fieldPoly = _mpkcParams.FieldPolynomial;

            _frcLinear = ParallelUtils.ForceLinear;
            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
            {
                ParallelUtils.ForceLinear = true;
            }
            else
            {
                ParallelUtils.ForceLinear = !Parallel;
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Get the cipher engine
 /// </summary>
 /// 
 /// <param name="CipherParams">The engine type</param>
 /// 
 /// <returns>An initialized cipher</returns>
 private IMPKCCiphers GetEngine(MPKCParameters CipherParams)
 {
     switch (CipherParams.CCA2Engine)
     {
         case CCA2Ciphers.KobaraImai:
             return new KobaraImaiCipher(CipherParams);
         case CCA2Ciphers.Pointcheval:
             return new PointchevalCipher(CipherParams);
         default:
             return new FujisakiCipher(CipherParams);
     }
 }
Ejemplo n.º 10
0
        static double KeyGenerator(int Iterations, MPKCParameters Param)
        {
            MPKCKeyGenerator mkgen = new MPKCKeyGenerator(Param);
            IAsymmetricKeyPair akp;
            Stopwatch runTimer = new Stopwatch();

            runTimer.Start();
            for (int i = 0; i < Iterations; i++)
                akp = mkgen.GenerateKeyPair();
            runTimer.Stop();

            return runTimer.Elapsed.TotalMilliseconds;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 /// 
 /// <param name="CipherParams">The cipher engine</param>
 public MPKCEncrypt(MPKCParameters CipherParams)
 {
     _encEngine = GetEngine(CipherParams);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 ///
 /// <param name="CipherParams">The cipher engine</param>
 public MPKCEncrypt(MPKCParameters CipherParams)
 {
     m_encEngine = GetEngine(CipherParams);
 }
Ejemplo n.º 13
0
        private void Dispose(bool Disposing)
        {
            if (!_isDisposed && Disposing)
            {
                try
                {
                    if (_mpkcParams != null)
                    {
                        _mpkcParams.Dispose();
                        _mpkcParams = null;
                    }
                    if (_rndEngine != null)
                    {
                        _rndEngine.Dispose();
                        _rndEngine = null;
                    }
                    _M = 0;
                    _N = 0;
                    _T = 0;
                    _fieldPoly = 0;
                }
                catch { }

                _isDisposed = true;
            }
        }
Ejemplo n.º 14
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 MPKCKeyGenerator(MPKCParameters CipherParams, IRandom RngEngine, bool Parallel = true)
        {
            _mpkcParams = (MPKCParameters)CipherParams;
            // set source of randomness
            _rndEngine = RngEngine;
            _M = _mpkcParams.M;
            _N = _mpkcParams.N;
            _T = _mpkcParams.T;
            _fieldPoly = _mpkcParams.FieldPolynomial;

            _frcLinear = ParallelUtils.ForceLinear;
            // passphrase gens must be linear processed
            if (RngEngine.GetType().Equals(typeof(PBPRng)))
                ParallelUtils.ForceLinear = true;
            else
                ParallelUtils.ForceLinear = !Parallel;
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 /// 
 /// <param name="CipherParams">The McEliece cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public MPKCSign(MPKCParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     _dgtEngine = GetDigest(CipherParams.Digest);
     _asyCipher = GetEngine(CipherParams);
 }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Initialize this class
 /// </summary>
 ///
 /// <param name="CipherParams">The McEliece cipher used to encrypt the hash</param>
 /// <param name="Digest">The type of digest engine used</param>
 public MPKCSign(MPKCParameters CipherParams, Digests Digest = Digests.SHA512)
 {
     m_dgtEngine = GetDigest(CipherParams.Digest);
     m_asyCipher = GetEngine(CipherParams);
 }