Commit 5af5c0ad authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Fix Ed25519CryptoServiceTest.packAndOpenBox

parent d981b6be
Pipeline #10269 passed with stage
in 33 seconds
......@@ -68,9 +68,11 @@ public interface CryptoService extends Bean {
String sign(String message, String secretKey);
String box(String message, byte[] nonce, String senderSignSk, String receiverSignPk);
String box(String message, String nonce, String recipientSignPk, String senderSignSk);
String box(String message, byte[] nonce, byte[] senderSignSk, byte[] receiverSignPk);
String box(String message, byte[] nonce, String recipientSignPk, String senderSignSk);
String box(String message, byte[] nonce, byte[] recipientSignPk, byte[] senderSignSk);
byte[] getBoxRandomNonce();
......
......@@ -154,27 +154,36 @@ public class Ed25519CryptoServiceImpl implements CryptoService {
}
@Override
public String box(String message, byte[] nonce, String senderSignSk, String receiverSignPk) {
byte[] senderSignSkBinary = CryptoUtils.decodeBase58(senderSignSk);
byte[] receiverSignPkBinary = CryptoUtils.decodeBase58(receiverSignPk);
return box(message, nonce, senderSignSkBinary, receiverSignPkBinary);
public String box(String message, String nonce, String recipientSignPk, String senderSignSk) {
return box(message,
CryptoUtils.decodeBase58(nonce),
CryptoUtils.decodeBase58(recipientSignPk),
CryptoUtils.decodeBase58(senderSignSk));
}
@Override
public String box(String message, byte[] nonce, String recipientSignPk, String senderSignSk) {
return box(message, nonce,
CryptoUtils.decodeBase58(recipientSignPk),
CryptoUtils.decodeBase58(senderSignSk));
}
@Override
public String box(String message, byte[] nonce, byte[] senderSignSk, byte[] receiverSignPk) {
public String box(String message, byte[] nonce, byte[] recipientSignPk, byte[] senderSignSk) {
checkLength(nonce, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_NONCEBYTES);
byte[] messageBinary = prependZeros(CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES, CryptoUtils.decodeBase64(message));
byte[] messageBinary = prependZeros(CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES, CryptoUtils.decodeUTF8(message));
byte[] senderBoxSk = new byte[Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SECRETKEYBYTES];
naCl.crypto_sign_ed25519_sk_to_curve25519(senderBoxSk, senderSignSk);
byte[] receiverBoxPk = new byte[Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES];
naCl.crypto_sign_ed25519_pk_to_curve25519(receiverBoxPk, receiverSignPk);
byte[] recipientBoxPk = new byte[Sodium.CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES];
naCl.crypto_sign_ed25519_pk_to_curve25519(recipientBoxPk, recipientSignPk);
byte[] cypherTextBinary = new byte[messageBinary.length];
isValid(sodium().crypto_box_curve25519xsalsa20poly1305(cypherTextBinary, messageBinary,
cypherTextBinary.length, nonce, senderBoxSk, receiverBoxPk), "Encryption failed");
cypherTextBinary.length, nonce, recipientBoxPk, senderBoxSk), "Encryption failed");
return CryptoUtils.encodeBase64(removeZeros(CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES, cypherTextBinary));
}
......
......@@ -25,11 +25,9 @@ package org.duniter.core.service;
import org.duniter.core.test.TestFixtures;
import org.duniter.core.util.crypto.Base58;
import org.duniter.core.util.crypto.CryptoUtils;
import org.duniter.core.util.crypto.SecretBox;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
......@@ -80,28 +78,27 @@ public class Ed25519CryptoServiceTest {
}
@Test
@Ignore
// FIXME: It's seems that pack does not work...
public void packThenOpenBox() throws Exception {
public void packThenOpenBox() {
//
String originalMessage = "test@test";
String nonce = "AHHfny8igAJp1h7P5d8bEobKZfgoRcXs9";
// Sender
SecretBox receiver = createSecretBox();
SecretBox sender = createSecretBox();
SecretBox recipient = createSecretBox();
// Create box
String cypherText = service.box(originalMessage,
CryptoUtils.decodeBase58(nonce),
receiver.getSecretKey(), sender.getPublicKey());
nonce,
recipient.getPublicKey(),
sender.getSecretKey());
// Open box
String decryptedText = service.openBox(
cypherText,
nonce,
sender.getPublicKey(), receiver.getSecretKey());
sender.getPublicKey(), recipient.getSecretKey());
Assert.assertEquals(originalMessage, decryptedText);
......@@ -126,10 +123,14 @@ public class Ed25519CryptoServiceTest {
/* -- internal methods */
protected SecretBox createSecretBox() {
String salt = fixtures.getUserSalt();
String password = fixtures.getUserPassword();
SecretBox secretBox = new SecretBox(salt, password);
return secretBox;
return createSecretBox("");
}
protected SecretBox createSecretBox(String saltPrefix) {
String salt = fixtures.getUserSalt() + saltPrefix;
String password = fixtures.getUserPassword();
SecretBox secretBox = new SecretBox(salt, password);
return secretBox;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment