diff --git a/native/compile-linux.sh b/native/compile-linux.sh index ceaa58251..5442dce9d 100755 --- a/native/compile-linux.sh +++ b/native/compile-linux.sh @@ -21,5 +21,5 @@ ARCH=$(uname -m) mkdir -p src/main/resources/linux_$ARCH $CC $CFLAGS -Ilibdeflate src/main/c/jni_util.c src/main/c/jni_zlib_deflate.c src/main/c/jni_zlib_inflate.c \ libdeflate/libdeflate.a -o src/main/resources/linux_$ARCH/velocity-compress.so -$CC $CFLAGS -shared src/main/c/jni_util.c src/main/c/jni_cipher.c \ +$CC $CFLAGS -shared src/main/c/jni_util.c src/main/c/jni_cipher_openssl.c \ -o src/main/resources/linux_$ARCH/velocity-cipher.so -lcrypto \ No newline at end of file diff --git a/native/compile-macos.sh b/native/compile-macos.sh new file mode 100755 index 000000000..d920c0921 --- /dev/null +++ b/native/compile-macos.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +if [ ! "$CC" ]; then + export CC=clang +fi + +if [ ! -d libdeflate ]; then + echo "Cloning libdeflate..." + git clone https://github.com/ebiggers/libdeflate.git +fi + +echo "Compiling libdeflate..." +cd libdeflate || exit +CFLAGS="-fPIC -O2 -fomit-frame-pointer" make +cd .. + +CFLAGS="-O2 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -fPIC -shared -Wall -Werror -fomit-frame-pointer" +ARCH=$(uname -m) +mkdir -p src/main/resources/linux_$ARCH +$CC $CFLAGS -Ilibdeflate src/main/c/jni_util.c src/main/c/jni_zlib_deflate.c src/main/c/jni_zlib_inflate.c \ + libdeflate/libdeflate.a -o src/main/resources/macos_$ARCH/velocity-compress.dylib +$CC $CFLAGS -shared src/main/c/jni_util.c src/main/c/jni_cipher_macos.c \ + -o src/main/resources/macos_$ARCH/velocity-cipher.dylib -lSystem \ No newline at end of file diff --git a/native/src/main/c/jni_cipher_macos.c b/native/src/main/c/jni_cipher_macos.c new file mode 100644 index 000000000..aa7d1aba3 --- /dev/null +++ b/native/src/main/c/jni_cipher_macos.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include "jni_util.h" + +typedef unsigned char byte; + +JNIEXPORT jlong JNICALL +Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env, + jclass clazz, + jbyteArray key, + jboolean encrypt) +{ + jsize keyLen = (*env)->GetArrayLength(env, key); + if (keyLen != 16) { + throwException(env, "java/lang/IllegalArgumentException", "cipher not 16 bytes"); + return 0; + } + + // Since we know the array size is always bounded, we can just use GetArrayRegion + // and save ourselves some error-checking headaches. + jbyte keyBytes[16]; + (*env)->GetByteArrayRegion(env, key, 0, keyLen, (jbyte*) keyBytes); + if ((*env)->ExceptionCheck(env)) { + return 0; + } + + CCCryptorRef cryptor = NULL; + CCCryptorStatus result = CCCryptorCreateWithMode(encrypt ? kCCEncrypt : kCCDecrypt, + kCCModeCFB8, + kCCAlgorithmAES128, + ccNoPadding, + keyBytes, + keyBytes, + 16, + NULL, + 0, + 0, + 0, + &cryptor); + if (result != kCCSuccess) { + throwException(env, "java/security/GeneralSecurityException", "openssl initialize cipher"); + return 0; + } + return (jlong) cryptor; +} + +JNIEXPORT void JNICALL +Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_free(JNIEnv *env, + jclass clazz, + jlong ptr) +{ + CCCryptorRelease((CCCryptorRef) ptr); +} + +JNIEXPORT void JNICALL +Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_process(JNIEnv *env, + jclass clazz, + jlong ptr, + jlong source, + jint len, + jlong dest) +{ + CCCryptorUpdate((CCCryptorRef) ptr, (byte*) source, len, (byte*) dest, len, NULL); +} \ No newline at end of file diff --git a/native/src/main/c/jni_cipher.c b/native/src/main/c/jni_cipher_openssl.c similarity index 100% rename from native/src/main/c/jni_cipher.c rename to native/src/main/c/jni_cipher_openssl.c diff --git a/native/src/main/java/com/velocitypowered/natives/util/NativeConstraints.java b/native/src/main/java/com/velocitypowered/natives/util/NativeConstraints.java index ac74c3b4a..68577a7b7 100644 --- a/native/src/main/java/com/velocitypowered/natives/util/NativeConstraints.java +++ b/native/src/main/java/com/velocitypowered/natives/util/NativeConstraints.java @@ -40,7 +40,6 @@ public class NativeConstraints { // give amd64. IS_AMD64 = osArch.equals("amd64") || osArch.equals("x86_64"); IS_AARCH64 = osArch.equals("aarch64") || osArch.equals("arm64"); - System.out.println(System.getProperty("os.name", "")); } static final BooleanSupplier NATIVE_BASE = () -> NATIVES_ENABLED && CAN_GET_MEMORYADDRESS; diff --git a/native/src/main/java/com/velocitypowered/natives/util/Natives.java b/native/src/main/java/com/velocitypowered/natives/util/Natives.java index 928b2dd3d..00db7b353 100644 --- a/native/src/main/java/com/velocitypowered/natives/util/Natives.java +++ b/native/src/main/java/com/velocitypowered/natives/util/Natives.java @@ -85,6 +85,10 @@ public class Natives { copyAndLoadNative("/linux_aarch64/velocity-compress.so"), "libdeflate (Linux aarch64)", LibdeflateVelocityCompressor.FACTORY), + new NativeCodeLoader.Variant<>(NativeConstraints.MACOS_AARCH64, + copyAndLoadNative("/macos_arm64/velocity-compress.dylib"), + "libdeflate (macOS ARM64 / Apple Silicon)", + LibdeflateVelocityCompressor.FACTORY), new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> { }, "Java", JavaVelocityCompressor.FACTORY) ) @@ -104,6 +108,10 @@ public class Natives { new NativeCodeLoader.Variant<>(NativeConstraints.LINUX_AARCH64, copyAndLoadNative("/linux_aarch64/velocity-cipher.so"), "OpenSSL (Linux aarch64)", NativeVelocityCipher.FACTORY), + new NativeCodeLoader.Variant<>(NativeConstraints.MACOS_AARCH64, + copyAndLoadNative("/macos_arm64/velocity-cipher.dylib"), + "native (macOS ARM64 / Apple Silicon)", + NativeVelocityCipher.FACTORY), new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> { }, "Java", JavaVelocityCipher.FACTORY) ) diff --git a/native/src/main/resources/macos_arm64/velocity-cipher.dylib b/native/src/main/resources/macos_arm64/velocity-cipher.dylib new file mode 100755 index 000000000..ba6883dd4 Binary files /dev/null and b/native/src/main/resources/macos_arm64/velocity-cipher.dylib differ diff --git a/native/src/main/resources/macos_arm64/velocity-compress.dylib b/native/src/main/resources/macos_arm64/velocity-compress.dylib new file mode 100755 index 000000000..f0838c361 Binary files /dev/null and b/native/src/main/resources/macos_arm64/velocity-compress.dylib differ