Example #1
0
        // https://stackoverflow.com/questions/17439732/recreating-keys-ecpublickeyparameters-in-c-sharp-with-bouncycastle
        // TODO: find curve name...
        private static Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters CreateEcPublicKeyParameters(
            Org.BouncyCastle.Math.BigInteger xx
            , Org.BouncyCastle.Math.BigInteger yy)
        {
            // Org.BouncyCastle.Math.EC.ECPoint q = new Org.BouncyCastle.Math.EC.ECPoint(x, y);
            // Org.BouncyCastle.Crypto.Tls.NamedCurve.secp224k1


            // DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1); // Alg 13
            // DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1); // Alg 14
            // DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);

            string curveName = "P-521";

            Org.BouncyCastle.Asn1.X9.X9ECParameters ecP = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName(curveName);
            Org.BouncyCastle.Math.EC.FpCurve        c   = (Org.BouncyCastle.Math.EC.FpCurve)ecP.Curve;

            Org.BouncyCastle.Math.EC.ECFieldElement x = c.FromBigInteger(xx);
            Org.BouncyCastle.Math.EC.ECFieldElement y = c.FromBigInteger(yy);
            Org.BouncyCastle.Math.EC.ECPoint        q = new Org.BouncyCastle.Math.EC.FpPoint(c, x, y);


            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters publicParams = null;
            // Org.BouncyCastle.Crypto.Parameters.ECKeyParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);
            // Org.BouncyCastle.Crypto.AsymmetricKeyParameter publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);
            // Org.BouncyCastle.Crypto.ICipherParameters publicParams = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain);


            // Org.BouncyCastle.Crypto.Digests.GeneralDigest.
            // Org.BouncyCastle.Crypto.Signers.GenericSigner
            // Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator


            // Org.BouncyCastle.Security.SignerUtilities.GetSigner("SHA-384withRSA");
            // Org.BouncyCastle.Security.DigestUtilities.GetDigest("");
            // Org.BouncyCastle.Security.CipherUtilities.GetCipher("");
            // Org.BouncyCastle.Security.GeneratorUtilities.GetKeyGenerator("");
            // Org.BouncyCastle.Security.WrapperUtilities.GetAlgorithmName
            // Org.BouncyCastle.Security.MacUtilities.CalculateMac("", null, System.Text.Encoding.UTF8.GetBytes("HashThis"));
            // Org.BouncyCastle.Security.ParameterUtilities.CreateKeyParameter("name", new byte[] { });
            // Org.BouncyCastle.Security.ParameterUtilities.GenerateParameters("name", new Org.BouncyCastle.Security.SecureRandom());
            // Org.BouncyCastle.Security.PublicKeyFactory.CreateKey()



            // Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters oara = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters("ECDH", q, Org.BouncyCastle.Asn1.Sec.SecObjectIdentifiers.SecP521r1);

            return(publicParams);
        }
Example #2
0
        public PubKey Derivate(byte[] cc, uint nChild, out byte[] ccChild)
        {
            byte[] lr = null;
            byte[] l  = new byte[32];
            byte[] r  = new byte[32];
            if ((nChild >> 31) == 0)
            {
                var pubKey = ToBytes();
                lr = Hashes.BIP32Hash(cc, nChild, pubKey[0], pubKey.Skip(1).ToArray());
            }
            else
            {
                throw new InvalidOperationException("Impossible to derivate a child key from a hardened one");
            }
            Array.Copy(lr, l, 32);
            Array.Copy(lr, 32, r, 0, 32);
            ccChild = r;


            BigInteger N          = ECKey.CURVE.N;
            BigInteger kPar       = new BigInteger(1, this.vch);
            BigInteger parse256LL = new BigInteger(1, l);

            if (parse256LL.CompareTo(N) >= 0)
            {
                throw new InvalidOperationException("You won a prize ! this should happen very rarely. Take a screenshot, and roll the dice again.");
            }

            var q = ECKey.CURVE.G.Multiply(parse256LL).Add(_Key.GetPublicKeyParameters().Q);

            if (q.IsInfinity)
            {
                throw new InvalidOperationException("You won the big prize ! this would happen only 1 in 2^127. Take a screenshot, and roll the dice again.");
            }

            var p = new Org.BouncyCastle.Math.EC.FpPoint(ECKey.CURVE.Curve, q.X, q.Y, true);

            return(new PubKey(p.GetEncoded()));
        }
Example #3
0
        public PubKey Derivate(byte[] cc, uint nChild, out byte[] ccChild)
        {
            byte[] lr = null;
            byte[] l = new byte[32];
            byte[] r = new byte[32];
            if((nChild >> 31) == 0)
            {
                var pubKey = ToBytes();
                lr = Hashes.BIP32Hash(cc, nChild, pubKey[0], pubKey.Skip(1).ToArray());
            }
            else
            {
                throw new InvalidOperationException("Impossible to derivate a child key from a hardened one");
            }
            Array.Copy(lr, l, 32);
            Array.Copy(lr, 32, r, 0, 32);
            ccChild = r;

            BigInteger N = ECKey.CURVE.N;
            BigInteger kPar = new BigInteger(1, this.vch);
            BigInteger parse256LL = new BigInteger(1, l);

            if(parse256LL.CompareTo(N) >= 0)
                throw new InvalidOperationException("You won a prize ! this should happen very rarely. Take a screenshot, and roll the dice again.");

            var q = ECKey.CURVE.G.Multiply(parse256LL).Add(_Key.GetPublicKeyParameters().Q);
            if(q.IsInfinity)
                throw new InvalidOperationException("You won the big prize ! this would happen only 1 in 2^127. Take a screenshot, and roll the dice again.");

            var p = new Org.BouncyCastle.Math.EC.FpPoint(ECKey.CURVE.Curve, q.X, q.Y, true);
            return new PubKey(p.GetEncoded());
        }