Skip to content

Commit b6111f5

Browse files
author
gefeili
committed
Add tests on AEADParameters
1 parent a89da1e commit b6111f5

File tree

17 files changed

+158
-104
lines changed

17 files changed

+158
-104
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/AEADBaseEngine.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ public byte[] getMac()
4141
return mac;
4242
}
4343

44+
public void reset()
45+
{
46+
reset(true);
47+
}
48+
4449
public int processByte(byte in, byte[] out, int outOff)
4550
throws DataLengthException
4651
{
47-
return processBytes(new byte[]{ in }, 0, 1, out, outOff);
52+
return processBytes(new byte[]{in}, 0, 1, out, outOff);
4853
}
4954

5055
protected byte[][] initialize(boolean forEncryption, CipherParameters params)
@@ -98,4 +103,16 @@ else if (params instanceof ParametersWithIV)
98103
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
99104
return new byte[][]{k, npub};
100105
}
106+
107+
protected void reset(boolean clearMac)
108+
{
109+
if (clearMac)
110+
{
111+
mac = null;
112+
}
113+
if (initialAssociatedText != null)
114+
{
115+
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
116+
}
117+
}
101118
}

core/src/main/java/org/bouncycastle/crypto/engines/AsconBaseEngine.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,9 @@ public int getOutputSize(int len)
423423
}
424424
}
425425

426-
public void reset()
427-
{
428-
reset(true);
429-
}
430426

431427
protected void reset(boolean clearMac)
432428
{
433-
if (clearMac)
434-
{
435-
mac = null;
436-
}
437429
Arrays.clear(m_buf);
438430
m_bufPos = 0;
439431

@@ -456,10 +448,7 @@ protected void reset(boolean clearMac)
456448
throw new IllegalStateException(getAlgorithmName() + " needs to be initialized");
457449
}
458450
ascon_aeadinit();
459-
if (initialAssociatedText != null)
460-
{
461-
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
462-
}
451+
super.reset(clearMac);
463452
}
464453

465454
public abstract String getAlgorithmVersion();

core/src/main/java/org/bouncycastle/crypto/engines/ElephantEngine.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,6 @@ public int getOutputSize(int len)
445445
return Math.max(0, len + inputOff - CRYPTO_ABYTES);
446446
}
447447

448-
@Override
449-
public void reset()
450-
{
451-
reset(true);
452-
}
453-
454448
private int processAADBytes()
455449
{
456450
byte[] ad = aadData.toByteArray();
@@ -464,19 +458,16 @@ private int processAADBytes()
464458
return ad.length;
465459
}
466460

467-
private void reset(boolean clearMac)
461+
protected void reset(boolean clearMac)
468462
{
469-
if (clearMac)
470-
{
471-
mac = null;
472-
}
473463
aadData.reset();
474464
Arrays.fill(tag_buffer, (byte)0);
475465
Arrays.fill(previous_outputMessage, (byte)0);
476466
inputOff = 0;
477467
nb_its = 0;
478468
adOff = -1;
479469
messageLen = 0;
470+
super.reset(clearMac);
480471
}
481472

482473
public int getBlockSize()

core/src/main/java/org/bouncycastle/crypto/engines/Grain128AEADEngine.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,14 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
265265
return len;
266266
}
267267

268-
public void reset()
268+
protected void reset(boolean clearMac)
269269
{
270-
reset(true);
271-
}
272-
273-
private void reset(boolean clearMac)
274-
{
275-
if (clearMac)
276-
{
277-
this.mac = null;
278-
}
279270
this.aadData.reset();
280271
this.aadFinished = false;
281272

282273
setKey(workingKey, workingIV);
283274
initGrain();
275+
super.reset(clearMac);
284276
}
285277

286278
private void getKeyStream(byte[] input, int inOff, int len, byte[] ciphertext, int outOff)
@@ -297,19 +289,24 @@ private void getKeyStream(byte[] input, int inOff, int len, byte[] ciphertext, i
297289
int input_i_j = (input_i >> j) & 1;
298290
cc |= (input_i_j ^ output) << j;
299291

300-
int mask = -input_i_j;
301-
authAcc[0] ^= authSr[0] & mask;
302-
authAcc[1] ^= authSr[1] & mask;
303-
304-
authShift(getOutput());
305-
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
306-
lfsr = shift(lfsr, (getOutputLFSR()) & 1);
292+
updateInternalState(input_i_j);
307293
}
308294
ciphertext[outOff + i] = cc;
309295
}
310296

311297
}
312298

299+
private void updateInternalState(int input_i_j)
300+
{
301+
int mask = -input_i_j;
302+
authAcc[0] ^= authSr[0] & mask;
303+
authAcc[1] ^= authSr[1] & mask;
304+
305+
authShift(getOutput());
306+
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
307+
lfsr = shift(lfsr, (getOutputLFSR()) & 1);
308+
}
309+
313310
public void processAADByte(byte in)
314311
{
315312
if (aadFinished)
@@ -367,13 +364,7 @@ private void doProcessAADBytes(byte[] input, int len)
367364

368365
int ader_i_j = (ader_i >> j) & 1;
369366

370-
int mask = -ader_i_j;
371-
authAcc[0] ^= authSr[0] & mask;
372-
authAcc[1] ^= authSr[1] & mask;
373-
374-
authShift(getOutput());
375-
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
376-
lfsr = shift(lfsr, (getOutputLFSR()) & 1);
367+
updateInternalState(ader_i_j);
377368
}
378369
}
379370
}

