I have sinned but it feels good
This commit is contained in:
66
native/src/main/c/jni_cipher_macos.c
Normal file
66
native/src/main/c/jni_cipher_macos.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <CommonCrypto/CommonCryptor.h>
|
||||
#include <jni.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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 Get<Primitive>ArrayRegion
|
||||
// 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);
|
||||
}
|
@@ -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;
|
||||
|
@@ -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)
|
||||
)
|
||||
|
BIN
native/src/main/resources/macos_arm64/velocity-cipher.dylib
Executable file
BIN
native/src/main/resources/macos_arm64/velocity-cipher.dylib
Executable file
Binary file not shown.
BIN
native/src/main/resources/macos_arm64/velocity-compress.dylib
Executable file
BIN
native/src/main/resources/macos_arm64/velocity-compress.dylib
Executable file
Binary file not shown.
Reference in New Issue
Block a user