Search in sources :

Example 6 with Nonce

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

the class BatchResendManagerImplTest method testStoreResendBatchMultipleVersions.

@Test
public void testStoreResendBatchMultipleVersions() {
    try (var payloadDigestMockedStatic = mockStatic(PayloadDigest.class);
        var payloadEncoderMockedStatic = mockStatic(PayloadEncoder.class)) {
        payloadDigestMockedStatic.when(PayloadDigest::create).thenReturn((PayloadDigest) cipherText -> cipherText);
        payloadEncoderMockedStatic.when(() -> PayloadEncoder.create(any())).thenReturn(payloadEncoder);
        final EncodedPayload encodedPayload = EncodedPayload.Builder.create().withSenderKey(publicKey).withCipherText("cipherText".getBytes()).withCipherTextNonce(new Nonce("nonce".getBytes())).withRecipientBoxes(singletonList("box".getBytes())).withRecipientNonce(new Nonce("recipientNonce".getBytes())).withRecipientKeys(singletonList(PublicKey.from("receiverKey".getBytes()))).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(emptyMap()).withExecHash(new byte[0]).build();
        when(payloadEncoder.decode(any())).thenReturn(encodedPayload);
        final byte[] raw = new PayloadEncoderImpl().encode(encodedPayload);
        PushBatchRequest request = PushBatchRequest.from(List.of(raw), EncodedPayloadCodec.LEGACY);
        StagingTransaction existing = new StagingTransaction();
        when(stagingEntityDAO.retrieveByHash(any())).thenReturn(Optional.of(existing));
        when(stagingEntityDAO.update(any(StagingTransaction.class))).thenReturn(new StagingTransaction());
        manager.storeResendBatch(request);
        verify(stagingEntityDAO).save(any(StagingTransaction.class));
        verify(payloadEncoder).decode(any());
        verify(payloadEncoder).encodedPayloadCodec();
        payloadDigestMockedStatic.verify(PayloadDigest::create);
        payloadDigestMockedStatic.verifyNoMoreInteractions();
    }
}
Also used : AdditionalMatchers.lt(org.mockito.AdditionalMatchers.lt) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Collections.singletonList(java.util.Collections.singletonList) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) After(org.junit.After) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) AdditionalMatchers.gt(org.mockito.AdditionalMatchers.gt) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) ServiceLoader(java.util.ServiceLoader) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Optional(java.util.Optional) Nonce(com.quorum.tessera.encryption.Nonce) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Test(org.junit.Test)

Example 7 with Nonce

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

the class EnclaveResource method encryptPayload.

@POST
@Path("encrypt/raw")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response encryptPayload(EnclaveRawPayload enclaveRawPayload) {
    byte[] encryptedPayload = enclaveRawPayload.getEncryptedPayload();
    byte[] encryptedKey = enclaveRawPayload.getEncryptedKey();
    Nonce nonce = new Nonce(enclaveRawPayload.getNonce());
    PublicKey from = PublicKey.from(enclaveRawPayload.getFrom());
    List<PublicKey> recipientPublicKeys = enclaveRawPayload.getRecipientPublicKeys().stream().map(PublicKey::from).collect(Collectors.toList());
    RawTransaction rawTransaction = new RawTransaction(encryptedPayload, encryptedKey, nonce, from);
    final List<AffectedTransaction> affectedTransactions = convertToAffectedTransactions(enclaveRawPayload.getAffectedContractTransactions());
    Set<PublicKey> mandatoryRecipients = enclaveRawPayload.getMandatoryRecipients().stream().map(PublicKey::from).collect(Collectors.toSet());
    final PrivacyMetadata.Builder privacyMetaDataBuilder = PrivacyMetadata.Builder.create().withPrivacyMode(enclaveRawPayload.getPrivacyMode()).withAffectedTransactions(affectedTransactions).withExecHash(enclaveRawPayload.getExecHash()).withMandatoryRecipients(mandatoryRecipients);
    Optional.ofNullable(enclaveRawPayload.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBytes).ifPresent(privacyMetaDataBuilder::withPrivacyGroupId);
    EncodedPayload outcome = enclave.encryptPayload(rawTransaction, recipientPublicKeys, privacyMetaDataBuilder.build());
    byte[] response = payloadEncoder.encode(outcome);
    final StreamingOutput streamingOutput = out -> out.write(response);
    return Response.ok(streamingOutput).build();
}
Also used : Service(com.quorum.tessera.service.Service) PublicKey(com.quorum.tessera.encryption.PublicKey) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) Set(java.util.Set) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) Json(jakarta.json.Json) Status(jakarta.ws.rs.core.Response.Status) Objects(java.util.Objects) Response(jakarta.ws.rs.core.Response) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Optional(java.util.Optional) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) PublicKey(com.quorum.tessera.encryption.PublicKey) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) Nonce(com.quorum.tessera.encryption.Nonce)

