private static ECKey GetPrivKey(EcCurveInformation curve, string encodedKey) { var bi = new BigInteger(encodedKey, 16); var privKey = new ECKey { PublicComponent = false, CurveProviderName = EcInformationStore.GetProvider(curve.Name), CurveName = curve.Name, EncodedKey = bi.ToByteArray() }; return(privKey); }
/// <summary> /// Create a new elliptic curve keypair. /// </summary> /// <param name="curveName">Name of the elliptic curve to use as the basis.</param> /// <returns>Elliptic curve keypair.</returns> public static ECKeypair GenerateECKeypair(string curveName, bool generateCanary = true) { ECKeypair keypair; if (curveName.Equals(DjbCurve.Curve25519.ToString())) { var privEntropy = new byte[Curve25519.PrivateKeySeedSizeInBytes]; StratCom.EntropySupplier.NextBytes(privEntropy); byte[] privateKey = Curve25519.CreatePrivateKey(privEntropy); byte[] publicKey = Curve25519.CreatePublicKey(privateKey); byte[] canary = null; if (generateCanary) { canary = new byte[128.BitsToBytes()]; StratCom.EntropySupplier.NextBytes(canary); } keypair = new ECKeypair { CurveProviderName = "DJB", CurveName = DjbCurve.Curve25519.ToString(), EncodedPublicKey = publicKey, EncodedPrivateKey = privateKey, UsePermissions = AsymmetricKeyUsePermission.KeyAgreements, ContextPermissions = KeyUseContextPermission.ManifestHeader, ConfirmationCanary = canary }; privEntropy.SecureWipe(); } else if (curveName.Equals(DjbCurve.Ed25519.ToString())) { var privEntropy = new byte[Ed25519.PrivateKeySeedSizeInBytes]; StratCom.EntropySupplier.NextBytes(privEntropy); byte[] privateKey = new byte[Ed25519.ExpandedPrivateKeySizeInBytes]; byte[] publicKey = new byte[Ed25519.PublicKeySizeInBytes]; Ed25519.KeyPairFromSeed(out publicKey, out privateKey, privEntropy); byte[] canary = null; if (generateCanary) { canary = new byte[128.BitsToBytes()]; StratCom.EntropySupplier.NextBytes(canary); } keypair = new ECKeypair { CurveProviderName = "DJB", CurveName = DjbCurve.Ed25519.ToString(), EncodedPublicKey = publicKey, EncodedPrivateKey = privateKey, UsePermissions = AsymmetricKeyUsePermission.KeyAgreements | AsymmetricKeyUsePermission.Signatures, ContextPermissions = KeyUseContextPermission.ManifestHeader, ConfirmationCanary = canary }; privEntropy.SecureWipe(); } else { ECPoint Q; BigInteger d; GenerateECKeypair(curveName, out Q, out d); byte[] canary = null; if (generateCanary) { canary = new byte[128.BitsToBytes()]; StratCom.EntropySupplier.NextBytes(canary); } keypair = new ECKeypair { CurveProviderName = EcInformationStore.GetProvider(curveName), CurveName = curveName, EncodedPublicKey = Q.GetEncoded(), EncodedPrivateKey = d.ToByteArray(), UsePermissions = AsymmetricKeyUsePermission.KeyAgreements | AsymmetricKeyUsePermission.Signatures, ContextPermissions = KeyUseContextPermission.ManifestHeader, ConfirmationCanary = canary }; } return(keypair); }