Exemplo n.º 1
0
        public static byte[] SignECDsa(byte[] message, byte[] prikey, byte[] pubkey, ECDsaCurve curve)
        {
            NetCrypto.ECCurve usedCurve = NetCrypto.ECCurve.NamedCurves.nistP256;
            switch (curve)
            {
            case ECDsaCurve.Secp256r1:
                // default
                break;

            case ECDsaCurve.Secp256k1:
                var oid = NetCrypto.Oid.FromFriendlyName("secP256k1", NetCrypto.OidGroup.PublicKeyAlgorithm);
                usedCurve = NetCrypto.ECCurve.CreateFromOid(oid);
                break;
            }
            ;

            using (var ecdsa = NetCrypto.ECDsa.Create(new NetCrypto.ECParameters
            {
                Curve = usedCurve,
                D = prikey,
                Q = ECPointDecode(pubkey, curve)
            }))
            {
                return(ecdsa.SignData(message, NetCrypto.HashAlgorithmName.SHA256));
            }
        }
Exemplo n.º 2
0
        public static ECCurve GetCurve(this ECDsaCurve curve)
        {
            switch (curve)
            {
            case ECDsaCurve.Secp256k1: return(ECCurve.Secp256k1);

            case ECDsaCurve.Secp256r1: return(ECCurve.Secp256r1);

            default: return(null);
            }
        }
Exemplo n.º 3
0
        private static NetCrypto.ECPoint ECPointDecode(byte[] pubKey, ECDsaCurve curve)
        {
            ECCurve usedCurve = ECC.ECCurve.Secp256r1;

            switch (curve)
            {
            case ECDsaCurve.Secp256r1:
                // default
                break;

            case ECDsaCurve.Secp256k1:
                usedCurve = ECC.ECCurve.Secp256k1;
                break;
            }
            ;

            byte[] bytes;
            if (pubKey.Length == 32)
            {
                pubKey = ByteArrayUtils.ConcatBytes(new byte[] { 2 }, pubKey.Skip(1).ToArray());
            }

            if (pubKey.Length == 33 && (pubKey[0] == 0x02 || pubKey[0] == 0x03))
            {
                try
                {
                    bytes = ECC.ECPoint.DecodePoint(pubKey, usedCurve).EncodePoint(false).Skip(1).ToArray();
                }
                catch
                {
                    throw new ArgumentException();
                }
            }
            else if (pubKey.Length == 65 && pubKey[0] == 0x04)
            {
                bytes = pubKey.Skip(1).ToArray();
            }
            else
            if (pubKey.Length != 64)
            {
                throw new ArgumentException();
            }
            else
            {
                bytes = pubKey;
            }

            return(new NetCrypto.ECPoint
            {
                X = bytes.Take(32).ToArray(),
                Y = bytes.Skip(32).ToArray()
            });
        }
Exemplo n.º 4
0
        public static ECDsaSignature Generate(IKeyPair keypair, byte[] message, ECDsaCurve curve)
        {
            Throw.If(curve != ECDsaCurve.Secp256r1, "curve support not implemented for " + curve);

            if (keypair.PublicKey.Length != 33)
            {
                throw new System.Exception("public key must be 33 bytes");
            }

            var signature = CryptoExtensions.SignECDsa(message, keypair.PrivateKey, keypair.PublicKey);

            return(new ECDsaSignature(signature, curve));
        }
Exemplo n.º 5
0
        public static ECDsaSignature Generate(IKeyPair keypair, byte[] message, ECDsaCurve curve, Func <byte[], byte[], byte[], byte[]> customSignFunction = null)
        {
            if (keypair.PublicKey.Length != 32 && keypair.PublicKey.Length != 33 && keypair.PublicKey.Length != 64)
            {
                throw new System.Exception($"public key must be 32, 33 or 64 bytes, given key's length: {keypair.PublicKey.Length}");
            }

            byte[] signature;
            if (customSignFunction != null)
            {
                signature = customSignFunction(message, keypair.PrivateKey, keypair.PublicKey);
            }
            else
            {
                signature = CryptoExtensions.SignECDsa(message, keypair.PrivateKey, keypair.PublicKey, curve);
            }

            return(new ECDsaSignature(signature, curve));
        }
