Пример #1
0
        internal static SafeEcKeyHandle EcKeyCreateByExplicitCurve(ECCurve curve)
        {
            byte[] p;
            if (curve.IsPrime)
            {
                p = curve.Prime;
            }
            else if (curve.IsCharacteristic2)
            {
                p = curve.Polynomial;
            }
            else
            {
                throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, curve.CurveType.ToString()));
            }

            SafeEcKeyHandle key = Interop.Crypto.EcKeyCreateByExplicitParameters(
                curve.CurveType,
                null, 0,
                null, 0,
                null, 0,
                p, p.Length,
                curve.A, curve.A.Length,
                curve.B, curve.B.Length,
                curve.G.X, curve.G.X.Length,
                curve.G.Y, curve.G.Y.Length,
                curve.Order, curve.Order.Length,
                curve.Cofactor, curve.Cofactor.Length,
                curve.Seed, curve.Seed == null ? 0 : curve.Seed.Length);

            if (key == null || key.IsInvalid)
            {
                if (key != null)
                {
                    key.Dispose();
                }
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }

            // EcKeyCreateByExplicitParameters may have polluted the error queue, but key was good in the end.
            // Clean up the error queue.
            Interop.Crypto.ErrClearError();

            return(key);
        }
        internal static SafeEcKeyHandle EcKeyCreateByExplicitCurve(ECCurve curve)
        {
            byte[] p;
            if (curve.IsPrime)
            {
                p = curve.Prime !;
            }
            else if (curve.IsCharacteristic2)
            {
                p = curve.Polynomial !;
            }
            else
            {
                throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, curve.CurveType.ToString()));
            }

            SafeEcKeyHandle key = EcKeyCreateByExplicitParameters(
                curve.CurveType,
                null, 0,
                null, 0,
                null, 0,
                p, p.Length,
                curve.A !, curve.A !.Length,
                curve.B !, curve.B !.Length,
                curve.G.X !, curve.G.X !.Length,
                curve.G.Y !, curve.G.Y !.Length,
                curve.Order !, curve.Order !.Length,
                curve.Cofactor, curve.Cofactor !.Length,
                curve.Seed, curve.Seed == null ? 0 : curve.Seed.Length);

            if (key == null || key.IsInvalid)
            {
                if (key != null)
                {
                    key.Dispose();
                }
                throw new CryptographicException();
            }

            return(key);
        }