Example 8 with Nonce

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

the class RestfulEnclaveClient method encryptRawPayload.

@Override
public RawTransaction encryptRawPayload(byte[] message, PublicKey sender) {
    return ClientCallback.execute(() -> {
        EnclavePayload enclavePayload = new EnclavePayload();
        enclavePayload.setData(message);
        enclavePayload.setSenderKey(sender.getKeyBytes());
        Response response = client.target(uri).path("encrypt").path("toraw").request().post(Entity.json(enclavePayload));
        validateResponseIsOk(response);
        EnclaveRawPayload enclaveRawPayload = response.readEntity(EnclaveRawPayload.class);
        byte[] encryptedPayload = enclaveRawPayload.getEncryptedPayload();
        byte[] encryptedKey = enclaveRawPayload.getEncryptedKey();
        Nonce nonce = new Nonce(enclaveRawPayload.getNonce());
        PublicKey senderKey = PublicKey.from(enclaveRawPayload.getFrom());
        return new RawTransaction(encryptedPayload, encryptedKey, nonce, senderKey);
    });
}
Also used : Response(jakarta.ws.rs.core.Response) Nonce(com.quorum.tessera.encryption.Nonce) PublicKey(com.quorum.tessera.encryption.PublicKey)

Example 9 with Nonce

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

the class PayloadEncoderTest method encodePsvPayloadNoRecipientsWithAffectedTx.

