Search in sources :

Example 1 with SignatureSignResponse

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

the class SignatureServiceImpl method sign.

/**
 * Signs a message.
 */
@Override
public void sign(SignatureSignRequest request, StreamObserver<SignatureSignResponse> responseObserver) {
    SignatureSignResponse response;
    try {
        KeysetHandle privateKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getPrivateKeyset().toByteArray()));
        PublicKeySign signer = privateKeysetHandle.getPrimitive(PublicKeySign.class);
        byte[] signatureValue = signer.sign(request.getData().toByteArray());
        response = SignatureSignResponse.newBuilder().setSignature(ByteString.copyFrom(signatureValue)).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = SignatureSignResponse.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) SignatureSignResponse(com.google.crypto.tink.proto.testing.SignatureSignResponse) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) PublicKeySign(com.google.crypto.tink.PublicKeySign)

Example 2 with SignatureSignResponse

use of com.google.crypto.tink.proto.testing.SignatureSignResponse 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 3 with SignatureSignResponse

use of com.google.crypto.tink.proto.testing.SignatureSignResponse 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)

Example 4 with SignatureSignResponse

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

the class AsymmetricTestingServicesTest method signatureSign_failsOnBadKeyset.

@Test
public void signatureSign_failsOnBadKeyset() throws Exception {
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    SignatureSignResponse response = signatureSign(signatureStub, badKeyset, data);
    assertThat(response.getErr()).isNotEmpty();
}
Also used : SignatureSignResponse(com.google.crypto.tink.proto.testing.SignatureSignResponse) Test(org.junit.Test)

Aggregations

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