Search in sources :

Example 11 with PublicKeySign

use of com.google.crypto.tink.PublicKeySign in project tink by google.

the class RsaSsaPssVerifyKeyManagerTest method createPrimitive.

@Test
public void createPrimitive() throws Exception {
    if (TestUtil.isTsan()) {
        // factory.createKey is too slow in Tsan.
        return;
    }
    RsaSsaPssKeyFormat keyFormat = RsaSsaPssKeyFormat.newBuilder().setParams(RsaSsaPssParams.newBuilder().setSigHash(HashType.SHA256).setMgf1Hash(HashType.SHA256).setSaltLength(32)).setModulusSizeInBits(3072).setPublicExponent(ByteString.copyFrom(RSAKeyGenParameterSpec.F4.toByteArray())).build();
    RsaSsaPssPrivateKey privateKey = factory.createKey(keyFormat);
    RsaSsaPssPublicKey publicKey = signManager.getPublicKey(privateKey);
    PublicKeySign signer = signManager.getPrimitive(privateKey, PublicKeySign.class);
    PublicKeyVerify verifier = verifyManager.getPrimitive(publicKey, PublicKeyVerify.class);
    byte[] message = Random.randBytes(135);
    verifier.verify(signer.sign(message), message);
}
Also used : RsaSsaPssPublicKey(com.google.crypto.tink.proto.RsaSsaPssPublicKey) RsaSsaPssPrivateKey(com.google.crypto.tink.proto.RsaSsaPssPrivateKey) RsaSsaPssKeyFormat(com.google.crypto.tink.proto.RsaSsaPssKeyFormat) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 12 with PublicKeySign

use of com.google.crypto.tink.PublicKeySign in project tink by google.

the class Ed25519PublicKeyManagerTest method createPrimitive.

@Test
public void createPrimitive() throws Exception {
    Ed25519PrivateKey privateKey = createPrivateKey();
    Ed25519PublicKey publicKey = signManager.getPublicKey(privateKey);
    PublicKeySign signer = signManager.getPrimitive(privateKey, PublicKeySign.class);
    PublicKeyVerify verifier = verifyManager.getPrimitive(publicKey, PublicKeyVerify.class);
    byte[] message = Random.randBytes(135);
    verifier.verify(signer.sign(message), message);
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 13 with PublicKeySign

use of com.google.crypto.tink.PublicKeySign in project tink by google.

the class Ed25519PrivateKeyManagerTest method createPrimitive.

@Test
public void createPrimitive() throws Exception {
    Ed25519PrivateKey privateKey = factory.createKey(Ed25519KeyFormat.getDefaultInstance());
    PublicKeySign signer = manager.getPrimitive(privateKey, PublicKeySign.class);
    PublicKeyVerify verifier = new Ed25519Verify(privateKey.getPublicKey().getKeyValue().toByteArray());
    byte[] message = Random.randBytes(135);
    verifier.verify(signer.sign(message), message);
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 14 with PublicKeySign

use of com.google.crypto.tink.PublicKeySign 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 15 with PublicKeySign

use of com.google.crypto.tink.PublicKeySign in project tink by google.

the class PublicKeySignCli method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.out.println("Usage: PublicKeySignCli keyset-file message-file output-file");
        System.exit(1);
    }
    String keysetFilename = args[0];
    String messageFilename = args[1];
    String outputFilename = args[2];
    System.out.println("Using keyset from file " + keysetFilename + " to sign message from " + messageFilename + ".");
    System.out.println("The resulting signature will be written to file " + outputFilename);
    // Init Tink.
    CliUtil.initTink();
    // Read the keyset.
    System.out.println("Reading the keyset...");
    KeysetHandle keysetHandle = CliUtil.readKeyset(keysetFilename);
    // Get the primitive.
    System.out.println("Getting the primitive...");
    PublicKeySign pkSign = PublicKeySignFactory.getPrimitive(keysetHandle);
    // Read the message.
    byte[] message = CliUtil.read(messageFilename);
    // Compute the signature.
    System.out.println("Signing...");
    byte[] signature = pkSign.sign(message);
    // Write the signature to the output file.
    CliUtil.write(signature, outputFilename);
    System.out.println("All done.");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) PublicKeySign(com.google.crypto.tink.PublicKeySign)

Aggregations

PublicKeySign (com.google.crypto.tink.PublicKeySign)28 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)23 Test (org.junit.Test)21 GeneralSecurityException (java.security.GeneralSecurityException)14 KeysetHandle (com.google.crypto.tink.KeysetHandle)10 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)9 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)7 Key (com.google.crypto.tink.proto.Keyset.Key)6 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)3 RsaSsaPkcs1KeyFormat (com.google.crypto.tink.proto.RsaSsaPkcs1KeyFormat)3 RsaSsaPkcs1PrivateKey (com.google.crypto.tink.proto.RsaSsaPkcs1PrivateKey)3 RsaSsaPssKeyFormat (com.google.crypto.tink.proto.RsaSsaPssKeyFormat)3 RsaSsaPssPrivateKey (com.google.crypto.tink.proto.RsaSsaPssPrivateKey)3 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)3 Ed25519PublicKey (com.google.crypto.tink.proto.Ed25519PublicKey)2 KeyData (com.google.crypto.tink.proto.KeyData)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 RsaSsaPkcs1PublicKey (com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey)2 RsaSsaPssPublicKey (com.google.crypto.tink.proto.RsaSsaPssPublicKey)2 MessageLite (com.google.protobuf.MessageLite)2