Skip to content

Commit 9c8c34e

Browse files
author
gefeili
committed
Add more tests of Ascon. Refactor function names in Ascon.
1 parent f265d88 commit 9c8c34e

File tree

8 files changed

+406
-132
lines changed

8 files changed

+406
-132
lines changed

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,58 @@ abstract class AsconBaseDigest
2121

2222
protected final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
2323

24-
protected long ROR(long x, int n)
24+
protected long ror(long x, int n)
2525
{
2626
return x >>> n | x << (64 - n);
2727
}
2828

29-
protected void ROUND(long C)
29+
protected void round(long C)
3030
{
3131
long t0 = x0 ^ x1 ^ x2 ^ x3 ^ C ^ (x1 & (x0 ^ x2 ^ x4 ^ C));
3232
long t1 = x0 ^ x2 ^ x3 ^ x4 ^ C ^ ((x1 ^ x2 ^ C) & (x1 ^ x3));
3333
long t2 = x1 ^ x2 ^ x4 ^ C ^ (x3 & x4);
3434
long t3 = x0 ^ x1 ^ x2 ^ C ^ ((~x0) & (x3 ^ x4));
3535
long t4 = x1 ^ x3 ^ x4 ^ ((x0 ^ x4) & x1);
36-
x0 = t0 ^ ROR(t0, 19) ^ ROR(t0, 28);
37-
x1 = t1 ^ ROR(t1, 39) ^ ROR(t1, 61);
38-
x2 = ~(t2 ^ ROR(t2, 1) ^ ROR(t2, 6));
39-
x3 = t3 ^ ROR(t3, 10) ^ ROR(t3, 17);
40-
x4 = t4 ^ ROR(t4, 7) ^ ROR(t4, 41);
36+
x0 = t0 ^ ror(t0, 19) ^ ror(t0, 28);
37+
x1 = t1 ^ ror(t1, 39) ^ ror(t1, 61);
38+
x2 = ~(t2 ^ ror(t2, 1) ^ ror(t2, 6));
39+
x3 = t3 ^ ror(t3, 10) ^ ror(t3, 17);
40+
x4 = t4 ^ ror(t4, 7) ^ ror(t4, 41);
4141
}
4242

43-
protected void P(int nr)
43+
protected void p(int nr)
4444
{
4545
if (nr == 12)
4646
{
47-
ROUND(0xf0L);
48-
ROUND(0xe1L);
49-
ROUND(0xd2L);
50-
ROUND(0xc3L);
47+
round(0xf0L);
48+
round(0xe1L);
49+
round(0xd2L);
50+
round(0xc3L);
5151
}
5252
if (nr >= 8)
5353
{
54-
ROUND(0xb4L);
55-
ROUND(0xa5L);
54+
round(0xb4L);
55+
round(0xa5L);
5656
}
57-
ROUND(0x96L);
58-
ROUND(0x87L);
59-
ROUND(0x78L);
60-
ROUND(0x69L);
61-
ROUND(0x5aL);
62-
ROUND(0x4bL);
57+
round(0x96L);
58+
round(0x87L);
59+
round(0x78L);
60+
round(0x69L);
61+
round(0x5aL);
62+
round(0x4bL);
6363
}
6464

65-
protected long PAD(int i)
65+
protected long pad(int i)
6666
{
6767
return 0x01L << (i << 3);
6868
}
6969

70-
protected long LOADBYTES(final byte[] bytes, int inOff, int n)
70+
protected long loadBytes(final byte[] bytes, int inOff, int n)
7171
{
7272
return Pack.littleEndianToLong(bytes, inOff, n);
7373
}
7474

75-
protected void STOREBYTES(long w, byte[] bytes, int inOff, int n)
75+
protected void setBytes(long w, byte[] bytes, int inOff, int n)
7676
{
7777
Pack.longToLittleEndian(w, bytes, inOff, n);
7878
}
@@ -111,29 +111,29 @@ protected void absorb(byte[] input, int len)
111111
/* absorb full plaintext blocks */
112112
while (len >= ASCON_HASH_RATE)
113113
{
114-
x0 ^= LOADBYTES(input, inOff, 8);
115-
P(ASCON_PB_ROUNDS);
114+
x0 ^= loadBytes(input, inOff, 8);
115+
p(ASCON_PB_ROUNDS);
116116
inOff += ASCON_HASH_RATE;
117117
len -= ASCON_HASH_RATE;
118118
}
119119
/* absorb final plaintext block */
120-
x0 ^= LOADBYTES(input, inOff, len);
121-
x0 ^= PAD(len);
122-
P(12);
120+
x0 ^= loadBytes(input, inOff, len);
121+
x0 ^= pad(len);
122+
p(12);
123123
}
124124

