Search in sources :

Example 71 with GCMParameterSpec

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

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 72 with GCMParameterSpec

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

the class KeyStoreHelper method unseal.

@RequiresApi(Build.VERSION_CODES.M)
public static byte[] unseal(@NonNull SealedData sealedData) {
    SecretKey secretKey = getKeyStoreEntry();
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(128, sealedData.iv));
        return cipher.doFinal(sealedData.data);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
        throw new AssertionError(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) 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) RequiresApi(androidx.annotation.RequiresApi)

Example 73 with GCMParameterSpec

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

the class ProfileCipher method encrypt.

/**
 * Encrypts an input and ensures padded length.
 * <p>
 * Padded length does not include {@link #ENCRYPTION_OVERHEAD}.
 */
public byte[] encrypt(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.serialize(), "AES"), new GCMParameterSpec(128, nonce));
        byte[] encryptedPadded = ByteUtil.combine(nonce, cipher.doFinal(inputPadded));
        if (encryptedPadded.length != (paddedLength + ENCRYPTION_OVERHEAD)) {
            throw new AssertionError(String.format(Locale.US, "Wrong output length %d != padded length %d + %d", encryptedPadded.length, paddedLength, ENCRYPTION_OVERHEAD));
        }
        return encryptedPadded;
    } 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 74 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.

the class GCMParameterSpecTest method testGetIV_Success.

public void testGetIV_Success() throws Exception {
    GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
    byte[] actual = spec.getIV();
    assertEquals(Arrays.toString(TEST_IV), Arrays.toString(actual));
    // XOR with 0xFF so we're sure we changed the array
    for (int i = 0; i < actual.length; i++) {
        actual[i] ^= 0xFF;
    }
    assertFalse("Changing the IV returned shouldn't change the parameter spec", Arrays.equals(spec.getIV(), actual));
    assertEquals(Arrays.toString(TEST_IV), Arrays.toString(spec.getIV()));
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 75 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.

the class CipherOutputStreamTest method testDecryptCorruptGCM.

// From b/36636576. CipherOutputStream had a bug where it would ignore exceptions
// thrown during close().
public void testDecryptCorruptGCM() throws Exception {
    for (Provider provider : Security.getProviders()) {
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("AES/GCM/NoPadding", provider);
        } catch (NoSuchAlgorithmException e) {
            continue;
        }
        SecretKey key;
        if (provider.getName().equals("AndroidKeyStoreBCWorkaround")) {
            key = getAndroidKeyStoreSecretKey();
        } else {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(256);
            key = keygen.generateKey();
        }
        GCMParameterSpec params = new GCMParameterSpec(128, new byte[12]);
        byte[] unencrypted = new byte[200];
        // we have to special-case it
        if (provider.getName().equals("AndroidKeyStoreBCWorkaround")) {
            cipher.init(Cipher.ENCRYPT_MODE, key);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, key, params);
        }
        byte[] encrypted = cipher.doFinal(unencrypted);
        // Corrupt the final byte, which will corrupt the authentication tag
        encrypted[encrypted.length - 1] ^= 1;
        cipher.init(Cipher.DECRYPT_MODE, key, params);
        CipherOutputStream cos = new CipherOutputStream(new ByteArrayOutputStream(), cipher);
        try {
            cos.write(encrypted);
            cos.close();
            fail("Writing a corrupted stream should throw an exception." + "  Provider: " + provider);
        } catch (IOException expected) {
            assertTrue(expected.getCause() instanceof AEADBadTagException);
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KeyGenerator(javax.crypto.KeyGenerator) AEADBadTagException(javax.crypto.AEADBadTagException) Provider(java.security.Provider)

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