Exemple #1
0
        /// <summary>
        /// Generates a prime number
        /// </summary>
        /// <param name="bits">Number of bits the resulting prime should have</param>
        /// <returns>Generated prime</returns>
        public static BigNumber GeneratePrime(int bits)
        {
            IntPtr res = OpenSSL.BN_new();

            OpenSSL.BN_generate_prime(res, bits, Convert.ToInt32(true), IntPtr.Zero, IntPtr.Zero, null, IntPtr.Zero);
            return(new BigNumber(res));
        }
Exemple #2
0
        public string ToString(string format)
        {
            if (fDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            IntPtr str = IntPtr.Zero;

            if (format.ToLower() == "d")
            {
                str = OpenSSL.BN_bn2dec(fBigNum);
            }
            else if (format.ToLower() == "x")
            {
                str = OpenSSL.BN_bn2hex(fBigNum);
            }
            else
            {
                throw new FormatException();
            }

            string res = Marshal.PtrToStringAnsi(str);

            OpenSSL.CRYPTO_free(str);
            return(res);
        }
Exemple #3
0
        /// <summary>
        /// Generates a random BigNum
        /// </summary>
        /// <param name="bits">How many bits the generated random number shoud have</param>
        /// <returns>Generated random number</returns>
        public static BigNumber Random(int bits)
        {
            RNG.Seed();
            BigNumber rand = new BigNumber();

            OpenSSL.BN_rand(rand.fBigNum, bits, 1, 1);
            return(rand);
        }
Exemple #4
0
 public static void Cleanup()
 {
     if (fSeeded)
     {
         OpenSSL.RAND_cleanup();
     }
     fSeeded = false;
 }
Exemple #5
0
 /// <summary>
 /// Explicitly frees resources consumed by this BigNum instance
 /// </summary>
 public void Dispose()
 {
     if (!fDisposed)
     {
         GC.SuppressFinalize(this);
         OpenSSL.BN_free(fBigNum);
         fDisposed = true;
     }
 }
Exemple #6
0
        /// <summary>
        /// Clears the BigNum from memory
        /// </summary>
        public void Clear()
        {
            if (fDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            OpenSSL.BN_clear(fBigNum);
        }
Exemple #7
0
        /// <summary>
        /// Replaces the content of the BigNum with a random BigNum
        /// </summary>
        /// <param name="bn">BigNum to replace</param>
        /// <param name="bits">How many bits the generated random number shoud have</param>
        public static void Random(BigNumber bn, int bits)
        {
            if (bn.fDisposed)
            {
                throw new ObjectDisposedException("BigNum");
            }

            RNG.Seed();
            OpenSSL.BN_rand(bn.fBigNum, bits, 1, 1);
        }
Exemple #8
0
        /// <summary>
        /// Returns the BigNum as a buffer
        /// </summary>
        /// <returns>Content of BigNum as byte array in OpenSSL byte order</returns>
        public byte[] ToBigArray()
        {
            if (fDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            byte[] buf = new byte[(OpenSSL.BN_num_bits(fBigNum) + 7) / 8];
            OpenSSL.BN_bn2bin(fBigNum, buf);
            return(buf);
        }
Exemple #9
0
 /// <summary>
 /// Seeds the Pseudo RNG with content from a file
 /// </summary>
 /// <param name="file">File to read</param>
 /// <param name="max_bytes">Number of bytes to read (-1 for the full contents)</param>
 /// <returns>Number of bytes actually read</returns>
 public static int LoadFile(string file, int max_bytes)
 {
     if (File.Exists(file))
     {
         int read = OpenSSL.RAND_load_file(Encoding.ASCII.GetBytes(file), max_bytes);
         fSeeded = true;
         return(read);
     }
     else
     {
         throw new IOException("The file does not exist.");
     }
 }
Exemple #10
0
        public static bool operator <(BigNumber left, BigNumber right)
        {
            //Ensure that the BigNums have not been disposed
            if (left.fDisposed)
            {
                throw new ObjectDisposedException("left");
            }
            if (right.fDisposed)
            {
                throw new ObjectDisposedException("right");
            }

            return(OpenSSL.BN_cmp(left.fBigNum, right.fBigNum) == -1);
        }
Exemple #11
0
 public BigNumber(string data, string format)
 {
     fBigNum = OpenSSL.BN_new();
     if (format.ToLower() == "d")
     {
         OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(data));
     }
     else if (format.ToLower() == "x")
     {
         OpenSSL.BN_hex2bn(ref fBigNum, Encoding.ASCII.GetBytes(data));
     }
     else
     {
         throw new FormatException();
     }
 }
Exemple #12
0
 public static byte[] Random(int bytes)
 {
     if (!fSeeded)
     {
         Seed();
     }
     byte[] buf = new byte[bytes];
     if (OpenSSL.RAND_bytes(buf, bytes) == 1)
     {
         return(buf);
     }
     else
     {
         uint err = OpenSSL.ERR_get_error();
         throw new OpenSSLException(Encoding.ASCII.GetString(OpenSSL.ERR_get_error_string(err, null)));
     }
 }
Exemple #13
0
        public static BigNumber operator -(BigNumber left, BigNumber right)
        {
            //Ensure that the BigNums have not been disposed
            if (left.fDisposed)
            {
                throw new ObjectDisposedException("left");
            }
            if (right.fDisposed)
            {
                throw new ObjectDisposedException("right");
            }

            //Actually do the operation
            IntPtr r = OpenSSL.BN_new();

            OpenSSL.BN_sub(r, left.fBigNum, right.fBigNum);
            return(new BigNumber(r));
        }
Exemple #14
0
        /// <summary>
        /// Compares the values of two BigNums
        /// </summary>
        /// <param name="obj">BigNum to compare with</param>
        /// <returns>If the two values are equal</returns>
        public override bool Equals(object obj)
        {
            if (!(obj is BigNumber))
            {
                throw new ArgumentException("obj must be a BigNum to compare");
            }
            BigNumber cmp = obj as BigNumber;

            //Ensure that the BigNums have not been disposed
            if (fDisposed)
            {
                throw new ObjectDisposedException("this");
            }
            if (cmp.fDisposed)
            {
                throw new ObjectDisposedException("cmp");
            }

            return(OpenSSL.BN_cmp(fBigNum, cmp.fBigNum) == 0);
        }
Exemple #15
0
        public static BigNumber operator /(BigNumber left, BigNumber right)
        {
            //Ensure that the BigNums have not been disposed
            if (left.fDisposed)
            {
                throw new ObjectDisposedException("left");
            }
            if (right.fDisposed)
            {
                throw new ObjectDisposedException("right");
            }

            //Actually do the operation
            IntPtr res = OpenSSL.BN_new();
            IntPtr ctx = OpenSSL.BN_CTX_new();

            OpenSSL.BN_div(res, IntPtr.Zero, left.fBigNum, right.fBigNum, ctx);
            OpenSSL.BN_CTX_free(ctx);
            return(new BigNumber(res));
        }
Exemple #16
0
        /// <summary>
        /// Raises this BigNum to a power and takes the modulus
        /// </summary>
        /// <param name="exp">The exponent</param>
        /// <param name="mod">Value to use when taking the modulus</param>
        /// <returns>this**exp % mod</returns>
        public BigNumber PowMod(BigNumber exp, BigNumber mod)
        {
            if (fDisposed)
            {
                throw new ObjectDisposedException("this");
            }
            if (exp.fDisposed)
            {
                throw new ObjectDisposedException("exp");
            }
            if (mod.fDisposed)
            {
                throw new ObjectDisposedException("mod");
            }

            IntPtr r   = OpenSSL.BN_new();
            IntPtr ctx = OpenSSL.BN_CTX_new();

            OpenSSL.BN_mod_exp(r, fBigNum, exp.fBigNum, mod.fBigNum, ctx);
            OpenSSL.BN_CTX_free(ctx);
            return(new BigNumber(r));
        }
Exemple #17
0
 /// <summary>
 /// Creates a new BigNum instance from an array of bytes
 /// </summary>
 /// <param name="data">An array of bytes in OpenSSL byte order (big endian)</param>
 public BigNumber(byte[] data)
 {
     fBigNum = OpenSSL.BN_bin2bn(data, data.Length, IntPtr.Zero);
 }
Exemple #18
0
 public BigNumber()
 {
     fBigNum = OpenSSL.BN_new();
 }
Exemple #19
0
 /// <summary>
 /// Creates a new BigNum instance from a decimal string
 /// </summary>
 /// <param name="data">Number in decimal</param>
 public BigNumber(string data)
 {
     fBigNum = OpenSSL.BN_new();
     OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(data));
 }
Exemple #20
0
 /// <summary>
 /// Adds the content of the screen to the Pseudo RNG
 /// </summary>
 public static void Screen()
 {
     OpenSSL.RAND_screen();
     fSeeded = true;
 }
Exemple #21
0
 public BigNumber(int data)
 {
     fBigNum = OpenSSL.BN_new();
     OpenSSL.BN_dec2bn(ref fBigNum, Encoding.ASCII.GetBytes(Convert.ToString(data)));
 }
Exemple #22
0
 /// <summary>
 /// Seeds the RNG with user data
 /// </summary>
 /// <param name="data">Data to seed the RNG with</param>
 public static void Seed(byte[] data)
 {
     OpenSSL.RAND_seed(data, data.Length);
     fSeeded = true;
 }