Exemplo n.º 6
0
        public ECDsaCertificateSign(X509Certificate2 certificate)
        {
            var curveOid = OidParser.ReadFromBytes(certificate.PublicKey.EncodedParameters.RawData);

            switch (curveOid.Value)
            {
            case KnownOids.EccCurves.EcdsaP256:
                ECDsaCurve = ECDsaCurve.p256;
                break;

            case KnownOids.EccCurves.EcdsaP384:
                ECDsaCurve = ECDsaCurve.p384;
                break;

            case KnownOids.EccCurves.EcdsaP521:
                ECDsaCurve = ECDsaCurve.p521;
                break;

            default:
                throw new NotSupportedException("The specified ECC curve is not supported.");
            }
            _algorithm = certificate.GetECDsaPrivateKey();
        }
Exemplo n.º 7
0
 public override void UnserializeData(BinaryReader reader)
 {
     this.Curve = (ECDsaCurve)reader.ReadByte();
     this.Bytes = reader.ReadByteArray();
 }
Exemplo n.º 8
0
 public ECDsaSignature(byte[] bytes, ECDsaCurve curve)
 {
     this.Bytes = bytes;
     this.Curve = curve;
 }
Exemplo n.º 9
0
        public static bool VerifySignatureECDsa(byte[] message, byte[] signature, byte[] pubkey, ECDsaCurve curve)
        {
            NetCrypto.ECCurve usedCurve = NetCrypto.ECCurve.NamedCurves.nistP256;
            switch (curve)
            {
            case ECDsaCurve.Secp256r1:
                // default
                break;

            case ECDsaCurve.Secp256k1:
                var oid = NetCrypto.Oid.FromFriendlyName("secP256k1", NetCrypto.OidGroup.PublicKeyAlgorithm);
                usedCurve = NetCrypto.ECCurve.CreateFromOid(oid);
                break;
            }
            ;
#if NET461
            const int ECDSA_PUBLIC_P256_MAGIC = 0x31534345;
            pubkey = BitConverter.GetBytes(ECDSA_PUBLIC_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey).ToArray();
            using (CngKey key = CngKey.Import(pubkey, CngKeyBlobFormat.EccPublicBlob))
                using (ECDsaCng ecdsa = new ECDsaCng(key))
                {
                    return(ecdsa.VerifyData(message, signature, HashAlgorithmName.SHA256));
                }
#else
            using (var ecdsa = NetCrypto.ECDsa.Create(new NetCrypto.ECParameters
            {
                Curve = usedCurve,
                Q = ECPointDecode(pubkey, curve)
            }))
            {
                return(ecdsa.VerifyData(message, signature, NetCrypto.HashAlgorithmName.SHA256));
            }
#endif
        }
Exemplo n.º 10
0
        public static SharedSecret Encrypt <T>(T message, PhantasmaKeys privateKey, Address publicAddress, ECDsaCurve curve)
        {
            var ecdCurve  = curve.GetCurve();
            var pubBytes  = ECDsaSignature.ExtractPublicKeyFromAddress(publicAddress);
            var publicKey = ECC.ECPoint.DecodePoint(pubBytes, ecdCurve);
            var secret    = GetSharedSecret(privateKey, publicKey);
            var payload   = Encrypt(message, secret);

            return(new SharedSecret(curve, publicAddress, payload));
        }
Exemplo n.º 11
0
 public SharedSecret(ECDsaCurve curve, Address address, byte[] payload)
 {
     this.Curve   = curve;
     this.Address = address;
     this.Payload = payload;
 }