Ejemplo n.º 1
0
        /// <summary>
        ///     Creates the key exchange data.
        /// </summary>
        /// <returns>The key exchange data to be sent to the intended recipient.</returns>
        public override byte[] CreateKeyExchange()
        {
            BigInteger y = m_G.ModPow(m_X, m_P);

            byte[] ret = y.GetBytes();
            y.Clear();
            return(ret);
        }
        public override byte[] CreateKeyExchange()
        {
            BigInteger bigInteger = this.m_G.ModPow(this.m_X, this.m_P);

            byte[] bytes = bigInteger.GetBytes();
            bigInteger.Clear();
            return(bytes);
        }
        public override byte[] DecryptKeyExchange(byte[] keyEx)
        {
            BigInteger bigInteger = new BigInteger(keyEx).ModPow(this.m_X, this.m_P);

            byte[] bytes = bigInteger.GetBytes();
            bigInteger.Clear();
            return(bytes);
        }
        private void Dispose()
        {
            if (!_disposed)
            {
                _p.Clear(); _g.Clear(); _x.Clear();
            }

            _disposed = true;
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Extracts secret information from the key exchange data.
        /// </summary>
        /// <param name="keyEx">The key exchange data within which the shared key is hidden.</param>
        /// <returns>The shared key derived from the key exchange data.</returns>
        public override byte[] DecryptKeyExchange(byte[] keyEx)
        {
            var        pvr = new BigInteger(keyEx);
            BigInteger z   = pvr.ModPow(m_X, m_P);

            byte[] ret = z.GetBytes();
            z.Clear();
            return(ret);
        }
Ejemplo n.º 6
0
 /// <summary>
 ///     Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 ///     <b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only
 ///     unmanaged resources.
 /// </param>
 protected override void Dispose(bool disposing)
 {
     if (!m_Disposed)
     {
         m_P.Clear();
         m_G.Clear();
         m_X.Clear();
     }
     m_Disposed = true;
 }
Ejemplo n.º 7
0
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                // Always zeroize private key
                if (d != null)
                {
                    d.Clear();
                    d = null;
                }
                if (p != null)
                {
                    p.Clear();
                    p = null;
                }
                if (q != null)
                {
                    q.Clear();
                    q = null;
                }
                if (dp != null)
                {
                    dp.Clear();
                    dp = null;
                }
                if (dq != null)
                {
                    dq.Clear();
                    dq = null;
                }
                if (qInv != null)
                {
                    qInv.Clear();
                    qInv = null;
                }

                if (disposing)
                {
                    // clear public key
                    if (e != null)
                    {
                        e.Clear();
                        e = null;
                    }
                    if (n != null)
                    {
                        n.Clear();
                        n = null;
                    }
                }
            }
            // call base class
            // no need as they all are abstract before us
            m_disposed = true;
        }
Ejemplo n.º 8
0
        public override byte[] DecryptValue(byte[] rgb)
        {
            if (this.m_disposed)
            {
                throw new ObjectDisposedException("private key");
            }
            if (!this.keypairGenerated)
            {
                this.GenerateKeyPair();
            }
            BigInteger bigInteger1 = new BigInteger(rgb);
            BigInteger bigInteger2 = (BigInteger)null;

            if (this.keyBlinding)
            {
                bigInteger2 = BigInteger.GenerateRandom(this.n.BitCount());
                bigInteger1 = bigInteger2.ModPow(this.e, this.n) * bigInteger1 % this.n;
            }
            BigInteger bigInteger3;

            if (this.isCRTpossible)
            {
                BigInteger bigInteger4 = bigInteger1.ModPow(this.dp, this.p);
                BigInteger bigInteger5 = bigInteger1.ModPow(this.dq, this.q);
                if (bigInteger5 > bigInteger4)
                {
                    BigInteger bigInteger6 = this.p - (bigInteger5 - bigInteger4) * this.qInv % this.p;
                    bigInteger3 = bigInteger5 + this.q * bigInteger6;
                }
                else
                {
                    BigInteger bigInteger6 = (bigInteger4 - bigInteger5) * this.qInv % this.p;
                    bigInteger3 = bigInteger5 + this.q * bigInteger6;
                }
            }
            else
            {
                if (this.PublicOnly)
                {
                    throw new CryptographicException(Locale.GetText("Missing private key to decrypt value."));
                }
                bigInteger3 = bigInteger1.ModPow(this.d, this.n);
            }
            if (this.keyBlinding)
            {
                bigInteger3 = bigInteger3 * bigInteger2.ModInverse(this.n) % this.n;
                bigInteger2.Clear();
            }
            byte[] paddedValue = this.GetPaddedValue(bigInteger3, this.KeySize >> 3);
            bigInteger1.Clear();
            bigInteger3.Clear();
            return(paddedValue);
        }
