Search in sources :

Example 86 with KeyTemplate

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

Example 87 with KeyTemplate

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());
}
Also used : HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 88 with KeyTemplate

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());
}
Also used : KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 89 with KeyTemplate

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());
}
Also used : EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 90 with KeyTemplate

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());
}
Also used : EcdsaKeyFormat(com.google.crypto.tink.proto.EcdsaKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)119 Test (org.junit.Test)116 GeneralSecurityException (java.security.GeneralSecurityException)14 ByteString (com.google.protobuf.ByteString)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)11 HashType (com.google.crypto.tink.proto.HashType)11 KeyData (com.google.crypto.tink.proto.KeyData)11 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)8 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)7 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)7 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TreeSet (java.util.TreeSet)7 KeysetHandle (com.google.crypto.tink.KeysetHandle)6 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)6 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 EciesAeadHkdfKeyFormat (com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat)6 EciesHkdfKemParams (com.google.crypto.tink.proto.EciesHkdfKemParams)6