Search in sources :

Example 96 with GCMParameterSpec

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

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

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

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

use of javax.crypto.spec.GCMParameterSpec in project android by nextcloud.

the class EncryptionUtils method encryptStringSymmetric.

private static String encryptStringSymmetric(String string, byte[] encryptionKeyBytes, String delimiter) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance(AES_CIPHER);
    byte[] iv = randomBytes(ivLength);
    Key key = new SecretKeySpec(encryptionKeyBytes, AES);
    GCMParameterSpec spec = new GCMParameterSpec(128, iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] bytes = encodeStringToBase64Bytes(string);
    byte[] cryptedBytes = cipher.doFinal(bytes);
    String encodedCryptedBytes = encodeBytesToBase64String(cryptedBytes);
    String encodedIV = encodeBytesToBase64String(iv);
    return encodedCryptedBytes + delimiter + encodedIV;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) PublicKey(java.security.PublicKey)

Example 99 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project android by nextcloud.

the class EncryptionUtils method decryptFile.

/**
 * @param file               encrypted file
 * @param encryptionKeyBytes key from metadata
 * @param iv                 initialization vector from metadata
 * @param authenticationTag  authenticationTag from metadata
 * @return decrypted byte[]
 */
public static byte[] decryptFile(File file, byte[] encryptionKeyBytes, byte[] iv, byte[] authenticationTag) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
    Cipher cipher = Cipher.getInstance(AES_CIPHER);
    Key key = new SecretKeySpec(encryptionKeyBytes, AES);
    GCMParameterSpec spec = new GCMParameterSpec(128, iv);
    cipher.init(Cipher.DECRYPT_MODE, key, spec);
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
    byte[] fileBytes = new byte[(int) randomAccessFile.length()];
    randomAccessFile.readFully(fileBytes);
    // check authentication tag
    byte[] extractedAuthenticationTag = Arrays.copyOfRange(fileBytes, fileBytes.length - (128 / 8), fileBytes.length);
    if (!Arrays.equals(extractedAuthenticationTag, authenticationTag)) {
        throw new SecurityException("Tag not correct");
    }
    return cipher.doFinal(fileBytes);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) PublicKey(java.security.PublicKey)

Example 100 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project parquet-mr by apache.

the class AesGcmEncryptor method encrypt.

public byte[] encrypt(boolean writeLength, byte[] plainText, byte[] nonce, byte[] AAD) {
    if (operationCounter > GCM_RANDOM_IV_SAME_KEY_MAX_OPS) {
        throw new ParquetCryptoRuntimeException("Exceeded limit of AES GCM encryption operations with same key and random IV");
    }
    operationCounter++;
    if (nonce.length != NONCE_LENGTH) {
        throw new ParquetCryptoRuntimeException("Wrong nonce length " + nonce.length);
    }
    int plainTextLength = plainText.length;
    int cipherTextLength = NONCE_LENGTH + plainTextLength + GCM_TAG_LENGTH;
    int lengthBufferLength = writeLength ? SIZE_LENGTH : 0;
    byte[] cipherText = new byte[lengthBufferLength + cipherTextLength];
    int inputLength = plainTextLength;
    int inputOffset = 0;
    int outputOffset = lengthBufferLength + NONCE_LENGTH;
    try {
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH_BITS, nonce);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, spec);
        if (null != AAD)
            cipher.updateAAD(AAD);
        cipher.doFinal(plainText, inputOffset, inputLength, cipherText, outputOffset);
    } catch (GeneralSecurityException e) {
        throw new ParquetCryptoRuntimeException("Failed to encrypt", e);
    }
    // Add ciphertext length
    if (writeLength) {
        System.arraycopy(BytesUtils.intToBytes(cipherTextLength), 0, cipherText, 0, lengthBufferLength);
    }
    // Add the nonce
    System.arraycopy(nonce, 0, cipherText, lengthBufferLength, NONCE_LENGTH);
    return cipherText;
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

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