Search in sources :

Example 1 with EciesAeadHkdfHybridEncrypt

use of com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt in project tink by google.

the class EciesAeadHkdfHybridDecryptTest method testModifyDecrypt.

private void testModifyDecrypt(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);
    byte[] plaintext = Random.randBytes(4);
    byte[] context = Random.randBytes(4);
    String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
    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);
    byte[] decrypted = hybridDecrypt.decrypt(ciphertext, context);
    assertArrayEquals(plaintext, decrypted);
    // implicitly checks the modification of public key and the raw ciphertext.
    for (int bytes = 0; bytes < ciphertext.length; bytes++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modifiedCiphertext = Arrays.copyOf(ciphertext, ciphertext.length);
            modifiedCiphertext[bytes] ^= (byte) (1 << bit);
            try {
                hybridDecrypt.decrypt(modifiedCiphertext, context);
                fail("Invalid ciphertext, should have thrown exception");
            } catch (GeneralSecurityException expected) {
            // Expected
            }
        }
    }
    // Modify context.
    for (int bytes = 0; bytes < context.length; bytes++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modifiedContext = Arrays.copyOf(context, context.length);
            modifiedContext[bytes] ^= (byte) (1 << bit);
            try {
                hybridDecrypt.decrypt(ciphertext, modifiedContext);
                fail("Invalid context, should have thrown exception");
            } catch (GeneralSecurityException expected) {
            // Expected
            }
        }
    }
    // modifying the length may not be detected.
    for (int bytes = 0; bytes < salt.length; bytes++) {
        for (int bit = 0; bit < 8; bit++) {
            byte[] modifiedSalt = Arrays.copyOf(salt, salt.length);
            modifiedSalt[bytes] ^= (byte) (1 << bit);
            hybridDecrypt = new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, modifiedSalt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
            try {
                hybridDecrypt.decrypt(ciphertext, context);
                fail("Invalid salt, should have thrown exception");
            } catch (GeneralSecurityException expected) {
            // Expected
            }
        }
    }
}
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) GeneralSecurityException(java.security.GeneralSecurityException) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Example 2 with EciesAeadHkdfHybridEncrypt

use of com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt in project tink by google.

the class EciesAeadHkdfHybridEncryptTest method testBasicMultipleEncrypts.

private void testBasicMultipleEncrypts(CurveType curveType, KeyTemplate keyTemplate) throws Exception {
    KeyPair recipientKey = EllipticCurves.generateKeyPair(curveType);
    ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
    ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
    byte[] salt = "some salt".getBytes("UTF-8");
    byte[] plaintext = Random.randBytes(20);
    byte[] context = "context info".getBytes("UTF-8");
    String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
    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));
    // Makes sure that the encryption is randomized.
    Set<String> ciphertexts = new TreeSet<String>();
    for (int j = 0; j < 8; j++) {
        byte[] ciphertext = hybridEncrypt.encrypt(plaintext, context);
        if (ciphertexts.contains(new String(ciphertext, "UTF-8"))) {
            throw new GeneralSecurityException("Encryption is not randomized");
        }
        ciphertexts.add(new String(ciphertext, "UTF-8"));
        byte[] decrypted = hybridDecrypt.decrypt(ciphertext, context);
        assertArrayEquals(plaintext, decrypted);
    }
    assertEquals(8, ciphertexts.size());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) GeneralSecurityException(java.security.GeneralSecurityException) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) 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) TreeSet(java.util.TreeSet) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Example 3 with EciesAeadHkdfHybridEncrypt

use of com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt in project tink by google.

the class EciesAeadHkdfPublicKeyManager method getPrimitive.

/**
 * @param recipientKey {@code EciesAeadHkdfPublicKey} proto
 */
@Override
public HybridEncrypt getPrimitive(MessageLite recipientKey) throws GeneralSecurityException {
    if (!(recipientKey instanceof EciesAeadHkdfPublicKey)) {
        throw new GeneralSecurityException("expected EciesAeadHkdfPublicKey proto");
    }
    EciesAeadHkdfPublicKey recipientKeyProto = (EciesAeadHkdfPublicKey) recipientKey;
    validate(recipientKeyProto);
    EciesAeadHkdfParams eciesParams = recipientKeyProto.getParams();
    EciesHkdfKemParams kemParams = eciesParams.getKemParams();
    ECPublicKey recipientPublicKey = EllipticCurves.getEcPublicKey(HybridUtil.toCurveType(kemParams.getCurveType()), recipientKeyProto.getX().toByteArray(), recipientKeyProto.getY().toByteArray());
    EciesAeadHkdfDemHelper demHelper = new RegistryEciesAeadHkdfDemHelper(eciesParams.getDemParams().getAeadDem());
    return new EciesAeadHkdfHybridEncrypt(recipientPublicKey, kemParams.getHkdfSalt().toByteArray(), HybridUtil.toHmacAlgo(kemParams.getHkdfHashType()), HybridUtil.toPointFormatType(eciesParams.getEcPointFormat()), demHelper);
}
Also used : EciesAeadHkdfDemHelper(com.google.crypto.tink.subtle.EciesAeadHkdfDemHelper) EciesAeadHkdfParams(com.google.crypto.tink.proto.EciesAeadHkdfParams) ECPublicKey(java.security.interfaces.ECPublicKey) GeneralSecurityException(java.security.GeneralSecurityException) EciesAeadHkdfPublicKey(com.google.crypto.tink.proto.EciesAeadHkdfPublicKey) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams) EciesAeadHkdfHybridEncrypt(com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)

Aggregations

EciesAeadHkdfHybridEncrypt (com.google.crypto.tink.subtle.EciesAeadHkdfHybridEncrypt)3 GeneralSecurityException (java.security.GeneralSecurityException)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 KeyPair (java.security.KeyPair)2 ECPrivateKey (java.security.interfaces.ECPrivateKey)2 EciesAeadHkdfParams (com.google.crypto.tink.proto.EciesAeadHkdfParams)1 EciesAeadHkdfPublicKey (com.google.crypto.tink.proto.EciesAeadHkdfPublicKey)1 EciesHkdfKemParams (com.google.crypto.tink.proto.EciesHkdfKemParams)1 EciesAeadHkdfDemHelper (com.google.crypto.tink.subtle.EciesAeadHkdfDemHelper)1 TreeSet (java.util.TreeSet)1