Skip to content

Commit

Permalink
improved java api layer, updated to latest libsodium (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml authored Mar 2, 2023
1 parent 8cd96f0 commit 930964b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
10 changes: 10 additions & 0 deletions bindings/jvm/src/main/java/org/ssohub/crypto/ecc/BaseData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/*
* Copyright (c) 2023, Alden Torres
*
* Licensed under the terms of the MIT license.
* Copy of the license at https://opensource.org/licenses/MIT
*/

package org.ssohub.crypto.ecc;

/**
* @author aldenml
*/
public abstract class BaseData implements DataLike {

protected final Data data;
Expand Down
4 changes: 2 additions & 2 deletions bindings/jvm/src/main/java/org/ssohub/crypto/ecc/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public final class Data implements DataLike {
* Internally perform a copy of the array.
*/
public Data(byte[] arr, int offset, int length) {
if (length < 0)
throw new IllegalArgumentException("length should be > 0");
if (offset < 0 || length < 0)
throw new IllegalArgumentException("both offset and length should be > 0");
this.arr = Arrays.copyOfRange(arr, offset, offset + length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public final class Opaque {
private Opaque() {
}

/**
* Returns a randomly generated private and public key pair.
* <p>
* This is implemented by generating a random "seed", then
* calling internally DeriveAuthKeyPair.
*
* @return a new key pair.
*/
public static GenerateAuthKeyPairResult generateAuthKeyPair() {

byte[] privateKey = new byte[ecc_opaque_ristretto255_sha512_Nsk];
Expand All @@ -36,6 +44,12 @@ public static GenerateAuthKeyPairResult generateAuthKeyPair() {
);
}

/**
* Recover the public key related to the input `privateKey`.
*
* @param privateKey the private key
* @return the corresponding public key.
*/
public static OpaquePk recoverPublicKey(OpaqueSk privateKey) {

byte[] publicKey = new byte[ecc_opaque_ristretto255_sha512_Npk];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public Data getMaskingKey() {
return data.copy(ecc_opaque_ristretto255_sha512_Npk, ecc_opaque_ristretto255_sha512_Nh);
}

// TODO: add envelope getter
public static RegistrationRecord fromHex(String hex) {
return new RegistrationRecord(Data.fromHex(hex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public RegistrationRequest(Data data) {
public R255Element getBlindedMessage() {
return new R255Element(data);
}

public static RegistrationRequest fromHex(String hex) {
return new RegistrationRequest(Data.fromHex(hex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public R255Element getEvaluatedMessage() {
public OpaquePk getServerPublicKey() {
return new OpaquePk(data.copy(ecc_opaque_ristretto255_sha512_Noe, ecc_opaque_ristretto255_sha512_Npk));
}

public static RegistrationResponse fromHex(String hex) {
return new RegistrationResponse(Data.fromHex(hex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,15 @@ void testProtocolWithBadPassword() {
assertNotEquals(0, serverFinishResult.getResult());
assertNotEquals(clientFinishResult.getSessionKey(), serverFinishResult.getSessionKey());
}

@Test
void testKeyPairGeneration() {

GenerateAuthKeyPairResult keyPairResult = Opaque.generateAuthKeyPair();
System.out.println("Opaque sk=" + keyPairResult.getPrivateKey().toHex());
System.out.println("Opaque pk=" + keyPairResult.getPublicKey().toHex());

OpaquePk pk = Opaque.recoverPublicKey(keyPairResult.getPrivateKey());
assertEquals(keyPairResult.getPublicKey(), pk);
}
}
2 changes: 1 addition & 1 deletion deps/libsodium
Submodule libsodium updated 1 files
+1 −9 build.zig

0 comments on commit 930964b

Please sign in to comment.