@Test
public void encodePsvPayloadNoRecipientsWithAffectedTx() {
    final byte[] sender = new byte[] { 5, 66, -34, 71, -62, 114, 81, 104, 98, -70, -32, -116, 83, -15, -53, 3, 68, 57, -89, 57, 24, 79, -25, 7, 32, -115, -39, 40, 23, -78, -36, 26 };
    final byte[] cipherText = new byte[] { -46, -26, -18, 127, 37, -2, -84, -56, -71, 26, 3, 102, -61, 38, -1, 37, 105, 2, 10, 86, 6, 117, 69, 73, 91, 81, 68, 106, 23, 74, 12, 104, -63, 63, -119, 95, -16, -82, -34, 101, 89, 38, -19, 8, 23, -70, 90, 5, -7, -15, 23, -8, -88, 47, 72, 105, -103, -34, 10, 109, -48, 114, -127, -38, 41, 12, 3, 72, 113, -56, -90, -70, 124, -25, 127, 60, 100, 95, 127, 31, -72, -101, 26, -12, -9, 108, 54, 2, 124, 22, 55, 9, 123, 54, -16, 51, 28, -25, -102, -100, -23, 89, -15, 86, 22, -100, -63, -110, -2, -32, -1, 12, -116, 102, -43, 92, 2, 105, -78, -73, 111, -123, -59, -118, -32, 47, -63, 41, 72, -72, 35, -68, 45, 77, 110, -24, -113, -106, -31, -42, 13, -123, 54, 45, 83, -38, -57, 116, 107, -84, 22, -30, -49, 84, 39, 17, -20, -75, -122, -6, 73, -61, 70, -53, -65, -22, 13, 23, 43, -101, 23, 16, 31, -1, -19, -8, -94, -119, -28, -127, -101, 43, 31, -28, 16, -78, -86, 47, 42, 21, 115, 127, -81, 44, -33, -12, -74, -77, 111, 0, 121, 70, 67, 81, 74, 90, 116, -14, -75, 82, -110, -119, -23, 84, 74, 61, -31, -66, -71, -106, 60, 127, -113, -26, 73, -50, -112, -45, 82, 37, -68, -49, 40, -73, -53, 85, -71, 82, 32, 117, 25, -81, -13, -30, -48, -118, -82, 125, -63, 1, -46, -115, -104, 32, 2, -1, -124, -88, -20, -77, 108, 123, 41, 78, 108, -88, 65, 84, 66, -40, 79, -118, 63, -109, -85, -52, 8, -97, -49, 87, -27, -63, 75, -45, 51, 7, 116, -68, 16, 89, 53, 14, -121, 53, 38, -16, 122, -47, -110, -19, 72, 102, -81, 13, 13, -28, -103, 39, -26, 36, -15, -61, -91, -64, -99, 118, -34, -45, -119, 33, 57, 92, 119, 95, -17, 19, 50, 46, -119, 88, -123, -49, -68, -105, 74, -15, 102, 74, -19, 29, 75, -114, -34, -54, -6, 111, 122, 2, 55, 99, 58, -31, 123, 50, -84, -128, 71, 79, 19, -40, 92, 7, 75, -31, -113, -60, -8, 121, 105, 91, -127, 69, 106, -49, -13, -91, -34 };
    final byte[] nonce = new byte[] { -114, -128, 47, 49, 6, -71, -111, -76, -100, -16, 113, -126, 3, 107, 55, 1, 43, -6, -43, -104, -128, -125, -37, 31 };
    final byte[] recipientBox = new byte[] { -111, -41, -32, 59, -89, -69, -51, -27, 64, 74, -89, -63, -97, 54, 12, -10, -104, 111, -100, -98, 4, 34, 67, 73, -57, -46, 15, 100, -21, -42, -14, -43, 72, 64, -127, -44, 113, -10, 82, 105, -81, 122, 61, -50, 28, 108, -56, -92 };
    final byte[] recipientNonce = new byte[] { -110, 45, 44, -76, 17, 23, -76, 0, -75, 112, 70, 97, 108, -70, -76, 32, 100, -46, -67, 107, -89, 98, 64, -85 };
    final EncodedPayload originalPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(sender)).withCipherText(cipherText).withCipherTextNonce(new Nonce(nonce)).withRecipientBoxes(singletonList(recipientBox)).withRecipientNonce(new Nonce(recipientNonce)).withRecipientKeys(emptyList()).withPrivacyMode(PrivacyMode.PRIVATE_STATE_VALIDATION).withAffectedContractTransactions(singletonMap(new TxHash("test".getBytes()), "test".getBytes())).withExecHash("execHash".getBytes()).build();
    final byte[] encodedResult = payloadEncoder.encode(originalPayload);
    final EncodedPayload decodedPayload = payloadEncoder.decode(encodedResult);
    assertThat(decodedPayload.getSenderKey()).isEqualTo(originalPayload.getSenderKey());
    assertThat(decodedPayload.getCipherText()).isEqualTo(originalPayload.getCipherText());
    assertThat(decodedPayload.getCipherTextNonce()).isEqualTo(originalPayload.getCipherTextNonce());
    assertThat(decodedPayload.getRecipientBoxes().size()).isEqualTo(originalPayload.getRecipientBoxes().size());
    assertThat(decodedPayload.getRecipientBoxes().get(0)).isEqualTo(originalPayload.getRecipientBoxes().get(0));
    assertThat(decodedPayload.getRecipientKeys()).isEqualTo(originalPayload.getRecipientKeys());
    assertThat(decodedPayload.getPrivacyMode()).isEqualTo(originalPayload.getPrivacyMode());
    assertThat(decodedPayload.getAffectedContractTransactions().get(new TxHash("test".getBytes())).getData()).isEqualTo("test".getBytes());
    assertThat(decodedPayload.getExecHash()).isEqualTo(originalPayload.getExecHash());
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) LegacyEncodedPayload(com.quorum.tessera.enclave.encoder.LegacyEncodedPayload) Test(org.junit.Test)

Example 10 with Nonce

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

the class PayloadEncoderTest method decodePayloadFromLegacyEncoderNoRecipient.

