From d96c2930120a0787a64ece846e5dc7e90cb1a3f4 Mon Sep 17 00:00:00 2001 From: federico Date: Tue, 1 Dec 2026 00:35:16 +0800 Subject: [PATCH] fix(crypto): reject public Sapling encryption shared secrets --- .../tron/core/zen/note/NoteEncryption.java | 10 ++ .../zen/note/NoteEncryptionRecipientTest.java | 134 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 framework/src/test/java/org/tron/core/zen/note/NoteEncryptionRecipientTest.java diff --git a/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java b/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java index 048f90dd9d2..1cbe4049849 100644 --- a/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java +++ b/framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java @@ -77,6 +77,16 @@ public Optional encryptToRecipient(byte[] pkD, EncPlaintext messa if (!JLibrustzcash.librustzcashKaAgree(new KaAgreeParams(pkD, esk, dhsecret))) { return Optional.empty(); } + // Rust serializes the shared point canonically. The identity (y = 1, x = 0) + // is public, including when an alternate encoding or any small-order pkD + // reaches key agreement. Reject before KDF/encryption; keep decryption intact. + int nonIdentity = dhsecret[0] ^ 1; + for (int i = 1; i < dhsecret.length; i++) { + nonIdentity |= dhsecret[i]; + } + if (nonIdentity == 0) { + return Optional.empty(); + } byte[] kEnc = new byte[NOTEENCRYPTION_CIPHER_KEYSIZE]; //generate kEnc by sharedsecret and epk diff --git a/framework/src/test/java/org/tron/core/zen/note/NoteEncryptionRecipientTest.java b/framework/src/test/java/org/tron/core/zen/note/NoteEncryptionRecipientTest.java new file mode 100644 index 00000000000..a3770cc6e86 --- /dev/null +++ b/framework/src/test/java/org/tron/core/zen/note/NoteEncryptionRecipientTest.java @@ -0,0 +1,134 @@ +package org.tron.core.zen.note; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.util.Optional; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.tron.common.zksnark.JLibrustzcash; +import org.tron.common.zksnark.JLibsodium; +import org.tron.common.zksnark.JLibsodiumParam.Chacha20Poly1305IetfEncryptParams; +import org.tron.common.zksnark.LibrustzcashParam.KaAgreeParams; +import org.tron.common.zksnark.LibrustzcashParam.KaDerivepublicParams; +import org.tron.core.config.args.Args; +import org.tron.core.exception.ZksnarkException; +import org.tron.core.zen.ShieldedTRC20ParametersBuilder; +import org.tron.core.zen.ZenTransactionBuilder; +import org.tron.core.zen.address.DiversifierT; +import org.tron.core.zen.note.NoteEncryption.Encryption; +import org.tron.core.zen.note.NoteEncryption.Encryption.EncCiphertext; +import org.tron.core.zen.note.NoteEncryption.Encryption.EncPlaintext; + +/** Uses the real JNI library, including the SDK 1.0.0 bundled in the node. */ +public class NoteEncryptionRecipientTest { + + private boolean allowShieldedApi; + private final byte[] diversifier = new byte[] { + 0x4c, 0x41, (byte) 0x8c, (byte) 0xf6, 0x5a, 0x72, + 0x10, (byte) 0xd9, (byte) 0xb6, 0x65, (byte) 0x88}; + + @Before + public void setUp() { + allowShieldedApi = Args.getInstance().isAllowShieldedTransactionApi(); + Args.getInstance().setAllowShieldedTransactionApi(true); + } + + @After + public void tearDown() { + Args.getInstance().setAllowShieldedTransactionApi(allowShieldedApi); + } + + private static byte[] scalar(int value) { + byte[] bytes = new byte[32]; + bytes[0] = (byte) value; + return bytes; + } + + private byte[] publicKey(int secret) throws Exception { + byte[] point = new byte[32]; + assertTrue(JLibrustzcash.librustzcashSaplingKaDerivepublic( + new KaDerivepublicParams(diversifier, scalar(secret), point))); + return point; + } + + @Test + public void rejectsIdentityAliasesAndSmallOrderPointsBeforeEncryption() throws Exception { + byte[] epk = publicKey(4); + byte[] recipient = publicKey(1); + EncPlaintext plaintext = new EncPlaintext(); + plaintext.getData()[20] = 42; + // y=1 is the identity; y=0 represents order-four points. Exercise both signs. + for (int y : new int[] {1, 0}) { + for (int sign : new int[] {0, 128}) { + byte[] badRecipient = scalar(y); + badRecipient[31] = (byte) sign; + byte[] shared = new byte[32]; + assertTrue(JLibrustzcash.librustzcashKaAgree( + new KaAgreeParams(badRecipient, scalar(4), shared))); + assertArrayEquals(scalar(1), shared); + NoteEncryption encryption = new NoteEncryption(epk, scalar(4)); + assertFalse(encryption.encryptToRecipient(badRecipient, plaintext).isPresent()); + // Rejection must not consume the encryption object's one-use state. + Optional ciphertext = encryption.encryptToRecipient(recipient, plaintext); + assertTrue(ciphertext.isPresent()); + assertArrayEquals(plaintext.getData(), Encryption.attemptEncDecryption( + ciphertext.get().getData(), scalar(1), epk).get().getData()); + } + } + } + + @Test + public void rejectsZeroEphemeralSecretForOrdinaryRecipient() throws Exception { + NoteEncryption encryption = new NoteEncryption(publicKey(0), scalar(0)); + assertFalse(encryption.encryptToRecipient(publicKey(1), new EncPlaintext()).isPresent()); + } + + @Test + public void nativeTransactionBuilderRejectsBeforeProving() throws Exception { + ZenTransactionBuilder builder = new ZenTransactionBuilder(); + Note note = new Note(new DiversifierT(diversifier), scalar(1), 7, scalar(6)); + long context = JLibrustzcash.librustzcashSaplingProvingCtxInit(); + try { + ZksnarkException error = assertThrows(ZksnarkException.class, + () -> builder.generateOutputProof( + builder.new ReceiveDescriptionInfo(new byte[32], note), context)); + assertEquals("Failed to encrypt note", error.getMessage()); + } finally { + JLibrustzcash.librustzcashSaplingProvingCtxFree(context); + } + } + + @Test + public void trc20BuilderRejectsBeforeProving() throws Exception { + ShieldedTRC20ParametersBuilder builder = new ShieldedTRC20ParametersBuilder("mint"); + builder.addOutput(new byte[32], new DiversifierT(diversifier), scalar(1), 7, + scalar(6), new byte[0]); + ZksnarkException error = assertThrows(ZksnarkException.class, () -> builder.build(true)); + assertEquals("build the shielded TRC-20 parameters error: Failed to encrypt note", + error.getMessage()); + } + + @Test + public void historicalDegenerateCiphertextRemainsReadable() throws Exception { + // Build the historical ciphertext directly; production sending must reject it. + byte[] epk = publicKey(4); + byte[] key = new byte[32]; + Encryption.kdfSapling(key, scalar(1), epk); + EncPlaintext plaintext = new EncPlaintext(); + plaintext.getData()[30] = 17; + EncCiphertext ciphertext = new EncCiphertext(); + assertEquals(0, JLibsodium.cryptoAeadChacha20Poly1305IetfEncrypt( + new Chacha20Poly1305IetfEncryptParams(ciphertext.getData(), null, + plaintext.getData(), plaintext.getData().length, null, 0, null, + new byte[12], key))); + assertArrayEquals(plaintext.getData(), Encryption.attemptEncDecryption( + ciphertext, epk, scalar(4), scalar(1)).get().getData()); + assertArrayEquals(plaintext.getData(), Encryption.attemptEncDecryption( + ciphertext.getData(), scalar(0), epk).get().getData()); + } +}