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);
}
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);
}
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");
}
}
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;
}
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);
}
Aggregations