Search in sources :

Example 1 with Ed25519PrivateKey

use of com.google.crypto.tink.proto.Ed25519PrivateKey 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 2 with Ed25519PrivateKey

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

use of com.google.crypto.tink.proto.Ed25519PrivateKey in project tink by google.

the class Ed25519PrivateKeyManager method getPrimitive.

@Override
public PublicKeySign getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof Ed25519PrivateKey)) {
        throw new GeneralSecurityException("expected Ed25519PrivateKey proto");
    }
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    validate(keyProto);
    return new Ed25519Sign(keyProto.getKeyValue().toByteArray());
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign)

Example 4 with Ed25519PrivateKey

use of com.google.crypto.tink.proto.Ed25519PrivateKey in project tink by google.

the class Ed25519PublicKeyManagerTest method testModifiedSignature.

@Test
public void testModifiedSignature() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    PublicKeySign signer = manager.getPrimitive(key);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Did not expect GeneralSecurityException: " + e);
    }
    // Flip bits in message.
    for (int i = 0; i < message.length; i++) {
        byte[] copy = Arrays.copyOf(message, message.length);
        copy[i] = (byte) (copy[i] ^ 0xff);
        try {
            verifier.verify(signature, copy);
            fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            assertExceptionContains(e, "Signature check failed.");
        }
    }
    // Flip bits in signature.
    // Flip the last byte.
    byte[] copySig = Arrays.copyOf(signature, signature.length);
    copySig[copySig.length - 1] = (byte) (copySig[copySig.length - 1] ^ 0xff);
    try {
        verifier.verify(copySig, message);
        fail("Expected GeneralSecurityException");
    } catch (GeneralSecurityException e) {
        assertExceptionContains(e, "Signature check failed.");
    }
    // Flip other bytes.
    for (int i = 0; i < signature.length - 1; i++) {
        byte[] copy = Arrays.copyOf(signature, signature.length);
        copy[i] = (byte) (copy[i] ^ 0xff);
        try {
            verifier.verify(copy, message);
            fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            assertExceptionContains(e, "Signature check failed.");
        }
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Aggregations

Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)4 GeneralSecurityException (java.security.GeneralSecurityException)4 PublicKeySign (com.google.crypto.tink.PublicKeySign)3 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)3 Test (org.junit.Test)3 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)2 MessageLite (com.google.protobuf.MessageLite)2 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 KeyData (com.google.crypto.tink.proto.KeyData)1 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)1