-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathBlockDecryptionHandlerTest.java
110 lines (91 loc) · 4.19 KB
/
BlockDecryptionHandlerTest.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
/*
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
* in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.encryptionsdk.internal;
import static org.junit.Assert.assertTrue;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.amazonaws.encryptionsdk.exception.BadCiphertextException;
import org.junit.Before;
import org.junit.Test;
import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
public class BlockDecryptionHandlerTest {
private static final SecureRandom RND = new SecureRandom();
private final CryptoAlgorithm cryptoAlgorithm_ = AwsCrypto.getDefaultCryptoAlgorithm();
private final byte[] messageId_ = new byte[Constants.MESSAGE_ID_LEN];
private final byte nonceLen_ = cryptoAlgorithm_.getNonceLen();
private final byte[] dataKeyBytes_ = new byte[cryptoAlgorithm_.getKeyLength()];
private final SecretKey dataKey_ = new SecretKeySpec(dataKeyBytes_, "AES");
private final BlockDecryptionHandler blockDecryptionHandler_ = new BlockDecryptionHandler(
dataKey_,
nonceLen_,
cryptoAlgorithm_,
messageId_);
@Before
public void setup() {
RND.nextBytes(messageId_);
RND.nextBytes(dataKeyBytes_);
}
@Test
public void estimateOutputSize() {
final int inLen = 1;
final int outSize = blockDecryptionHandler_.estimateOutputSize(inLen);
// the estimated output size must at least be equal to inLen.
assertTrue(outSize >= inLen);
}
@Test(expected= BadCiphertextException.class)
public void doFinalCalledWhileNotComplete() {
blockDecryptionHandler_.doFinal(new byte[1], 0);
}
@Test(expected = AwsCryptoException.class)
public void decryptMaxContentLength() {
final BlockEncryptionHandler blockEncryptionHandler = new BlockEncryptionHandler(
dataKey_,
nonceLen_,
cryptoAlgorithm_,
messageId_);
final byte[] in = new byte[0];
final int outLen = blockEncryptionHandler.estimateOutputSize(in.length);
final byte[] out = new byte[outLen];
blockEncryptionHandler.processBytes(in, 0, in.length, out, 0);
blockEncryptionHandler.doFinal(out, 0);
final ByteBuffer outBuff = ByteBuffer.wrap(out);
// pull out nonce to get to content length.
final byte[] nonce = new byte[nonceLen_];
outBuff.get(nonce);
// set content length to integer max value + 1.
outBuff.putLong(Integer.MAX_VALUE + 1L);
final int decryptedOutLen = blockDecryptionHandler_.estimateOutputSize(outLen);
final byte[] decryptedOut = new byte[decryptedOutLen];
blockDecryptionHandler_.processBytes(outBuff.array(), 0, outBuff.array().length, decryptedOut, 0);
}
@Test(expected = AwsCryptoException.class)
public void processBytesCalledWhileComplete() {
final BlockEncryptionHandler blockEncryptionHandler = new BlockEncryptionHandler(
dataKey_,
nonceLen_,
cryptoAlgorithm_,
messageId_);
final byte[] in = new byte[0];
final int outLen = blockEncryptionHandler.estimateOutputSize(in.length);
final byte[] out = new byte[outLen];
blockEncryptionHandler.processBytes(in, 0, in.length, out, 0);
blockEncryptionHandler.doFinal(out, 0);
final byte[] decryptedOut = new byte[outLen];
blockDecryptionHandler_.processBytes(out, 0, outLen, decryptedOut, 0);
blockDecryptionHandler_.processBytes(out, 0, outLen, decryptedOut, 0);
}
}