Skip to content

Commit f265d88

Browse files
author
gefeili
committed
Refactor Ascon
1 parent 5bec993 commit f265d88

File tree

9 files changed

+583
-995
lines changed

9 files changed

+583
-995
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/AsconDefaultDigest.java renamed to core/src/main/java/org/bouncycastle/crypto/digests/AsconBaseDigest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.bouncycastle.crypto.OutputLengthException;
88
import org.bouncycastle.util.Pack;
99

10-
abstract class AsconDefaultDigest
10+
abstract class AsconBaseDigest
1111
implements ExtendedDigest
1212
{
1313
protected long x0;

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import java.io.ByteArrayOutputStream;
4-
53
import org.bouncycastle.crypto.DataLengthException;
64
import org.bouncycastle.crypto.OutputLengthException;
75
import org.bouncycastle.crypto.Xof;
6+
import org.bouncycastle.util.Arrays;
87

98
/**
109
* ASCON v1.2 XOF, https://ascon.iaik.tugraz.at/ .
@@ -14,51 +13,49 @@
1413
* ASCON v1.2 XOF with reference to C Reference Impl from: https://github.com/ascon/ascon-c .
1514
*/
1615
public class AsconCxof128
17-
extends AsconDefaultDigest
16+
extends AsconBaseDigest
1817
implements Xof
1918
{
20-
public AsconCxof128()
21-
{
22-
reset();
23-
}
19+
private byte[] s;
2420

25-
private final ByteArrayOutputStream customizedString = new ByteArrayOutputStream();
26-
27-
28-
@Override
29-
public String getAlgorithmName()
21+
public AsconCxof128(byte[] s)
3022
{
31-
return "Ascon-XOF-128";
32-
}
33-
34-
35-
public void updateCustomizedString(byte in)
36-
{
37-
customizedString.write(in);
23+
if (s.length > 2048)
24+
{
25+
throw new DataLengthException("customized string is too long");
26+
}
27+
this.s = Arrays.clone(s);
28+
reset();
3829
}
3930

40-
public void updateCustomizedString(byte[] input, int inOff, int len)
31+
public AsconCxof128(byte[] s, int off, int len)
4132
{
42-
if ((inOff + len) > input.length)
33+
if ((off + len) > s.length)
4334
{
4435
throw new DataLengthException("input buffer too short");
4536
}
46-
customizedString.write(input, inOff, len);
37+
if (len > 2048)
38+
{
39+
throw new DataLengthException("customized string is too long");
40+
}
41+
this.s = Arrays.copyOfRange(s, off, off + len);
42+
reset();
43+
}
44+
@Override
45+
public String getAlgorithmName()
46+
{
47+
return "Ascon-XOF-128";
4748
}
4849

4950
@Override
5051
public int doOutput(byte[] output, int outOff, int outLen)
5152
{
53+
5254
if (CRYPTO_BYTES + outOff > output.length)
5355
{
5456
throw new OutputLengthException("output buffer is too short");
5557
}
56-
int customizedStringLen = customizedString.size();
57-
if (customizedStringLen > 2048)
58-
{
59-
throw new DataLengthException("customized string is too long");
60-
}
61-
absorb(customizedString.toByteArray(), customizedStringLen);
58+
absorb(s, s.length);
6259
absorb(buffer.toByteArray(), buffer.size());
6360
/* squeeze full output blocks */
6461
squeeze(output, outOff, outLen);
@@ -80,7 +77,6 @@ public int doFinal(byte[] output, int outOff, int outLen)
8077
@Override
8178
public void reset()
8279
{
83-
customizedString.reset();
8480
buffer.reset();
8581
/* initialize */
8682
x0 = 7445901275803737603L;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import org.bouncycastle.crypto.OutputLengthException;
43
import org.bouncycastle.util.Pack;
54

65
/** ASCON v1.2 Digest, https://ascon.iaik.tugraz.at/ .
@@ -11,7 +10,7 @@
1110
* @deprecated use Ascon Hash 256 Digest
1211
*/
1312
public class AsconDigest
14-
extends AsconDefaultDigest
13+
extends AsconBaseDigest
1514
{
1615
public enum AsconParameters
1716
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* ASCON v1.2 Digest with reference to C Reference Impl from: https://github.com/ascon/ascon-c .
99
*/
1010
public class AsconHash256Digest
11-
extends AsconDefaultDigest
11+
extends AsconBaseDigest
1212
{
1313
public AsconHash256Digest()
1414
{

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import java.io.ByteArrayOutputStream;
4-
5-
import org.bouncycastle.crypto.DataLengthException;
6-
import org.bouncycastle.crypto.OutputLengthException;
73
import org.bouncycastle.crypto.Xof;
84
import org.bouncycastle.util.Pack;
95

@@ -15,7 +11,7 @@
1511
* @deprecated Now superseded - please use AsconXof128
1612
*/
1713
public class AsconXof
18-
extends AsconDefaultDigest
14+
extends AsconBaseDigest
1915
implements Xof
2016
{
2117
public enum AsconParameters

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package org.bouncycastle.crypto.digests;
22

3-
import java.io.ByteArrayOutputStream;
4-
5-
import org.bouncycastle.crypto.DataLengthException;
6-
import org.bouncycastle.crypto.OutputLengthException;
73
import org.bouncycastle.crypto.Xof;
8-
import org.bouncycastle.util.Pack;
94

105
/**
116
* ASCON v1.2 XOF, https://ascon.iaik.tugraz.at/ .
@@ -16,7 +11,7 @@
1611
*
1712
*/
1813
public class AsconXof128
19-
extends AsconDefaultDigest
14+
extends AsconBaseDigest
2015
implements Xof
2116
{
2217
public AsconXof128()

0 commit comments

Comments
 (0)