Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #161 from cleishm/sodium-signature-native
Browse files Browse the repository at this point in the history
Use native pointers in sodium Signature key data and make secret keys destroyable
  • Loading branch information
atoulme committed Feb 9, 2019
2 parents 541784a + 50eaae1 commit 06dbed1
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 143 deletions.
23 changes: 18 additions & 5 deletions crypto/src/main/java/net/consensys/cava/crypto/sodium/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,25 @@ public static KeyPair fromSeed(Seed seed) {
* @return A {@link KeyPair}.
*/
public static KeyPair forSignatureKeyPair(Signature.KeyPair keyPair) {
byte[] curvedSk = new byte[Box.SecretKey.length()];
int rc = Sodium.crypto_sign_ed25519_sk_to_curve25519(curvedSk, keyPair.secretKey().bytesArray());
if (rc != 0) {
throw new SodiumException("crypto_sign_ed25519_sk_to_curve25519: failed with results " + rc);
Pointer signatureSecretKeyPtr = keyPair.secretKey().ptr;
checkArgument(signatureSecretKeyPtr != null, "Signature.SecretKey has been destroyed");
Pointer secretKey = null;
try {
int secretKeyLength = SecretKey.length();
secretKey = Sodium.malloc(secretKeyLength);
int rc = Sodium.crypto_sign_ed25519_sk_to_curve25519(secretKey, keyPair.secretKey().ptr);
if (rc != 0) {
throw new SodiumException("crypto_sign_ed25519_sk_to_curve25519: failed with results " + rc);
}
SecretKey sk = new SecretKey(secretKey, secretKeyLength);
secretKey = null;
return forSecretKey(sk);
} catch (Throwable e) {
if (secretKey != null) {
Sodium.sodium_free(secretKey);
}
throw e;
}
return forSecretKey(SecretKey.fromBytes(curvedSk));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package net.consensys.cava.crypto.sodium;

import javax.annotation.Nullable;

import jnr.ffi.Pointer;
import jnr.ffi.annotations.In;
import jnr.ffi.annotations.Out;
Expand Down Expand Up @@ -1857,13 +1859,13 @@ int crypto_sign_ed25519_detached(
int crypto_sign_ed25519_pk_to_curve25519(@Out byte[] curve25519_pk, @In byte[] ed25519_pk);

// int crypto_sign_ed25519_sk_to_curve25519(unsigned char * curve25519_sk, const unsigned char * ed25519_sk);
int crypto_sign_ed25519_sk_to_curve25519(@Out byte[] curve25519_sk, @In byte[] ed25519_sk);
int crypto_sign_ed25519_sk_to_curve25519(@Out Pointer curve25519_sk, @In Pointer ed25519_sk);

// int crypto_sign_ed25519_sk_to_seed(unsigned char * seed, const unsigned char * sk);
int crypto_sign_ed25519_sk_to_seed(@Out byte[] seed, @In byte[] sk);

// int crypto_sign_ed25519_sk_to_pk(unsigned char * pk, const unsigned char * sk);
int crypto_sign_ed25519_sk_to_pk(@Out byte[] pk, @In byte[] sk);
int crypto_sign_ed25519_sk_to_pk(@Out Pointer pk, @In Pointer sk);

// int crypto_sign_ed25519ph_init(crypto_sign_ed25519ph_state * state);
int crypto_sign_ed25519ph_init(@Out Pointer state);
Expand Down Expand Up @@ -1909,37 +1911,37 @@ int crypto_sign_ed25519ph_final_create(
String crypto_sign_primitive();

// int crypto_sign_seed_keypair(unsigned char * pk, unsigned char * sk, const unsigned char * seed);
int crypto_sign_seed_keypair(@Out byte[] pk, @Out byte[] sk, @In byte[] seed);
int crypto_sign_seed_keypair(@Out Pointer pk, @Out Pointer sk, @In Pointer seed);

// int crypto_sign_keypair(unsigned char * pk, unsigned char * sk);
int crypto_sign_keypair(@Out byte[] pk, @Out byte[] sk);
int crypto_sign_keypair(@Out Pointer pk, @Out Pointer sk);

// int crypto_sign(unsigned char * sm, unsigned long long * smlen_p, const unsigned char * m, unsigned long long mlen, const unsigned char * sk);
int crypto_sign(
@Out byte[] sm,
@Out LongLongByReference smlen_p,
@Nullable @Out LongLongByReference smlen_p,
@In byte[] m,
@In @u_int64_t long mlen,
@In byte[] sk);
@In Pointer sk);

// int crypto_sign_open(unsigned char * m, unsigned long long * mlen_p, const unsigned char * sm, unsigned long long smlen, const unsigned char * pk);
int crypto_sign_open(
@Out byte[] m,
@Out LongLongByReference mlen_p,
@In byte[] sm,
@In @u_int64_t long smlen,
@In byte[] pk);
@In Pointer pk);

// int crypto_sign_detached(unsigned char * sig, unsigned long long * siglen_p, const unsigned char * m, unsigned long long mlen, const unsigned char * sk);
int crypto_sign_detached(
@Out byte[] sig,
@Out LongLongByReference siglen_p,
@Nullable @Out LongLongByReference siglen_p,
@In byte[] m,
@In @u_int64_t long mlen,
@In byte[] sk);
@In Pointer sk);

// int crypto_sign_verify_detached(const unsigned char * sig, const unsigned char * m, unsigned long long mlen, const unsigned char * pk);
int crypto_sign_verify_detached(@In byte[] sig, @In byte[] m, @In @u_int64_t long mlen, @In byte[] pk);
int crypto_sign_verify_detached(@In byte[] sig, @In byte[] m, @In @u_int64_t long mlen, @In Pointer pk);

// int crypto_sign_init(crypto_sign_state * state);
int crypto_sign_init(@Out Pointer state);
Expand Down
Loading

0 comments on commit 06dbed1

Please sign in to comment.