-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace libsodium with bouncy castle in SecretBox
- Loading branch information
1 parent
5b1b713
commit 0f1c292
Showing
8 changed files
with
213 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 69 additions & 38 deletions
107
autobahn/src/main/java/xbr/network/crypto/SecretBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,96 @@ | ||
package xbr.network.crypto; | ||
|
||
import org.libsodium.jni.crypto.Random; | ||
import org.libsodium.jni.crypto.Util; | ||
import org.libsodium.jni.encoders.Encoder; | ||
import static xbr.network.Util.NONCE_SIZE; | ||
import static xbr.network.Util.SECRET_KEY_LEN; | ||
import static xbr.network.Util.generateRandomBytesArray; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.libsodium.jni.NaCl.sodium; | ||
import static org.libsodium.jni.SodiumConstants.BOXZERO_BYTES; | ||
import static org.libsodium.jni.SodiumConstants.XSALSA20_POLY1305_SECRETBOX_KEYBYTES; | ||
import static org.libsodium.jni.SodiumConstants.XSALSA20_POLY1305_SECRETBOX_NONCEBYTES; | ||
import static org.libsodium.jni.SodiumConstants.ZERO_BYTES; | ||
import static org.libsodium.jni.crypto.Util.checkLength; | ||
import static org.libsodium.jni.crypto.Util.isValid; | ||
import static org.libsodium.jni.crypto.Util.removeZeros; | ||
import org.bouncycastle.crypto.engines.XSalsa20Engine; | ||
import org.bouncycastle.crypto.macs.Poly1305; | ||
import org.bouncycastle.crypto.params.KeyParameter; | ||
import org.bouncycastle.crypto.params.ParametersWithIV; | ||
|
||
import java.security.MessageDigest; | ||
import java.util.Arrays; | ||
|
||
public class SecretBox { | ||
|
||
private byte[] mKey; | ||
private Encoder mEncoder; | ||
private final byte[] mKey; | ||
|
||
public SecretBox(byte[] key) { | ||
checkLength(key, XSALSA20_POLY1305_SECRETBOX_KEYBYTES); | ||
mEncoder = Encoder.RAW; | ||
mKey = key; | ||
checkLength(key, SECRET_KEY_LEN); | ||
mKey = Arrays.copyOf(key, key.length); | ||
} | ||
|
||
public byte[] encrypt(byte[] message) { | ||
byte[] nonce = new Random().randomBytes(XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); | ||
byte[] nonce = generateRandomBytesArray(NONCE_SIZE); | ||
return encrypt(nonce, message); | ||
} | ||
|
||
public byte[] encrypt(byte[] nonce, byte[] message) { | ||
checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); | ||
byte[] msg = org.libsodium.jni.crypto.Util.prependZeros(ZERO_BYTES, message); | ||
byte[] ct = org.libsodium.jni.crypto.Util.zeros(msg.length); | ||
isValid(sodium().crypto_secretbox_xsalsa20poly1305(ct, msg, msg.length, | ||
nonce, mKey), "Encryption failed"); | ||
byte[] cipherWithoutNonce = removeZeros(BOXZERO_BYTES, ct); | ||
public byte[] encrypt(byte[] nonce, byte[] plaintext) { | ||
checkLength(nonce, NONCE_SIZE); | ||
|
||
XSalsa20Engine xsalsa20 = new XSalsa20Engine(); | ||
Poly1305 poly1305 = new Poly1305(); | ||
|
||
xsalsa20.init(true, new ParametersWithIV(new KeyParameter(mKey), nonce)); | ||
byte[] subKey = new byte[SECRET_KEY_LEN]; | ||
xsalsa20.processBytes(subKey, 0, SECRET_KEY_LEN, subKey, 0); | ||
byte[] cipherWithoutNonce = new byte[plaintext.length + poly1305.getMacSize()]; | ||
xsalsa20.processBytes(plaintext, 0, plaintext.length, cipherWithoutNonce, poly1305.getMacSize()); | ||
|
||
// hash ciphertext and prepend mac to ciphertext | ||
poly1305.init(new KeyParameter(subKey)); | ||
poly1305.update(cipherWithoutNonce, poly1305.getMacSize(), plaintext.length); | ||
poly1305.doFinal(cipherWithoutNonce, 0); | ||
|
||
byte[] ciphertext = new byte[cipherWithoutNonce.length + | ||
XSALSA20_POLY1305_SECRETBOX_NONCEBYTES]; | ||
NONCE_SIZE]; | ||
System.arraycopy(nonce, 0, ciphertext, 0, nonce.length); | ||
System.arraycopy(cipherWithoutNonce, 0, ciphertext, nonce.length, | ||
cipherWithoutNonce.length); | ||
return ciphertext; | ||
} | ||
|
||
public byte[] decrypt(byte[] ciphertext) { | ||
byte[] nonce = Arrays.copyOfRange(ciphertext, 0, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); | ||
byte[] message = Arrays.copyOfRange(ciphertext, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES, | ||
byte[] nonce = Arrays.copyOfRange(ciphertext, 0, NONCE_SIZE); | ||
byte[] message = Arrays.copyOfRange(ciphertext, NONCE_SIZE, | ||
ciphertext.length); | ||
return decrypt(nonce, message); | ||
} | ||
|
||
public byte[] decrypt(byte[] nonce, byte[] ciphertext) { | ||
checkLength(nonce, XSALSA20_POLY1305_SECRETBOX_NONCEBYTES); | ||
byte[] ct = org.libsodium.jni.crypto.Util.prependZeros(BOXZERO_BYTES, ciphertext); | ||
byte[] message = Util.zeros(ct.length); | ||
isValid(sodium().crypto_secretbox_xsalsa20poly1305_open(message, ct, | ||
ct.length, nonce, mKey), "Decryption failed. Ciphertext failed verification"); | ||
return removeZeros(ZERO_BYTES, message); | ||
|
||
private byte[] decrypt(byte[] nonce, byte[] ciphertext) { | ||
checkLength(nonce, NONCE_SIZE); | ||
|
||
XSalsa20Engine xsalsa20 = new XSalsa20Engine(); | ||
Poly1305 poly1305 = new Poly1305(); | ||
|
||
xsalsa20.init(false, new ParametersWithIV(new KeyParameter(mKey), nonce)); | ||
byte[] sk = new byte[SECRET_KEY_LEN]; | ||
xsalsa20.processBytes(sk, 0, sk.length, sk, 0); | ||
|
||
// hash ciphertext | ||
poly1305.init(new KeyParameter(sk)); | ||
int len = Math.max(ciphertext.length - poly1305.getMacSize(), 0); | ||
poly1305.update(ciphertext, poly1305.getMacSize(), len); | ||
byte[] calculatedMAC = new byte[poly1305.getMacSize()]; | ||
poly1305.doFinal(calculatedMAC, 0); | ||
|
||
// extract mac | ||
final byte[] presentedMAC = new byte[poly1305.getMacSize()]; | ||
System.arraycopy( | ||
ciphertext, 0, presentedMAC, 0, Math.min(ciphertext.length, poly1305.getMacSize())); | ||
|
||
if (!MessageDigest.isEqual(calculatedMAC, presentedMAC)) { | ||
throw new IllegalArgumentException("Invalid MAC"); | ||
} | ||
|
||
byte[] plaintext = new byte[len]; | ||
xsalsa20.processBytes(ciphertext, poly1305.getMacSize(), plaintext.length, plaintext, 0); | ||
return plaintext; | ||
} | ||
|
||
private void checkLength(byte[] data, int size) { | ||
if (data == null || data.length != size) | ||
throw new IllegalArgumentException("Invalid size: " + data.length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xbr.network; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.junit.Test; | ||
|
||
import io.crossbar.autobahn.utils.Pair; | ||
|
||
public class UtilTest { | ||
|
||
@Test | ||
public void testGenerateRandomBytesArray() { | ||
int size = 32; | ||
byte[] randomBytes = Util.generateRandomBytesArray(size); | ||
|
||
assertNotNull(randomBytes); | ||
assertEquals(size, randomBytes.length); | ||
} | ||
|
||
@Test | ||
public void testGenerateKeyPair() { | ||
Pair<byte[], byte[]> keyPair = Util.generateX25519KeyPair(); | ||
|
||
assertNotNull(keyPair); | ||
assertNotNull(keyPair.first); | ||
assertNotNull(keyPair.second); | ||
|
||
assertEquals(32, keyPair.first.length); | ||
assertEquals(32, keyPair.second.length); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
autobahn/src/test/java/xbr/network/crypto/SecretBoxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package xbr.network.crypto; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static xbr.network.Util.NONCE_SIZE; | ||
import static xbr.network.Util.generateRandomBytesArray; | ||
|
||
import org.junit.Test; | ||
|
||
public class SecretBoxTest { | ||
|
||
@Test | ||
public void testConstructor() { | ||
// test with valid key | ||
new SecretBox(new byte[32]); | ||
|
||
// test with invalid key | ||
assertThrows(IllegalArgumentException.class, () -> new SecretBox(new byte[16])); | ||
|
||
// test with null key | ||
assertThrows(NullPointerException.class, () -> new SecretBox(null)); | ||
} | ||
|
||
@Test | ||
public void testEncryptAndDecrypt() { | ||
SecretBox secretBox = new SecretBox(new byte[32]); | ||
byte[] message = "Hello, World!".getBytes(); | ||
byte[] encrypted = secretBox.encrypt(message); | ||
byte[] decrypted = secretBox.decrypt(encrypted); | ||
assertArrayEquals(message, decrypted); | ||
} | ||
|
||
@Test | ||
public void testEncryptAndDecryptWithNonce() { | ||
SecretBox secretBox = new SecretBox(new byte[32]); | ||
byte[] nonce = generateRandomBytesArray(NONCE_SIZE); | ||
byte[] message = "Hello, World!".getBytes(); | ||
byte[] encrypted = secretBox.encrypt(nonce, message); | ||
byte[] decrypted = secretBox.decrypt(encrypted); | ||
assertArrayEquals(message, decrypted); | ||
} | ||
|
||
@Test | ||
public void testEncryptAndDecryptWithInvalidMAC() { | ||
SecretBox secretBox = new SecretBox(new byte[32]); | ||
byte[] message = "Hello, World!".getBytes(); | ||
byte[] encrypted = secretBox.encrypt(message); | ||
encrypted[encrypted.length - 1] ^= 0xFF; // Modify last byte | ||
assertThrows(IllegalArgumentException.class, () -> secretBox.decrypt(encrypted)); | ||
} | ||
|
||
@Test | ||
public void testEncryptAndDecryptWithInvalidNonce() { | ||
SecretBox secretBox = new SecretBox(new byte[32]); | ||
byte[] message = "Hello, World!".getBytes(); | ||
byte[] encrypted = secretBox.encrypt(message); | ||
encrypted[0] ^= 0xFF; // Modify first byte | ||
assertThrows(IllegalArgumentException.class, () -> secretBox.decrypt(encrypted)); | ||
} | ||
|
||
@Test | ||
public void testEncryptAndDecryptWithModifiedCiphertext() { | ||
byte[] key = new byte[32]; | ||
SecretBox secretBox = new SecretBox(key); | ||
byte[] message = "Hello, World!".getBytes(); | ||
byte[] encrypted = secretBox.encrypt(message); | ||
encrypted[NONCE_SIZE + 1] ^= 0xFF; // Modify the byte next to nonce | ||
assertThrows(IllegalArgumentException.class, () -> secretBox.decrypt(encrypted)); | ||
} | ||
|
||
} |