public virtual BigInteger[] GenerateSignature(byte[] message) { DsaParameters parameters = key.Parameters; BigInteger q = parameters.Q; BigInteger bigInteger = CalculateE(q, message); BigInteger x = ((DsaPrivateKeyParameters)key).X; if (kCalculator.IsDeterministic) { kCalculator.Init(q, x, message); } else { kCalculator.Init(q, random); } BigInteger bigInteger2 = kCalculator.NextK(); BigInteger bigInteger3 = parameters.G.ModPow(bigInteger2, parameters.P).Mod(q); bigInteger2 = bigInteger2.ModInverse(q).Multiply(bigInteger.Add(x.Multiply(bigInteger3))); BigInteger bigInteger4 = bigInteger2.Mod(q); return(new BigInteger[2] { bigInteger3, bigInteger4 }); }
/** * Generate a signature for the given message using the key we were * initialised with. For conventional DSA the message should be a SHA-1 * hash of the message of interest. * * @param message the message that will be verified later. */ public virtual BigInteger[] GenerateSignature(byte[] message) { DsaParameters parameters = key.Parameters; BigInteger q = parameters.Q; BigInteger m = CalculateE(q, message); BigInteger x = ((DsaPrivateKeyParameters)key).X; if (kCalculator.IsDeterministic) { kCalculator.Init(q, x, message); } else { kCalculator.Init(q, random); } BigInteger k = kCalculator.NextK(); BigInteger r = parameters.G.ModPow(k, parameters.P).Mod(q); k = k.ModInverse(q).Multiply(m.Add(x.Multiply(r))); BigInteger s = k.Mod(q); return(new BigInteger[] { r, s }); }
/// <inheritdoc /> public void GenerateSignature(byte[] message, out BigInteger r, out BigInteger s) { BigInteger n = _ecDomain.N; BigInteger e = CalculateE(n, message); var d = new BigInteger(_privateKey.EncodedKey); if (_kCalculator.IsDeterministic) { _kCalculator.Init(n, d, message); } else { _kCalculator.Init(n, _random); } // 5.3.2 // Generate s do { BigInteger k; // Generate r do { k = _kCalculator.NextK(); ECPoint p = EcBasePointMultiplier.Multiply(_ecDomain.G, k).Normalize(); // 5.3.3 r = p.AffineXCoord.ToBigInteger().Mod(n); } while (r.SignValue == 0); s = k.ModInverse(n).Multiply(e.Add(d.Multiply(r))).Mod(n); } while (s.SignValue == 0); }
public virtual void Init(bool forSigning, ICipherParameters parameters) { ICipherParameters baseParam; byte[] userID; if (parameters is ParametersWithID) { baseParam = ((ParametersWithID)parameters).Parameters; userID = ((ParametersWithID)parameters).GetID(); if (userID.Length >= 8192) { throw new ArgumentException("SM2 user ID must be less than 2^16 bits long"); } } else { baseParam = parameters; // the default value, string value is "1234567812345678" userID = Hex.Decode("31323334353637383132333435363738"); } if (forSigning) { if (baseParam is ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)baseParam; ecKey = (ECKeyParameters)rParam.Parameters; ecParams = ecKey.Parameters; kCalculator.Init(ecParams.N, rParam.Random); } else { ecKey = (ECKeyParameters)baseParam; ecParams = ecKey.Parameters; kCalculator.Init(ecParams.N, new SecureRandom()); } pubPoint = CreateBasePointMultiplier().Multiply(ecParams.G, ((ECPrivateKeyParameters)ecKey).D).Normalize(); } else { ecKey = (ECKeyParameters)baseParam; ecParams = ecKey.Parameters; pubPoint = ((ECPublicKeyParameters)ecKey).Q; } digest.Reset(); z = GetZ(userID); digest.BlockUpdate(z, 0, z.Length); }
// 5.3 pg 28 /** * Generate a signature for the given message using the key we were * initialised with. For conventional DSA the message should be a SHA-1 * hash of the message of interest. * * @param message the message that will be verified later. */ public virtual BigInteger[] GenerateSignature(byte[] message) { ECDomainParameters ec = key.Parameters; BigInteger n = ec.N; BigInteger e = CalculateE(n, message); BigInteger d = ((ECPrivateKeyParameters)key).D; if (kCalculator.IsDeterministic) { kCalculator.Init(n, d, message); } else { kCalculator.Init(n, random); } BigInteger r, s; ECMultiplier basePointMultiplier = CreateBasePointMultiplier(); // 5.3.2 do // Generate s { BigInteger k; byte[] rBytes = null; do // Generate r { do { k = kCalculator.NextK(); ECPoint p = basePointMultiplier.Multiply(ec.G, k).Normalize(); // 5.3.3 r = p.AffineXCoord.ToBigInteger().Mod(n); }while(r.SignValue == 0); }while(forceLowR && r.ToByteArrayUnsigned()[0] >= 0x80); s = k.ModInverse(n).Multiply(e.Add(d.Multiply(r))).Mod(n); }while(s.SignValue == 0); return(new BigInteger[] { r, s }); }
public virtual BigInteger[] GenerateSignature(byte[] message) { ECDomainParameters parameters = key.Parameters; BigInteger n = parameters.N; BigInteger bigInteger = CalculateE(n, message); BigInteger d = ((ECPrivateKeyParameters)key).D; if (kCalculator.IsDeterministic) { kCalculator.Init(n, d, message); } else { kCalculator.Init(n, random); } ECMultiplier eCMultiplier = CreateBasePointMultiplier(); BigInteger bigInteger3; BigInteger bigInteger4; while (true) { BigInteger bigInteger2 = kCalculator.NextK(); ECPoint eCPoint = eCMultiplier.Multiply(parameters.G, bigInteger2).Normalize(); bigInteger3 = eCPoint.AffineXCoord.ToBigInteger().Mod(n); if (bigInteger3.SignValue != 0) { bigInteger4 = bigInteger2.ModInverse(n).Multiply(bigInteger.Add(d.Multiply(bigInteger3))).Mod(n); if (bigInteger4.SignValue != 0) { break; } } } return(new BigInteger[2] { bigInteger3, bigInteger4 }); }
public Requester() { _k = new RandomDsaKCalculator(); _k.Init(BigInteger.Arbitrary(256), new SecureRandom()); }