Exemplo n.º 1
0
 /// <summary>
 /// Constructs a new key pair from a byte array
 /// </summary>
 ///
 /// <param name="KeyPair">An encoded key pair</param>
 public NTRUKeyPair(byte[] KeyPair)
 {
     using (MemoryStream keyStream = new MemoryStream(KeyPair))
     {
         _publicKey  = new NTRUPublicKey(keyStream);
         _privateKey = new NTRUPrivateKey(keyStream);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a new key pair
        /// </summary>
        /// 
        /// <param name="PublicKey">The Public key</param>
        /// <param name="PrivateKey">The Private Key</param>
        /// 
        /// <exception cref="NTRUException">Thrown if an invalid key is used</exception>
        public NTRUKeyPair(IAsymmetricKey PublicKey, IAsymmetricKey PrivateKey)
        {
            if (!(PublicKey is NTRUPublicKey))
                throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Public key!", new InvalidDataException());
            if (!(PrivateKey is NTRUPrivateKey))
                throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Private key!", new InvalidDataException());

            _publicKey = (NTRUPublicKey)PublicKey;
            _privateKey = (NTRUPrivateKey)PrivateKey;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a new key pair
        /// </summary>
        ///
        /// <param name="PublicKey">The Public key</param>
        /// <param name="PrivateKey">The Private Key</param>
        ///
        /// <exception cref="NTRUException">Thrown if an invalid key is used</exception>
        public NTRUKeyPair(IAsymmetricKey PublicKey, IAsymmetricKey PrivateKey)
        {
            if (!(PublicKey is NTRUPublicKey))
            {
                throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Public key!", new InvalidDataException());
            }
            if (!(PrivateKey is NTRUPrivateKey))
            {
                throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU Private key!", new InvalidDataException());
            }

            _publicKey  = (NTRUPublicKey)PublicKey;
            _privateKey = (NTRUPrivateKey)PrivateKey;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Constructs a new key pair
 /// </summary>
 ///
 /// <param name="Key">The public or private key</param>
 ///
 /// <exception cref="NTRUException">Thrown if an invalid name is used</exception>
 public NTRUKeyPair(IAsymmetricKey Key)
 {
     if (Key is NTRUPublicKey)
     {
         _publicKey = (NTRUPublicKey)PublicKey;
     }
     else if (Key is NTRUPrivateKey)
     {
         _privateKey = (NTRUPrivateKey)PrivateKey;
     }
     else
     {
         throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU key!", new InvalidDataException());
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Generates a new encryption key pair
        /// </summary>
        ///
        /// <param name="RngF">The random number generator to use for generating the secret polynomial f</param>
        /// <param name="RngG">The random number generator to use for generating the secret polynomial g</param>
        ///
        /// <returns>A key pair</returns>
        private IAsymmetricKeyPair GenerateKeyPair(IRandom RngF, IRandom RngG)
        {
            int  N      = _encParams.N;
            int  q      = _encParams.Q;
            bool fastFp = _encParams.FastFp;
            bool sparse = _encParams.Sparse;
            TernaryPolynomialType polyType = _encParams.PolyType;
            IPolynomial           t        = null;
            IntegerPolynomial     fq       = null;
            IntegerPolynomial     fp       = null;
            IntegerPolynomial     g        = null;

            if (ParallelUtils.IsParallel && _isParallel)
            {
                Action[] gA = new Action[] {
                    new Action(() => g = GenerateG(RngG)),
                    new Action(() => GenerateFQ(RngF, out t, out fq, out fp))
                };
                Parallel.Invoke(gA);
            }
            else
            {
                // Choose a random g that is invertible mod q.
                g = GenerateG(RngG);
                // choose a random f that is invertible mod 3 and q
                GenerateFQ(RngF, out t, out fq, out fp);
            }

            // if fastFp=true, fp=1
            if (fastFp)
            {
                fp           = new IntegerPolynomial(N);
                fp.Coeffs[0] = 1;
            }

            IntegerPolynomial h = g.Multiply(fq, q);

            h.Mult3(q);
            h.EnsurePositive(q);

            NTRUPrivateKey priv = new NTRUPrivateKey(t, fp, N, q, sparse, fastFp, polyType);
            NTRUPublicKey  pub  = new NTRUPublicKey(h, N, q);

            return(new NTRUKeyPair(pub, priv));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Constructs a new key pair
 /// </summary>
 /// 
 /// <param name="Key">The public or private key</param>
 /// 
 /// <exception cref="NTRUException">Thrown if an invalid name is used</exception>
 public NTRUKeyPair(IAsymmetricKey Key)
 {
     if (Key is NTRUPublicKey)
         _publicKey = (NTRUPublicKey)PublicKey;
     else if (Key is NTRUPrivateKey)
         _privateKey = (NTRUPrivateKey)PrivateKey;
     else
         throw new NTRUException("NTRUKeyPair:CTor", "Not a valid NTRU key!", new InvalidDataException());
 }
Exemplo n.º 7
0
 /// <summary>
 /// Constructs a new key pair from an input stream
 /// </summary>
 /// 
 /// <param name="KeyStream">An input stream containing an encoded key pair</param>
 public NTRUKeyPair(MemoryStream KeyStream)
 {
     _publicKey = new NTRUPublicKey(KeyStream);
     _privateKey = new NTRUPrivateKey(KeyStream);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructs a new key pair from a byte array
 /// </summary>
 /// 
 /// <param name="KeyPair">An encoded key pair</param>
 public NTRUKeyPair(byte[] KeyPair)
 {
     using (MemoryStream keyStream = new MemoryStream(KeyPair))
     {
         _publicKey = new NTRUPublicKey(keyStream);
         _privateKey = new NTRUPrivateKey(keyStream);
     }
 }
Exemplo n.º 9
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)
            {
                return(false);
            }

            NTRUPrivateKey other = (NTRUPrivateKey)obj;

            if (N != other.N)
            {
                return(false);
            }
            if (_fastFp != other._fastFp)
            {
                return(false);
            }

            if (FP == null)
            {
                if (other.FP != null)
                {
                    return(false);
                }
            }
            else if (!FP.Equals(other.FP))
            {
                return(false);
            }

            if (PolyType != other.PolyType)
            {
                return(false);
            }
            if (Q != other.Q)
            {
                return(false);
            }
            if (_sparse != other._sparse)
            {
                return(false);
            }

            if (T == null)
            {
                if (other.T != null)
                {
                    return(false);
                }
            }
            else if (!T.Equals(other.T))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Generates a new encryption key pair
        /// </summary>
        /// 
        /// <param name="RngF">The random number generator to use for generating the secret polynomial f</param>
        /// <param name="RngG">The random number generator to use for generating the secret polynomial g</param>
        /// 
        /// <returns>A key pair</returns>
        private IAsymmetricKeyPair GenerateKeyPair(IRandom RngF, IRandom RngG)
        {
            int N = _ntruParams.N;
            int q = _ntruParams.Q;
            bool fastFp = _ntruParams.FastFp;
            bool sparse = _ntruParams.Sparse;
            TernaryPolynomialType polyType = _ntruParams.PolyType;
            IPolynomial t = null;
            IntegerPolynomial fq = null;
            IntegerPolynomial fp = null;
            IntegerPolynomial g = null;

            if (ParallelUtils.IsParallel && _isParallel)
            {
                Action[] gA = new Action[] {
                    new Action(()=> g = GenerateG(RngG)),
                    new Action(()=> GenerateFQ(RngF, out t, out fq, out fp))
                };
                Parallel.Invoke(gA);
            }
            else
            {
                // Choose a random g that is invertible mod q.
                g = GenerateG(RngG);
                // choose a random f that is invertible mod 3 and q
                GenerateFQ(RngF, out t, out fq, out fp);
            }

            // if fastFp=true, fp=1
            if (fastFp)
            {
                fp = new IntegerPolynomial(N);
                fp.Coeffs[0] = 1;
            }

            IntegerPolynomial h = g.Multiply(fq, q);
            h.Mult3(q);
            h.EnsurePositive(q);

            NTRUPrivateKey priv = new NTRUPrivateKey(t, fp, N, q, sparse, fastFp, polyType);
            NTRUPublicKey pub = new NTRUPublicKey(h, N, q);

            return new NTRUKeyPair(pub, priv);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Constructs a new key pair from an input stream
 /// </summary>
 ///
 /// <param name="KeyStream">An input stream containing an encoded key pair</param>
 public NTRUKeyPair(MemoryStream KeyStream)
 {
     _publicKey  = new NTRUPublicKey(KeyStream);
     _privateKey = new NTRUPrivateKey(KeyStream);
 }