Ejemplo n.º 9
0
        public override byte[] DecryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("private key");
            }
            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }
            BigInteger bigInteger  = new BigInteger(rgb);
            BigInteger bigInteger2 = null;

            if (keyBlinding)
            {
                bigInteger2 = BigInteger.GenerateRandom(n.BitCount());
                bigInteger  = bigInteger2.ModPow(e, n) * bigInteger % n;
            }
            BigInteger bigInteger5;

            if (isCRTpossible)
            {
                BigInteger bigInteger3 = bigInteger.ModPow(dp, p);
                BigInteger bigInteger4 = bigInteger.ModPow(dq, q);
                if (bigInteger4 > bigInteger3)
                {
                    BigInteger bi = p - (bigInteger4 - bigInteger3) * qInv % p;
                    bigInteger5 = bigInteger4 + q * bi;
                }
                else
                {
                    BigInteger bi = (bigInteger3 - bigInteger4) * qInv % p;
                    bigInteger5 = bigInteger4 + q * bi;
                }
            }
            else
            {
                if (PublicOnly)
                {
                    throw new CryptographicException(Locale.GetText("Missing private key to decrypt value."));
                }
                bigInteger5 = bigInteger.ModPow(d, n);
            }
            if (keyBlinding)
            {
                bigInteger5 = bigInteger5 * bigInteger2.ModInverse(n) % n;
                bigInteger2.Clear();
            }
            byte[] paddedValue = GetPaddedValue(bigInteger5, KeySize >> 3);
            bigInteger.Clear();
            bigInteger5.Clear();
            return(paddedValue);
        }
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                // Always zeroize private key
                if (x != null)
                {
                    x.Clear();
                    x = null;
                }

                if (disposing)
                {
                    // clear group
                    if (p != null)
                    {
                        p.Clear();
                        p = null;
                    }
                    if (q != null)
                    {
                        q.Clear();
                        q = null;
                    }
                    if (g != null)
                    {
                        g.Clear();
                        g = null;
                    }
                    if (j != null)
                    {
                        j.Clear();
                        j = null;
                    }
                    if (seed != null)
                    {
                        seed.Clear();
                        seed = null;
                    }
                    // clear public key
                    if (y != null)
                    {
                        y.Clear();
                        y = null;
                    }
                }
            }
            // call base class
            // no need as they all are abstract before us
            m_disposed = true;
        }
Ejemplo n.º 11
0
 protected override void Dispose(bool disposing)
 {
     if (!m_disposed)
     {
         if (d != null)
         {
             d.Clear();
             d = null;
         }
         if (p != null)
         {
             p.Clear();
             p = null;
         }
         if (q != null)
         {
             q.Clear();
             q = null;
         }
         if (dp != null)
         {
             dp.Clear();
             dp = null;
         }
         if (dq != null)
         {
             dq.Clear();
             dq = null;
         }
         if (qInv != null)
         {
             qInv.Clear();
             qInv = null;
         }
         if (disposing)
         {
             if (e != null)
             {
                 e.Clear();
                 e = null;
             }
             if (n != null)
             {
                 n.Clear();
                 n = null;
             }
         }
     }
     m_disposed = true;
 }
Ejemplo n.º 12
0
        public override byte[] DecryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("private key");
            }

            // decrypt operation is used for signature
            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            BigInteger input = new BigInteger(rgb);
            BigInteger output;

            // decrypt (which uses the private key) can be
            // optimized by using CRT (Chinese Remainder Theorem)
            if (isCRTpossible)
            {
                // m1 = c^dp mod p
                BigInteger m1 = input.ModPow(dp, p);
                // m2 = c^dq mod q
                BigInteger m2 = input.ModPow(dq, q);
                BigInteger h;
                if (m2 > m1)
                {
                    // thanks to benm!
                    h      = p - ((m2 - m1) * qInv % p);
                    output = m2 + q * h;
                }
                else
                {
                    // h = (m1 - m2) * qInv mod p
                    h = (m1 - m2) * qInv % p;
                    // m = m2 + q * h;
                    output = m2 + q * h;
                }
            }
            else
            {
                // m = c^d mod n
                output = input.ModPow(d, n);
            }
            byte[] result = output.GetBytes();
            // zeroize value
            input.Clear();
            output.Clear();
            return(result);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!m_Disposed)
                {
                    m_P.Clear();
                    m_G.Clear();
                    m_X.Clear();
                }
                m_Disposed = true;
            }

            GC.SuppressFinalize(this);
        }
Ejemplo n.º 14
0
        public override byte[] EncryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("public key");
            }
            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }
            BigInteger bigInteger  = new BigInteger(rgb);
            BigInteger bigInteger2 = bigInteger.ModPow(e, n);

            byte[] paddedValue = GetPaddedValue(bigInteger2, KeySize >> 3);
            bigInteger.Clear();
            bigInteger2.Clear();
            return(paddedValue);
        }