core/src/main/java/org/bouncycastle/crypto/engines/ISAPEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public ISAPEngine(IsapType isapType)
3131
{
3232
CRYPTO_KEYBYTES = 16;
3333
CRYPTO_NPUBBYTES = 16;
34+
CRYPTO_ABYTES = 16;
3435
switch (isapType)
3536
{
3637
case ISAP_A_128A:
@@ -905,8 +906,7 @@ public int getOutputSize(int len)
905906
return Math.max(0, len + message.size() + (forEncryption ? 16 : -16));
906907
}
907908

908-
@Override
909-
public void reset()
909+
protected void reset(boolean clearMac)
910910
{
911911
if (!initialised)
912912
{
@@ -916,6 +916,7 @@ public void reset()
916916
ISAPAEAD.reset();
917917
message.reset();
918918
outputStream.reset();
919+
super.reset(clearMac);
919920
}
920921

921922
public int getBlockSize()

core/src/main/java/org/bouncycastle/crypto/engines/PhotonBeetleEngine.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,14 @@ public void reset()
239239
reset(true);
240240
}
241241

242-
private void reset(boolean clearMac)
242+
protected void reset(boolean clearMac)
243243
{
244-
if (clearMac)
245-
{
246-
mac = null;
247-
}
248244
input_empty = true;
249245
aadData.reset();
250246
message.reset();
251247
System.arraycopy(K, 0, state, 0, K.length);
252248
System.arraycopy(N, 0, state, K.length, N.length);
249+
super.reset(clearMac);
253250
}
254251

255252
private void PHOTON_Permutation()

core/src/main/java/org/bouncycastle/crypto/engines/SparkleEngine.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,6 @@ public int getOutputSize(int len)
440440
}
441441
}
442442

443-
public void reset()
444-
{
445-
reset(true);
446-
}
447-
448443
private void checkAAD()
449444
{
450445
switch (m_state)
@@ -624,13 +619,8 @@ private void processFinalAAD()
624619
sparkle_opt(state, SPARKLE_STEPS_BIG);
625620
}
626621

627-
private void reset(boolean clearMac)
622+
protected void reset(boolean clearMac)
628623
{
629-
if (clearMac)
630-
{
631-
mac = null;
632-
}
633-
634624
Arrays.clear(m_buf);
635625
m_bufPos = 0;
636626
encrypted = false;
@@ -663,10 +653,7 @@ private void reset(boolean clearMac)
663653

664654
sparkle_opt(state, SPARKLE_STEPS_BIG);
665655

666-
if (initialAssociatedText != null)
667-
{
668-
processAADBytes(initialAssociatedText, 0, initialAssociatedText.length);
669-
}
656+
super.reset(clearMac);
670657
}
671658

672659
private static int ELL(int x)

core/src/main/java/org/bouncycastle/crypto/engines/XoodyakEngine.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,8 @@ public void reset()
238238
reset(true);
239239
}
240240

241-
private void reset(boolean clearMac)
241+
protected void reset(boolean clearMac)
242242
{
243-
if (clearMac)
244-
{
245-
mac = null;
246-
}
247243
Arrays.fill(state, (byte)0);
248244
aadFinished = false;
249245
encrypted = false;
@@ -260,6 +256,7 @@ private void reset(boolean clearMac)
260256
System.arraycopy(iv, 0, KID, KLen, IDLen);
261257
KID[KLen + IDLen] = (byte)IDLen;
262258
AbsorbAny(KID, 0, KLen + IDLen + 1, Rabsorb, 0x02);
259+
super.reset(clearMac);
263260
}
264261

265262
private void AbsorbAny(byte[] X, int Xoff, int XLen, int r, int Cd)

core/src/main/java/org/bouncycastle/util/test/SimpleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void operation()
273273
throws Exception;
274274
}
275275

276-
protected Exception testException(String failMessage, String exceptionClass, TestExceptionOperation operation)
276+
public Exception testException(String failMessage, String exceptionClass, TestExceptionOperation operation)
277277
{
278278
try
279279
{
@@ -286,7 +286,7 @@ protected Exception testException(String failMessage, String exceptionClass, Tes
286286
{
287287
isTrue(e.getMessage(), e.getMessage().indexOf(failMessage) >= 0);
288288
}
289-
isTrue(e.getClass().getName().indexOf(exceptionClass) >= 0);
289+
isTrue(e.getMessage(),e.getClass().getName().indexOf(exceptionClass) >= 0);
290290
return e;
291291
}
292292
return null;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void performTest()
9191
testVectorsXof_AsconXof();
9292
testVectorsXof_AsconXofA();
9393

94+
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconAEAD128());
95+
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
96+
CipherTest.checkAEADParemeter(this, 16,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
97+
CipherTest.checkAEADParemeter(this, 20,16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
98+
9499
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
95100
{
96101
@Override

0 commit comments

Comments
 (0)