Warrick FitzGerald
Warrick FitzGerald

Reputation: 2835

deterministic signatures using BouncyCastle SHA-256withPLAIN-ECDSA

I'm currently signing using code that looks as below. I'm trying to find a way to create deterministic signatures. Is that possible with BouncyCastle?

var curve = SecNamedCurves.GetByName("secp256r1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);

var p = new Org.BouncyCastle.Math.BigInteger(privateKey);

var keyParameters = new ECPrivateKeyParameters(
                        new Org.BouncyCastle.Math.BigInteger(1,privateKey),
                        domain);

ISigner signer = SignerUtilities.GetSigner("SHA-256withPLAIN-ECDSA");

signer.Init(true, keyParameters);
signer.BlockUpdate(message, 0, message.Length);
var signature = signer.GenerateSignature();

Upvotes: 1

Views: 1123

Answers (1)

Topaco
Topaco

Reputation: 49400

Yes it is. The following shows the generation of a signature with deterministic ECDSA using curve P-256 aka secp256r1 and a test vector from RFC6979, A.2.5:

  • Test vector:

private key:
x = C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721

With SHA-256, message = "sample":
r = EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
s = F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8

  • C#/BC Code
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Text;
...
string messageToSign = "sample";
var digest = new System.Security.Cryptography.SHA256Managed();
byte[] messageHash = digest.ComputeHash(Encoding.UTF8.GetBytes(messageToSign));

X9ECParameters x9Params = SecNamedCurves.GetByName("secp256r1"); ;
ECDomainParameters ecParams = new ECDomainParameters(x9Params.Curve, x9Params.G, x9Params.N, x9Params.H);
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(new BigInteger(1, Hex.Decode("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")), ecParams);
ECDsaSigner signer = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest()));
signer.Init(true, privKey);
var signature = signer.GenerateSignature(messageHash);

Console.WriteLine(signature[0].ToString(16).ToUpper()); // r: EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
Console.WriteLine(signature[1].ToString(16).ToUpper()); // s: F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8

The output:

EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8

matches the signature of the test vector.

Upvotes: 2

Related Questions