Ejemplo n.º 15
0
        public override byte[] EncryptValue(byte[] rgb)
        {
            if (this.m_disposed)
            {
                throw new ObjectDisposedException("public key");
            }
            if (!this.keypairGenerated)
            {
                this.GenerateKeyPair();
            }
            BigInteger bigInteger1 = new BigInteger(rgb);
            BigInteger bigInteger2 = bigInteger1.ModPow(this.e, this.n);

            byte[] paddedValue = this.GetPaddedValue(bigInteger2, this.KeySize >> 3);
            bigInteger1.Clear();
            bigInteger2.Clear();
            return(paddedValue);
        }
Ejemplo n.º 16
0
        public override byte[] EncryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("public key");
            }

            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            BigInteger input  = new BigInteger(rgb);
            BigInteger output = input.ModPow(e, n);

            byte[] result = output.GetBytes();
            // zeroize value
            input.Clear();
            output.Clear();
            return(result);
        }
Ejemplo n.º 17
0
        public override byte[] EncryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("public key");
            }

            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            BigInteger input  = new BigInteger(rgb);
            BigInteger output = input.ModPow(e, n);

            // it's sometimes possible for the results to be a byte short
            // and this can break some software (see #79502) so we 0x00 pad the result
            byte[] result = GetPaddedValue(output, (KeySize >> 3));
            // zeroize value
            input.Clear();
            output.Clear();
            return(result);
        }
Ejemplo n.º 18
0
        public override byte[] DecryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("private key");
            }

            // decrypt operation is used for signature
            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            BigInteger input = new BigInteger(rgb);
            BigInteger r     = null;

            // we use key blinding (by default) against timing attacks
            if (keyBlinding)
            {
                // x = (r^e * g) mod n
                // *new* random number (so it's timing is also random)
                r     = BigInteger.GenerateRandom(n.BitCount());
                input = r.ModPow(e, n) * input % n;
            }

            BigInteger output;

            // decrypt (which uses the private key) can be
            // optimized by using CRT (Chinese Remainder Theorem)
            if (isCRTpossible)
            {
                // m1 = c^dp mod p
                BigInteger m1 = input.ModPow(dp, p);
                // m2 = c^dq mod q
                BigInteger m2 = input.ModPow(dq, q);
                BigInteger h;
                if (m2 > m1)
                {
                    // thanks to benm!
                    h      = p - ((m2 - m1) * qInv % p);
                    output = m2 + q * h;
                }
                else
                {
                    // h = (m1 - m2) * qInv mod p
                    h = (m1 - m2) * qInv % p;
                    // m = m2 + q * h;
                    output = m2 + q * h;
                }
            }
            else
            {
                // m = c^d mod n
                output = input.ModPow(d, n);
            }

            if (keyBlinding)
            {
                // Complete blinding
                // x^e / r mod n
                output = output * r.ModInverse(n) % n;
                r.Clear();
            }

            byte[] result = output.GetBytes();
            // zeroize values
            input.Clear();
            output.Clear();
            return(result);
        }
Ejemplo n.º 19
0
        public override byte[] DecryptValue(byte[] rgb)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException("private key");
            }

            // decrypt operation is used for signature
            if (!keypairGenerated)
            {
                GenerateKeyPair();
            }

            BigInteger input = new BigInteger(rgb);
            BigInteger r     = null;

            // we use key blinding (by default) against timing attacks
            if (keyBlinding)
            {
                // x = (r^e * g) mod n
                // *new* random number (so it's timing is also random)
                r     = BigInteger.GenerateRandom(n.BitCount());
                input = r.ModPow(e, n) * input % n;
            }

            BigInteger output;

            // decrypt (which uses the private key) can be
            // optimized by using CRT (Chinese Remainder Theorem)
            if (isCRTpossible)
            {
                // m1 = c^dp mod p
                BigInteger m1 = input.ModPow(dp, p);
                // m2 = c^dq mod q
                BigInteger m2 = input.ModPow(dq, q);
                BigInteger h;
                if (m2 > m1)
                {
                    // thanks to benm!
                    h      = p - ((m2 - m1) * qInv % p);
                    output = m2 + q * h;
                }
                else
                {
                    // h = (m1 - m2) * qInv mod p
                    h = (m1 - m2) * qInv % p;
                    // m = m2 + q * h;
                    output = m2 + q * h;
                }
            }
            else if (!PublicOnly)
            {
                // m = c^d mod n
                output = input.ModPow(d, n);
            }
            else
            {
                throw new CryptographicException(Locale.GetText("Missing private key to decrypt value."));
            }

            if (keyBlinding)
            {
                // Complete blinding
                // x^e / r mod n
                output = output * r.ModInverse(n) % n;
                r.Clear();
            }

            // it's sometimes possible for the results to be a byte short
            // and this can break some software (see #79502) so we 0x00 pad the result
            byte[] result = GetPaddedValue(output, (KeySize >> 3));
            // zeroize values
            input.Clear();
            output.Clear();
            return(result);
        }