Coverage Summary for Class: KeyCrypterAes (co.rsk.crypto)
Class |
Class, %
|
Method, %
|
Line, %
|
KeyCrypterAes |
100%
(1/1)
|
100%
(4/4)
|
85.2%
(23/27)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 /*
21 * Copyright 2013 Jim Burton.
22 * Copyright 2014 Andreas Schildbach
23 *
24 * Licensed under the MIT license (the "License")
25 * you may not use this file except in compliance with the License.
26 * You may obtain a copy of the License at
27 *
28 * http://opensource.org/licenses/mit-license.php
29 *
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
35 */
36
37 package co.rsk.crypto;
38
39 import org.bouncycastle.crypto.BufferedBlockCipher;
40 import org.bouncycastle.crypto.engines.AESEngine;
41 import org.bouncycastle.crypto.modes.CBCBlockCipher;
42 import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
43 import org.bouncycastle.crypto.params.KeyParameter;
44 import org.bouncycastle.crypto.params.ParametersWithIV;
45
46 import java.security.SecureRandom;
47 import java.util.Arrays;
48
49 import static com.google.common.base.Preconditions.checkNotNull;
50
51 /**
52 * This class encrypts and decrypts byte arrays using AES symmetric cipher.
53 */
54 public class KeyCrypterAes implements KeyCrypter {
55
56 /**
57 * The size of an AES block in bytes.
58 * This is also the length of the initialisation vector.
59 */
60 private static final int BLOCK_LENGTH = 16; // = 128 bits.
61
62 private static final SecureRandom secureRandom = new SecureRandom();
63
64 /**
65 * Password based encryption using AES - CBC 256 bits.
66 */
67 @Override
68 public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) {
69 checkNotNull(plainBytes);
70 checkNotNull(key);
71
72 try {
73 // Generate iv - each encryption call has a different iv.
74 byte[] iv = new byte[BLOCK_LENGTH];
75 secureRandom.nextBytes(iv);
76
77 ParametersWithIV keyWithIv = new ParametersWithIV(key, iv);
78
79 // Encrypt using AES.
80 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
81 cipher.init(true, keyWithIv);
82 byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
83 final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
84 final int length2 = cipher.doFinal(encryptedBytes, length1);
85
86 return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
87 } catch (Exception e) {
88 throw new KeyCrypterException("Could not encrypt bytes.", e);
89 }
90 }
91
92 /**
93 * Decrypt bytes previously encrypted with this class.
94 *
95 * @param dataToDecrypt The data to decrypt
96 * @param key The AES key to use for decryption
97 * @return The decrypted bytes
98 * @throws KeyCrypterException if bytes could not be decrypted
99 */
100 @Override
101 public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) {
102 checkNotNull(dataToDecrypt);
103 checkNotNull(key);
104
105 try {
106 ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector);
107
108 // Decrypt the message.
109 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
110 cipher.init(false, keyWithIv);
111
112 byte[] cipherBytes = dataToDecrypt.encryptedBytes;
113 byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
114 final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
115 final int length2 = cipher.doFinal(decryptedBytes, length1);
116
117 return Arrays.copyOf(decryptedBytes, length1 + length2);
118 } catch (Exception e) {
119 throw new KeyCrypterException("Could not decrypt bytes", e);
120 }
121 }
122 }