Search in sources :

Example 61 with Nonce

use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.

the class EncoderCompatibilityTest method v2toV3NonPsv.

@Test
public void v2toV3NonPsv() {
    final V2EncodedPayload v2Payload = V2EncodedPayload.Builder.create().withSenderKey(PublicKey.from("SENDER".getBytes())).withCipherText("CIPHER_TEXT".getBytes()).withCipherTextNonce(new Nonce("NONCE".getBytes())).withRecipientBoxes(singletonList("recipientBox".getBytes())).withRecipientNonce(new Nonce("recipientNonce".getBytes())).withRecipientKeys(List.of(PublicKey.from("KEY1".getBytes()))).withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withAffectedContractTransactions(Map.of(TxHash.from("hash".getBytes()), "hash".getBytes())).build();
    final byte[] encoded = v2Encoder.encode(v2Payload);
    final V3EncodedPayload encodedPayload = v3Encoder.decode(encoded);
    assertThat(encodedPayload.getSenderKey()).isEqualTo(v2Payload.getSenderKey());
    assertThat(encodedPayload.getCipherText()).isEqualTo(v2Payload.getCipherText());
    assertThat(encodedPayload.getCipherTextNonce()).isEqualTo(v2Payload.getCipherTextNonce());
    assertThat(encodedPayload.getRecipientBoxes()).isEqualTo(v2Payload.getRecipientBoxes());
    assertThat(encodedPayload.getRecipientNonce()).isEqualTo(v2Payload.getRecipientNonce());
    assertThat(encodedPayload.getRecipientKeys()).isEqualTo(v2Payload.getRecipientKeys());
    // Enhanced privacy values
    assertThat(encodedPayload.getPrivacyMode()).isEqualTo(v2Payload.getPrivacyMode());
    assertThat(encodedPayload.getAffectedContractTransactions()).isEqualTo(v2Payload.getAffectedContractTransactions());
    assertThat(encodedPayload.getExecHash()).isNullOrEmpty();
    assertThat(encodedPayload.getPrivacyGroupId()).isEmpty();
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 62 with Nonce

use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.

the class EncoderCompatibilityTest method legacyToV4.

@Test
public void legacyToV4() {
    final LegacyEncodedPayload legacyPayload = new LegacyEncodedPayload(PublicKey.from("SENDER".getBytes()), "cipherText".getBytes(), new Nonce("cipherTextNonce".getBytes()), List.of("box".getBytes()), new Nonce("recipientNonce".getBytes()), List.of(PublicKey.from("RECIPIENT".getBytes())));
    final byte[] encoded = legacyEncoder.encode(legacyPayload);
    final EncodedPayload payload = v4Encoder.decode(encoded);
    final List<RecipientBox> boxes = legacyPayload.getRecipientBoxes().stream().map(RecipientBox::from).collect(Collectors.toList());
    assertThat(payload.getSenderKey()).isEqualTo(legacyPayload.getSenderKey());
    assertThat(payload.getCipherText()).isEqualTo(legacyPayload.getCipherText());
    assertThat(payload.getCipherTextNonce()).isEqualTo(legacyPayload.getCipherTextNonce());
    assertThat(payload.getRecipientBoxes()).isEqualTo(boxes);
    assertThat(payload.getRecipientNonce()).isEqualTo(legacyPayload.getRecipientNonce());
    assertThat(payload.getRecipientKeys()).isEqualTo(legacyPayload.getRecipientKeys());
    // Default enhanced privacy values
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(payload.getAffectedContractTransactions()).isEmpty();
    assertThat(payload.getExecHash()).isNullOrEmpty();
    assertThat(payload.getPrivacyGroupId()).isEmpty();
    assertThat(payload.getMandatoryRecipients()).isEmpty();
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 63 with Nonce

use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.

the class EncoderCompatibilityTest method legacyToV2.

@Test
public void legacyToV2() {
    final LegacyEncodedPayload legacyPayload = new LegacyEncodedPayload(PublicKey.from("SENDER".getBytes()), "cipherText".getBytes(), new Nonce("cipherTextNonce".getBytes()), List.of("box".getBytes()), new Nonce("recipientNonce".getBytes()), List.of(PublicKey.from("RECIPIENT".getBytes())));
    final byte[] encoded = legacyEncoder.encode(legacyPayload);
    final V2EncodedPayload v2Payload = v2Encoder.decode(encoded);
    final List<RecipientBox> boxes = legacyPayload.getRecipientBoxes().stream().map(RecipientBox::from).collect(Collectors.toList());
    assertThat(v2Payload.getSenderKey()).isEqualTo(legacyPayload.getSenderKey());
    assertThat(v2Payload.getCipherText()).isEqualTo(legacyPayload.getCipherText());
    assertThat(v2Payload.getCipherTextNonce()).isEqualTo(legacyPayload.getCipherTextNonce());
    assertThat(v2Payload.getRecipientBoxes()).isEqualTo(boxes);
    assertThat(v2Payload.getRecipientNonce()).isEqualTo(legacyPayload.getRecipientNonce());
    assertThat(v2Payload.getRecipientKeys()).isEqualTo(legacyPayload.getRecipientKeys());
    // Default enhanced privacy values
    assertThat(v2Payload.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(v2Payload.getAffectedContractTransactions()).isEmpty();
    assertThat(v2Payload.getExecHash()).isNullOrEmpty();
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 64 with Nonce

use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.

the class EncoderCompatibilityTest method v2ToLegacy.

@Test
public void v2ToLegacy() {
    final V2EncodedPayload v2Payload = V2EncodedPayload.Builder.create().withSenderKey(PublicKey.from("SENDER".getBytes())).withCipherText("CIPHER_TEXT".getBytes()).withCipherTextNonce(new Nonce("NONCE".getBytes())).withRecipientBoxes(singletonList("recipientBox".getBytes())).withRecipientNonce(new Nonce("recipientNonce".getBytes())).withRecipientKeys(List.of(PublicKey.from("KEY1".getBytes()))).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(emptyMap()).build();
    final byte[] encoded = v2Encoder.encode(v2Payload);
    final LegacyEncodedPayload legacyPayload = legacyEncoder.decode(encoded);
    final List<RecipientBox> boxes = legacyPayload.getRecipientBoxes().stream().map(RecipientBox::from).collect(Collectors.toList());
    assertThat(legacyPayload.getSenderKey()).isEqualTo(v2Payload.getSenderKey());
    assertThat(legacyPayload.getCipherText()).isEqualTo(v2Payload.getCipherText());
    assertThat(legacyPayload.getCipherTextNonce()).isEqualTo(v2Payload.getCipherTextNonce());
    assertThat(boxes).isEqualTo(v2Payload.getRecipientBoxes());
    assertThat(legacyPayload.getRecipientNonce()).isEqualTo(v2Payload.getRecipientNonce());
    assertThat(legacyPayload.getRecipientKeys()).isEqualTo(v2Payload.getRecipientKeys());
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 65 with Nonce

use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.

the class LegacyPayloadEncoder method decode.

public LegacyEncodedPayload decode(final byte[] input) {
    final ByteBuffer buffer = ByteBuffer.wrap(input);
    final long senderSize = buffer.getLong();
    final byte[] senderKey = new byte[Math.toIntExact(senderSize)];
    buffer.get(senderKey);
    final long cipherTextSize = buffer.getLong();
    final byte[] cipherText = new byte[Math.toIntExact(cipherTextSize)];
    buffer.get(cipherText);
    final long nonceSize = buffer.getLong();
    final byte[] nonce = new byte[Math.toIntExact(nonceSize)];
    buffer.get(nonce);
    final long numberOfRecipients = buffer.getLong();
    final List<byte[]> recipientBoxes = new ArrayList<>();
    for (long i = 0; i < numberOfRecipients; i++) {
        final long boxSize = buffer.getLong();
        final byte[] box = new byte[Math.toIntExact(boxSize)];
        buffer.get(box);
        recipientBoxes.add(box);
    }
    final long recipientNonceSize = buffer.getLong();
    final byte[] recipientNonce = new byte[Math.toIntExact(recipientNonceSize)];
    buffer.get(recipientNonce);
    // participant)
    if (!buffer.hasRemaining()) {
        return new LegacyEncodedPayload(PublicKey.from(senderKey), cipherText, new Nonce(nonce), recipientBoxes, new Nonce(recipientNonce), emptyList());
    }
    final long recipientLength = buffer.getLong();
    final List<byte[]> recipientKeys = new ArrayList<>();
    for (long i = 0; i < recipientLength; i++) {
        final long boxSize = buffer.getLong();
        final byte[] box = new byte[Math.toIntExact(boxSize)];
        buffer.get(box);
        recipientKeys.add(box);
    }
    return new LegacyEncodedPayload(PublicKey.from(senderKey), cipherText, new Nonce(nonce), recipientBoxes, new Nonce(recipientNonce), recipientKeys.stream().map(PublicKey::from).collect(toList()));
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) PublicKey(com.quorum.tessera.encryption.PublicKey) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Nonce (com.quorum.tessera.encryption.Nonce)72 Test (org.junit.Test)64 LegacyEncodedPayload (com.quorum.tessera.enclave.encoder.LegacyEncodedPayload)31 PublicKey (com.quorum.tessera.encryption.PublicKey)18 SharedKey (com.quorum.tessera.encryption.SharedKey)6 ArgonResult (com.quorum.tessera.argon2.ArgonResult)4 com.quorum.tessera.enclave (com.quorum.tessera.enclave)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ArgonOptions (com.quorum.tessera.config.ArgonOptions)3 PrivateKeyData (com.quorum.tessera.config.PrivateKeyData)3 MasterKey (com.quorum.tessera.encryption.MasterKey)3 PrivateKey (com.quorum.tessera.encryption.PrivateKey)3 Response (jakarta.ws.rs.core.Response)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Service (com.quorum.tessera.service.Service)2 RecipientKeyNotFoundException (com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)2 TransactionNotFoundException (com.quorum.tessera.transaction.exception.TransactionNotFoundException)2 Json (jakarta.json.Json)2