Commit f9a0fb2a authored by GreenAddress's avatar GreenAddress Committed by Andreas Schildbach

Native ECDSA implementation updated to latest libsecp256k1

parent 840df06b
/*
* Copyright 2014-2016 the libsecp256k1 contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NativeSecp256k1Util {
private static final Logger log = LoggerFactory.getLogger(NativeSecp256k1Util.class);
public static void assertEquals(int val, int val2, String message) throws AssertFailException {
if (val != val2)
throw new AssertFailException("FAIL: " + message);
}
public static void assertEquals(boolean val, boolean val2, String message) throws AssertFailException {
if (val != val2)
throw new AssertFailException("FAIL: " + message);
else
log.debug("PASS: " + message);
}
public static void assertEquals(String val, String val2, String message) throws AssertFailException {
if (!val.equals(val2))
throw new AssertFailException("FAIL: " + message);
else
log.debug("PASS: " + message);
}
public static class AssertFailException extends Exception {
public AssertFailException(String message) {
super(message);
}
}
}
/*
* Copyright 2014-2016 the libsecp256k1 contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class holds the context reference used in native methods to handle ECDSA operations.
*/
public class Secp256k1Context {
private static final boolean enabled; // true if the library is loaded
private static final long context; // ref to pointer to context obj
private static final Logger log = LoggerFactory.getLogger(Secp256k1Context.class);
static { // static initializer
boolean isEnabled = true;
long contextRef = -1;
try {
System.loadLibrary("secp256k1");
contextRef = secp256k1_init_context();
} catch (UnsatisfiedLinkError e) {
log.info("UnsatisfiedLinkError: " + e.toString());
isEnabled = false;
}
enabled = isEnabled;
context = contextRef;
}
public static boolean isEnabled() {
return enabled;
}
public static long getContext() {
if (!enabled)
return -1; // sanity check
return context;
}
private static native long secp256k1_init_context();
}
/*
* Copyright 2011 Google Inc.
* Copyright 2014 Andreas Schildbach
* Copyright 2014-2016 the libsecp256k1 contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -25,6 +26,8 @@ import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;
import com.google.common.primitives.UnsignedBytes;
import org.bitcoin.NativeSecp256k1;
import org.bitcoin.NativeSecp256k1Util;
import org.bitcoin.Secp256k1Context;
import org.bitcoinj.wallet.Protos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -663,6 +666,18 @@ public class ECKey implements EncryptableItem {
}
protected ECDSASignature doSign(Sha256Hash input, BigInteger privateKeyForSigning) {
if (Secp256k1Context.isEnabled()) {
try {
byte[] signature = NativeSecp256k1.sign(
input.getBytes(),
Utils.bigIntegerToBytes(privateKeyForSigning, 32)
);
return ECDSASignature.decodeFromDER(signature);
} catch (NativeSecp256k1Util.AssertFailException e) {
log.error("Caught AssertFailException inside secp256k1", e);
throw new RuntimeException(e);
}
}
if (FAKE_SIGNATURES)
return TransactionSignature.dummy();
checkNotNull(privateKeyForSigning);
......@@ -687,8 +702,14 @@ public class ECKey implements EncryptableItem {
if (FAKE_SIGNATURES)
return true;
if (NativeSecp256k1.enabled)
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub);
if (Secp256k1Context.isEnabled()) {
try {
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub);
} catch (NativeSecp256k1Util.AssertFailException e) {
log.error("Caught AssertFailException inside secp256k1", e);
return false;
}
}
ECDSASigner signer = new ECDSASigner();
ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
......@@ -711,8 +732,14 @@ public class ECKey implements EncryptableItem {
* @param pub The public key bytes to use.
*/
public static boolean verify(byte[] data, byte[] signature, byte[] pub) {
if (NativeSecp256k1.enabled)
return NativeSecp256k1.verify(data, signature, pub);
if (Secp256k1Context.isEnabled()) {
try {
return NativeSecp256k1.verify(data, signature, pub);
} catch (NativeSecp256k1Util.AssertFailException e) {
log.error("Caught AssertFailException inside secp256k1", e);
return false;
}
}
return verify(data, ECDSASignature.decodeFromDER(signature), pub);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment