Search in sources :

Example 6 with KeysetGenerateResponse

use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.

the class TestingServicesTest method verifyMac_failsOnBadKeyset.

@Test
public void verifyMac_failsOnBadKeyset() 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();
    ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
    assertThat(compResponse.getErr()).isEmpty();
    byte[] macValue = compResponse.getMacValue().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    VerifyMacResponse verifyResponse = verifyMac(macStub, badKeyset, macValue, data);
    assertThat(verifyResponse.getErr()).isNotEmpty();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) VerifyMacResponse(com.google.crypto.tink.proto.testing.VerifyMacResponse) ComputeMacResponse(com.google.crypto.tink.proto.testing.ComputeMacResponse) Test(org.junit.Test)

Example 7 with KeysetGenerateResponse

use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.

the class TestingServicesTest method encryptDecryptInvalidKeyset_fails.

@Test
public void encryptDecryptInvalidKeyset_fails() throws Exception {
    byte[] invalidData = "invalid".getBytes(UTF_8);
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    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 writeResponse1 = keysetWriteEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse1.getErr()).isNotEmpty();
    KeysetWriteEncryptedResponse writeResponse2 = keysetWriteEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse2.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse1 = keysetReadEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse1.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse2 = keysetReadEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse2.getErr()).isNotEmpty();
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 8 with KeysetGenerateResponse

use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.

the class JwtServiceImplTest method jwtComputeVerifyMac_success.

@Test
public void jwtComputeVerifyMac_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("JWT_HS256"));
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    long expSecs = 1234 + 100;
    int expNanos = 567000000;
    JwtToken token = generateToken("audience", expSecs, expNanos);
    JwtSignRequest signRequest = JwtSignRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setRawJwt(token).build();
    JwtSignResponse signResponse = jwtStub.computeMacAndEncode(signRequest);
    assertThat(signResponse.getErr()).isEmpty();
    JwtValidator validator = JwtValidator.newBuilder().setExpectedTypeHeader(StringValue.newBuilder().setValue("typeHeader")).setExpectedIssuer(StringValue.newBuilder().setValue("issuer")).setExpectedAudience(StringValue.newBuilder().setValue("audience")).setNow(Timestamp.newBuilder().setSeconds(1234)).build();
    JwtVerifyRequest verifyRequest = JwtVerifyRequest.newBuilder().setKeyset(ByteString.copyFrom(keyset)).setSignedCompactJwt(signResponse.getSignedCompactJwt()).setValidator(validator).build();
    JwtToken expectedToken = generateToken("audience", expSecs, 0);
    JwtVerifyResponse verifyResponse = jwtStub.verifyMacAndDecode(verifyRequest);
    assertThat(verifyResponse.getErr()).isEmpty();
    assertThat(verifyResponse.getVerifiedJwt()).isEqualTo(expectedToken);
}
Also used : JwtToken(com.google.crypto.tink.proto.testing.JwtToken) JwtSignRequest(com.google.crypto.tink.proto.testing.JwtSignRequest) JwtValidator(com.google.crypto.tink.proto.testing.JwtValidator) JwtSignResponse(com.google.crypto.tink.proto.testing.JwtSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) JwtVerifyRequest(com.google.crypto.tink.proto.testing.JwtVerifyRequest) JwtVerifyResponse(com.google.crypto.tink.proto.testing.JwtVerifyResponse) Test(org.junit.Test)

Example 9 with KeysetGenerateResponse

use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.

the class TestingServicesTest method generateEncryptDecryptKeyset.

@Test
public void generateEncryptDecryptKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    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, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse.getErr()).isEmpty();
    byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
    assertThat(encryptedKeyset).isNotEqualTo(keyset);
    KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse.getErr()).isEmpty();
    byte[] output = readResponse.getKeyset().toByteArray();
    assertThat(output).isEqualTo(keyset);
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 10 with KeysetGenerateResponse

use of com.google.crypto.tink.proto.testing.KeysetGenerateResponse in project tink by google.

the class TestingServicesTest method aeadDecrypt_failsOnBadKeyset.

@Test
public void aeadDecrypt_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    AeadEncryptResponse encResponse = aeadEncrypt(aeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, badKeyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) AeadEncryptResponse(com.google.crypto.tink.proto.testing.AeadEncryptResponse) DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)34 Test (org.junit.Test)33 JwtSignRequest (com.google.crypto.tink.proto.testing.JwtSignRequest)7 JwtSignResponse (com.google.crypto.tink.proto.testing.JwtSignResponse)7 JwtToken (com.google.crypto.tink.proto.testing.JwtToken)7 JwtValidator (com.google.crypto.tink.proto.testing.JwtValidator)7 JwtVerifyRequest (com.google.crypto.tink.proto.testing.JwtVerifyRequest)7 JwtVerifyResponse (com.google.crypto.tink.proto.testing.JwtVerifyResponse)7 KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)7 DeterministicAeadDecryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse)6 StreamingAeadDecryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse)6 DeterministicAeadEncryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse)4 StreamingAeadEncryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse)4 AeadDecryptResponse (com.google.crypto.tink.proto.testing.AeadDecryptResponse)3 HybridDecryptResponse (com.google.crypto.tink.proto.testing.HybridDecryptResponse)3 KeysetReadEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse)3 KeysetWriteEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse)3 PrfSetComputeResponse (com.google.crypto.tink.proto.testing.PrfSetComputeResponse)3 SignatureVerifyResponse (com.google.crypto.tink.proto.testing.SignatureVerifyResponse)3 VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)3