Skip to content

Commit 900671b

Browse files
author
gefeili
committed
Correct AsconCXof128. Rename AsconAEAD128 and AsconHash256.
1 parent 392d5ea commit 900671b

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/AsconBaseDigest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public int doFinal(byte[] output, int outOff)
130130
return hash(output, outOff, CRYPTO_BYTES);
131131
}
132132

133-
protected void finishAbsorbing()
133+
protected void padAndAbsorb()
134134
{
135135
x0 ^= loadBytes(m_buf, 0, m_bufPos);
136136
x0 ^= pad(m_bufPos);
@@ -158,7 +158,7 @@ protected int hash(byte[] output, int outOff, int outLen)
158158
{
159159
throw new OutputLengthException("output buffer is too short");
160160
}
161-
finishAbsorbing();
161+
padAndAbsorb();
162162
/* squeeze full output blocks */
163163
squeeze(output, outOff, outLen);
164164
return outLen;

core/src/main/java/org/bouncycastle/crypto/digests/AsconCxof128.java renamed to core/src/main/java/org/bouncycastle/crypto/digests/AsconCXof128.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,43 @@
1717
* ASM implementations of Ascon (NIST SP 800-232)</a>.
1818
* </p>
1919
*/
20-
public class AsconCxof128
20+
public class AsconCXof128
2121
extends AsconBaseDigest
2222
implements Xof
2323
{
24-
private byte[] s;
2524

26-
public AsconCxof128(byte[] s)
25+
private final long z0, z1, z2, z3, z4;
26+
27+
public AsconCXof128()
2728
{
28-
if (s.length > 2048)
29-
{
30-
throw new DataLengthException("customized string is too long");
31-
}
32-
this.s = Arrays.clone(s);
33-
reset();
29+
this(new byte[0], 0, 0);
3430
}
3531

36-
public AsconCxof128(byte[] s, int off, int len)
32+
public AsconCXof128(byte[] s)
33+
{
34+
this(s, 0, s.length);
35+
}
36+
37+
public AsconCXof128(byte[] s, int off, int len)
3738
{
3839
if ((off + len) > s.length)
3940
{
4041
throw new DataLengthException("input buffer too short");
4142
}
42-
if (len > 2048)
43+
if (len > 256)
4344
{
4445
throw new DataLengthException("customized string is too long");
4546
}
46-
this.s = Arrays.copyOfRange(s, off, off + len);
47-
reset();
47+
initState(s, off, len);
48+
// NOTE: Cache the initialized state
49+
z0 = x0;
50+
z1 = x1;
51+
z2 = x2;
52+
z3 = x3;
53+
z4 = x4;
4854
}
4955

50-
public AsconCxof128()
51-
{
52-
reset();
53-
}
56+
5457

5558
protected long pad(int i)
5659
{
@@ -90,7 +93,7 @@ public int doOutput(byte[] output, int outOff, int outLen)
9093
{
9194
throw new OutputLengthException("output buffer is too short");
9295
}
93-
finishAbsorbing();
96+
padAndAbsorb();
9497
/* squeeze full output blocks */
9598
squeeze(output, outOff, outLen);
9699
return outLen;
@@ -108,16 +111,25 @@ public void reset()
108111
{
109112
super.reset();
110113
/* initialize */
114+
x0 = z0;
115+
x1 = z1;
116+
x2 = z2;
117+
x3 = z3;
118+
x4 = z4;
119+
}
120+
121+
private void initState(byte[] z, int zOff, int zLen)
122+
{
111123
x0 = 7445901275803737603L;
112124
x1 = 4886737088792722364L;
113125
x2 = -1616759365661982283L;
114126
x3 = 3076320316797452470L;
115127
x4 = -8124743304765850554L;
116-
if (s != null)
117-
{
118-
update(s, 0, s.length);
119-
finishAbsorbing();
120-
}
128+
long bitLength = ((long)zLen) << 3;
129+
Pack.longToLittleEndian(bitLength, m_buf, 0);
130+
p(12);
131+
update(z, zOff, zLen);
132+
padAndAbsorb();
121133
}
122134
}
123135

core/src/main/java/org/bouncycastle/crypto/digests/AsconHash256Digest.java renamed to core/src/main/java/org/bouncycastle/crypto/digests/AsconHash256.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* ASM implementations of Ascon (NIST SP 800-232)</a>.
1414
* </p>
1515
*/
16-
public class AsconHash256Digest
16+
public class AsconHash256
1717
extends AsconBaseDigest
1818
{
19-
public AsconHash256Digest()
19+
public AsconHash256()
2020
{
2121
reset();
2222
}

core/src/main/java/org/bouncycastle/crypto/engines/AsconAEAD128Engine.java renamed to core/src/main/java/org/bouncycastle/crypto/engines/AsconAEAD128.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
*
2222
* @version 1.3
2323
*/
24-
public class AsconAEAD128Engine
24+
public class AsconAEAD128
2525
extends AsconBaseEngine
2626
{
27-
public AsconAEAD128Engine()
27+
public AsconAEAD128()
2828
{
2929
CRYPTO_KEYBYTES = 16;
3030
CRYPTO_ABYTES = 16;

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import org.bouncycastle.crypto.InvalidCipherTextException;
1313
import org.bouncycastle.crypto.OutputLengthException;
1414
import org.bouncycastle.crypto.Xof;
15-
import org.bouncycastle.crypto.digests.AsconCxof128;
15+
import org.bouncycastle.crypto.digests.AsconCXof128;
1616
import org.bouncycastle.crypto.digests.AsconDigest;
17-
import org.bouncycastle.crypto.digests.AsconHash256Digest;
17+
import org.bouncycastle.crypto.digests.AsconHash256;
1818
import org.bouncycastle.crypto.digests.AsconXof;
1919
import org.bouncycastle.crypto.digests.AsconXof128;
20-
import org.bouncycastle.crypto.engines.AsconAEAD128Engine;
20+
import org.bouncycastle.crypto.engines.AsconAEAD128;
2121
import org.bouncycastle.crypto.engines.AsconEngine;
2222
import org.bouncycastle.crypto.modes.AEADCipher;
2323
import org.bouncycastle.crypto.params.AEADParameters;
@@ -96,7 +96,7 @@ public void performTest()
9696
@Override
9797
public AEADCipher CreateInstace()
9898
{
99-
return new AsconAEAD128Engine();
99+
return new AsconAEAD128();
100100
}
101101
});
102102

@@ -127,10 +127,10 @@ public AEADCipher CreateInstace()
127127
}
128128
});
129129

