001/** 002 * 003 * Copyright 2017 Paul Schaub, 2019-2021 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.omemo.internal; 018 019import java.nio.charset.StandardCharsets; 020import java.security.InvalidAlgorithmParameterException; 021import java.security.InvalidKeyException; 022import java.security.NoSuchAlgorithmException; 023 024import javax.crypto.BadPaddingException; 025import javax.crypto.Cipher; 026import javax.crypto.IllegalBlockSizeException; 027import javax.crypto.NoSuchPaddingException; 028import javax.crypto.SecretKey; 029import javax.crypto.spec.IvParameterSpec; 030import javax.crypto.spec.SecretKeySpec; 031 032import org.jivesoftware.smack.util.RandomUtil; 033import org.jivesoftware.smackx.omemo.util.OmemoConstants; 034import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder; 035 036public class OmemoAesCipher { 037 038 static { 039 byte[] iv = OmemoMessageBuilder.generateIv(); 040 byte[] key = new byte[16]; 041 RandomUtil.fillWithSecureRandom(key); 042 043 try { 044 encryptAesGcmNoPadding("This is just a test", key, iv); 045 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException 046 | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) { 047 String message = "Unable to perform " + OmemoConstants.Crypto.CIPHERMODE 048 + " operation requires by OMEMO. Ensure that a suitable crypto provider for is available." 049 + " For example Bouncycastle on Android (BouncyCastleProvider)"; 050 throw new AssertionError(message); 051 } 052 } 053 054 private enum CipherOpmode { 055 encrypt(Cipher.ENCRYPT_MODE), 056 decrypt(Cipher.DECRYPT_MODE), 057 ; 058 059 public final int opmodeInt; 060 061 CipherOpmode(int opmodeInt) { 062 this.opmodeInt = opmodeInt; 063 } 064 } 065 066 private static byte[] performCipherOperation(CipherOpmode opmode, byte[] input, byte[] key, 067 byte[] initializationVector) 068 throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, 069 NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { 070 SecretKey secretKey = new SecretKeySpec(key, OmemoConstants.Crypto.KEYTYPE); 071 IvParameterSpec ivSpec = new IvParameterSpec(initializationVector); 072 073 Cipher cipher = Cipher.getInstance(OmemoConstants.Crypto.CIPHERMODE); 074 cipher.init(opmode.opmodeInt, secretKey, ivSpec); 075 076 byte[] ciphertext = cipher.doFinal(input); 077 078 return ciphertext; 079 } 080 081 public static byte[] decryptAesGcmNoPadding(byte[] ciphertext, byte[] key, byte[] initializationVector) 082 throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, 083 NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException { 084 return performCipherOperation(CipherOpmode.decrypt, ciphertext, key, initializationVector); 085 } 086 087 public static byte[] encryptAesGcmNoPadding(byte[] plaintext, byte[] key, byte[] initializationVector) 088 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 089 InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { 090 return performCipherOperation(CipherOpmode.encrypt, plaintext, key, initializationVector); 091 } 092 093 public static byte[] encryptAesGcmNoPadding(String plaintext, byte[] key, byte[] initializationVector) 094 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 095 InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { 096 byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); 097 return encryptAesGcmNoPadding(plaintextBytes, key, initializationVector); 098 } 099}