Search in sources :

Example 66 with Nonce

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

the class EllipticalCurveEncryptorTest method sealOpenAfterPrecomputation.

@Test
public void sealOpenAfterPrecomputation() {
    MasterKey masterKey = encryptor.createMasterKey();
    byte[] clearText = "MessageToEncrypt123".getBytes();
    Nonce nonce = encryptor.randomNonce();
    byte[] cipherText = encryptor.sealAfterPrecomputation(clearText, nonce, masterKey);
    LOGGER.info("Encrypted outpout: {}", Base64.getEncoder().encode(cipherText));
    byte[] decryptedText = encryptor.openAfterPrecomputation(cipherText, nonce, masterKey);
    assertThat(decryptedText).containsExactly(clearText);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) MasterKey(com.quorum.tessera.encryption.MasterKey) Test(org.junit.Test)

Example 67 with Nonce

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

the class EllipticalCurveEncryptorTest method openAfterPrecomputationInvalidSymmetricCipher.

@Test(expected = EncryptorException.class)
public void openAfterPrecomputationInvalidSymmetricCipher() {
    EllipticalCurveEncryptor facade = new EllipticalCurveEncryptor("garbage", "secp256r1");
    MasterKey masterKey = encryptor.createMasterKey();
    Nonce nonce = encryptor.randomNonce();
    facade.openAfterPrecomputation("test".getBytes(), nonce, masterKey);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) MasterKey(com.quorum.tessera.encryption.MasterKey) Test(org.junit.Test)

Example 68 with Nonce

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

the class EllipticalCurveEncryptorTest method randomNonce.

@Test
public void randomNonce() {
    Nonce nonce = encryptor.randomNonce();
    assertThat(nonce).isNotNull();
    assertThat(nonce.getNonceBytes()).hasSize(24);
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 69 with Nonce

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

the class TransactionManagerTest method storePayloadWithInvalidSecurityHashesIgnoreIfNotPsv.

@Test
public void storePayloadWithInvalidSecurityHashesIgnoreIfNotPsv() {
    Map<TxHash, SecurityHash> affectedTx = Map.of(TxHash.from("invalidHash".getBytes()), SecurityHash.from("security".getBytes()));
    final EncodedPayload payload = mock(EncodedPayload.class);
    when(payload.getSenderKey()).thenReturn(PublicKey.from("sender".getBytes()));
    when(payload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
    when(payload.getCipherTextNonce()).thenReturn(new Nonce("nonce".getBytes()));
    when(payload.getRecipientBoxes()).thenReturn(List.of(RecipientBox.from("box1".getBytes())));
    when(payload.getRecipientNonce()).thenReturn(new Nonce("recipientNonce".getBytes()));
    when(payload.getRecipientKeys()).thenReturn(singletonList(PublicKey.from("recipient".getBytes())));
    when(payload.getPrivacyMode()).thenReturn(PrivacyMode.PARTY_PROTECTION);
    when(payload.getAffectedContractTransactions()).thenReturn(affectedTx);
    when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    ArgumentCaptor<EncryptedTransaction> txCaptor = ArgumentCaptor.forClass(EncryptedTransaction.class);
    when(enclave.findInvalidSecurityHashes(any(), any())).thenReturn(singleton(new TxHash("invalidHash".getBytes())));
    transactionManager.storePayload(payload);
    verify(encryptedTransactionDAO).save(txCaptor.capture());
    EncodedPayload sanitisedPayload = txCaptor.getValue().getPayload();
    // Assert that the invalid ACOTH had been removed
    assertThat(sanitisedPayload.getAffectedContractTransactions().get(TxHash.from("invalidHash".getBytes()))).isNull();
    verify(encryptedTransactionDAO).findByHashes(any());
    verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave).getPublicKeys();
    verify(enclave).findInvalidSecurityHashes(any(), any());
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) Test(org.junit.Test)

Example 70 with Nonce

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

the class TransactionManagerTest method storeRaw.

@Test
public void storeRaw() {
    PublicKey sender = PublicKey.from("SENDER".getBytes());
    RawTransaction rawTransaction = mock(RawTransaction.class);
    when(rawTransaction.getEncryptedPayload()).thenReturn("CIPHERTEXT".getBytes());
    when(rawTransaction.getEncryptedKey()).thenReturn("SomeKey".getBytes());
    when(rawTransaction.getNonce()).thenReturn(new Nonce("nonce".getBytes()));
    when(rawTransaction.getFrom()).thenReturn(sender);
    when(enclave.encryptRawPayload(any(), any())).thenReturn(rawTransaction);
    byte[] payload = Base64.getEncoder().encode("PAYLOAD".getBytes());
    StoreRawRequest sendRequest = mock(StoreRawRequest.class);
    when(sendRequest.getSender()).thenReturn(sender);
    when(sendRequest.getPayload()).thenReturn(payload);
    MessageHash expectedHash = new MessageHash(mockDigest.digest("CIPHERTEXT".getBytes()));
    StoreRawResponse result = transactionManager.store(sendRequest);
    assertThat(result).isNotNull();
    assertThat(result.getHash().getHashBytes()).containsExactly(expectedHash.getHashBytes());
    verify(enclave).encryptRawPayload(eq(payload), eq(sender));
    verify(encryptedRawTransactionDAO).save(argThat(et -> {
        assertThat(et.getEncryptedKey()).containsExactly("SomeKey".getBytes());
        assertThat(et.getEncryptedPayload()).containsExactly("CIPHERTEXT".getBytes());
        assertThat(et.getHash()).isEqualTo(expectedHash);
        assertThat(et.getNonce()).containsExactly("nonce".getBytes());
        assertThat(et.getSender()).containsExactly(sender.getKeyBytes());
        return true;
    }));
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) com.quorum.tessera.transaction(com.quorum.tessera.transaction) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) BatchPayloadPublisher(com.quorum.tessera.transaction.publish.BatchPayloadPublisher) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) MandatoryRecipientsNotAvailableException(com.quorum.tessera.transaction.exception.MandatoryRecipientsNotAvailableException) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Mockito(org.mockito.Mockito) EncryptorException(com.quorum.tessera.encryption.EncryptorException) ArgumentCaptor(org.mockito.ArgumentCaptor) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) After(org.junit.After) ResendManager(com.quorum.tessera.transaction.resend.ResendManager) Assertions(org.assertj.core.api.Assertions) com.quorum.tessera.data(com.quorum.tessera.data) Collections(java.util.Collections) Before(org.junit.Before) PublicKey(com.quorum.tessera.encryption.PublicKey) 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