use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method computePrf_success.
@Test
public void computePrf_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacPrfKeyManager.hmacSha256Template());
byte[] inputData = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
int outputLength = 15;
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
PrfSetKeyIdsResponse keyIdsResponse = keyIds(prfSetStub, keyset);
assertThat(keyIdsResponse.getErr()).isEmpty();
int primaryKeyId = keyIdsResponse.getOutput().getPrimaryKeyId();
PrfSetComputeResponse computeResponse = computePrf(prfSetStub, keyset, primaryKeyId, inputData, outputLength);
assertThat(computeResponse.getErr()).isEmpty();
assertThat(computeResponse.getOutput().size()).isEqualTo(outputLength);
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method computePrf_failsOnBadOutputLength.
@Test
public void computePrf_failsOnBadOutputLength() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacPrfKeyManager.hmacSha256Template());
byte[] inputData = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
int outputLength = 12345;
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
PrfSetKeyIdsResponse keyIdsResponse = keyIds(prfSetStub, keyset);
assertThat(keyIdsResponse.getErr()).isEmpty();
int primaryKeyId = keyIdsResponse.getOutput().getPrimaryKeyId();
PrfSetComputeResponse computeResponse = computePrf(prfSetStub, keyset, primaryKeyId, inputData, outputLength);
assertThat(computeResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method aeadDecrypt_failsOnBadCiphertext.
@Test
public void aeadDecrypt_failsOnBadCiphertext() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
byte[] badCiphertext = "bad ciphertext".getBytes(UTF_8);
byte[] associatedData = "aead_decrypt_fails_on_bad_ciphertext".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, keyset, badCiphertext, associatedData);
assertThat(decResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method verifyMac_failsOnBadMacValue.
@Test
public void verifyMac_failsOnBadMacValue() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
VerifyMacResponse verifyResponse = verifyMac(macStub, keyset, "bad mac_value".getBytes(UTF_8), data);
assertThat(verifyResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.
the class TestingServicesTest method generateEncryptDecryptKeysetWithAssociatedData.
@Test
public void generateEncryptDecryptKeysetWithAssociatedData() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
byte[] associatedData = "a".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
assertThat(masterKeysetResponse.getErr()).isEmpty();
byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
KeysetWriteEncryptedResponse writeResponse = keysetWriteEncrypted(keysetStub, keyset, masterKeyset, Optional.of(associatedData));
assertThat(writeResponse.getErr()).isEmpty();
byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
assertThat(encryptedKeyset).isNotEqualTo(keyset);
KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, Optional.of(associatedData));
assertThat(readResponse.getErr()).isEmpty();
byte[] output = readResponse.getKeyset().toByteArray();
assertThat(output).isEqualTo(keyset);
}
Aggregations