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);
}
}
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);
}
}
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());
}
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.");
}
}
}
Aggregations