130-
DigestTest.checkDigestReset(this, new AsconHash256Digest());
130+
DigestTest.checkDigestReset(this, new AsconHash256());
131131
DigestTest.checkDigestReset(this, new AsconXof128());
132-
DigestTest.checkDigestReset(this, new AsconCxof128());
133-
DigestTest.checkDigestReset(this, new AsconCxof128());
132+
DigestTest.checkDigestReset(this, new AsconCXof128());
133+
DigestTest.checkDigestReset(this, new AsconCXof128());
134134
DigestTest.checkDigestReset(this, new AsconXof(AsconXof.AsconParameters.AsconXof));
135135
DigestTest.checkDigestReset(this, new AsconXof(AsconXof.AsconParameters.AsconXofA));
136136
DigestTest.checkDigestReset(this, new AsconDigest(AsconDigest.AsconParameters.AsconHash));
@@ -184,7 +184,7 @@ public void testBufferingEngine_asconaead128()
184184
@Override
185185
public AEADCipher createEngine()
186186
{
187-
return new AsconAEAD128Engine();
187+
return new AsconAEAD128();
188188
}
189189
});
190190
}
@@ -223,7 +223,7 @@ public void testExceptionsDigest_AsconHash256()
223223
@Override
224224
public ExtendedDigest createDigest()
225225
{
226-
return new AsconHash256Digest();
226+
return new AsconHash256();
227227
}
228228
});
229229
}
@@ -275,7 +275,7 @@ public void testExceptionsEngine_asconaead128()
275275
@Override
276276
public AEADCipher createEngine()
277277
{
278-
return new AsconAEAD128Engine();
278+
return new AsconAEAD128();
279279
}
280280
});
281281
}
@@ -327,7 +327,7 @@ public void testExceptionsXof_AsconCxof128()
327327
@Override
328328
public ExtendedDigest createDigest()
329329
{
330-
return new AsconCxof128();
330+
return new AsconCXof128();
331331
}
332332
});
333333
}
@@ -366,7 +366,7 @@ public void testParametersDigest_AsconHash256()
366366
@Override
367367
public ExtendedDigest createDigest()
368368
{
369-
return new AsconHash256Digest();
369+
return new AsconHash256();
370370
}
371371
}, 32);
372372
}
@@ -418,7 +418,7 @@ public void testParametersEngine_asconaead128()
418418
@Override
419419
public AEADCipher createEngine()
420420
{
421-
return new AsconAEAD128Engine();
421+
return new AsconAEAD128();
422422
}
423423
}, 16, 16, 16);
424424
}
@@ -456,7 +456,7 @@ public void testParametersXof_AsconCxof128()
456456
@Override
457457
public ExtendedDigest createDigest()
458458
{
459-
return new AsconCxof128();
459+
return new AsconCXof128();
460460
}
461461
}, 32);
462462
}
@@ -494,13 +494,13 @@ public void testVectorsEngine_ascon80pq()
494494
public void testVectorsEngine_asconaead128()
495495
throws Exception
496496
{
497-
implTestVectorsEngine(new AsconAEAD128Engine(), "crypto/ascon/asconaead128", "128_128");
497+
implTestVectorsEngine(new AsconAEAD128(), "crypto/ascon/asconaead128", "128_128");
498498
}
499499

