Esempio n. 1
0
 /// <summary>
 /// Builds a parameter set from an encoded input stream
 /// </summary>
 ///
 /// <param name="ParamStream">Stream containing a parameter set</param>
 ///
 /// <exception cref="CryptoAsymmetricException">Thrown if the Stream is unreadable</exception>
 public MPKCParameters(Stream ParamStream)
 {
     try
     {
         BinaryReader reader = new BinaryReader(ParamStream);
         _oId           = reader.ReadBytes(OID_SIZE);
         _cca2Engine    = (CCA2Ciphers)reader.ReadInt32();
         _dgtEngineType = (Digests)reader.ReadInt32();
         _rndEngineType = (Prngs)reader.ReadInt32();
         _M             = reader.ReadInt32();
         _T             = reader.ReadInt32();
         _fieldPoly     = reader.ReadInt32();
         _N             = 1 << M;
     }
     catch (Exception ex)
     {
         throw new CryptoAsymmetricException("MPKCParameters:CTor", "The stream could not be read!", ex);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Initialize this class
        /// </summary>
        ///
        /// <param name="OId">OId - Unique identifier; <c>Family</c>, <c>Set</c>, <c>SubSet</c>, and <c>Designator</c>. The McEliece family must be <c>1</c> corresponding with the <see cref="AsymmetricEngines"/> enumeration.</param>
        /// <param name="M">The degree of the finite field GF(2^m)</param>
        /// <param name="T">The error correction capability of the code</param>
        /// <param name="CCA2Engine">The McEliece CCA2 cipher engine</param>
        /// <param name="Digest">The digest used by the cipher engine</param>
        /// <param name="Prng">The Prng used by the cipher</param>
        ///
        /// <exception cref="CryptoAsymmetricException">Thrown if the OId is invalid or; <c>m &lt; 1</c>, <c>m &gt; 32</c>, <c>t &lt; 0</c> or <c>t &gt; n</c></exception>
        public MPKCParameters(byte[] OId, int M, int T, CCA2Ciphers CCA2Engine = CCA2Ciphers.Fujisaki, Digests Digest = Digests.SHA256, Prngs Prng = Prngs.CTRPrng)
        {
            if (OId.Length != OID_SIZE)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, the OId length must be {0} bytes!", OID_SIZE, new ArgumentException()));
            }
            if (OId[0] != (byte)AsymmetricEngines.McEliece)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, first byte must be family designator ({0})!", AsymmetricEngines.McEliece, new ArgumentException()));
            }
            if (M < 1)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "M must be positive!", new ArgumentException());
            }
            if (M > 32)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "M is too large!", new ArgumentOutOfRangeException());
            }

            this.Digest       = Digest;
            this.CCA2Engine   = CCA2Engine;
            this.RandomEngine = Prng;

            Array.Copy(OId, this.OId, Math.Min(OId.Length, OID_SIZE));
            m_M = M;
            m_N = 1 << M;

            if (T < 0)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "T must be positive!", new ArgumentException());
            }
            if (T > N)
            {
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "T must be less than n = 2^m!", new ArgumentOutOfRangeException());
            }

            m_T         = T;
            m_fieldPoly = PolynomialRingGF2.GetIrreduciblePolynomial(M);
        }
 /// <summary>
 /// Set the default parameters: extension degree
 /// </summary>
 /// 
 /// <param name="OId">OId - Unique identifier; <c>Family</c>, <c>Set</c>, <c>SubSet</c>, and <c>Designator</c>. The McEliece family must be <c>1</c> corresponding with the <see cref="AsymmetricEngines"/> enumeration.</param>
 /// <param name="CCA2Engine">The McEliece CCA2 cipher engine</param>
 /// <param name="Digest">The digest used by the cipher engine</param>
 /// <param name="Prng">The prng used by the cipher engine</param>
 public MPKCParameters(byte[] OId, CCA2Ciphers CCA2Engine = CCA2Ciphers.Fujisaki, Digests Digest = Digests.SHA256, Prngs Prng = Prngs.CTRPrng)
     : this(OId, DEFAULT_M, DEFAULT_T)
 {
     this.Digest = Digest;
     this.CCA2Engine = CCA2Engine;
     this.RandomEngine = Prng;
 }
 private void Dispose(bool Disposing)
 {
     if (!_isDisposed && Disposing)
     {
         try
         {
             if (_oId != null)
             {
                 Array.Clear(_oId, 0, _oId.Length);
                 _oId = null;
             }
             _N = 0;
             _M = 0;
             _T = 0;
             _fieldPoly = 0;
             _cca2Engine = CCA2Ciphers.Fujisaki;
             _dgtEngineType = Digests.SHA256;
             _rndEngineType = Prngs.CTRPrng;
         }
         finally
         {
             _isDisposed = true;
         }
     }
 }
 /// <summary>
 /// Builds a parameter set from an encoded input stream
 /// </summary>
 /// 
 /// <param name="ParamStream">Stream containing a parameter set</param>
 /// 
 /// <exception cref="CryptoAsymmetricException">Thrown if the Stream is unreadable</exception>
 public MPKCParameters(Stream ParamStream)
 {
     try
     {
         BinaryReader reader = new BinaryReader(ParamStream);
         _oId = reader.ReadBytes(OID_SIZE);
         _cca2Engine = (CCA2Ciphers)reader.ReadInt32();
         _dgtEngineType = (Digests)reader.ReadInt32();
         _rndEngineType = (Prngs)reader.ReadInt32();
         _M = reader.ReadInt32();
         _T = reader.ReadInt32();
         _fieldPoly = reader.ReadInt32();
         _N = 1 << M;
     }
     catch (Exception ex)
     {
         throw new CryptoAsymmetricException("MPKCParameters:CTor", "The stream could not be read!", ex);
     }
 }
        /// <summary>
        /// Initialize this class
        /// </summary>
        /// 
        /// <param name="OId">OId - Unique identifier; <c>Family</c>, <c>Set</c>, <c>SubSet</c>, and <c>Designator</c>. The McEliece family must be <c>1</c> corresponding with the <see cref="AsymmetricEngines"/> enumeration.</param>
        /// <param name="M">The degree of the finite field GF(2^m)</param>
        /// <param name="T">The error correction capability of the code</param>
        /// <param name="FieldPoly">The field polynomial</param>
        /// <param name="CCA2Engine">The McEliece CCA2 cipher engine</param>
        /// <param name="Digest">The digest used by the cipher engine</param>
        /// <param name="Prng">The Prng used by the cipher</param>
        /// 
        /// <exception cref="CryptoAsymmetricException">Thrown if the OId is invalid or; <c>t &lt; 0</c>, <c>t &gt; n</c>, or <c>poly</c> is not an irreducible field polynomial</exception>
        public MPKCParameters(byte[] OId, int M, int T, int FieldPoly, CCA2Ciphers CCA2Engine = CCA2Ciphers.Fujisaki, Digests Digest = Digests.SHA256, Prngs Prng = Prngs.CTRPrng)
        {
            if (OId.Length != OID_SIZE)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, the OId length must be {0} bytes!", OID_SIZE, new ArgumentException()));
            if (OId[0] != (byte)AsymmetricEngines.McEliece)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, first byte must be family designator ({0})!", AsymmetricEngines.McEliece, new ArgumentException()));
            if (M < 1)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "M must be positive!", new ArgumentException());
            if (M > 32)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "M is too large!", new ArgumentOutOfRangeException());

            _M = M;
            this.Digest = Digest;
            this.CCA2Engine = CCA2Engine;
            this.RandomEngine = Prng;

            Array.Copy(OId, this.OId, Math.Min(OId.Length, OID_SIZE));
            _N = 1 << M;
            _T = T;

            if (T < 0)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "T must be positive!", new ArgumentException());
            if (T > N)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "T must be less than n = 2^m!", new ArgumentOutOfRangeException());

            if ((PolynomialRingGF2.Degree(FieldPoly) == M) && (PolynomialRingGF2.IsIrreducible(FieldPoly)))
                _fieldPoly = FieldPoly;
            else
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "Polynomial is not a field polynomial for GF(2^m)", new InvalidDataException());
        }
        /// <summary>
        /// Initialize this class
        /// </summary>
        /// 
        /// <param name="OId">OId - Unique identifier; <c>Family</c>, <c>Set</c>, <c>SubSet</c>, and <c>Designator</c>. The McEliece family must be <c>1</c> corresponding with the <see cref="AsymmetricEngines"/> enumeration.</param>
        /// <param name="Keysize">The length of a Goppa code</param>
        /// <param name="CCA2Engine">The McEliece CCA2 cipher engine</param>
        /// <param name="Digest">The digest used by the cipher engine</param>
        /// <param name="Prng">The Prng used by the cipher</param>
        /// 
        /// <exception cref="CryptoAsymmetricException">Thrown if the OId is invalid, or <c>keysize &lt; 1</c></exception>
        public MPKCParameters(byte[] OId, int Keysize, CCA2Ciphers CCA2Engine = CCA2Ciphers.Fujisaki, Digests Digest = Digests.SHA256, Prngs Prng = Prngs.CTRPrng)
        {
            if (Keysize < 1)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", "The key size must be positive!", new ArgumentException());
            if (OId.Length != OID_SIZE)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, the OId length must be {0} bytes!", OID_SIZE, new ArgumentException()));
            if (OId[0] != (byte)AsymmetricEngines.McEliece)
                throw new CryptoAsymmetricException("MPKCParameters:Ctor", string.Format("The OId is invalid, first byte must be family designator ({0})!", AsymmetricEngines.McEliece, new ArgumentException()));

            this.Digest = Digest;
            this.CCA2Engine = CCA2Engine;
            this.RandomEngine = Prng;
            Array.Copy(OId, this.OId, Math.Min(OId.Length, OID_SIZE));
            _M = 0;
            _N = 1;

            while (_N < Keysize)
            {
                _N <<= 1;
                _M++;
            }
            _T = _N >> 1;
            _T /= _M;

            _fieldPoly = PolynomialRingGF2.GetIrreduciblePolynomial(_M);
        }