Search in sources :

Example 1 with SignatureVerifyResponse

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

the class SignatureServiceImpl method verify.

/**
 * Verifies a signature.
 */
@Override
public void verify(SignatureVerifyRequest request, StreamObserver<SignatureVerifyResponse> responseObserver) {
    SignatureVerifyResponse response;
    try {
        KeysetHandle publicKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getPublicKeyset().toByteArray()));
        PublicKeyVerify verifier = publicKeysetHandle.getPrimitive(PublicKeyVerify.class);
        verifier.verify(request.getSignature().toByteArray(), request.getData().toByteArray());
        response = SignatureVerifyResponse.getDefaultInstance();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = SignatureVerifyResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) SignatureVerifyResponse(com.google.crypto.tink.proto.testing.SignatureVerifyResponse)

Example 2 with SignatureVerifyResponse

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

the class AsymmetricTestingServicesTest method signatureVerify_failsOnBadSignature.

@Test
public void signatureVerify_failsOnBadSignature() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(EcdsaSignKeyManager.ecdsaP256Template());
    byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    KeysetGenerateResponse genResponse = generateKeyset(keysetStub, template);
    assertThat(genResponse.getErr()).isEmpty();
    byte[] privateKeyset = genResponse.getKeyset().toByteArray();
    KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
    assertThat(pubResponse.getErr()).isEmpty();
    byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
    SignatureVerifyResponse verifyResponse = signatureVerify(signatureStub, publicKeyset, "bad signature".getBytes(UTF_8), data);
    assertThat(verifyResponse.getErr()).isNotEmpty();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) SignatureVerifyResponse(com.google.crypto.tink.proto.testing.SignatureVerifyResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 3 with SignatureVerifyResponse

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

the class AsymmetricTestingServicesTest method signatureVerify_failsOnBadKeyset.

@Test
public void signatureVerify_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(EcdsaSignKeyManager.ecdsaP256Template());
    byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    KeysetGenerateResponse genResponse = generateKeyset(keysetStub, template);
    assertThat(genResponse.getErr()).isEmpty();
    byte[] privateKeyset = genResponse.getKeyset().toByteArray();
    SignatureSignResponse signResponse = signatureSign(signatureStub, privateKeyset, data);
    assertThat(signResponse.getErr()).isEmpty();
    byte[] signature = signResponse.getSignature().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    SignatureVerifyResponse verifyResponse = signatureVerify(signatureStub, badKeyset, signature, data);
    assertThat(verifyResponse.getErr()).isNotEmpty();
}
Also used : SignatureSignResponse(com.google.crypto.tink.proto.testing.SignatureSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) SignatureVerifyResponse(com.google.crypto.tink.proto.testing.SignatureVerifyResponse) Test(org.junit.Test)

Example 4 with SignatureVerifyResponse

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

the class AsymmetricTestingServicesTest method signatureSignVerify_success.

@Test
public void signatureSignVerify_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(EcdsaSignKeyManager.ecdsaP256Template());
    byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    KeysetGenerateResponse genResponse = generateKeyset(keysetStub, template);
    assertThat(genResponse.getErr()).isEmpty();
    byte[] privateKeyset = genResponse.getKeyset().toByteArray();
    KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
    assertThat(pubResponse.getErr()).isEmpty();
    byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
    SignatureSignResponse signResponse = signatureSign(signatureStub, privateKeyset, data);
    assertThat(signResponse.getErr()).isEmpty();
    byte[] signature = signResponse.getSignature().toByteArray();
    SignatureVerifyResponse verifyResponse = signatureVerify(signatureStub, publicKeyset, signature, data);
    assertThat(verifyResponse.getErr()).isEmpty();
}
Also used : SignatureSignResponse(com.google.crypto.tink.proto.testing.SignatureSignResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) SignatureVerifyResponse(com.google.crypto.tink.proto.testing.SignatureVerifyResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Aggregations

SignatureVerifyResponse (com.google.crypto.tink.proto.testing.SignatureVerifyResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 Test (org.junit.Test)3 KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)2 SignatureSignResponse (com.google.crypto.tink.proto.testing.SignatureSignResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1