500500
public void testVectorsDigest_AsconHash256()
501501
throws Exception
502502
{
503-
implTestVectorsDigest(new AsconHash256Digest(), "crypto/ascon/asconhash256", "LWC_HASH_KAT_256");
503+
implTestVectorsDigest(new AsconHash256(), "crypto/ascon/asconhash256", "LWC_HASH_KAT_256");
504504
}
505505

506506
public void testVectorsXof_AsconXof128()
@@ -653,8 +653,8 @@ private void implTestExceptionsEngine(CreateEngine operator)
653653
}
654654
else
655655
{
656-
keySize = ((AsconAEAD128Engine)ascon).getKeyBytesSize();
657-
ivSize = ((AsconAEAD128Engine)ascon).getIVBytesSize();
656+
keySize = ((AsconAEAD128)ascon).getKeyBytesSize();
657+
ivSize = ((AsconAEAD128)ascon).getIVBytesSize();
658658
}
659659

660660
int offset;
@@ -1009,8 +1009,8 @@ private void implTestParametersEngine(CreateEngine operator, int keySize, int iv
10091009
}
10101010
else
10111011
{
1012-
keySize2 = ((AsconAEAD128Engine)ascon).getKeyBytesSize();
1013-
ivSize2 = ((AsconAEAD128Engine)ascon).getIVBytesSize();
1012+
keySize2 = ((AsconAEAD128)ascon).getKeyBytesSize();
1013+
ivSize2 = ((AsconAEAD128)ascon).getIVBytesSize();
10141014
}
10151015
if (keySize2 != keySize)
10161016
{
@@ -1221,8 +1221,8 @@ private static void initEngine(AEADCipher ascon, boolean forEncryption)
12211221
}
12221222
else
12231223
{
1224-
keySize = ((AsconAEAD128Engine)ascon).getKeyBytesSize();
1225-
ivSize = ((AsconAEAD128Engine)ascon).getIVBytesSize();
1224+
keySize = ((AsconAEAD128)ascon).getKeyBytesSize();
1225+
ivSize = ((AsconAEAD128)ascon).getIVBytesSize();
12261226
}
12271227
int macSize = ivSize * 8;
12281228

0 commit comments

Comments
 (0)