Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Ed25519 to Curve25519 keys conversion #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/abstractj/kalium/NaCl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/abstractj/kalium/keys/SigningKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/abstractj/kalium/keys/VerifyKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/abstractj/kalium/fixture/TestVectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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";


/**
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/abstractj/kalium/keys/SigningKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/org/abstractj/kalium/keys/VerifyKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}

}