Skip to content

Commit

Permalink
Replace lazysodium with tink
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 20, 2024
1 parent dc41b20 commit 6eceab9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 64 deletions.
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ dependencies {
implementation(libs.bundles.jackson)

//Audio crypto libraries
implementation(libs.jna)
implementation(libs.lazysodium)
implementation(libs.tink)

//Sets the dependencies for the examples
configurations["examplesImplementation"].withDependencies {
Expand Down Expand Up @@ -211,7 +210,7 @@ val noOpusJar by tasks.creating(ShadowJar::class) {
from(sourceSets["main"].output)
exclude("natives/**") // ~2 MB
exclude("com/sun/jna/**") // ~1 MB
exclude("com/goterl/**") // ~1 MB
exclude("com/google/crypto/tink/**") // ~2 MB
exclude("club/minnced/opus/util/*")
exclude("tomp2p/opuswrapper/*")

Expand All @@ -227,7 +226,7 @@ val minimalJar by tasks.creating(ShadowJar::class) {
from(sourceSets["main"].output)
exclude("natives/**") // ~2 MB
exclude("com/sun/jna/**") // ~1 MB
exclude("com/goterl/**") // ~1 MB
exclude("com/google/crypto/tink/**") // ~2 MB
exclude("club/minnced/opus/util/*")
exclude("tomp2p/opuswrapper/*")
manifest.inheritFrom(jar.manifest)
Expand Down
3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ dependencyResolutionManagement {
library("mockito", "org.mockito", "mockito-core" ).version("5.11.0")
library("reflections", "org.reflections", "reflections" ).version("0.10.2")
library("slf4j", "org.slf4j", "slf4j-api" ).version("2.0.13")
library("jna", "net.java.dev.jna", "jna" ).version("5.14.0")
library("lazysodium", "com.goterl", "lazysodium-java" ).version("5.1.4")
library("tink", "com.google.crypto.tink", "tink" ).version("1.14.1")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

public enum AudioEncryption
{
// ordered by priority, suffix > normal

// ordered by priority descending
AEAD_AES256_GCM_RTPSIZE,
AEAD_XCHACHA20_POLY1305_RTPSIZE,
XSALSA20_POLY1305_SUFFIX,
Expand Down
81 changes: 25 additions & 56 deletions src/main/java/net/dv8tion/jda/internal/audio/CryptoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@

package net.dv8tion.jda.internal.audio;

import com.goterl.lazysodium.LazySodiumJava;
import com.goterl.lazysodium.SodiumJava;
import com.goterl.lazysodium.interfaces.AEAD;
import com.google.crypto.tink.aead.internal.InsecureNonceXChaCha20Poly1305;
import com.iwebpp.crypto.TweetNaclFast;
import net.dv8tion.jda.internal.utils.IOUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;
import java.util.EnumSet;

public interface CryptoAdapter
{
SodiumJava sodium = new SodiumJava();
LazySodiumJava lazySodium = new LazySodiumJava(sodium);

String AES_GCM_NO_PADDING = "AES_256/GCM/NOPADDING";
String XCHACHA_POLY1305 = "XCHACHA20-POLY1305";

AudioEncryption getMode();

Expand Down Expand Up @@ -66,7 +60,6 @@ static boolean isModeSupported(AudioEncryption mode)
case AEAD_AES256_GCM_RTPSIZE:
return Security.getAlgorithms("Cipher").contains(AES_GCM_NO_PADDING);
case AEAD_XCHACHA20_POLY1305_RTPSIZE:
return true; // Security.getAlgorithms("Cipher").contains(CHACHA_POLY1305);
case XSALSA20_POLY1305_SUFFIX:
case XSALSA20_POLY1305:
return true;
Expand Down Expand Up @@ -244,61 +237,37 @@ public ByteBuffer encrypt(ByteBuffer output, ByteBuffer audio)
output = newBuffer;
}

byte[] cipherText = new byte[minimumOutputSize];
long[] cipherLength = new long[]{0};

byte[] noncePacked = new byte[AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES];
IOUtil.setIntBigEndian(noncePacked, 0, encryptCounter);
boolean success = lazySodium.cryptoAeadXChaCha20Poly1305IetfEncrypt(
cipherText,
cipherLength,
audio.array(),
audio.remaining(),
output.array(),
output.position(),
null,
noncePacked,
secretKey
);

return output;

// byte[] iv = new byte[24];
// IOUtil.setIntBigEndian(iv, 0, encryptCounter);
//
// try
// {
// Cipher cipher = getCipher(iv);
// cipher.updateAAD(output.array(), 0, output.position());
// cipher.doFinal(audio, output);
// output.putInt(encryptCounter++);
// return output;
// }
// catch (Exception e)
// {
// throw new RuntimeException(e);
// }
}

@Override
public byte[] decrypt(byte[] data, int offset, int length, byte[] nonce)
{
// TODO
return new byte[0];
}
byte[] iv = new byte[24];
IOUtil.setIntBigEndian(iv, 0, encryptCounter);

private Cipher getCipher(byte[] iv)
{
try
{
Cipher cipher = Cipher.getInstance(XCHACHA_POLY1305);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(iv));
return cipher;
InsecureNonceXChaCha20Poly1305 xChaCha20Poly1305 = new InsecureNonceXChaCha20Poly1305(secretKey);

byte[] input = Arrays.copyOfRange(audio.array(), audio.arrayOffset() + audio.position(), audio.arrayOffset() + audio.limit());
byte[] additionalData = Arrays.copyOfRange(output.array(), output.arrayOffset(), output.arrayOffset() + output.position());

byte[] encrypted = xChaCha20Poly1305.encrypt(
iv,
input,
additionalData
);

output.put(encrypted);
output.putInt(encryptCounter++);
return output;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

@Override
public byte[] decrypt(byte[] data, int offset, int length, byte[] nonce)
{
// TODO
return new byte[0];
}
}
}

0 comments on commit 6eceab9

Please sign in to comment.