<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Fail in verifying micro ECC signature in Android</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/23505/fail-in-verifying-micro-ecc-signature-in-android</link><description>In order to prevent communication from middle-man attack, I append the signature after ECDH publickey. However, though I get the correct share secret by ECDH, I fail in verifying the micro ecc signature in Android studio, and 52832 also fails in verify</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 14 Jul 2017 12:33:17 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/23505/fail-in-verifying-micro-ecc-signature-in-android" /><item><title>RE: Fail in verifying micro ECC signature in Android</title><link>https://devzone.nordicsemi.com/thread/92338?ContentTypeID=1</link><pubDate>Fri, 14 Jul 2017 12:33:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3f1b5dd0-4c4e-4229-ab01-8fe32dabf58c</guid><dc:creator>Aleksander Nowakowski</dc:creator><description>&lt;p&gt;Hi, Could you check this code. It&amp;#39;s using ECDSASigner class from Spongy Castle.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;private static final X9ECParameters curve = SECNamedCurves.getByName(&amp;quot;secp256k1&amp;quot;);
private static final ECDomainParameters domain = new ECDomainParameters(curve.getCurve(), curve.getG(), curve.getN(), curve.getH());

/**
 * Verifies the message using given signature and public key (given as X and Y points).
 * @param message the message to be verified
 * @param signature the messsage signature
 * @param x X coordinate of public key
 * @param y Y coordinate of public key
 * @return true if signature is valid, false otherwise
 */
static boolean verify(@NonNull final byte[] message, @NonNull final byte[] signature, @NonNull final BigInteger x, @NonNull final BigInteger y) {
	if (signature.length != 64)
		return false;

	try {
		// Initialize signer
		final ECDSASigner signer = new ECDSASigner();
		signer.init(false, new ECPublicKeyParameters(curve.getCurve().createPoint(x, y), domain));

		// Calculate SHA-256 of the message
		final byte[] hash = sha256(message);

		final int nLength = (curve.getN().bitLength() + 7) / 8;
		final byte[] r = Arrays.copyOfRange(signature, 0, nLength);
		final byte[] s = Arrays.copyOfRange(signature, nLength, 2 * nLength);
		final BigInteger R = new BigInteger(r);
		final BigInteger S = new BigInteger(s);
		return signer.verifySignature(hash, R, S);
	} catch (final Exception e) {
		Log.e(TAG, &amp;quot;Verify failed&amp;quot;, e);
	}
	return false;
}

/**
 * Calculates SHA-256 of given message
 * @param message the message to be hashed
 * @return hash
 */
private static byte[] sha256(@NonNull final byte[] message) throws NoSuchAlgorithmException {
	final MessageDigest digest = MessageDigest.getInstance(&amp;quot;SHA-256&amp;quot;);
	return digest.digest(message);
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>