Esempio n. 1
0
        internal RSACryptoSystem( BackgroundWorker UseWorker, RSACryptoWorkerInfo UseWInfo )
        {
            Worker = UseWorker;
            WorkerInfo = UseWInfo;
            StartTime = new ECTime();
            StartTime.SetToNow();

            RngCsp = new RNGCryptoServiceProvider();
            IntMath = new IntegerMath();
            IntMathNewForP = new IntegerMathNew( IntMath );
            IntMathNewForQ = new IntegerMathNew( IntMath );

            Worker.ReportProgress( 0, IntMath.GetStatusString() );
            Quotient = new Integer();
            Remainder = new Integer();
            PrimeP = new Integer();
            PrimeQ = new Integer();
            PrimePMinus1 = new Integer();
            PrimeQMinus1 = new Integer();
            PubKeyN = new Integer();
            PubKeyExponent = new Integer();
            PrivKInverseExponent = new Integer();
            PrivKInverseExponentDP = new Integer();
            PrivKInverseExponentDQ = new Integer();
            QInv = new Integer();
            PhiN = new Integer();
            TestForDecrypt = new Integer();
            M1ForInverse = new Integer();
            M2ForInverse = new Integer();
            HForQInv = new Integer();
            M1MinusM2 = new Integer();
            M1M2SizeDiff = new Integer();

            PubKeyExponent.SetFromULong( PubKeyExponentUint );
        }
Esempio n. 2
0
        internal CRTBaseMath( BackgroundWorker UseWorker, CRTMath UseCRTMath )
        {
            // Most of these are created ahead of time so that
            // they don't have to be created inside a loop.
            Worker = UseWorker;
            IntMath = new IntegerMath();
            CRTMath1 = UseCRTMath;
            Quotient = new Integer();
            Remainder = new Integer();
            CRTAccumulateBase = new ChineseRemainder( IntMath );
            CRTAccumulateBasePart = new ChineseRemainder( IntMath );
            CRTAccumulateForBaseMultiples = new ChineseRemainder( IntMath );
            CRTAccumulatePart = new ChineseRemainder( IntMath );
            BaseModArrayModulus = new Integer();
            CRTTempForIsEqual = new ChineseRemainder( IntMath );
            CRTWorkingTemp = new ChineseRemainder( IntMath );
            ExponentCopy = new Integer();
            CRTXForModPower = new ChineseRemainder( IntMath );
            CRTAccumulate = new ChineseRemainder( IntMath );
            CRTCopyForSquare = new ChineseRemainder( IntMath );
            FermatExponent = new Integer();
            CRTFermatModulus = new ChineseRemainder( IntMath );
            FermatModulus = new Integer();
            CRTTestFermat = new ChineseRemainder( IntMath );

            Worker.ReportProgress( 0, "Setting up numbers array." );
            SetupNumbersArray();

            Worker.ReportProgress( 0, "Setting up base array." );
            SetupBaseArray();

            Worker.ReportProgress( 0, "Setting up multiplicative inverses." );
            SetMultiplicativeInverses();
        }
Esempio n. 3
0
        internal MultiplyBits( MainForm UseForm )
        {
            MForm = UseForm;
            try
            {
            IntMath = new IntegerMath();
            // MForm.ShowStatus( IntMath.GetStatusString());

            InputOutputDictionary = new SortedDictionary<uint, uint>();

            Product = new Integer();
            TestProduct = new Integer();
            Factor1 = new Integer();
            Factor2 = new Integer();
            Quotient = new Integer();
            Remainder = new Integer();

            MultArray = new LineRec[MultArraySize];
            MultArrayCopyForMult2 = new LineRec[MultArraySize];
            for( int Count = 0; Count < MultArraySize; Count++ )
              {
              MultArray[Count].OneLine = new uint[MultArraySize];
              MultArrayCopyForMult2[Count].OneLine = new uint[MultArraySize];
              }

            SetupFermatLittle( 107 );

            }
            catch( Exception )
              {
              MForm.ShowStatus( "Not enough RAM for the MultArray." );
              }
        }
