From abcf3f47946be1a2297ef2fb3dd8416299529079 Mon Sep 17 00:00:00 2001 From: lp1ee9d Date: Mon, 26 Nov 2018 15:14:16 +0100 Subject: [PATCH 1/2] add new crypto methods --- src/main/java/org/abstractj/kalium/NaCl.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/org/abstractj/kalium/NaCl.java b/src/main/java/org/abstractj/kalium/NaCl.java index a1dfbf9..e43a7a0 100644 --- a/src/main/java/org/abstractj/kalium/NaCl.java +++ b/src/main/java/org/abstractj/kalium/NaCl.java @@ -499,6 +499,11 @@ int crypto_stream_xor( // --------------------------------------------------------------------- // Advanced: Ed25519 to Curve25519 + int crypto_sign_ed25519_pk_to_curve25519( + @Out byte[] curve25519_pk, @In byte[] ed25519_pk); + + int crypto_sign_ed25519_sk_to_curve25519( + @Out byte[] curve25519_sk, @In byte[] ed25519_sk); } /** From dbdd78a4fc127d3fcbe19b97b10ad70ad99a89dd Mon Sep 17 00:00:00 2001 From: blavenie Date: Wed, 5 Dec 2018 11:35:53 +0100 Subject: [PATCH 2/2] Add Ed25519 to Curve25519 conversion - fix #19 - Rename functions to convertToCurve() - Add unit test --- .../org/abstractj/kalium/keys/SigningKey.java | 7 +++++++ .../java/org/abstractj/kalium/keys/VerifyKey.java | 12 ++++++++---- .../org/abstractj/kalium/fixture/TestVectors.java | 3 ++- .../org/abstractj/kalium/keys/SigningKeyTest.java | 6 ++++++ .../org/abstractj/kalium/keys/VerifyKeyTest.java | 15 ++++++++------- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/abstractj/kalium/keys/SigningKey.java b/src/main/java/org/abstractj/kalium/keys/SigningKey.java index fc68934..657314c 100644 --- a/src/main/java/org/abstractj/kalium/keys/SigningKey.java +++ b/src/main/java/org/abstractj/kalium/keys/SigningKey.java @@ -23,6 +23,7 @@ import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES; import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SECRETKEYBYTES; +import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SCALARMULT_CURVE25519_BYTES; import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SIGN_ED25519_BYTES; import static org.abstractj.kalium.NaCl.sodium; import static org.abstractj.kalium.crypto.Util.checkLength; @@ -60,6 +61,12 @@ public VerifyKey getVerifyKey() { return this.verifyKey; } + public PrivateKey convertToCurve() { + byte[] secretKey = zeros(CRYPTO_SCALARMULT_CURVE25519_BYTES); + sodium().crypto_sign_ed25519_sk_to_curve25519(secretKey, seed); + return new PrivateKey(secretKey); + } + public byte[] sign(byte[] message) { byte[] signature = new byte[CRYPTO_SIGN_ED25519_BYTES]; LongLongByReference bufferLen = new LongLongByReference(0); diff --git a/src/main/java/org/abstractj/kalium/keys/VerifyKey.java b/src/main/java/org/abstractj/kalium/keys/VerifyKey.java index 8727996..87c3cce 100644 --- a/src/main/java/org/abstractj/kalium/keys/VerifyKey.java +++ b/src/main/java/org/abstractj/kalium/keys/VerifyKey.java @@ -18,11 +18,9 @@ import org.abstractj.kalium.encoders.Encoder; -import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES; -import static org.abstractj.kalium.NaCl.Sodium.CRYPTO_SIGN_ED25519_BYTES; +import static org.abstractj.kalium.NaCl.Sodium.*; import static org.abstractj.kalium.NaCl.sodium; -import static org.abstractj.kalium.crypto.Util.checkLength; -import static org.abstractj.kalium.crypto.Util.isValid; +import static org.abstractj.kalium.crypto.Util.*; import static org.abstractj.kalium.encoders.Encoder.HEX; public class VerifyKey { @@ -38,6 +36,12 @@ public VerifyKey(String key, Encoder encoder) { this(encoder.decode(key)); } + public PublicKey convertToCurve() { + byte[] publicKey = zeros(CRYPTO_SCALARMULT_CURVE25519_BYTES); + sodium().crypto_sign_ed25519_pk_to_curve25519(publicKey, key); + return new PublicKey(publicKey); + } + public boolean verify(byte[] message, byte[] signature) { checkLength(signature, CRYPTO_SIGN_ED25519_BYTES); return isValid(sodium().crypto_sign_ed25519_verify_detached(signature, message, message.length, key), "signature was forged or corrupted"); diff --git a/src/test/java/org/abstractj/kalium/fixture/TestVectors.java b/src/test/java/org/abstractj/kalium/fixture/TestVectors.java index 1a9dcb1..a7acf48 100644 --- a/src/test/java/org/abstractj/kalium/fixture/TestVectors.java +++ b/src/test/java/org/abstractj/kalium/fixture/TestVectors.java @@ -105,6 +105,7 @@ public class TestVectors { public static final String SECRET_KEY = "1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389"; public static final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd"; + public static final String SIGN_PRIVATE_CURVE25519 = "38e5cdf33bc9e13086f58a3fea86d574e85e7865cffa5e8c9335f200a41d036c"; public static final String SIGN_MESSAGE = "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171" + "ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01" + "dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313" + @@ -113,7 +114,7 @@ public class TestVectors { public static final String SIGN_SIGNATURE = "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b" + "4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509"; public static final String SIGN_PUBLIC = "77f48b59caeda77751ed138b0ec667ff50f8768c25d48309a8f386a2bad187fb"; - + public static final String SIGN_PUBLIC_CURVE25519 = "35488a98f7ec26ae27099809afb27587b198b1197b5bcb0dec41153db2bf9952"; /** diff --git a/src/test/java/org/abstractj/kalium/keys/SigningKeyTest.java b/src/test/java/org/abstractj/kalium/keys/SigningKeyTest.java index 4bf5043..4298461 100644 --- a/src/test/java/org/abstractj/kalium/keys/SigningKeyTest.java +++ b/src/test/java/org/abstractj/kalium/keys/SigningKeyTest.java @@ -137,6 +137,12 @@ public void testAccessVerifyKey() { assertEquals(v.toString(), SIGN_PUBLIC); } + @Test + public void testConvertToCurve() { + SigningKey key = new SigningKey(SIGN_PRIVATE, HEX); + assertEquals(key.convertToCurve().toString(), SIGN_PRIVATE_CURVE25519); + } + @Test public void testRoundTrip() { SigningKey key = new SigningKey(SIGN_PRIVATE, HEX); diff --git a/src/test/java/org/abstractj/kalium/keys/VerifyKeyTest.java b/src/test/java/org/abstractj/kalium/keys/VerifyKeyTest.java index f08fe3c..3cd1675 100644 --- a/src/test/java/org/abstractj/kalium/keys/VerifyKeyTest.java +++ b/src/test/java/org/abstractj/kalium/keys/VerifyKeyTest.java @@ -20,14 +20,9 @@ import java.util.Arrays; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; import static org.abstractj.kalium.encoders.Encoder.HEX; -import static org.abstractj.kalium.fixture.TestVectors.SIGN_MESSAGE; -import static org.abstractj.kalium.fixture.TestVectors.SIGN_PRIVATE; -import static org.abstractj.kalium.fixture.TestVectors.SIGN_PUBLIC; -import static org.abstractj.kalium.fixture.TestVectors.SIGN_SIGNATURE; -import static org.junit.Assert.fail; +import static org.abstractj.kalium.fixture.TestVectors.*; +import static org.junit.Assert.*; public class VerifyKeyTest { @@ -82,4 +77,10 @@ public void testInitializeFromHex() throws Exception { assertTrue(verifyKey.verify(SIGN_MESSAGE, SIGN_SIGNATURE, HEX)); } + @Test + public void testConvertToCurve() { + VerifyKey verifyKey = new VerifyKey(SIGN_PUBLIC, HEX); + assertEquals(verifyKey.convertToCurve().toString(), SIGN_PUBLIC_CURVE25519); + } + }