Beispiel #1
0
        //@Override
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            //if (getClass() != obj.getClass())
            //     return false;
            SignaturePublicKey other = (SignaturePublicKey)obj;

            if (h == null)
            {
                if (other.h != null)
                {
                    return(false);
                }
            }
            else if (!h.Equals(other.h))
            {
                return(false);
            }
            if (q != other.q)
            {
                return(false);
            }
            return(true);
        }
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (!(obj.GetType() == typeof(NTRUEncryptionPrivateKeyParameters)))
            {
                return(false);
            }
            NTRUEncryptionPrivateKeyParameters other = (NTRUEncryptionPrivateKeyParameters)obj;

            if (parameters == null)
            {
                if (other.parameters != null)
                {
                    return(false);
                }
            }
            else if (!parameters.Equals(other.parameters))
            {
                return(false);
            }
            if (t == null)
            {
                if (other.t != null)
                {
                    return(false);
                }
            }
            else if (!t.Equals(other.t))
            {
                return(false);
            }
            if (!h.Equals(other.h))
            {
                return(false);
            }
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Decrypts a message
        /// </summary>
        ///
        /// <param name="Input">The message to decrypt</param>
        ///
        /// <returns>The decrypted message</returns>
        ///
        /// <exception cref="NTRUException">If not initialized, the specified hash algorithm is invalid, the encrypted data is invalid, or <c>MaxLenBytes</c> is greater than 255</exception>
        public byte[] Decrypt(byte[] Input)
        {
            if (!_isInitialized)
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "The cipher has not been initialized!", new InvalidOperationException());
            }

            IPolynomial       priv_t  = ((NTRUPrivateKey)_keyPair.PrivateKey).T;
            IntegerPolynomial priv_fp = ((NTRUPrivateKey)_keyPair.PrivateKey).FP;
            IntegerPolynomial pub     = ((NTRUPublicKey)_keyPair.PublicKey).H;
            int  N               = _encParams.N;
            int  q               = _encParams.Q;
            int  db              = _encParams.Db;
            int  maxMsgLenBytes  = _encParams.MaxMsgLenBytes;
            int  dm0             = _encParams.Dm0;
            int  maxM1           = _encParams.MaxM1;
            int  minCallsMask    = _encParams.MinMGFHashCalls;
            bool hashSeed        = _encParams.HashSeed;
            int  bLen            = db / 8;
            IntegerPolynomial e  = IntegerPolynomial.FromBinary(Input, N, q);
            IntegerPolynomial ci = Decrypt(e, priv_t, priv_fp);

            if (ci.Count(-1) < dm0)
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "Less than dm0 coefficients equal -1", new InvalidDataException());
            }
            if (ci.Count(0) < dm0)
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "Less than dm0 coefficients equal 0", new InvalidDataException());
            }
            if (ci.Count(1) < dm0)
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "Less than dm0 coefficients equal 1", new InvalidDataException());
            }
            //if (maxMsgLenBytes > 255)
            //    throw new NTRUException("NTRUEncrypt:Decrypt", "maxMsgLenBytes values bigger than 255 are not supported", new ArgumentOutOfRangeException());

            IntegerPolynomial cR = e;

            cR.Subtract(ci);
            cR.ModPositive(q);

            byte[]            coR4   = cR.ToBinary4();
            IntegerPolynomial mask   = MGF(coR4, N, minCallsMask, hashSeed);
            IntegerPolynomial cMTrin = ci;

            cMTrin.Subtract(mask);
            cMTrin.Mod3();

            byte[] cb, p0, cm;
            using (BinaryReader reader = new BinaryReader(new MemoryStream(cMTrin.ToBinary3Sves(maxM1 > 0))))
            {
                cb = new byte[bLen];
                reader.Read(cb, 0, cb.Length);
                // llen=1, so read one byte
                int cl = reader.ReadByte() & 0xFF;

                if (cl > maxMsgLenBytes)
                {
                    throw new NTRUException("NTRUEncrypt:Decrypt", string.Format("Message too long: {0} > {1}!", cl, maxMsgLenBytes), new InvalidDataException());
                }

                cm = new byte[cl];
                reader.Read(cm, 0, cm.Length);
                p0 = new byte[reader.BaseStream.Length - reader.BaseStream.Position];
                reader.Read(p0, 0, p0.Length);
            }

            if (!Compare.AreEqual(p0, new byte[p0.Length]))
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "The message is not followed by zeroes!", new InvalidDataException());
            }

            byte[]            sData   = GetSeed(cm, pub, cb);
            IPolynomial       cr      = GenerateBlindingPoly(sData);
            IntegerPolynomial cRPrime = cr.Multiply(pub);

            cRPrime.ModPositive(q);

            if (!cRPrime.Equals(cR))
            {
                throw new NTRUException("NTRUEncrypt:Decrypt", "Invalid message encoding!", new InvalidDataException());
            }

            return(cm);
        }
Beispiel #4
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (!(obj.GetType().IsAssignableFrom(typeof(Basis))))
            {
                return(false);
            }
            Basis other = (Basis)obj;

            if (N != other.N)
            {
                return(false);
            }
            if (basisType != other.basisType)
            {
                return(false);
            }
            if (f == null)
            {
                if (other.f != null)
                {
                    return(false);
                }
            }
            else if (!f.Equals(other.f))
            {
                return(false);
            }
            if (fPrime == null)
            {
                if (other.fPrime != null)
                {
                    return(false);
                }
            }
            else if (!fPrime.Equals(other.fPrime))
            {
                return(false);
            }
            if (h == null)
            {
                if (other.h != null)
                {
                    return(false);
                }
            }
            else if (!h.Equals(other.h))
            {
                return(false);
            }
            if (BitConverter.DoubleToInt64Bits(keyNormBoundSq) != BitConverter.DoubleToInt64Bits(other.keyNormBoundSq))
            {
                return(false);
            }
            if (polyType != other.polyType)
            {
                return(false);
            }
            if (q != other.q)
            {
                return(false);
            }
            return(true);
        }