Search in sources :

Example 36 with GCMParameterSpec

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

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)

Example 37 with GCMParameterSpec

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

the class ProfileCipher method decrypt.

/**
 * Returns original data with padding still intact.
 */
public byte[] decrypt(byte[] input) throws InvalidCiphertextException {
    try {
        if (input.length < 12 + 16 + 1) {
            throw new InvalidCiphertextException("Too short: " + input.length);
        }
        byte[] nonce = new byte[12];
        System.arraycopy(input, 0, nonce, 0, nonce.length);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, nonce));
        return cipher.doFinal(input, nonce.length, input.length - nonce.length);
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException e) {
        throw new AssertionError(e);
    } catch (InvalidKeyException | BadPaddingException e) {
        throw new InvalidCiphertextException(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 38 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project cas by apereo.

the class BaseBinaryCipherExecutor method buildParameterSpec.

private AlgorithmParameterSpec buildParameterSpec(final int encryptionKeySize) {
    val iv = new byte[encryptionSecretKey.length];
    System.arraycopy(this.encryptionSecretKey, 0, iv, 0, encryptionSecretKey.length);
    return encryptionKeySize <= MINIMUM_ENCRYPTION_KEY_LENGTH ? new IvParameterSpec(iv) : new GCMParameterSpec(GCM_TAG_LENGTH, iv);
}
Also used : lombok.val(lombok.val) IvParameterSpec(javax.crypto.spec.IvParameterSpec) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 39 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project keywhiz by square.

the class ContentCryptographer method gcm.

private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
    try {
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
        SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
        GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
        cipher.init(mode.cipherMode, derivedKey, gcmParameters);
        return cipher.doFinal(data);
    } catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
        throw Throwables.propagate(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 40 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project sonarqube by SonarSource.

the class AesGCMCipher method encrypt.

@Override
public String encrypt(String clearText) {
    try {
        javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_ALGO);
        byte[] iv = new byte[GCM_IV_LENGTH_IN_BYTES];
        new SecureRandom().nextBytes(iv);
        cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, loadSecretFile(), new GCMParameterSpec(GCM_TAG_LENGTH_IN_BITS, iv));
        byte[] encryptedText = cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8.name()));
        return Base64.encodeBase64String(ByteBuffer.allocate(GCM_IV_LENGTH_IN_BYTES + encryptedText.length).put(iv).put(encryptedText).array());
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)109 Cipher (javax.crypto.Cipher)79 SecretKeySpec (javax.crypto.spec.SecretKeySpec)47 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 RequiresApi (androidx.annotation.RequiresApi)6 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