Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace libsodium with cryptology #558

Merged
merged 22 commits into from
Mar 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d6aec07
use bouncycastle in cryptosign auth
muzzammilshahid Feb 14, 2024
9262042
add missing space
muzzammilshahid Feb 14, 2024
5b1b713
add unit tests for CryptosignAuth
muzzammilshahid Feb 19, 2024
0f1c292
replace libsodium with bouncy castle in SecretBox
muzzammilshahid Feb 24, 2024
967e127
add missing EOL
muzzammilshahid Feb 24, 2024
838b6b8
add function to create sealedbox nonce
om26er Feb 26, 2024
b1e88c6
add function to compute shared secret for sealedbox
om26er Feb 26, 2024
cdcc45e
implement the libsodium compatible seal function
om26er Feb 26, 2024
8ce13a5
implement the libsodium compatible unseal function
om26er Feb 26, 2024
af76c2c
make encrypt to only use the new code
om26er Feb 26, 2024
2ce33d2
Merge pull request #2 from om26er/sealedbox-bouncy
muzzammilshahid Feb 26, 2024
c6f45a7
remove dependency of libsodium
muzzammilshahid Feb 26, 2024
41b71b9
use correct version of web3j
muzzammilshahid Feb 26, 2024
fbdd505
use correct dependencies
muzzammilshahid Feb 26, 2024
492a7f1
add HSalsa20 implementation based on bouncy castle
muzzammilshahid Feb 26, 2024
5ec6bda
make HSALSA20_SEED global,static variable
muzzammilshahid Feb 26, 2024
b0300d7
add lisence for HSalsa20
muzzammilshahid Feb 26, 2024
cffc968
update web3j and java
muzzammilshahid Feb 27, 2024
0910973
use cryptology
muzzammilshahid Mar 6, 2024
e0a1b0d
exclude bouncy-castle from cryptology
muzzammilshahid Mar 6, 2024
9bf9cf5
remove unnecessary checkLength() function
muzzammilshahid Mar 6, 2024
3ebb097
update cryptology
muzzammilshahid Mar 20, 2024
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.crossbar.autobahn.wamp.auth;

import org.libsodium.jni.crypto.Random;
import org.libsodium.jni.keys.SigningKey;
import org.libsodium.jni.keys.VerifyKey;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.crypto.signers.Ed25519Signer;

import java.io.File;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -16,7 +20,6 @@
import io.crossbar.autobahn.wamp.types.Challenge;
import io.crossbar.autobahn.wamp.types.ChallengeResponse;

import static org.libsodium.jni.SodiumConstants.SECRETKEY_BYTES;

public class CryptosignAuth implements IAuthenticator {
public static final String authmethod = "cryptosign";
Expand All @@ -28,22 +31,29 @@ public class CryptosignAuth implements IAuthenticator {
private final byte[] privateKeyRaw;

public static Pair<String, String> generateSigningKeyPair() {
VerifyKey key = new VerifyKey(new Random().randomBytes(SECRETKEY_BYTES));
SigningKey signingKey = new SigningKey(key.toBytes());
Ed25519KeyPairGenerator keyPairGenerator = new Ed25519KeyPairGenerator();
keyPairGenerator.init(new Ed25519KeyGenerationParameters(new SecureRandom()));

String privateKey = AuthUtil.toHexString(key.toBytes());
String publicKey = AuthUtil.toHexString(signingKey.getVerifyKey().toBytes());
AsymmetricCipherKeyPair keyPair = keyPairGenerator.generateKeyPair();

return new Pair<>(publicKey, privateKey);
Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters) keyPair.getPrivate();
Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters) keyPair.getPublic();

String privateKeyHex = AuthUtil.toHexString(privateKey.getEncoded());
String publicKeyHex = AuthUtil.toHexString(publicKey.getEncoded());

return new Pair<>(publicKeyHex, privateKeyHex);
}

public CryptosignAuth(String authid, String privateKey) {
this(authid, privateKey, getPublicKey(AuthUtil.toBinary(privateKey)));
}

public static String getPublicKey(byte[] privateKeyRaw) {
SigningKey signingKey = new SigningKey(privateKeyRaw);
return AuthUtil.toHexString(signingKey.getVerifyKey().toBytes());
Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(privateKeyRaw, 0);

byte[] publicKeyBytes = privateKey.generatePublicKey().getEncoded();
return AuthUtil.toHexString(publicKeyBytes);
}

public CryptosignAuth(String authid, String privkey, Map<String, Object> authextra) {
Expand Down Expand Up @@ -85,8 +95,11 @@ public CompletableFuture<ChallengeResponse> onChallenge(Session session, Challen
String hexChallenge = (String) challenge.extra.get("challenge");
byte[] rawChallenge = AuthUtil.toBinary(hexChallenge);

SigningKey key = new SigningKey(privateKeyRaw);
byte[] signed = key.sign(rawChallenge);
Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(privateKeyRaw, 0);
Ed25519Signer signer = new Ed25519Signer();
signer.init(true, privateKey);
signer.update(rawChallenge, 0, rawChallenge.length);
byte[] signed =signer.generateSignature();

String signatureHex = AuthUtil.toHexString(signed);
String res = signatureHex + hexChallenge;
Expand Down
Loading