Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
70692bd
26.4-snapshot-1..5: Add Minecraft 26.4 protocol version
WouterGritter Nov 25, 2026
6a07a1f
26.4-snapshot-1..5: Remove partial hashed seed from JoinGame and Respawn
WouterGritter Nov 25, 2026
cd73d8d
26.4-snapshot-1..5: Support transfer properties
WouterGritter Nov 25, 2026
288a1ef
26.4-snapshot-1..5: Replay client mod list to backend servers
WouterGritter Nov 25, 2026
2586605
26.4-snapshot-1..5: Drop config plugin messages while backend connect…
WouterGritter Nov 25, 2026
caf5c56
26.4-snapshot-1..5: Defer enabling compression until login is acknowl…
WouterGritter Nov 25, 2026
56c40e7
26.4-snapshot-1..5: Defer enabling encryption until client applies op…
WouterGritter Nov 25, 2026
9f04983
26.4-snapshot-6: Bump protocol version
WouterGritter Nov 25, 2026
9499bf6
26.4-snapshot-8: Bump protocol version
WouterGritter Nov 25, 2026
5a10a4d
26.4-snapshot-8: Renumber play packet IDs
WouterGritter Nov 25, 2026
bb0f42b
26.4-snapshot-8: Remove debug flag from JoinGame and Respawn
WouterGritter Nov 25, 2026
ac1193d
26.4-snapshot-8: Renumber command argument type IDs
WouterGritter Nov 25, 2026
fa674c0
26.4-snapshot-8: Update command suggestion and argument serialization
WouterGritter Nov 25, 2026
8711d76
26.4-snapshot-9..10: Bump protocol version
WouterGritter Nov 25, 2026
7644e34
26.4-snapshot-9..10: Split string argument type into word, quoted_str…
WouterGritter Nov 25, 2026
2e71237
26.4-snapshot-9..10: Replace RSA login encryption with X25519 key exc…
WouterGritter Nov 25, 2026
685e3ec
26.4-pre-1: Bump protocol version
WouterGritter Nov 25, 2026
9bd706c
Native encryption support for 26.4+ with tests (TODO: rebuild macos n…
WouterGritter Nov 25, 2026
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
Expand Up @@ -97,7 +97,8 @@ public boolean isSupported() {
MINECRAFT_1_21_11(774, "1.21.11"),
MINECRAFT_26_1(775, "26.1", "26.1.1", "26.1.2"),
MINECRAFT_26_2(776, "26.2"),
MINECRAFT_26_3(777, "26.3");
MINECRAFT_26_3(777, "26.3"),
MINECRAFT_26_4(-1, 349, "26.4"); // TODO

private static final int SNAPSHOT_BIT = 30;

Expand Down
26 changes: 16 additions & 10 deletions native/src/main/c/jni_cipher_macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
jclass clazz,
jbyteArray key,
jbyteArray iv,
jboolean encrypt)
{
jsize keyLen = (*env)->GetArrayLength(env, key);
Expand All @@ -18,6 +19,12 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
return 0;
}

jsize ivLen = (*env)->GetArrayLength(env, iv);
if (ivLen != 16) {
throwException(env, "java/lang/IllegalArgumentException", "IV 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];
Expand All @@ -26,21 +33,20 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
return 0;
}

// But, you're saying, *why* are we using the key as the IV? After all, reusing the key as
// the IV defeats the entire point - we might as well just initialize it to all zeroes.
//
// You can blame Mojang. For the record, we also don't consider the Minecraft protocol
// encryption scheme to be secure, and it has reached the point where any serious cryptographic
// protocol needs a refresh. There are multiple obvious weaknesses, and this is far from the
// most serious.
//
// If you are using Minecraft in a security-sensitive application, *I don't know what to say.*
jbyte ivBytes[16];
(*env)->GetByteArrayRegion(env, iv, 0, ivLen, (jbyte*) ivBytes);
if ((*env)->ExceptionCheck(env)) {
return 0;
}

// Before 26.4, the protocol used the shared secret as both the key and the IV. That is the
// caller's business now: the Java side passes the key as the IV for those older protocols.
CCCryptorRef cryptor = NULL;
CCCryptorStatus result = CCCryptorCreateWithMode(encrypt ? kCCEncrypt : kCCDecrypt,
kCCModeCFB8,
kCCAlgorithmAES128,
ccNoPadding,
keyBytes,
ivBytes,
keyBytes,
16,
NULL,
Expand Down
32 changes: 19 additions & 13 deletions native/src/main/c/jni_cipher_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
jclass clazz,
jbyteArray key,
jbyteArray iv,
jboolean encrypt)
{
jsize keyLen = (*env)->GetArrayLength(env, key);
Expand All @@ -18,9 +19,9 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
return 0;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
throwException(env, "java/lang/OutOfMemoryError", "allocate cipher");
jsize ivLen = (*env)->GetArrayLength(env, iv);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: GetArrayLength on a null reference crashes the JVM instead of throwing. The only caller never passes null today, but a short null check that throws NullPointerException would be cheap insurance (same in jni_cipher_macos.c, and the same hazard already exists for key).

if (ivLen != 16) {
throwException(env, "java/lang/IllegalArgumentException", "IV not 16 bytes");
return 0;
}

Expand All @@ -32,16 +33,21 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
return 0;
}

// But, you're saying, *why* are we using the key as the IV? After all, reusing the key as
// the IV defeats the entire point - we might as well just initialize it to all zeroes.
//
// You can blame Mojang. For the record, we also don't consider the Minecraft protocol
// encryption scheme to be secure, and it has reached the point where any serious cryptographic
// protocol needs a refresh. There are multiple obvious weaknesses, and this is far from the
// most serious.
//
// If you are using Minecraft in a security-sensitive application, *I don't know what to say.*
int result = EVP_CipherInit(ctx, EVP_aes_128_cfb8(), (byte*) keyBytes, (byte*) keyBytes,
jbyte ivBytes[16];
(*env)->GetByteArrayRegion(env, iv, 0, ivLen, (jbyte*) ivBytes);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: keyBytes and now ivBytes are left on the stack after return. With 26.4 the IV is HKDF-derived, so it may be worth an OPENSSL_cleanse on both before returning (memset_s on the macOS side). Also, the (jbyte*) cast is redundant here since the buffer is already jbyte[].

if ((*env)->ExceptionCheck(env)) {
return 0;
}

EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
throwException(env, "java/lang/OutOfMemoryError", "allocate cipher");
return 0;
}

// Before 26.4, the protocol used the shared secret as both the key and the IV. That is the
// caller's business now: the Java side passes the key as the IV for those older protocols.
int result = EVP_CipherInit(ctx, EVP_aes_128_cfb8(), (byte*) keyBytes, (byte*) ivBytes,
encrypt);
if (result != 1) {
EVP_CIPHER_CTX_free(ctx);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
* Copyright (C) 2018-2026 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,6 +25,7 @@
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Implements AES-CFB8 encryption/decryption using {@link Cipher}.
Expand All @@ -33,32 +34,41 @@ public class JavaVelocityCipher implements VelocityCipher {

public static final VelocityCipherFactory FACTORY = new VelocityCipherFactory() {
@Override
public VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(true, key);
public VelocityCipher forEncryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
return new JavaVelocityCipher(true, key, iv);
}

@Override
public VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(false, key);
public VelocityCipher forDecryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
return new JavaVelocityCipher(false, key, iv);
}
};

private final Cipher cipher;
private boolean disposed = false;

private JavaVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
private JavaVelocityCipher(boolean encrypt, SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
final byte[] effectiveIv;
if (iv == null) {
// Before 26.4, the protocol used the shared secret as both the key and the IV, in both
// directions. Reusing the key as the IV defeats the entire point of an IV - we might as well
// just initialize it to all zeroes - and because both directions use the same key and IV,
// both streams start out in the same cipher state.
//
// 26.4 replaced this with a proper key exchange that derives a separate key and IV for each
// direction. Not passing an IV (insecure!) is only for older protocols.
effectiveIv = key.getEncoded();
} else if (iv.length == VelocityCipherFactory.IV_LENGTH) {
effectiveIv = iv;
} else {
throw new IllegalArgumentException("IV not " + VelocityCipherFactory.IV_LENGTH + " bytes");
}
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
// But, you're saying, *why* are we using the key as the IV? After all, reusing the key as
// the IV defeats the entire point - we might as well just initialize it to all zeroes.
//
// You can blame Mojang. For the record, we also don't consider the Minecraft protocol
// encryption scheme to be secure, and it has reached the point where any serious cryptographic
// protocol needs a refresh. There are multiple obvious weaknesses, and this is far from the
// most serious.
//
// If you are using Minecraft in a security-sensitive application, *I don't know what to say.*
this.cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key,
new IvParameterSpec(key.getEncoded()));
new IvParameterSpec(effectiveIv));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
* Copyright (C) 2018-2026 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,6 +22,7 @@
import io.netty.buffer.ByteBuf;
import java.security.GeneralSecurityException;
import javax.crypto.SecretKey;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Implements AES-CFB8 encryption/decryption using a native library.
Expand All @@ -30,20 +31,25 @@ public class NativeVelocityCipher implements VelocityCipher {

public static final VelocityCipherFactory FACTORY = new VelocityCipherFactory() {
@Override
public VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException {
return new NativeVelocityCipher(true, key);
public VelocityCipher forEncryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
return new NativeVelocityCipher(true, key, iv);
}

@Override
public VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException {
return new NativeVelocityCipher(false, key);
public VelocityCipher forDecryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
return new NativeVelocityCipher(false, key, iv);
}
};
private final long ctx;
private boolean disposed = false;

private NativeVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
this.ctx = OpenSslCipherImpl.init(key.getEncoded(), encrypt);
private NativeVelocityCipher(boolean encrypt, SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException {
byte[] keyBytes = key.getEncoded();
// A missing IV means the pre-26.4 behavior of using the key as the IV (see JavaVelocityCipher).
this.ctx = OpenSslCipherImpl.init(keyBytes, iv == null ? keyBytes : iv, encrypt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2021 Velocity Contributors
* Copyright (C) 2020-2026 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,7 @@

class OpenSslCipherImpl {

static native long init(byte[] key, boolean encrypt) throws GeneralSecurityException;
static native long init(byte[] key, byte[] iv, boolean encrypt) throws GeneralSecurityException;

static native void process(long ctx, long source, int len, long dest);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
* Copyright (C) 2018-2026 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,13 +19,59 @@

import java.security.GeneralSecurityException;
import javax.crypto.SecretKey;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A factory interface for {@link VelocityCipher}.
*/
public interface VelocityCipherFactory {

VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException;
/**
* The length of the AES-CFB8 initialization vector, in bytes.
*/
int IV_LENGTH = 16;

VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException;
/**
* Creates an encrypting cipher that uses the key as the IV, as the pre-26.4 protocol does.
*
* @param key the AES key
* @return the cipher
* @throws GeneralSecurityException if the cipher can't be initialized
*/
default VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException {
return forEncryption(key, null);
}

/**
* Creates an encrypting cipher.
*
* @param key the AES key
* @param iv the {@value #IV_LENGTH}-byte IV, or {@code null} to use the key as the IV
* @return the cipher
* @throws GeneralSecurityException if the cipher can't be initialized
*/
VelocityCipher forEncryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException;

/**
* Creates a decrypting cipher that uses the key as the IV, as the pre-26.4 protocol does.
*
* @param key the AES key
* @return the cipher
* @throws GeneralSecurityException if the cipher can't be initialized
*/
default VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException {
return forDecryption(key, null);
}

/**
* Creates a decrypting cipher.
*
* @param key the AES key
* @param iv the {@value #IV_LENGTH}-byte IV, or {@code null} to use the key as the IV
* @return the cipher
* @throws GeneralSecurityException if the cipher can't be initialized
*/
VelocityCipher forDecryption(SecretKey key, byte @Nullable [] iv)
throws GeneralSecurityException;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions native/src/main/resources/macos_arm64/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: REMOVE .gitkeep WHEN MACOS BINARIES ARE REBUILT
Binary file not shown.
Loading