Search in sources :

Example 16 with HybridEncrypt

use of com.google.crypto.tink.HybridEncrypt in project tink by google.

the class WebPushHybridDecryptTest method testModifyCiphertext.

@Test
public void testModifyCiphertext() throws Exception {
    KeyPair uaKeyPair = EllipticCurves.generateKeyPair(WebPushConstants.NIST_P256_CURVE_TYPE);
    ECPrivateKey uaPrivateKey = (ECPrivateKey) uaKeyPair.getPrivate();
    ECPublicKey uaPublicKey = (ECPublicKey) uaKeyPair.getPublic();
    byte[] authSecret = Random.randBytes(16);
    HybridEncrypt hybridEncrypt = new WebPushHybridEncrypt.Builder().withAuthSecret(authSecret).withRecipientPublicKey(uaPublicKey).build();
    HybridDecrypt hybridDecrypt = new WebPushHybridDecrypt.Builder().withAuthSecret(authSecret).withRecipientPublicKey(uaPublicKey).withRecipientPrivateKey(uaPrivateKey).build();
    byte[] plaintext = Random.randBytes(20);
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null);
    // Flipping bits.
    for (int b = 0; b < ciphertext.length; b++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modified = Arrays.copyOf(ciphertext, ciphertext.length);
            modified[b] ^= (byte) (1 << bit);
            try {
                byte[] unused = hybridDecrypt.decrypt(modified, null);
                fail("Decrypting modified ciphertext should fail");
            } catch (GeneralSecurityException ex) {
            // This is expected.
            }
        }
    }
    // Truncate the message.
    for (int length = 0; length < ciphertext.length; length++) {
        byte[] modified = Arrays.copyOf(ciphertext, length);
        try {
            byte[] unused = hybridDecrypt.decrypt(modified, null);
            fail("Decrypting modified ciphertext should fail");
        } catch (GeneralSecurityException ex) {
        // This is expected.
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) ECPublicKey(java.security.interfaces.ECPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) Test(org.junit.Test)

Aggregations

HybridEncrypt (com.google.crypto.tink.HybridEncrypt)16 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)13 Test (org.junit.Test)12 KeyPair (java.security.KeyPair)10 ECPublicKey (java.security.interfaces.ECPublicKey)10 ECPrivateKey (java.security.interfaces.ECPrivateKey)9 GeneralSecurityException (java.security.GeneralSecurityException)6 KeysetHandle (com.google.crypto.tink.KeysetHandle)4 EciesAeadHkdfPrivateKey (com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey)3 EcPointFormat (com.google.crypto.tink.proto.EcPointFormat)2 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)2 HashType (com.google.crypto.tink.proto.HashType)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 Key (com.google.crypto.tink.proto.Keyset.Key)2 EciesAeadHkdfHybridDecrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridDecrypt)2 EciesAeadHkdfHybridEncrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)2 KeyPairGenerator (java.security.KeyPairGenerator)2 ECParameterSpec (java.security.spec.ECParameterSpec)2 TreeSet (java.util.TreeSet)2 KeyManager (com.google.crypto.tink.KeyManager)1