deterministic signatures using BouncyCastle SHA-256withPLAIN-ECDSA
Asked Answered
S

1

1

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();
Squier answered 23/8, 2021 at 21:8 Comment(0)
B
2

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.

Beatriz answered 23/8, 2021 at 22:16 Comment(3)
Very strange. All the comments that were here have vanished?Squier
@WarrickFitzGerald - I've initiated the deletion of the comments as the content was way beyond the scope of the original question. If the issue is not resolved, please post a new question with all the necessary information, the C/C++ code and/or complete sample data so that a comparison can be made. You can reference this post if needed.Beatriz
Indeed your answer was excellent. There were a few factors that I had not considered in my initial question. I'll try and resolve those and open a new question if needed. Thank you again for your time \ answer.Squier

© 2022 - 2024 — McMap. All rights reserved.