125125
protected void squeeze(byte[] output, int outOff, int len)
126126
{
127127
/* squeeze full output blocks */
128128
while (len > ASCON_HASH_RATE)
129129
{
130-
STOREBYTES(x0, output, outOff, 8);
131-
P(ASCON_PB_ROUNDS);
130+
setBytes(x0, output, outOff, 8);
131+
p(ASCON_PB_ROUNDS);
132132
outOff += ASCON_HASH_RATE;
133133
len -= ASCON_HASH_RATE;
134134
}
135135
/* squeeze final output block */
136-
STOREBYTES(x0, output, outOff, len);
136+
setBytes(x0, output, outOff, len);
137137
reset();
138138
}
139139

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public AsconCxof128(byte[] s, int off, int len)
4141
this.s = Arrays.copyOfRange(s, off, off + len);
4242
reset();
4343
}
44+
45+
public AsconCxof128()
46+
{
47+
reset();
48+
}
49+
4450
@Override
4551
public String getAlgorithmName()
4652
{
@@ -55,7 +61,10 @@ public int doOutput(byte[] output, int outOff, int outLen)
5561
{
5662
throw new OutputLengthException("output buffer is too short");
5763
}
58-
absorb(s, s.length);
64+
if (s != null)
65+
{
66+
absorb(s, s.length);
67+
}
5968
absorb(buffer.toByteArray(), buffer.size());
6069
/* squeeze full output blocks */
6170
squeeze(output, outOff, outLen);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public AsconDigest(AsconParameters parameters)
4141

4242
private final String algorithmName;
4343

44-
protected long PAD(int i)
44+
protected long pad(int i)
4545
{
4646
return 0x80L << (56 - (i << 3));
4747
}
4848

49-
protected long LOADBYTES(final byte[] bytes, int inOff, int n)
49+
protected long loadBytes(final byte[] bytes, int inOff, int n)
5050
{
5151
return Pack.bigEndianToLong(bytes, inOff, n);
5252
}
5353

54-
protected void STOREBYTES( long w, byte[] bytes, int inOff,int n)
54+
protected void setBytes(long w, byte[] bytes, int inOff, int n)
5555
{
5656
Pack.longToBigEndian(w, bytes, inOff, n);
5757
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public AsconXof(AsconXof.AsconParameters parameters)
4444
private final String algorithmName;
4545

4646

47-
protected long PAD(int i)
47+
protected long pad(int i)
4848
{
4949
return 0x80L << (56 - (i << 3));
5050
}
5151

52-
protected long LOADBYTES(final byte[] bytes, int inOff, int n)
52+
protected long loadBytes(final byte[] bytes, int inOff, int n)
5353
{
5454
return Pack.bigEndianToLong(bytes, inOff, n);
5555
}
5656

57-
protected void STOREBYTES( long w, byte[] bytes, int inOff,int n)
57+
protected void setBytes(long w, byte[] bytes, int inOff, int n)
5858
{
5959
Pack.longToBigEndian(w, bytes, inOff, n);
6060
}

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public AsconAEAD128Engine()
2424
dsep = -9223372036854775808L; //0x80L << 56
2525
}
2626

27-
protected long PAD(int i)
27+
protected long pad(int i)
2828
{
2929
return 0x01L << (i << 3);
3030
}
@@ -50,21 +50,21 @@ protected void ascon_aeadinit()
5050
x2 = K1;
5151
x3 = N0;
5252
x4 = N1;
53-
P(12);
53+
p(12);
5454
x3 ^= K0;
5555
x4 ^= K1;
5656
}
5757

58-
protected void processFianlAADBlock()
58+
protected void processFinalADBBlock()
5959
{
6060
if (m_bufPos >= 8) // ASCON_AEAD_RATE == 16 is implied
6161
{
6262
x0 ^= Pack.littleEndianToLong(m_buf, 0);
63-
x1 ^= Pack.littleEndianToLong(m_buf, 8) ^ PAD(m_bufPos);
63+
x1 ^= Pack.littleEndianToLong(m_buf, 8) ^ pad(m_bufPos);
6464
}
6565
else
6666
{
67-
x0 ^= Pack.littleEndianToLong(m_buf, 0) ^ PAD(m_bufPos);
67+
x0 ^= Pack.littleEndianToLong(m_buf, 0) ^ pad(m_bufPos);
6868
}
6969
}
7070

@@ -73,7 +73,7 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
7373
if (inLen >= 8) // ASCON_AEAD_RATE == 16 is implied
7474
{
7575
long c0 = Pack.littleEndianToLong(input, 0);
76-
long c1 = Pack.littleEndianToLong(input, 0 + 8, inLen - 8);
76+
long c1 = Pack.littleEndianToLong(input, 8, inLen - 8);
7777

7878
Pack.longToLittleEndian(x0 ^ c0, output, outOff);
7979
Pack.longToLittleEndian(x1 ^ c1, output, outOff + 8, inLen - 8);
@@ -82,7 +82,7 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
8282
inLen -= 8;
8383
x1 &= -(1L << (inLen << 3));
8484
x1 |= c1;
85-
x1 ^= PAD(inLen);
85+
x1 ^= pad(inLen);
8686
}
8787
else
8888
{
@@ -93,9 +93,8 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
9393
x0 &= -(1L << (inLen << 3));
9494
x0 |= c0;
9595
}
96-
x0 ^= PAD(inLen);
96+
x0 ^= pad(inLen);
9797
}
98-
9998
finishData(State.DecFinal);
10099
}
101100

