use of com.google.crypto.tink.PublicKeyVerify 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.");
}
}
}
use of com.google.crypto.tink.PublicKeyVerify in project tink by google.
the class EcdsaSignKeyManagerTest method testNewKeyWithVerifier.
private void testNewKeyWithVerifier(KeyTemplate keyTemplate) throws Exception {
// Call newKey multiple times and make sure that it generates different keys.
int numTests = 9;
EcdsaPrivateKey[] privKeys = new EcdsaPrivateKey[numTests];
EcdsaSignKeyManager signManager = new EcdsaSignKeyManager();
Set<String> keys = new TreeSet<String>();
for (int j = 0; j < numTests / 3; j++) {
privKeys[3 * j] = (EcdsaPrivateKey) signManager.newKey(EcdsaKeyFormat.parseFrom(keyTemplate.getValue()));
keys.add(TestUtil.hexEncode(privKeys[3 * j].toByteArray()));
privKeys[3 * j + 1] = (EcdsaPrivateKey) signManager.newKey(keyTemplate.getValue());
keys.add(TestUtil.hexEncode(privKeys[3 * j + 1].toByteArray()));
privKeys[3 * j + 2] = EcdsaPrivateKey.parseFrom(signManager.newKeyData(keyTemplate.getValue()).getValue());
keys.add(TestUtil.hexEncode(privKeys[3 * j + 2].toByteArray()));
}
assertEquals(numTests, keys.size());
// failure is 2^-64 which happens when a key has 8 leading zeros.
for (int j = 0; j < numTests; j++) {
int keySize = privKeys[j].getKeyValue().toByteArray().length;
EcdsaKeyFormat ecdsaKeyFormat = EcdsaKeyFormat.parseFrom(keyTemplate.getValue());
switch(ecdsaKeyFormat.getParams().getCurve()) {
case NIST_P256:
assertTrue(256 / 8 - 8 <= keySize);
assertTrue(256 / 8 + 1 >= keySize);
break;
case NIST_P384:
assertTrue(384 / 8 - 8 <= keySize);
assertTrue(384 / 8 + 1 >= keySize);
break;
case NIST_P521:
assertTrue(521 / 8 - 8 <= keySize);
assertTrue(521 / 8 + 1 >= keySize);
break;
default:
break;
}
}
// Test whether signer works correctly with the corresponding verifier.
EcdsaVerifyKeyManager verifyManager = new EcdsaVerifyKeyManager();
for (int j = 0; j < numTests; j++) {
PublicKeySign signer = signManager.getPrimitive(privKeys[j]);
byte[] signature = signer.sign(msg);
for (int k = 0; k < numTests; k++) {
PublicKeyVerify verifier = verifyManager.getPrimitive(privKeys[k].getPublicKey());
if (j == k) {
// The same key
try {
verifier.verify(signature, msg);
} catch (GeneralSecurityException ex) {
fail("Valid signature, should not throw exception");
}
} else {
// Different keys
try {
verifier.verify(signature, msg);
fail("Invalid signature, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
}
use of com.google.crypto.tink.PublicKeyVerify in project tink by google.
the class EcdsaVerifyKeyManagerTest method testGetPrimitiveWithUnsupportedKey.
@Test
public void testGetPrimitiveWithUnsupportedKey() throws Exception {
HashAndCurveType[] hashAndCurves = { new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P256), new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA1, EllipticCurveType.NIST_P521), new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P521), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P256) };
for (int i = 0; i < hashAndCurves.length; i++) {
HashType hashType = hashAndCurves[i].hashType;
EllipticCurveType curveType = hashAndCurves[i].curveType;
ECParameterSpec ecParams = EllipticCurves.getCurveSpec(SigUtil.toCurveType(curveType));
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey unusedPrivKey = (ECPrivateKey) keyPair.getPrivate();
// Create PublicKeyVerify.
ECPoint w = pubKey.getW();
try {
PublicKeyVerify unusedVerifier = createVerifier(hashType, curveType, EcdsaSignatureEncoding.DER, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
fail("Unsupported key, should have thrown exception: " + hashType + " " + curveType);
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
use of com.google.crypto.tink.PublicKeyVerify in project tink by google.
the class EcdsaVerifyKeyManagerTest method testGetPrimitiveWithJCE.
@Test
public void testGetPrimitiveWithJCE() throws Exception {
HashAndCurveType[] hashAndCurves = { new HashAndCurveType(HashType.SHA256, EllipticCurveType.NIST_P256), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P384), new HashAndCurveType(HashType.SHA512, EllipticCurveType.NIST_P521) };
for (int i = 0; i < hashAndCurves.length; i++) {
HashType hashType = hashAndCurves[i].hashType;
EllipticCurveType curveType = hashAndCurves[i].curveType;
ECParameterSpec ecParams = EllipticCurves.getCurveSpec(SigUtil.toCurveType(curveType));
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privKey = (ECPrivateKey) keyPair.getPrivate();
// Sign with JCE's Signature.
Signature signer = Signature.getInstance(SigUtil.toEcdsaAlgo(hashType));
signer.initSign(privKey);
byte[] msg = Random.randBytes(1231);
signer.update(msg);
byte[] signature = signer.sign();
// Create PublicKeyVerify.
ECPoint w = pubKey.getW();
PublicKeyVerify verifier = createVerifier(hashType, curveType, EcdsaSignatureEncoding.DER, w.getAffineX().toByteArray(), w.getAffineY().toByteArray());
try {
verifier.verify(signature, msg);
} catch (GeneralSecurityException e) {
fail("Valid signature, should not throw exception");
}
}
}
Aggregations