use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.
the class KaliumIT method encryptDecrpytWithoutPrecomputation.
@Test
public void encryptDecrpytWithoutPrecomputation() {
final String payload = "Hello world";
final byte[] payloadBytes = payload.getBytes(UTF_8);
final Nonce nonce = kalium.randomNonce();
final byte[] encryptedPayload = kalium.seal(payloadBytes, nonce, keypairOne.getPublicKey(), keypairTwo.getPrivateKey());
final byte[] decryptedPayload = kalium.open(encryptedPayload, nonce, keypairTwo.getPublicKey(), keypairOne.getPrivateKey());
final String decryptedMessage = new String(decryptedPayload, UTF_8);
assertThat(decryptedMessage).isEqualTo(payload);
}
use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.
the class EncoderCompatibilityTest method v3ToLegacy.
@Test
public void v3ToLegacy() {
final V3EncodedPayload payload = V3EncodedPayload.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 = v3Encoder.encode(payload);
final LegacyEncodedPayload legacy = legacyEncoder.decode(encoded);
final List<RecipientBox> boxes = legacy.getRecipientBoxes().stream().map(RecipientBox::from).collect(Collectors.toList());
assertThat(legacy.getSenderKey()).isEqualTo(payload.getSenderKey());
assertThat(legacy.getCipherText()).isEqualTo(payload.getCipherText());
assertThat(legacy.getCipherTextNonce()).isEqualTo(payload.getCipherTextNonce());
assertThat(boxes).isEqualTo(payload.getRecipientBoxes());
assertThat(legacy.getRecipientNonce()).isEqualTo(payload.getRecipientNonce());
assertThat(legacy.getRecipientKeys()).isEqualTo(payload.getRecipientKeys());
}
use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.
the class EncoderCompatibilityTest method v3ToV4.
@Test
public void v3ToV4() {
// V3 payload can contain privacyGroupId - but not mandatoryRecipients
final V3EncodedPayload v3Payload = V3EncodedPayload.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.PRIVATE_STATE_VALIDATION).withAffectedContractTransactions(Map.of(TxHash.from("hash".getBytes()), "hash".getBytes())).withExecHash("execHash".getBytes()).withPrivacyGroupId(PrivacyGroup.Id.fromBytes("group".getBytes())).build();
final byte[] encoded = v3Encoder.encode(v3Payload);
final EncodedPayload encodedPayload = v4Encoder.decode(encoded);
assertThat(encodedPayload.getSenderKey()).isEqualTo(v3Payload.getSenderKey());
assertThat(encodedPayload.getCipherText()).isEqualTo(v3Payload.getCipherText());
assertThat(encodedPayload.getCipherTextNonce()).isEqualTo(v3Payload.getCipherTextNonce());
assertThat(encodedPayload.getRecipientBoxes()).isEqualTo(v3Payload.getRecipientBoxes());
assertThat(encodedPayload.getRecipientNonce()).isEqualTo(v3Payload.getRecipientNonce());
assertThat(encodedPayload.getRecipientKeys()).isEqualTo(v3Payload.getRecipientKeys());
// Enhanced privacy values
assertThat(encodedPayload.getPrivacyMode()).isEqualTo(v3Payload.getPrivacyMode());
assertThat(encodedPayload.getAffectedContractTransactions()).isEqualTo(v3Payload.getAffectedContractTransactions());
assertThat(encodedPayload.getExecHash()).isEqualTo(v3Payload.getExecHash());
assertThat(encodedPayload.getPrivacyGroupId().get().getBytes()).isEqualTo("group".getBytes());
assertThat(encodedPayload.getMandatoryRecipients()).isEmpty();
}
use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.
the class RestfulEnclaveClientTest method encryptPayloadRaw.
@Test
public void encryptPayloadRaw() {
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 encodedPayload = Fixtures.createSample();
List<AffectedTransaction> affectedTransactions = List.of(AffectedTransaction.Builder.create().withHash("hash".getBytes()).withPayload(encodedPayload).build());
final PrivacyMetadata privacyMetaData = PrivacyMetadata.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withAffectedTransactions(affectedTransactions).build();
when(enclave.encryptPayload(any(RawTransaction.class), any(List.class), any())).thenReturn(encodedPayload);
EncodedPayload result = enclaveClient.encryptPayload(rawTransaction, recipientPublicKeys, privacyMetaData);
assertThat(result).isNotNull();
byte[] encodedResult = payloadEncoder.encode(result);
byte[] encodedEncodedPayload = payloadEncoder.encode(encodedPayload);
assertThat(encodedResult).isEqualTo(encodedEncodedPayload);
verify(enclave).encryptPayload(any(RawTransaction.class), any(List.class), any());
}
use of com.quorum.tessera.encryption.Nonce in project tessera by ConsenSys.
the class PayloadEncoderTest method encodeStandardPrivatePayloadNoRecipient.
@Test
public void encodeStandardPrivatePayloadNoRecipient() {
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.STANDARD_PRIVATE).withAffectedContractTransactions(emptyMap()).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()).isEqualTo(originalPayload.getAffectedContractTransactions());
assertThat(decodedPayload.getExecHash()).isNullOrEmpty();
}
Aggregations