Remove the declaration of thrown exception
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / AES.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi;
23
24 import java.io.File;
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;
31
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;
41
42 import org.onap.aaf.cadi.Symm.Encryption;
43 import org.onap.aaf.cadi.util.Chmod;
44
45
46 /**
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.
50  * 
51  * @author Jonathan
52  *
53  */
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.
57         
58     private SecretKeySpec aeskeySpec;
59
60     public static SecretKey newKey() throws NoSuchAlgorithmException {
61         KeyGenerator kgen = KeyGenerator.getInstance(AES);
62         kgen.init(AES_KEY_SIZE);
63         return kgen.generateKey();
64     }
65
66     public AES(byte[] aeskey, int offset, int len){
67         aeskeySpec = new SecretKeySpec(aeskey,offset,len,AES);
68     }
69     
70     public byte[] encrypt(byte[] in) throws CadiException {
71         try {
72             Cipher c = Cipher.getInstance(AES);
73             c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
74             return c.doFinal(in);
75         } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException e) {
76             throw new CadiException(e);
77         }
78     }
79     
80     public byte[] decrypt(byte[] in) throws CadiException {
81         try {
82             Cipher c = Cipher.getInstance(AES);
83             c.init(Cipher.DECRYPT_MODE,aeskeySpec); 
84             return c.doFinal(in);
85         } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException e) {
86             throw new CadiException(e);
87         }
88     }
89     
90     public void save(File keyfile) throws IOException {
91         FileOutputStream fis = new FileOutputStream(keyfile);
92         try {
93             fis.write(aeskeySpec.getEncoded());
94         } finally {
95             fis.close();
96         }
97         Chmod.to400.chmod(keyfile);
98     }
99
100     public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {
101         try {
102             Cipher c = Cipher.getInstance(AES);
103             if (encrypt) {
104                 c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
105             } else {
106                 c.init(Cipher.DECRYPT_MODE,aeskeySpec);
107             }
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.
113         }
114     }
115     
116     public CipherInputStream inputStream(InputStream is, boolean encrypt) {
117         try {
118             Cipher c = Cipher.getInstance(AES);
119             if (encrypt) {
120                 c.init(Cipher.ENCRYPT_MODE,aeskeySpec);
121             } else {
122                 c.init(Cipher.DECRYPT_MODE,aeskeySpec);
123             }
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.
129         }
130     }
131 }