Esempio n. 1
0
        public override void GenerateKey(ECCurve curve)
        {
            curve.Validate();
            var keySizeInByted = curve.Prime.Length;

            KeySize = keySizeInByted * 8;

            BigInteger
                prime         = CryptoUtils.Normalize(new BigInteger(curve.Prime), _modulus),
                subgroupOrder = CryptoUtils.Normalize(new BigInteger(curve.Order), _modulus) /
                                CryptoUtils.Normalize(new BigInteger(curve.Cofactor), _modulus),
                a = CryptoUtils.Normalize(new BigInteger(curve.A), _modulus);

            var        privateKey = new byte[keySizeInByted];
            BigInteger key;

            do
            {
                CryptoUtils.StaticRandomNumberGenerator.GetBytes(privateKey);
                key = CryptoUtils.Normalize(new BigInteger(privateKey), _modulus);
            } while (BigInteger.Zero >= key || key >= subgroupOrder);

            var basePoint = new BigIntegerPoint(curve.G, _modulus);

            var publicKey = BigIntegerPoint.Multiply(basePoint, key, prime, a).ToECPoint(KeySize);

            CryptoUtils.EraseData(ref _privateKey);
            _curve         = curve.Clone();
            _publicKey     = publicKey;
            _privateKey    = privateKey;
            _parametersSet = true;
        }
Esempio n. 2
0
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="MagmaManagedTransform" />
 /// class and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 /// <see langword="true"/> to release both managed and unmanaged resources;
 /// <see langword="false"/> to release only unmanaged resources.
 /// </param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         CryptoUtils.EraseData(ref _keyExpansion);
     }
     base.Dispose(disposing);
 }
Esempio n. 3
0
        private void Reset()
        {
            CryptoUtils.EraseData(ref _depadBuffer);

            if (_cipherMode == CipherMode.CBC || _cipherMode == CipherMode.CFB || _cipherMode == CipherMode.OFB)
            {
                Buffer.BlockCopy(_rgbIV, 0, _stateBuffer, 0, _rgbIV !.Length);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="SymmetricTransform" /> class
 /// and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 /// <see langword="true"/> to release both managed and unmanaged resources;
 /// <see langword="false"/> to release only unmanaged resources.
 /// </param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         CryptoUtils.EraseData(ref _rgbKey !);
         CryptoUtils.EraseData(ref _rgbIV);
         CryptoUtils.EraseData(ref _depadBuffer);
         CryptoUtils.EraseData(ref _stateBuffer);
         CryptoUtils.EraseData(ref _tempBuffer);
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="GostECDsa256Managed"/> class
        /// and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">
        /// <see langword="true"/>true to release both managed and unmanaged resources;
        /// <see langword="false"/> to release only unmanaged resources.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                CryptoUtils.EraseData(ref _privateKey);

                if (disposing)
                {
                    CryptoUtils.EraseData(ref _curve.Prime);
                    CryptoUtils.EraseData(ref _curve.A);
                    CryptoUtils.EraseData(ref _curve.B);
                    CryptoUtils.EraseData(ref _curve.Order);
                    CryptoUtils.EraseData(ref _curve.Cofactor);
                    CryptoUtils.EraseData(ref _publicKey.X);
                    CryptoUtils.EraseData(ref _publicKey.Y);
                    var g = _curve.G;
                    CryptoUtils.EraseData(ref g.X);
                    CryptoUtils.EraseData(ref g.Y);
                }
            }

            base.Dispose(disposing);
            _disposed = true;
        }