2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.security.InvalidKeyException;
30 import java.security.NoSuchAlgorithmException;
32 import javax.crypto.BadPaddingException;
33 import javax.crypto.Cipher;
34 import javax.crypto.CipherInputStream;
35 import javax.crypto.CipherOutputStream;
36 import javax.crypto.IllegalBlockSizeException;
37 import javax.crypto.KeyGenerator;
38 import javax.crypto.NoSuchPaddingException;
39 import javax.crypto.SecretKey;
40 import javax.crypto.spec.SecretKeySpec;
42 import org.onap.aaf.cadi.Symm.Encryption;
43 import org.onap.aaf.cadi.util.Chmod;
47 * AES Class wraps Cipher AES, 128
48 * NOTE: While not explicitly stated in JavaDocs, Ciphers AND SecretKeySpecs are NOT ThreadSafe
49 * Ciphers take time to create, therefore, we have pooled them.
54 public class AES implements Encryption {
55 public static final String AES = AES.class.getSimpleName();
56 public static final int AES_KEY_SIZE = 128; // 256 isn't supported on all JDKs.
58 private SecretKeySpec aeskeySpec;
60 public static SecretKey newKey() throws NoSuchAlgorithmException {
61 KeyGenerator kgen = KeyGenerator.getInstance(AES);
62 kgen.init(AES_KEY_SIZE);
63 return kgen.generateKey();
66 public AES(byte[] aeskey, int offset, int len){
67 aeskeySpec = new SecretKeySpec(aeskey,offset,len,AES);
70 public byte[] encrypt(byte[] in) throws CadiException {
72 Cipher c = Cipher.getInstance(AES);
73 c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
75 } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException e) {
76 throw new CadiException(e);
80 public byte[] decrypt(byte[] in) throws CadiException {
82 Cipher c = Cipher.getInstance(AES);
83 c.init(Cipher.DECRYPT_MODE,aeskeySpec);
85 } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException e) {
86 throw new CadiException(e);
90 public void save(File keyfile) throws IOException {
91 FileOutputStream fis = new FileOutputStream(keyfile);
93 fis.write(aeskeySpec.getEncoded());
97 Chmod.to400.chmod(keyfile);
100 public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {
102 Cipher c = Cipher.getInstance(AES);
104 c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
106 c.init(Cipher.DECRYPT_MODE,aeskeySpec);
108 return new CipherOutputStream(os,c);
109 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
110 // Cannot add Exception to this API. throw Runtime
111 System.err.println("Error creating Aes CipherOutputStream");
112 return null; // should never get here.
116 public CipherInputStream inputStream(InputStream is, boolean encrypt) {
118 Cipher c = Cipher.getInstance(AES);
120 c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
122 c.init(Cipher.DECRYPT_MODE,aeskeySpec);
124 return new CipherInputStream(is,c);
125 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
126 // Cannot add Exception to this API. throw Runtime
127 System.err.println("Error creating Aes CipherInputStream");
128 return null; // should never get here.