/// <summary> /// Verify that signature matches the data. /// </summary> /// <param name="signature">Generated signature.</param> /// <param name="data">Data to valuate.</param> /// <returns></returns> public bool Verify(byte[] signature, byte[] data) { GXBigInteger msg; if (PublicKey == null) { if (PrivateKey == null) { throw new ArgumentNullException("Invalid private key."); } PublicKey = PrivateKey.GetPublicKey(); } if (PublicKey.Scheme == Ecc.P256) { using (SHA256 sha = new SHA256CryptoServiceProvider()) { msg = new GXBigInteger(sha.ComputeHash(data)); } } else { using (SHA384 sha = new SHA384CryptoServiceProvider()) { msg = new GXBigInteger(sha.ComputeHash(data)); } } GXByteBuffer pk = new GXByteBuffer(PublicKey.RawValue); GXByteBuffer bb = new GXByteBuffer(signature); int size = SchemeSize(PublicKey.Scheme); GXBigInteger sigR = new GXBigInteger(bb.SubArray(0, size)); GXBigInteger sigS = new GXBigInteger(bb.SubArray(size, size)); GXBigInteger inv = sigS; inv.Inv(curve.N); // Calculate u1 and u2. GXEccPoint u1 = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1)); GXEccPoint u2 = new GXEccPoint(new GXBigInteger(pk.SubArray(1, size)), new GXBigInteger(pk.SubArray(1 + size, size)), new GXBigInteger(1)); GXBigInteger n = msg; n.Multiply(inv); n.Mod(curve.N); Multiply(u1, n, curve.N, curve.A, curve.P); n = new GXBigInteger(sigR); n.Multiply(inv); n.Mod(curve.N); Multiply(u2, n, curve.N, curve.A, curve.P); u1.z = new GXBigInteger(1); u2.z = new GXBigInteger(1); JacobianAdd(u1, u2, curve.A, curve.P); FromJacobian(u1, curve.P); return(sigR.Compare(u1.x) == 0); }
/// <summary> /// Verify that signature matches the data. /// </summary> /// <param name="signature">Generated signature.</param> /// <param name="data">Data to valuate.</param> /// <returns></returns> public bool Verify(byte[] signature, byte[] data) { GXBigInteger msg; using (SHA256 sha = new SHA256CryptoServiceProvider()) { msg = new GXBigInteger(sha.ComputeHash(data)); } if (PublicKey == null) { PublicKey = PrivateKey.GetPublicKey(); } GXByteBuffer pk = new GXByteBuffer(PublicKey.RawValue); GXByteBuffer bb = new GXByteBuffer(signature); GXBigInteger sigR = new GXBigInteger(bb.SubArray(0, 32)); GXBigInteger sigS = new GXBigInteger(bb.SubArray(32, 32)); GXBigInteger inv = sigS; inv.Inv(curve.N); // Calculate u1 and u2. GXEccPoint u1 = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1)); GXEccPoint u2 = new GXEccPoint(new GXBigInteger(pk.SubArray(1, 32)), new GXBigInteger(pk.SubArray(33, 32)), new GXBigInteger(1)); GXBigInteger n = msg; n.Multiply(inv); n.Mod(curve.N); Multiply(u1, n, curve.N, curve.A, curve.P); n = new GXBigInteger(sigR); n.Multiply(inv); n.Mod(curve.N); Multiply(u2, n, curve.N, curve.A, curve.P); // add = Math.add(u1, u2, P = curve.P, A = curve.A) u1.z = new GXBigInteger(1); u2.z = new GXBigInteger(1); JacobianAdd(u1, u2, curve.A, curve.P); FromJacobian(u1, curve.P); return(sigR.Compare(u1.x) == 0); }
/// <summary> /// Sign given data using public and private key. /// </summary> /// <param name="data">Data to sign.</param> /// <returns>Signature</returns> public byte[] Sign(byte[] data) { if (PrivateKey == null) { throw new ArgumentException("Invalid private key."); } GXBigInteger msg; using (SHA256 sha = new SHA256CryptoServiceProvider()) { msg = new GXBigInteger(sha.ComputeHash(data)); } GXBigInteger pk = new GXBigInteger(PrivateKey.RawValue); GXEccPoint p; GXBigInteger n = new GXBigInteger(10); GXBigInteger r; GXBigInteger s; do { if (CustomRandomNumber != null) { n = CustomRandomNumber; } else { n = GetRandomNumber(PrivateKey.Scheme); } p = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1)); Multiply(p, n, curve.N, curve.A, curve.P); r = p.x; r.Mod(curve.N); n.Inv(curve.N); //s s = new GXBigInteger(r); s.Multiply(pk); s.Add(msg); s.Multiply(n); s.Mod(curve.N); } while (r.IsZero || s.IsZero); byte recoveryId; if (p.y.IsOne) { recoveryId = 1; } else { recoveryId = 0; } if (p.y.Compare(curve.N) == 1) { recoveryId += 2; } GXByteBuffer signature = new GXByteBuffer(); signature.Set(r.ToArray()); signature.Set(s.ToArray()); return(signature.Array()); }