Skip to content

Commit 0bd3e09

Browse files
author
gefeili
committed
TODO: Add DecryptionFailureCounter to doFinal of decryption, and check Ascon key and iv reused.
1 parent f6f4cf5 commit 0bd3e09

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,54 @@ public void reset()
705705
}
706706
}
707707

708+
protected class DecryptionFailureCounter
709+
{
710+
public DecryptionFailureCounter(int n)
711+
{
712+
this.n = n;
713+
counter = new int[(n + 31) >>> 5];
714+
}
715+
716+
private final int n;
717+
private final int[] counter;
718+
719+
public boolean increment()
720+
{
721+
int i = counter.length;
722+
while (--i >= 0)
723+
{
724+
if (++counter[i] != 0)
725+
{
726+
break;
727+
}
728+
}
729+
if ((n & 31) == 0)
730+
{
731+
for (i = 0; i < counter.length; ++i)
732+
{
733+
if (counter[i] != 0)
734+
{
735+
return false;
736+
}
737+
}
738+
return true;
739+
}
740+
for (i = 1; i < counter.length; ++i)
741+
{
742+
if (counter[i] != 0)
743+
{
744+
return false;
745+
}
746+
}
747+
return counter[0] != (1 << (n & 31));
748+
}
749+
750+
public void reset()
751+
{
752+
Arrays.fill(counter, 0);
753+
}
754+
}
755+
708756
@Override
709757
public void processAADByte(byte input)
710758
{

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public String getName()
4545
public void performTest()
4646
throws Exception
4747
{
48+
testDecryptionFailureCounter();
4849
testVectorsAsconCXof128_512();
4950
DigestTest.checkXof(new AsconXof128(), 1429, 317, new SecureRandom(), this);
5051
DigestTest.checkXof(new AsconCXof128(), 1429, 317, new SecureRandom(), this);
@@ -108,7 +109,7 @@ public void performTest()
108109
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
109110
CipherTest.checkAEADParemeter(this, 20, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
110111

111-
CipherTest.testOverlapping(this,16, 16, 16, 16, new AsconAEAD128());
112+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new AsconAEAD128());
112113
CipherTest.testOverlapping(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
113114
CipherTest.testOverlapping(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
114115
CipherTest.testOverlapping(this, 20, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
@@ -1355,4 +1356,63 @@ private static void initEngine(AEADCipher ascon, boolean forEncryption)
13551356
AEADParameters parameters = new AEADParameters(new KeyParameter(new byte[keySize]), macSize, new byte[ivSize], null);
13561357
ascon.init(forEncryption, parameters);
13571358
}
1359+
1360+
protected class DecryptionFailureCounter
1361+
{
1362+
public DecryptionFailureCounter(int n)
1363+
{
1364+
this.n = n;
1365+
counter = new int[(n + 31) >>> 5];
1366+
}
1367+
1368+
public final int n;
1369+
1370+
public final int[] counter;
1371+
1372+
public boolean increment()
1373+
{
1374+
int i = counter.length;
1375+
while (--i >= 0)
1376+
{
1377+
if (++counter[i] != 0)
1378+
{
1379+
break;
1380+
}
1381+
}
1382+
if ((n & 31) == 0)
1383+
{
1384+
for (i = 0; i < counter.length; ++i)
1385+
{
1386+
if (counter[i] != 0)
1387+
{
1388+
return false;
1389+
}
1390+
}
1391+
return true;
1392+
}
1393+
for (i = 1; i < counter.length; ++i)
1394+
{
1395+
if (counter[i] != 0)
1396+
{
1397+
return false;
1398+
}
1399+
}
1400+
return counter[0] != (1 << (n & 31));
1401+
}
1402+
1403+
public void reset()
1404+
{
1405+
Arrays.fill(counter, 0);
1406+
}
1407+
}
1408+
1409+
public void testDecryptionFailureCounter()
1410+
{
1411+
int n = 34;
1412+
DecryptionFailureCounter counter = new DecryptionFailureCounter(n);
1413+
counter.counter[counter.counter.length - 1] = -2;
1414+
counter.counter[0] = 1;
1415+
isTrue(!counter.increment());
1416+
isTrue(counter.increment());
1417+
}
13581418
}

0 commit comments

Comments
 (0)