Esempio n. 4
0
 internal ModularReduction()
 {
     IntMath = new IntegerMath();
     XForModPower = new Integer();
     ExponentCopy = new Integer();
     Quotient = new Integer();
     Remainder = new Integer();
     TempForModPower = new Integer();
 }
Esempio n. 5
0
        internal CRTBase( IntegerMath UseIntMath )
        {
            if( DigitsArraySize > IntegerMath.PrimeArrayLength )
              throw( new Exception( "CRTBase digit size is too big." ));

            IntMath = UseIntMath;

            DigitsArray = new int[DigitsArraySize];
            // SetToZero(); Not necessary for managed code.
        }
Esempio n. 6
0
 // private uint GBaseSmallModulus = 0;
 internal IntegerMathNew( IntegerMath UseIntMath )
 {
     IntMath = UseIntMath;
     Quotient = new Integer();
     Remainder = new Integer();
     XForModPower = new Integer();
     ExponentCopy = new Integer();
     TempForModPower = new Integer();
     TestForModPower = new Integer();
     AccumulateBase = new Integer();
     TestForModInverse1 = new Integer();
 }
Esempio n. 7
0
 internal FindFactors(  BackgroundWorker UseWorker, IntegerMath UseIntMath )
 {
     Worker = UseWorker;
     IntMath = UseIntMath;
     Quotient = new Integer();
     Remainder = new Integer();
     OriginalFindFrom = new Integer();
     FindFrom = new Integer();
     FactorsArray = new OneFactorRec[8];
     SortIndexArray = new int[8];
     StatsDictionary = new SortedDictionary<uint, uint>();
 }
        // internal ExponentVectorNumber( BackgroundWorker UseWorker, IntegerMath UseIntMath )
        internal ExponentVectorNumber( IntegerMath UseIntMath )
        {
            // Worker = UseWorker;
            IntMath = UseIntMath;
            Quotient = new Integer();
            Remainder = new Integer();
            ExtraValue = new Integer();
            PartAForAdd = new Integer();
            PartBForAdd = new Integer();

            VectorValuesArray = new VectorValueRec[IncreaseArraySizeBy];
        }
        internal ChineseRemainder( IntegerMath UseIntegerMath )
        {
            IntMath = UseIntegerMath;

            if( DigitsArraySize > IntegerMath.PrimeArrayLength )
              throw( new Exception( "ChineseRemainder digit size is too big." ));

            DigitsArray = new OneDigit[DigitsArraySize];
            for( int Count = 0; Count < DigitsArraySize; Count++ )
              DigitsArray[Count].Prime = (int)IntMath.GetPrimeAt( Count );

            // SetToZero(); Not necessary for managed code.
        }
Esempio n. 10
0
        internal FactorDictionary( MainForm UseForm )
        {
            MForm = UseForm;
            IntMath = new IntegerMath();
            Product = new Integer();
            SolutionP = new Integer();
            SolutionQ = new Integer();
            Quotient = new Integer();
            Remainder = new Integer();

            MainDictionary = new SortedDictionary<uint, ListRec>();
            NumberList = new List<NumberRec>();
        }
Esempio n. 11
0
        internal EquationParts( QuadResWorkerInfo UseWInfo, BackgroundWorker UseWorker )
        {
            WInfo = UseWInfo;
            Worker = UseWorker;
            IntMath = new IntegerMath();
            Quotient = new Integer();
            Remainder = new Integer();
            Product = new Integer();
            SolutionP = new Integer();
            SolutionQ = new Integer();
            ProductSqrRoot = new Integer();
            MaxX = new Integer();
            FindFactors1 = new FindFactors(  Worker, IntMath );

            IntMath.SetFromString( Product, WInfo.PublicKeyModulus );
        }
