-
Notifications
You must be signed in to change notification settings - Fork 21
/
AESCBC.java
430 lines (348 loc) · 13.9 KB
/
AESCBC.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* nimbus-jose-jwt
*
* Copyright 2012-2016, Connect2id Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.nimbusds.jose.crypto;
import java.nio.ByteBuffer;
import java.security.Provider;
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWEHeader;
import com.nimbusds.jose.crypto.utils.ConstantTimeUtils;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.util.ByteUtils;
import net.jcip.annotations.ThreadSafe;
/**
* AES/CBC/PKCS5Padding and AES/CBC/PKCS5Padding/HMAC-SHA2 encryption and
* decryption methods. This class is thread-safe.
*
* <p>Also supports the deprecated AES/CBC/HMAC encryption using a custom
* concat KDF (JOSE draft suite 08).
*
* <p>See RFC 7518 (JWA), section 5.2.
*
* @author Vladimir Dzhuvinov
* @author Axel Nennker
* @version 2017-05-30
*/
@ThreadSafe
class AESCBC {
/**
* The standard Initialisation Vector (IV) length (128 bits).
*/
public static final int IV_BIT_LENGTH = 128;
/**
* Generates a random 128 bit (16 byte) Initialisation Vector(IV) for
* use in AES-CBC encryption.
*
* @param randomGen The secure random generator to use. Must be
* correctly initialised and not {@code null}.
*
* @return The random 128 bit IV, as 16 byte array.
*/
public static byte[] generateIV(final SecureRandom randomGen) {
byte[] bytes = new byte[ByteUtils.byteLength(IV_BIT_LENGTH)];
randomGen.nextBytes(bytes);
return bytes;
}
/**
* Creates a new AES/CBC/PKCS5Padding cipher.
*
* @param secretKey The AES key. Must not be {@code null}.
* @param forEncryption If {@code true} creates an encryption cipher,
* else creates a decryption cipher.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param provider The JCA provider, or {@code null} to use the
* default one.
*
* @return The AES/CBC/PKCS5Padding cipher.
*/
private static Cipher createAESCBCCipher(final SecretKey secretKey,
final boolean forEncryption,
final byte[] iv,
final Provider provider)
throws JOSEException {
Cipher cipher;
try {
cipher = CipherHelper.getInstance("AES/CBC/PKCS5Padding", provider);
SecretKeySpec keyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
if (forEncryption) {
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivSpec);
} else {
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivSpec);
}
} catch (Exception e) {
throw new JOSEException(e.getMessage(), e);
}
return cipher;
}
/**
* Encrypts the specified plain text using AES/CBC/PKCS5Padding.
*
* @param secretKey The AES key. Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param plainText The plain text. Must not be {@code null}.
* @param provider The JCA provider, or {@code null} to use the
* default one.
*
* @return The cipher text.
*
* @throws JOSEException If encryption failed.
*/
public static byte[] encrypt(final SecretKey secretKey,
final byte[] iv,
final byte[] plainText,
final Provider provider)
throws JOSEException {
Cipher cipher = createAESCBCCipher(secretKey, true, iv, provider);
try {
return cipher.doFinal(plainText);
} catch (Exception e) {
throw new JOSEException(e.getMessage(), e);
}
}
/**
* Encrypts the specified plain text using AES/CBC/PKCS5Padding/
* HMAC-SHA2.
*
* <p>See RFC 7518 (JWA), section 5.2.2.1
*
* <p>See draft-mcgrew-aead-aes-cbc-hmac-sha2-01
*
* @param secretKey The secret key. Must be 256 or 512 bits long.
* Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param plainText The plain text. Must not be {@code null}.
* @param aad The additional authenticated data. Must not be
* {@code null}.
* @param ceProvider The JCA provider for the content encryption, or
* {@code null} to use the default one.
* @param macProvider The JCA provider for the MAC computation, or
* {@code null} to use the default one.
*
* @return The authenticated cipher text.
*
* @throws JOSEException If encryption failed.
*/
public static AuthenticatedCipherText encryptAuthenticated(final SecretKey secretKey,
final byte[] iv,
final byte[] plainText,
final byte[] aad,
final Provider ceProvider,
final Provider macProvider)
throws JOSEException {
// Extract MAC + AES/CBC keys from input secret key
CompositeKey compositeKey = new CompositeKey(secretKey);
// Encrypt plain text
byte[] cipherText = encrypt(compositeKey.getAESKey(), iv, plainText, ceProvider);
// AAD length to 8 byte array
byte[] al = AAD.computeLength(aad);
// Do MAC
int hmacInputLength = aad.length + iv.length + cipherText.length + al.length;
byte[] hmacInput = ByteBuffer.allocate(hmacInputLength).put(aad).put(iv).put(cipherText).put(al).array();
byte[] hmac = HMAC.compute(compositeKey.getMACKey(), hmacInput, macProvider);
byte[] authTag = Arrays.copyOf(hmac, compositeKey.getTruncatedMACByteLength());
return new AuthenticatedCipherText(cipherText, authTag);
}
/**
* Encrypts the specified plain text using the deprecated concat KDF
* from JOSE draft suite 09.
*
* @param header The JWE header. Must not be {@code null}.
* @param secretKey The secret key. Must be 256 or 512 bits long.
* Must not be {@code null}.
* @param encryptedKey The encrypted key. Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param plainText The plain text. Must not be {@code null}.
* @param ceProvider The JCA provider for the content encryption, or
* {@code null} to use the default one.
* @param macProvider The JCA provider for the MAC computation, or
* {@code null} to use the default one.
*
* @return The authenticated cipher text.
*
* @throws JOSEException If encryption failed.
*/
public static AuthenticatedCipherText encryptWithConcatKDF(final JWEHeader header,
final SecretKey secretKey,
final Base64URL encryptedKey,
final byte[] iv,
final byte[] plainText,
final Provider ceProvider,
final Provider macProvider)
throws JOSEException {
byte[] epu = null;
if (header.getCustomParam("epu") instanceof String) {
epu = new Base64URL((String)header.getCustomParam("epu")).decode();
}
byte[] epv = null;
if (header.getCustomParam("epv") instanceof String) {
epv = new Base64URL((String)header.getCustomParam("epv")).decode();
}
// Generate alternative CEK using concat-KDF
SecretKey altCEK = LegacyConcatKDF.generateCEK(secretKey, header.getEncryptionMethod(), epu, epv);
byte[] cipherText = AESCBC.encrypt(altCEK, iv, plainText, ceProvider);
// Generate content integrity key for HMAC
SecretKey cik = LegacyConcatKDF.generateCIK(secretKey, header.getEncryptionMethod(), epu, epv);
String macInput = header.toBase64URL().toString() + "." +
encryptedKey.toString() + "." +
Base64URL.encode(iv).toString() + "." +
Base64URL.encode(cipherText);
byte[] mac = HMAC.compute(cik, macInput.getBytes(), macProvider);
return new AuthenticatedCipherText(cipherText, mac);
}
/**
* Decrypts the specified cipher text using AES/CBC/PKCS5Padding.
*
* @param secretKey The AES key. Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param cipherText The cipher text. Must not be {@code null}.
* @param provider The JCA provider, or {@code null} to use the
* default one.
*
* @return The decrypted plain text.
*
* @throws JOSEException If decryption failed.
*/
public static byte[] decrypt(final SecretKey secretKey,
final byte[] iv,
final byte[] cipherText,
final Provider provider)
throws JOSEException {
Cipher cipher = createAESCBCCipher(secretKey, false, iv, provider);
try {
return cipher.doFinal(cipherText);
} catch (Exception e) {
throw new JOSEException(e.getMessage(), e);
}
}
/**
* Decrypts the specified cipher text using AES/CBC/PKCS5Padding/
* HMAC-SHA2.
*
* <p>See RFC 7518 (JWA), section 5.2.2.2
*
* <p>See draft-mcgrew-aead-aes-cbc-hmac-sha2-01
*
* @param secretKey The secret key. Must be 256 or 512 bits long.
* Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param cipherText The cipher text. Must not be {@code null}.
* @param aad The additional authenticated data. Must not be
* {@code null}.
* @param authTag The authentication tag. Must not be {@code null}.
* @param ceProvider The JCA provider for the content encryption, or
* {@code null} to use the default one.
* @param macProvider The JCA provider for the MAC computation, or
* {@code null} to use the default one.
*
* @return The decrypted plain text.
*
* @throws JOSEException If decryption failed.
*/
public static byte[] decryptAuthenticated(final SecretKey secretKey,
final byte[] iv,
final byte[] cipherText,
final byte[] aad,
final byte[] authTag,
final Provider ceProvider,
final Provider macProvider)
throws JOSEException {
// Extract MAC + AES/CBC keys from input secret key
CompositeKey compositeKey = new CompositeKey(secretKey);
// AAD length to 8 byte array
byte[] al = AAD.computeLength(aad);
// Check MAC
int hmacInputLength = aad.length + iv.length + cipherText.length + al.length;
byte[] hmacInput = ByteBuffer.allocate(hmacInputLength).
put(aad).
put(iv).
put(cipherText).
put(al).
array();
byte[] hmac = HMAC.compute(compositeKey.getMACKey(), hmacInput, macProvider);
byte[] expectedAuthTag = Arrays.copyOf(hmac, compositeKey.getTruncatedMACByteLength());
if (! ConstantTimeUtils.areEqual(expectedAuthTag, authTag)) {
throw new JOSEException("MAC check failed");
}
return decrypt(compositeKey.getAESKey(), iv, cipherText, ceProvider);
}
/**
* Decrypts the specified cipher text using the deprecated concat KDF
* from JOSE draft suite 09.
*
* @param header The JWE header. Must not be {@code null}.
* @param secretKey The secret key. Must be 256 or 512 bits long.
* Must not be {@code null}.
* @param encryptedKey The encrypted key. Must not be {@code null}.
* @param iv The initialisation vector (IV). Must not be
* {@code null}.
* @param cipherText The cipher text. Must not be {@code null}.
* @param authTag The authentication tag. Must not be {@code null}.
* @param ceProvider The JCA provider for the content encryption, or
* {@code null} to use the default one.
* @param macProvider The JCA provider for the MAC computation, or
* {@code null} to use the default one.
*
* @return The decrypted plain text.
*
* @throws JOSEException If decryption failed.
*/
public static byte[] decryptWithConcatKDF(final JWEHeader header,
final SecretKey secretKey,
final Base64URL encryptedKey,
final Base64URL iv,
final Base64URL cipherText,
final Base64URL authTag,
final Provider ceProvider,
final Provider macProvider)
throws JOSEException {
byte[] epu = null;
if (header.getCustomParam("epu") instanceof String) {
epu = new Base64URL((String)header.getCustomParam("epu")).decode();
}
byte[] epv = null;
if (header.getCustomParam("epv") instanceof String) {
epv = new Base64URL((String)header.getCustomParam("epv")).decode();
}
SecretKey cik = LegacyConcatKDF.generateCIK(secretKey, header.getEncryptionMethod(), epu, epv);
String macInput = header.toBase64URL().toString() + "." +
encryptedKey.toString() + "." +
iv.toString() + "." +
cipherText.toString();
byte[] mac = HMAC.compute(cik, macInput.getBytes(), macProvider);
if (! ConstantTimeUtils.areEqual(authTag.decode(), mac)) {
throw new JOSEException("MAC check failed");
}
SecretKey cekAlt = LegacyConcatKDF.generateCEK(secretKey, header.getEncryptionMethod(), epu, epv);
return AESCBC.decrypt(cekAlt, iv.decode(), cipherText.decode(), ceProvider);
}
/**
* Prevents public instantiation.
*/
private AESCBC() { }
}