Skip to content

Commit b781792

Browse files
committed
added test for long AEAD length setting
1 parent 3de04eb commit b781792

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

core/src/test/java/org/bouncycastle/crypto/test/Grain128AEADTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void performTest()
2828
testVectors();
2929
testSplitUpdate();
3030
testExceptions();
31+
testLongAEAD();
3132
}
3233

3334
private void testVectors()
@@ -124,6 +125,58 @@ private void testSplitUpdate()
124125
}
125126
}
126127

128+
private void testLongAEAD()
129+
throws InvalidCipherTextException
130+
{
131+
byte[] Key = Hex.decode("000102030405060708090A0B0C0D0E0F");
132+
byte[] Nonce = Hex.decode("000102030405060708090A0B");
133+
byte[] PT = Hex.decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
134+
byte[] AD = Hex.decode( // 186 bytes
135+
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
136+
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
137+
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
138+
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
139+
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
140+
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
141+
byte[] CT = Hex.decode("731daa8b1d15317a1ccb4e3dd320095fb27e5bb2a10f2c669f870538637d4f1641bdc02d3cc432a5");
142+
143+
Grain128AEADEngine grain = new Grain128AEADEngine();
144+
ParametersWithIV params = new ParametersWithIV(new KeyParameter(Key), Nonce);
145+
grain.init(true, params);
146+
147+
grain.processAADBytes(AD, 0, AD.length);
148+
149+
byte[] rv = new byte[CT.length];
150+
int len = grain.processBytes(PT, 0, 10, rv, 0);
151+
len += grain.processByte(PT[10], rv, len);
152+
len += grain.processBytes(PT, 11, PT.length - 11, rv, len);
153+
154+
grain.doFinal(rv, len);
155+
156+
isTrue(Arrays.areEqual(rv, CT));
157+
158+
grain.processBytes(PT, 0, 10, rv, 0);
159+
try
160+
{
161+
grain.processAADByte((byte)0x01);
162+
fail("no exception");
163+
}
164+
catch (IllegalStateException e)
165+
{
166+
isEquals("associated data must be added before plaintext/ciphertext", e.getMessage());
167+
}
168+
169+
try
170+
{
171+
grain.processAADBytes(AD, 0, AD.length);
172+
fail("no exception");
173+
}
174+
catch (IllegalStateException e)
175+
{
176+
isEquals("associated data must be added before plaintext/ciphertext", e.getMessage());
177+
}
178+
}
179+
127180
private void testExceptions()
128181
throws InvalidCipherTextException
129182
{

0 commit comments

Comments
 (0)