@@ -108,7 +107,7 @@ protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int o
108107
Pack.longToLittleEndian(x0, output, outOff);
109108
Pack.longToLittleEndian(x1, output, outOff + 8);
110109
inLen -= 8;
111-
x1 ^= PAD(inLen);
110+
x1 ^= pad(inLen);
112111
}
113112
else
114113
{
@@ -117,18 +116,16 @@ protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int o
117116
x0 ^= Pack.littleEndianToLong(input, 0, inLen);
118117
Pack.longToLittleEndian(x0, output, outOff, inLen);
119118
}
120-
x0 ^= PAD(inLen);
119+
x0 ^= pad(inLen);
121120
}
122-
123-
124121
finishData(State.EncFinal);
125122
}
126123

127124
private void finishData(State nextState)
128125
{
129126
x2 ^= K0;
130127
x3 ^= K1;
131-
P(12);
128+
p(12);
132129
x3 ^= K0;
133130
x4 ^= K1;
134131
m_state = nextState;

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.bouncycastle.crypto.modes.AEADCipher;
77
import org.bouncycastle.util.Arrays;
88
import org.bouncycastle.util.Longs;
9-
import org.bouncycastle.util.Pack;
109

1110
abstract class AsconBaseEngine
1211
implements AEADCipher
@@ -48,13 +47,13 @@ protected enum State
4847
protected int m_bufPos = 0;
4948
protected long dsep; //domain separation
5049

51-
protected abstract long PAD(int i);
50+
protected abstract long pad(int i);
5251

5352
protected abstract long loadBytes(byte[] in, int inOff);
5453

5554
protected abstract void setBytes(long n, byte[] bs, int off);
5655

57-
protected void ROUND(long C)
56+
protected void round(long C)
5857
{
5958
long t0 = x0 ^ x1 ^ x2 ^ x3 ^ C ^ (x1 & (x0 ^ x2 ^ x4 ^ C));
6059
long t1 = x0 ^ x2 ^ x3 ^ x4 ^ C ^ ((x1 ^ x2 ^ C) & (x1 ^ x3));
@@ -68,26 +67,26 @@ protected void ROUND(long C)
6867
x4 = t4 ^ Longs.rotateRight(t4, 7) ^ Longs.rotateRight(t4, 41);
6968
}
7069

71-
protected void P(int nr)
70+
protected void p(int nr)
7271
{
7372
if (nr == 12)
7473
{
75-
ROUND(0xf0L);
76-
ROUND(0xe1L);
77-
ROUND(0xd2L);
78-
ROUND(0xc3L);
74+
round(0xf0L);
75+
round(0xe1L);
76+
round(0xd2L);
77+
round(0xc3L);
7978
}
8079
if (nr >= 8)
8180
{
82-
ROUND(0xb4L);
83-
ROUND(0xa5L);
81+
round(0xb4L);
82+
round(0xa5L);
8483
}
85-
ROUND(0x96L);
86-
ROUND(0x87L);
87-
ROUND(0x78L);
88-
ROUND(0x69L);
89-
ROUND(0x5aL);
90-
ROUND(0x4bL);
84+
round(0x96L);
85+
round(0x87L);
86+
round(0x78L);
87+
round(0x69L);
88+
round(0x5aL);
89+
round(0x4bL);
9190
}
9291

9392
protected abstract void ascon_aeadinit();
@@ -142,8 +141,8 @@ private void finishAAD(State nextState)
142141
{
143142
case DecAad:
144143
case EncAad:
145-
processFianlAADBlock();
146-
P(nr);
144+
processFinalADBBlock();
145+
p(nr);
147146
break;
148147
default:
149148
break;
@@ -154,7 +153,7 @@ private void finishAAD(State nextState)
154153
m_state = nextState;
155154
}
156155

157-
protected abstract void processFianlAADBlock();
156+
protected abstract void processFinalADBBlock();
158157

159158
protected abstract void processFinalDecrypt(byte[] input, int inLen, byte[] output, int outOff);
160159

@@ -167,7 +166,7 @@ protected void processBufferAAD(byte[] buffer, int inOff)
167166
{
168167
x1 ^= loadBytes(buffer, 8 + inOff);
169168
}
170-
P(nr);
169+
p(nr);
171170
}
172171

173172

@@ -187,8 +186,9 @@ protected void processBufferDecrypt(byte[] buffer, int bufOff, byte[] output, in
187186
setBytes(x1 ^ t1, output, outOff + 8);
188187
x1 = t1;
189188
}
190-
P(nr);
189+
p(nr);
191190
}
191+
192192
protected void processBufferEncrypt(byte[] buffer, int bufOff, byte[] output, int outOff)
193193
{
194194
if (outOff + ASCON_AEAD_RATE > output.length)
@@ -203,8 +203,7 @@ protected void processBufferEncrypt(byte[] buffer, int bufOff, byte[] output, in
203203
x1 ^= loadBytes(buffer, bufOff + 8);
204204
setBytes(x1, output, outOff + 8);
205205
}
206-
207-
P(nr);
206+
p(nr);
208207
}
209208

210209
public void processAADByte(byte in)

0 commit comments

Comments
 (0)