Search in sources :

Example 31 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project grpc-java by grpc.

the class AesGcmAeadCrypter method encryptAad.

private int encryptAad(ByteBuffer ciphertext, ByteBuffer plaintext, @Nullable ByteBuffer aad, byte[] nonce) throws GeneralSecurityException {
    checkArgument(nonce.length == NONCE_LENGTH);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(this.key, AES), new GCMParameterSpec(TAG_LENGTH * 8, nonce));
    if (aad != null) {
        cipher.updateAAD(aad);
    }
    return cipher.doFinal(plaintext, ciphertext);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 32 with GCMParameterSpec

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

the class GCMParameterSpecTest method testGetIV_Subarray_Success.

public void testGetIV_Subarray_Success() throws Exception {
    GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV, 2, 4);
    assertEquals(Arrays.toString(Arrays.copyOfRange(TEST_IV, 2, 6)), Arrays.toString(spec.getIV()));
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 33 with GCMParameterSpec

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

the class GCMParameterSpecTest method testGetTLen_Success.

public void testGetTLen_Success() throws Exception {
    GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
    assertEquals(8, spec.getTLen());
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 34 with GCMParameterSpec

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

the class SignalStorageCipher method encrypt.

public static byte[] encrypt(StorageCipherKey key, byte[] data) {
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        byte[] iv = Util.getSecretBytes(IV_LENGTH);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, iv));
        byte[] ciphertext = cipher.doFinal(data);
        return Util.join(iv, ciphertext);
    } catch (NoSuchAlgorithmException | java.security.InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException 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(org.whispersystems.libsignal.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 35 with GCMParameterSpec

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

the class AESCipher method decrypt.

static byte[] decrypt(byte[] key, byte[] iv, byte[] ciphertext, byte[] tag) throws InvalidCiphertextException {
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, iv));
        return cipher.doFinal(ByteUtil.combine(ciphertext, tag));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException e) {
        throw new AssertionError(e);
    } catch (InvalidKeyException | BadPaddingException e) {
        throw new InvalidCiphertextException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) 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)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