Esempio n. 12
0
 internal FactorBase( QuadResWorkerInfo UseWInfo, BackgroundWorker UseWorker )
 {
     WInfo = UseWInfo;
     Worker = UseWorker;
     IntMath = new IntegerMath();
     Quotient = new Integer();
     Remainder = new Integer();
     Product = new Integer();
     ProductSqrRoot = new Integer();
     EulerExponent = new Integer();
     EulerResult = new Integer();
     EulerModulus = new Integer();
     OneMainFactor = new Integer();
     ExpOneMainFactor = new ExponentVectorNumber( IntMath );
     StartTime = new ECTime();
     StartTime.SetToNow();
     IntMath.SetFromString( Product, WInfo.PublicKeyModulus );
     IntMath.SquareRoot( Product, ProductSqrRoot );
 }
Esempio n. 13
0
        internal QuadResCombinatorics( QuadResWorkerInfo UseWInfo, BackgroundWorker UseWorker )
        {
            WInfo = UseWInfo;
            Worker = UseWorker;
            IntMath = new IntegerMath();
            Quotient = new Integer();
            Remainder = new Integer();
            Product = new Integer();
            SolutionP = new Integer();
            SolutionQ = new Integer();
            ToDivideMod32 = new Integer();
            LastAccumulateValue = new Integer();
            GetValueBasePart = new Integer();
            StartTime = new ECTime();
            StartTime.SetToNow();

            IntMath.SetFromString( Product, WInfo.PublicKeyModulus );
            QuadResDigitsArray = new QuadResDigitsRec[DigitsArrayLength];
        }
