Ejemplo n.º 1
0
        /// <summary>
        /// Read a Private key from a stream
        /// </summary>
        ///
        /// <param name="KeyStream">The stream containing the key</param>
        ///
        /// <returns>An initialized NTRUPrivateKey class</returns>
        ///
        /// <exception cref="NTRUException">Thrown if the stream can not be read</exception>
        public static NTRUPrivateKey From(MemoryStream KeyStream)
        {
            BinaryReader dataStream = new BinaryReader(KeyStream);

            try
            {
                // ins.Position = 0; wrong here, ins pos is wrong
                int         n      = IntUtils.ReadShort(KeyStream);
                int         q      = IntUtils.ReadShort(KeyStream);
                byte        flags  = dataStream.ReadByte();
                bool        sparse = (flags & 1) != 0;
                bool        fastFp = (flags & 2) != 0;
                IPolynomial t;

                TernaryPolynomialType polyType = (flags & 4) == 0 ?
                                                 TernaryPolynomialType.SIMPLE :
                                                 TernaryPolynomialType.PRODUCT;

                if (polyType == TernaryPolynomialType.PRODUCT)
                {
                    t = ProductFormPolynomial.FromBinary(KeyStream, n);
                }
                else
                {
                    IntegerPolynomial fInt = IntegerPolynomial.FromBinary3Tight(KeyStream, n);

                    if (sparse)
                    {
                        t = new SparseTernaryPolynomial(fInt);
                    }
                    else
                    {
                        t = new DenseTernaryPolynomial(fInt);
                    }
                }

                // Initializes fp from t
                IntegerPolynomial fp;
                if (fastFp)
                {
                    fp           = new IntegerPolynomial(n);
                    fp.Coeffs[0] = 1;
                }
                else
                {
                    fp = t.ToIntegerPolynomial().InvertF3();
                }

                return(new NTRUPrivateKey(t, fp, n, q, sparse, fastFp, polyType));
            }
            catch (IOException ex)
            {
                throw new NTRUException("NTRUPrivateKey:From", ex.Message, ex);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Read a Public Key from a Stream
 /// </summary>
 ///
 /// <param name="KeyStream">An input stream containing an encoded key</param>
 ///
 /// <exception cref="CryptoAsymmetricException">Thrown if the key could not be loaded</exception>
 public NTRUPublicKey(Stream KeyStream)
 {
     try
     {
         _N = IntUtils.ReadShort(KeyStream);
         _Q = IntUtils.ReadShort(KeyStream);
         _H = IntegerPolynomial.FromBinary(KeyStream, N, Q);
     }
     catch (Exception ex)
     {
         throw new CryptoAsymmetricException("NTRUPublicKey:CTor", "The Public key could not be loaded!", ex);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Read a Public key from a stream
        /// </summary>
        ///
        /// <param name="KeyStream">The stream containing the key</param>
        ///
        /// <returns>An initialized NTRUPublicKey class</returns>
        ///
        /// <exception cref="NTRUException">Thrown if the stream can not be read</exception>
        public static NTRUPublicKey From(MemoryStream KeyStream)
        {
            try
            {
                int n = IntUtils.ReadShort(KeyStream);
                int q = IntUtils.ReadShort(KeyStream);
                IntegerPolynomial h = IntegerPolynomial.FromBinary(KeyStream, n, q);

                return(new NTRUPublicKey(h, n, q));
            }
            catch (IOException ex)
            {
                throw new NTRUException("NTRUPublicKey:From", ex.Message, ex);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads a Private Key from a Stream
        /// </summary>
        ///
        /// <param name="KeyStream">An input stream containing an encoded key</param>
        ///
        /// <exception cref="CryptoAsymmetricException">Thrown if the key could not be loaded</exception>
        public NTRUPrivateKey(MemoryStream KeyStream)
        {
            BinaryReader dataStream = new BinaryReader(KeyStream);

            try
            {
                // ins.Position = 0; wrong here, ins pos is wrong
                m_N = IntUtils.ReadShort(KeyStream);
                m_Q = IntUtils.ReadShort(KeyStream);
                byte flags = dataStream.ReadByte();
                m_sparse = (flags & 1) != 0;
                m_fastFp = (flags & 2) != 0;

                m_polyType = (flags & 4) == 0 ?
                             TernaryPolynomialType.SIMPLE :
                             TernaryPolynomialType.PRODUCT;

                if (PolyType == TernaryPolynomialType.PRODUCT)
                {
                    m_T = ProductFormPolynomial.FromBinary(KeyStream, N);
                }
                else
                {
                    IntegerPolynomial fInt = IntegerPolynomial.FromBinary3Tight(KeyStream, N);

                    if (m_sparse)
                    {
                        m_T = new SparseTernaryPolynomial(fInt);
                    }
                    else
                    {
                        m_T = new DenseTernaryPolynomial(fInt);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new CryptoAsymmetricException("NTRUPrivateKey:Ctor", "The Private key could not be loaded!", ex);
            }

            Initialize();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decodes a polynomial encoded with ToBinary()
        /// </summary>
        ///
        /// <param name="InputStream">An input stream containing an encoded polynomial</param>
        /// <param name="N">Number of coefficients in the polynomial</param>
        ///
        /// <returns>The decoded polynomial</returns>
        public static SparseTernaryPolynomial FromBinary(MemoryStream InputStream, int N)
        {
            BinaryReader br = new BinaryReader(InputStream);
            // number of coefficients equal to 1
            int numOnes = IntUtils.ReadShort(InputStream);
            // number of coefficients equal to -1
            int numNegOnes   = IntUtils.ReadShort(InputStream);
            int maxIndex     = 1 << BITS_PER_INDEX;
            int bitsPerIndex = 32 - IntUtils.NumberOfLeadingZeros(maxIndex - 1);

            int data1Len = (numOnes * bitsPerIndex + 7) / 8;

            byte[] data1 = ArrayEncoder.ReadFullLength(InputStream, data1Len);
            int[]  ones  = ArrayEncoder.DecodeModQ(data1, numOnes, maxIndex);

            int data2Len = (numNegOnes * bitsPerIndex + 7) / 8;

            byte[] data2   = ArrayEncoder.ReadFullLength(InputStream, data2Len);
            int[]  negOnes = ArrayEncoder.DecodeModQ(data2, numNegOnes, maxIndex);

            return(new SparseTernaryPolynomial(N, ones, negOnes));
        }