Search in sources :

Example 11 with HybridEncrypt

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

the class CreatePublicKeysetCommandTest method assertHybrid.

private void assertHybrid(KeysetReader privateReader, KeysetReader publicReader) throws Exception {
    HybridDecrypt decrypter = HybridDecryptFactory.getPrimitive(CleartextKeysetHandle.read(privateReader));
    HybridEncrypt encrypter = HybridEncryptFactory.getPrimitive(CleartextKeysetHandle.read(publicReader));
    byte[] message = Random.randBytes(10);
    byte[] contextInfo = Random.randBytes(20);
    assertThat(decrypter.decrypt(encrypter.encrypt(message, contextInfo), contextInfo)).isEqualTo(message);
}
Also used : HybridDecrypt(com.google.crypto.tink.HybridDecrypt) HybridEncrypt(com.google.crypto.tink.HybridEncrypt)

Example 12 with HybridEncrypt

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

the class PaymentMethodTokenHybridDecryptTest method testModifyDecrypt.

@Test
public void testModifyDecrypt() throws Exception {
    ECParameterSpec spec = EllipticCurves.getNistP256Params();
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    keyGen.initialize(spec);
    KeyPair recipientKey = keyGen.generateKeyPair();
    ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
    ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
    HybridEncrypt hybridEncrypt = new PaymentMethodTokenHybridEncrypt(recipientPublicKey, ProtocolVersionConfig.EC_V1);
    HybridDecrypt hybridDecrypt = new PaymentMethodTokenHybridDecrypt(recipientPrivateKey, ProtocolVersionConfig.EC_V1);
    testModifyDecrypt(hybridEncrypt, hybridDecrypt);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) Test(org.junit.Test)

Example 13 with HybridEncrypt

use of com.google.crypto.tink.HybridEncrypt 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 14 with HybridEncrypt

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

the class HybridEncryptCli method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.out.println("Usage: HybridEncryptCli keyset-file plaintext-file context-info output-file");
        System.exit(1);
    }
    String keysetFilename = args[0];
    String plaintextFilename = args[1];
    String contextInfo = args[2];
    String outputFilename = args[3];
    System.out.println("Using keyset from file " + keysetFilename + " to encrypt file " + plaintextFilename + " with context info '" + contextInfo + "'.");
    System.out.println("The resulting ciphertext will be written to file " + outputFilename);
    // Init Tink.
    CliUtil.initTink();
    // Read the keyset.
    System.out.println("Reading the keyset...");
    KeysetHandle keysetHandle = CliUtil.readKeyset(keysetFilename);
    // Get the primitive.
    System.out.println("Getting the primitive...");
    HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandle);
    // Read the plaintext.
    byte[] plaintext = CliUtil.read(plaintextFilename);
    // Compute the ciphertext.
    System.out.println("Encrypting...");
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo.getBytes(CliUtil.UTF_8));
    // Write the ciphertext to the output file.
    CliUtil.write(ciphertext, outputFilename);
    System.out.println("All done.");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) HybridEncrypt(com.google.crypto.tink.HybridEncrypt)

Example 15 with HybridEncrypt

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

the class WebPushHybridEncryptTest method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    for (int i = 0; i < 10; i++) {
        KeyPair uaKeyPair = EllipticCurves.generateKeyPair(WebPushConstants.NIST_P256_CURVE_TYPE);
        ECPrivateKey uaPrivateKey = (ECPrivateKey) uaKeyPair.getPrivate();
        ECPublicKey uaPublicKey = (ECPublicKey) uaKeyPair.getPublic();
        byte[] uaPublicKeyBytes = EllipticCurves.pointEncode(WebPushConstants.NIST_P256_CURVE_TYPE, WebPushConstants.UNCOMPRESSED_POINT_FORMAT, uaPublicKey.getW());
        byte[] authSecret = Random.randBytes(16);
        HybridEncrypt hybridEncrypt = new WebPushHybridEncrypt.Builder().withAuthSecret(authSecret).withRecipientPublicKey(uaPublicKeyBytes).build();
        HybridDecrypt hybridDecrypt = new WebPushHybridDecrypt.Builder().withAuthSecret(authSecret).withRecipientPublicKey(uaPublicKeyBytes).withRecipientPrivateKey(uaPrivateKey).build();
        Set<String> salts = new TreeSet<String>();
        Set<String> ephemeralPublicKeys = new TreeSet<String>();
        Set<String> payloads = new TreeSet<String>();
        int numTests = 50;
        for (int j = 0; j < numTests; j++) {
            byte[] plaintext = Random.randBytes(j);
            byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null);
            assertEquals(ciphertext.length, plaintext.length + WebPushConstants.CIPHERTEXT_OVERHEAD);
            assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, null));
            // Checks that the encryption is randomized.
            ByteBuffer record = ByteBuffer.wrap(ciphertext);
            byte[] salt = new byte[WebPushConstants.SALT_SIZE];
            record.get(salt);
            salts.add(Hex.encode(salt));
            int unused1 = record.getInt();
            int unused2 = (int) record.get();
            byte[] ephemeralPublicKey = new byte[WebPushConstants.PUBLIC_KEY_SIZE];
            record.get(ephemeralPublicKey);
            ephemeralPublicKeys.add(Hex.encode(ephemeralPublicKey));
            byte[] payload = new byte[ciphertext.length - WebPushConstants.CONTENT_CODING_HEADER_SIZE];
            record.get(payload);
            payloads.add(Hex.encode(payload));
        }
        assertEquals(numTests, salts.size());
        assertEquals(numTests, ephemeralPublicKeys.size());
        assertEquals(numTests, payloads.size());
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) ByteBuffer(java.nio.ByteBuffer) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) ECPublicKey(java.security.interfaces.ECPublicKey) TreeSet(java.util.TreeSet) 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