Search in sources :

Example 51 with GCMParameterSpec

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

the class WebPushHybridEncrypt method encrypt.

private byte[] encrypt(final byte[] key, final byte[] nonce, final byte[] plaintext) throws GeneralSecurityException {
    Cipher cipher = EngineFactory.CIPHER.getInstance("AES/GCM/NoPadding");
    GCMParameterSpec params = new GCMParameterSpec(8 * WebPushConstants.TAG_SIZE, nonce);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), params);
    byte[] paddedPlaintext = new byte[plaintext.length + 1 + paddingSize];
    paddedPlaintext[plaintext.length] = WebPushConstants.PADDING_DELIMITER_BYTE;
    System.arraycopy(plaintext, 0, paddedPlaintext, 0, plaintext.length);
    return cipher.doFinal(paddedPlaintext);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 52 with GCMParameterSpec

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

the class AndroidKeystoreAesGcm method decryptInternal.

private byte[] decryptInternal(final byte[] ciphertext, final byte[] aad) throws GeneralSecurityException {
    if (ciphertext.length < IV_SIZE_IN_BYTES + TAG_SIZE_IN_BYTES) {
        throw new GeneralSecurityException("ciphertext too short");
    }
    GCMParameterSpec params = new GCMParameterSpec(8 * TAG_SIZE_IN_BYTES, ciphertext, 0, IV_SIZE_IN_BYTES);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, key, params);
    cipher.updateAAD(aad);
    return cipher.doFinal(ciphertext, IV_SIZE_IN_BYTES, ciphertext.length - IV_SIZE_IN_BYTES);
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) Cipher(javax.crypto.Cipher)

Example 53 with GCMParameterSpec

use of javax.crypto.spec.GCMParameterSpec in project openj9 by eclipse.

the class OpenSSLInvalidGCMKeySizeTest method testGCM.

@Test
public static void testGCM() {
    final int len = 1024;
    final int tagLen = 16;
    // 15 bytes is an illegal key size
    final int key_size = 15;
    byte[] skey_bytes = new byte[key_size / 8];
    SecretKeySpec skey = new SecretKeySpec(skey_bytes, "AES");
    byte[] data = new byte[len];
    byte[] tagBuffer = new byte[len + tagLen];
    byte[] iv = new byte[12];
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        AlgorithmParameterSpec iva = new GCMParameterSpec(tagLen * 8, iv);
        cipher.init(Cipher.ENCRYPT_MODE, skey, iva);
        cipher.doFinal(data, 0, data.length, tagBuffer);
        Assert.fail("InvalidKeyException is expected to be thrown");
    } catch (InvalidKeyException e) {
        logger.debug("InvalidKeyException thrown as expected");
        logger.debug(e);
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Unexpected exception thrown");
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidKeyException(java.security.InvalidKeyException) Test(org.testng.annotations.Test)

Example 54 with GCMParameterSpec

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

the class AesGcmDecryptor method decrypt.

public byte[] decrypt(byte[] ciphertext, int cipherTextOffset, int cipherTextLength, byte[] AAD) {
    int plainTextLength = cipherTextLength - GCM_TAG_LENGTH - NONCE_LENGTH;
    if (plainTextLength < 1) {
        throw new ParquetCryptoRuntimeException("Wrong input length " + plainTextLength);
    }
    // Get the nonce from ciphertext
    System.arraycopy(ciphertext, cipherTextOffset, localNonce, 0, NONCE_LENGTH);
    byte[] plainText = new byte[plainTextLength];
    int inputLength = cipherTextLength - NONCE_LENGTH;
    int inputOffset = cipherTextOffset + NONCE_LENGTH;
    int outputOffset = 0;
    try {
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH_BITS, localNonce);
        cipher.init(Cipher.DECRYPT_MODE, aesKey, spec);
        if (null != AAD)
            cipher.updateAAD(AAD);
        cipher.doFinal(ciphertext, inputOffset, inputLength, plainText, outputOffset);
    } catch (AEADBadTagException e) {
        throw new TagVerificationException("GCM tag check failed", e);
    } catch (GeneralSecurityException e) {
        throw new ParquetCryptoRuntimeException("Failed to decrypt", e);
    }
    return plainText;
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 55 with GCMParameterSpec

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

the class LockSettingsService method getDecryptedPasswordForTiedProfile.

private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
    if (DEBUG)
        Slog.v(TAG, "Get child profile decrytped key");
    byte[] storedData = mStorage.readChildProfileLock(userId);
    if (storedData == null) {
        throw new FileNotFoundException("Child profile lock file not found");
    }
    byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
    byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
    byte[] decryptionResult;
    java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
    keyStore.load(null);
    SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
    Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
    decryptionResult = cipher.doFinal(encryptedPassword);
    return new String(decryptionResult, StandardCharsets.UTF_8);
}
Also used : SecretKey(javax.crypto.SecretKey) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) 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