Skip to content

Commit

Permalink
Adding deriveRandomKey for Secp256k1PrivateKey and fixing the verifie…
Browse files Browse the repository at this point in the history
…r signature for Secp256k1PrivateKey.
  • Loading branch information
AB3rtz committed Sep 13, 2022
1 parent e7efafc commit 7c02456
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crypto-key-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>crypto-keys</artifactId>
<groupId>com.syntifi.crypto</groupId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
6 changes: 3 additions & 3 deletions crypto-key-ed25519/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>crypto-keys</artifactId>
<groupId>com.syntifi.crypto</groupId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -18,12 +18,12 @@
<dependency>
<groupId>com.syntifi.crypto</groupId>
<artifactId>crypto-key-common</artifactId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>com.syntifi.crypto</groupId>
<artifactId>crypto-key-common</artifactId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.syntifi.crypto.key;

import com.syntifi.crypto.key.deterministic.HierarchicalDeterministicKey;
import com.syntifi.crypto.key.encdec.Hex;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.bouncycastle.asn1.*;
Expand All @@ -13,13 +12,11 @@
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.crypto.signers.Ed25519Signer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.List;

/**
* ed25519 implementation of {@link AbstractPrivateKey}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ void create_privateKey_from_seed() throws IOException {
assertEquals(Hex.encode(pk2.derivePublicKey().getKey()), Hex.encode(pk1.derivePublicKey().getKey()));
}

@Test
void create_random_key() {
Ed25519PrivateKey sk = Ed25519PrivateKey.deriveRandomKey();
Ed25519PublicKey pk = (Ed25519PublicKey) sk.derivePublicKey();
byte[] msg = "this is a test".getBytes();
byte[] signature = sk.sign(msg);
assertTrue(pk.verify(msg, signature));
}

private Ed25519PrivateKey readPrivateKey(String privateKeyPath) throws URISyntaxException, IOException {
Ed25519PrivateKey privateKey = new Ed25519PrivateKey();
String keyFilePath = getResourcesKeyPath(privateKeyPath);
Expand Down
6 changes: 3 additions & 3 deletions crypto-key-secp256k1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>crypto-keys</artifactId>
<groupId>com.syntifi.crypto</groupId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.syntifi.crypto</groupId>
<artifactId>crypto-key-common</artifactId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
Expand All @@ -36,7 +36,7 @@
<dependency>
<groupId>com.syntifi.crypto</groupId>
<artifactId>crypto-key-common</artifactId>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.bouncycastle.asn1.*;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.pqc.crypto.gmss.GMSSKeyPairGenerator;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Sign;
Expand All @@ -24,7 +20,6 @@
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.Arrays;

Expand All @@ -39,6 +34,7 @@
@EqualsAndHashCode(callSuper = true)
public class Secp256k1PrivateKey extends AbstractPrivateKey {
@Getter
@Setter
private ECKeyPair keyPair;

public Secp256k1PrivateKey(byte[] privateKey) throws IOException {
Expand Down Expand Up @@ -112,9 +108,16 @@ public byte[] sign(byte[] message) {
@Override
public AbstractPublicKey derivePublicKey() {
BigInteger pubKey = keyPair.getPublicKey();
String pubKeyPrefix = pubKey.testBit(0) ? "03" : "02";
byte[] pubKeyBytes = Arrays.copyOf(pubKey.toByteArray(), 32);
return new Secp256k1PublicKey(Hex.decode(pubKeyPrefix + Hex.encode(pubKeyBytes)));
byte[] pubKeyBytes = Secp256k1PublicKey.getShortKey(pubKey.toByteArray());
return new Secp256k1PublicKey(pubKeyBytes);
}

public static Secp256k1PrivateKey deriveRandomKey() throws IOException {
SecureRandom rnd = new SecureRandom();
ECKeyPair keyPair = ECKeyPair.create(rnd.generateSeed(32));
Secp256k1PrivateKey sk = new Secp256k1PrivateKey();
sk.setKeyPair(keyPair);
return sk;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@ public void writePublicKey(String filename) throws IOException {

@Override
public Boolean verify(byte[] message, byte[] signature) throws GeneralSecurityException {
SignatureData signatureData = new SignatureData(
//TODO: Double check the issue the getV(), for now we are trying with both (27 and 28)
SignatureData signatureData1 = new SignatureData(
(byte) 27,
Arrays.copyOfRange(signature, 0, 32),
Arrays.copyOfRange(signature, 32, 64));
BigInteger derivedKey = Sign.signedMessageHashToKey(Hash.sha256(message), signatureData);
return Arrays.equals(Secp256k1PublicKey.getShortKey(derivedKey.toByteArray()), getKey());
BigInteger derivedKey1 = Sign.signedMessageHashToKey(Hash.sha256(message), signatureData1);
SignatureData signatureData2 = new SignatureData(
(byte) 28,
Arrays.copyOfRange(signature, 0, 32),
Arrays.copyOfRange(signature, 32, 64));
BigInteger derivedKey2 = Sign.signedMessageHashToKey(Hash.sha256(message), signatureData2);
return Arrays.equals(Secp256k1PublicKey.getShortKey(derivedKey1.toByteArray()), getKey()) ||
Arrays.equals(Secp256k1PublicKey.getShortKey(derivedKey2.toByteArray()), getKey());
}

/**
Expand All @@ -85,7 +92,7 @@ public Boolean verify(byte[] message, byte[] signature) throws GeneralSecurityEx
public static byte[] getShortKey(byte[] key) {
BigInteger pubKey = new BigInteger(key);
String pubKeyPrefix = pubKey.testBit(0) ? "03" : "02";
byte[] pubKeyBytes = Arrays.copyOf(key, 32);
byte[] pubKeyBytes = Arrays.copyOfRange(key, 0, 32);
return Hex.decode(pubKeyPrefix + Hex.encode(pubKeyBytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -79,4 +80,17 @@ void sign_should_sign_message() throws URISyntaxException, IOException {
"ea5b38fd0db5fb3d871c47fde1fa4c4db75d1a9e1c0ac54d826e178ee0e63707176b4e63b4f838bd031f007fffd6a4f71d920a10c48ea53dd1573fa2b58a829e",
Hex.encode(signature));
}

@Test
void create_random_key() throws GeneralSecurityException, IOException {
Secp256k1PrivateKey sk = Secp256k1PrivateKey.deriveRandomKey();
Secp256k1PublicKey pk = (Secp256k1PublicKey) sk.derivePublicKey();
LOGGER.info(sk.getKeyPair().getPrivateKey().toString(16));
LOGGER.info(sk.getKeyPair().getPublicKey().toString(16));
LOGGER.info(Hex.encode(pk.getKey()));
byte[] msg = "this is a test".getBytes();
byte[] signature = sk.sign(msg);
LOGGER.info(Hex.encode(signature));
assertTrue(pk.verify(msg, signature));
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.syntifi.crypto</groupId>
<artifactId>crypto-keys</artifactId>
<packaging>pom</packaging>
<version>0.4.0-SNAPSHOT</version>
<version>0.4.0</version>
<modules>
<module>crypto-key-ed25519</module>
<module>crypto-key-secp256k1</module>
Expand Down

0 comments on commit 7c02456

Please sign in to comment.