@Test
public void decodePayloadFromLegacyEncoderNoRecipient() {
    final byte[] sender = new byte[] { 5, 66, -34, 71, -62, 114, 81, 104, 98, -70, -32, -116, 83, -15, -53, 3, 68, 57, -89, 57, 24, 79, -25, 7, 32, -115, -39, 40, 23, -78, -36, 26 };
    final byte[] cipherText = new byte[] { -46, -26, -18, 127, 37, -2, -84, -56, -71, 26, 3, 102, -61, 38, -1, 37, 105, 2, 10, 86, 6, 117, 69, 73, 91, 81, 68, 106, 23, 74, 12, 104, -63, 63, -119, 95, -16, -82, -34, 101, 89, 38, -19, 8, 23, -70, 90, 5, -7, -15, 23, -8, -88, 47, 72, 105, -103, -34, 10, 109, -48, 114, -127, -38, 41, 12, 3, 72, 113, -56, -90, -70, 124, -25, 127, 60, 100, 95, 127, 31, -72, -101, 26, -12, -9, 108, 54, 2, 124, 22, 55, 9, 123, 54, -16, 51, 28, -25, -102, -100, -23, 89, -15, 86, 22, -100, -63, -110, -2, -32, -1, 12, -116, 102, -43, 92, 2, 105, -78, -73, 111, -123, -59, -118, -32, 47, -63, 41, 72, -72, 35, -68, 45, 77, 110, -24, -113, -106, -31, -42, 13, -123, 54, 45, 83, -38, -57, 116, 107, -84, 22, -30, -49, 84, 39, 17, -20, -75, -122, -6, 73, -61, 70, -53, -65, -22, 13, 23, 43, -101, 23, 16, 31, -1, -19, -8, -94, -119, -28, -127, -101, 43, 31, -28, 16, -78, -86, 47, 42, 21, 115, 127, -81, 44, -33, -12, -74, -77, 111, 0, 121, 70, 67, 81, 74, 90, 116, -14, -75, 82, -110, -119, -23, 84, 74, 61, -31, -66, -71, -106, 60, 127, -113, -26, 73, -50, -112, -45, 82, 37, -68, -49, 40, -73, -53, 85, -71, 82, 32, 117, 25, -81, -13, -30, -48, -118, -82, 125, -63, 1, -46, -115, -104, 32, 2, -1, -124, -88, -20, -77, 108, 123, 41, 78, 108, -88, 65, 84, 66, -40, 79, -118, 63, -109, -85, -52, 8, -97, -49, 87, -27, -63, 75, -45, 51, 7, 116, -68, 16, 89, 53, 14, -121, 53, 38, -16, 122, -47, -110, -19, 72, 102, -81, 13, 13, -28, -103, 39, -26, 36, -15, -61, -91, -64, -99, 118, -34, -45, -119, 33, 57, 92, 119, 95, -17, 19, 50, 46, -119, 88, -123, -49, -68, -105, 74, -15, 102, 74, -19, 29, 75, -114, -34, -54, -6, 111, 122, 2, 55, 99, 58, -31, 123, 50, -84, -128, 71, 79, 19, -40, 92, 7, 75, -31, -113, -60, -8, 121, 105, 91, -127, 69, 106, -49, -13, -91, -34 };
    final byte[] nonce = new byte[] { -114, -128, 47, 49, 6, -71, -111, -76, -100, -16, 113, -126, 3, 107, 55, 1, 43, -6, -43, -104, -128, -125, -37, 31 };
    final byte[] recipientBox = new byte[] { -111, -41, -32, 59, -89, -69, -51, -27, 64, 74, -89, -63, -97, 54, 12, -10, -104, 111, -100, -98, 4, 34, 67, 73, -57, -46, 15, 100, -21, -42, -14, -43, 72, 64, -127, -44, 113, -10, 82, 105, -81, 122, 61, -50, 28, 108, -56, -92 };
    final byte[] recipientNonce = new byte[] { -110, 45, 44, -76, 17, 23, -76, 0, -75, 112, 70, 97, 108, -70, -76, 32, 100, -46, -67, 107, -89, 98, 64, -85 };
    LegacyEncodedPayload legacyPayload = new LegacyEncodedPayload(PublicKey.from(sender), cipherText, new Nonce(nonce), singletonList(recipientBox), new Nonce(recipientNonce), emptyList());
    final byte[] encoded = legacyPayloadEncoder.encode(legacyPayload);
    EncodedPayload newPayload = payloadEncoder.decode(encoded);
    assertThat(newPayload.getSenderKey()).isEqualTo(legacyPayload.getSenderKey());
    assertThat(newPayload.getCipherText()).isEqualTo(legacyPayload.getCipherText());
    assertThat(newPayload.getCipherTextNonce()).isEqualTo(legacyPayload.getCipherTextNonce());
    assertThat(newPayload.getRecipientBoxes().get(0).getData()).isEqualTo(legacyPayload.getRecipientBoxes().get(0));
    assertThat(newPayload.getRecipientNonce()).isEqualTo(legacyPayload.getRecipientNonce());
    assertThat(newPayload.getRecipientKeys()).isEmpty();
    assertThat(newPayload.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(newPayload.getAffectedContractTransactions()).isEmpty();
    assertThat(newPayload.getExecHash()).isNullOrEmpty();
}
Also used : Nonce(com.quorum.tessera.encryption.Nonce) LegacyEncodedPayload(com.quorum.tessera.enclave.encoder.LegacyEncodedPayload) LegacyEncodedPayload(com.quorum.tessera.enclave.encoder.LegacyEncodedPayload) 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