Esempio n. 14
0
        private int ProcessOneTag( int Index, byte[] TagsBuffer )
        {
            try
            {
            StatusString += "Top of ProcessOneTag().\r\n";

            if( IntMath == null )
              IntMath = new IntegerMath();

            if( TagsBuffer == null )
              {
              StatusString += "Tags buffer was null.\r\n";
              return -1;
              }

            if( ObjectIDNames == null )
              ObjectIDNames = new X509ObjectIDNames();

            if( Index < 0 )
              {
              StatusString += "Index < 0.\r\n";
              return -1;
              }

            if( Index >= TagsBuffer.Length )
              {
              StatusString += "It's past the end of the buffer.\r\n";
              return -1;
              }

            StatusString += "Index: " + Index.ToString() + "\r\n";

            int Tag = TagsBuffer[Index];
            Index++;

            int ClassBits = Tag & 0xC0;
            ClassBits >>= 6;
            StatusString += "ClassBits: " + ClassBits.ToString( "X0" ) + "\r\n";

            int PrimitiveOrConstructed = Tag & 0x20;
            if( (PrimitiveOrConstructed & 0x20) == 0 )
              StatusString += "Tag is a Primitive type.\r\n";
            else
              StatusString += "Tag is a Constructed type.\r\n";

            int TagVal = Tag & 0x1F; // Bottom 5 bits.
            ShowTagType( TagVal );
            MostRecentTagValue = TagVal;

            int Length = TagsBuffer[Index];
            Index++;
            if( (Length & 0x80) != 0 )
              {
              // Then it's in the long form.
              int HowManyBytes = Length & 0x7F;
              if( HowManyBytes > 4 )
            throw( new Exception( "This can't be right for HowManyBytes: " + HowManyBytes.ToString() ));

              StatusString += "Long form HowManyBytes: " + HowManyBytes.ToString() + "\r\n";

              Length = 0;
              for( int Count = 0; Count < HowManyBytes; Count++ )
            {
            Length <<= 8;
            Length |= TagsBuffer[Index];
            Index++;
            }
              }
            else
              {
              StatusString += "Length is in short form.\r\n";
              }

            StatusString += "Length is: " + Length.ToString() + "\r\n";
            if( Length > TagsBuffer.Length )
              {
              StatusString += "The Length is not a reasonable number.\r\n";
              return -1;
              }

            if( // (TagVal == 0) || // That context specific tag.
            (TagVal == 1) || // Boolean
            // (TagVal == 4) || // Octet String
            (TagVal == 5) || // Null (Should have a length of zero.)
            (TagVal == 7) || // Object Descriptor
            // (TagVal == 8) || // External
            (TagVal == 9) || // Real/Float
            (TagVal == 10) || // Enumerated
            (TagVal == 11) || // Embedded PDV
            (TagVal == 12) || // UTF8 String
            (TagVal == 13) || // Relative OID
            // (TagVal == 14) || // Reserved
            // (TagVal == 15) || // Reserved
            // (TagVal == 16) || // Sequence
            // (TagVal == 17) || // Set
            (TagVal == 18) || // Numeric String
            (TagVal == 20) || // T61 String
            (TagVal == 21) || // Videotex String
            // (TagVal == 22) || // IA5 String
            (TagVal == 23) || // UTC Time
            (TagVal == 24) || // Generalized Time
            (TagVal == 25) || // Graphic String
            (TagVal == 26) || // Visible String
            (TagVal == 27) || // General String
            (TagVal == 28) || // Universal String
            (TagVal == 29) || // Character String
            (TagVal == 30)) // BMP String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            int ContentByte = TagsBuffer[Index];
            Index++;
            StatusString += "  Byte: 0x" + ContentByte.ToString( "X2" ) + "\r\n";
            }
              }

            if( TagVal == 2 ) // Integer
              {
              // This length was already checked above to see if it's a reasonable
              // number.
              byte[] BytesToSet = new byte[Length];
              try
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            BytesToSet[Count] = TagsBuffer[Index];
            Index++;
            }

              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: BytesToSet[Count] = TagsBuffer[Index].\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }

              try
              {
              MostRecentIntegerValue.SetFromBigEndianByteArray( BytesToSet );
              StatusString += "MostRecentIntegerValue: " + IntMath.ToString10( MostRecentIntegerValue ) + "\r\n";
              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: SetFromBigEndianByteArray().\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }
              }

            if( TagVal == 3 ) // bit String
              {
              // This length was already checked above to see if it's a reasonable
              // number.
              byte[] BytesToSet = new byte[Length];
              try
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            BytesToSet[Count] = TagsBuffer[Index];
            Index++;
            }

              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: BytesToSet[Count] = TagsBuffer[Index].\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }

              try
              {
              BitStreamRec Rec = new BitStreamRec();
              Rec.BitStream = BytesToSet;
              Rec.OIDString = MostRecentObjectIDString;
              AddBitStreamRec( Rec );
              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: AddBitStreamRec().\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }
              }

            // Just a rough draft, to see what's in them.
            if( TagVal == 4 ) // Octet String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 )
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            // UTF8String.

            if( TagVal == 19 ) // Printable String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 ) // 127 is the ASCII DEL character.
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            if( TagVal == 22 ) // IA5 String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 )
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            if( TagVal == 6 ) // Object Indentifier
              {
              if( Length < 2 )
            {
            StatusString += "The Length can't be right for this OID.\r\n";
            return -1;
            }

              byte[] CodedBytes = new byte[Length];
              for( int Count = 0; Count < Length; Count++ )
            {
            CodedBytes[Count] = TagsBuffer[Index];
            Index++;
            }

              X509ObjectID X509ID = new X509ObjectID();
              if( !X509ID.MakeFromBytes( CodedBytes ))
            {
            StatusString += "X509ID.MakeFromBytes() returned false.\r\n";
            return -1;
            }

              MostRecentObjectIDString = X509ID.GetStringValue();
              StatusString += X509ID.GetStatusString();
              StatusString += "StringValue is: " + X509ID.GetStringValue() + "\r\n";
              StatusString += "OID Name is: " + ObjectIDNames.GetNameFromDictionary( X509ID.GetStringValue()) + "\r\n";
              }

            StatusString += "\r\n";
            return Index;

            }
            catch( Exception Except )
              {
              // Probably over-ran the buffer.
              StatusString += "Exception in DomainX509Record.ProcessOneTag().\r\n";
              StatusString += Except.Message + "\r\n";
              return -1;
              }
        }
Esempio n. 15
0
        internal FermatLittle( MainForm UseForm )
        {
            MForm = UseForm;

            IntMath = new IntegerMath();
        }