Search in sources :

Example 91 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project libsignal-service-java by signalapp.

the class ProfileCipher method encryptName.

public byte[] encryptName(byte[] input, int paddedLength) {
    try {
        byte[] inputPadded = new byte[paddedLength];
        if (input.length > inputPadded.length) {
            throw new IllegalArgumentException("Input is too long: " + new String(input));
        }
        System.arraycopy(input, 0, inputPadded, 0, input.length);
        byte[] nonce = Util.getSecretBytes(12);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, nonce));
        return ByteUtil.combine(nonce, cipher.doFinal(inputPadded));
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 92 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project libsignal-service-java by signalapp.

the class UnidentifiedAccess method deriveAccessKeyFrom.

public static byte[] deriveAccessKeyFrom(byte[] profileKey) {
    try {
        byte[] nonce = new byte[12];
        byte[] input = new byte[16];
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(profileKey, "AES"), new GCMParameterSpec(128, nonce));
        byte[] ciphertext = cipher.doFinal(input);
        return ByteUtil.trim(ciphertext, 16);
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 93 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project libsignal-service-java by signalapp.

the class ContactDiscoveryCipher method createDiscoveryRequest.

public DiscoveryRequest createDiscoveryRequest(List<String> addressBook, RemoteAttestation remoteAttestation) {
    try {
        ByteArrayOutputStream requestDataStream = new ByteArrayOutputStream();
        for (String address : addressBook) {
            requestDataStream.write(ByteUtil.longToByteArray(Long.parseLong(address)));
        }
        byte[] requestData = requestDataStream.toByteArray();
        byte[] nonce = Util.getSecretBytes(12);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(remoteAttestation.getKeys().getClientKey(), "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, nonce));
        cipher.updateAAD(remoteAttestation.getRequestId());
        byte[] cipherText = cipher.doFinal(requestData);
        byte[][] parts = ByteUtil.split(cipherText, cipherText.length - TAG_LENGTH_BYTES, TAG_LENGTH_BYTES);
        return new DiscoveryRequest(addressBook.size(), remoteAttestation.getRequestId(), nonce, parts[0], parts[1]);
    } catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) DiscoveryRequest(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest)

Example 94 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by signalapp.

the class SignalStorageCipher method decrypt.

public static byte[] decrypt(StorageCipherKey key, byte[] data) throws InvalidKeyException {
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        byte[][] split = Util.split(data, IV_LENGTH, data.length - IV_LENGTH);
        byte[] iv = split[0];
        byte[] cipherText = split[1];
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, iv));
        return cipher.doFinal(cipherText);
    } catch (java.security.InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        throw new InvalidKeyException(e);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 95 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by signalapp.

the class AESCipher method encrypt.

static AESEncryptedResult encrypt(byte[] key, byte[] aad, byte[] requestData) {
    try {
        byte[] iv = Util.getSecretBytes(12);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, iv));
        if (aad != null) {
            cipher.updateAAD(aad);
        }
        byte[] cipherText = cipher.doFinal(requestData);
        byte[][] parts = ByteUtil.split(cipherText, cipherText.length - TAG_LENGTH_BYTES, TAG_LENGTH_BYTES);
        byte[] mac = parts[1];
        byte[] data = parts[0];
        return new AESEncryptedResult(iv, data, mac, aad);
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)101 Cipher (javax.crypto.Cipher)71 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)31 InvalidKeyException (java.security.InvalidKeyException)30 BadPaddingException (javax.crypto.BadPaddingException)29 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)29 SecretKey (javax.crypto.SecretKey)21 GeneralSecurityException (java.security.GeneralSecurityException)12 AEADBadTagException (javax.crypto.AEADBadTagException)12 Key (java.security.Key)11 ByteBuffer (java.nio.ByteBuffer)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ExcludedTest (com.google.security.wycheproof.WycheproofRunner.ExcludedTest)5 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)5 FileNotFoundException (java.io.FileNotFoundException)5