use of com.google.crypto.tink.proto.KeyTemplate 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.proto.KeyTemplate in project tink by google.
the class MacKeyTemplatesTest method testHMAC_SHA256_128BITTAG.
@Test
public void testHMAC_SHA256_128BITTAG() throws Exception {
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
assertEquals(HmacKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
HmacKeyFormat format = HmacKeyFormat.parseFrom(template.getValue());
assertEquals(32, format.getKeySize());
assertEquals(16, format.getParams().getTagSize());
assertEquals(HashType.SHA256, format.getParams().getHash());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class SignatureKeyTemplatesTest method testED25519.
@Test
public void testED25519() throws Exception {
KeyTemplate template = SignatureKeyTemplates.ED25519;
assertEquals(Ed25519PrivateKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
// Empty format.
assertTrue(template.getValue().isEmpty());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class SignatureKeyTemplatesTest method testECDSA_P256.
@Test
public void testECDSA_P256() throws Exception {
KeyTemplate template = SignatureKeyTemplates.ECDSA_P256;
assertEquals(EcdsaSignKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EcdsaKeyFormat format = EcdsaKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasParams());
assertEquals(HashType.SHA256, format.getParams().getHashType());
assertEquals(EllipticCurveType.NIST_P256, format.getParams().getCurve());
assertEquals(EcdsaSignatureEncoding.DER, format.getParams().getEncoding());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class SignatureKeyTemplatesTest method ecdsaP256Ieee.
@Test
public void ecdsaP256Ieee() throws Exception {
KeyTemplate template = SignatureKeyTemplates.ECDSA_P256_IEEE_P1363;
assertEquals(new EcdsaSignKeyManager().getKeyType(), template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
EcdsaKeyFormat format = EcdsaKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertTrue(format.hasParams());
assertEquals(HashType.SHA256, format.getParams().getHashType());
assertEquals(EllipticCurveType.NIST_P256, format.getParams().getCurve());
assertEquals(EcdsaSignatureEncoding.IEEE_P1363, format.getParams().getEncoding());
}
Aggregations