Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions framework/src/main/java/org/tron/core/zen/note/NoteEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public Optional<EncCiphertext> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EncCiphertext> 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());
}
}
Loading