Search in sources :

Example 51 with Nonce

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

the class RestfulEnclaveClientTest method encryptPayloadToRaw.

@Test
public void encryptPayloadToRaw() {
    byte[] message = "HELLOW".getBytes();
    PublicKey senderPublicKey = PublicKey.from("SenderPublicKey".getBytes());
    byte[] encryptedKey = "encryptedKey".getBytes();
    Nonce nonce = new Nonce("Nonce".getBytes());
    RawTransaction rawTransaction = new RawTransaction(message, encryptedKey, nonce, senderPublicKey);
    when(enclave.encryptRawPayload(message, senderPublicKey)).thenReturn(rawTransaction);
    RawTransaction result = enclaveClient.encryptRawPayload(message, senderPublicKey);
    assertThat(result).isNotNull();
    assertThat(result).isEqualTo(rawTransaction);
    verify(enclave).encryptRawPayload(message, senderPublicKey);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 52 with Nonce

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

the class RestfulEnclaveClientTest method unencryptRawPayload.

@Test
public void unencryptRawPayload() throws Exception {
    byte[] message = "HELLOW".getBytes();
    PublicKey senderPublicKey = PublicKey.from("SenderPublicKey".getBytes());
    byte[] encryptedKey = "encryptedKey".getBytes();
    Nonce nonce = new Nonce("Nonce".getBytes());
    RawTransaction rawTransaction = new RawTransaction(message, encryptedKey, nonce, senderPublicKey);
    when(enclave.unencryptRawPayload(any(RawTransaction.class))).thenReturn("unencryptedRawTransaction".getBytes());
    byte[] result = enclaveClient.unencryptRawPayload(rawTransaction);
    assertThat(result).containsExactly("unencryptedRawTransaction".getBytes());
    verify(enclave).unencryptRawPayload(any(RawTransaction.class));
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 53 with Nonce

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

the class RestfulEnclaveClientTest method encryptPayloadRawWithPrivacyGroupId.

@Test
public void encryptPayloadRawWithPrivacyGroupId() {
    byte[] message = "HELLOW".getBytes();
    byte[] encryptedKey = "encryptedKey".getBytes();
    Nonce nonce = new Nonce("Nonce".getBytes());
    PublicKey senderPublicKey = PublicKey.from("SenderPublicKey".getBytes());
    List<PublicKey> recipientPublicKeys = Arrays.asList(PublicKey.from("RecipientPublicKey".getBytes()));
    RawTransaction rawTransaction = new RawTransaction(message, encryptedKey, nonce, senderPublicKey);
    EncodedPayload encodedPayloadWithGroupId = EncodedPayload.Builder.from(Fixtures.createSample()).withPrivacyGroupId(PrivacyGroup.Id.fromBytes("group".getBytes())).build();
    List<AffectedTransaction> affectedTransactions = List.of(AffectedTransaction.Builder.create().withHash("hash".getBytes()).withPayload(encodedPayloadWithGroupId).build());
    final PrivacyMetadata privacyMetaData = PrivacyMetadata.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withAffectedTransactions(affectedTransactions).withPrivacyGroupId(PrivacyGroup.Id.fromBytes("group".getBytes())).build();
    when(enclave.encryptPayload(any(RawTransaction.class), any(List.class), any())).thenReturn(encodedPayloadWithGroupId);
    EncodedPayload result = enclaveClient.encryptPayload(rawTransaction, recipientPublicKeys, privacyMetaData);
    assertThat(result).isNotNull();
    byte[] encodedResult = payloadEncoder.encode(result);
    byte[] encodedEncodedPayload = payloadEncoder.encode(encodedPayloadWithGroupId);
    assertThat(encodedResult).isEqualTo(encodedEncodedPayload);
    ArgumentCaptor<PrivacyMetadata> argumentCaptor = ArgumentCaptor.forClass(PrivacyMetadata.class);
    verify(enclave).encryptPayload(any(RawTransaction.class), any(List.class), argumentCaptor.capture());
    final PrivacyMetadata passingThroughPrivacyData = argumentCaptor.getValue();
    assertThat(passingThroughPrivacyData.getPrivacyGroupId()).isPresent().get().isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
    assertThat(passingThroughPrivacyData.getPrivacyMode()).isEqualTo(privacyMetaData.getPrivacyMode());
    assertThat(passingThroughPrivacyData.getAffectedContractTransactions()).isEqualTo(privacyMetaData.getAffectedContractTransactions());
    assertThat(passingThroughPrivacyData.getExecHash()).isEqualTo(privacyMetaData.getExecHash());
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 54 with Nonce

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

the class KaliumIT method randomKeyCanEncryptAndDecrpytPayload.

@Test
public void randomKeyCanEncryptAndDecrpytPayload() {
    final String payload = "Hello world";
    final byte[] payloadBytes = payload.getBytes(UTF_8);
    final Nonce nonce = kalium.randomNonce();
    final SharedKey symmentricKey = kalium.createSingleKey();
    final byte[] encryptedPayload = kalium.sealAfterPrecomputation(payloadBytes, nonce, symmentricKey);
    final byte[] decryptedPayload = kalium.openAfterPrecomputation(encryptedPayload, nonce, symmentricKey);
    final String decryptedMessage = new String(decryptedPayload, UTF_8);
    assertThat(decryptedMessage).isEqualTo(payload);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) SharedKey(com.quorum.tessera.encryption.SharedKey) Test(org.junit.Test)

Example 55 with Nonce

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

the class KaliumIT method encryptAndDecryptPayloadUsingSameKeys.

@Test
public void encryptAndDecryptPayloadUsingSameKeys() {
    final String payload = "Hello world";
    final SharedKey sharedKey = kalium.computeSharedKey(keypairOne.getPublicKey(), keypairTwo.getPrivateKey());
    final byte[] payloadBytes = payload.getBytes(UTF_8);
    final Nonce nonce = kalium.randomNonce();
    final byte[] encryptedPayload = kalium.sealAfterPrecomputation(payloadBytes, nonce, sharedKey);
    final byte[] decryptedPayload = kalium.openAfterPrecomputation(encryptedPayload, nonce, sharedKey);
    final String decryptedMessage = new String(decryptedPayload, UTF_8);
    assertThat(decryptedMessage).isEqualTo(payload);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) SharedKey(com.quorum.tessera.encryption.SharedKey) Test(org.junit.Test)

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