-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAesKeyring.java
More file actions
204 lines (160 loc) · 7.34 KB
/
Copy pathAesKeyring.java
File metadata and controls
204 lines (160 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.encryption.s3.materials;
import software.amazon.encryption.s3.S3EncryptionClientException;
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
import software.amazon.encryption.s3.internal.CryptoFactory;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
/**
* This keyring can wrap keys with the active keywrap algorithm and
* unwrap with the active and legacy algorithms for AES keys.
*/
public class AesKeyring extends RawKeyring {
private static final String KEY_ALGORITHM = "AES";
private final SecretKey _wrappingKey;
private final DecryptDataKeyStrategy _aesStrategy = new DecryptDataKeyStrategy() {
private static final String KEY_PROVIDER_INFO = "AES";
private static final String CIPHER_ALGORITHM = "AES";
@Override
public boolean isLegacy() {
return true;
}
@Override
public String keyProviderInfo() {
return KEY_PROVIDER_INFO;
}
@Override
public byte[] decryptDataKey(DecryptionMaterials materials, byte[] encryptedDataKey) throws GeneralSecurityException {
final Cipher cipher = CryptoFactory.createCipher(CIPHER_ALGORITHM, materials.cryptoProvider());
cipher.init(Cipher.DECRYPT_MODE, _wrappingKey);
return cipher.doFinal(encryptedDataKey);
}
};
private final DecryptDataKeyStrategy _aesWrapStrategy = new DecryptDataKeyStrategy() {
private static final String KEY_PROVIDER_INFO = "AESWrap";
private static final String CIPHER_ALGORITHM = "AESWrap";
@Override
public boolean isLegacy() {
return true;
}
@Override
public String keyProviderInfo() {
return KEY_PROVIDER_INFO;
}
@Override
public byte[] decryptDataKey(DecryptionMaterials materials, byte[] encryptedDataKey) throws GeneralSecurityException {
final Cipher cipher = CryptoFactory.createCipher(CIPHER_ALGORITHM, materials.cryptoProvider());
cipher.init(Cipher.UNWRAP_MODE, _wrappingKey);
Key plaintextKey = cipher.unwrap(encryptedDataKey, CIPHER_ALGORITHM, Cipher.SECRET_KEY);
return plaintextKey.getEncoded();
}
};
private final DataKeyStrategy _aesGcmStrategy = new DataKeyStrategy() {
private static final String KEY_PROVIDER_INFO = "AES/GCM";
private static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";
private static final int IV_LENGTH_BYTES = 12;
private static final int TAG_LENGTH_BYTES = 16;
private static final int TAG_LENGTH_BITS = TAG_LENGTH_BYTES * 8;
@Override
public boolean isLegacy() {
return false;
}
@Override
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
return modifyMaterialsForRawKeyring(materials);
}
@Override
public String keyProviderInfo() {
return KEY_PROVIDER_INFO;
}
@Override
public EncryptionMaterials generateDataKey(EncryptionMaterials materials) {
return defaultGenerateDataKey(materials);
}
@Override
public byte[] encryptDataKey(SecureRandom secureRandom,
EncryptionMaterials materials)
throws GeneralSecurityException {
byte[] iv = new byte[IV_LENGTH_BYTES];
secureRandom.nextBytes(iv);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, iv);
final Cipher cipher = CryptoFactory.createCipher(CIPHER_ALGORITHM, materials.cryptoProvider());
cipher.init(Cipher.ENCRYPT_MODE, _wrappingKey, gcmParameterSpec, secureRandom);
final byte[] aADBytes = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName().getBytes(StandardCharsets.UTF_8);
cipher.updateAAD(aADBytes);
byte[] ciphertext = cipher.doFinal(materials.plaintextDataKey());
// The encrypted data key is the iv prepended to the ciphertext
byte[] encodedBytes = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, encodedBytes, 0, iv.length);
System.arraycopy(ciphertext, 0, encodedBytes, iv.length, ciphertext.length);
return encodedBytes;
}
@Override
public byte[] decryptDataKey(DecryptionMaterials materials, byte[] encryptedDataKey) throws GeneralSecurityException {
byte[] iv = new byte[IV_LENGTH_BYTES];
byte[] ciphertext = new byte[encryptedDataKey.length - iv.length];
System.arraycopy(encryptedDataKey, 0, iv, 0, iv.length);
System.arraycopy(encryptedDataKey, iv.length, ciphertext, 0, ciphertext.length);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BITS, iv);
final Cipher cipher = CryptoFactory.createCipher(CIPHER_ALGORITHM, materials.cryptoProvider());
cipher.init(Cipher.DECRYPT_MODE, _wrappingKey, gcmParameterSpec);
final byte[] aADBytes = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName().getBytes(StandardCharsets.UTF_8);
cipher.updateAAD(aADBytes);
return cipher.doFinal(ciphertext);
}
};
private final Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies = new HashMap<>();
private AesKeyring(Builder builder) {
super(builder);
_wrappingKey = builder._wrappingKey;
decryptDataKeyStrategies.put(_aesStrategy.keyProviderInfo(), _aesStrategy);
decryptDataKeyStrategies.put(_aesWrapStrategy.keyProviderInfo(), _aesWrapStrategy);
decryptDataKeyStrategies.put(_aesGcmStrategy.keyProviderInfo(), _aesGcmStrategy);
}
public static Builder builder() {
return new Builder();
}
@Override
protected GenerateDataKeyStrategy generateDataKeyStrategy() {
return _aesGcmStrategy;
}
@Override
protected EncryptDataKeyStrategy encryptDataKeyStrategy() {
return _aesGcmStrategy;
}
@Override
protected Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies() {
return decryptDataKeyStrategies;
}
public static class Builder extends RawKeyring.Builder<AesKeyring, Builder> {
private SecretKey _wrappingKey;
private Builder() {
super();
}
@Override
protected Builder builder() {
return this;
}
public Builder wrappingKey(final SecretKey wrappingKey) {
if (wrappingKey == null) {
throw new S3EncryptionClientException("Wrapping key cannot be null!");
}
if (!wrappingKey.getAlgorithm().equals(KEY_ALGORITHM)) {
throw new S3EncryptionClientException("Invalid algorithm: " + wrappingKey.getAlgorithm() + ", expecting " + KEY_ALGORITHM);
}
_wrappingKey = wrappingKey;
return builder();
}
public AesKeyring build() {
return new AesKeyring(this);
}
}
}