Search in sources :

Example 1 with PublicKeyVerify

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

the class PublicKeyVerifyCli method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.out.println("Usage: PublicKeyVerifyCli keyset-file signature-file message-file output-file");
        System.exit(1);
    }
    String keysetFilename = args[0];
    String signatureFilename = args[1];
    String messageFilename = args[2];
    String outputFilename = args[3];
    System.out.println("Using keyset from file " + keysetFilename + " to verify signature from file " + signatureFilename + " of the message from file " + messageFilename + ".");
    System.out.println("The verification result 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...");
    PublicKeyVerify pkVerify = PublicKeyVerifyFactory.getPrimitive(keysetHandle);
    // Read the signature.
    byte[] signature = CliUtil.read(signatureFilename);
    // Read the message.
    byte[] message = CliUtil.read(messageFilename);
    // Verify the signature.
    System.out.println("Verifying...");
    String verificationResult;
    try {
        pkVerify.verify(signature, message);
        verificationResult = "valid";
    } catch (GeneralSecurityException e) {
        System.out.println("Verification failed: " + e);
        verificationResult = "invalid";
    }
    // Write the verification result to the output file.
    CliUtil.write(verificationResult.getBytes(CliUtil.UTF_8), outputFilename);
    System.out.println("All done.");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify)

Example 2 with PublicKeyVerify

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

the class EcdsaVerifyKeyManagerTest method testRfcTestVectors.

@Test
public void testRfcTestVectors() throws Exception {
    for (int i = 0; i < rfcTestVectors.length; i++) {
        RfcTestVector t = rfcTestVectors[i];
        PublicKeyVerify verifier = createVerifier(t);
        try {
            verifier.verify(t.sig, t.msg);
        } catch (GeneralSecurityException e) {
            fail("Valid signature, should not throw exception");
        }
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) ECPoint(java.security.spec.ECPoint) Test(org.junit.Test)

Example 3 with PublicKeyVerify

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

the class Ed25519PrivateKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    assertTrue(key instanceof Ed25519PrivateKey);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    assertEquals(32, keyProto.getKeyValue().size());
    PublicKeySign signer = manager.getPrimitive(key);
    assertTrue(signer instanceof Ed25519Sign);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    assertEquals(64, signature.length);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    assertTrue(verifier instanceof Ed25519Verify);
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Do not expect GeneralSecurityException: " + e);
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 4 with PublicKeyVerify

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

the class Ed25519PrivateKeyManagerTest method testGetPublicKeyData.

/**
 * Tests that a public key is extracted properly from a private key.
 */
@Test
public void testGetPublicKeyData() throws Exception {
    KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ED25519);
    KeyData privateKeyData = TestUtil.getKeyset(privateHandle).getKey(0).getKeyData();
    Ed25519PrivateKeyManager privateManager = new Ed25519PrivateKeyManager();
    KeyData publicKeyData = privateManager.getPublicKeyData(privateKeyData.getValue());
    assertEquals(Ed25519PublicKeyManager.TYPE_URL, publicKeyData.getTypeUrl());
    assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType());
    Ed25519PrivateKey privateKey = Ed25519PrivateKey.parseFrom(privateKeyData.getValue());
    assertArrayEquals(privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray());
    Ed25519PublicKeyManager publicManager = new Ed25519PublicKeyManager();
    PublicKeySign signer = privateManager.getPrimitive(privateKeyData.getValue());
    PublicKeyVerify verifier = publicManager.getPrimitive(publicKeyData.getValue());
    byte[] message = Random.randBytes(20);
    try {
        verifier.verify(signer.sign(message), message);
    } catch (GeneralSecurityException e) {
        fail("Should not fail: " + e);
    }
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) PublicKeySign(com.google.crypto.tink.PublicKeySign) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 5 with PublicKeyVerify

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

the class EcdsaSignKeyManagerTest method testGetPublicKeyData.

/**
 * Tests that a public key is extracted properly from a private key.
 */
@Test
public void testGetPublicKeyData() throws Exception {
    KeysetHandle privateHandle = KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
    KeyData privateKeyData = TestUtil.getKeyset(privateHandle).getKey(0).getKeyData();
    EcdsaSignKeyManager privateManager = new EcdsaSignKeyManager();
    KeyData publicKeyData = privateManager.getPublicKeyData(privateKeyData.getValue());
    assertEquals(EcdsaVerifyKeyManager.TYPE_URL, publicKeyData.getTypeUrl());
    assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType());
    EcdsaPrivateKey privateKey = EcdsaPrivateKey.parseFrom(privateKeyData.getValue());
    assertArrayEquals(privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray());
    EcdsaVerifyKeyManager publicManager = new EcdsaVerifyKeyManager();
    PublicKeySign signer = privateManager.getPrimitive(privateKeyData.getValue());
    PublicKeyVerify verifier = publicManager.getPrimitive(publicKeyData.getValue());
    byte[] message = Random.randBytes(20);
    try {
        verifier.verify(signer.sign(message), message);
    } catch (GeneralSecurityException e) {
        fail("Should not fail: " + e);
    }
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey) PublicKeySign(com.google.crypto.tink.PublicKeySign) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Aggregations

PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)14 GeneralSecurityException (java.security.GeneralSecurityException)12 Test (org.junit.Test)11 PublicKeySign (com.google.crypto.tink.PublicKeySign)8 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 ECPoint (java.security.spec.ECPoint)5 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)4 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)3 KeyPair (java.security.KeyPair)3 KeyPairGenerator (java.security.KeyPairGenerator)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 ECParameterSpec (java.security.spec.ECParameterSpec)3 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)2 HashType (com.google.crypto.tink.proto.HashType)2 KeyData (com.google.crypto.tink.proto.KeyData)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 Key (com.google.crypto.tink.proto.Keyset.Key)2 MessageLite (com.google.protobuf.MessageLite)2 KeyManager (com.google.crypto.tink.KeyManager)1