Search in sources :

Example 1 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class EciesAeadHkdfHybridDecryptTest method testEncryptDecrypt_mutatedContext_throws.

private static void testEncryptDecrypt_mutatedContext_throws(CurveType curveType, KeyTemplate keyTemplate) throws Exception {
    KeyPair recipientKey = EllipticCurves.generateKeyPair(curveType);
    ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
    ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
    byte[] salt = Random.randBytes(8);
    String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
    byte[] plaintext = Random.randBytes(4);
    byte[] context = Random.randBytes(4);
    HybridEncrypt hybridEncrypt = new EciesAeadHkdfHybridEncrypt(recipientPublicKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
    HybridDecrypt hybridDecrypt = new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, context);
    for (BytesMutation mutation : TestUtil.generateMutations(context)) {
        // The test takes too long in TSan, so we stop after the first case.
        assertThrows(GeneralSecurityException.class, () -> hybridDecrypt.decrypt(ciphertext, mutation.value));
        if (TestUtil.isTsan()) {
            return;
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) EciesAeadHkdfHybridDecrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt) ECPublicKey(java.security.interfaces.ECPublicKey) EciesAeadHkdfHybridDecrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Example 2 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class EciesAeadHkdfHybridDecryptTest method testEncryptDecrypt_mutatedCiphertext_throws.

private static void testEncryptDecrypt_mutatedCiphertext_throws(CurveType curveType, KeyTemplate keyTemplate) throws Exception {
    KeyPair recipientKey = EllipticCurves.generateKeyPair(curveType);
    ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
    ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
    byte[] salt = Random.randBytes(8);
    String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
    byte[] plaintext = Random.randBytes(4);
    byte[] context = Random.randBytes(4);
    HybridEncrypt hybridEncrypt = new EciesAeadHkdfHybridEncrypt(recipientPublicKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
    HybridDecrypt hybridDecrypt = new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, context);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(GeneralSecurityException.class, () -> hybridDecrypt.decrypt(mutation.value, context));
        // The test takes too long in TSan, so we stop after the first case.
        if (TestUtil.isTsan()) {
            return;
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) EciesAeadHkdfHybridDecrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt) ECPublicKey(java.security.interfaces.ECPublicKey) EciesAeadHkdfHybridDecrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Example 3 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class InsecureNonceChaCha20Poly1305Test method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    Assume.assumeFalse(TinkFips.useOnlyFips());
    byte[] key = Random.randBytes(KEY_SIZE_IN_BYTES);
    InsecureNonceChaCha20Poly1305 cipher = createInstance(key);
    byte[] aad = Random.randBytes(16);
    byte[] message = Random.randBytes(32);
    byte[] nonce = Random.randBytes(NONCE_SIZE_IN_BYTES);
    byte[] ciphertext = cipher.encrypt(nonce, message, aad);
    for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
        assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Arrays.toString(aad), mutation.description), GeneralSecurityException.class, () -> {
            byte[] unused = cipher.decrypt(nonce, mutation.value, aad);
        });
    }
    // Modify AAD
    for (int b = 0; b < aad.length; b++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modified = Arrays.copyOf(aad, aad.length);
            modified[b] ^= (byte) (1 << bit);
            assertThrows(AEADBadTagException.class, () -> {
                byte[] unused = cipher.decrypt(nonce, ciphertext, modified);
            });
        }
    }
}
Also used : BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Example 4 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class EcdsaVerifyKeyManagerTest method testRfcTestVectors.

@Test
public void testRfcTestVectors() throws Exception {
    for (int i = 0; i < rfcTestVectors.length; i++) {
        RfcTestVector t = rfcTestVectors[i];
        PublicKeyVerify verifier = createVerifier(t);
        verifier.verify(t.sig, t.msg);
        for (BytesMutation mutation : TestUtil.generateMutations(t.sig)) {
            assertThrows(String.format("Invalid signature, should have thrown exception : sig = %s, msg = %s," + " description = %s", Hex.encode(mutation.value), Hex.encode(t.msg), mutation.description), GeneralSecurityException.class, () -> verifier.verify(mutation.value, t.msg));
        }
    }
}
Also used : PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) ECPoint(java.security.spec.ECPoint) BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Example 5 with BytesMutation

use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.

the class EcdsaVerifyJceTest method testModification.

@Test
public void testModification() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    ECParameterSpec ecParams = EllipticCurves.getNistP256Params();
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    keyGen.initialize(ecParams);
    KeyPair keyPair = keyGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
    ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
    EcdsaEncoding[] encodings = new EcdsaEncoding[] { EcdsaEncoding.IEEE_P1363, EcdsaEncoding.DER };
    for (EcdsaEncoding encoding : encodings) {
        // Sign with EcdsaSignJce
        EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, encoding);
        byte[] message = "Hello".getBytes("UTF-8");
        byte[] signature = signer.sign(message);
        // Verify with EcdsaVerifyJce.
        EcdsaVerifyJce verifier = new EcdsaVerifyJce(pub, HashType.SHA256, encoding);
        for (final BytesMutation mutation : TestUtil.generateMutations(signature)) {
            assertThrows(String.format("Invalid signature, should have thrown exception : signature = %s, message = %s, " + " description = %s", Hex.encode(mutation.value), Arrays.toString(message), mutation.description), GeneralSecurityException.class, () -> verifier.verify(mutation.value, message));
        }
        // Encodings mismatch.
        EcdsaVerifyJce verifier2 = new EcdsaVerifyJce(pub, HashType.SHA256, encoding == EcdsaEncoding.IEEE_P1363 ? EcdsaEncoding.DER : EcdsaEncoding.IEEE_P1363);
        assertThrows(GeneralSecurityException.class, () -> verifier2.verify(signature, message));
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) EcdsaEncoding(com.google.crypto.tink.subtle.EllipticCurves.EcdsaEncoding) KeyPairGenerator(java.security.KeyPairGenerator) BytesMutation(com.google.crypto.tink.testing.TestUtil.BytesMutation) Test(org.junit.Test)

Aggregations

BytesMutation (com.google.crypto.tink.testing.TestUtil.BytesMutation)9 Test (org.junit.Test)7 KeyPair (java.security.KeyPair)3 ECPrivateKey (java.security.interfaces.ECPrivateKey)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)2 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)2 EciesAeadHkdfHybridDecrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt)2 EciesAeadHkdfHybridEncrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)2 Aead (com.google.crypto.tink.Aead)1 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)1 EcdsaEncoding (com.google.crypto.tink.subtle.EllipticCurves.EcdsaEncoding)1 KeyPairGenerator (java.security.KeyPairGenerator)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 ECPoint (java.security.spec.ECPoint)1